window.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package mobile
  2. import (
  3. "fyne.io/fyne/v2"
  4. "fyne.io/fyne/v2/container"
  5. "fyne.io/fyne/v2/internal/cache"
  6. "fyne.io/fyne/v2/internal/driver/common"
  7. "fyne.io/fyne/v2/layout"
  8. "fyne.io/fyne/v2/theme"
  9. "fyne.io/fyne/v2/widget"
  10. )
  11. type window struct {
  12. common.Window
  13. title string
  14. visible bool
  15. onClosed func()
  16. onCloseIntercepted func()
  17. isChild bool
  18. clipboard fyne.Clipboard
  19. canvas *mobileCanvas
  20. icon fyne.Resource
  21. menu *fyne.MainMenu
  22. }
  23. func (w *window) Title() string {
  24. return w.title
  25. }
  26. func (w *window) SetTitle(title string) {
  27. w.title = title
  28. }
  29. func (w *window) FullScreen() bool {
  30. return true
  31. }
  32. func (w *window) SetFullScreen(bool) {
  33. // no-op
  34. }
  35. func (w *window) Resize(size fyne.Size) {
  36. w.Canvas().(*mobileCanvas).Resize(size)
  37. }
  38. func (w *window) RequestFocus() {
  39. // no-op - we cannot change which window is focused
  40. }
  41. func (w *window) FixedSize() bool {
  42. return true
  43. }
  44. func (w *window) SetFixedSize(bool) {
  45. // no-op - all windows are fixed size
  46. }
  47. func (w *window) CenterOnScreen() {
  48. // no-op
  49. }
  50. func (w *window) Padded() bool {
  51. return w.canvas.padded
  52. }
  53. func (w *window) SetPadded(padded bool) {
  54. w.canvas.padded = padded
  55. }
  56. func (w *window) Icon() fyne.Resource {
  57. if w.icon == nil {
  58. return fyne.CurrentApp().Icon()
  59. }
  60. return w.icon
  61. }
  62. func (w *window) SetIcon(icon fyne.Resource) {
  63. w.icon = icon
  64. }
  65. func (w *window) SetMaster() {
  66. // no-op on mobile
  67. }
  68. func (w *window) MainMenu() *fyne.MainMenu {
  69. return w.menu
  70. }
  71. func (w *window) SetMainMenu(menu *fyne.MainMenu) {
  72. w.menu = menu
  73. }
  74. func (w *window) SetOnClosed(callback func()) {
  75. w.onClosed = callback
  76. }
  77. func (w *window) SetCloseIntercept(callback func()) {
  78. w.onCloseIntercepted = callback
  79. }
  80. func (w *window) SetOnDropped(dropped func(fyne.Position, []fyne.URI)) {
  81. // not implemented yet
  82. }
  83. func (w *window) Show() {
  84. menu := fyne.CurrentApp().Driver().(*mobileDriver).findMenu(w)
  85. menuButton := w.newMenuButton(menu)
  86. if menu == nil {
  87. menuButton.Hide()
  88. }
  89. if w.isChild {
  90. exit := widget.NewButtonWithIcon("", theme.CancelIcon(), func() {
  91. w.tryClose()
  92. })
  93. title := widget.NewLabel(w.title)
  94. title.Alignment = fyne.TextAlignCenter
  95. w.canvas.setWindowHead(container.NewHBox(menuButton,
  96. layout.NewSpacer(), title, layout.NewSpacer(), exit))
  97. w.canvas.Resize(w.canvas.size)
  98. } else {
  99. w.canvas.setWindowHead(container.NewHBox(menuButton))
  100. }
  101. w.visible = true
  102. if w.Content() != nil {
  103. w.Content().Refresh()
  104. w.Content().Show()
  105. }
  106. }
  107. func (w *window) Hide() {
  108. w.visible = false
  109. if w.Content() != nil {
  110. w.Content().Hide()
  111. }
  112. }
  113. func (w *window) tryClose() {
  114. if w.onCloseIntercepted != nil {
  115. w.QueueEvent(w.onCloseIntercepted)
  116. return
  117. }
  118. w.Close()
  119. }
  120. func (w *window) Close() {
  121. d := fyne.CurrentApp().Driver().(*mobileDriver)
  122. pos := -1
  123. for i, win := range d.windows {
  124. if win == w {
  125. pos = i
  126. }
  127. }
  128. if pos != -1 {
  129. d.windows = append(d.windows[:pos], d.windows[pos+1:]...)
  130. }
  131. cache.RangeTexturesFor(w.canvas, func(obj fyne.CanvasObject) {
  132. w.canvas.Painter().Free(obj)
  133. })
  134. w.canvas.WalkTrees(nil, func(node *common.RenderCacheNode, _ fyne.Position) {
  135. if wid, ok := node.Obj().(fyne.Widget); ok {
  136. cache.DestroyRenderer(wid)
  137. }
  138. })
  139. w.QueueEvent(func() {
  140. cache.CleanCanvas(w.canvas)
  141. })
  142. // Call this in a go routine, because this function could be called
  143. // inside a button which callback would be queued in this event queue
  144. // and it will lead to a deadlock if this is performed in the same go
  145. // routine.
  146. go w.DestroyEventQueue()
  147. if w.onClosed != nil {
  148. w.onClosed()
  149. }
  150. }
  151. func (w *window) ShowAndRun() {
  152. w.Show()
  153. fyne.CurrentApp().Driver().Run()
  154. }
  155. func (w *window) Content() fyne.CanvasObject {
  156. return w.canvas.Content()
  157. }
  158. func (w *window) SetContent(content fyne.CanvasObject) {
  159. w.canvas.SetContent(content)
  160. }
  161. func (w *window) Canvas() fyne.Canvas {
  162. return w.canvas
  163. }
  164. func (w *window) Clipboard() fyne.Clipboard {
  165. if w.clipboard == nil {
  166. w.clipboard = &mobileClipboard{}
  167. }
  168. return w.clipboard
  169. }
  170. func (w *window) RunWithContext(f func()) {
  171. // ctx, _ = e.DrawContext.(gl.Context)
  172. f()
  173. }
  174. func (w *window) RescaleContext() {
  175. // TODO
  176. }
  177. func (w *window) Context() interface{} {
  178. return fyne.CurrentApp().Driver().(*mobileDriver).glctx
  179. }