swift - NSApplication menubar does not respond as expected in nib-less Cocoa application -


i have following single file of code, in i'm trying create cocoa application basic functionality in little code possible without using nibs or xcode. have been getting of information following blog post, in equivalent objective-c code has been posted: (http://www.cocoawithlove.com/2010/09/minimalist-cocoa-programming.html). major change have made appdelegate class manage window, typically done in xcode projects.

import cocoa  class appdelegate: nsobject, nsapplicationdelegate {     var window: nswindow      override init() {         self.window = nswindow()         self.window.setframe(nsrect(x: 0, y: 0, width: 1280, height: 720), display: true)         self.window.collectionbehavior = .fullscreenprimary         self.window.stylemask = nstitledwindowmask | nsclosablewindowmask | nsminiaturizablewindowmask         self.window.title = "main window"     }      func applicationdidfinishlaunching(notification: nsnotification) {         window.makekeyandorderfront(self)     }      func applicationshouldterminateafterlastwindowclosed(sender: nsapplication) -> bool {         return true     } }  autoreleasepool {     nsapplication.sharedapplication()     nsapp.setactivationpolicy(.regular)      let delegate = appdelegate()     nsapp.delegate = delegate      let mainmenu = nsmenu()     nsapp.mainmenu = mainmenu      let applicationmenuitem = nsmenuitem()     mainmenu.additem(applicationmenuitem)      let applicationmenu = nsmenu()      let quitmenuitem = nsmenuitem()     quitmenuitem.action = #selector(nsapp.terminate(_:))     quitmenuitem.keyequivalent = "q"     quitmenuitem.title = "quit cocoa window test"     applicationmenu.additem(quitmenuitem)      applicationmenuitem.submenu = applicationmenu      nsapp.activateignoringotherapps(true)     nsapp.run() } 

i compile terminal following command:

swiftc -o bin/cocoawindowtest -g -framework cocoa ./src/main.swift 

my issue arises menu. although ⌘q keyboard shortcut works expected, application menu i've created won't open, , haven't been able figure out why.

you should use mainmenu.setsubmenu(applicationmenu, foritem:applicationmenuitem) set submenu. items submenus have special action, submenuaction(_:), assigned, responsible showing submenu. above method assigns action (and preferred setting yourself).

for it's worth, wouldn't set nsapp.mainmenu until menu complete.


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -