osx - Rich, Window-like Context Menu (like Interface Builder) -


i need context menu similar in capabilities interface builder presents when right(control)-clicking on view, view controller etc.:

enter image description here

at first sight, looks nspanel style attribute set "hud panel", containing sort of outline view.

the window shouldn't difficult implement, usual way of presenting context menu on right(control)-click overriding method:

func menu(for event: nsevent) -> nsmenu? 

...which takes nsmenu return value; can't pass nswindow / nspanel instead.


perhaps this:

override func menu(for event: nsevent) -> nsmenu? {      // create our popup window (hud panel) , present     // @ location of event:      // (...actual code omitted...)      // prevent actual menu being displayed:      return nil  } 

...but feels hack; tricking system giving away timing of right(control)-click event pretending care presenting actual nsmenu (i.e., overriding method explicitly intended that), using timing something different.

i need place logic dismiss window when user clicks somewhere else (context menues have functionality built in).

i don't think subclassing nsmenu , nsmenuitem obtain above behaviour , appearance feasible either...

is there better way? does know (or able guess) interface builder does?


edit: pointed out in comment @willeke, conditions panel shown not same context menu, surely not one. means hijacking method above in order display window not unelegant, wrong. question stands how display window (i.e., detect static, non-dragging right click).

you'll want override rightmousedown(with:) view subclass , use trigger showing panel. nsview's implementation of method calls menu(for:) , presents returned menu, custom subclass can use show custom menu panel instead of calling super.

for full behavioral consistency both standard context menus , menus in interface builder, you'll want handle ctrl-left-clicks overriding mousedown(with:) , check if event's modifierflags includes .control.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -