Skip to content

Commit 21da8c6

Browse files
committed
Migrate validateMenuItem(_:) to validateUserInterfaceItem(_:) in documents
1 parent b374813 commit 21da8c6

File tree

6 files changed

+23
-33
lines changed

6 files changed

+23
-33
lines changed

CotEditor/Sources/Document Window/Content View/DocumentViewController.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -397,8 +397,8 @@ final class DocumentViewController: NSSplitViewController, ThemeChanging, NSTool
397397

398398
case #selector(toggleSplitOrientation):
399399
(item as? NSMenuItem)?.title = self.splitView.isVertical
400-
? String(localized: "Stack Editors Horizontally", table: "MainMenu")
401-
: String(localized: "Stack Editors Vertically", table: "MainMenu")
400+
? String(localized: "Stack Editors Horizontally", table: "MainMenu")
401+
: String(localized: "Stack Editors Vertically", table: "MainMenu")
402402

403403
case #selector(closeSplitTextView):
404404
return self.splitViewItems.count > 1

CotEditor/Sources/Document/DataDocument.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//
99
// ---------------------------------------------------------------------------
1010
//
11-
// © 2024 1024jp
11+
// © 2024-2025 1024jp
1212
//
1313
// Licensed under the Apache License, Version 2.0 (the "License");
1414
// you may not use this file except in compliance with the License.
@@ -36,16 +36,16 @@ import AppKit
3636

3737
// MARK: Document Methods
3838

39-
override func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
39+
override func validateUserInterfaceItem(_ item: any NSValidatedUserInterfaceItem) -> Bool {
4040

41-
switch menuItem.action {
41+
switch item.action {
4242
case #selector(showInFinder):
4343
return self.fileURL != nil
4444

4545
default: break
4646
}
4747

48-
return super.validateMenuItem(menuItem)
48+
return super.validateUserInterfaceItem(item)
4949
}
5050

5151

CotEditor/Sources/Document/DirectoryDocument+Actions.swift

+5-19
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//
99
// ---------------------------------------------------------------------------
1010
//
11-
// © 2024 1024jp
11+
// © 2024-2025 1024jp
1212
//
1313
// Licensed under the Apache License, Version 2.0 (the "License");
1414
// you may not use this file except in compliance with the License.
@@ -28,23 +28,6 @@ import AppKit
2828
// -> Pass all possible actions manually since NSDocument has no next responder (2024-05, macOS 14)
2929
extension DirectoryDocument {
3030

31-
override func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
32-
33-
switch menuItem.action {
34-
case #selector(changeEncoding),
35-
#selector(changeLineEnding),
36-
#selector(changeSyntax):
37-
return (self.currentDocument as? Document)?.validateMenuItem(menuItem) ?? false
38-
39-
case #selector(showInFinder):
40-
return self.currentDocument?.validateMenuItem(menuItem) ?? false
41-
42-
default:
43-
return super.validateMenuItem(menuItem)
44-
}
45-
}
46-
47-
4831
override func validateUserInterfaceItem(_ item: any NSValidatedUserInterfaceItem) -> Bool {
4932

5033
switch item.action {
@@ -60,7 +43,10 @@ extension DirectoryDocument {
6043
#selector(lock(_:)),
6144
#selector(unlock(_:)),
6245
#selector(runPageLayout),
63-
#selector(printDocument):
46+
#selector(printDocument),
47+
#selector(changeEncoding),
48+
#selector(changeLineEnding),
49+
#selector(changeSyntax):
6450
// -> PreviewDocument doesn't support file manipulation.
6551
return (self.currentDocument as? Document)?.validateUserInterfaceItem(item) ?? false
6652

CotEditor/Sources/Document/Document.swift

+12-6
Original file line numberDiff line numberDiff line change
@@ -819,22 +819,28 @@ extension Document: EditorSource {
819819
}
820820

821821

822-
override func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
822+
override func validateUserInterfaceItem(_ item: any NSValidatedUserInterfaceItem) -> Bool {
823823

824-
switch menuItem.action {
824+
switch item.action {
825825
case #selector(changeEncoding(_:)):
826-
menuItem.state = (menuItem.representedObject as? FileEncoding == self.fileEncoding) ? .on : .off
826+
if let item = item as? NSMenuItem {
827+
item.state = (item.representedObject as? FileEncoding == self.fileEncoding) ? .on : .off
828+
}
827829

828830
case #selector(changeLineEnding(_:)):
829-
menuItem.state = (menuItem.tag == self.lineEnding.index) ? .on : .off
831+
if let item = item as? NSMenuItem {
832+
item.state = (item.tag == self.lineEnding.index) ? .on : .off
833+
}
830834

831835
case #selector(changeSyntax(_:)):
832-
menuItem.state = (menuItem.representedObject as? String == self.syntaxParser.name) ? .on : .off
836+
if let item = item as? NSMenuItem {
837+
item.state = (item.representedObject as? String == self.syntaxParser.name) ? .on : .off
838+
}
833839

834840
default: break
835841
}
836842

837-
return super.validateMenuItem(menuItem)
843+
return super.validateUserInterfaceItem(item)
838844
}
839845

840846

CotEditor/Sources/Settings Window/Other Views/SyntaxListViewController.swift

-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ final class SyntaxListViewController: NSViewController, NSMenuItemValidation, NS
8787

8888
// MARK: Menu Item Validation
8989

90-
/// Applies the current state to menu items.
9190
func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
9291

9392
let isContextMenu = (menuItem.menu == self.contextMenu)

CotEditor/Sources/Text Finder/Multiple Replace/Views/MultipleReplaceListViewController.swift

-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ final class MultipleReplaceListViewController: NSViewController, NSMenuItemValid
104104

105105
// MARK: Menu Item Validation
106106

107-
/// Applies current state to menu items.
108107
func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
109108

110109
let isContextualMenu = (menuItem.menu == self.tableView?.menu)

0 commit comments

Comments
 (0)