From ea3e2d4b3645fd8b52981611c10e161ad46be97c Mon Sep 17 00:00:00 2001 From: Kyle Date: Sat, 28 Oct 2023 23:18:29 +0800 Subject: [PATCH] [NFC] Refactor EZPreferencesWindowController into Swift --- Easydict.xcodeproj/project.pbxproj | 22 ++++--- Easydict/App/Easydict-Bridging-Header.h | 7 +++ .../Feature/EventMonitor/EZEventMonitor.m | 2 +- .../EZPreferencesWindowController.h | 24 -------- .../EZPreferencesWindowController.m | 58 ------------------- .../PreferencesWindowController.swift | 46 +++++++++++++++ .../Feature/StatusItem/EZMenuItemManager.m | 2 +- .../Window/WindowManager/EZWindowManager.m | 2 +- 8 files changed, 66 insertions(+), 97 deletions(-) delete mode 100644 Easydict/Feature/PerferenceWindow/EZPreferencesWindowController.h delete mode 100644 Easydict/Feature/PerferenceWindow/EZPreferencesWindowController.m create mode 100644 Easydict/Feature/PerferenceWindow/PreferencesWindowController.swift diff --git a/Easydict.xcodeproj/project.pbxproj b/Easydict.xcodeproj/project.pbxproj index d6c432310..d9c658fa0 100644 --- a/Easydict.xcodeproj/project.pbxproj +++ b/Easydict.xcodeproj/project.pbxproj @@ -87,7 +87,6 @@ 039CC914292FB3180037B91E /* EZPopUpButton.m in Sources */ = {isa = PBXBuildFile; fileRef = 039CC913292FB3180037B91E /* EZPopUpButton.m */; }; 039D119929D5E26300C93F46 /* EZAudioUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = 039D119829D5E26300C93F46 /* EZAudioUtils.m */; }; 039E5021296E5D9900072344 /* EZScrollViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 039E5020296E5D9900072344 /* EZScrollViewController.m */; }; - 039F5504294B6E29004AB940 /* EZPreferencesWindowController.m in Sources */ = {isa = PBXBuildFile; fileRef = 039F54FD294B6E29004AB940 /* EZPreferencesWindowController.m */; }; 039F5506294B6E29004AB940 /* EZSettingViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 039F5501294B6E29004AB940 /* EZSettingViewController.m */; }; 039F5508294B6E29004AB940 /* EZAboutViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 039F5503294B6E29004AB940 /* EZAboutViewController.m */; }; 03B022E629231FA6001C7E63 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 03B0221D29231FA6001C7E63 /* Assets.xcassets */; }; @@ -208,6 +207,7 @@ 03F639952AA6CFBB009B9914 /* EZBingConfig.m in Sources */ = {isa = PBXBuildFile; fileRef = 03F639942AA6CFBB009B9914 /* EZBingConfig.m */; }; 27B7919E2AEC36A1006E07C6 /* Easydict.xcconfig in Resources */ = {isa = PBXBuildFile; fileRef = 27B7919C2AEC36A1006E07C6 /* Easydict.xcconfig */; }; 27B7919F2AEC36A1006E07C6 /* Easydict-debug.xcconfig in Resources */ = {isa = PBXBuildFile; fileRef = 27B7919D2AEC36A1006E07C6 /* Easydict-debug.xcconfig */; }; + 27CBABD92AED5D80001F1E74 /* PreferencesWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 27CBABD82AED5D80001F1E74 /* PreferencesWindowController.swift */; }; 6220AD5B2A82812300BBFB52 /* EZBingService.m in Sources */ = {isa = PBXBuildFile; fileRef = 6220AD5A2A82812300BBFB52 /* EZBingService.m */; }; 6295DE312A84D82E006145F4 /* EZBingTranslateModel.m in Sources */ = {isa = PBXBuildFile; fileRef = 6295DE302A84D82E006145F4 /* EZBingTranslateModel.m */; }; 6295DE342A84EF76006145F4 /* EZBingLookupModel.m in Sources */ = {isa = PBXBuildFile; fileRef = 6295DE332A84EF76006145F4 /* EZBingLookupModel.m */; }; @@ -399,9 +399,7 @@ 039E501F296E5D9900072344 /* EZScrollViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = EZScrollViewController.h; sourceTree = ""; }; 039E5020296E5D9900072344 /* EZScrollViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = EZScrollViewController.m; sourceTree = ""; }; 039F54FC294B6E29004AB940 /* EZSettingViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EZSettingViewController.h; sourceTree = ""; }; - 039F54FD294B6E29004AB940 /* EZPreferencesWindowController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = EZPreferencesWindowController.m; sourceTree = ""; }; 039F54FE294B6E29004AB940 /* EZAboutViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EZAboutViewController.h; sourceTree = ""; }; - 039F54FF294B6E29004AB940 /* EZPreferencesWindowController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EZPreferencesWindowController.h; sourceTree = ""; }; 039F5501294B6E29004AB940 /* EZSettingViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = EZSettingViewController.m; sourceTree = ""; }; 039F5503294B6E29004AB940 /* EZAboutViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = EZAboutViewController.m; sourceTree = ""; }; 03B0221B29231FA6001C7E63 /* Easydict-Bridging-Header.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "Easydict-Bridging-Header.h"; sourceTree = ""; }; @@ -630,6 +628,7 @@ 27B7919C2AEC36A1006E07C6 /* Easydict.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = Easydict.xcconfig; sourceTree = ""; }; 27B7919D2AEC36A1006E07C6 /* Easydict-debug.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = "Easydict-debug.xcconfig"; sourceTree = ""; }; 27B791A02AEC3A5C006E07C6 /* Easydict-debug.entitlements */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.entitlements; path = "Easydict-debug.entitlements"; sourceTree = ""; }; + 27CBABD82AED5D80001F1E74 /* PreferencesWindowController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PreferencesWindowController.swift; sourceTree = ""; }; 357E179B303EF855EF4561FB /* Pods-EasydictTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-EasydictTests.release.xcconfig"; path = "Target Support Files/Pods-EasydictTests/Pods-EasydictTests.release.xcconfig"; sourceTree = ""; }; 378E73A7EA8FC8FB9C975A63 /* Pods_EasydictTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_EasydictTests.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 6220AD592A82812300BBFB52 /* EZBingService.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = EZBingService.h; sourceTree = ""; }; @@ -1369,8 +1368,7 @@ children = ( 03DC7C5F2A3C6F3B000BF7C9 /* DisableAutoSelectTextViewController */, 037852AD2957FE9B00D0E2CF /* ServiceViewController */, - 039F54FF294B6E29004AB940 /* EZPreferencesWindowController.h */, - 039F54FD294B6E29004AB940 /* EZPreferencesWindowController.m */, + 27CBABD82AED5D80001F1E74 /* PreferencesWindowController.swift */, 039F54FE294B6E29004AB940 /* EZAboutViewController.h */, 039F5503294B6E29004AB940 /* EZAboutViewController.m */, 039F54FC294B6E29004AB940 /* EZSettingViewController.h */, @@ -2298,6 +2296,7 @@ 03BDA7C02A26DA280079D04F /* XPMArgumentPackage.m in Sources */, 037852B02957FEB200D0E2CF /* EZServiceViewController.m in Sources */, 6220AD5B2A82812300BBFB52 /* EZBingService.m in Sources */, + 27CBABD92AED5D80001F1E74 /* PreferencesWindowController.swift in Sources */, 039F5508294B6E29004AB940 /* EZAboutViewController.m in Sources */, 03D8A6592A42A1A300D9A968 /* EZAppModel.m in Sources */, 036E7D7B293F4FC8002675DF /* EZOpenLinkButton.m in Sources */, @@ -2309,7 +2308,6 @@ 0309E1ED292B439A00AFB76A /* EZTextView.m in Sources */, 03B0232B29231FA6001C7E63 /* NSMutableAttributedString+MM.m in Sources */, 03B022E829231FA6001C7E63 /* main.m in Sources */, - 039F5504294B6E29004AB940 /* EZPreferencesWindowController.m in Sources */, 03008B3F29444B0A0062B821 /* NSView+EZAnimatedHidden.m in Sources */, 03B022FD29231FA6001C7E63 /* EZFixedQueryWindow.m in Sources */, 03B0232C29231FA6001C7E63 /* NSView+MM.m in Sources */, @@ -2365,7 +2363,7 @@ ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; BUNDLE_LOADER = "$(TEST_HOST)"; CLANG_CXX_LANGUAGE_STANDARD = "gnu++20"; - CODE_SIGN_IDENTITY = "$CODE_SIGN_IDENTITY"; + CODE_SIGN_IDENTITY = $CODE_SIGN_IDENTITY; CODE_SIGN_STYLE = $CODE_SIGN_STYLE; CURRENT_PROJECT_VERSION = 1; DEVELOPMENT_TEAM = "$(DEVELOPMENT_TEAM)"; @@ -2389,7 +2387,7 @@ ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; BUNDLE_LOADER = "$(TEST_HOST)"; CLANG_CXX_LANGUAGE_STANDARD = "gnu++20"; - CODE_SIGN_IDENTITY = "$CODE_SIGN_IDENTITY"; + CODE_SIGN_IDENTITY = $CODE_SIGN_IDENTITY; CODE_SIGN_STYLE = $CODE_SIGN_STYLE; CURRENT_PROJECT_VERSION = 1; DEVELOPMENT_TEAM = "$(DEVELOPMENT_TEAM)"; @@ -2412,7 +2410,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = "white-black-icon"; "ASSETCATALOG_COMPILER_APPICON_NAME[sdk=macosx*]" = "white-black-icon"; CODE_SIGN_ENTITLEMENTS = EasydictHelper/EasydictHelper.entitlements; - CODE_SIGN_IDENTITY = "$CODE_SIGN_IDENTITY"; + CODE_SIGN_IDENTITY = $CODE_SIGN_IDENTITY; CODE_SIGN_STYLE = $CODE_SIGN_STYLE; COMBINE_HIDPI_IMAGES = YES; CURRENT_PROJECT_VERSION = 22; @@ -2437,7 +2435,7 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = "white-black-icon"; CODE_SIGN_ENTITLEMENTS = EasydictHelper/EasydictHelper.entitlements; - CODE_SIGN_IDENTITY = "$CODE_SIGN_IDENTITY"; + CODE_SIGN_IDENTITY = $CODE_SIGN_IDENTITY; CODE_SIGN_STYLE = $CODE_SIGN_STYLE; COMBINE_HIDPI_IMAGES = YES; CURRENT_PROJECT_VERSION = 22; @@ -2590,7 +2588,7 @@ ASSETCATALOG_COMPILER_INCLUDE_ALL_APPICON_ASSETS = NO; CLANG_ENABLE_MODULES = YES; CODE_SIGN_ENTITLEMENTS = "Easydict/App/Easydict-debug.entitlements"; - CODE_SIGN_IDENTITY = "$CODE_SIGN_IDENTITY"; + CODE_SIGN_IDENTITY = $CODE_SIGN_IDENTITY; CODE_SIGN_STYLE = $CODE_SIGN_STYLE; COMBINE_HIDPI_IMAGES = YES; COPY_PHASE_STRIP = NO; @@ -2628,7 +2626,7 @@ ASSETCATALOG_COMPILER_INCLUDE_ALL_APPICON_ASSETS = NO; CLANG_ENABLE_MODULES = YES; CODE_SIGN_ENTITLEMENTS = Easydict/App/Easydict.entitlements; - CODE_SIGN_IDENTITY = "$CODE_SIGN_IDENTITY"; + CODE_SIGN_IDENTITY = $CODE_SIGN_IDENTITY; CODE_SIGN_STYLE = $CODE_SIGN_STYLE; COMBINE_HIDPI_IMAGES = YES; COPY_PHASE_STRIP = NO; diff --git a/Easydict/App/Easydict-Bridging-Header.h b/Easydict/App/Easydict-Bridging-Header.h index 724abb748..0ff3dbada 100644 --- a/Easydict/App/Easydict-Bridging-Header.h +++ b/Easydict/App/Easydict-Bridging-Header.h @@ -3,3 +3,10 @@ // #import "MMLog.h" + +#import "EZConst.h" +#import "EZSettingViewController.h" +#import "EZAboutViewController.h" +#import "EZServiceViewController.h" +#import "EZPrivacyViewController.h" +#import "EZDisableAutoSelectTextViewController.h" diff --git a/Easydict/Feature/EventMonitor/EZEventMonitor.m b/Easydict/Feature/EventMonitor/EZEventMonitor.m index f0f28d0ec..dfa1be483 100644 --- a/Easydict/Feature/EventMonitor/EZEventMonitor.m +++ b/Easydict/Feature/EventMonitor/EZEventMonitor.m @@ -9,7 +9,6 @@ #import "EZEventMonitor.h" #import "EZWindowManager.h" #import "EZConfiguration.h" -#import "EZPreferencesWindowController.h" #import "EZScriptExecutor.h" #import "EZAudioUtils.h" #import "EZCoordinateUtils.h" @@ -17,6 +16,7 @@ #import "EZLocalStorage.h" #import "EZAppleScriptManager.h" #import "EZSystemUtility.h" +#import "Easydict-Swift.h" static CGFloat const kDismissPopButtonDelayTime = 0.1; static NSTimeInterval const kDelayGetSelectedTextTime = 0.1; diff --git a/Easydict/Feature/PerferenceWindow/EZPreferencesWindowController.h b/Easydict/Feature/PerferenceWindow/EZPreferencesWindowController.h deleted file mode 100644 index bb3c9eeaf..000000000 --- a/Easydict/Feature/PerferenceWindow/EZPreferencesWindowController.h +++ /dev/null @@ -1,24 +0,0 @@ -// -// EZPreferencesWindowController.m -// Easydict -// -// Created by tisfeng on 2022/12/15. -// Copyright © 2022 izual. All rights reserved. -// - -#import - -NS_ASSUME_NONNULL_BEGIN - - -@interface EZPreferencesWindowController : MASPreferencesWindowController - -@property (nonatomic, assign, readonly) BOOL isShowing; - -+ (instancetype)shared; - -- (void)show; - -@end - -NS_ASSUME_NONNULL_END diff --git a/Easydict/Feature/PerferenceWindow/EZPreferencesWindowController.m b/Easydict/Feature/PerferenceWindow/EZPreferencesWindowController.m deleted file mode 100644 index b8b393b44..000000000 --- a/Easydict/Feature/PerferenceWindow/EZPreferencesWindowController.m +++ /dev/null @@ -1,58 +0,0 @@ -// -// EZPreferencesWindowController.m -// Easydict -// -// Created by tisfeng on 2022/12/15. -// Copyright © 2022 izual. All rights reserved. -// - -#import "EZPreferencesWindowController.h" -#import "EZSettingViewController.h" -#import "EZAboutViewController.h" -#import "EZServiceViewController.h" -#import "EZPrivacyViewController.h" -#import "EZDisableAutoSelectTextViewController.h" - -@interface EZPreferencesWindowController () - -@end - -@implementation EZPreferencesWindowController - -static EZPreferencesWindowController *_instance; -+ (instancetype)shared { - if (!_instance) { - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - NSArray *viewControllers = @[ - [[EZSettingViewController alloc] init], - [[EZServiceViewController alloc] init], - [[EZDisableAutoSelectTextViewController alloc] init], - [[EZPrivacyViewController alloc] init], - [[EZAboutViewController alloc] init], - ]; - - NSString *appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleName"]; - _instance = [[self alloc] initWithViewControllers:viewControllers title:appName]; - }); - } - return _instance; -} - -#pragma mark - - -- (void)show { - _isShowing = YES; - - [self.window makeKeyAndOrderFront:nil]; - if (!self.window.isKeyWindow) { - [NSApp activateIgnoringOtherApps:YES]; - } - [self.window center]; -} - -- (void)windowWillClose:(NSNotification *)notification { - _isShowing = NO; -} - -@end diff --git a/Easydict/Feature/PerferenceWindow/PreferencesWindowController.swift b/Easydict/Feature/PerferenceWindow/PreferencesWindowController.swift new file mode 100644 index 000000000..be32f026f --- /dev/null +++ b/Easydict/Feature/PerferenceWindow/PreferencesWindowController.swift @@ -0,0 +1,46 @@ +// +// PreferencesWindowController.swift +// Easydict +// +// Created by Kyle on 2023/10/28. +// Copyright © 2023 izual. All rights reserved. +// + +import Foundation +import MASPreferences +import AppKit + +@objc(EZPreferencesWindowController) +public final class PreferencesWindowController: MASPreferencesWindowController { + + @objc public private(set) var isShowing = false + + @objc public static let shared: PreferencesWindowController = { + let viewControllers = [ + EZSettingViewController(), + EZServiceViewController(), + EZDisableAutoSelectTextViewController(), + EZPrivacyViewController(), + EZAboutViewController(), + ] + let appName = (Bundle.main.object(forInfoDictionaryKey: "CFBundleName") as? String) ?? "" + return PreferencesWindowController(viewControllers: viewControllers, title: appName) + }() + + + @objc public func show() { + isShowing = true + guard let window else { + return + } + window.makeKeyAndOrderFront(nil) + if !window.isKeyWindow { + NSApp.activate(ignoringOtherApps: true) + } + window.center() + } + + public override func windowWillClose(_ notification: Notification) { + isShowing = false + } +} diff --git a/Easydict/Feature/StatusItem/EZMenuItemManager.m b/Easydict/Feature/StatusItem/EZMenuItemManager.m index 2535dca35..097dad8c1 100644 --- a/Easydict/Feature/StatusItem/EZMenuItemManager.m +++ b/Easydict/Feature/StatusItem/EZMenuItemManager.m @@ -7,12 +7,12 @@ // #import "EZMenuItemManager.h" -#import "EZPreferencesWindowController.h" #import "EZWindowManager.h" #import "Snip.h" #import "EZShortcut.h" #import #import "EZRightClickDetector.h" +#import "Easydict-Swift.h" static CGFloat const kImageMenuItemHeightRatio = 1.4; static CGFloat const kTitleMenuItemHeightRatio = 1.2; diff --git a/Easydict/Feature/ViewController/Window/WindowManager/EZWindowManager.m b/Easydict/Feature/ViewController/Window/WindowManager/EZWindowManager.m index 0a1d46242..27feb7922 100644 --- a/Easydict/Feature/ViewController/Window/WindowManager/EZWindowManager.m +++ b/Easydict/Feature/ViewController/Window/WindowManager/EZWindowManager.m @@ -12,9 +12,9 @@ #import "EZEventMonitor.h" #import "Snip.h" #import "EZCoordinateUtils.h" -#import "EZPreferencesWindowController.h" #import "EZConfiguration.h" #import "EZLog.h" +#import "Easydict-Swift.h" @interface EZWindowManager ()