我开始研究这个兔子洞,因为一个 SwiftUI 按钮,指定用于弹出菜单,具有 .buttonStyle(BorderlessButtonStyle())
样式,当从灯光切换到灯光时,其行为不正确深色模式。
很沮丧,我决定制作自己的按钮。我注意到 SwiftUI 按钮在弹出菜单时会进行切换,因此我也希望我的按钮具有这种行为。因此,我需要找出 NSMenu 何时关闭。
我已经尝试过建议的答案 here ⤵︎
class NSMenuDelegateModified: NSObject, NSMenuDelegate, ObservableObject {
var menu: NSMenu
@Published var isVisible: Bool?
internal init(menu: NSMenu) {
self.menu = menu
super.init()
self.menu.delegate = self
}
func menuDidClose(_ menu: NSMenu) {
isVisible = false
}
func menuWillOpen(_ menu: NSMenu) {
isVisible = true
}
}
现在这将告诉我菜单在类中是否可见,但是当我尝试在类的实例化对象上打印 .isVisible
时,它仅返回 false。
有办法解决这个问题吗?
最佳答案
所以我明白了为什么我无法在类之外从 .isVisible
中获得 true
!这是因为我使用 menu.popUp(...)
打开我的菜单。
事实证明,这个函数会暂停应用程序的某些部分或整个主线程(我真的不确定)的执行,直到它可以返回状态⤵︎
true = user selected something from the menu
false = user hasn't selected anything and the menu closed
您可以在 Apple's documentation 上查看更多详细信息.
解决方案
考虑到这一点,一切都变得容易多了!要切换按钮,我只需在调用 menu.popUp(...)
后更改颜色即可。
这意味着当弹出菜单时,颜色不会再次改变,直到从函数返回状态并恢复执行!
更新答案
看起来我搞乱了功能,而且比我预期的要复杂。这是我最终得到的结果如下⤵︎
.opacity(pressed ? 1 : 0.6)
.inactiveWindowTap { pressed in
if popped == nil {
// Only register mouse ups
if !pressed {
popped = menu.popUp(...)
// Execution pauses here and waits till a state is returned
}
self.pressed = pressed
}
else if !pressed {
popped = nil
}
}
.whenHovered { hovering in
if hovering == false, popped != nil {
popped = nil
}
}
这个解决方案非常适合我的需求,所以我希望这里所说的仍然可以帮助别人。希望有人能提出比这更好的答案。
旧答案(这是不正确的并且无法按预期工作)
.opacity(pressed ? 1 : 0.6)
.inactiveWindowTap { pressed in
// Only register mouse ups
if !pressed {
// You can use the bool returned from this function but you don't have to.
menu.popUp(...) // Execution pauses here until a state is found.
}
self.pressed = pressed
}
关于swift - 如何检测 NSMenu 是否关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68323835/