Skip to content

Commit

Permalink
Migrate validateMenuItem(_:) to validateUserInterfaceItem(_:) in docu…
Browse files Browse the repository at this point in the history
…ments
  • Loading branch information
1024jp committed Jan 19, 2025
1 parent b374813 commit 21da8c6
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,8 @@ final class DocumentViewController: NSSplitViewController, ThemeChanging, NSTool

case #selector(toggleSplitOrientation):
(item as? NSMenuItem)?.title = self.splitView.isVertical
? String(localized: "Stack Editors Horizontally", table: "MainMenu")
: String(localized: "Stack Editors Vertically", table: "MainMenu")
? String(localized: "Stack Editors Horizontally", table: "MainMenu")
: String(localized: "Stack Editors Vertically", table: "MainMenu")

case #selector(closeSplitTextView):
return self.splitViewItems.count > 1
Expand Down
8 changes: 4 additions & 4 deletions CotEditor/Sources/Document/DataDocument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
//
// ---------------------------------------------------------------------------
//
// © 2024 1024jp
// © 2024-2025 1024jp
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -36,16 +36,16 @@ import AppKit

// MARK: Document Methods

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

switch menuItem.action {
switch item.action {
case #selector(showInFinder):
return self.fileURL != nil

default: break
}

return super.validateMenuItem(menuItem)
return super.validateUserInterfaceItem(item)
}


Expand Down
24 changes: 5 additions & 19 deletions CotEditor/Sources/Document/DirectoryDocument+Actions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
//
// ---------------------------------------------------------------------------
//
// © 2024 1024jp
// © 2024-2025 1024jp
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -28,23 +28,6 @@ import AppKit
// -> Pass all possible actions manually since NSDocument has no next responder (2024-05, macOS 14)
extension DirectoryDocument {

override func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {

switch menuItem.action {
case #selector(changeEncoding),
#selector(changeLineEnding),
#selector(changeSyntax):
return (self.currentDocument as? Document)?.validateMenuItem(menuItem) ?? false

case #selector(showInFinder):
return self.currentDocument?.validateMenuItem(menuItem) ?? false

default:
return super.validateMenuItem(menuItem)
}
}


override func validateUserInterfaceItem(_ item: any NSValidatedUserInterfaceItem) -> Bool {

switch item.action {
Expand All @@ -60,7 +43,10 @@ extension DirectoryDocument {
#selector(lock(_:)),
#selector(unlock(_:)),
#selector(runPageLayout),
#selector(printDocument):
#selector(printDocument),
#selector(changeEncoding),
#selector(changeLineEnding),
#selector(changeSyntax):
// -> PreviewDocument doesn't support file manipulation.
return (self.currentDocument as? Document)?.validateUserInterfaceItem(item) ?? false

Expand Down
18 changes: 12 additions & 6 deletions CotEditor/Sources/Document/Document.swift
Original file line number Diff line number Diff line change
Expand Up @@ -819,22 +819,28 @@ extension Document: EditorSource {
}


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

switch menuItem.action {
switch item.action {
case #selector(changeEncoding(_:)):
menuItem.state = (menuItem.representedObject as? FileEncoding == self.fileEncoding) ? .on : .off
if let item = item as? NSMenuItem {
item.state = (item.representedObject as? FileEncoding == self.fileEncoding) ? .on : .off
}

case #selector(changeLineEnding(_:)):
menuItem.state = (menuItem.tag == self.lineEnding.index) ? .on : .off
if let item = item as? NSMenuItem {
item.state = (item.tag == self.lineEnding.index) ? .on : .off
}

case #selector(changeSyntax(_:)):
menuItem.state = (menuItem.representedObject as? String == self.syntaxParser.name) ? .on : .off
if let item = item as? NSMenuItem {
item.state = (item.representedObject as? String == self.syntaxParser.name) ? .on : .off
}

default: break
}

return super.validateMenuItem(menuItem)
return super.validateUserInterfaceItem(item)
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ final class SyntaxListViewController: NSViewController, NSMenuItemValidation, NS

// MARK: Menu Item Validation

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

let isContextMenu = (menuItem.menu == self.contextMenu)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ final class MultipleReplaceListViewController: NSViewController, NSMenuItemValid

// MARK: Menu Item Validation

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

let isContextualMenu = (menuItem.menu == self.tableView?.menu)
Expand Down

0 comments on commit 21da8c6

Please sign in to comment.