From dcdfe853e1e6251798b3e84843917597884fc768 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 5 Nov 2023 11:14:58 -0500 Subject: [PATCH] Fix Cmd+W closing sometimes closing non-foreground window on macOS Closes #444 --- Tusker/AppDelegate.swift | 7 ------- Tusker/MenuController.swift | 17 ++++++++++------- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/Tusker/AppDelegate.swift b/Tusker/AppDelegate.swift index 42674b30..7d0c21b0 100644 --- a/Tusker/AppDelegate.swift +++ b/Tusker/AppDelegate.swift @@ -162,13 +162,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate { } } - @objc func closeWindow() { - guard let scene = UIApplication.shared.connectedScenes.first(where: { $0.activationState == .foregroundActive }) else { - return - } - UIApplication.shared.requestSceneSessionDestruction(scene.session, options: nil) - } - private func swizzleStatusBar() { let selector = Selector(("handleTapAction:")) var originalIMP: IMP? diff --git a/Tusker/MenuController.swift b/Tusker/MenuController.swift index 3013b898..eb6da265 100644 --- a/Tusker/MenuController.swift +++ b/Tusker/MenuController.swift @@ -41,22 +41,25 @@ struct MenuController { static let prevSubTabCommand = UIKeyCommand(title: "Previous Sub Tab", action: #selector(TabbedPageViewController.selectPrevPage), input: "[", modifierFlags: [.command, .shift]) static func buildMainMenu(builder: UIMenuBuilder) { - builder.replace(menu: .file, with: buildFileMenu()) + builder.replace(menu: .file, with: buildFileMenu(builder: builder)) builder.insertChild(buildSubTabMenu(), atStartOfMenu: .view) builder.insertChild(buildSidebarShortcuts(), atStartOfMenu: .view) } - private static func buildFileMenu() -> UIMenu { + private static func buildFileMenu(builder: UIMenuBuilder) -> UIMenu { + var children: [UIMenuElement] = [ + composeCommand, + refreshCommand(discoverabilityTitle: nil), + ] + if let close = builder.menu(for: .close) { + children.append(close) + } return UIMenu( title: "File", image: nil, identifier: nil, options: [], - children: [ - composeCommand, - refreshCommand(discoverabilityTitle: nil), - UIKeyCommand(title: "Close", action: #selector(AppDelegate.closeWindow), input: "w", modifierFlags: .command), - ] + children: children ) }