From 4d56ed6fca01d7d4235281d19512265eee4e5536 Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Fri, 19 Aug 2022 08:45:36 +0200 Subject: [PATCH] Only hide app by closing it Keep constant "Show" action in menu: - Bring app to foreground if in background - Activate app if already in foreground --- Passepartout/Mac/Menu/PassepartoutMenu.swift | 7 ++++--- .../Mac/Menu/VisibilityItem+ViewModel.swift | 19 ++++--------------- Passepartout/Mac/Menu/VisibilityItem.swift | 15 +++++++-------- 3 files changed, 15 insertions(+), 26 deletions(-) diff --git a/Passepartout/Mac/Menu/PassepartoutMenu.swift b/Passepartout/Mac/Menu/PassepartoutMenu.swift index b8aeebd5..195c5268 100644 --- a/Passepartout/Mac/Menu/PassepartoutMenu.swift +++ b/Passepartout/Mac/Menu/PassepartoutMenu.swift @@ -57,9 +57,10 @@ class PassepartoutMenu { var children: [ItemGroup] = [] children.append(contentsOf: [ - VisibilityItem(utils: macMenuDelegate.utils) { - $0 ? L10n.Global.Strings.hide : L10n.Global.Strings.show - }, + VisibilityItem( + L10n.Global.Strings.show, + utils: macMenuDelegate.utils + ), LaunchOnLoginItem( L10n.Preferences.Items.LaunchesOnLogin.caption, utils: macMenuDelegate.utils diff --git a/Passepartout/Mac/Menu/VisibilityItem+ViewModel.swift b/Passepartout/Mac/Menu/VisibilityItem+ViewModel.swift index d7974bdb..2bfcbeec 100644 --- a/Passepartout/Mac/Menu/VisibilityItem+ViewModel.swift +++ b/Passepartout/Mac/Menu/VisibilityItem+ViewModel.swift @@ -32,28 +32,17 @@ extension VisibilityItem { private let utils: LightUtils - private let titleBlock: (Bool) -> String - - init(utils: LightUtils, titleBlock: @escaping (Bool) -> String) { - self.titleBlock = titleBlock - + init(utils: LightUtils) { transformer = .shared self.utils = utils } - var title: String { - titleBlock(transformer.isForeground) - } - - @objc func toggleForeground() { - transformer.toggleForeground() + @objc func bringToForeground() { + transformer.bringToForeground() if transformer.isForeground { utils.requestScene() + NSApp.activate(ignoringOtherApps: true) } } - - func subscribe(_ block: @escaping () -> Void) { - transformer.subscribe(block) - } } } diff --git a/Passepartout/Mac/Menu/VisibilityItem.swift b/Passepartout/Mac/Menu/VisibilityItem.swift index 2f7dc6d0..b1d05778 100644 --- a/Passepartout/Mac/Menu/VisibilityItem.swift +++ b/Passepartout/Mac/Menu/VisibilityItem.swift @@ -27,24 +27,23 @@ import Foundation import AppKit struct VisibilityItem: Item { + private let title: String + private let viewModel: ViewModel - init(utils: LightUtils, titleBlock: @escaping (Bool) -> String) { - viewModel = ViewModel(utils: utils, titleBlock: titleBlock) + init(_ title: String, utils: LightUtils) { + self.title = title + viewModel = ViewModel(utils: utils) } func asMenuItem(withParent parent: NSMenu) -> NSMenuItem { let item = NSMenuItem( - title: viewModel.title, - action: #selector(viewModel.toggleForeground), + title: title, + action: #selector(viewModel.bringToForeground), keyEquivalent: "" ) item.target = viewModel item.representedObject = viewModel - - viewModel.subscribe { - item.title = viewModel.title - } return item } }