mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-06 09:55:40 +00:00
io/input: merge pointer and key filters
Refactor the pointer and key filter unions into the handler state struct. This is a preparation for replacing calls to filtersMatches with queries to the filter union. Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
+27
-22
@@ -46,12 +46,7 @@ type keyHandler struct {
|
||||
hint key.InputHint
|
||||
orderPlusOne int
|
||||
dirOrder int
|
||||
// filter are the key filters accumulated in the previous frame,
|
||||
// used for routing events in the current frame.
|
||||
filter keyFilter
|
||||
// nextFilter is the filter accumulator for the current frame.
|
||||
nextFilter keyFilter
|
||||
trans f32.Affine2D
|
||||
trans f32.Affine2D
|
||||
}
|
||||
|
||||
type keyFilter struct {
|
||||
@@ -97,8 +92,6 @@ func (q *keyQueue) InputHint(handlers map[event.Tag]*handler, state keyState) (k
|
||||
}
|
||||
|
||||
func (k *keyHandler) Reset() {
|
||||
k.filter, k.nextFilter = k.nextFilter, k.filter
|
||||
k.nextFilter = keyFilter{}
|
||||
k.visible = false
|
||||
k.orderPlusOne = 0
|
||||
k.hint = key.HintAny
|
||||
@@ -119,7 +112,7 @@ func (k *keyHandler) ResetEvent() (event.Event, bool) {
|
||||
|
||||
func (q *keyQueue) Frame(handlers map[event.Tag]*handler, state keyState) keyState {
|
||||
if state.focus != nil {
|
||||
if h, ok := handlers[state.focus]; !ok || !h.key.isFocusable() {
|
||||
if h, ok := handlers[state.focus]; !ok || !h.filter.key.focusable || !h.key.visible {
|
||||
// Remove focus from the handler that is no longer focusable.
|
||||
state.focus = nil
|
||||
state.state = TextInputClose
|
||||
@@ -260,11 +253,16 @@ func (q *keyQueue) AreaFor(k *keyHandler) int {
|
||||
return q.dirOrder[order].area
|
||||
}
|
||||
|
||||
func (k *keyHandler) Accepts(e key.Event) bool {
|
||||
for _, f := range k.filter.filters {
|
||||
if keyFilterMatch(f, e) {
|
||||
return true
|
||||
func (k *keyFilter) Matches(e event.Event) bool {
|
||||
switch e := e.(type) {
|
||||
case key.Event:
|
||||
for _, f := range k.filters {
|
||||
if keyFilterMatch(f, e) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
case key.FocusEvent, key.SnippetEvent, key.EditEvent, key.SelectionEvent:
|
||||
return k.focusable
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -284,7 +282,7 @@ func keyFilterMatch(f key.Filter, e key.Event) bool {
|
||||
|
||||
func (q *keyQueue) Focus(handlers map[event.Tag]*handler, state keyState, focus event.Tag) (keyState, []taggedEvent) {
|
||||
if focus != nil {
|
||||
if h, exists := handlers[focus]; !exists || !h.key.isFocusable() {
|
||||
if h, exists := handlers[focus]; !exists || !h.filter.key.focusable || !h.key.visible {
|
||||
focus = nil
|
||||
}
|
||||
}
|
||||
@@ -315,16 +313,23 @@ func (s keyState) softKeyboard(show bool) keyState {
|
||||
return s
|
||||
}
|
||||
|
||||
func (k *keyHandler) Filter(f key.Filter) {
|
||||
k.nextFilter.filters = append(k.nextFilter.filters, f)
|
||||
func (k *keyFilter) Add(f event.Filter) {
|
||||
switch f := f.(type) {
|
||||
case key.FocusFilter:
|
||||
k.focusable = true
|
||||
case key.Filter:
|
||||
for _, f2 := range k.filters {
|
||||
if f == f2 {
|
||||
return
|
||||
}
|
||||
}
|
||||
k.filters = append(k.filters, f)
|
||||
}
|
||||
}
|
||||
|
||||
func (k *keyHandler) isFocusable() bool {
|
||||
return k.filter.focusable && k.visible
|
||||
}
|
||||
|
||||
func (k *keyHandler) Focusable() {
|
||||
k.nextFilter.focusable = true
|
||||
func (k *keyFilter) Merge(k2 keyFilter) {
|
||||
k.focusable = k.focusable || k2.focusable
|
||||
k.filters = append(k.filters, k2.filters...)
|
||||
}
|
||||
|
||||
func (q *keyQueue) inputOp(tag event.Tag, state *keyHandler, t f32.Affine2D, area int, bounds image.Rectangle) {
|
||||
|
||||
Reference in New Issue
Block a user