Posts

Post not yet marked as solved
4 Replies
0 Views
I ended up getting the following to work on macOS using the .searchable() modifier (only tested on macOS 12.3): import SwiftUI @main struct MyApp: App {     @State var searchText = ""     var items = ["Item 1", "Item 2", "Item 3"]     var searchResults: [String] {         if searchText.isEmpty {             return items         } else {             return items.filter { $0.contains(searchText) }         }     }     var body: some Scene {         WindowGroup {             List(self.searchResults, id: \.self) { item in                 Text(item)             }.searchable(text: self.$searchText)         }.commands {             CommandMenu("Find") {                 Button("Find") {                     if let toolbar = NSApp.keyWindow?.toolbar,                         let search = toolbar.items.first(where: { $0.itemIdentifier.rawValue == "com.apple.SwiftUI.search" }) as? NSSearchToolbarItem {                         search.beginSearchInteraction()                     }                 }.keyboardShortcut("f", modifiers: .command)             }         }     } }
Post marked as solved
2 Replies
0 Views
I started using the subscriptionID to ensure that I would not be creating duplicate subscriptions and this did not alleviate the issue either.