From 733408a788bc9c607eb75b85db6be962079d65da Mon Sep 17 00:00:00 2001 From: Underthestars-zhy Date: Mon, 21 Mar 2022 19:11:21 +0800 Subject: [PATCH 1/7] Fix the bugs of setting view and change to swift lifecycle --- CodeEdit.xcodeproj/project.pbxproj | 12 +++-- .../xcshareddata/xcschemes/CodeEdit.xcscheme | 7 +++ CodeEdit/AppDelegate.swift | 44 +++++++++---------- CodeEdit/CodeEdit.swift | 24 ++++++++++ CodeEdit/Settings/GeneralSettingsView.swift | 5 +++ CodeEdit/Settings/SettingsView.swift | 2 - 6 files changed, 66 insertions(+), 28 deletions(-) create mode 100644 CodeEdit/CodeEdit.swift diff --git a/CodeEdit.xcodeproj/project.pbxproj b/CodeEdit.xcodeproj/project.pbxproj index c5a9464de4..252a7ee516 100644 --- a/CodeEdit.xcodeproj/project.pbxproj +++ b/CodeEdit.xcodeproj/project.pbxproj @@ -35,6 +35,7 @@ 34EE19BE27E0469C00F152CE /* BlurView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 34EE19BD27E0469C00F152CE /* BlurView.swift */; }; 5C403B8F27E20F8000788241 /* WorkspaceClient in Frameworks */ = {isa = PBXBuildFile; productRef = 5C403B8E27E20F8000788241 /* WorkspaceClient */; }; 5CF38A5E27E48E6C0096A0F7 /* CodeFile in Frameworks */ = {isa = PBXBuildFile; productRef = 5CF38A5D27E48E6C0096A0F7 /* CodeFile */; }; + 90571F6027E89210006E606C /* CodeEdit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 90571F5F27E89210006E606C /* CodeEdit.swift */; }; B658FB3427DA9E1000EA4DBD /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = B658FB3327DA9E1000EA4DBD /* Assets.xcassets */; }; B658FB3727DA9E1000EA4DBD /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = B658FB3627DA9E1000EA4DBD /* Preview Assets.xcassets */; }; B65E614627E6765D00255275 /* Introspect in Frameworks */ = {isa = PBXBuildFile; productRef = B65E614527E6765D00255275 /* Introspect */; }; @@ -96,6 +97,7 @@ 34EE19BD27E0469C00F152CE /* BlurView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BlurView.swift; sourceTree = ""; }; 5E3C6A3427E72AE000A7CA0D /* fr */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = fr; path = fr.lproj/Localizable.strings; sourceTree = ""; }; 70F2E28327E848720002BA81 /* zh-Hans */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hans"; path = "zh-Hans.lproj/Localizable.strings"; sourceTree = ""; }; + 90571F5F27E89210006E606C /* CodeEdit.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CodeEdit.swift; sourceTree = ""; }; B658FB2C27DA9E0F00EA4DBD /* CodeEdit.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = CodeEdit.app; sourceTree = BUILT_PRODUCTS_DIR; }; B658FB3127DA9E0F00EA4DBD /* WorkspaceView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WorkspaceView.swift; sourceTree = ""; }; B658FB3327DA9E1000EA4DBD /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; @@ -267,6 +269,7 @@ B658FB3827DA9E1000EA4DBD /* CodeEdit.entitlements */, B658FB3527DA9E1000EA4DBD /* Preview Content */, 043C321927E32295006AE443 /* MainMenu.xib */, + 90571F5F27E89210006E606C /* CodeEdit.swift */, ); path = CodeEdit; sourceTree = ""; @@ -492,6 +495,7 @@ files = ( 2B7A583527E4BA0100D25D4E /* AppDelegate.swift in Sources */, 2875A46D27E3BE5B007805F8 /* BreadcrumbsView.swift in Sources */, + 90571F6027E89210006E606C /* CodeEdit.swift in Sources */, 0485EB1D27E7338100138301 /* QuickOpenItem.swift in Sources */, 04540D5B27DD08C300E91B77 /* SettingsView.swift in Sources */, D72E1A8927E44D7C00EB11B9 /* WelcomeWindowView.swift in Sources */, @@ -693,12 +697,12 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIconDev; ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; CODE_SIGN_ENTITLEMENTS = CodeEdit/CodeEdit.entitlements; - CODE_SIGN_IDENTITY = "-"; + CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; CURRENT_PROJECT_VERSION = 1; DEVELOPMENT_ASSET_PATHS = "\"CodeEdit/Preview Content\""; - DEVELOPMENT_TEAM = ""; + DEVELOPMENT_TEAM = 44BN78992X; ENABLE_HARDENED_RUNTIME = YES; ENABLE_PREVIEWS = YES; GENERATE_INFOPLIST_FILE = YES; @@ -724,12 +728,12 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIconDev; ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; CODE_SIGN_ENTITLEMENTS = CodeEdit/CodeEdit.entitlements; - CODE_SIGN_IDENTITY = "-"; + CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; CURRENT_PROJECT_VERSION = 1; DEVELOPMENT_ASSET_PATHS = "\"CodeEdit/Preview Content\""; - DEVELOPMENT_TEAM = ""; + DEVELOPMENT_TEAM = 44BN78992X; ENABLE_HARDENED_RUNTIME = YES; ENABLE_PREVIEWS = YES; GENERATE_INFOPLIST_FILE = YES; diff --git a/CodeEdit.xcodeproj/xcshareddata/xcschemes/CodeEdit.xcscheme b/CodeEdit.xcodeproj/xcshareddata/xcschemes/CodeEdit.xcscheme index d92ea28491..0ca4d11bbe 100644 --- a/CodeEdit.xcodeproj/xcshareddata/xcschemes/CodeEdit.xcscheme +++ b/CodeEdit.xcodeproj/xcshareddata/xcschemes/CodeEdit.xcscheme @@ -70,6 +70,13 @@ ReferencedContainer = "container:CodeEdit.xcodeproj"> + + + + ) != nil - }).first { - window.makeKeyAndOrderFront(self) - return - } - - let window = NSWindow( - contentRect: NSRect(x: 0, y: 0, width: 500, height: 400), - styleMask: [.titled, .closable], - backing: .buffered, defer: false) - window.center() - window.toolbar = NSToolbar() - window.title = "Settings" - window.toolbarStyle = .unifiedCompact - _ = NSWindowController(window: window) - let contentView = SettingsView() - window.contentView = NSHostingView(rootView: contentView) - window.makeKeyAndOrderFront(sender) - } +// @IBAction func openPreferences(_ sender: Any) { +// if let window = NSApp.windows.filter({ window in +// return (window.contentView as? NSHostingView) != nil +// }).first { +// window.makeKeyAndOrderFront(self) +// return +// } +// +// let window = NSWindow( +// contentRect: NSRect(x: 0, y: 0, width: 500, height: 400), +// styleMask: [.titled, .closable], +// backing: .buffered, defer: false) +// window.center() +// window.toolbar = NSToolbar() +// window.title = "Settings" +// window.toolbarStyle = .unifiedCompact +// _ = NSWindowController(window: window) +// let contentView = SettingsView() +// window.contentView = NSHostingView(rootView: contentView) +// window.makeKeyAndOrderFront(sender) +// } @IBAction func openWelcome(_ sender: Any) { if let window = NSApp.windows.filter({ window in diff --git a/CodeEdit/CodeEdit.swift b/CodeEdit/CodeEdit.swift new file mode 100644 index 0000000000..d9ea735980 --- /dev/null +++ b/CodeEdit/CodeEdit.swift @@ -0,0 +1,24 @@ +// +// CodeEdit.swift +// CodeEdit +// +// Created by 朱浩宇 on 2022/3/21. +// + +import SwiftUI + +@main +struct CodeEdit: App { + @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate + + var body: some Scene { + Settings { + SettingsView() + .frame(width: 500) + .task { + try? await Task.sleep(nanoseconds: NSEC_PER_MSEC) + NSApp.keyWindow?.center() + } + } + } +} diff --git a/CodeEdit/Settings/GeneralSettingsView.swift b/CodeEdit/Settings/GeneralSettingsView.swift index 91d07a9f17..f13fa0e0e5 100644 --- a/CodeEdit/Settings/GeneralSettingsView.swift +++ b/CodeEdit/Settings/GeneralSettingsView.swift @@ -30,6 +30,7 @@ struct GeneralSettingsView: View { .onChange(of: appearance) { tag in tag.applyAppearance() } + .fixedSize() Picker("File Icon Style".localized(), selection: $fileIconStyle) { Text("Color".localized()) @@ -37,6 +38,7 @@ struct GeneralSettingsView: View { Text("Monochrome".localized()) .tag(FileIconStyle.monochrome) } + .fixedSize() Picker("Reopen Behavior".localized(), selection: $reopenBehavior) { Text("Welcome Screen".localized()) @@ -47,6 +49,7 @@ struct GeneralSettingsView: View { Text("New Document".localized()) .tag(ReopenBehavior.newDocument) } + .fixedSize() Picker("Editor Theme".localized(), selection: $editorTheme) { Text("Atelier Savanna (Auto)") @@ -63,7 +66,9 @@ struct GeneralSettingsView: View { Text("Ocean") .tag(CodeEditor.ThemeName.ocean) } + .fixedSize() } + .frame(width: 450) .padding() } } diff --git a/CodeEdit/Settings/SettingsView.swift b/CodeEdit/Settings/SettingsView.swift index 6549f479a9..c38478e7f1 100644 --- a/CodeEdit/Settings/SettingsView.swift +++ b/CodeEdit/Settings/SettingsView.swift @@ -15,8 +15,6 @@ struct SettingsView: View { Label("General".localized(), systemImage: "gearshape") } } - .padding() - .frame(width: 450, height: 250) } } From 2ceedf63713ebba0124ba9e1c8cdd78197b9e188 Mon Sep 17 00:00:00 2001 From: Underthestars-zhy Date: Mon, 21 Mar 2022 20:09:18 +0800 Subject: [PATCH 2/7] Fix the problem that main menu doesn't show --- CodeEdit/AppDelegate.swift | 80 +++++++++++++++++++------------------- CodeEdit/MainMenu.xib | 4 +- 2 files changed, 43 insertions(+), 41 deletions(-) diff --git a/CodeEdit/AppDelegate.swift b/CodeEdit/AppDelegate.swift index ac7d111ac3..8104f0134f 100644 --- a/CodeEdit/AppDelegate.swift +++ b/CodeEdit/AppDelegate.swift @@ -9,61 +9,63 @@ import SwiftUI class CodeEditApplication: NSApplication { let strongDelegate = AppDelegate() - + override init() { super.init() self.delegate = strongDelegate } - + required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } - + } class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject { func applicationWillFinishLaunching(_ notification: Notification) { _ = CodeEditDocumentController.shared + let nib = NSNib(nibNamed: NSNib.Name("MainMenu"), bundle: Bundle.main) + nib?.instantiate(withOwner: NSApplication.shared, topLevelObjects: nil) } - + func applicationDidFinishLaunching(_ notification: Notification) { if let appearanceString = UserDefaults.standard.string(forKey: Appearances.storageKey) { Appearances(rawValue: appearanceString)?.applyAppearance() } - + DispatchQueue.main.async { if NSApp.windows.isEmpty { self.handleOpen() } } } - + func applicationWillTerminate(_ aNotification: Notification) { } - + func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool { return true } - + func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool { if flag { return false } - + handleOpen() - + return false } - + func applicationShouldOpenUntitledFile(_ sender: NSApplication) -> Bool { return false } - + func handleOpen() { let behavior = ReopenBehavior(rawValue: UserDefaults.standard.string(forKey: ReopenBehavior.storageKey) ?? ReopenBehavior.default.rawValue) ?? ReopenBehavior.default - + switch behavior { case .welcome: openWelcome(self) @@ -73,7 +75,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject { CodeEditDocumentController.shared.newDocument(self) } } - + func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply { CodeEditDocumentController.shared.documents.flatMap { doc in return doc.windowControllers @@ -84,31 +86,31 @@ class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject { } return .terminateNow } - + // MARK: - Open windows - -// @IBAction func openPreferences(_ sender: Any) { -// if let window = NSApp.windows.filter({ window in -// return (window.contentView as? NSHostingView) != nil -// }).first { -// window.makeKeyAndOrderFront(self) -// return -// } -// -// let window = NSWindow( -// contentRect: NSRect(x: 0, y: 0, width: 500, height: 400), -// styleMask: [.titled, .closable], -// backing: .buffered, defer: false) -// window.center() -// window.toolbar = NSToolbar() -// window.title = "Settings" -// window.toolbarStyle = .unifiedCompact -// _ = NSWindowController(window: window) -// let contentView = SettingsView() -// window.contentView = NSHostingView(rootView: contentView) -// window.makeKeyAndOrderFront(sender) -// } - + + // @IBAction func openPreferences(_ sender: Any) { + // if let window = NSApp.windows.filter({ window in + // return (window.contentView as? NSHostingView) != nil + // }).first { + // window.makeKeyAndOrderFront(self) + // return + // } + // + // let window = NSWindow( + // contentRect: NSRect(x: 0, y: 0, width: 500, height: 400), + // styleMask: [.titled, .closable], + // backing: .buffered, defer: false) + // window.center() + // window.toolbar = NSToolbar() + // window.title = "Settings" + // window.toolbarStyle = .unifiedCompact + // _ = NSWindowController(window: window) + // let contentView = SettingsView() + // window.contentView = NSHostingView(rootView: contentView) + // window.makeKeyAndOrderFront(sender) + // } + @IBAction func openWelcome(_ sender: Any) { if let window = NSApp.windows.filter({ window in return (window.contentView as? NSHostingView) != nil @@ -116,7 +118,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject { window.makeKeyAndOrderFront(self) return } - + let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 800, height: 460), styleMask: [.titled, .fullSizeContentView], backing: .buffered, defer: false) let windowController = NSWindowController(window: window) diff --git a/CodeEdit/MainMenu.xib b/CodeEdit/MainMenu.xib index 605193b449..caeb99259a 100644 --- a/CodeEdit/MainMenu.xib +++ b/CodeEdit/MainMenu.xib @@ -1,7 +1,7 @@ - + - + From 9cf2d2f1ccfeb354bc144354349b6378d41606bb Mon Sep 17 00:00:00 2001 From: Underthestars-zhy Date: Mon, 21 Mar 2022 20:48:39 +0800 Subject: [PATCH 3/7] Fix the problem that cannot use menubar to open the setting view --- CodeEdit/AppDelegate.swift | 26 +++++--------------------- CodeEdit/Info.plist | 2 ++ CodeEdit/MainMenu.xib | 6 +----- 3 files changed, 8 insertions(+), 26 deletions(-) diff --git a/CodeEdit/AppDelegate.swift b/CodeEdit/AppDelegate.swift index 8104f0134f..d71caa927f 100644 --- a/CodeEdit/AppDelegate.swift +++ b/CodeEdit/AppDelegate.swift @@ -27,6 +27,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject { _ = CodeEditDocumentController.shared let nib = NSNib(nibNamed: NSNib.Name("MainMenu"), bundle: Bundle.main) nib?.instantiate(withOwner: NSApplication.shared, topLevelObjects: nil) + NSApplication.shared.mainMenu?.items.first { $0.title == "CodeEdit" }?.submenu?.items.first { $0.title == "Preferences" }?.action = #selector(openPreferences(_:)) } func applicationDidFinishLaunching(_ notification: Notification) { @@ -89,27 +90,10 @@ class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject { // MARK: - Open windows - // @IBAction func openPreferences(_ sender: Any) { - // if let window = NSApp.windows.filter({ window in - // return (window.contentView as? NSHostingView) != nil - // }).first { - // window.makeKeyAndOrderFront(self) - // return - // } - // - // let window = NSWindow( - // contentRect: NSRect(x: 0, y: 0, width: 500, height: 400), - // styleMask: [.titled, .closable], - // backing: .buffered, defer: false) - // window.center() - // window.toolbar = NSToolbar() - // window.title = "Settings" - // window.toolbarStyle = .unifiedCompact - // _ = NSWindowController(window: window) - // let contentView = SettingsView() - // window.contentView = NSHostingView(rootView: contentView) - // window.makeKeyAndOrderFront(sender) - // } + @objc func openPreferences(_ sender: Any) { + NSApp.sendAction(Selector(("showPreferencesWindow:")), to: nil, from: nil) + } + @IBAction func openWelcome(_ sender: Any) { if let window = NSApp.windows.filter({ window in diff --git a/CodeEdit/Info.plist b/CodeEdit/Info.plist index 16de6697dc..34da852161 100644 --- a/CodeEdit/Info.plist +++ b/CodeEdit/Info.plist @@ -38,6 +38,8 @@ CFBundleTypeRole Editor + CFBundleURLName + austincondiff.CodeEdit.url CFBundleURLSchemes codeedit diff --git a/CodeEdit/MainMenu.xib b/CodeEdit/MainMenu.xib index caeb99259a..7eb6ed570d 100644 --- a/CodeEdit/MainMenu.xib +++ b/CodeEdit/MainMenu.xib @@ -21,11 +21,7 @@ - - - - - + From 2c438b7ccc4ed2b63651ee007455812f791befc2 Mon Sep 17 00:00:00 2001 From: Underthestars-zhy Date: Mon, 21 Mar 2022 20:49:10 +0800 Subject: [PATCH 4/7] Remove something --- CodeEdit.xcodeproj/project.pbxproj | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/CodeEdit.xcodeproj/project.pbxproj b/CodeEdit.xcodeproj/project.pbxproj index 252a7ee516..22846969c0 100644 --- a/CodeEdit.xcodeproj/project.pbxproj +++ b/CodeEdit.xcodeproj/project.pbxproj @@ -697,12 +697,12 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIconDev; ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; CODE_SIGN_ENTITLEMENTS = CodeEdit/CodeEdit.entitlements; - CODE_SIGN_IDENTITY = "Apple Development"; + CODE_SIGN_IDENTITY = "-"; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; CURRENT_PROJECT_VERSION = 1; DEVELOPMENT_ASSET_PATHS = "\"CodeEdit/Preview Content\""; - DEVELOPMENT_TEAM = 44BN78992X; + DEVELOPMENT_TEAM = ""; ENABLE_HARDENED_RUNTIME = YES; ENABLE_PREVIEWS = YES; GENERATE_INFOPLIST_FILE = YES; @@ -728,12 +728,12 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIconDev; ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; CODE_SIGN_ENTITLEMENTS = CodeEdit/CodeEdit.entitlements; - CODE_SIGN_IDENTITY = "Apple Development"; + CODE_SIGN_IDENTITY = "-"; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; CURRENT_PROJECT_VERSION = 1; DEVELOPMENT_ASSET_PATHS = "\"CodeEdit/Preview Content\""; - DEVELOPMENT_TEAM = 44BN78992X; + DEVELOPMENT_TEAM = ""; ENABLE_HARDENED_RUNTIME = YES; ENABLE_PREVIEWS = YES; GENERATE_INFOPLIST_FILE = YES; From b8083f7b11c4fd702a96d0b7b65cd019eca0bc67 Mon Sep 17 00:00:00 2001 From: Underthestars-zhy Date: Mon, 21 Mar 2022 20:59:38 +0800 Subject: [PATCH 5/7] Fix menu bar problems --- CodeEdit/AppDelegate.swift | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CodeEdit/AppDelegate.swift b/CodeEdit/AppDelegate.swift index d71caa927f..0ed47f0521 100644 --- a/CodeEdit/AppDelegate.swift +++ b/CodeEdit/AppDelegate.swift @@ -23,11 +23,14 @@ class CodeEditApplication: NSApplication { class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject { + private(set) var menu: NSMenu! = nil + func applicationWillFinishLaunching(_ notification: Notification) { _ = CodeEditDocumentController.shared let nib = NSNib(nibNamed: NSNib.Name("MainMenu"), bundle: Bundle.main) nib?.instantiate(withOwner: NSApplication.shared, topLevelObjects: nil) NSApplication.shared.mainMenu?.items.first { $0.title == "CodeEdit" }?.submenu?.items.first { $0.title == "Preferences" }?.action = #selector(openPreferences(_:)) + menu = NSApplication.shared.mainMenu } func applicationDidFinishLaunching(_ notification: Notification) { @@ -92,6 +95,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject { @objc func openPreferences(_ sender: Any) { NSApp.sendAction(Selector(("showPreferencesWindow:")), to: nil, from: nil) + NSApplication.shared.mainMenu = menu } From ec409f59eb2d8614467779fc4a0556d8c5d584e7 Mon Sep 17 00:00:00 2001 From: Underthestars-zhy Date: Mon, 21 Mar 2022 21:22:30 +0800 Subject: [PATCH 6/7] change the code style --- CodeEdit/AppDelegate.swift | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/CodeEdit/AppDelegate.swift b/CodeEdit/AppDelegate.swift index 0ed47f0521..2890da0a59 100644 --- a/CodeEdit/AppDelegate.swift +++ b/CodeEdit/AppDelegate.swift @@ -18,10 +18,8 @@ class CodeEditApplication: NSApplication { required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } - } - class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject { private(set) var menu: NSMenu! = nil @@ -29,7 +27,9 @@ class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject { _ = CodeEditDocumentController.shared let nib = NSNib(nibNamed: NSNib.Name("MainMenu"), bundle: Bundle.main) nib?.instantiate(withOwner: NSApplication.shared, topLevelObjects: nil) - NSApplication.shared.mainMenu?.items.first { $0.title == "CodeEdit" }?.submenu?.items.first { $0.title == "Preferences" }?.action = #selector(openPreferences(_:)) + NSApplication.shared.mainMenu?.items + .first { $0.title == "CodeEdit" }?.submenu?.items + .first { $0.title == "Preferences" }?.action = #selector(openPreferences(_:)) menu = NSApplication.shared.mainMenu } @@ -98,7 +98,6 @@ class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject { NSApplication.shared.mainMenu = menu } - @IBAction func openWelcome(_ sender: Any) { if let window = NSApp.windows.filter({ window in return (window.contentView as? NSHostingView) != nil From 2900c0b4b4efff5aee8ddb57c19ed96b9a4d7613 Mon Sep 17 00:00:00 2001 From: Underthestars-zhy Date: Mon, 21 Mar 2022 22:04:41 +0800 Subject: [PATCH 7/7] swift style --- CodeEdit/AppDelegate.swift | 19 +------------------ CodeEdit/CodeEdit.swift | 1 - 2 files changed, 1 insertion(+), 19 deletions(-) diff --git a/CodeEdit/AppDelegate.swift b/CodeEdit/AppDelegate.swift index 2890da0a59..f6ded472d7 100644 --- a/CodeEdit/AppDelegate.swift +++ b/CodeEdit/AppDelegate.swift @@ -9,12 +9,10 @@ import SwiftUI class CodeEditApplication: NSApplication { let strongDelegate = AppDelegate() - override init() { super.init() self.delegate = strongDelegate } - required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } @@ -22,7 +20,6 @@ class CodeEditApplication: NSApplication { class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject { private(set) var menu: NSMenu! = nil - func applicationWillFinishLaunching(_ notification: Notification) { _ = CodeEditDocumentController.shared let nib = NSNib(nibNamed: NSNib.Name("MainMenu"), bundle: Bundle.main) @@ -32,44 +29,34 @@ class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject { .first { $0.title == "Preferences" }?.action = #selector(openPreferences(_:)) menu = NSApplication.shared.mainMenu } - func applicationDidFinishLaunching(_ notification: Notification) { if let appearanceString = UserDefaults.standard.string(forKey: Appearances.storageKey) { Appearances(rawValue: appearanceString)?.applyAppearance() } - DispatchQueue.main.async { if NSApp.windows.isEmpty { self.handleOpen() } } } - func applicationWillTerminate(_ aNotification: Notification) { } - func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool { return true } - func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool { if flag { return false } - handleOpen() - return false } - func applicationShouldOpenUntitledFile(_ sender: NSApplication) -> Bool { return false } - func handleOpen() { let behavior = ReopenBehavior(rawValue: UserDefaults.standard.string(forKey: ReopenBehavior.storageKey) ?? ReopenBehavior.default.rawValue) ?? ReopenBehavior.default - switch behavior { case .welcome: openWelcome(self) @@ -79,7 +66,6 @@ class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject { CodeEditDocumentController.shared.newDocument(self) } } - func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply { CodeEditDocumentController.shared.documents.flatMap { doc in return doc.windowControllers @@ -90,14 +76,12 @@ class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject { } return .terminateNow } - + // MARK: - Open windows - @objc func openPreferences(_ sender: Any) { NSApp.sendAction(Selector(("showPreferencesWindow:")), to: nil, from: nil) NSApplication.shared.mainMenu = menu } - @IBAction func openWelcome(_ sender: Any) { if let window = NSApp.windows.filter({ window in return (window.contentView as? NSHostingView) != nil @@ -105,7 +89,6 @@ class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject { window.makeKeyAndOrderFront(self) return } - let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 800, height: 460), styleMask: [.titled, .fullSizeContentView], backing: .buffered, defer: false) let windowController = NSWindowController(window: window) diff --git a/CodeEdit/CodeEdit.swift b/CodeEdit/CodeEdit.swift index d9ea735980..fff6ea8950 100644 --- a/CodeEdit/CodeEdit.swift +++ b/CodeEdit/CodeEdit.swift @@ -10,7 +10,6 @@ import SwiftUI @main struct CodeEdit: App { @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate - var body: some Scene { Settings { SettingsView()