Skip to content

Commit

Permalink
Add clipboard to fyne.App. Fixes fyne-io#4418.
Browse files Browse the repository at this point in the history
NB. getting a lot of errors when running go test ./..., unrelated
to this change. Will investigate some of them as they relate to the
locale, others I have no idea.
  • Loading branch information
pierre committed Sep 8, 2024
1 parent 7d81356 commit f1e0d6a
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 10 deletions.
5 changes: 5 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 4 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand Down
4 changes: 4 additions & 0 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 10 additions & 10 deletions cmd/fyne_demo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, a, w)
})
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, a, w)
})
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, a, w)
})
pasteItem.Shortcut = pasteShortcut
performFind := func() { fmt.Println("Menu Find") }
Expand Down Expand Up @@ -251,14 +251,14 @@ 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) {
func shortcutFocused(s fyne.Shortcut, a fyne.App, w fyne.Window) {
switch sh := s.(type) {
case *fyne.ShortcutCopy:
sh.Clipboard = w.Clipboard()
sh.Clipboard = a.Clipboard()
case *fyne.ShortcutCut:
sh.Clipboard = w.Clipboard()
sh.Clipboard = a.Clipboard()
case *fyne.ShortcutPaste:
sh.Clipboard = w.Clipboard()
sh.Clipboard = a.Clipboard()
}
if focused, ok := w.Canvas().Focused().(fyne.Shortcutable); ok {
focused.TypedShortcut(s)
Expand Down
5 changes: 5 additions & 0 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@ type Driver interface {
//
// Since: 2.5
SetDisableScreenBlanking(bool)

// Clipboard returns the system clipboard.
//
// Since: 2.6
Clipboard() Clipboard
}
4 changes: 4 additions & 0 deletions internal/driver/glfw/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 4 additions & 0 deletions internal/driver/mobile/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 4 additions & 0 deletions test/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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?
}
Expand Down
4 changes: 4 additions & 0 deletions test/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions theme/themedtestapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,7 @@ func (t *themedApp) ShowAnimations() bool {

func (t *themedApp) AddChangeListener(chan fyne.Settings) {
}

func (t *themedApp) Clipboard() fyne.Clipboard {
return nil
}
2 changes: 2 additions & 0 deletions window.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,7 @@ type Window interface {
Canvas() Canvas

// Clipboard returns the system clipboard
//
// Deprecated: use App.Clipboard() instead.
Clipboard() Clipboard
}

0 comments on commit f1e0d6a

Please sign in to comment.