Skip to content

Commit

Permalink
Automatically calculate menu width from MenuItems.
Browse files Browse the repository at this point in the history
  • Loading branch information
aarzilli committed Jan 23, 2017
1 parent 2b5d702 commit 110e634
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 15 deletions.
36 changes: 29 additions & 7 deletions _demo/overview.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ type overviewDemo struct {

Resizing1, Resizing2, Resizing3, Resizing4 bool

edEntry1, edEntry2, edEntry3 nucular.TextEditor

Theme nstyle.Theme
}

Expand Down Expand Up @@ -182,6 +184,13 @@ func newOverviewDemo() (od *overviewDemo) {
od.Text1Editor.Flags = nucular.EditField | nucular.EditSigEnter
od.Text1Editor.Maxlen = 64

od.edEntry1.Flags = nucular.EditSimple
od.edEntry1.Buffer = []rune("Menu Item 1")
od.edEntry2.Flags = nucular.EditSimple
od.edEntry2.Buffer = []rune("Menu Item 2")
od.edEntry3.Flags = nucular.EditSimple
od.edEntry3.Buffer = []rune("Menu Item 3")

return od
}

Expand Down Expand Up @@ -543,10 +552,9 @@ func (od *overviewDemo) errorPopup(w *nucular.Window) {

func (od *overviewDemo) overviewPopup(w *nucular.Window) {
// Menu contextual
w.Row(30).Static(180)
bounds := w.WidgetBounds()
w.Row(30).Dynamic(1)
w.Label("Right click me for menu", "LC")
if w := w.ContextualOpen(0, image.Point{100, 300}, bounds, nil); w != nil {
if w := w.ContextualOpen(0, image.Point{100, 300}, w.LastWidgetBounds, nil); w != nil {
w.Row(25).Dynamic(1)
w.CheckboxText("Menu", &od.ShowMenu)
w.Progress(&od.PProg, 100, true)
Expand All @@ -566,6 +574,22 @@ func (od *overviewDemo) overviewPopup(w *nucular.Window) {
w.SelectableLabel(sel(3), "LC", &od.PSelect[3])
}

w.Label("Right click me for a simple autoresizing menu", "LC")
if w := w.ContextualOpen(0, image.Point{}, w.LastWidgetBounds, nil); w != nil {
w.Row(25).Dynamic(1)
w.MenuItem(label.TA(string(od.edEntry1.Buffer), "LC"))
w.MenuItem(label.TA(string(od.edEntry2.Buffer), "LC"))
w.MenuItem(label.TA(string(od.edEntry3.Buffer), "LC"))
}

w.Row(30).Static(100, 150)
w.Label("Menu Item 1:", "LC")
od.edEntry1.Edit(w)
w.Label("Menu Item 2:", "LC")
od.edEntry2.Edit(w)
w.Label("Menu Item 3:", "LC")
od.edEntry3.Edit(w)

// Popup
w.Row(30).Static(100, 50)
w.Label("Popup:", "LC")
Expand All @@ -575,17 +599,15 @@ func (od *overviewDemo) overviewPopup(w *nucular.Window) {

// Tooltip
w.Row(30).Static(180)
bounds = w.WidgetBounds()
w.Label("Hover me for tooltip", "LC")
if w.Input().Mouse.HoveringRect(bounds) {
if w.Input().Mouse.HoveringRect(w.LastWidgetBounds) {
w.Tooltip("This is a tooltip")
}

// Second tooltip
w.Row(30).Static(420)
bounds = w.WidgetBounds()
w.Label("Can also hover me for a different tooltip", "LC")
if w.Input().Mouse.HoveringRect(bounds) {
if w.Input().Mouse.HoveringRect(w.LastWidgetBounds) {
w.Tooltip("This is another tooltip")
}
}
Expand Down
40 changes: 35 additions & 5 deletions nucular.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ type Window struct {
// editor of the active property widget (see PropertyInt, PropertyFloat)
editor *TextEditor
// update function
updateFn UpdateFn
usingSub bool
began bool
rowCtor rowConstructor
updateFn UpdateFn
usingSub bool
began bool
rowCtor rowConstructor
menuItemWidth int
}

type treeNode struct {
Expand Down Expand Up @@ -132,6 +133,7 @@ const (
windowMenu
windowTooltip
windowEnabled
windowHDynamic

WindowDefaultFlags = WindowBorder | WindowMovable | WindowScalable | WindowClosable | WindowMinimizable | WindowTitle
)
Expand Down Expand Up @@ -465,6 +467,13 @@ func (win *Window) specialPanelBegin() {
}
}

if win.flags&windowHDynamic != 0 && !win.first {
uw := win.menuItemWidth + 2*win.style().Padding.X + 2*win.style().Border
if uw < win.Bounds.W {
win.Bounds.W = uw
}
}

if win.flags&windowCombo != 0 && win.flags&WindowDynamic != 0 {
prevbody := win.Bounds
prevbody.H = win.layout.Height
Expand Down Expand Up @@ -2508,11 +2517,17 @@ func (win *Window) Close() {
///////////////////////////////////////////////////////////////////////////////////

// Opens a contextual menu with maximum size equal to 'size'.
// Specify size == image.Point{} if you want a menu big enough to fit its larges MenuItem
func (win *Window) ContextualOpen(flags WindowFlags, size image.Point, trigger_bounds rect.Rect, updateFn UpdateFn) *Window {
if popup := win.ctx.Windows[len(win.ctx.Windows)-1]; popup.header == trigger_bounds {
popup.specialPanelBegin()
return popup
}
if size == (image.Point{}) {
size.X = nk_null_rect.W
size.Y = nk_null_rect.H
flags = flags | windowHDynamic
}
size.X = win.ctx.scale(size.X)
size.Y = win.ctx.scale(size.Y)
if trigger_bounds.W > 0 && trigger_bounds.H > 0 {
Expand Down Expand Up @@ -2546,6 +2561,13 @@ func (win *Window) MenuItem(lbl label.Label) bool {
return false
}

if win.flags&windowHDynamic != 0 {
w := FontWidth(style.Font, lbl.Text) + 2*style.ContextualButton.Padding.X
if w > win.menuItemWidth {
win.menuItemWidth = w
}
}

in := win.inputMaybe(state)
if doButton(win, lbl, bounds, &style.ContextualButton, in, false) {
win.Close()
Expand Down Expand Up @@ -2681,6 +2703,7 @@ func (win *Window) ComboSimple(items []string, selected int, item_height int) in
///////////////////////////////////////////////////////////////////////////////////

// Adds a menu to win with a text label.
// If width == 0 the width will be automatically adjusted to fit the largest MenuItem
func (win *Window) Menu(lbl label.Label, width int, updateFn UpdateFn) *Window {
state, header := win.widget()
if !state {
Expand All @@ -2703,15 +2726,22 @@ func (win *Window) Menu(lbl label.Label, width int, updateFn UpdateFn) *Window {
return nil
}

flags := windowMenu | WindowNoScrollbar

width = win.ctx.scale(width)

if width == 0 {
width = nk_null_rect.W
flags = flags | windowHDynamic
}

var body rect.Rect
body.X = header.X
body.W = width
body.Y = header.Y + header.H
body.H = (win.layout.Bounds.Y + win.layout.Bounds.H) - body.Y

return win.ctx.nonblockOpen(windowMenu|WindowNoScrollbar, body, header, updateFn)
return win.ctx.nonblockOpen(flags, body, header, updateFn)
}

///////////////////////////////////////////////////////////////////////////////////
Expand Down
7 changes: 4 additions & 3 deletions shiny.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,7 @@ func (w *masterWindow) handleEventLocked(ei interface{}) bool {
c = true
}
if c {
changed := atomic.LoadInt32(&w.ctx.changed)
if changed < 2 {
if changed := atomic.LoadInt32(&w.ctx.changed); changed < 2 {
atomic.StoreInt32(&w.ctx.changed, 2)
}
}
Expand All @@ -171,7 +170,9 @@ func (w *masterWindow) handleEventLocked(ei interface{}) bool {
w.setupBuffer(sz)
}
w.prevCmds = w.prevCmds[:0]
w.Changed()
if changed := atomic.LoadInt32(&w.ctx.changed); changed < 2 {
atomic.StoreInt32(&w.ctx.changed, 2)
}

case mouse.Event:
changed := atomic.LoadInt32(&w.ctx.changed)
Expand Down

0 comments on commit 110e634

Please sign in to comment.