// // KMBaseViewController.swift // PDF Master // // Created by tangchao on 2023/5/5. // import Cocoa // 基类 [抽象类] class KMBaseViewController: NSViewController { // 是否需要菜单 var needMenu = false { didSet { if (self.needMenu) { self.addMenu(to: self.view) } else { self.removeMenu(to: self.view) } } } override func viewDidLoad() { super.viewDidLoad() if (self.needMenu) { self.addMenu(to: self.view) } else { self.removeMenu(to: self.view) } } // MARK: - Menu Add & Remove public func addMenu(to view: NSView?) { if let menuView = view { self.addMenu(to: menuView) return } self.addMenu(to: self.view) } public func removeMenu(to view: NSView?) { if let menuView = view { self.removeMenu(to: menuView) return } self.removeMenu(to: self.view) } private func addMenu(to view: NSView) { // 先移除 self.removeMenu(to: view) let menu = NSMenu() menu.delegate = self view.menu = menu } private func removeMenu(to view: NSView) { view.menu?.delegate = nil view.menu = nil } } extension KMBaseViewController: NSMenuDelegate, NSMenuItemValidation { func validateMenuItem(_ menuItem: NSMenuItem) -> Bool { return true } func menuNeedsUpdate(_ menu: NSMenu) { menu.removeAllItems() } }