1
0
mirror of https://github.com/mum4k/termdash.git synced 2025-04-25 13:48:50 +08:00

Fakewidget now indicates focus state when an event is received.

This commit is contained in:
Jakub Sobon 2020-11-27 22:56:37 -05:00
parent 43c0f26c5e
commit 6a4dc1dc3f
No known key found for this signature in database
GPG Key ID: F2451A77FB05D3B7
2 changed files with 126 additions and 19 deletions

View File

@ -53,11 +53,15 @@ type Event struct {
// Mirror is a fake widget. The fake widget draws a border around its assigned
// canvas and writes the size of its assigned canvas on the first line of the
// canvas. It writes the last received keyboard event onto the second line. It
// writes the last received mouse event onto the third line. If a non-empty
// string is provided via the Text() method, that text will be written right
// after the canvas size on the first line. If the widget's container is
// focused it writes "focus" onto the fourth line.
// canvas.
//
// It writes the last received keyboard event onto the second line. It
// writes the last received mouse event onto the third line. If the widget was
// focused at the time of the event, the event will be prepended with a "F:".
//
// If a non-empty string is provided via the Text() method, that text will be
// written right after the canvas size on the first line. If the widget's
// container is focused it writes "focus" onto the fourth line.
//
// The widget requests the same options that are provided to the constructor.
// If the options or canvas size don't allow for the lines mentioned above, the
@ -142,7 +146,11 @@ func (mi *Mirror) Keyboard(k *terminalapi.Keyboard, meta *widgetapi.EventMeta) e
mi.lines[keyboardLine] = ""
return fmt.Errorf("fakewidget received keyboard event: %v", k)
}
mi.lines[keyboardLine] = k.Key.String()
if meta.Focused {
mi.lines[keyboardLine] = fmt.Sprintf("F:%s", k.Key.String())
} else {
mi.lines[keyboardLine] = k.Key.String()
}
return nil
}
@ -159,7 +167,11 @@ func (mi *Mirror) Mouse(m *terminalapi.Mouse, meta *widgetapi.EventMeta) error {
mi.lines[mouseLine] = ""
return fmt.Errorf("fakewidget received mouse event: %v", m)
}
mi.lines[mouseLine] = fmt.Sprintf("%v%v", m.Position, m.Button)
if meta.Focused {
mi.lines[mouseLine] = fmt.Sprintf("F:%v%v", m.Position, m.Button)
} else {
mi.lines[mouseLine] = fmt.Sprintf("%v%v", m.Position, m.Button)
}
return nil
}

View File

