Skip to content

Commit d93423d

Browse files
committed
refactor: Use common base EventTime for events
This also fixes the missing time initialization for focus events.
1 parent 2c2799c commit d93423d

File tree

7 files changed

+31
-54
lines changed

7 files changed

+31
-54
lines changed

errors.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2015 The TCell Authors
1+
// Copyright 2025 The TCell Authors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use file except in compliance with the License.
@@ -16,7 +16,6 @@ package tcell
1616

1717
import (
1818
"errors"
19-
"time"
2019
)
2120

2221
var (
@@ -42,21 +41,18 @@ var (
4241
// An EventError is an event representing some sort of error, and carries
4342
// an error payload.
4443
type EventError struct {
45-
t time.Time
44+
EventTime
4645
err error
4746
}
4847

49-
// When returns the time when the event was created.
50-
func (ev *EventError) When() time.Time {
51-
return ev.t
52-
}
53-
5448
// Error implements the error.
5549
func (ev *EventError) Error() string {
5650
return ev.err.Error()
5751
}
5852

5953
// NewEventError creates an ErrorEvent with the given error payload.
6054
func NewEventError(err error) *EventError {
61-
return &EventError{t: time.Now(), err: err}
55+
ev := &EventError{err: err}
56+
ev.SetEventNow()
57+
return ev
6258
}

event_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ func TestChannelMouseEvents(t *testing.T) {
2424
s := mkTestScreen(t, "")
2525
defer s.Fini()
2626

27+
now := time.Now()
2728
s.EnableMouse()
2829
s.InjectMouse(4, 9, Button1, ModCtrl)
2930
em := new(EventMouse)
@@ -39,6 +40,10 @@ loop:
3940
em = evm
4041
break loop
4142
}
43+
if !now.Before(ev.When()) {
44+
t.Errorf("Events arrived out of order!")
45+
}
46+
now = ev.When()
4247
continue
4348
case <-time.After(time.Second):
4449
break loop

focus.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ package tcell
1717
// EventFocus is a focus event. It is sent when the terminal window (or tab)
1818
// gets or loses focus.
1919
type EventFocus struct {
20-
*EventTime
20+
EventTime
2121

2222
// True if the window received focus, false if it lost focus
2323
Focused bool
2424
}
2525

2626
func NewEventFocus(focused bool) *EventFocus {
27-
return &EventFocus{Focused: focused}
27+
ev := &EventFocus{Focused: focused}
28+
ev.SetEventNow()
29+
return ev
2830
}

interrupt.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,21 @@
1414

1515
package tcell
1616

17-
import (
18-
"time"
19-
)
20-
2117
// EventInterrupt is a generic wakeup event. Its can be used to
2218
// to request a redraw. It can carry an arbitrary payload, as well.
2319
type EventInterrupt struct {
24-
t time.Time
20+
EventTime
2521
v any
2622
}
2723

28-
// When returns the time when this event was created.
29-
func (ev *EventInterrupt) When() time.Time {
30-
return ev.t
31-
}
32-
3324
// Data is used to obtain the opaque event payload.
3425
func (ev *EventInterrupt) Data() any {
3526
return ev.v
3627
}
3728

3829
// NewEventInterrupt creates an EventInterrupt with the given payload.
3930
func NewEventInterrupt(data any) *EventInterrupt {
40-
return &EventInterrupt{t: time.Now(), v: data}
31+
ev := &EventInterrupt{v: data}
32+
ev.SetEventNow()
33+
return ev
4134
}

key.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package tcell
1717
import (
1818
"fmt"
1919
"strings"
20-
"time"
2120
)
2221

2322
// EventKey represents a key press. Usually this is a key press followed
@@ -45,18 +44,12 @@ import (
4544
// overly much on availability of modifiers, or the availability of any
4645
// specific keys.
4746
type EventKey struct {
48-
t time.Time
47+
EventTime
4948
mod ModMask
5049
key Key
5150
str string // string for key, usually just one character, but may be composed sequence
5251
}
5352

54-
// When returns the time when this Event was created, which should closely
55-
// match the time when the key was pressed.
56-
func (ev *EventKey) When() time.Time {
57-
return ev.t
58-
}
59-
6053
// Str returns the string corresponding to the key press, if it makes sense.
6154
// The result is only defined if the value of Key() is KeyRune. It will be
6255
// either one key (e.g. 'A'), or could be a composed sequence.
@@ -306,7 +299,9 @@ func NewEventKey(k Key, str string, mod ModMask) *EventKey {
306299
k = KeyBacktab
307300
mod &^= ModShift
308301
}
309-
return &EventKey{t: time.Now(), key: k, str: str, mod: mod}
302+
ev := &EventKey{key: k, str: str, mod: mod}
303+
ev.SetEventNow()
304+
return ev
310305
}
311306

312307
// ModMask is a mask of modifier keys. Note that it will not always be

mouse.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@
1414

1515
package tcell
1616

17-
import (
18-
"time"
19-
)
20-
2117
// EventMouse is a mouse event. It is sent on either mouse up or mouse down
2218
// events. It is also sent on mouse motion events - if the terminal supports
2319
// it. We make every effort to ensure that mouse release events are delivered.
@@ -35,18 +31,13 @@ import (
3531
// Applications can inspect the time between events to resolve double or
3632
// triple clicks.
3733
type EventMouse struct {
38-
t time.Time
34+
EventTime
3935
btn ButtonMask
4036
mod ModMask
4137
x int
4238
y int
4339
}
4440

45-
// When returns the time when this EventMouse was created.
46-
func (ev *EventMouse) When() time.Time {
47-
return ev.t
48-
}
49-
5041
// Buttons returns the list of buttons that were pressed or wheel motions.
5142
func (ev *EventMouse) Buttons() ButtonMask {
5243
return ev.btn
@@ -67,7 +58,9 @@ func (ev *EventMouse) Position() (int, int) {
6758
// NewEventMouse is used to create a new mouse event. Applications
6859
// shouldn't need to use this; its mostly for screen implementors.
6960
func NewEventMouse(x, y int, btn ButtonMask, mod ModMask) *EventMouse {
70-
return &EventMouse{t: time.Now(), x: x, y: y, btn: btn, mod: mod}
61+
ev := &EventMouse{x: x, y: y, btn: btn, mod: mod}
62+
ev.SetEventNow()
63+
return ev
7164
}
7265

7366
// ButtonMask is a mask of mouse buttons and wheel events. Mouse button presses

resize.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2015 The TCell Authors
1+
// Copyright 2025 The TCell Authors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use file except in compliance with the License.
@@ -14,13 +14,9 @@
1414

1515
package tcell
1616

17-
import (
18-
"time"
19-
)
20-
2117
// EventResize is sent when the window size changes.
2218
type EventResize struct {
23-
t time.Time
19+
EventTime
2420
ws WindowSize
2521
}
2622

@@ -31,12 +27,9 @@ func NewEventResize(width, height int) *EventResize {
3127
Width: width,
3228
Height: height,
3329
}
34-
return &EventResize{t: time.Now(), ws: ws}
35-
}
36-
37-
// When returns the time when the Event was created.
38-
func (ev *EventResize) When() time.Time {
39-
return ev.t
30+
ev := &EventResize{ws: ws}
31+
ev.SetEventNow()
32+
return ev
4033
}
4134

4235
// Size returns the new window size as width, height in character cells.

0 commit comments

Comments
 (0)