Skip to content

Commit

Permalink
Use separete shortcut functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacalz committed Nov 11, 2021
1 parent 59381a6 commit a2c5335
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
22 changes: 10 additions & 12 deletions calc.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,17 @@ func (c *calc) onTypedKey(ev *fyne.KeyEvent) {
}
}

func (c *calc) onTypedShortcut(shortcut fyne.Shortcut) {
if pasted, ok := shortcut.(*fyne.ShortcutPaste); ok {
content := pasted.Clipboard.Content()
if _, err := strconv.ParseFloat(content, 64); err == nil {
c.display(c.equation + content)
}

func (c *calc) onPasteShortcut(shortcut fyne.Shortcut) {
content := shortcut.(*fyne.ShortcutPaste).Clipboard.Content()
if _, err := strconv.ParseFloat(content, 64); err != nil {
return
}

if copied, ok := shortcut.(*fyne.ShortcutCopy); ok {
copied.Clipboard.SetContent(c.equation)
}
c.display(c.equation + content)
}

func (c *calc) onCopyShortcut(shortcut fyne.Shortcut) {
shortcut.(*fyne.ShortcutCopy).Clipboard.SetContent(c.equation)
}

func (c *calc) loadUI(app fyne.App) {
Expand Down Expand Up @@ -144,8 +142,8 @@ func (c *calc) loadUI(app fyne.App) {

c.window.Canvas().SetOnTypedRune(c.onTypedRune)
c.window.Canvas().SetOnTypedKey(c.onTypedKey)
c.window.Canvas().AddShortcut(&fyne.ShortcutCopy{}, c.onTypedShortcut)
c.window.Canvas().AddShortcut(&fyne.ShortcutPaste{}, c.onTypedShortcut)
c.window.Canvas().AddShortcut(&fyne.ShortcutCopy{}, c.onCopyShortcut)
c.window.Canvas().AddShortcut(&fyne.ShortcutPaste{}, c.onPasteShortcut)
c.window.Resize(fyne.NewSize(200, 300))
c.window.Show()
}
Expand Down
6 changes: 3 additions & 3 deletions calc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,16 +162,16 @@ func TestShortcuts(t *testing.T) {
clipboard := app.Driver().AllWindows()[0].Clipboard()

test.TypeOnCanvas(calc.window.Canvas(), "720 + 80")
calc.onTypedShortcut(&fyne.ShortcutCopy{Clipboard: clipboard})
calc.onCopyShortcut(&fyne.ShortcutCopy{Clipboard: clipboard})
assert.Equal(t, clipboard.Content(), calc.output.Text)

test.TypeOnCanvas(calc.window.Canvas(), "+")
clipboard.SetContent("50")
calc.onTypedShortcut(&fyne.ShortcutPaste{Clipboard: clipboard})
calc.onPasteShortcut(&fyne.ShortcutPaste{Clipboard: clipboard})
test.TypeOnCanvas(calc.window.Canvas(), "=")
assert.Equal(t, "850", calc.output.Text)

clipboard.SetContent("not a valid number")
calc.onTypedShortcut(&fyne.ShortcutPaste{Clipboard: clipboard})
calc.onPasteShortcut(&fyne.ShortcutPaste{Clipboard: clipboard})
assert.Equal(t, "850", calc.output.Text)
}

0 comments on commit a2c5335

Please sign in to comment.