Skip to content

Commit 3386966

Browse files
authored
Partial move to SwiftUI lifecycle (#1152)
* Start of reimplement Signed-off-by: Wouter01 <[email protected]> * Added more commands Signed-off-by: Wouter01 <[email protected]> * Moved WelcomeWindow to SwiftUI Window Signed-off-by: Wouter01 <[email protected]> * Moved aboutview to SwiftUI Window Signed-off-by: Wouter01 <[email protected]> * Fixed resizing welcome window Signed-off-by: Wouter01 <[email protected]> * removed unused window controllers Signed-off-by: Wouter01 <[email protected]> * Removed unused window controller Signed-off-by: Wouter01 <[email protected]> * changed hiddenOption to hidden Signed-off-by: Wouter01 <[email protected]> * Fixed NSDocumentController tied to SwiftUI Signed-off-by: Wouter01 <[email protected]> * removed unused code Signed-off-by: Wouter01 <[email protected]> * Added Navigate, Source Control, Help commands Signed-off-by: Wouter01 <[email protected]> * Added some actions to menu items Signed-off-by: Wouter01 <[email protected]> * small improvements to menu items Signed-off-by: Wouter01 <[email protected]> * removed MainMenu.xib Signed-off-by: Wouter01 <[email protected]> * Cleaned up code Signed-off-by: Wouter01 <[email protected]> * Cleaned up code Signed-off-by: Wouter01 <[email protected]> * Cleaned up code Signed-off-by: Wouter01 <[email protected]> * Small fix Signed-off-by: Wouter01 <[email protected]> * changed requested things Signed-off-by: Wouter01 <[email protected]> * Fixed lint warning Signed-off-by: Wouter01 <[email protected]> * Small changes Signed-off-by: Wouter01 <[email protected]> --------- Signed-off-by: Wouter01 <[email protected]>
1 parent 55c45cc commit 3386966

29 files changed

+868
-1114
lines changed

CodeEdit.xcodeproj/project.pbxproj

Lines changed: 88 additions & 12 deletions
Large diffs are not rendered by default.

CodeEdit/AppDelegate.swift

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,23 @@ import SwiftUI
99
import Preferences
1010
import CodeEditSymbols
1111

12-
final class CodeEditApplication: NSApplication {
13-
let strongDelegate = AppDelegate()
14-
15-
override init() {
16-
super.init()
17-
self.delegate = strongDelegate
18-
}
19-
20-
@available(*, unavailable)
21-
required init?(coder: NSCoder) {
22-
fatalError("init(coder:) has not been implemented")
23-
}
24-
25-
}
26-
27-
@main
2812
final class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject {
2913
var updater: SoftwareUpdater = SoftwareUpdater()
3014

31-
func applicationWillFinishLaunching(_ notification: Notification) {
32-
_ = CodeEditDocumentController.shared
33-
enableWindowSizeSaveOnQuit()
34-
}
35-
3615
func applicationDidFinishLaunching(_ notification: Notification) {
16+
enableWindowSizeSaveOnQuit()
3717
AppPreferencesModel.shared.preferences.general.appAppearance.applyAppearance()
3818
checkForFilesToOpen()
3919

20+
NSApp.closeWindow(.welcome, .about)
21+
4022
DispatchQueue.main.async {
4123
var needToHandleOpen = true
4224

4325
// If no windows were reopened by NSQuitAlwaysKeepsWindows, do default behavior.
44-
if !NSApp.windows.isEmpty {
26+
// Non-WindowGroup SwiftUI Windows are still in NSApp.windows when they are closed,
27+
// So we need to think about those.
28+
if NSApp.windows.count > NSApp.openSwiftUIWindows {
4529
needToHandleOpen = false
4630
}
4731

@@ -71,6 +55,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject {
7155
}
7256

7357
func applicationWillTerminate(_ aNotification: Notification) {
58+
7459
}
7560

7661
func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool {
@@ -96,7 +81,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject {
9681

9782
switch behavior {
9883
case .welcome:
99-
openWelcome(self)
84+
NSApp.openWindow(.welcome)
10085
case .openPanel:
10186
CodeEditDocumentController.shared.openDocument(self)
10287
case .newDocument:
@@ -148,22 +133,22 @@ final class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject {
148133
return .terminateNow
149134
}
150135

136+
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
137+
false
138+
}
139+
151140
// MARK: - Open windows
152141

153142
@IBAction func openPreferences(_ sender: Any) {
154143
preferencesWindowController.show()
155144
}
156145

157146
@IBAction func openWelcome(_ sender: Any) {
158-
if tryFocusWindow(of: WelcomeWindowView.self) { return }
159-
160-
WelcomeWindowView.openWelcomeWindow()
147+
NSApp.openWindow(.welcome)
161148
}
162149

163150
@IBAction func openAbout(_ sender: Any) {
164-
if tryFocusWindow(of: AboutView.self) { return }
165-
166-
AboutView().showWindow(width: 530, height: 220)
151+
NSApp.openWindow(.about)
167152
}
168153

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

CodeEdit/CodeEditApp.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// CodeEditApp.swift
3+
// CodeEdit
4+
//
5+
// Created by Wouter Hennen on 11/03/2023.
6+
//
7+
8+
import SwiftUI
9+
10+
@main
11+
struct CodeEditApp: App {
12+
@NSApplicationDelegateAdaptor var appdelegate: AppDelegate
13+
14+
init() {
15+
_ = CodeEditDocumentController.shared
16+
NSMenuItem.swizzle()
17+
}
18+
19+
var body: some Scene {
20+
21+
WelcomeWindow()
22+
23+
AboutWindow()
24+
25+
Settings {
26+
VStack {
27+
Text("Hello world!")
28+
}
29+
.frame(maxWidth: .infinity, maxHeight: .infinity)
30+
}
31+
.defaultSize(width: 500, height: 500)
32+
.commands {
33+
CodeEditCommands()
34+
}
35+
}
36+
}

CodeEdit/Features/About/Views/AboutView.swift

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ enum AboutMode: String, CaseIterable {
1616
public struct AboutView: View {
1717
@Environment(\.openURL) private var openURL
1818
@Environment(\.colorScheme) private var colorScheme
19+
@Environment(\.dismiss) private var dismiss
1920

2021
@State var aboutMode: AboutMode = .about
2122

@@ -40,13 +41,21 @@ public struct AboutView: View {
4041
// if anyone knows of a better way to do this feel free to refactor
4142
.background(.regularMaterial.opacity(0))
4243
.background(EffectView(.popover, blendingMode: .behindWindow).ignoresSafeArea())
43-
}
44-
45-
public func showWindow(width: CGFloat, height: CGFloat) {
46-
AboutViewWindowController(
47-
view: self,
48-
size: NSSize(width: width, height: height)
49-
)
50-
.showWindow(nil)
44+
.background {
45+
Button("") {
46+
dismiss()
47+
}
48+
.keyboardShortcut(.escape, modifiers: [])
49+
.hidden()
50+
}
51+
.task {
52+
if let window = NSApp.findWindow(.about) {
53+
window.styleMask = [.closable, .fullSizeContentView, .titled, .nonactivatingPanel]
54+
window.standardWindowButton(.miniaturizeButton)?.isHidden = true
55+
window.standardWindowButton(.zoomButton)?.isHidden = true
56+
window.backgroundColor = .gray.withAlphaComponent(0.15)
57+
window.isMovableByWindowBackground = true
58+
}
59+
}
5160
}
5261
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// AboutWindow.swift
3+
// CodeEdit
4+
//
5+
// Created by Wouter Hennen on 14/03/2023.
6+
//
7+
8+
import SwiftUI
9+
10+
struct AboutWindow: Scene {
11+
var body: some Scene {
12+
Window("", id: SceneID.about.rawValue) {
13+
AboutView()
14+
}
15+
.defaultSize(width: 530, height: 220)
16+
.windowResizability(.contentSize)
17+
.windowStyle(.hiddenTitleBar)
18+
}
19+
}

CodeEdit/Features/About/Views/AboutWindowController.swift

Lines changed: 0 additions & 68 deletions
This file was deleted.

CodeEdit/Features/Contributors/ContributorsWindowController.swift

Lines changed: 0 additions & 54 deletions
This file was deleted.

CodeEdit/Features/Documents/Controllers/CodeEditDocumentController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ final class CodeEditDocumentController: NSDocumentController {
7878
super.removeDocument(document)
7979

8080
if CodeEditDocumentController.shared.documents.isEmpty {
81-
WelcomeWindowView.openWelcomeWindow()
81+
NSApp.openWindow(.welcome)
8282
}
8383
}
8484

CodeEdit/Features/Documents/Controllers/CodeEditSplitViewController.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@
66
//
77

88
import Cocoa
9+
import SwiftUI
10+
11+
struct CodeEditSplitView: NSViewControllerRepresentable {
12+
let controller: NSSplitViewController
13+
14+
func makeNSViewController(context: Context) -> NSSplitViewController {
15+
controller
16+
}
17+
18+
func updateNSViewController(_ nsViewController: NSSplitViewController, context: Context) {}
19+
}
920

1021
private extension CGFloat {
1122
static let snapWidth: CGFloat = 272

CodeEdit/Features/Documents/Controllers/CodeEditWindowController.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,20 @@ final class CodeEditWindowController: NSWindowController, NSToolbarDelegate {
1717
var quickOpenPanel: OverlayPanel?
1818
var commandPalettePanel: OverlayPanel?
1919

20-
private var splitViewController: NSSplitViewController! {
21-
get { contentViewController as? NSSplitViewController }
22-
set { contentViewController = newValue }
23-
}
20+
private var splitViewController: NSSplitViewController!
2421

2522
init(window: NSWindow, workspace: WorkspaceDocument) {
2623
super.init(window: window)
2724
self.workspace = workspace
2825

2926
setupSplitView(with: workspace)
27+
28+
let view = CodeEditSplitView(controller: splitViewController).ignoresSafeArea()
29+
30+
// An NSHostingController is used, so the root viewController of the window is a SwiftUI-managed one.
31+
// This allows us to use some SwiftUI features, like focusedSceneObject.
32+
contentViewController = NSHostingController(rootView: view)
33+
3034
setupToolbar()
3135
registerCommands()
3236
}

0 commit comments

Comments
 (0)