# Building a NSMenu

**URL:** <https://ifnotnil.com/t/building-a-nsmenu/4576>\
**Category:** Swift\
**Created:** [1 January 2026 18:33 UTC](https://ifnotnil.com/t/building-a-nsmenu/4576 "2026-01-01T18:33:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![DaveS](https://ifnotnil.com/user_avatar/ifnotnil.com/daves/32/79_2.png) [@DaveS](https://ifnotnil.com/u/DaveS)\
**Post date:** [1 January 2026 18:33 UTC](https://ifnotnil.com/t/building-a-nsmenu/4576/1 "2026-01-01T18:33:43Z")

</div>

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

```auto
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

---

<div class="post-metadata">

**Author:** ![pjsmith67](https://ifnotnil.com/user_avatar/ifnotnil.com/pjsmith67/32/5028_2.png) [@pjsmith67](https://ifnotnil.com/u/pjsmith67)\
**Post date:** [2 January 2026 15:53 UTC](https://ifnotnil.com/t/building-a-nsmenu/4576/2 "2026-01-02T15:53:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![DrScott](https://ifnotnil.com/user_avatar/ifnotnil.com/drscott/32/1465_2.png) [@DrScott](https://ifnotnil.com/u/DrScott)\
**Post date:** [3 January 2026 00:23 UTC](https://ifnotnil.com/t/building-a-nsmenu/4576/3 "2026-01-03T00:23:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![DaveS](https://ifnotnil.com/user_avatar/ifnotnil.com/daves/32/79_2.png) [@DaveS](https://ifnotnil.com/u/DaveS)\
**Post date:** [5 January 2026 15:35 UTC](https://ifnotnil.com/t/building-a-nsmenu/4576/4 "2026-01-05T15:35:12Z")

</div>

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

```auto
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

```

---

<div class="post-metadata">

**Author:** ![rgb](https://ifnotnil.com/letter_avatar_proxy/v4/letter/r/858c86/32.png) [@rgb](https://ifnotnil.com/u/rgb)\
**Post date:** [5 January 2026 21:23 UTC](https://ifnotnil.com/t/building-a-nsmenu/4576/5 "2026-01-05T21:23:44Z")

</div>

Hi,

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