Skip to content

Commit c4f114c

Browse files
authored
Fix: Window Actions (#1627)
* - Fix #1625 - Refactor the code to be consistent - Remove unnecessary code * Use SceneID enum instead of hard-coded settings
1 parent f22f1d2 commit c4f114c

File tree

13 files changed

+30
-64
lines changed

13 files changed

+30
-64
lines changed

CodeEdit/AppDelegate.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import CodeEditSymbols
1212
final class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject {
1313
private let updater = SoftwareUpdater()
1414

15+
@Environment(\.openWindow)
16+
private var openWindow
17+
1518
func applicationDidFinishLaunching(_ notification: Notification) {
1619
enableWindowSizeSaveOnQuit()
1720
Settings.shared.preferences.general.appAppearance.applyAppearance()
@@ -79,7 +82,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject {
7982
switch behavior {
8083
case .welcome:
8184
if !tryFocusWindow(id: .welcome) {
82-
NSApp.openWindow(.welcome)
85+
openWindow(sceneID: .welcome)
8386
}
8487
case .openPanel:
8588
CodeEditDocumentController.shared.openDocument(self)
@@ -141,11 +144,11 @@ final class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject {
141144
// MARK: - Open windows
142145

143146
@IBAction private func openWelcome(_ sender: Any) {
144-
NSApp.openWindow(.welcome)
147+
openWindow(sceneID: .welcome)
145148
}
146149

147150
@IBAction private func openAbout(_ sender: Any) {
148-
NSApp.openWindow(.about)
151+
openWindow(sceneID: .about)
149152
}
150153

151154
@IBAction func openFeedback(_ sender: Any) {

CodeEdit/CodeEditApp.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ struct CodeEditApp: App {
3737
NSDocumentGroup(for: WorkspaceDocument.self) { workspace in
3838
WindowSplitView(workspace: workspace)
3939
} defaultAction: {
40-
openWindow(id: SceneID.welcome.rawValue)
40+
openWindow(sceneID: .welcome)
4141
}
4242
.register(.document(WorkspaceDocument.self)) // Required to make transition modifier work
4343
.transition(.documentWindow)

CodeEdit/Features/Documents/Controllers/CodeEditDocumentController.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66
//
77

88
import Cocoa
9+
import SwiftUI
910

1011
final class CodeEditDocumentController: NSDocumentController {
12+
@Environment(\.openWindow)
13+
private var openWindow
14+
1115
lazy var fileManager: FileManager = {
1216
FileManager.default
1317
}()
@@ -81,7 +85,7 @@ final class CodeEditDocumentController: NSDocumentController {
8185
switch Settings[\.general].reopenWindowAfterClose {
8286
case .showWelcomeWindow:
8387
// Opens the welcome window
84-
NSApp.openWindow(.welcome)
88+
openWindow(sceneID: .welcome)
8589
case .quit:
8690
// Quits CodeEdit
8791
NSApplication.shared.terminate(nil)

CodeEdit/Features/Settings/SettingsWindow.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ struct SettingsWindow: Scene {
1111
private let updater = SoftwareUpdater()
1212

1313
var body: some Scene {
14-
Window("Settings", id: "settings") {
14+
Window("Settings", id: SceneID.settings.rawValue) {
1515
SettingsView(updater: updater)
1616
.frame(minWidth: 715, maxWidth: 715)
1717
.task {
18-
let window = NSApp.windows.first { $0.identifier?.rawValue == "settings" }!
18+
let window = NSApp.windows.first { $0.identifier?.rawValue == SceneID.settings.rawValue }!
1919
window.titlebarAppearsTransparent = true
2020
}
2121
}

CodeEdit/Features/Settings/Views/View+ConstrainHeightToWindow.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import SwiftUI
99

1010
extension NSWindow {
1111
var isSettingsWindow: Bool {
12-
self.identifier?.rawValue == "settings"
12+
self.identifier?.rawValue == SceneID.settings.rawValue
1313
}
1414
}
1515

CodeEdit/Features/Settings/Views/View+HideSidebarToggle.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ struct HideSidebarToggleViewModifier: ViewModifier {
1717
func body(content: Content) -> some View {
1818
content
1919
.task {
20-
let window = NSApp.windows.first { $0.identifier?.rawValue == "settings" }!
20+
let window = NSApp.windows.first { $0.identifier?.rawValue == SceneID.settings.rawValue }!
2121
let sidebaritem = "com.apple.SwiftUI.navigationSplitView.toggleSidebar"
2222
let index = window.toolbar?.items.firstIndex { $0.itemIdentifier.rawValue == sidebaritem }
2323
if let index {

CodeEdit/Features/Welcome/Views/WelcomeWindow.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ struct WelcomeWindow: Scene {
4646
dismiss()
4747
CodeEditDocumentController.shared.openDocument(
4848
onCompletion: { _, _ in opened() },
49-
onCancel: { openWindow(id: SceneID.welcome.rawValue) }
49+
onCancel: { openWindow(sceneID: .welcome) }
5050
)
5151
}
5252
} newDocument: {

CodeEdit/Features/WindowCommands/ExtensionCommands.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ struct ExtensionCommands: Commands {
1717
var body: some Commands {
1818
CommandMenu("Extensions") {
1919
Button("Open Extensions Window") {
20-
openWindow(id: SceneID.extensions.rawValue)
20+
openWindow(sceneID: .extensions)
2121
}
2222
}
2323
}

CodeEdit/Features/WindowCommands/MainCommands.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ struct MainCommands: Commands {
1515
var body: some Commands {
1616
CommandGroup(replacing: .appInfo) {
1717
Button("About CodeEdit") {
18-
openWindow(id: SceneID.about.rawValue)
18+
openWindow(sceneID: .about)
1919
}
2020

2121
Button("Check for updates...") {
@@ -25,7 +25,7 @@ struct MainCommands: Commands {
2525

2626
CommandGroup(replacing: .appSettings) {
2727
Button("Settings...") {
28-
openWindow(id: "settings")
28+
openWindow(sceneID: .settings)
2929
}
3030
.keyboardShortcut(",")
3131
}

CodeEdit/Features/WindowCommands/Utils/CommandsFixes.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ extension EventModifiers {
1212
}
1313

1414
extension NSMenuItem {
15-
16-
/// Helper variable to allow opening SwiftUI windows from anywhere in the app.
17-
/// Don't use this, unless you know what you're doing.
18-
static var openWindowAction: (() -> (SceneID?, (any Codable & Hashable)?))?
19-
2015
@objc
2116
fileprivate func fixAlternate(_ newValue: NSEvent.ModifierFlags) {
2217

0 commit comments

Comments
 (0)