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
73 changes: 73 additions & 0 deletions ui/menu_icon_inset_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package ui_test

import (
"testing"

"github.com/vivaui/viva/gfx"
"github.com/vivaui/viva/ui"
)

// A menu that shows an icon on one row keeps the icon's room on its rows without
// one, its submenu triggers, checkboxes and labels too, so that every label starts
// at one edge; a menu with no icon keeps its labels at the row's edge.
func TestMenuRowsWithoutAnIconKeepTheIconsRoom(t *testing.T) {
for _, context := range []bool{false, true} {
name := "dropdown"
if context {
name = "context"
}
t.Run(name, func(t *testing.T) {
icons := true
open := false
d := openPage(t, func(c ui.C) {
anchor := ui.Button(c, "More")
if anchor.Clicked() {
open = true
}
body := func(menu *ui.MenuItems) {
menu.Label("Group")
item := menu.Item("Cut")
if icons {
item = item.Icon(testIcon)
}
item.Clicked()
menu.Item("Paste").Clicked()
menu.Item("Wrap").Checked(true).Clicked()
menu.Sub("Copy special", func(sub *ui.MenuItems) {
sub.Item("Copy path").Clicked()
})
}
if context {
ui.ContextMenuAt(c, anchor, &open, gfx.Point{X: 40, Y: 80}, body)
} else {
ui.Menu(c, anchor, &open, body)
}
})
d.click("More")
left := func(label string) float32 {
t.Helper()
texts := d.textsNamed(label)
if len(texts) != 1 {
t.Fatalf("%q is painted %d times, want once", label, len(texts))
}
return texts[0].X - d.hit("Paste").Bounds.X
}
cut := left("Cut")
for _, label := range []string{"Group", "Paste", "Wrap", "Copy special"} {
if got := left(label); got != cut {
t.Fatalf("%q starts %v into its row and the icon row's label %v", label, got, cut)
}
}

icons = false
d.repaint()
plain := left("Paste")
if plain >= cut {
t.Fatalf("with no icon in the menu, the label starts %v into its row, want less than the %v an icon takes", plain, cut)
}
if got := left("Cut"); got != plain {
t.Fatalf("with no icon in the menu, Cut starts at %v and Paste at %v", got, plain)
}
})
}
}
66 changes: 30 additions & 36 deletions ui/menu_paint.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,6 @@ import (
"github.com/vivaui/viva/keymap"
)

// row is row i as the components paint it. Its Keys are the MenuItems' scratch,
// good until the next row.
func menuItemsRow(m *MenuItems, i int) components.DropdownMenuItem {
row := &m.Items[i]
item := components.DropdownMenuItem{ID: m.GetScope().At(MenuRowsPart + i), Label: row.Label, Mnemonic: row.GetMnemonic(), Description: row.GetDescription(), Disabled: row.GetDisabled(), Icon: row.GetIcon(), TrailingWidth: row.GetTrailingWidth()}
if row.GetDestructive() {
item.Variant = components.DropdownMenuItemDestructive
}
if chord := (keymap.Chord{Modifiers: row.GetModifiers(), Char: row.GetChar(), Key: row.GetKey()}); chord.Valid() {
item.Keys = chord.Keycaps(m.KeysSlice()[:0])
}
switch row.GetChecked() {
case 1, 2:
item.Kind, item.Checked = components.DropdownMenuCheckbox, row.GetChecked() == 1
case 3, 4:
item.Kind, item.Checked = components.DropdownMenuRadio, row.GetChecked() == 3
}
item.Selected = item.Checked
if row.GetSubmenu() {
item.Kind, item.Expanded = components.DropdownMenuSubmenu, i == m.GetSub()
}
return item
}

