Skip to content

Commit

Permalink
rename events.Mgr to events.Source
Browse files Browse the repository at this point in the history
  • Loading branch information
kkoreilly committed Apr 17, 2024
1 parent 3ded301 commit b2492ea
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 73 deletions.
2 changes: 1 addition & 1 deletion core/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (wb *WidgetBase) EventMgr() *EventMgr {

// SystemEventMgr returns the lower-level system event
// manager for this [Widget]'s [Scene].
func (wb *WidgetBase) SystemEventMgr() *events.Mgr {
func (wb *WidgetBase) SystemEventMgr() *events.Source {
return wb.Scene.RenderWindow().SystemWindow.EventMgr()
}

Expand Down
2 changes: 1 addition & 1 deletion core/mainstage.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ func (st *Stage) MainHandleEvent(e events.Event) {
return
}
st.Popups.PopupHandleEvent(e)
if e.IsHandled() || st.Popups.TopIsModal() {
if e.IsHandled() || (st.Popups != nil && st.Popups.TopIsModal()) {
if DebugSettings.EventTrace && e.Type() != events.MouseMove {
fmt.Println("Event handled by popup:", e)
}
Expand Down
126 changes: 62 additions & 64 deletions events/mgr.go → events/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,32 @@ import (
"cogentcore.org/core/mimedata"
)

// TraceWindowPaint prints out a . for each WindowPaint event
// - for other window events, * for mouse move events.
// TraceWindowPaint prints out a . for each WindowPaint event,
// - for other window events, and * for mouse move events.
// Makes it easier to see what is going on in the overall flow.
var TraceWindowPaint = false

// Mgr manages the event construction and sending process,
// for its parent window. Caches state as needed
// to generate derived events such as MouseDrag.
type Mgr struct {
// Source is a source of events that manages the event
// construction and sending process for its parent window.
// It caches state as needed to generate derived events such
// as [MouseDrag].
type Source struct {
// Deque is the event queue
Deque Deque

// flag for ignoring mouse events when disabling mouse movement
ResettingPos bool

// Last has the prior state for key variables
Last MgrState
Last SourceState

// PaintCount is used for printing paint events as .
PaintCount int
}

// MgrState tracks basic event state over time
// SourceState tracks basic event state over time
// to enable recognition and full data for generating events.
type MgrState struct {
type SourceState struct {
// last mouse button event type (down or up)
MouseButtonType Types

Expand All @@ -64,110 +65,107 @@ type MgrState struct {
Key key.Codes
}

///////////////////////////////////////////////////////////////
// New Events

// SendKey processes a basic key event and sends it
func (em *Mgr) Key(typ Types, rn rune, code key.Codes, mods key.Modifiers) {
func (es *Source) Key(typ Types, rn rune, code key.Codes, mods key.Modifiers) {
ev := NewKey(typ, rn, code, mods)
em.Last.Mods = mods
em.Last.Key = code
es.Last.Mods = mods
es.Last.Key = code
ev.Init()
em.Deque.Send(ev)
es.Deque.Send(ev)

_, mapped := key.CodeRuneMap[code]

if typ == KeyDown && ev.Code < key.CodeLeftControl &&
(ev.HasAnyModifier(key.Control, key.Meta) || !mapped || ev.Code == key.CodeTab) {
che := NewKey(KeyChord, rn, code, mods)
che.Init()
em.Deque.Send(che)
es.Deque.Send(che)
}
}

// KeyChord processes a basic KeyChord event and sends it
func (em *Mgr) KeyChord(rn rune, code key.Codes, mods key.Modifiers) {
func (es *Source) KeyChord(rn rune, code key.Codes, mods key.Modifiers) {
ev := NewKey(KeyChord, rn, code, mods)
// no further processing of these
ev.Init()
em.Deque.Send(ev)
es.Deque.Send(ev)
}

// MouseButton creates and sends a mouse button event with given values
func (em *Mgr) MouseButton(typ Types, but Buttons, where image.Point, mods key.Modifiers) {
func (es *Source) MouseButton(typ Types, but Buttons, where image.Point, mods key.Modifiers) {
ev := NewMouse(typ, but, where, mods)
em.Last.Mods = mods
em.Last.MouseButtonType = typ
em.Last.MouseButton = but
em.Last.MousePos = where
es.Last.Mods = mods
es.Last.MouseButtonType = typ
es.Last.MouseButton = but
es.Last.MousePos = where
ev.Init()
if typ == MouseDown {
em.Last.MouseDownPos = where
em.Last.MouseDownTime = ev.GenTime
em.Last.MouseMoveTime = ev.GenTime
es.Last.MouseDownPos = where
es.Last.MouseDownTime = ev.GenTime
es.Last.MouseMoveTime = ev.GenTime
}
em.Deque.Send(ev)
es.Deque.Send(ev)
}

// MouseMove creates and sends a mouse move or drag event with given values
func (em *Mgr) MouseMove(where image.Point) {
lastPos := em.Last.MousePos
func (es *Source) MouseMove(where image.Point) {
lastPos := es.Last.MousePos
var ev *Mouse
if em.Last.MouseButtonType == MouseDown {
ev = NewMouseDrag(em.Last.MouseButton, where, lastPos, em.Last.MouseDownPos, em.Last.Mods)
ev.StTime = em.Last.MouseDownTime
ev.PrvTime = em.Last.MouseMoveTime
if es.Last.MouseButtonType == MouseDown {
ev = NewMouseDrag(es.Last.MouseButton, where, lastPos, es.Last.MouseDownPos, es.Last.Mods)
ev.StTime = es.Last.MouseDownTime
ev.PrvTime = es.Last.MouseMoveTime
} else {
ev = NewMouseMove(em.Last.MouseButton, where, lastPos, em.Last.Mods)
ev.PrvTime = em.Last.MouseMoveTime
ev = NewMouseMove(es.Last.MouseButton, where, lastPos, es.Last.Mods)
ev.PrvTime = es.Last.MouseMoveTime
}
ev.Init()
em.Last.MouseMoveTime = ev.GenTime
es.Last.MouseMoveTime = ev.GenTime
// if em.Win.IsCursorEnabled() {
em.Last.MousePos = where
es.Last.MousePos = where
// }
if TraceWindowPaint {
fmt.Printf("*")
}
em.Deque.Send(ev)
es.Deque.Send(ev)
}

// Scroll creates and sends a scroll event with given values
func (em *Mgr) Scroll(where image.Point, delta math32.Vector2) {
ev := NewScroll(where, delta, em.Last.Mods)
func (es *Source) Scroll(where image.Point, delta math32.Vector2) {
ev := NewScroll(where, delta, es.Last.Mods)
ev.Init()
em.Deque.Send(ev)
es.Deque.Send(ev)
}

// DropExternal creates and sends a Drop event with given values
func (em *Mgr) DropExternal(where image.Point, md mimedata.Mimes) {
ev := NewExternalDrop(Drop, em.Last.MouseButton, where, em.Last.Mods, md)
em.Last.MousePos = where
func (es *Source) DropExternal(where image.Point, md mimedata.Mimes) {
ev := NewExternalDrop(Drop, es.Last.MouseButton, where, es.Last.Mods, md)
es.Last.MousePos = where
ev.Init()
em.Deque.Send(ev)
es.Deque.Send(ev)
}

// Touch creates and sends a touch event with the given values.
// It also creates and sends a corresponding mouse event.
func (em *Mgr) Touch(typ Types, seq Sequence, where image.Point) {
func (es *Source) Touch(typ Types, seq Sequence, where image.Point) {
ev := NewTouch(typ, seq, where)
ev.Init()
em.Deque.Send(ev)
es.Deque.Send(ev)

if typ == TouchStart {
em.MouseButton(MouseDown, Left, where, 0) // TODO: modifiers
es.MouseButton(MouseDown, Left, where, 0) // TODO: modifiers
} else if typ == TouchEnd {
em.MouseButton(MouseUp, Left, where, 0) // TODO: modifiers
es.MouseButton(MouseUp, Left, where, 0) // TODO: modifiers
} else {
em.MouseMove(where)
es.MouseMove(where)
}
}

// Magnify creates and sends a [TouchMagnify] event with the given values.
func (em *Mgr) Magnify(scaleFactor float32, where image.Point) {
func (es *Source) Magnify(scaleFactor float32, where image.Point) {
ev := NewMagnify(scaleFactor, where)
ev.Init()
em.Deque.Send(ev)
es.Deque.Send(ev)
}

// func (em *Mgr) DND(act dnd.Actions, where image.Point, data mimedata.Mimes) {
Expand All @@ -177,42 +175,42 @@ func (em *Mgr) Magnify(scaleFactor float32, where image.Point) {
// em.Deque.Send(ev)
// }

func (em *Mgr) Window(act WinActions) {
func (es *Source) Window(act WinActions) {
ev := NewWindow(act)
ev.Init()
if TraceWindowPaint {
fmt.Printf("-")
}
em.Deque.SendFirst(ev)
es.Deque.SendFirst(ev)
}

func (em *Mgr) WindowPaint() {
func (es *Source) WindowPaint() {
ev := NewWindowPaint()
ev.Init()
if TraceWindowPaint {
fmt.Printf(".")
em.PaintCount++
if em.PaintCount > 60 {
es.PaintCount++
if es.PaintCount > 60 {
fmt.Println("")
em.PaintCount = 0
es.PaintCount = 0
}
}
em.Deque.SendFirst(ev) // separate channel for window!
es.Deque.SendFirst(ev) // separate channel for window!
}

func (em *Mgr) WindowResize() {
func (es *Source) WindowResize() {
ev := NewWindowResize()
ev.Init()
if TraceWindowPaint {
fmt.Printf("r")
}
em.Deque.SendFirst(ev)
es.Deque.SendFirst(ev)
}

func (em *Mgr) Custom(data any) {
func (es *Source) Custom(data any) {
ce := &CustomEvent{}
ce.Typ = Custom
ce.Data = data
ce.Init()
em.Deque.Send(ce)
es.Deque.Send(ce)
}
6 changes: 3 additions & 3 deletions system/driver/base/app_single.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type AppSingle[D system.Drawer, W system.Window] struct {
App

// EvMgr is the event manager for the app
EvMgr events.Mgr `label:"Event manger"`
EvMgr events.Source `label:"Event manger"`

// Draw is the single [system.Drawer] used for the app.
Draw D
Expand All @@ -48,7 +48,7 @@ type AppSingler interface {
system.App

// EventMgr returns the single [events.Mgr] associated with this app.
EventMgr() *events.Mgr
EventMgr() *events.Source

// Drawer returns the single [system.Drawer] associated with this app.
Drawer() system.Drawer
Expand All @@ -66,7 +66,7 @@ func NewAppSingle[D system.Drawer, W system.Window]() AppSingle[D, W] {
}
}

func (a *AppSingle[D, W]) EventMgr() *events.Mgr {
func (a *AppSingle[D, W]) EventMgr() *events.Source {
return &a.EvMgr
}

Expand Down
4 changes: 2 additions & 2 deletions system/driver/base/window_multi.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type WindowMulti[A system.App, D system.Drawer] struct {
Window[A]

// EvMgr is the event manager for the window
EvMgr events.Mgr `label:"Event manger"`
EvMgr events.Source `label:"Event manger"`

// Draw is the [system.Drawer] used for this window.
Draw D `label:"Drawer"`
Expand Down Expand Up @@ -63,7 +63,7 @@ func NewWindowMulti[A system.App, D system.Drawer](a A, opts *system.NewWindowOp
}
}

func (w *WindowMulti[A, D]) EventMgr() *events.Mgr {
func (w *WindowMulti[A, D]) EventMgr() *events.Source {
return &w.EvMgr
}

Expand Down
2 changes: 1 addition & 1 deletion system/driver/base/window_single.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func NewWindowSingle[A AppSingler](a A, opts *system.NewWindowOptions) WindowSin
}
}

func (w *WindowSingle[A]) EventMgr() *events.Mgr {
func (w *WindowSingle[A]) EventMgr() *events.Source {
return w.App.EventMgr()
}

Expand Down
2 changes: 1 addition & 1 deletion system/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ type Window interface {

// EventMgr returns the events.Mgr for this window,
// which manages all of the Event sending.
EventMgr() *events.Mgr
EventMgr() *events.Source
}

////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit b2492ea

Please sign in to comment.