macOS 13.0+, Apple introduce a new api named MenuBarExtra which can add a menubar in macOS. But I can t add a searchable protocol into the content to add a search view ?
@main
struct AppWithMenuBarExtra: App {
@AppStorage("showMenuBarExtra") private var showMenuBarExtra = true
var body: some Scene {
WindowGroup {
ContentView()
}
MenuBarExtra(
"App Menu Bar Extra", systemImage: "star",
isInserted: $showMenuBarExtra)
{
NavigationLink {
Text("Search")
}.searchable()
}
}
}
我希望搜索栏可以显示在菜单栏中
The solution:
@main
struct AppWithMenuBarExtra: App {
@AppStorage("showMenuBarExtra") private var showMenuBarExtra = true
@State var searchText: String
var body: some Scene {
WindowGroup {
ContentView()
}
MenuBarExtra(
"App Menu Bar Extra", systemImage: "star",
isInserted: $showMenuBarExtra)
{
//NavigationLink {
// Text("Search")
//}.searchable()
TextField("Input the search text",text: $searchText)
.onSubmit { //do search action }
}
.menuBarExtraStyle(.window)//add the extra style
}
}