gui.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. package main
  2. import (
  3. "fmt"
  4. "fyne.io/fyne/v2"
  5. "fyne.io/fyne/v2/app"
  6. "fyne.io/fyne/v2/container"
  7. "fyne.io/fyne/v2/data/binding"
  8. "fyne.io/fyne/v2/dialog"
  9. "fyne.io/fyne/v2/driver/desktop"
  10. "fyne.io/fyne/v2/storage"
  11. "fyne.io/fyne/v2/widget"
  12. )
  13. var APP fyne.App
  14. var HOME fyne.Window
  15. var NEED_SAVEDIR bool
  16. var SAVEDIR string
  17. var SAVEBIND binding.String
  18. func SetupApp() {
  19. APP = app.NewWithID("apollox.endless-edit")
  20. HOME = APP.NewWindow("Endless Edit")
  21. HOME.CenterOnScreen()
  22. HOME.Resize(fyne.NewSize(1200, 900))
  23. pref := APP.Preferences()
  24. SAVEDIR = pref.StringWithFallback("save_dir", "")
  25. SAVEBIND = binding.BindString(&SAVEDIR)
  26. if SAVEDIR == "" {
  27. NEED_SAVEDIR = true
  28. SetSaveDir()
  29. }
  30. }
  31. func SetSaveDir() {
  32. pref := APP.Preferences()
  33. d := dialog.NewFolderOpen(func(lu fyne.ListableURI, err error) {
  34. if err != nil {
  35. d1 := dialog.NewError(fmt.Errorf("newfolderopen.err %v", err), HOME)
  36. d1.Show()
  37. return
  38. }
  39. if lu == nil {
  40. return
  41. }
  42. pref.SetString("save_dir", lu.Path())
  43. SAVEBIND.Set(lu.Path())
  44. if NEED_SAVEDIR {
  45. NEED_SAVEDIR = false
  46. }
  47. }, HOME)
  48. if !NEED_SAVEDIR {
  49. uri, err := storage.ParseURI("file://" + pref.String("save_dir"))
  50. if err != nil {
  51. d1 := dialog.NewError(fmt.Errorf("parseuri.err %v", err), HOME)
  52. d1.Show()
  53. return
  54. }
  55. dir, err := storage.ListerForURI(uri)
  56. if err != nil {
  57. d1 := dialog.NewError(fmt.Errorf("listerforuri.err %v", err), HOME)
  58. d1.Show()
  59. return
  60. }
  61. d.SetLocation(dir)
  62. }
  63. d.Show()
  64. }
  65. func SetupHome() {
  66. if HOME == nil {
  67. return
  68. }
  69. meta := APP.Metadata()
  70. pref := APP.Preferences()
  71. // Assign short cuts
  72. new_short := &desktop.CustomShortcut{
  73. fyne.KeyN,
  74. fyne.KeyModifierControl,
  75. }
  76. open_short := &desktop.CustomShortcut{
  77. fyne.KeyO,
  78. fyne.KeyModifierControl,
  79. }
  80. save_short := &desktop.CustomShortcut{
  81. fyne.KeyS,
  82. fyne.KeyModifierControl,
  83. }
  84. about_short := &desktop.CustomShortcut{
  85. fyne.KeyH,
  86. fyne.KeyModifierControl,
  87. }
  88. quit_short := &desktop.CustomShortcut{
  89. fyne.KeyQ,
  90. fyne.KeyModifierControl,
  91. }
  92. var about_save_btn *widget.Button
  93. // For now most stuff just prints out into the terminal/console what we clicked (except about and quit)
  94. file_new := fyne.NewMenuItem("New", func() { // CTRL + N
  95. fmt.Println("File>New")
  96. if NEED_SAVEDIR {
  97. SetSaveDir()
  98. }
  99. })
  100. file_new.Shortcut = new_short
  101. file_open := fyne.NewMenuItem("Open", func() { // CTRL + O
  102. fmt.Println("File>Open")
  103. if NEED_SAVEDIR {
  104. SetSaveDir()
  105. }
  106. })
  107. file_open.Shortcut = open_short
  108. file_save := fyne.NewMenuItem("Save", func() { // CTRL + S
  109. fmt.Println("File>Save")
  110. if NEED_SAVEDIR {
  111. SetSaveDir()
  112. }
  113. })
  114. file_save.Shortcut = save_short
  115. about_save_btn = widget.NewButton(pref.StringWithFallback("save_dir", "Needs to be set"), func() {
  116. SetSaveDir()
  117. })
  118. SAVEBIND.AddListener(binding.NewDataListener(func() { // Use a binding to update based on the preference change (More of a signal/slot to say hey update)
  119. val, err := SAVEBIND.Get()
  120. if err != nil {
  121. d1 := dialog.NewError(fmt.Errorf("savebind.err %v", err), HOME)
  122. d1.Show()
  123. return
  124. }
  125. about_save_btn.Text = val
  126. about_save_btn.Refresh()
  127. }))
  128. showing_about := false
  129. about := func() {
  130. d := dialog.NewCustom("About - Endless Edit", "Close", container.NewVBox(
  131. container.NewGridWithColumns(2,
  132. widget.NewLabel("Endless Edit"), widget.NewLabel(meta.Version),
  133. widget.NewLabel("Save Directory"), about_save_btn,
  134. widget.NewLabel("Apollo"), widget.NewLabel("© 2022"),
  135. ),
  136. ), HOME)
  137. d.SetOnClosed(func() {
  138. if showing_about {
  139. showing_about = false
  140. }
  141. about_save_btn.Text = pref.StringWithFallback("save_dir", "Needs to be set")
  142. about_save_btn.Refresh()
  143. })
  144. if !showing_about {
  145. showing_about = true
  146. d.Show()
  147. }
  148. }
  149. file_about := fyne.NewMenuItem("About", about) // CTRL + H
  150. file_about.Shortcut = about_short
  151. file_quit := fyne.NewMenuItem("Quit", func() { // CTRL + Q
  152. APP.Quit()
  153. })
  154. file_quit.Shortcut = quit_short
  155. file := fyne.NewMenu("File",
  156. file_new,
  157. file_open,
  158. file_save,
  159. fyne.NewMenuItemSeparator(),
  160. file_about,
  161. file_quit,
  162. )
  163. HOME.Canvas().AddShortcut(new_short, func(shortcut fyne.Shortcut) {
  164. fmt.Println("File>New (Via Shortcut)")
  165. if NEED_SAVEDIR {
  166. SetSaveDir()
  167. about_save_btn.Text = pref.StringWithFallback("save_dir", "Needs to be set")
  168. about_save_btn.Refresh()
  169. }
  170. })
  171. HOME.Canvas().AddShortcut(quit_short, func(shortcut fyne.Shortcut) {
  172. APP.Quit()
  173. })
  174. HOME.Canvas().AddShortcut(open_short, func(shortcut fyne.Shortcut) {
  175. fmt.Println("File>Open (Via Shortcut)")
  176. if NEED_SAVEDIR {
  177. SetSaveDir()
  178. about_save_btn.Text = pref.StringWithFallback("save_dir", "Needs to be set")
  179. about_save_btn.Refresh()
  180. }
  181. })
  182. HOME.Canvas().AddShortcut(save_short, func(shortcut fyne.Shortcut) {
  183. fmt.Println("File>Save (Via Shortcut)")
  184. if NEED_SAVEDIR {
  185. SetSaveDir()
  186. about_save_btn.Text = pref.StringWithFallback("save_dir", "Needs to be set")
  187. about_save_btn.Refresh()
  188. }
  189. })
  190. HOME.Canvas().AddShortcut(about_short, func(shortcut fyne.Shortcut) {
  191. about()
  192. })
  193. HOME.SetMainMenu(fyne.NewMainMenu(
  194. file,
  195. fyne.NewMenu("Edit", // This will most likely become AppTabs instead (as we'll be editing Profile, Account, Conditions and Ships)
  196. fyne.NewMenuItem("Profile", func() {
  197. fmt.Println("Edit>Profile")
  198. }),
  199. fyne.NewMenuItem("Ships", func() {
  200. fmt.Println("Edit>Ships")
  201. }),
  202. fyne.NewMenuItem("Account", func() {
  203. fmt.Println("Edit>Account")
  204. }),
  205. ),
  206. ))
  207. }