Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 37 additions & 9 deletions ui/menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ import (
// })
//
// An item may carry a second line with Description; WidthRange bounds the menu's
// width; Above opens it over the anchor, for a trigger along the bottom of a panel.
// width; Above opens it over the anchor, for a trigger along the bottom of a panel,
// and Beside to its right, for a row of a list whose actions should not cover the
// rows under it.
//
// Not inside a Memo.
func Menu(c C, anchor Box, open *bool, body func(*MenuItems)) {
Expand Down Expand Up @@ -110,6 +112,7 @@ func (p *Page) declareMenu(c C, menu *MenuItems, scope gfx.Scope, open *bool, an
menu.page, menu.f, menu.open, menu.anchor = p, f, open, anchor
menu.scope, menu.root, menu.parent = scope, menu, nil
menu.layer = f.widgetLayer() + 2 // over the page, and over a dialog it is declared in
menu.side = menuBelow // unless body says otherwise again in this paint
menu.fresh()
menu.declare(body)
if f.canvas != nil && f.canvas.Clicked(scope.At(menuDismiss)) {
Expand All @@ -120,9 +123,12 @@ func (p *Page) declareMenu(c C, menu *MenuItems, scope gfx.Scope, open *bool, an
popup := f.Popup()
if anchor >= 0 {
anchorBox := Box{f: f, i: anchor, gen: f.gen}
if menu.above {
switch menu.side {
case menuAbove:
popup.AboveBox(anchorBox, 4, Start)
} else {
case menuBeside:
popup.BesideBox(anchorBox, 4, Start)
default:
popup.BelowBox(anchorBox, 4, Start)
}
} else {
Expand Down Expand Up @@ -263,8 +269,8 @@ type MenuItems struct {
shown bool // a ContextMenu is open
at Rect // where a ContextMenu opened

above bool // open over the anchor instead of under it (Menu only)
widthMin, widthMax float32 // the least and greatest surface width; zero keeps the default
side menuSide // the edge of the anchor a Menu opens on
widthMin, widthMax float32 // the least and greatest surface width; zero keeps the default

// A submenu's menu, and the menu of the one open in it: root is the Menu or
// ContextMenu the chain hangs off, itself for that one, and parent the menu its
Expand Down Expand Up @@ -365,11 +371,26 @@ func (m *MenuItems) Sub(label string, body func(*MenuItems)) MenuItem {
return item
}

// menuSide is the edge of its anchor a Menu opens on.
type menuSide uint8

const (
menuBelow menuSide = iota // under the anchor, flipping over it when there is no room
menuAbove // over the anchor, flipping under it when there is no room
menuBeside // right of the anchor, flipping to its left when there is no room
)

// Above opens the menu over its anchor instead of under it, flipping under when
// there is no room over: the menu of a trigger along the bottom of a panel, which
// should cover what is above it. A submenu opens beside its row and a ContextMenu
// opens at the pointer, so neither is affected.
func (m *MenuItems) Above() { m.above = true }
func (m *MenuItems) Above() { m.side = menuAbove }

// Beside opens the menu to the right of its anchor, its top at the anchor's,
// flipping to the left when there is no room on the right: the menu of a row in a
// list or a popup, which should leave the rows under it in sight, as a submenu
// leaves its menu. A submenu and a ContextMenu are not affected.
func (m *MenuItems) Beside() { m.side = menuBeside }

// WidthRange sets the least and greatest width of the menu's surface. Zero keeps
// the menu's own minimum (128 for a Menu, 144 for a ContextMenu) or leaves that end
Expand Down Expand Up @@ -497,11 +518,18 @@ func (m *MenuItems) paintAt(canvas *gfx.Canvas, theme *Theme, bounds Rect, layer
}
menu.End()
} else {
side := components.OverlayRight
side := components.OverlayRight // the edge the menu slides in from, its anchor's
if m.parent == nil {
side = components.OverlayBottom
if anchor := f.nodes[m.anchor].rect; bounds.Y+bounds.Height <= anchor.Y+anchor.Height/2 {
anchor := f.nodes[m.anchor].rect
switch {
case m.side == menuBeside:
if bounds.X+bounds.Width <= anchor.X+anchor.Width/2 {
side = components.OverlayLeft // flipped for want of room
}
case bounds.Y+bounds.Height <= anchor.Y+anchor.Height/2:
side = components.OverlayTop
default:
side = components.OverlayBottom
}
}
menu := components.BeginDropdownMenu(canvas, *theme, bounds, components.DropdownMenuOptions{
Expand Down
24 changes: 23 additions & 1 deletion ui/menu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,10 +397,11 @@ func TestSubmenuRepaintAllocates(t *testing.T) {
}

// menuOptionsModel exercises the menu calls that shape its surface: a second line
// under an item, its width range, and opening over the anchor.
// under an item, its width range, and opening over or beside the anchor.
type menuOptionsModel struct {
open bool
above bool
beside bool
least, greatest float32
description string
label string
Expand All @@ -421,6 +422,9 @@ func (m *menuOptionsModel) page(c ui.C) {
if m.above {
menu.Above()
}
if m.beside {
menu.Beside()
}
label := m.label
if label == "" {
label = "Model"
Expand Down Expand Up @@ -476,6 +480,24 @@ func TestMenuWidthRange(t *testing.T) {
})
}

// Beside opens the menu right of its anchor, its first row level with the anchor
// rather than under it, and a menu that stops asking for it opens under it again.
func TestMenuBeside(t *testing.T) {
m := &menuOptionsModel{beside: true}
d := openPage(t, m.page)
d.click("More")
anchor, item := d.hit("More").Bounds, d.hit("Model").Bounds
if item.X < anchor.X+anchor.Width || item.Y > anchor.Y+anchor.Height {
t.Fatalf("the menu at %v is not beside its anchor at %v", item, anchor)
}
d.key(gfx.KeyEscape)
m.beside = false
d.click("More")
if item := d.hit("Model").Bounds; item.Y < anchor.Y+anchor.Height {
t.Fatalf("the menu at %v is not under its anchor at %v once Beside is gone", item, anchor)
}
}

// Above puts the menu over the anchor even where it would fit under it.
func TestMenuAbove(t *testing.T) {
m := &menuOptionsModel{above: true}
Expand Down
Loading