Skip to content

Commit

Permalink
Move shortcuts to the event queue (fyne-io#613)
Browse files Browse the repository at this point in the history
This does break the definition of shortcuttable, and removes the functionality to check if it will apply.
But nonetheless seems complete - we should not have been sending key presses when they are recognised shortcuts.

Fixes fyne-io#611
  • Loading branch information
andydotxyz authored Jan 15, 2020
1 parent 64cc3ec commit f7f32a2
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 42 deletions.
2 changes: 1 addition & 1 deletion canvasobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,5 @@ type Focusable interface {

// Shortcutable describes any CanvasObject that can respond to shortcut commands (quit, cut, copy, and paste).
type Shortcutable interface {
TypedShortcut(shortcut Shortcut) bool
TypedShortcut(Shortcut)
}
10 changes: 5 additions & 5 deletions internal/driver/glfw/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -970,13 +970,13 @@ func (w *window) keyPressed(viewport *glfw.Window, key glfw.Key, scancode int, a
}

if shortcut != nil {
if shortcutable, ok := w.canvas.Focused().(fyne.Shortcutable); ok {
if shortcutable.TypedShortcut(shortcut) {
return
}
} else if w.canvas.shortcut.TypedShortcut(shortcut) {
if focused, ok := w.canvas.Focused().(fyne.Shortcutable); ok {
w.queueEvent(func() { focused.TypedShortcut(shortcut) })
return
}

w.queueEvent(func() { w.canvas.shortcut.TypedShortcut(shortcut) })
return
}

// No shortcut detected, pass down to TypedKey
Expand Down
13 changes: 5 additions & 8 deletions shortcut.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,12 @@ type ShortcutHandler struct {
}

// TypedShortcut handle the registered shortcut
func (sh *ShortcutHandler) TypedShortcut(shortcut Shortcut) bool {
if shortcut == nil {
return false
func (sh *ShortcutHandler) TypedShortcut(shortcut Shortcut) {
if _, ok := sh.entry[shortcut.ShortcutName()]; !ok {
return
}
if sc, ok := sh.entry[shortcut.ShortcutName()]; ok {
sc(shortcut)
return true
}
return false

sh.entry[shortcut.ShortcutName()](shortcut)
}

// AddShortcut register an handler to be executed when the shortcut action is triggered
Expand Down
15 changes: 3 additions & 12 deletions shortcut_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,10 @@ func TestShortcutHandler_HandleShortcut(t *testing.T) {
pasteCalled = true
})

assert.True(t, handle.TypedShortcut(&ShortcutCut{}))
handle.TypedShortcut(&ShortcutCut{})
assert.True(t, cutCalled)
assert.True(t, handle.TypedShortcut(&ShortcutCopy{}))
handle.TypedShortcut(&ShortcutCopy{})
assert.True(t, copyCalled)
assert.True(t, handle.TypedShortcut(&ShortcutPaste{}))
handle.TypedShortcut(&ShortcutPaste{})
assert.True(t, pasteCalled)
}

func TestShortcutHandler_HandleShortcut_Failures(t *testing.T) {
handle := &ShortcutHandler{}
handle.AddShortcut(&ShortcutPaste{}, func(shortcut Shortcut) {})

assert.False(t, handle.TypedShortcut(nil))
assert.False(t, handle.TypedShortcut(&ShortcutCut{}))

}
4 changes: 2 additions & 2 deletions widget/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -963,8 +963,8 @@ func (e *Entry) TypedKey(key *fyne.KeyEvent) {
}

// TypedShortcut implements the Shortcutable interface
func (e *Entry) TypedShortcut(shortcut fyne.Shortcut) bool {
return e.shortcut.TypedShortcut(shortcut)
func (e *Entry) TypedShortcut(shortcut fyne.Shortcut) {
e.shortcut.TypedShortcut(shortcut)
}

// textProvider returns the text handler for this entry
Expand Down
21 changes: 7 additions & 14 deletions widget/entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -793,9 +793,8 @@ func TestEntry_OnCut(t *testing.T) {

clipboard := test.NewClipboard()
shortcut := &fyne.ShortcutCut{Clipboard: clipboard}
handled := e.TypedShortcut(shortcut)
e.TypedShortcut(shortcut)

assert.True(t, handled)
assert.Equal(t, "sti", clipboard.Content())
assert.Equal(t, "Teng", e.Text)
}
Expand All @@ -807,9 +806,8 @@ func TestEntry_OnCut_Password(t *testing.T) {

clipboard := test.NewClipboard()
shortcut := &fyne.ShortcutCut{Clipboard: clipboard}
handled := e.TypedShortcut(shortcut)
e.TypedShortcut(shortcut)

assert.True(t, handled)
assert.Equal(t, "", clipboard.Content())
assert.Equal(t, "Testing", e.Text)
}
Expand All @@ -821,9 +819,8 @@ func TestEntry_OnCopy(t *testing.T) {

clipboard := test.NewClipboard()
shortcut := &fyne.ShortcutCopy{Clipboard: clipboard}
handled := e.TypedShortcut(shortcut)
e.TypedShortcut(shortcut)

assert.True(t, handled)
assert.Equal(t, "sti", clipboard.Content())
assert.Equal(t, "Testing", e.Text)
}
Expand All @@ -835,9 +832,8 @@ func TestEntry_OnCopy_Password(t *testing.T) {

clipboard := test.NewClipboard()
shortcut := &fyne.ShortcutCopy{Clipboard: clipboard}
handled := e.TypedShortcut(shortcut)
e.TypedShortcut(shortcut)

assert.True(t, handled)
assert.Equal(t, "", clipboard.Content())
assert.Equal(t, "Testing", e.Text)
}
Expand Down Expand Up @@ -913,8 +909,7 @@ func TestEntry_OnPaste(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
clipboard.SetContent(tt.clipboardContent)
handled := tt.entry.TypedShortcut(shortcut)
assert.True(t, handled)
tt.entry.TypedShortcut(shortcut)
assert.Equal(t, tt.wantText, tt.entry.Text)
assert.Equal(t, tt.wantRow, tt.entry.CursorRow)
assert.Equal(t, tt.wantCol, tt.entry.CursorColumn)
Expand All @@ -930,9 +925,8 @@ func TestEntry_PasteOverSelection(t *testing.T) {
clipboard := test.NewClipboard()
clipboard.SetContent("Insert")
shortcut := &fyne.ShortcutPaste{Clipboard: clipboard}
handled := e.TypedShortcut(shortcut)
e.TypedShortcut(shortcut)

assert.True(t, handled)
assert.Equal(t, "Insert", clipboard.Content())
assert.Equal(t, "TeInsertng", e.Text)
}
Expand Down Expand Up @@ -1450,9 +1444,8 @@ func TestEntry_PasteUnicode(t *testing.T) {
clipboard := test.NewClipboard()
clipboard.SetContent("thing {\n\titem: 'val测试'\n}")
shortcut := &fyne.ShortcutPaste{Clipboard: clipboard}
handled := e.TypedShortcut(shortcut)
e.TypedShortcut(shortcut)

assert.True(t, handled)
assert.Equal(t, "thing {\n\titem: 'val测试'\n}", clipboard.Content())
assert.Equal(t, "linething {\n\titem: 'val测试'\n}", e.Text)

Expand Down

0 comments on commit f7f32a2

Please sign in to comment.