widget: [API] re-implement Selectable.Focus in terms of commands

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2023-10-20 20:58:36 -05:00
parent 8334d2abb4
commit 9de80749e1
2 changed files with 13 additions and 18 deletions
+12 -17
View File
@@ -70,15 +70,14 @@ type Selectable struct {
source stringSource source stringSource
// scratch is a buffer reused to efficiently read text out of the // scratch is a buffer reused to efficiently read text out of the
// textView. // textView.
scratch []byte scratch []byte
lastValue string lastValue string
text textView text textView
focused bool focused bool
requestFocus bool dragging bool
dragging bool dragger gesture.Drag
dragger gesture.Drag scroller gesture.Scroll
scroller gesture.Scroll scrollOff image.Point
scrollOff image.Point
clicker gesture.Click clicker gesture.Click
// events is the list of events not yet processed. // events is the list of events not yet processed.
@@ -99,8 +98,9 @@ func (l *Selectable) initialize() {
} }
// Focus requests the input focus for the label. // Focus requests the input focus for the label.
func (l *Selectable) Focus() { func (l *Selectable) Focus(gtx layout.Context) {
l.requestFocus = true gtx.Queue(key.FocusCmd{Tag: l})
gtx.Queue(key.SoftKeyboardCmd{Show: true})
} }
// Focused returns whether the label is focused or not. // Focused returns whether the label is focused or not.
@@ -209,11 +209,6 @@ func (l *Selectable) Layout(gtx layout.Context, lt *text.Shaper, font font.Font,
keys = keyFilter keys = keyFilter
} }
key.InputOp{Tag: l, Keys: keys}.Add(gtx.Ops) key.InputOp{Tag: l, Keys: keys}.Add(gtx.Ops)
if l.requestFocus {
gtx.Queue(key.FocusCmd{Tag: l})
gtx.Queue(key.SoftKeyboardCmd{Show: true})
}
l.requestFocus = false
l.clicker.Add(gtx.Ops) l.clicker.Add(gtx.Ops)
l.dragger.Add(gtx.Ops) l.dragger.Add(gtx.Ops)
@@ -249,7 +244,7 @@ func (e *Selectable) processPointer(gtx layout.Context) {
X: int(math.Round(float64(evt.Position.X))), X: int(math.Round(float64(evt.Position.X))),
Y: int(math.Round(float64(evt.Position.Y))), Y: int(math.Round(float64(evt.Position.Y))),
}) })
e.requestFocus = true e.Focus(gtx)
if evt.Modifiers == key.ModShift { if evt.Modifiers == key.ModShift {
start, end := e.text.Selection() start, end := e.text.Selection()
// If they clicked closer to the end, then change the end to // If they clicked closer to the end, then change the end to
+1 -1
View File
@@ -49,7 +49,7 @@ func TestSelectableMove(t *testing.T) {
// Layout once to populate e.lines and get focus. // Layout once to populate e.lines and get focus.
s := new(Selectable) s := new(Selectable)
s.Focus() s.Focus(gtx)
s.SetText(str) s.SetText(str)
// Set up selection so the Selectable filters for all 4 directional keys. // Set up selection so the Selectable filters for all 4 directional keys.
s.Layout(gtx, cache, font.Font{}, fontSize, op.CallOp{}, op.CallOp{}) s.Layout(gtx, cache, font.Font{}, fontSize, op.CallOp{}, op.CallOp{})