123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- package main
- import (
- "fmt"
- "fyne.io/fyne/v2"
- "fyne.io/fyne/v2/app"
- "fyne.io/fyne/v2/container"
- "fyne.io/fyne/v2/data/binding"
- "fyne.io/fyne/v2/dialog"
- "fyne.io/fyne/v2/driver/desktop"
- "fyne.io/fyne/v2/storage"
- "fyne.io/fyne/v2/widget"
- )
- var APP fyne.App
- var HOME fyne.Window
- var NEED_SAVEDIR bool
- var SAVEDIR string
- var SAVEBIND binding.String
- func SetupApp() {
- APP = app.NewWithID("apollox.endless-edit")
- HOME = APP.NewWindow("Endless Edit")
- HOME.CenterOnScreen()
- HOME.Resize(fyne.NewSize(1200, 900))
- pref := APP.Preferences()
- SAVEDIR = pref.StringWithFallback("save_dir", "")
- SAVEBIND = binding.BindString(&SAVEDIR)
- if SAVEDIR == "" {
- NEED_SAVEDIR = true
- SetSaveDir()
- }
- }
- func SetSaveDir() {
- pref := APP.Preferences()
- d := dialog.NewFolderOpen(func(lu fyne.ListableURI, err error) {
- if err != nil {
- d1 := dialog.NewError(fmt.Errorf("newfolderopen.err %v", err), HOME)
- d1.Show()
- return
- }
- if lu == nil {
- return
- }
- pref.SetString("save_dir", lu.Path())
- SAVEBIND.Set(lu.Path())
- if NEED_SAVEDIR {
- NEED_SAVEDIR = false
- }
- }, HOME)
- if !NEED_SAVEDIR {
- uri, err := storage.ParseURI("file://" + pref.String("save_dir"))
- if err != nil {
- d1 := dialog.NewError(fmt.Errorf("parseuri.err %v", err), HOME)
- d1.Show()
- return
- }
- dir, err := storage.ListerForURI(uri)
- if err != nil {
- d1 := dialog.NewError(fmt.Errorf("listerforuri.err %v", err), HOME)
- d1.Show()
- return
- }
- d.SetLocation(dir)
- }
- d.Show()
- }
- func SetupHome() {
- if HOME == nil {
- return
- }
- meta := APP.Metadata()
- pref := APP.Preferences()
- // Assign short cuts
- new_short := &desktop.CustomShortcut{
- fyne.KeyN,
- fyne.KeyModifierControl,
- }
- open_short := &desktop.CustomShortcut{
- fyne.KeyO,
- fyne.KeyModifierControl,
- }
- save_short := &desktop.CustomShortcut{
- fyne.KeyS,
- fyne.KeyModifierControl,
- }
- about_short := &desktop.CustomShortcut{
- fyne.KeyH,
- fyne.KeyModifierControl,
- }
- quit_short := &desktop.CustomShortcut{
- fyne.KeyQ,
- fyne.KeyModifierControl,
- }
- var about_save_btn *widget.Button
- // For now most stuff just prints out into the terminal/console what we clicked (except about and quit)
- file_new := fyne.NewMenuItem("New", func() { // CTRL + N
- fmt.Println("File>New")
- if NEED_SAVEDIR {
- SetSaveDir()
- }
- })
- file_new.Shortcut = new_short
- file_open := fyne.NewMenuItem("Open", func() { // CTRL + O
- fmt.Println("File>Open")
- if NEED_SAVEDIR {
- SetSaveDir()
- }
- })
- file_open.Shortcut = open_short
- file_save := fyne.NewMenuItem("Save", func() { // CTRL + S
- fmt.Println("File>Save")
- if NEED_SAVEDIR {
- SetSaveDir()
- }
- })
- file_save.Shortcut = save_short
- about_save_btn = widget.NewButton(pref.StringWithFallback("save_dir", "Needs to be set"), func() {
- SetSaveDir()
- })
- SAVEBIND.AddListener(binding.NewDataListener(func() { // Use a binding to update based on the preference change (More of a signal/slot to say hey update)
- val, err := SAVEBIND.Get()
- if err != nil {
- d1 := dialog.NewError(fmt.Errorf("savebind.err %v", err), HOME)
- d1.Show()
- return
- }
- about_save_btn.Text = val
- about_save_btn.Refresh()
- }))
- showing_about := false
- about := func() {
- d := dialog.NewCustom("About - Endless Edit", "Close", container.NewVBox(
- container.NewGridWithColumns(2,
- widget.NewLabel("Endless Edit"), widget.NewLabel(meta.Version),
- widget.NewLabel("Save Directory"), about_save_btn,
- widget.NewLabel("Apollo"), widget.NewLabel("© 2022"),
- ),
- ), HOME)
- d.SetOnClosed(func() {
- if showing_about {
- showing_about = false
- }
- about_save_btn.Text = pref.StringWithFallback("save_dir", "Needs to be set")
- about_save_btn.Refresh()
- })
- if !showing_about {
- showing_about = true
- d.Show()
- }
- }
- file_about := fyne.NewMenuItem("About", about) // CTRL + H
- file_about.Shortcut = about_short
- file_quit := fyne.NewMenuItem("Quit", func() { // CTRL + Q
- APP.Quit()
- })
- file_quit.Shortcut = quit_short
- file := fyne.NewMenu("File",
- file_new,
- file_open,
- file_save,
- fyne.NewMenuItemSeparator(),
- file_about,
- file_quit,
- )
- HOME.Canvas().AddShortcut(new_short, func(shortcut fyne.Shortcut) {
- fmt.Println("File>New (Via Shortcut)")
- if NEED_SAVEDIR {
- SetSaveDir()
- about_save_btn.Text = pref.StringWithFallback("save_dir", "Needs to be set")
- about_save_btn.Refresh()
- }
- })
- HOME.Canvas().AddShortcut(quit_short, func(shortcut fyne.Shortcut) {
- APP.Quit()
- })
- HOME.Canvas().AddShortcut(open_short, func(shortcut fyne.Shortcut) {
- fmt.Println("File>Open (Via Shortcut)")
- if NEED_SAVEDIR {
- SetSaveDir()
- about_save_btn.Text = pref.StringWithFallback("save_dir", "Needs to be set")
- about_save_btn.Refresh()
- }
- })
- HOME.Canvas().AddShortcut(save_short, func(shortcut fyne.Shortcut) {
- fmt.Println("File>Save (Via Shortcut)")
- if NEED_SAVEDIR {
- SetSaveDir()
- about_save_btn.Text = pref.StringWithFallback("save_dir", "Needs to be set")
- about_save_btn.Refresh()
- }
- })
- HOME.Canvas().AddShortcut(about_short, func(shortcut fyne.Shortcut) {
- about()
- })
- HOME.SetMainMenu(fyne.NewMainMenu(
- file,
- fyne.NewMenu("Edit", // This will most likely become AppTabs instead (as we'll be editing Profile, Account, Conditions and Ships)
- fyne.NewMenuItem("Profile", func() {
- fmt.Println("Edit>Profile")
- }),
- fyne.NewMenuItem("Ships", func() {
- fmt.Println("Edit>Ships")
- }),
- fyne.NewMenuItem("Account", func() {
- fmt.Println("Edit>Account")
- }),
- ),
- ))
- }
|