Skip to content

Instantly share code, notes, and snippets.

@turing85
Last active November 13, 2023 22:27
Show Gist options
  • Save turing85/d4951f660cb014adcfbcbf3c6d9a999e to your computer and use it in GitHub Desktop.
Save turing85/d4951f660cb014adcfbcbf3c6d9a999e to your computer and use it in GitHub Desktop.
golang fyne example
package main
import (
"fmt"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/data/binding"
"fyne.io/fyne/v2/driver/desktop"
"fyne.io/fyne/v2/widget"
)
var (
altQ = &desktop.CustomShortcut{KeyName: fyne.KeyQ, Modifier: fyne.KeyModifierAlt}
altH = &desktop.CustomShortcut{KeyName: fyne.KeyH, Modifier: fyne.KeyModifierAlt}
altLeft = &desktop.CustomShortcut{KeyName: fyne.KeyLeft, Modifier: fyne.KeyModifierAlt}
altRight = &desktop.CustomShortcut{KeyName: fyne.KeyRight, Modifier: fyne.KeyModifierAlt}
)
func main() {
myApp := app.New().NewWindow("Hello")
quitItem := fyne.NewMenuItem("Quit", myApp.Close)
quitItem.IsQuit = true
quitItem.Shortcut = altQ
fileMenu := fyne.NewMenu("File", quitItem)
mainMenu := fyne.NewMainMenu(fileMenu)
myApp.SetMainMenu(mainMenu)
stringBind := binding.NewString()
myEntryLeft := newMyEntryWithData(stringBind)
updateEntryLeft := widget.NewButton("Update Left", myEntryLeft.updateFromBind)
myEntryRight := newMyEntryWithData(stringBind)
updateEntryRight := widget.NewButton("Update Right", myEntryRight.updateFromBind)
sayHiButton := widget.NewButton(
"Hi!",
func() {
if err := stringBind.Set("Welcome :)"); err != nil {
fmt.Printf("Error %s\n", err)
}
},
)
clickQuitItem := func(shortcut fyne.Shortcut) {
quitItem.Action()
}
clickUpdateEntryLeft := func(shortcut fyne.Shortcut) {
updateEntryLeft.Tapped(nil)
}
clickUpdateEntryRight := func(shortcut fyne.Shortcut) {
updateEntryRight.Tapped(nil)
}
clickHiButton := func(shortcut fyne.Shortcut) {
sayHiButton.Tapped(nil)
}
setupShortcuts(myApp, clickHiButton, clickQuitItem, clickUpdateEntryLeft, clickUpdateEntryRight, myEntryLeft, myEntryRight)
floatBind := binding.NewFloat()
progressBar1 := widget.NewProgressBarWithData(floatBind)
progressBar2 := widget.NewProgressBarWithData(floatBind)
periodicallyIncrease(floatBind)
myApp.SetContent(
container.NewVBox(
container.NewStack(
container.NewGridWithColumns(
2,
container.NewVBox(
container.NewStack(myEntryLeft),
updateEntryLeft,
),
container.NewVBox(
container.NewStack(myEntryRight),
updateEntryRight,
),
),
),
sayHiButton,
container.NewGridWithColumns(
2,
progressBar1,
progressBar2,
),
),
)
myApp.ShowAndRun()
}
func setupShortcuts(myApp fyne.Window, sayHiKeyAction func(shortcut fyne.Shortcut), closeAppKeyAction func(shortcut fyne.Shortcut), updateLeftKeyAction func(shortcut fyne.Shortcut), updateRightKeyAction func(shortcut fyne.Shortcut), myEntryLeft *myEntry, myEntryRight *myEntry) {
myApp.Canvas().AddShortcut(altH, sayHiKeyAction)
myApp.Canvas().AddShortcut(altQ, closeAppKeyAction)
myApp.Canvas().AddShortcut(altLeft, updateLeftKeyAction)
myApp.Canvas().AddShortcut(altRight, updateRightKeyAction)
myEntryLeft.AddShortcut(altH, sayHiKeyAction)
myEntryLeft.AddShortcut(altQ, closeAppKeyAction)
myEntryLeft.AddShortcut(altLeft, updateLeftKeyAction)
myEntryLeft.AddShortcut(altRight, updateRightKeyAction)
myEntryRight.AddShortcut(altH, sayHiKeyAction)
myEntryRight.AddShortcut(altQ, closeAppKeyAction)
myEntryRight.AddShortcut(altLeft, updateLeftKeyAction)
myEntryRight.AddShortcut(altRight, updateRightKeyAction)
}
func periodicallyIncrease(floatBind binding.Float) {
go func() {
for {
if value, err := floatBind.Get(); err != nil {
fmt.Printf("Error: %s\n", err)
} else {
time.Sleep(50 * time.Millisecond)
if value > 1 {
value = 0
}
if err := floatBind.Set(value + 0.01); err != nil {
fmt.Printf("Error: %s\n", err)
}
}
}
}()
}
type myEntry struct {
widget.Entry
stringBind binding.String
shortcuts map[string]func(fyne.Shortcut)
}
func newMyEntryWithData(data binding.String) *myEntry {
myEntry := &myEntry{widget.Entry{Wrapping: fyne.TextWrapWord, MultiLine: true}, data, nil}
myEntry.ExtendBaseWidget(myEntry)
myEntry.shortcuts = make(map[string]func(fyne.Shortcut))
myEntry.Bind(data)
myEntry.Validator = nil
return myEntry
}
func (m *myEntry) updateFromBind() {
if text, err := m.stringBind.Get(); err == nil {
m.Entry.SetText(text)
m.Refresh()
} else {
fmt.Printf("Error: %s", err)
}
}
func (m *myEntry) TypedShortcut(s fyne.Shortcut) {
if _, ok := s.(*desktop.CustomShortcut); !ok {
m.Entry.TypedShortcut(s)
}
if function := m.shortcuts[s.ShortcutName()]; function != nil {
function(s)
}
}
func (m *myEntry) AddShortcut(shortcut fyne.Shortcut, action func(fyne.Shortcut)) {
m.shortcuts[shortcut.ShortcutName()] = action
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment