Giter VIP home page Giter VIP logo

macpreviewutils's Introduction

MacPreviewUtils

This package includes a few tools that I use all the time when developing macOS apps, whether using SwiftUI or AppKit. These tools help make SwiftUI previews more useful for Mac app developers.

Translucency Fix

By default, Material and other views that rely on an app being active in order to render properly won't look right in SwiftUI previews, making it difficult to iterate on detailed designs that use such effects.

Just by linking against the MacPreviewUtils library, you'll get an automatic fix for translucency in SwiftUI previews.

Example:

No code needed, just link your app target (or one of the targets it links against) to MacPreviewUtils.

Material preview

Am I a SwiftUI Preview?

The library includes a couple of extensions on ProcessInfo that can be used at runtime to check whether the process is running in a SwiftUI preview. This should be used sparingly, but it can be extremely useful to adapt certain app behaviors that can be problematic when running in SwiftUI previews.

Example:

func doSomethingThatBreaksSwiftUIPreviews() {
    guard !ProcessInfo.isSwiftUIPreview else { return }
    
    // ...
}

Pin SwiftUI Preview to a Specific Display

Sometimes you might want to be looking at your SwiftUI preview in an external display, such as an iPad connected via Sidecar. This library includes a modifier that can do just that.

Example:

struct MyView_Previews: PreviewProvider {
    static var previews: some View {
        /// This preview will show up on the connected Sidecar display.
        MyView()
            .pin(to: .sidecarDisplay, alignment: .center, options: [.interactiveOnly])
    }
}

Debug Previews Using print()

Breakpoints are great for debugging an app while it's running, and sometimes print() statements may also be a good way to debug certain values over time. Unfortunately, while running in Xcode previews, there's no easy way to view your app's standard output, unless you're using OSLog/NSLog and filtering in the Console app.

This library includes a modifier that displays a little console window while running an Xcode preview, allowing you to debug your previews using print statements.

The previewConsole() may also be combined with the pin(to:) modifier mentioned above, in which case it'll follow the display and position selection from that modifier.

struct MyView_Previews: PreviewProvider {
    static var previews: some View {
        MyView()
            .previewConsole()
    }
}

Note: this modifier will only show the app's standard output, so it won't display messages logged with os_log or Logger. Support for the unified logging system and stderr is planned.

Preview Console Demo

Will this mess up my app when debugging or in production?

Nope!

Most of the code in this library is in #if DEBUG/#endif statements, so it won't even be included in release builds of the app.

The modifiers included are designed for use only in SwiftUI previews, so you should not use them directly on view implementations. However, they all check if the code is running in a SwiftUI preview before doing anything, so that regular debug builds are not affected.

Be sure to include #if DEBUG/#endif around your SwiftUI previews that use this library as well, in order to prevent compiler errors when building for release.

macpreviewutils's People

Contributors

insidegui avatar

Stargazers

adamz avatar Charlie Cortial avatar Deandra avatar MetaSky avatar Cole Townsend avatar Mack avatar immateria avatar  avatar Daniel Boroujerdi avatar NH avatar Andrea Mario Lufino avatar David Dennison avatar Sergi avatar Liam Ronan avatar Igor Muzyka avatar Chris avatar Vinh Nguyen avatar  avatar  avatar  avatar Pete Schaffner avatar Luitbald avatar Jahidul Islam avatar Dronca Raul avatar Danilo Requena avatar Tony Arnold avatar Firdavs Khaydarov avatar Sapozhnik Ivan avatar Eduardo Callado avatar Andreas Koslowski avatar Lucas Eduardo Schlögl avatar Harlans avatar Adam Bell avatar Alex Plekhanov avatar  avatar Pedro Paulo Amorim avatar Wayne Dahlberg avatar Feras Alnatsheh avatar Sam Burkhard avatar Mihaela Mihaljevic avatar Aleksei Kakoulin avatar Edward Valentini avatar André Neves avatar Igor Tarasenko avatar Mas Kipli avatar Karol Bielski avatar Rinat Abidullin avatar  avatar Tom avatar Eric Summers avatar venj avatar O/I avatar Chris Zelazo avatar Felipe Espinoza avatar Christoffer Winterkvist avatar Manuel Kehl avatar Ehab Abdalaa avatar  avatar Kyle Howells avatar Alex  avatar Sree Sai Raghava avatar Bastian Kusserow avatar Fernando Bunn avatar  avatar Tanner Stokes avatar Dan Korkelia avatar Pavel Sorokin avatar Amin Amjadi avatar TTMedia Services Inc. avatar  avatar Muhammad Umer avatar Sören Gade avatar Jeremy Legendre avatar Michal Zelinka avatar Yılmaz Yağız Dokumacı avatar Kotaro Suto avatar HZ.Liu avatar Jan-Willem Oskam avatar Tim Oliver avatar  avatar Vinícius Chagas avatar  avatar Melvin Gundlach avatar  avatar Alexandre Morgado avatar Thomas Harr avatar Ricardo Magalhães avatar Pavel Alexeev avatar Daniel Aditya Istyana avatar Daniel Calderon avatar Objective_Charm avatar  avatar Will Jessop avatar Homero Oliveira avatar Victor Baro avatar Robert Manson avatar Chris avatar  avatar Nikita avatar Bobby Bobak avatar

Watchers

 avatar  avatar Marcelo Perretta avatar Gabriel Jacoby-Cooper avatar  avatar

Forkers

mumer92 mihaelamj

macpreviewutils's Issues

ConsoleView & ProcessPipe usage outside of SwiftUI Previews?

Hi, this is a question, not an issue.

I borrowed the ConsoleView & ProcessPipe to use in SwiftUI macOS App which i run with swift run ... for debug purposes.

ConsoleSearchFiled obviously not working in this setup, because keyboard is captured by the terminal, not the process, but thats just a note not of any importance.

ProcessPipe though has the following code in read method:

if !ProcessInfo.isSwiftUIPreview {
    assertionFailure("\(source) streaming failed: \(error)")
}

It naturally got overwritten with print(error) which i guess is a bad idea.

At some point I noticed that if i print(...) something with multiple lines it sometimes gets corrupted on the receiving side.

I understand this wasn't designed for how i use it at all.

My question is what changes would you suggest to get rid of that multiline string corruption issue?
I suppose it has to do with how we read the Pipe.
A nudge in the right direction would be much appreciated.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.