mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-04 00:45:35 +00:00
io/key,io/input: [API] move FocusDirection to package io/key
Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
+11
-11
@@ -565,19 +565,19 @@ func (c *callbacks) SetEditorSnippet(r key.Range) {
|
|||||||
c.Event(key.SnippetEvent(r))
|
c.Event(key.SnippetEvent(r))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *Window) moveFocus(dir input.FocusDirection, d driver) {
|
func (w *Window) moveFocus(dir key.FocusDirection, d driver) {
|
||||||
if w.queue.MoveFocus(dir) {
|
if w.queue.MoveFocus(dir) {
|
||||||
w.queue.RevealFocus(w.viewport)
|
w.queue.RevealFocus(w.viewport)
|
||||||
} else {
|
} else {
|
||||||
var v image.Point
|
var v image.Point
|
||||||
switch dir {
|
switch dir {
|
||||||
case input.FocusRight:
|
case key.FocusRight:
|
||||||
v = image.Pt(+1, 0)
|
v = image.Pt(+1, 0)
|
||||||
case input.FocusLeft:
|
case key.FocusLeft:
|
||||||
v = image.Pt(-1, 0)
|
v = image.Pt(-1, 0)
|
||||||
case input.FocusDown:
|
case key.FocusDown:
|
||||||
v = image.Pt(0, +1)
|
v = image.Pt(0, +1)
|
||||||
case input.FocusUp:
|
case key.FocusUp:
|
||||||
v = image.Pt(0, -1)
|
v = image.Pt(0, -1)
|
||||||
default:
|
default:
|
||||||
return
|
return
|
||||||
@@ -895,17 +895,17 @@ func (w *Window) processEvent(d driver, e event.Event) bool {
|
|||||||
isMobile := runtime.GOOS == "ios" || runtime.GOOS == "android"
|
isMobile := runtime.GOOS == "ios" || runtime.GOOS == "android"
|
||||||
switch {
|
switch {
|
||||||
case e.Name == key.NameTab && e.Modifiers == 0:
|
case e.Name == key.NameTab && e.Modifiers == 0:
|
||||||
w.moveFocus(input.FocusForward, d)
|
w.moveFocus(key.FocusForward, d)
|
||||||
case e.Name == key.NameTab && e.Modifiers == key.ModShift:
|
case e.Name == key.NameTab && e.Modifiers == key.ModShift:
|
||||||
w.moveFocus(input.FocusBackward, d)
|
w.moveFocus(key.FocusBackward, d)
|
||||||
case e.Name == key.NameUpArrow && e.Modifiers == 0 && isMobile:
|
case e.Name == key.NameUpArrow && e.Modifiers == 0 && isMobile:
|
||||||
w.moveFocus(input.FocusUp, d)
|
w.moveFocus(key.FocusUp, d)
|
||||||
case e.Name == key.NameDownArrow && e.Modifiers == 0 && isMobile:
|
case e.Name == key.NameDownArrow && e.Modifiers == 0 && isMobile:
|
||||||
w.moveFocus(input.FocusDown, d)
|
w.moveFocus(key.FocusDown, d)
|
||||||
case e.Name == key.NameLeftArrow && e.Modifiers == 0 && isMobile:
|
case e.Name == key.NameLeftArrow && e.Modifiers == 0 && isMobile:
|
||||||
w.moveFocus(input.FocusLeft, d)
|
w.moveFocus(key.FocusLeft, d)
|
||||||
case e.Name == key.NameRightArrow && e.Modifiers == 0 && isMobile:
|
case e.Name == key.NameRightArrow && e.Modifiers == 0 && isMobile:
|
||||||
w.moveFocus(input.FocusRight, d)
|
w.moveFocus(key.FocusRight, d)
|
||||||
default:
|
default:
|
||||||
handled = false
|
handled = false
|
||||||
}
|
}
|
||||||
|
|||||||
+8
-19
@@ -65,17 +65,6 @@ const (
|
|||||||
TextInputOpen
|
TextInputOpen
|
||||||
)
|
)
|
||||||
|
|
||||||
type FocusDirection int
|
|
||||||
|
|
||||||
const (
|
|
||||||
FocusRight FocusDirection = iota
|
|
||||||
FocusLeft
|
|
||||||
FocusUp
|
|
||||||
FocusDown
|
|
||||||
FocusForward
|
|
||||||
FocusBackward
|
|
||||||
)
|
|
||||||
|
|
||||||
// InputState returns the last text input state as
|
// InputState returns the last text input state as
|
||||||
// determined in Frame.
|
// determined in Frame.
|
||||||
func (q *keyQueue) InputState() TextInputState {
|
func (q *keyQueue) InputState() TextInputState {
|
||||||
@@ -171,7 +160,7 @@ func (q *keyQueue) updateFocusLayout() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// MoveFocus attempts to move the focus in the direction of dir, returning true if it succeeds.
|
// MoveFocus attempts to move the focus in the direction of dir, returning true if it succeeds.
|
||||||
func (q *keyQueue) MoveFocus(dir FocusDirection, events *handlerEvents) bool {
|
func (q *keyQueue) MoveFocus(dir key.FocusDirection, events *handlerEvents) bool {
|
||||||
if len(q.dirOrder) == 0 {
|
if len(q.dirOrder) == 0 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@@ -181,17 +170,17 @@ func (q *keyQueue) MoveFocus(dir FocusDirection, events *handlerEvents) bool {
|
|||||||
}
|
}
|
||||||
focus := q.dirOrder[order]
|
focus := q.dirOrder[order]
|
||||||
switch dir {
|
switch dir {
|
||||||
case FocusForward, FocusBackward:
|
case key.FocusForward, key.FocusBackward:
|
||||||
if len(q.order) == 0 {
|
if len(q.order) == 0 {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
order := 0
|
order := 0
|
||||||
if dir == FocusBackward {
|
if dir == key.FocusBackward {
|
||||||
order = -1
|
order = -1
|
||||||
}
|
}
|
||||||
if q.focus != nil {
|
if q.focus != nil {
|
||||||
order = q.handlers[q.focus].order
|
order = q.handlers[q.focus].order
|
||||||
if dir == FocusForward {
|
if dir == key.FocusForward {
|
||||||
order++
|
order++
|
||||||
} else {
|
} else {
|
||||||
order--
|
order--
|
||||||
@@ -200,11 +189,11 @@ func (q *keyQueue) MoveFocus(dir FocusDirection, events *handlerEvents) bool {
|
|||||||
order = (order + len(q.order)) % len(q.order)
|
order = (order + len(q.order)) % len(q.order)
|
||||||
q.setFocus(q.order[order], events)
|
q.setFocus(q.order[order], events)
|
||||||
return true
|
return true
|
||||||
case FocusRight, FocusLeft:
|
case key.FocusRight, key.FocusLeft:
|
||||||
next := order
|
next := order
|
||||||
if q.focus != nil {
|
if q.focus != nil {
|
||||||
next = order + 1
|
next = order + 1
|
||||||
if dir == FocusLeft {
|
if dir == key.FocusLeft {
|
||||||
next = order - 1
|
next = order - 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -215,9 +204,9 @@ func (q *keyQueue) MoveFocus(dir FocusDirection, events *handlerEvents) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case FocusUp, FocusDown:
|
case key.FocusUp, key.FocusDown:
|
||||||
delta := +1
|
delta := +1
|
||||||
if dir == FocusUp {
|
if dir == key.FocusUp {
|
||||||
delta = -1
|
delta = -1
|
||||||
}
|
}
|
||||||
nextRow := 0
|
nextRow := 0
|
||||||
|
|||||||
+13
-13
@@ -241,26 +241,26 @@ func TestDirectionalFocus(t *testing.T) {
|
|||||||
}
|
}
|
||||||
r.Frame(ops)
|
r.Frame(ops)
|
||||||
|
|
||||||
r.MoveFocus(FocusLeft)
|
r.MoveFocus(key.FocusLeft)
|
||||||
assertFocus(t, r, &handlers[0])
|
assertFocus(t, r, &handlers[0])
|
||||||
r.MoveFocus(FocusLeft)
|
r.MoveFocus(key.FocusLeft)
|
||||||
assertFocus(t, r, &handlers[0])
|
assertFocus(t, r, &handlers[0])
|
||||||
r.MoveFocus(FocusRight)
|
r.MoveFocus(key.FocusRight)
|
||||||
assertFocus(t, r, &handlers[1])
|
assertFocus(t, r, &handlers[1])
|
||||||
r.MoveFocus(FocusRight)
|
r.MoveFocus(key.FocusRight)
|
||||||
assertFocus(t, r, &handlers[1])
|
assertFocus(t, r, &handlers[1])
|
||||||
r.MoveFocus(FocusDown)
|
r.MoveFocus(key.FocusDown)
|
||||||
assertFocus(t, r, &handlers[2])
|
assertFocus(t, r, &handlers[2])
|
||||||
r.MoveFocus(FocusDown)
|
r.MoveFocus(key.FocusDown)
|
||||||
assertFocus(t, r, &handlers[2])
|
assertFocus(t, r, &handlers[2])
|
||||||
r.MoveFocus(FocusLeft)
|
r.MoveFocus(key.FocusLeft)
|
||||||
assertFocus(t, r, &handlers[3])
|
assertFocus(t, r, &handlers[3])
|
||||||
r.MoveFocus(FocusUp)
|
r.MoveFocus(key.FocusUp)
|
||||||
assertFocus(t, r, &handlers[0])
|
assertFocus(t, r, &handlers[0])
|
||||||
|
|
||||||
r.MoveFocus(FocusForward)
|
r.MoveFocus(key.FocusForward)
|
||||||
assertFocus(t, r, &handlers[1])
|
assertFocus(t, r, &handlers[1])
|
||||||
r.MoveFocus(FocusBackward)
|
r.MoveFocus(key.FocusBackward)
|
||||||
assertFocus(t, r, &handlers[0])
|
assertFocus(t, r, &handlers[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -285,7 +285,7 @@ func TestFocusScroll(t *testing.T) {
|
|||||||
parent.Pop()
|
parent.Pop()
|
||||||
r.Frame(ops)
|
r.Frame(ops)
|
||||||
|
|
||||||
r.MoveFocus(FocusLeft)
|
r.MoveFocus(key.FocusLeft)
|
||||||
r.RevealFocus(image.Rect(0, 0, 15, 40))
|
r.RevealFocus(image.Rect(0, 0, 15, 40))
|
||||||
evts := r.Events(h)
|
evts := r.Events(h)
|
||||||
assertScrollEvent(t, evts[len(evts)-1], f32.Pt(6, -9))
|
assertScrollEvent(t, evts[len(evts)-1], f32.Pt(6, -9))
|
||||||
@@ -305,14 +305,14 @@ func TestFocusClick(t *testing.T) {
|
|||||||
cl.Pop()
|
cl.Pop()
|
||||||
r.Frame(ops)
|
r.Frame(ops)
|
||||||
|
|
||||||
r.MoveFocus(FocusLeft)
|
r.MoveFocus(key.FocusLeft)
|
||||||
r.ClickFocus()
|
r.ClickFocus()
|
||||||
assertEventPointerTypeSequence(t, r.Events(h), pointer.Cancel, pointer.Press, pointer.Release)
|
assertEventPointerTypeSequence(t, r.Events(h), pointer.Cancel, pointer.Press, pointer.Release)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNoFocus(t *testing.T) {
|
func TestNoFocus(t *testing.T) {
|
||||||
r := new(Router)
|
r := new(Router)
|
||||||
r.MoveFocus(FocusForward)
|
r.MoveFocus(key.FocusForward)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestKeyRouting(t *testing.T) {
|
func TestKeyRouting(t *testing.T) {
|
||||||
|
|||||||
+1
-1
@@ -236,7 +236,7 @@ func (q *Router) queueKeyEvent(e key.Event) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Router) MoveFocus(dir FocusDirection) bool {
|
func (q *Router) MoveFocus(dir key.FocusDirection) bool {
|
||||||
return q.key.queue.MoveFocus(dir, &q.handlers)
|
return q.key.queue.MoveFocus(dir, &q.handlers)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -225,6 +225,17 @@ const (
|
|||||||
NameBack = "Back"
|
NameBack = "Back"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type FocusDirection int
|
||||||
|
|
||||||
|
const (
|
||||||
|
FocusRight FocusDirection = iota
|
||||||
|
FocusLeft
|
||||||
|
FocusUp
|
||||||
|
FocusDown
|
||||||
|
FocusForward
|
||||||
|
FocusBackward
|
||||||
|
)
|
||||||
|
|
||||||
// Contain reports whether m contains all modifiers
|
// Contain reports whether m contains all modifiers
|
||||||
// in m2.
|
// in m2.
|
||||||
func (m Modifiers) Contain(m2 Modifiers) bool {
|
func (m Modifiers) Contain(m2 Modifiers) bool {
|
||||||
|
|||||||
Reference in New Issue
Block a user