Skip to content
Closed
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
8 changes: 8 additions & 0 deletions base/accessors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down Expand Up @@ -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 }

Expand Down
41 changes: 27 additions & 14 deletions base/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
62 changes: 52 additions & 10 deletions base/shortcut.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,23 @@ type shortcutEntry struct {
modifiers gfx.Modifiers
char byte
key gfx.Key
button gfx.PointerButton
ID gfx.ID
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
// 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()
Expand All @@ -46,6 +53,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)
}
Expand All @@ -61,13 +73,20 @@ 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 {
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)
Expand All @@ -82,8 +101,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)
Expand All @@ -102,14 +125,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 {
Expand All @@ -118,7 +160,7 @@ func (p *Page) matchShortcut(event gfx.KeyEvent) (gfx.EventResult, bool) {
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:
Expand Down
84 changes: 74 additions & 10 deletions keymap/chord.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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()
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
}
Expand All @@ -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}
Expand Down
Loading
Loading