From 55c6d11f29027dccd992917d69839793c0af8cd2 Mon Sep 17 00:00:00 2001 From: pierre Date: Sun, 8 Sep 2024 10:13:22 +0200 Subject: [PATCH] Add fyne.App.Clipboard method, fixes #4418 --- app.go | 5 +++++ app/app.go | 4 ++++ app_test.go | 4 ++++ cmd/fyne_demo/main.go | 24 ++++++++---------------- driver.go | 5 +++++ internal/driver/glfw/driver.go | 4 ++++ internal/driver/mobile/driver.go | 4 ++++ test/app.go | 4 ++++ test/driver.go | 4 ++++ theme/themedtestapp_test.go | 4 ++++ window.go | 2 ++ 11 files changed, 48 insertions(+), 16 deletions(-) diff --git a/app.go b/app.go index 74ac0bf89a..eadd1c8cd5 100644 --- a/app.go +++ b/app.go @@ -79,6 +79,11 @@ type App interface { // // Since: 2.3 SetCloudProvider(CloudProvider) // configure cloud for this app + + // Clipboard returns the system clipboard. + // + // Since: 2.6 + Clipboard() Clipboard } var app atomic.Pointer[App] diff --git a/app/app.go b/app/app.go index 6c44d95141..d6bf57a55e 100644 --- a/app/app.go +++ b/app/app.go @@ -109,6 +109,10 @@ func (a *fyneApp) newDefaultPreferences() *preferences { return p } +func (a *fyneApp) Clipboard() fyne.Clipboard { + return a.Driver().Clipboard() +} + // New returns a new application instance with the default driver and no unique ID (unless specified in FyneApp.toml) func New() fyne.App { if meta.ID == "" { diff --git a/app_test.go b/app_test.go index 1322b3c918..46c2ad697b 100644 --- a/app_test.go +++ b/app_test.go @@ -68,6 +68,10 @@ func (dummyApp) Metadata() AppMetadata { return AppMetadata{} } +func (dummyApp) Clipboard() Clipboard { + return nil +} + func TestSetCurrentApp(t *testing.T) { a := &dummyApp{} SetCurrentApp(a) diff --git a/cmd/fyne_demo/main.go b/cmd/fyne_demo/main.go index 6a1f0c53de..07b6cbbbeb 100644 --- a/cmd/fyne_demo/main.go +++ b/cmd/fyne_demo/main.go @@ -133,19 +133,19 @@ func makeMenu(a fyne.App, w fyne.Window) *fyne.MainMenu { openSettings() }) - cutShortcut := &fyne.ShortcutCut{Clipboard: w.Clipboard()} + cutShortcut := &fyne.ShortcutCut{Clipboard: a.Clipboard()} cutItem := fyne.NewMenuItem("Cut", func() { - shortcutFocused(cutShortcut, w) + shortcutFocused(cutShortcut, w.Canvas().Focused()) }) cutItem.Shortcut = cutShortcut - copyShortcut := &fyne.ShortcutCopy{Clipboard: w.Clipboard()} + copyShortcut := &fyne.ShortcutCopy{Clipboard: a.Clipboard()} copyItem := fyne.NewMenuItem("Copy", func() { - shortcutFocused(copyShortcut, w) + shortcutFocused(copyShortcut, w.Canvas().Focused()) }) copyItem.Shortcut = copyShortcut - pasteShortcut := &fyne.ShortcutPaste{Clipboard: w.Clipboard()} + pasteShortcut := &fyne.ShortcutPaste{Clipboard: a.Clipboard()} pasteItem := fyne.NewMenuItem("Paste", func() { - shortcutFocused(pasteShortcut, w) + shortcutFocused(pasteShortcut, w.Canvas().Focused()) }) pasteItem.Shortcut = pasteShortcut performFind := func() { fmt.Println("Menu Find") } @@ -251,16 +251,8 @@ func makeNav(setTutorial func(tutorial tutorials.Tutorial), loadPrevious bool) f return container.NewBorder(nil, themes, nil, nil, tree) } -func shortcutFocused(s fyne.Shortcut, w fyne.Window) { - switch sh := s.(type) { - case *fyne.ShortcutCopy: - sh.Clipboard = w.Clipboard() - case *fyne.ShortcutCut: - sh.Clipboard = w.Clipboard() - case *fyne.ShortcutPaste: - sh.Clipboard = w.Clipboard() - } - if focused, ok := w.Canvas().Focused().(fyne.Shortcutable); ok { +func shortcutFocused(s fyne.Shortcut, f fyne.Focusable) { + if focused, ok := f.(fyne.Shortcutable); ok { focused.TypedShortcut(s) } } diff --git a/driver.go b/driver.go index 21c4906fb5..4e6fb2819f 100644 --- a/driver.go +++ b/driver.go @@ -43,4 +43,9 @@ type Driver interface { // // Since: 2.5 SetDisableScreenBlanking(bool) + + // Clipboard returns the system clipboard. + // + // Since: 2.6 + Clipboard() Clipboard } diff --git a/internal/driver/glfw/driver.go b/internal/driver/glfw/driver.go index 5f9f254b58..3aa4de6d9d 100644 --- a/internal/driver/glfw/driver.go +++ b/internal/driver/glfw/driver.go @@ -68,6 +68,10 @@ func toOSIcon(icon []byte) ([]byte, error) { return buf.Bytes(), nil } +func (d *gLDriver) Clipboard() fyne.Clipboard { + return &clipboard{} +} + func (d *gLDriver) RenderedTextSize(text string, textSize float32, style fyne.TextStyle, source fyne.Resource) (size fyne.Size, baseline float32) { return painter.RenderedTextSize(text, textSize, style, source) } diff --git a/internal/driver/mobile/driver.go b/internal/driver/mobile/driver.go index 41675ee581..49c306df5c 100644 --- a/internal/driver/mobile/driver.go +++ b/internal/driver/mobile/driver.go @@ -100,6 +100,10 @@ func (d *driver) currentWindow() *window { return last } +func (d *driver) Clipboard() fyne.Clipboard { + return &mobileClipboard{} +} + func (d *driver) RenderedTextSize(text string, textSize float32, style fyne.TextStyle, source fyne.Resource) (size fyne.Size, baseline float32) { return painter.RenderedTextSize(text, textSize, style, source) } diff --git a/test/app.go b/test/app.go index 4e5e427a59..d06318dafc 100644 --- a/test/app.go +++ b/test/app.go @@ -63,6 +63,10 @@ func (a *app) Quit() { // no-op } +func (a *app) Clipboard() fyne.Clipboard { + return nil +} + func (a *app) UniqueID() string { return "testApp" // TODO should this be randomised? } diff --git a/test/driver.go b/test/driver.go index e4a30dc06d..9e093c484d 100644 --- a/test/driver.go +++ b/test/driver.go @@ -116,6 +116,10 @@ func (d *driver) Quit() { // no-op } +func (d *driver) Clipboard() fyne.Clipboard { + return nil +} + func (d *driver) removeWindow(w *window) { d.windowsMutex.Lock() i := 0 diff --git a/theme/themedtestapp_test.go b/theme/themedtestapp_test.go index e5b8a5668e..7bd5dbdd76 100644 --- a/theme/themedtestapp_test.go +++ b/theme/themedtestapp_test.go @@ -106,3 +106,7 @@ func (t *themedApp) ShowAnimations() bool { func (t *themedApp) AddChangeListener(chan fyne.Settings) { } + +func (t *themedApp) Clipboard() fyne.Clipboard { + return nil +} diff --git a/window.go b/window.go index 3e366acb96..604d05c598 100644 --- a/window.go +++ b/window.go @@ -101,5 +101,7 @@ type Window interface { Canvas() Canvas // Clipboard returns the system clipboard + // + // Deprecated: use App.Clipboard() instead. Clipboard() Clipboard }