Created
March 17, 2023 00:44
-
-
Save flbn/42ac54f9bccef4a569417adf6b17a825 to your computer and use it in GitHub Desktop.
hide toolbar (focus mode)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private struct IsFocusedKey: EnvironmentKey { | |
| static let defaultValue: Bool = false | |
| } | |
| extension EnvironmentValues { | |
| var isFocused: Bool { | |
| get { self[IsFocusedKey.self] } | |
| set { self[IsFocusedKey.self] = newValue } | |
| } | |
| } | |
| @main | |
| struct NousApp: App { | |
| @State private var isFocused = false | |
| var body: some Scene { | |
| DocumentGroup(newDocument: { TextDocument() }) { file in | |
| ContentView() | |
| .environment(\.isFocused, isFocused) | |
| } | |
| .commands { | |
| CommandGroup(before: CommandGroupPlacement.appTermination) { | |
| Button("Toggle Focus Mode") { | |
| isFocused.toggle() | |
| toggleWindowStyle() | |
| } | |
| .keyboardShortcut(KeyEquivalent("f"), modifiers: [.command]) | |
| } | |
| } | |
| } | |
| func toggleWindowStyle() { | |
| // NOTE: animation isn't working. not sure how iA Writer is doing it but i'm still experimenting | |
| withAnimation(.spring(response: 0.5, dampingFraction: 0.5)) { | |
| for window in NSApplication.shared.windows { | |
| window.standardWindowButton(NSWindow.ButtonType.closeButton)!.isHidden = isFocused | |
| window.standardWindowButton(NSWindow.ButtonType.miniaturizeButton)!.isHidden = isFocused | |
| window.standardWindowButton(NSWindow.ButtonType.zoomButton)!.isHidden = isFocused | |
| window.titleVisibility = isFocused ? .hidden : .visible | |
| window.titlebarAppearsTransparent = isFocused | |
| window.toolbarStyle = isFocused ? .unifiedCompact : .automatic | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment