mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-07 02:15:34 +00:00
app,io/key,io/system: [API] replace system.CommandEvent with key.Event
It's much simpler to map the Android back button to a key.Event and let the usual key filtering determine whether to block its default behaviour. Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
+1
-3
@@ -571,9 +571,7 @@ func Java_org_gioui_GioView_onFrameCallback(env *C.JNIEnv, class C.jclass, view
|
|||||||
//export Java_org_gioui_GioView_onBack
|
//export Java_org_gioui_GioView_onBack
|
||||||
func Java_org_gioui_GioView_onBack(env *C.JNIEnv, class C.jclass, view C.jlong) C.jboolean {
|
func Java_org_gioui_GioView_onBack(env *C.JNIEnv, class C.jclass, view C.jlong) C.jboolean {
|
||||||
w := cgo.Handle(view).Value().(*window)
|
w := cgo.Handle(view).Value().(*window)
|
||||||
ev := &system.CommandEvent{Type: system.CommandBack}
|
if w.callbacks.Event(key.Event{Name: key.NameBack}) {
|
||||||
w.callbacks.Event(ev)
|
|
||||||
if ev.Cancel {
|
|
||||||
return C.JNI_TRUE
|
return C.JNI_TRUE
|
||||||
}
|
}
|
||||||
return C.JNI_FALSE
|
return C.JNI_FALSE
|
||||||
|
|||||||
+1
-4
@@ -172,12 +172,9 @@ func (w *window) addEventListeners() {
|
|||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
w.addEventListener(w.window, "popstate", func(this js.Value, args []js.Value) interface{} {
|
w.addEventListener(w.window, "popstate", func(this js.Value, args []js.Value) interface{} {
|
||||||
ev := &system.CommandEvent{Type: system.CommandBack}
|
if w.w.Event(key.Event{Name: key.NameBack}) {
|
||||||
w.w.Event(ev)
|
|
||||||
if ev.Cancel {
|
|
||||||
return w.browserHistory.Call("forward")
|
return w.browserHistory.Call("forward")
|
||||||
}
|
}
|
||||||
|
|
||||||
return w.browserHistory.Call("back")
|
return w.browserHistory.Call("back")
|
||||||
})
|
})
|
||||||
w.addEventListener(w.document, "visibilitychange", func(this js.Value, args []js.Value) interface{} {
|
w.addEventListener(w.document, "visibilitychange", func(this js.Value, args []js.Value) interface{} {
|
||||||
|
|||||||
+14
-9
@@ -416,29 +416,31 @@ func (c *callbacks) SetDriver(d driver) {
|
|||||||
c.w.wakeupFuncs <- wakeup
|
c.w.wakeupFuncs <- wakeup
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *callbacks) Event(e event.Event) {
|
func (c *callbacks) Event(e event.Event) bool {
|
||||||
if c.d == nil {
|
if c.d == nil {
|
||||||
panic("event while no driver active")
|
panic("event while no driver active")
|
||||||
}
|
}
|
||||||
c.waitEvents = append(c.waitEvents, e)
|
c.waitEvents = append(c.waitEvents, e)
|
||||||
if c.busy {
|
if c.busy {
|
||||||
return
|
return true
|
||||||
}
|
}
|
||||||
c.busy = true
|
c.busy = true
|
||||||
defer func() {
|
defer func() {
|
||||||
c.busy = false
|
c.busy = false
|
||||||
}()
|
}()
|
||||||
|
var handled bool
|
||||||
for len(c.waitEvents) > 0 {
|
for len(c.waitEvents) > 0 {
|
||||||
e := c.waitEvents[0]
|
e := c.waitEvents[0]
|
||||||
copy(c.waitEvents, c.waitEvents[1:])
|
copy(c.waitEvents, c.waitEvents[1:])
|
||||||
c.waitEvents = c.waitEvents[:len(c.waitEvents)-1]
|
c.waitEvents = c.waitEvents[:len(c.waitEvents)-1]
|
||||||
c.w.processEvent(c.d, e)
|
handled = c.w.processEvent(c.d, e)
|
||||||
}
|
}
|
||||||
c.w.updateState(c.d)
|
c.w.updateState(c.d)
|
||||||
if c.w.closing {
|
if c.w.closing {
|
||||||
c.w.closing = false
|
c.w.closing = false
|
||||||
c.d.Close()
|
c.d.Close()
|
||||||
}
|
}
|
||||||
|
return handled
|
||||||
}
|
}
|
||||||
|
|
||||||
// SemanticRoot returns the ID of the semantic root.
|
// SemanticRoot returns the ID of the semantic root.
|
||||||
@@ -740,10 +742,10 @@ func (w *Window) updateState(d driver) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *Window) processEvent(d driver, e event.Event) {
|
func (w *Window) processEvent(d driver, e event.Event) bool {
|
||||||
select {
|
select {
|
||||||
case <-w.dead:
|
case <-w.dead:
|
||||||
return
|
return false
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
switch e2 := e.(type) {
|
switch e2 := e.(type) {
|
||||||
@@ -816,9 +818,6 @@ func (w *Window) processEvent(d driver, e event.Event) {
|
|||||||
}
|
}
|
||||||
w.processFrame(d, frameStart)
|
w.processFrame(d, frameStart)
|
||||||
w.updateCursor(d)
|
w.updateCursor(d)
|
||||||
case *system.CommandEvent:
|
|
||||||
w.out <- e
|
|
||||||
w.waitAck(d)
|
|
||||||
case system.DestroyEvent:
|
case system.DestroyEvent:
|
||||||
w.destroyGPU()
|
w.destroyGPU()
|
||||||
w.out <- e2
|
w.out <- e2
|
||||||
@@ -838,10 +837,12 @@ func (w *Window) processEvent(d driver, e event.Event) {
|
|||||||
e2.Config.Size = e2.Config.Size.Sub(w.decorations.size)
|
e2.Config.Size = e2.Config.Size.Sub(w.decorations.size)
|
||||||
w.out <- e2
|
w.out <- e2
|
||||||
case event.Event:
|
case event.Event:
|
||||||
if w.queue.q.Queue(e2) {
|
handled := w.queue.q.Queue(e2)
|
||||||
|
if handled {
|
||||||
w.setNextFrame(time.Time{})
|
w.setNextFrame(time.Time{})
|
||||||
w.updateAnimation(d)
|
w.updateAnimation(d)
|
||||||
} else if e, ok := e.(key.Event); ok && e.State == key.Press {
|
} else if e, ok := e.(key.Event); ok && e.State == key.Press {
|
||||||
|
handled = true
|
||||||
switch {
|
switch {
|
||||||
case e.Name == key.NameTab && e.Modifiers == 0:
|
case e.Name == key.NameTab && e.Modifiers == 0:
|
||||||
w.moveFocus(router.FocusForward, d)
|
w.moveFocus(router.FocusForward, d)
|
||||||
@@ -855,10 +856,14 @@ func (w *Window) processEvent(d driver, e event.Event) {
|
|||||||
w.moveFocus(router.FocusLeft, d)
|
w.moveFocus(router.FocusLeft, d)
|
||||||
case e.Name == key.NameRight && e.Modifiers == 0:
|
case e.Name == key.NameRight && e.Modifiers == 0:
|
||||||
w.moveFocus(router.FocusRight, d)
|
w.moveFocus(router.FocusRight, d)
|
||||||
|
default:
|
||||||
|
handled = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
w.updateCursor(d)
|
w.updateCursor(d)
|
||||||
|
return handled
|
||||||
}
|
}
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *Window) run(options []Option) {
|
func (w *Window) run(options []Option) {
|
||||||
|
|||||||
@@ -223,6 +223,7 @@ const (
|
|||||||
NameDown = "Down"
|
NameDown = "Down"
|
||||||
NameLeft = "Left"
|
NameLeft = "Left"
|
||||||
NameRight = "Right"
|
NameRight = "Right"
|
||||||
|
NameBack = "Back"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Contain reports whether m contains all modifiers
|
// Contain reports whether m contains all modifiers
|
||||||
|
|||||||
+3
-21
@@ -54,20 +54,9 @@ type StageEvent struct {
|
|||||||
Stage Stage
|
Stage Stage
|
||||||
}
|
}
|
||||||
|
|
||||||
// CommandEvent is a system event. Unlike most other events, CommandEvent is
|
|
||||||
// delivered as a pointer to allow Cancel to suppress it.
|
|
||||||
type CommandEvent struct {
|
|
||||||
Type CommandType
|
|
||||||
// Cancel suppress the default action of the command.
|
|
||||||
Cancel bool
|
|
||||||
}
|
|
||||||
|
|
||||||
// Stage of a Window.
|
// Stage of a Window.
|
||||||
type Stage uint8
|
type Stage uint8
|
||||||
|
|
||||||
// CommandType is the type of a CommandEvent.
|
|
||||||
type CommandType uint8
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// StagePaused is the Stage for inactive Windows.
|
// StagePaused is the Stage for inactive Windows.
|
||||||
// Inactive Windows don't receive FrameEvents.
|
// Inactive Windows don't receive FrameEvents.
|
||||||
@@ -76,12 +65,6 @@ const (
|
|||||||
StageRunning
|
StageRunning
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
// CommandBack is the command for a back action
|
|
||||||
// such as the Android back button.
|
|
||||||
CommandBack CommandType = iota
|
|
||||||
)
|
|
||||||
|
|
||||||
func (l Stage) String() string {
|
func (l Stage) String() string {
|
||||||
switch l {
|
switch l {
|
||||||
case StagePaused:
|
case StagePaused:
|
||||||
@@ -93,7 +76,6 @@ func (l Stage) String() string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (FrameEvent) ImplementsEvent() {}
|
func (FrameEvent) ImplementsEvent() {}
|
||||||
func (StageEvent) ImplementsEvent() {}
|
func (StageEvent) ImplementsEvent() {}
|
||||||
func (*CommandEvent) ImplementsEvent() {}
|
func (DestroyEvent) ImplementsEvent() {}
|
||||||
func (DestroyEvent) ImplementsEvent() {}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user