From 413625442efa050c5eaa1f9ff52e258fda1d49c6 Mon Sep 17 00:00:00 2001 From: Yao Jingxi <23722032@bjtu.edu.cn> Date: Tue, 1 Dec 2026 23:38:44 +0800 Subject: [PATCH 1/2] feat(keymap): bind the middle and side mouse buttons in a Shortcut A Chord may now name the middle button or a side button of a mouse (mouse_middle, mouse_back, mouse_forward) with modifiers, and FromPointer reads one from a press. ui.Shortcut and Box.Shortcut take these names: a page-wide one acts on a press anywhere in the page before the element under the pointer has it, and an element's own acts on a press on it or inside it, the innermost winning. The primary and secondary buttons are never a chord: they select, drag and open menus. --- base/accessors.go | 8 ++++ base/page.go | 41 +++++++++++------- base/shortcut.go | 51 +++++++++++++++++++---- keymap/chord.go | 84 ++++++++++++++++++++++++++++++++----- keymap/chord_test.go | 61 +++++++++++++++++++++++++++ keymap/doc.go | 9 ++-- ui/shortcut_test.go | 99 ++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 317 insertions(+), 36 deletions(-) diff --git a/base/accessors.go b/base/accessors.go index 8d0ceca05..ef174d548 100644 --- a/base/accessors.go +++ b/base/accessors.go @@ -880,6 +880,11 @@ func (x *Page) GetShortcutRoute() func(*Page, gfx.KeyEvent) (gfx.EventResult, bo return x.shortcutRoute } +// GetPointerShortcutRoute reads the pointerShortcutRoute of Page for the styled painting layer. +func (x *Page) GetPointerShortcutRoute() func(*Page, gfx.PointerEvent) (gfx.EventResult, bool) { + return x.pointerShortcutRoute +} + // GetShortcuts reads the shortcuts of Page for the styled painting layer. func (x *Page) GetShortcuts() []shortcutEntry { return x.shortcuts } @@ -1882,6 +1887,9 @@ func (x *segmented) GetOptions() []style.SegmentOption { return x.options } // GetSelected reads the selected of segmented for the styled painting layer. func (x *segmented) GetSelected() gfx.ID { return x.selected } +// GetButton reads the button of shortcutEntry for the styled painting layer. +func (x *shortcutEntry) GetButton() gfx.PointerButton { return x.button } + // GetChar reads the char of shortcutEntry for the styled painting layer. func (x *shortcutEntry) GetChar() byte { return x.char } diff --git a/base/page.go b/base/page.go index 9fe13af7e..b491ae920 100644 --- a/base/page.go +++ b/base/page.go @@ -356,20 +356,22 @@ type Page struct { pointerRoute func(*Page, gfx.PointerEvent) (gfx.EventResult, bool) areaKey func(*Field, gfx.KeyEvent) gfx.EventResult shortcutRoute func(*Page, gfx.KeyEvent) (gfx.EventResult, bool) - menuRoute func(*Page, gfx.KeyEvent) (gfx.EventResult, bool) - roveRoute func(*Page, gfx.KeyEvent) (gfx.EventResult, bool) // arrow keys in a Choice or Tabs - groupRoute func(*Page, gfx.KeyEvent) (gfx.EventResult, bool) // arrow keys in a FocusGroup - sliderRoute func(*Page, gfx.KeyEvent) (gfx.EventResult, bool) // the keys of a Slider - splitRoute func(*Page, gfx.KeyEvent) (gfx.EventResult, bool) // the keys of a Splitter - tableKeyRoute func(*Page, gfx.KeyEvent) (gfx.EventResult, bool) // moves the selection of a Table - selectRoute func(*Page, gfx.KeyEvent) (gfx.EventResult, bool) // opens and moves the list of a Select - comboRoute func(*Page, gfx.KeyEvent) (gfx.EventResult, bool) - treeRoute func(*Page, gfx.KeyEvent) (gfx.EventResult, bool) - listRoute func(*Page, gfx.KeyEvent) (gfx.EventResult, bool) // moves and chooses rows of a List - wheelXRoute func(*Page, gfx.ScrollEvent) (moved, only bool) // the sideways wheel of a List with RowWidth - typeRoute func(*Page, gfx.KeyEvent) (gfx.EventResult, bool) // a Tree's typeahead, from OnShortcut - wheelRoute func(*Page, gfx.ScrollEvent) (gfx.EventResult, bool) - textRoute func(*Page, gfx.ID) *window.TextInput + // pointerShortcutRoute is set by the first mouse button's Shortcut. + pointerShortcutRoute func(*Page, gfx.PointerEvent) (gfx.EventResult, bool) + menuRoute func(*Page, gfx.KeyEvent) (gfx.EventResult, bool) + roveRoute func(*Page, gfx.KeyEvent) (gfx.EventResult, bool) // arrow keys in a Choice or Tabs + groupRoute func(*Page, gfx.KeyEvent) (gfx.EventResult, bool) // arrow keys in a FocusGroup + sliderRoute func(*Page, gfx.KeyEvent) (gfx.EventResult, bool) // the keys of a Slider + splitRoute func(*Page, gfx.KeyEvent) (gfx.EventResult, bool) // the keys of a Splitter + tableKeyRoute func(*Page, gfx.KeyEvent) (gfx.EventResult, bool) // moves the selection of a Table + selectRoute func(*Page, gfx.KeyEvent) (gfx.EventResult, bool) // opens and moves the list of a Select + comboRoute func(*Page, gfx.KeyEvent) (gfx.EventResult, bool) + treeRoute func(*Page, gfx.KeyEvent) (gfx.EventResult, bool) + listRoute func(*Page, gfx.KeyEvent) (gfx.EventResult, bool) // moves and chooses rows of a List + wheelXRoute func(*Page, gfx.ScrollEvent) (moved, only bool) // the sideways wheel of a List with RowWidth + typeRoute func(*Page, gfx.KeyEvent) (gfx.EventResult, bool) // a Tree's typeahead, from OnShortcut + wheelRoute func(*Page, gfx.ScrollEvent) (gfx.EventResult, bool) + textRoute func(*Page, gfx.ID) *window.TextInput } // protoNode is a container of the frame: where it is named (in the container at @@ -630,6 +632,8 @@ func (p *Page) onPointer(event gfx.PointerEvent) gfx.EventResult { var result gfx.EventResult if done, ok := p.dropPointer(event); ok { result = done + } else if done, ok := p.routePointerShortcut(event); ok { + result = done } else if p.pointerRoute != nil { if routed, ok := p.pointerRoute(p, event); ok { result = routed @@ -644,6 +648,15 @@ func (p *Page) onPointer(event gfx.PointerEvent) gfx.EventResult { return result } +// routePointerShortcut gives a press of a mouse button to the Shortcut it makes, +// before the element under the pointer has it. +func (p *Page) routePointerShortcut(event gfx.PointerEvent) (gfx.EventResult, bool) { + if p.pointerShortcutRoute == nil { + return gfx.EventResult{}, false + } + return p.pointerShortcutRoute(p, event) +} + // hoverPlaceAt names the innermost scroll area or self-scrolling table under a // point; the zero ID means none. func (p *Page) hoverPlaceAt(x, y float32) gfx.ID { diff --git a/base/shortcut.go b/base/shortcut.go index 328b6c0b0..3cecafece 100644 --- a/base/shortcut.go +++ b/base/shortcut.go @@ -24,6 +24,7 @@ type shortcutEntry struct { modifiers gfx.Modifiers char byte key gfx.Key + button gfx.PointerButton ID gfx.ID focus gfx.ID scope gfx.Scope @@ -32,8 +33,9 @@ type shortcutEntry struct { // Shortcut reports whether this paint delivers the chord modifiers+key pressed // anywhere in the page: a window's Save or Find, declared next to what it does. -// key is a character ("s", "/") or a key's name ("enter", "escape", "delete", -// "f2", "up"; see keymap.Parse). +// key is a character ("s", "/"), a key's name ("enter", "escape", "delete", +// "f2", "up") or a mouse button's ("mouse_middle", "mouse_back", +// "mouse_forward"; see keymap.Parse). // // if ui.Shortcut(c, ui.Primary, "s") { // m.save() @@ -46,6 +48,11 @@ type shortcutEntry struct { // field, Combobox or terminal being typed in. While an overlay is open , a page-wide Shortcut does not act // (the overlay has the keyboard); Box.Shortcut on an element in it does. Not inside // a Memo. +// +// A mouse button's Shortcut acts on the press of the button, anywhere in the page, +// before the press reaches the element under the pointer: a page-wide +// "mouse_back" goes back wherever the pointer is. While an overlay is open it +// does not act. func Shortcut(c C, modifiers gfx.Modifiers, key string) bool { return c.page.shortcut(c, modifiers, key, gfx.ID{}, 0, 0) } @@ -61,6 +68,11 @@ func Shortcut(c C, modifiers gfx.Modifiers, key string) bool { // if list.Shortcut(0, "delete") { // m.removeSelected() // } +// +// A mouse button's Shortcut on an element acts on a press of the button on the +// element or in it, where the keyboard focus is does not matter: a tab's +// "mouse_middle" closes the tab pressed. It wins over a page-wide one of the same +// chord, and the innermost of nested ones acts. func (b Box) Shortcut(modifiers gfx.Modifiers, key string) bool { p := b.f.page if p == nil { @@ -82,8 +94,12 @@ func (p *Page) shortcut(c C, modifiers gfx.Modifiers, key string, focus gfx.ID, panic("ui: Shortcut " + key + ": not a key keymap.Parse names, nor one character") } id := p.ControlID(pkShortcut) - p.shortcuts = append(p.shortcuts, shortcutEntry{modifiers: chord.Modifiers, char: chord.Char, key: chord.Key, ID: id, focus: focus, scope: scope, rows: rows}) - if p.shortcutRoute == nil { + p.shortcuts = append(p.shortcuts, shortcutEntry{modifiers: chord.Modifiers, char: chord.Char, key: chord.Key, button: chord.Button, ID: id, focus: focus, scope: scope, rows: rows}) + if chord.Button != gfx.PointerButtonNone { + if p.pointerShortcutRoute == nil { + p.pointerShortcutRoute = (*Page).matchPointerShortcut + } + } else if p.shortcutRoute == nil { p.keyRoute, p.shortcutRoute = (*Page).RouteKey, (*Page).matchShortcut } return c.f.Surface != nil && c.f.Surface.Clicked(id) @@ -102,14 +118,33 @@ func (p *Page) matchShortcut(event gfx.KeyEvent) (gfx.EventResult, bool) { if chord.Modifiers&(keymap.Primary|gfx.ModifierControl|gfx.ModifierAlt) == 0 && p.typing(event.Target) { return gfx.EventResult{}, false // the key types, or edits, in the field } - target := event.Target - // A page-wide Shortcut acts with the focus in the page, or with none in a window - // the page fills; not while an overlay has the keyboard. + return p.deliverShortcut(chord, event.Target) +} + +// matchPointerShortcut finds the mouse button's Shortcut a press makes where it +// lands, and returns the result that delivers it. +func (p *Page) matchPointerShortcut(event gfx.PointerEvent) (gfx.EventResult, bool) { + if event.Kind != gfx.PointerDown || len(p.shortcuts) == 0 { + return gfx.EventResult{}, false + } + chord, ok := keymap.FromPointer(event) + if !ok { + return gfx.EventResult{}, false + } + return p.deliverShortcut(chord, event.Target) +} + +// deliverShortcut finds the Shortcut of chord nearest target, the focus for a key +// and the element pressed for a mouse button, and returns the result that +// delivers it. +func (p *Page) deliverShortcut(chord keymap.Chord, target gfx.ID) (gfx.EventResult, bool) { + // A page-wide Shortcut acts with the target in the page, or with none in a + // window the page fills; not while an overlay has the keyboard. pageWide := !p.overlay && (p.owns(target) || target.IsZero() && p.window) best := -1 for i := range p.shortcuts { entry := &p.shortcuts[i] - if entry.modifiers != chord.Modifiers || entry.char != chord.Char || entry.key != chord.Key { + if entry.modifiers != chord.Modifiers || entry.char != chord.Char || entry.key != chord.Key || entry.button != chord.Button { continue } switch { diff --git a/keymap/chord.go b/keymap/chord.go index 06af43cb3..41d332b84 100644 --- a/keymap/chord.go +++ b/keymap/chord.go @@ -6,14 +6,20 @@ import ( "github.com/vivaui/viva/gfx" ) -// Chord is a modifier set and one key: a printable character in Char, or a -// named key in Key. A valid chord sets exactly one of the two. Letters are -// stored in lower case, so Shift+P is Modifiers Shift with Char 'p'. The zero -// Chord is no chord at all. +// Chord is a modifier set and one key: a printable character in Char, a named +// key in Key, or a mouse button in Button. A valid chord sets exactly one of the +// three. Letters are stored in lower case, so Shift+P is Modifiers Shift with +// Char 'p'. The zero Chord is no chord at all. +// +// The buttons a chord may use are the middle button and the two side buttons +// (gfx.PointerButtonMiddle, PointerButtonBack, PointerButtonForward): the +// primary and secondary buttons select, drag and open menus, so a chord never +// takes them. type Chord struct { Modifiers gfx.Modifiers Char byte Key gfx.Key + Button gfx.PointerButton } const modifierMask = gfx.ModifierShift | gfx.ModifierControl | gfx.ModifierAlt | gfx.ModifierMeta @@ -55,6 +61,28 @@ var namedKeys = [...]struct { {gfx.KeyF12, "F12", "F12", "f12"}, } +// mouseButtons are the mouse buttons a chord may use, written the same three +// ways as namedKeys. +var mouseButtons = [...]struct { + button gfx.PointerButton + label string + keycap string + saved string +}{ + {gfx.PointerButtonMiddle, "Middle Click", "Middle Click", "mouse_middle"}, + {gfx.PointerButtonBack, "Mouse Back", "Mouse Back", "mouse_back"}, + {gfx.PointerButtonForward, "Mouse Forward", "Mouse Forward", "mouse_forward"}, +} + +func mouseButton(button gfx.PointerButton) (int, bool) { + for index := range mouseButtons { + if mouseButtons[index].button == button { + return index, true + } + } + return 0, false +} + func namedKey(key gfx.Key) (int, bool) { for index := range namedKeys { if namedKeys[index].key == key { @@ -65,12 +93,16 @@ func namedKey(key gfx.Key) (int, bool) { } // Valid reports whether the chord can be bound: only the four modifiers, and -// either a named key or a printable ASCII character that is not an upper-case -// letter. +// one of a named key, a printable ASCII character that is not an upper-case +// letter, or the middle or a side mouse button. func (chord Chord) Valid() bool { if chord.Modifiers&^modifierMask != 0 { return false } + if chord.Button != gfx.PointerButtonNone { + _, bound := mouseButton(chord.Button) + return bound && chord.Key == gfx.KeyUnknown && chord.Char == 0 + } if chord.Key != gfx.KeyUnknown { _, named := namedKey(chord.Key) return named && chord.Char == 0 @@ -103,6 +135,21 @@ func FromEvent(event gfx.KeyEvent) (chord Chord, ok bool) { return chord, chord.Valid() } +// FromPointer is the chord a press of a mouse button makes: the button with the +// modifiers held. ok is false for a button a chord cannot use: the primary and +// secondary buttons, or none. A shortcut acts on the press, so a caller that +// records chords offers PointerDown events. +func FromPointer(event gfx.PointerEvent) (chord Chord, ok bool) { + chord = Chord{Modifiers: event.Modifiers & modifierMask, Button: event.Button} + if !chord.Valid() { + return Chord{}, false + } + return chord, true +} + +// IsMouse reports whether the chord is a mouse button rather than a key. +func (chord Chord) IsMouse() bool { return chord.Button != gfx.PointerButtonNone } + // Label is the chord as a sentence, modifiers first: "Cmd+Shift+P", // "Ctrl+Tab", "Alt+Space". The names are the same on every platform; a menu // that wants the platform's symbols uses Keycaps. An invalid chord has no label. @@ -123,6 +170,10 @@ func (chord Chord) Label() string { if chord.Modifiers&gfx.ModifierShift != 0 { label.WriteString("Shift+") } + if index, ok := mouseButton(chord.Button); ok { + label.WriteString(mouseButtons[index].label) + return label.String() + } if index, ok := namedKey(chord.Key); ok { label.WriteString(namedKeys[index].label) return label.String() @@ -164,6 +215,9 @@ func (chord Chord) Keycaps(dst []string) []string { if chord.Modifiers&gfx.ModifierMeta != 0 { dst = append(dst, modifierKeycap(gfx.ModifierMeta)) } + if index, ok := mouseButton(chord.Button); ok { + return append(dst, mouseButtons[index].keycap) + } if index, ok := namedKey(chord.Key); ok { return append(dst, namedKeys[index].keycap) } @@ -174,12 +228,16 @@ func (chord Chord) Keycaps(dst []string) []string { } // Name is the chord's key as a settings file writes it, without the modifiers: -// the character, or the name of a named key ("page_up", "f5"). An invalid chord -// has no name. Parse reads it back. +// the character, the name of a named key ("page_up", "f5"), or of a mouse +// button ("mouse_middle", "mouse_back", "mouse_forward"). An invalid chord has +// no name. Parse reads it back. func (chord Chord) Name() string { if !chord.Valid() { return "" } + if index, ok := mouseButton(chord.Button); ok { + return mouseButtons[index].saved + } if index, ok := namedKey(chord.Key); ok { return namedKeys[index].saved } @@ -188,9 +246,15 @@ func (chord Chord) Name() string { // Parse is the inverse of Name: the chord with the given modifiers and the key // the name stands for. An upper-case letter is read as its lower-case form. ok -// is false for a name that is neither a named key nor one character, and for a -// result that is not a valid chord. +// is false for a name that is neither a named key, a mouse button nor one +// character, and for a result that is not a valid chord. func Parse(modifiers gfx.Modifiers, name string) (Chord, bool) { + for index := range mouseButtons { + if mouseButtons[index].saved == name { + chord := Chord{Modifiers: modifiers, Button: mouseButtons[index].button} + return chord, chord.Valid() + } + } for index := range namedKeys { if namedKeys[index].saved == name { chord := Chord{Modifiers: modifiers, Key: namedKeys[index].key} diff --git a/keymap/chord_test.go b/keymap/chord_test.go index 3c4326a6d..ad58ee55b 100644 --- a/keymap/chord_test.go +++ b/keymap/chord_test.go @@ -112,3 +112,64 @@ func TestPrimaryIsOneOfTheModifiers(t *testing.T) { t.Fatalf("Primary = %v", Primary) } } + +func TestFromPointerNamesTheMiddleAndSideButtons(t *testing.T) { + for _, test := range []struct { + name string + event gfx.PointerEvent + want Chord + ok bool + }{ + {"middle", gfx.PointerEvent{Kind: gfx.PointerDown, Button: gfx.PointerButtonMiddle}, Chord{Button: gfx.PointerButtonMiddle}, true}, + {"back with a modifier", gfx.PointerEvent{Kind: gfx.PointerDown, Button: gfx.PointerButtonBack, Modifiers: gfx.ModifierControl}, Chord{Modifiers: gfx.ModifierControl, Button: gfx.PointerButtonBack}, true}, + {"forward", gfx.PointerEvent{Kind: gfx.PointerDown, Button: gfx.PointerButtonForward, Modifiers: Primary | gfx.ModifierShift}, Chord{Modifiers: Primary | gfx.ModifierShift, Button: gfx.PointerButtonForward}, true}, + {"modifiers beyond the four are dropped", gfx.PointerEvent{Button: gfx.PointerButtonMiddle, Modifiers: 1<<6 | gfx.ModifierAlt}, Chord{Modifiers: gfx.ModifierAlt, Button: gfx.PointerButtonMiddle}, true}, + {"primary selects", gfx.PointerEvent{Kind: gfx.PointerDown, Button: gfx.PointerButtonPrimary}, Chord{}, false}, + {"secondary opens a menu", gfx.PointerEvent{Kind: gfx.PointerDown, Button: gfx.PointerButtonSecondary, Modifiers: Primary}, Chord{}, false}, + {"no button", gfx.PointerEvent{Kind: gfx.PointerMove}, Chord{}, false}, + } { + got, ok := FromPointer(test.event) + if ok != test.ok || got != test.want { + t.Errorf("%s: FromPointer = %+v, %v; want %+v, %v", test.name, got, ok, test.want, test.ok) + } + if got.IsMouse() != test.ok { + t.Errorf("%s: IsMouse = %v", test.name, got.IsMouse()) + } + } +} + +func TestMouseChordsWriteThemselvesAndRoundTrip(t *testing.T) { + for _, test := range []struct { + chord Chord + label, keycap, saved string + }{ + {Chord{Button: gfx.PointerButtonMiddle}, "Middle Click", "Middle Click", "mouse_middle"}, + {Chord{Modifiers: gfx.ModifierControl, Button: gfx.PointerButtonBack}, "Ctrl+Mouse Back", "Mouse Back", "mouse_back"}, + {Chord{Modifiers: gfx.ModifierAlt | gfx.ModifierShift, Button: gfx.PointerButtonForward}, "Alt+Shift+Mouse Forward", "Mouse Forward", "mouse_forward"}, + } { + if got := test.chord.Label(); got != test.label { + t.Errorf("%+v: Label = %q, want %q", test.chord, got, test.label) + } + if got := test.chord.Name(); got != test.saved { + t.Errorf("%+v: Name = %q, want %q", test.chord, got, test.saved) + } + if caps := test.chord.Keycaps(nil); len(caps) == 0 || caps[len(caps)-1] != test.keycap { + t.Errorf("%+v: Keycaps = %q", test.chord, caps) + } + parsed, ok := Parse(test.chord.Modifiers, test.chord.Name()) + if !ok || parsed != test.chord { + t.Errorf("Parse(%q) = %+v, %v; want %+v", test.chord.Name(), parsed, ok, test.chord) + } + } + for _, chord := range []Chord{ + {Button: gfx.PointerButtonPrimary}, + {Button: gfx.PointerButtonSecondary}, + {Button: gfx.PointerButtonMiddle, Char: 'a'}, + {Button: gfx.PointerButtonBack, Key: gfx.KeyTab}, + {Button: gfx.PointerButtonForward + 1}, + } { + if chord.Valid() || chord.Name() != "" || chord.Label() != "" { + t.Errorf("%+v is a valid chord", chord) + } + } +} diff --git a/keymap/doc.go b/keymap/doc.go index 7e5a25bd2..55655e2d1 100644 --- a/keymap/doc.go +++ b/keymap/doc.go @@ -1,9 +1,10 @@ -// Package keymap names keyboard shortcuts and keeps a user-editable table of +// Package keymap names keyboard and mouse-button shortcuts and keeps a user-editable table of // them. // -// A Chord is a set of modifiers and one key: a printable ASCII character, or -// one of the named keys that type nothing (Tab, the arrows, the function keys). -// Chord.FromEvent turns the gfx.KeyEvent a window delivers into one, and a chord +// A Chord is a set of modifiers and one key: a printable ASCII character, one +// of the named keys that type nothing (Tab, the arrows, the function keys), or +// the middle or a side mouse button. FromEvent turns the gfx.KeyEvent a window +// delivers into one and FromPointer a press of a mouse button, and a chord // writes itself three ways: Label for a sentence ("Cmd+Shift+P"), Keycaps for // the symbols a menu paints (⌘ ⇧ P on macOS, Ctrl Shift P elsewhere) and Name // for a settings file ("page_up"), which Parse reads back. diff --git a/ui/shortcut_test.go b/ui/shortcut_test.go index 08a46be95..b8c6eb135 100644 --- a/ui/shortcut_test.go +++ b/ui/shortcut_test.go @@ -193,3 +193,102 @@ func TestListShortcutActsWhileARowHasFocus(t *testing.T) { t.Fatalf("the List's Delete acted %d times with a row focused", deletes) } } + +// mouseModel is a window that goes back on the mouse's back button anywhere, +// closes a tab on a middle click on it, and has a strip whose middle click acts on +// any tab inside it. +type mouseModel struct { + backs, ctrlForwards, tabCloses, stripCloses, pageMiddles, clicks int + menu bool +} + +func (m *mouseModel) page(c ui.C) { + if ui.Shortcut(c, 0, "mouse_back") { + m.backs++ + } + if ui.Shortcut(c, ui.Ctrl, "mouse_forward") { + m.ctrlForwards++ + } + if ui.Shortcut(c, 0, "mouse_middle") { + m.pageMiddles++ + } + ui.Column(c).Pad(16).Gap(8).Children(func() { + tab := ui.Button(c, "Tab") + if tab.Clicked() { + m.clicks++ + } + if tab.Shortcut(0, "mouse_middle") { + m.tabCloses++ + } + strip := ui.Row(c).Gap(4).Children(func() { + ui.Button(c, "Inside") + }) + if strip.Shortcut(0, "mouse_middle") { + m.stripCloses++ + } + ui.Button(c, "Elsewhere") + more := ui.Button(c, "More") + if more.Clicked() { + m.menu = !m.menu + } + ui.Popover(c, more, &m.menu, ui.PopoverSpec{Align: ui.Start}, func() { ui.Button(c, "Rename") }) + }) +} + +func (d *pageDriver) press(Label string, button gfx.PointerButton) { + d.t.Helper() + x, y := center(d.hit(Label).Bounds) + if err := d.driver.Press(x, y, button); err != nil { + d.t.Fatal(err) + } + if err := d.driver.Release(x, y, button); err != nil { + d.t.Fatal(err) + } +} + +// A mouse button's Shortcut acts on a press wherever it lands, the innermost +// element's own winning over a page-wide one, and never clicks what it lands on. +func TestShortcutActsOnMouseButtonsWhereThePressLands(t *testing.T) { + m := &mouseModel{} + d := openPage(t, m.page) + + d.press("Elsewhere", gfx.PointerButtonBack) + if m.backs != 1 { + t.Fatalf("back button: backs %d", m.backs) + } + d.press("Tab", gfx.PointerButtonForward) + if m.ctrlForwards != 0 { + t.Fatal("forward without Control acted on the Control+forward Shortcut") + } + d.withModifier(gfx.KeyControlLeft, func() { d.press("Tab", gfx.PointerButtonForward) }) + if m.ctrlForwards != 1 { + t.Fatalf("Control+forward: %d", m.ctrlForwards) + } + + d.press("Tab", gfx.PointerButtonMiddle) + if m.tabCloses != 1 || m.pageMiddles != 0 || m.clicks != 0 { + t.Fatalf("middle click on the tab: tab %d, page %d, clicks %d", m.tabCloses, m.pageMiddles, m.clicks) + } + d.press("Inside", gfx.PointerButtonMiddle) + if m.stripCloses != 1 || m.pageMiddles != 0 { + t.Fatalf("middle click in the strip: strip %d, page %d", m.stripCloses, m.pageMiddles) + } + d.press("Elsewhere", gfx.PointerButtonMiddle) + if m.pageMiddles != 1 || m.tabCloses != 1 || m.stripCloses != 1 { + t.Fatalf("middle click elsewhere: page %d, tab %d, strip %d", m.pageMiddles, m.tabCloses, m.stripCloses) + } + + d.click("Tab") + if m.clicks != 1 || m.tabCloses != 1 { + t.Fatalf("primary click: clicks %d, closes %d", m.clicks, m.tabCloses) + } + + d.click("More") + if !m.menu { + t.Fatal("the popover did not open") + } + d.press("Rename", gfx.PointerButtonBack) + if m.backs != 1 { + t.Fatal("a page-wide mouse Shortcut acted while an overlay was open") + } +} From 349189f61ba66efc68ca915c8bdc606135b0ddb4 Mon Sep 17 00:00:00 2001 From: Yao Jingxi <23722032@bjtu.edu.cn> Date: Wed, 2 Dec 2026 00:04:49 +0800 Subject: [PATCH 2/2] fix(base): a container's Shortcut acts on the container itself A container that names a place and has a hit of its own, such as a tab holding an icon and a close button, matched its Shortcut only for targets inside the place: a press on the tab itself, or the focus on it, fell through to the page-wide Shortcut. The entry now keeps the container's identity, and a target equal to it counts as on the element. --- base/shortcut.go | 11 +++++++++-- ui/shortcut_test.go | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/base/shortcut.go b/base/shortcut.go index 3cecafece..dd8fee224 100644 --- a/base/shortcut.go +++ b/base/shortcut.go @@ -29,6 +29,11 @@ type shortcutEntry struct { focus gfx.ID scope gfx.Scope rows gfx.Scope // a List's or a Tree's rows, which its box's Shortcut also covers + // own is the identity of a container that names a place and has a hit of its + // own, as a tab holding an icon and a close button does: the focus or a press + // on the container itself is on the element, though its identity is not in + // the place it names. + own gfx.ID } // Shortcut reports whether this paint delivers the chord modifiers+key pressed @@ -79,7 +84,9 @@ func (b Box) Shortcut(modifiers gfx.Modifiers, key string) bool { panic("ui: Box.Shortcut needs a page: declare it with a ui.C") } if int(b.i) < len(p.Nodes) && p.Nodes[b.i].named { - return p.shortcut(&p.ctx, modifiers, key, gfx.ID{}, p.resolve(b.i), p.listRowScope(b.i)) + ok := p.shortcut(&p.ctx, modifiers, key, gfx.ID{}, p.resolve(b.i), p.listRowScope(b.i)) + p.shortcuts[len(p.shortcuts)-1].own = b.N().ID + return ok } if id := b.N().ID; !id.IsZero() { return p.shortcut(&p.ctx, modifiers, key, id, 0, 0) @@ -153,7 +160,7 @@ func (p *Page) deliverShortcut(chord keymap.Chord, target gfx.ID) (gfx.EventResu continue } case entry.scope != 0: - if target.IsZero() || !p.shortcutCovers(entry, target.Scope()) { + if target.IsZero() || target != entry.own && !p.shortcutCovers(entry, target.Scope()) { continue } case !pageWide: diff --git a/ui/shortcut_test.go b/ui/shortcut_test.go index b8c6eb135..c84038bed 100644 --- a/ui/shortcut_test.go +++ b/ui/shortcut_test.go @@ -199,6 +199,7 @@ func TestListShortcutActsWhileARowHasFocus(t *testing.T) { // any tab inside it. type mouseModel struct { backs, ctrlForwards, tabCloses, stripCloses, pageMiddles, clicks int + chipCloses, chipButtons int menu bool } @@ -226,6 +227,18 @@ func (m *mouseModel) page(c ui.C) { if strip.Shortcut(0, "mouse_middle") { m.stripCloses++ } + // A tab drawn as a container: its own hit, a title without one, and a + // close button inside it. + k := c.Key("chip") + chip := ui.Row(k).Gap(4).Pad(4).Hit(ui.ID(k), "Chip").Children(func() { + ui.Text(c, "Chip title") + if ui.Button(k, "Close chip").Clicked() { + m.chipButtons++ + } + }) + if chip.Shortcut(0, "mouse_middle") { + m.chipCloses++ + } ui.Button(c, "Elsewhere") more := ui.Button(c, "More") if more.Clicked() { @@ -273,6 +286,14 @@ func TestShortcutActsOnMouseButtonsWhereThePressLands(t *testing.T) { if m.stripCloses != 1 || m.pageMiddles != 0 { t.Fatalf("middle click in the strip: strip %d, page %d", m.stripCloses, m.pageMiddles) } + d.press("Chip", gfx.PointerButtonMiddle) + if m.chipCloses != 1 || m.pageMiddles != 0 { + t.Fatalf("middle click on a container with a hit of its own: chip %d, page %d", m.chipCloses, m.pageMiddles) + } + d.press("Close chip", gfx.PointerButtonMiddle) + if m.chipCloses != 2 || m.chipButtons != 0 || m.pageMiddles != 0 { + t.Fatalf("middle click on a button in the container: chip %d, button %d, page %d", m.chipCloses, m.chipButtons, m.pageMiddles) + } d.press("Elsewhere", gfx.PointerButtonMiddle) if m.pageMiddles != 1 || m.tabCloses != 1 || m.stripCloses != 1 { t.Fatalf("middle click elsewhere: page %d, tab %d, strip %d", m.pageMiddles, m.tabCloses, m.stripCloses)