简体   繁体   English

如何在 macOS 13 中处理 MenuBarExtra 的生命周期事件 SwiftUI

[英]How to handle lifecycle events for MenuBarExtra in macOS 13 SwiftUI

I'm trying to create a simple menu bar extra in Swift UI using the new MenuBarExtra.我正在尝试使用新的 MenuBarExtra 在 Swift UI 中创建一个额外的简单菜单栏。 I would like the button text in the popover to update dynamically every time the menu is open.我希望弹出窗口中的按钮文本在每次打开菜单时动态更新。

I'm creating the MenuBarExtra like this.我正在像这样创建 MenuBarExtra。

在此处输入图像描述

        MenuBarExtra("Example menu title") {
            Button("Item 1") {
              
            }
            
            Button("Item 2") {
              
            }
            
            Button("Item 3") {
              
            }
        }

I would like the button text (ie. Item 1) to change every time the menu is open.我希望每次打开菜单时更改按钮文本(即项目 1)。 I would have expected onAppear to fire every time the menu is open, but it only fires the first time.我本以为每次打开菜单时都会触发 onAppear,但它只会在第一次触发。 After the initial opening of the popover, there is no clear way to detect a menu close or open event.弹出窗口初始打开后,没有明确的方法来检测菜单关闭或打开事件。

I have tried using the various event handling callbacks to detect the popover opening.我尝试使用各种事件处理回调来检测弹出窗口的打开。 OnAppear works for detecting the initial creation of the view while onDisappear is notably never called. OnAppear 用于检测视图的初始创建,而 onDisappear 从未被调用。

    MenuBarExtra("Example menu title") {
        VStack {
            Button("Item 1") {
                
            }
            
            Button("Item 2") {
                
            }
            
            Button("Item 3") {
                
            }
        }.onAppear() {
            print("This only prints the very first time the menu is opened")
        }
    }

According to Apple's Docs , MenuBarExtra conforms to Scene - this means that you can use an Environment variable of ScenePhase to call something every time the MenuBarExtra enters the foreground or background.根据Apple 的 DocsMenuBarExtra符合Scene - 这意味着每次 MenuBarExtra 进入前景或背景时,您都可以使用ScenePhaseEnvironment变量来调用某些东西。 Article source: https://www.hackingwithswift.com/quick-start/swiftui/how-to-detect-when-your-app-moves-to-the-background-or-foreground-with-scenephase .文章来源: https://www.hackingwithswift.com/quick-start/swiftui/how-to-detect-when-your-app-moves-to-the-background-or-foreground-with-scenephase Example usage:用法示例:

@main
struct AppWithMenuBarExtra: App {
@Environment(\.scenePhase) var scenePhase // <-- HERE! This will give you the values

    var body: some Scene {
        WindowGroup {
            ContentView()
        }
        MenuBarExtra(
            "App Menu Bar Extra", systemImage: "star")
        {
            StatusMenu()
.onChange(of: scenePhase) { newPhase in //<-- HERE TOO! This modifier allows you to detect change of scene.
                if newPhase == .inactive {
                    //Code for moved to inactive
                    print("Moved to inactive")
                } else if newPhase == .active {
                    //Code for moved to foreground
                    print("Moved to foreground - now active")
                    //This is where you would want to change your text
                } else if newPhase == .background {
                    //Code for moved to background
                    print("Moved to background")
                }
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM