theme.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package app
  2. import (
  3. "fyne.io/fyne/v2"
  4. "fyne.io/fyne/v2/internal/cache"
  5. )
  6. // ApplyThemeTo ensures that the specified canvasobject and all widgets and themeable objects will
  7. // be updated for the current theme.
  8. func ApplyThemeTo(content fyne.CanvasObject, canv fyne.Canvas) {
  9. if content == nil {
  10. return
  11. }
  12. switch o := content.(type) {
  13. case fyne.Widget:
  14. for _, co := range cache.Renderer(o).Objects() {
  15. ApplyThemeTo(co, canv)
  16. }
  17. cache.Renderer(o).Layout(content.Size()) // theme can cause sizing changes
  18. case *fyne.Container:
  19. for _, co := range o.Objects {
  20. ApplyThemeTo(co, canv)
  21. }
  22. if l := o.Layout; l != nil {
  23. l.Layout(o.Objects, o.Size()) // theme can cause sizing changes
  24. }
  25. }
  26. content.Refresh()
  27. }
  28. // ApplySettings ensures that all widgets and themeable objects in an application will be updated for the current theme.
  29. // It also checks that scale changes are reflected if required
  30. func ApplySettings(set fyne.Settings, app fyne.App) {
  31. ApplySettingsWithCallback(set, app, nil)
  32. }
  33. // ApplySettingsWithCallback ensures that all widgets and themeable objects in an application will be updated for the current theme.
  34. // It also checks that scale changes are reflected if required. Also it will call `onEveryWindow` on every window
  35. // interaction
  36. func ApplySettingsWithCallback(set fyne.Settings, app fyne.App, onEveryWindow func(w fyne.Window)) {
  37. for _, window := range app.Driver().AllWindows() {
  38. ApplyThemeTo(window.Content(), window.Canvas())
  39. for _, overlay := range window.Canvas().Overlays().List() {
  40. ApplyThemeTo(overlay, window.Canvas())
  41. }
  42. if onEveryWindow != nil {
  43. onEveryWindow(window)
  44. }
  45. }
  46. }