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
This commit is contained in:
Davide De Rosa 2022-08-19 08:45:36 +02:00
parent d10edc69fe
commit 4d56ed6fca
3 changed files with 15 additions and 26 deletions

View File

@ -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

View File

@ -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)
}
}
}

View File

@ -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
}
}