io/input: [API] execute commands immediately

Change the semantics of commands to execute immediately. In cases where
execution of a command introduces a inconsistency, freeze event routing
and defer the command as well as queued events to the next frame.

Rename Source.Queue to Source.Execute to better fit the new command
semantics.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2023-11-20 16:25:05 -06:00
parent 67b58a6006
commit fc208248b7
11 changed files with 190 additions and 142 deletions
+2 -2
View File
@@ -80,7 +80,7 @@ func (b *Clickable) Pressed() bool {
// Focus requests the input focus for the element.
func (b *Clickable) Focus(gtx layout.Context) {
gtx.Queue(key.FocusCmd{Tag: &b.keyTag})
gtx.Execute(key.FocusCmd{Tag: &b.keyTag})
}
// Focused reports whether b has focus.
@@ -149,7 +149,7 @@ func (b *Clickable) Update(gtx layout.Context) []Click {
}
case gesture.KindPress:
if e.Source == pointer.Mouse {
gtx.Queue(key.FocusCmd{Tag: &b.keyTag})
gtx.Execute(key.FocusCmd{Tag: &b.keyTag})
}
b.history = append(b.history, Press{
Position: e.Position,
+1 -1
View File
@@ -76,7 +76,7 @@ func (d *Draggable) Update(gtx layout.Context) (mime string, requested bool) {
// Offer the data ready for a drop. Must be called after being Requested.
// The mime must be one in the requested list.
func (d *Draggable) Offer(gtx layout.Context, mime string, data io.ReadCloser) {
gtx.Queue(transfer.OfferCmd{Tag: &d.handle, Type: mime, Data: data})
gtx.Execute(transfer.OfferCmd{Tag: &d.handle, Type: mime, Data: data})
}
// Pos returns the drag position relative to its initial click position.
+2 -3
View File
@@ -51,17 +51,16 @@ func TestDraggable(t *testing.T) {
},
)
ofr := &offer{data: "hello"}
drag.Offer(gtx, "file", ofr)
drag.Update(gtx)
r.Events(drag, transfer.TargetFilter{Type: drag.Type})
drag.Offer(gtx, "file", ofr)
r.Frame(gtx.Ops)
evs := r.Events(drag, transfer.TargetFilter{Type: drag.Type})
if len(evs) != 2 {
t.Fatalf("expected 2 event, got %d", len(evs))
t.Fatalf("expected 3 event, got %d", len(evs))
}
ev := evs[0].(transfer.DataEvent)
ev.Open = nil
if got, want := ev.Type, "file"; got != want {
t.Errorf("expected %v; got %v", got, want)
}
+6 -6
View File
@@ -475,13 +475,13 @@ func (e *Editor) command(gtx layout.Context, k key.Event) {
// half is in Editor.processKey() under clipboard.Event.
case "V":
if !e.ReadOnly {
gtx.Queue(clipboard.ReadCmd{Tag: &e.eventKey})
gtx.Execute(clipboard.ReadCmd{Tag: &e.eventKey})
}
// Copy or Cut selection -- ignored if nothing selected.
case "C", "X":
e.scratch = e.text.SelectedText(e.scratch)
if text := string(e.scratch); text != "" {
gtx.Queue(clipboard.WriteCmd{Type: "application/text", Data: io.NopCloser(strings.NewReader(text))})
gtx.Execute(clipboard.WriteCmd{Type: "application/text", Data: io.NopCloser(strings.NewReader(text))})
if k.Name == "X" && !e.ReadOnly {
e.Delete(1)
}
@@ -556,8 +556,8 @@ func (e *Editor) command(gtx layout.Context, k key.Event) {
// Focus requests the input focus for the Editor.
func (e *Editor) Focus(gtx layout.Context) {
gtx.Queue(key.FocusCmd{Tag: &e.eventKey})
gtx.Queue(key.SoftKeyboardCmd{Show: true})
gtx.Execute(key.FocusCmd{Tag: &e.eventKey})
gtx.Execute(key.SoftKeyboardCmd{Show: true})
}
// Focused returns whether the editor is focused or not.
@@ -601,7 +601,7 @@ func (e *Editor) Update(gtx layout.Context) {
}
if newSel != e.ime.selection {
e.ime.selection = newSel
gtx.Queue(key.SelectionCmd{Tag: &e.eventKey, Range: newSel.rng, Caret: newSel.caret})
gtx.Execute(key.SelectionCmd{Tag: &e.eventKey, Range: newSel.rng, Caret: newSel.caret})
}
e.updateSnippet(gtx, e.ime.start, e.ime.end)
@@ -658,7 +658,7 @@ func (e *Editor) updateSnippet(gtx layout.Context, start, end int) {
return
}
e.ime.snippet = newSnip
gtx.Queue(key.SnippetCmd{Tag: &e.eventKey, Snippet: newSnip})
gtx.Execute(key.SnippetCmd{Tag: &e.eventKey, Snippet: newSnip})
}
func (e *Editor) layout(gtx layout.Context, textMaterial, selectMaterial op.CallOp) layout.Dimensions {
+1 -1
View File
@@ -51,7 +51,7 @@ func (e *Enum) Update(gtx layout.Context) bool {
switch ev.Kind {
case gesture.KindPress:
if ev.Source == pointer.Mouse {
gtx.Queue(key.FocusCmd{Tag: &state.tag})
gtx.Execute(key.FocusCmd{Tag: &state.tag})
}
case gesture.KindClick:
if state.key != e.Value {
+3 -3
View File
@@ -100,8 +100,8 @@ func (l *Selectable) initialize() {
// Focus requests the input focus for the label.
func (l *Selectable) Focus(gtx layout.Context) {
gtx.Queue(key.FocusCmd{Tag: l})
gtx.Queue(key.SoftKeyboardCmd{Show: true})
gtx.Execute(key.FocusCmd{Tag: l})
gtx.Execute(key.SoftKeyboardCmd{Show: true})
}
// Focused returns whether the label is focused or not.
@@ -348,7 +348,7 @@ func (e *Selectable) command(gtx layout.Context, k key.Event) {
case "C", "X":
e.scratch = e.text.SelectedText(e.scratch)
if text := string(e.scratch); text != "" {
gtx.Queue(clipboard.WriteCmd{Type: "application/text", Data: io.NopCloser(strings.NewReader(text))})
gtx.Execute(clipboard.WriteCmd{Type: "application/text", Data: io.NopCloser(strings.NewReader(text))})
}
// Select all
case "A":