@ -32,12 +32,14 @@ import (
// keyEvents are keyboard events to send to the widget.
type keyEvents struct {
k *terminalapi.Keyboard
meta *widgetapi.EventMeta
wantErr bool
}
// mouseEvents are mouse events to send to the widget.
type mouseEvents struct {
m *terminalapi.Mouse
meta *widgetapi.EventMeta
wantErr bool
}
@ -131,10 +133,12 @@ func TestMirror(t *testing.T) {
desc: "draws the last keyboard event",
keyEvents: []keyEvents{
{
k: &terminalapi.Keyboard{Key: keyboard.KeyEnter},
k: &terminalapi.Keyboard{Key: keyboard.KeyEnter},
meta: &widgetapi.EventMeta{},
},
{
k: &terminalapi.Keyboard{Key: keyboard.KeyEnd},
k: &terminalapi.Keyboard{Key: keyboard.KeyEnd},
meta: &widgetapi.EventMeta{},
},
},
cvs: testcanvas.MustNew(image.Rect(0, 0, 8, 4)),
@ -149,11 +153,36 @@ func TestMirror(t *testing.T) {
return ft
},
},
{
desc: "draws the last keyboard event when focused",
keyEvents: []keyEvents{
{
k: &terminalapi.Keyboard{Key: keyboard.KeyEnter},
meta: &widgetapi.EventMeta{Focused: true},
},
{
k: &terminalapi.Keyboard{Key: keyboard.KeyEnd},
meta: &widgetapi.EventMeta{Focused: true},
},
},
cvs: testcanvas.MustNew(image.Rect(0, 0, 10, 4)),
meta: &widgetapi.Meta{},
want: func(size image.Point) *faketerm.Terminal {
ft := faketerm.MustNew(size)
cvs := testcanvas.MustNew(ft.Area())
testdraw.MustBorder(cvs, cvs.Area())
testdraw.MustText(cvs, "(10,4)", image.Point{1, 1})
testdraw.MustText(cvs, "F:KeyEnd", image.Point{1, 2})
testcanvas.MustApply(cvs, ft)
return ft
},
},
{
desc: "skips the keyboard event if there isn't a line for it",
keyEvents: []keyEvents{
{
k: &terminalapi.Keyboard{Key: keyboard.KeyEnd},
k: &terminalapi.Keyboard{Key: keyboard.KeyEnd},
meta: &widgetapi.EventMeta{},
},
},
cvs: testcanvas.MustNew(image.Rect(0, 0, 8, 3)),
@ -171,12 +200,15 @@ func TestMirror(t *testing.T) {
desc: "draws the last mouse event",
mouseEvents: []mouseEvents{
{
m: &terminalapi.Mouse{Button: mouse.ButtonLeft},
m: &terminalapi.Mouse{Button: mouse.ButtonLeft},
meta: &widgetapi.EventMeta{},
},
{
m: &terminalapi.Mouse{
Position: image.Point{1, 2},
Button: mouse.ButtonMiddle},
Button: mouse.ButtonMiddle,
},
meta: &widgetapi.EventMeta{},
},
},
cvs: testcanvas.MustNew(image.Rect(0, 0, 19, 5)),
@ -191,11 +223,39 @@ func TestMirror(t *testing.T) {
return ft
},
},
{
desc: "draws the last mouse event when focused",
mouseEvents: []mouseEvents{
{
m: &terminalapi.Mouse{Button: mouse.ButtonLeft},
meta: &widgetapi.EventMeta{},
},
{
m: &terminalapi.Mouse{
Position: image.Point{1, 2},
Button: mouse.ButtonMiddle,
},
meta: &widgetapi.EventMeta{Focused: true},
},
},
cvs: testcanvas.MustNew(image.Rect(0, 0, 21, 5)),
meta: &widgetapi.Meta{},
want: func(size image.Point) *faketerm.Terminal {
ft := faketerm.MustNew(size)
cvs := testcanvas.MustNew(ft.Area())
testdraw.MustBorder(cvs, cvs.Area())
testdraw.MustText(cvs, "(21,5)", image.Point{1, 1})
testdraw.MustText(cvs, "F:(1,2)ButtonMiddle", image.Point{1, 3})
testcanvas.MustApply(cvs, ft)
return ft
},
},
{
desc: "skips the mouse event if there isn't a line for it",
mouseEvents: []mouseEvents{
{
m: &terminalapi.Mouse{Button: mouse.ButtonLeft},
m: &terminalapi.Mouse{Button: mouse.ButtonLeft},
meta: &widgetapi.EventMeta{},
},
},
cvs: testcanvas.MustNew(image.Rect(0, 0, 13, 4)),
@ -213,12 +273,14 @@ func TestMirror(t *testing.T) {
desc: "draws both keyboard and mouse events",
keyEvents: []keyEvents{
{
k: &terminalapi.Keyboard{Key: keyboard.KeyEnter},
k: &terminalapi.Keyboard{Key: keyboard.KeyEnter},
meta: &widgetapi.EventMeta{},
},
},
mouseEvents: []mouseEvents{
{
m: &terminalapi.Mouse{Button: mouse.ButtonLeft},
m: &terminalapi.Mouse{Button: mouse.ButtonLeft},
meta: &widgetapi.EventMeta{},
},
},
cvs: testcanvas.MustNew(image.Rect(0, 0, 17, 5)),
@ -238,19 +300,23 @@ func TestMirror(t *testing.T) {
desc: "KeyEsc and ButtonRight reset the last event and return error",
keyEvents: []keyEvents{
{
k: &terminalapi.Keyboard{Key: keyboard.KeyEnter},
k: &terminalapi.Keyboard{Key: keyboard.KeyEnter},
meta: &widgetapi.EventMeta{},
},
{
k: &terminalapi.Keyboard{Key: keyboard.KeyEsc},
meta: &widgetapi.EventMeta{},
wantErr: true,
},
},
mouseEvents: []mouseEvents{
{
m: &terminalapi.Mouse{Button: mouse.ButtonLeft},
m: &terminalapi.Mouse{Button: mouse.ButtonLeft},
meta: &widgetapi.EventMeta{},
},
{
m: &terminalapi.Mouse{Button: mouse.ButtonRight},
meta: &widgetapi.EventMeta{},
wantErr: true,
},
},
@ -276,14 +342,14 @@ func TestMirror(t *testing.T) {
}
for _, keyEv := range tc.keyEvents {
err := w.Keyboard(keyEv.k, &widgetapi.EventMeta{})
err := w.Keyboard(keyEv.k, keyEv.meta)
if (err != nil) != keyEv.wantErr {
t.Errorf("Keyboard => got error:%v, wantErr: %v", err, keyEv.wantErr)
}
}
for _, mouseEv := range tc.mouseEvents {
err := w.Mouse(mouseEv.m, &widgetapi.EventMeta{})
err := w.Mouse(mouseEv.m, mouseEv.meta)
if (err != nil) != mouseEv.wantErr {
t.Errorf("Mouse => got error:%v, wantErr: %v", err, mouseEv.wantErr)
}
@ -380,6 +446,35 @@ func TestDraw(t *testing.T) {
return ft
},
},
{
desc: "draws both keyboard and mouse events while focused",
opts: widgetapi.Options{
WantKeyboard: widgetapi.KeyScopeFocused,
WantMouse: widgetapi.MouseScopeWidget,
},
cvs: testcanvas.MustNew(image.Rect(0, 0, 19, 5)),
meta: &widgetapi.Meta{},
events: []*Event{
{
Ev: &terminalapi.Keyboard{Key: keyboard.KeyEnter},
Meta: &widgetapi.EventMeta{Focused: true},
},
{
Ev: &terminalapi.Mouse{Button: mouse.ButtonLeft},
Meta: &widgetapi.EventMeta{Focused: true},
},
},
want: func(size image.Point) *faketerm.Terminal {
ft := faketerm.MustNew(size)
cvs := testcanvas.MustNew(ft.Area())
testdraw.MustBorder(cvs, cvs.Area())
testdraw.MustText(cvs, "(19,5)", image.Point{1, 1})
testdraw.MustText(cvs, "F:KeyEnter", image.Point{1, 2})
testdraw.MustText(cvs, "F:(0,0)ButtonLeft", image.Point{1, 3})
testcanvas.MustApply(cvs, ft)
return ft
},
},
}
for _, tc := range tests {