// rowExtent is how tall row i is, as size and the page rows' placement both need.
func rowExtent(m *MenuItems, i int) float32 {
row := &m.Items[i]
Expand All @@ -45,9 +21,9 @@ func rowExtent(m *MenuItems, i int) float32 {
// Both branches size a row by GetRowSize, which RowHeight sets: a dropdown of
// the application's own is asked for the same rows the context menu gets.
if m.GetContext() {
return components.ContextMenuItemHeight(m.GetF().Palette, contextItem(menuItemRow(m, i)), m.GetRowSize())
return components.ContextMenuItemHeight(m.GetF().Palette, contextItem(menuItemRow(m, i, false)), m.GetRowSize())
}
return components.DropdownMenuItemHeightFor(m.GetF().Palette, menuItemRow(m, i), m.GetRowSize())
return components.DropdownMenuItemHeightFor(m.GetF().Palette, menuItemRow(m, i, false), m.GetRowSize())
}

// paintContent calls a content row's painter over its row, inside the menu's clip.
Expand Down Expand Up @@ -75,6 +51,7 @@ func paintAt(m *MenuItems, Surface *gfx.Canvas, Palette *Theme, bounds Rect, lay
dismiss = scope.At(MenuDismiss)
}
var trigger Rect // the row of the open submenu
inset := hasIconRow(m)
if m.GetContext() {
menu := components.BeginContextMenu(Surface, *Palette, bounds, components.ContextMenuOptions{
Layer: layer, Scope: focus, DismissID: dismiss, Viewport: f.GetBounds(), ID: id,
Expand All @@ -86,19 +63,19 @@ func paintAt(m *MenuItems, Surface *gfx.Canvas, Palette *Theme, bounds Rect, lay
case MenuRowSeparator:
menu.AddSeparator()
case MenuRowLabel:
menu.AddLabel(row.Label, false)
menu.AddLabel(row.Label, inset)
case MenuRowContent:
paintContent(m, Surface, *Palette, row, menu.AddContent(row.GetHeight()), func() { menu.BeginContent() }, func() { menu.EndContent() }, menu.Bounds())
case MenuRowPageContent:
menu.AddContent(row.GetHeight()) // the page declares the row's controls
default:
if row.GetTrailing() != nil {
r, slot := menu.AddItemSlot(contextItem(menuItemRow(m, i)))
r, slot := menu.AddItemSlot(contextItem(menuItemRow(m, i, inset)))
if i == m.GetSub() {
trigger = r
}
m.PaintTrailing(Surface, *Palette, row, slot, menu.BeginContent, menu.EndContent)
} else if r := menu.AddItem(contextItem(menuItemRow(m, i))); i == m.GetSub() {
} else if r := menu.AddItem(contextItem(menuItemRow(m, i, inset))); i == m.GetSub() {
trigger = r
}
}
Expand Down Expand Up @@ -130,19 +107,19 @@ func paintAt(m *MenuItems, Surface *gfx.Canvas, Palette *Theme, bounds Rect, lay
case MenuRowSeparator:
menu.AddSeparator()
case MenuRowLabel:
menu.AddLabel(row.Label, false)
menu.AddLabel(row.Label, inset)
case MenuRowContent:
paintContent(m, Surface, *Palette, row, menu.AddContent(row.GetHeight()), func() { menu.BeginContent() }, func() { menu.EndContent() }, menu.Bounds())
case MenuRowPageContent:
menu.AddContent(row.GetHeight()) // the page declares the row's controls
default:
if row.GetTrailing() != nil {
r, slot := menu.AddItemSlot(menuItemRow(m, i))
r, slot := menu.AddItemSlot(menuItemRow(m, i, inset))
if i == m.GetSub() {
trigger = r
}
m.PaintTrailing(Surface, *Palette, row, slot, menu.BeginContent, menu.EndContent)
} else if r := menu.AddItem(menuItemRow(m, i)); i == m.GetSub() {
} else if r := menu.AddItem(menuItemRow(m, i, inset)); i == m.GetSub() {
trigger = r
}
}
Expand All @@ -169,7 +146,7 @@ func paintAt(m *MenuItems, Surface *gfx.Canvas, Palette *Theme, bounds Rect, lay
func contextItem(item components.DropdownMenuItem) components.ContextMenuItem {
context := components.ContextMenuItem{
ID: item.ID, Label: item.Label, Mnemonic: item.Mnemonic, Description: item.Description, Keys: item.Keys, Icon: item.Icon, Disabled: item.Disabled, TrailingWidth: item.TrailingWidth,
Checked: item.Checked, Selected: item.Selected,
Checked: item.Checked, Selected: item.Selected, Inset: item.Inset,
}
switch item.Kind {
case components.DropdownMenuCheckbox:
Expand All @@ -190,13 +167,14 @@ func menuSize(m *MenuItems) (width, height float32) {
f := m.GetF()
theme := &f.Palette
width, height = m.MinWidth(), 2*base.DropdownMenuPadding
inset := hasIconRow(m)
for i := range m.Items {
item := &m.Items[i]
height += rowExtent(m, i)
switch item.Kind {
case MenuRowSeparator, MenuRowLabel, MenuRowContent, MenuRowPageContent:
default:
row := menuItemRow(m, i)
row := menuItemRow(m, i, inset)
if m.GetContext() {
if f.Surface != nil {
width = max(width, components.ContextMenuItemWidth(f.Surface, *theme, contextItem(row)))
Expand All @@ -217,11 +195,27 @@ func menuSize(m *MenuItems) (width, height float32) {
return width, height
}

// hasIconRow reports whether an item row of m shows an icon. The rows without one,
// and the menu's labels, then keep the icon's room, so that every label of the
// menu starts at one edge.
func hasIconRow(m *MenuItems) bool {
for i := range m.Items {
if row := &m.Items[i]; row.Kind == MenuRowItem && hasIcon(row.GetIcon()) {
return true
}
}
return false
}

func hasIcon(icon gfx.Icon) bool { return len(icon.Commands) > 0 || len(icon.Layers) > 0 }

// menuItemRow is row i as the components paint it. Its Keys are the MenuItems'
// scratch, good until the next row.
func menuItemRow(m *MenuItems, i int) components.DropdownMenuItem {
// scratch, good until the next row. inset, which hasIconRow gives, makes a row
// without an icon keep the room of one.
func menuItemRow(m *MenuItems, i int, inset bool) components.DropdownMenuItem {
row := &m.Items[i]
item := components.DropdownMenuItem{ID: m.GetScope().At(MenuRowsPart + i), Label: row.Label, Mnemonic: row.GetMnemonic(), Description: row.GetDescription(), Disabled: row.GetDisabled(), Icon: row.GetIcon(), TrailingWidth: row.GetTrailingWidth()}
item.Inset = inset && !hasIcon(item.Icon)
if row.GetDestructive() {
item.Variant = components.DropdownMenuItemDestructive
}
Expand Down
Loading