I’m new to Swift Programming but I have written ~30k lines of swift code at this point. I’ve dug a hole and I can’t figure how to get out of it. I’ve written my command menus with simple model setters (which work), my content area my side bar are fully functional at this point and I thought I’d wrap around and start connecting up any menu item that required a real function call to perform real work. It seams that .command menus don’t have access to my model and calling async doesn’t appear to get me the current state of my model. The suggestions that I’m getting from ChatGPT is a bunch of hacks basically writing code that will create a large dispatch mechanism and passing various variables from my model into the function. Basically changing function signatures for dozens of functions. For simplicity I have passed my model around. Most of my functions have that one parameter and I have kept the parameter count down to 3 or less. I don’t see ChatGPTs suggestion as practical. Any suggestions / ideas would be greatly appreciated.
Here are some details. I get the following debugging output:
ACI: Selection count: 0
ACI Tool: distHorizontally
.command Dist Menu: distHorizontally
doDistribute: distHorizontally
DH: No selection
From top down the code basically looks like this. I won’t do the lowest levels since the selection is already wrong. The actual selection count is3 objects at the time that I ran the menu:
ACI: Selection count: 0
ACI Tool: distHorizontally
.command Dist Menu: distHorizontally
doDistribute: distHorizontally
DH: No selection
struct MyApp: App {
@StateObject private var dm = DrawModel() // owner
let content: ContentView
var body: some Scene {
WindowGroup { content }
.commands { CommandMenu("Arrange") {
Menu("Distibute") { AddCommandItems( dm, distributeMenu ){ tool in
print(".command Dist Menu: \(tool.rawValue)")
dm.doDistribute(tool)
}}}}}}
@ViewBuilder
func AddCommandItems( _ dm: DrawModel,_ theCommands: [ToolbarInfo],
actionHandler: ((Tools) -> Void)? = nil
) -> some View {
ForEach(theCommands, id: \.name) { curCommand in
if curCommand.name == "-" {
Divider()
} else {
Button(curCommand.name) {
let currentSelection = dm.selObjs // read here, on click
print("ACI: Selection count: \(currentSelection.count)")
print("ACI Tool: \(curCommand.tool)")
actionHandler?(curCommand.tool)
DBG_OUT(.menus, "Menu: \(curCommand.tool)")
}}}}
So the action handler is being called and has the correct type. It simply doesn’t have the correct model.