-
Notifications
You must be signed in to change notification settings - Fork 148
Rewrite MarkEditWritingTools in pure Swift #1265
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
182424c
Initial plan
Copilot 3e93bd2
Rewrite MarkEditWritingTools from Objective-C to pure Swift
Copilot 7771ffa
Use implicit return in shouldReselect(with:) for Swifty style
Copilot 8453f6c
Add line break before return on line 59 for consistent code style
Copilot 60e5a85
Fix build
cyanzhong 2e8680e
nit-picking
cyanzhong 1cedb1d
Improve AppWritingTools: extract reusable invoke helpers, use compact…
Copilot bbaa48e
Combine two invoke overloads into a single generic invoke<Result> wit…
Copilot 976c800
Revert generic invoke<Result> to two concrete overloads — @convention…
Copilot 133e035
nit-picking
cyanzhong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| // | ||
| // AppWritingTools.swift | ||
| // MarkEditMac | ||
| // | ||
| // Created by cyan on 8/14/24. | ||
| // | ||
|
|
||
| import AppKit | ||
| import MarkEditKit | ||
|
|
||
| @available(macOS 15.1, *) | ||
| enum AppWritingTools { | ||
| enum Tool: Int { | ||
| case panel = 0 | ||
| case proofread = 1 | ||
| case rewrite = 2 | ||
| case makeFriendly = 11 | ||
| case makeProfessional = 12 | ||
| case makeConcise = 13 | ||
| case summarize = 21 | ||
| case createKeyPoints = 22 | ||
| case makeList = 23 | ||
| case makeTable = 24 | ||
| case compose = 201 | ||
| } | ||
|
|
||
| static var requestedTool: Tool { | ||
| guard let controller = NSApp.windows | ||
| .compactMap(\.contentViewController) | ||
| .first(where: { $0.className == "WTWritingToolsViewController" }) else { | ||
| return .panel | ||
| } | ||
|
|
||
| // WTWritingToolsConfiguration | ||
| guard let target = invokeObject(controller, selector: "writingToolsConfiguration") else { | ||
| return .panel | ||
| } | ||
|
|
||
| return .init(rawValue: invokeInt(target, selector: "requestedTool")) ?? .panel | ||
| } | ||
|
|
||
| static var affordanceIcon: NSImage? { | ||
| let configuration = NSImage.SymbolConfiguration( | ||
| pointSize: 12.5, | ||
| weight: .medium | ||
| ) | ||
|
|
||
| for symbolName in ["apple.writing.tools", "_gm"] { | ||
| if let symbolImage = NSImage(systemSymbolName: symbolName, accessibilityDescription: nil) { | ||
| return symbolImage.withSymbolConfiguration(configuration) | ||
| } | ||
| } | ||
|
|
||
| guard let affordanceClass = NSClassFromString("WTAffordanceView") as? NSView.Type else { | ||
| Logger.assertFail("Missing WTAffordanceView class") | ||
| return nil | ||
| } | ||
|
|
||
| let affordanceView = affordanceClass.init(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) | ||
| for case let imageView as NSImageView in affordanceView.subviews { | ||
| return imageView.image?.withSymbolConfiguration(configuration) | ||
| } | ||
|
|
||
| Logger.assertFail("Failed to retrieve affordance icon") | ||
| return nil | ||
| } | ||
|
|
||
| static func shouldReselect(withItem item: Any?) -> Bool { | ||
| guard let menuItem = item as? NSMenuItem else { | ||
| return false | ||
| } | ||
|
|
||
| return shouldReselect(with: .init(rawValue: menuItem.tag) ?? .panel) | ||
| } | ||
|
|
||
| static func shouldReselect(with tool: Tool) -> Bool { | ||
| // Compose mode can start without text selections | ||
| tool != .compose | ||
| } | ||
| } | ||
|
|
||
| // MARK: - Private | ||
|
|
||
| @available(macOS 15.1, *) | ||
| private extension AppWritingTools { | ||
| /// Invokes a selector on a target and returns the result as `NSObject?`. | ||
| static func invokeObject(_ target: NSObject, selector name: String) -> NSObject? { | ||
| guard let (sel, impl) = invocation(of: target, selector: name) else { | ||
| return nil | ||
| } | ||
|
|
||
| let fn = unsafeBitCast(impl, to: (@convention(c) (NSObject, Selector) -> NSObject?).self) | ||
| return fn(target, sel) | ||
| } | ||
|
|
||
| /// Invokes a selector on a target and returns the result as `Int`. | ||
| static func invokeInt(_ target: NSObject, selector name: String) -> Int { | ||
| guard let (sel, impl) = invocation(of: target, selector: name) else { | ||
| return 0 | ||
| } | ||
|
|
||
| let fn = unsafeBitCast(impl, to: (@convention(c) (NSObject, Selector) -> Int).self) | ||
| return fn(target, sel) | ||
| } | ||
|
|
||
| static func invocation(of target: NSObject, selector name: String) -> (Selector, IMP)? { | ||
| let selector = sel_getUid(name) | ||
| guard target.responds(to: selector) else { | ||
| Logger.assertFail("Missing method selector for: \(target), \(name)") | ||
cyanzhong marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return nil | ||
| } | ||
|
|
||
| return (selector, target.method(for: selector)) | ||
| } | ||
| } | ||
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.