Post

Replies

Boosts

Views

Activity

Using @FocusedBinding with a Class to update a CommandMenu
This is for MacOS. I am trying to send a message to a Model Object using a CommandMenu and then change the menu button title in response, with only partial success. I am using @FocusedBinding and .focusedValue so that the model can be created in my view rather than at the App level. I want to do this so that I can switch to a document style app and have different model instances. In the following example everything works as it should IF the model is a Struct. However I REQUIRE that the model be a Class so I can observe changes. If the model is switched to a class by commenting out the struct Model and uncommenting the class Model in the example below there are two problems that I can't seem to fix: First, the menu never switches from ‘Play’ to ‘Stop’ Second, I can’t change the Model var annotation to @StateObject - which is what a class really needs - without getting an error on the .focusedValue modifier. Any help sorting this out would be greatly appreciated import SwiftUI @main struct MessageApp : App {     var body: some Scene {         WindowGroup {             MessageView()         }         .commands {             AppCommands()         }     } } struct AppCommands : Commands { struct PlayMenu : View {         @FocusedBinding(\.model) var model: Model?         var body: some View {             Button((model?.playing ?? false) ? "Stop" : "Play", action: { model?.send(command: .play) }).keyboardShortcut(.space, modifiers: [])         }     }     var body: some Commands {         CommandMenu("Play") {             PlayMenu()         }     } } struct MessageView : View {     @State private var model = Model()     var body: some View {         Rectangle()             .focusedValue(\.model, $model)             .frame(idealWidth: 600, idealHeight: 400)     } } //class Model : ObservableObject{ //    @Published var playing: Bool = false // //    func send(command: ModelCommands) { //        playing.toggle() //        print(playing) //    } //} struct Model{     var playing: Bool = false     mutating func send(command: ModelCommands) {         playing.toggle()         print(playing)     } } struct FocusedModelBinding: FocusedValueKey {     typealias Value = Binding<Model> } extension FocusedValues {     var model: FocusedModelBinding.Value? {         get { self[FocusedModelBinding.self] }         set { self[FocusedModelBinding.self] = newValue }     } } enum ModelCommands: CaseIterable{     case play }
1
0
634
Jun ’21
SF Symbols in Big Sur
I am using SF Symbols extensively in a MacOS SwiftUI app. I recently upgraded the os on both my Mac and laptop to Big Sur. My Mac already had the SF Symbols app and I installed the new os without a clean install whereas my laptop was completely wiped clean before installing Big Sur. Now when I run my app it displays the symbols correctly on the Mac but they don't show up on the laptop. I thought the SF symbols were part of the Big Sur Os but it looks like at least in my case they need to be downloaded separately. How does that work with an app that uses them? Thanks!
0
0
579
Dec ’20
LazyVGrid as multi column NSTableview replacement for MacOS?
I am currently using a List and HStack to hack together a basic multi column Tableview. Fortunately I don't need more advanced features such as column resizing or moving - but scrolling horizontally has proved to be an issue. SwiftUI Lists normally won't scroll sideways so I placed mine in a horizontal scrollbar which does work but only when scrolled from the list 'header' which is separate from the list. Like so: ScrollView(.horizontal){ VStack{ Rectangle() // 'header' - scroll sideways from here List() // mousing here won't scroll sideways } } It 'works', but only just. I was hoping that the new LazyVGrid would work better and enable us to create at least a basic multi column tableview but it doesn't seem possible. Why doesn't Apple give the multi column tableview some love? It's a pretty basic control for many apps on macOS, but seems to have been completely ignored.
0
0
414
Sep ’20