Building a NSMenu

basically an NSMenu is a tree… with the horizontal items (File, View etc) all have submenus which in turn have NSMenuItems, and some of those items are also submenus, createing anywhere from 1 to n “levels”. I’m trying to create a generic class to make it easy to build these in code (no wizard like Xojo has)

for now I have

  • addHeader(title:String) - this adds an entry to the very top level (File, View etc) and creates an empty submenu attached to it
  • addSubmenu(title:String) adds an entry to the previous menu
  • addItem (title:String) adds an entry to either the last header or submenu
addHeader("FILE")
   addSubMenu("Submenu")
      additem("A")
      additem("B")
/// PROBLEM... how to indicate I'm done adding stuff to submenu, so my ref goes back to "FILE"

      

I “could” put a “endSubmenu” function, but that isn’t very clean

For what its worth… that’s pretty much how Purebasic does it.

The only other solution that comes to mind is overloading addItem to allow the submenu to be named. Then you wouldn’t need an end submenu method.

addItem(submenu: String, title: String)

so the code sample becomes:

addHeader(“FILE”)

addSubmenu(“Submenu”)

addItem(“Submenu”, “A”)

addItem(“Submenu”, “B”)

But that’s not a great option either.

Finally got it to work….. I created a enum with definitions of all the “standard” menu items to cut down on the amount of code the developer needed to add, but allowed for the creation of whatever “custom” menu items would be required… includiing sub menus…. here is my test example

let temp = systemMenu(title: "testMenu")
        temp.addHeader(title: "<AppMenu>")
        temp.addMenuItem(id: .about)
        temp.addMenuItem(id: .preferences)
        temp.addMenuItem(id: .hide)
        temp.addMenuItem(id: .hideOthers)
        temp.addMenuItem(id: .showAll)
        temp.addMenuItem(id: .quit)
        // File Menu
        temp.addHeader(title: "File")
        temp.addMenuItem(id: .new)
        temp.addMenuItem(id: .open)
        temp.addMenuItem(id: .save)
        temp.addMenuItem(id: .saveAs)
        temp.addMenuItem(id: .closeFile)
        temp.addMenuItem(id: .closeWindow)
        // Edit Menu
        temp.addHeader(title: "Edit")
        temp.addMenuItem(id: .undo)
        temp.addMenuItem(id: .redo)
        temp.addMenuItem(id: .cut)
        temp.addMenuItem(id: .copy)
        temp.addMenuItem(id: .paste)
        temp.addMenuItem(id: .delete)
        temp.addMenuItem(id: .selectAll)

        temp.addHeader(title: "View")
        temp.addHeader(title: "Find")
        temp.addMenuItem(id: .find)
        temp.addMenuItem(id: .findNext)
        temp.addMenuItem(id: .findPrev)
        // Window Menu
        temp.addHeader(title: "Window")
        temp.addMenuItem(id: .minimize)
        temp.addMenuItem(id: .zoom)
        temp.addMenuItem(id: .bringFront)
        // menu to test sub menu feature
        temp.addHeader(title: "test")
        temp.addMenuItem(title: "test-a")
        temp.addSubMenu(title: "sub test")
        temp.addMenuItem(title: "test-b")
        temp.addMenuItem(title: "test-c")
        temp.endSubMenu()
        temp.addMenuItem(title: "test-d")

        NSApp.mainMenu = temp


Hi,

Set also NSapp.windowsMenu with the pointer of your Window NSMenu pointer