preference.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // auto-generated
  2. // **** THIS FILE IS AUTO-GENERATED, PLEASE DO NOT EDIT IT **** //
  3. package binding
  4. import (
  5. "sync/atomic"
  6. "fyne.io/fyne/v2"
  7. )
  8. const keyTypeMismatchError = "A previous preference binding exists with different type for key: "
  9. type prefBoundBool struct {
  10. base
  11. key string
  12. p fyne.Preferences
  13. cache atomic.Value // bool
  14. }
  15. // BindPreferenceBool returns a bindable bool value that is managed by the application preferences.
  16. // Changes to this value will be saved to application storage and when the app starts the previous values will be read.
  17. //
  18. // Since: 2.0
  19. func BindPreferenceBool(key string, p fyne.Preferences) Bool {
  20. binds := prefBinds.getBindings(p)
  21. if binds != nil {
  22. if listen := binds.getItem(key); listen != nil {
  23. if l, ok := listen.(Bool); ok {
  24. return l
  25. }
  26. fyne.LogError(keyTypeMismatchError+key, nil)
  27. }
  28. }
  29. listen := &prefBoundBool{key: key, p: p}
  30. binds = prefBinds.ensurePreferencesAttached(p)
  31. binds.setItem(key, listen)
  32. return listen
  33. }
  34. func (b *prefBoundBool) Get() (bool, error) {
  35. cache := b.p.Bool(b.key)
  36. b.cache.Store(cache)
  37. return cache, nil
  38. }
  39. func (b *prefBoundBool) Set(v bool) error {
  40. b.p.SetBool(b.key, v)
  41. b.lock.RLock()
  42. defer b.lock.RUnlock()
  43. b.trigger()
  44. return nil
  45. }
  46. func (b *prefBoundBool) checkForChange() {
  47. val := b.cache.Load()
  48. if val != nil {
  49. cache := val.(bool)
  50. if b.p.Bool(b.key) == cache {
  51. return
  52. }
  53. }
  54. b.trigger()
  55. }
  56. func (b *prefBoundBool) replaceProvider(p fyne.Preferences) {
  57. b.p = p
  58. }
  59. type prefBoundFloat struct {
  60. base
  61. key string
  62. p fyne.Preferences
  63. cache atomic.Value // float64
  64. }
  65. // BindPreferenceFloat returns a bindable float64 value that is managed by the application preferences.
  66. // Changes to this value will be saved to application storage and when the app starts the previous values will be read.
  67. //
  68. // Since: 2.0
  69. func BindPreferenceFloat(key string, p fyne.Preferences) Float {
  70. binds := prefBinds.getBindings(p)
  71. if binds != nil {
  72. if listen := binds.getItem(key); listen != nil {
  73. if l, ok := listen.(Float); ok {
  74. return l
  75. }
  76. fyne.LogError(keyTypeMismatchError+key, nil)
  77. }
  78. }
  79. listen := &prefBoundFloat{key: key, p: p}
  80. binds = prefBinds.ensurePreferencesAttached(p)
  81. binds.setItem(key, listen)
  82. return listen
  83. }
  84. func (b *prefBoundFloat) Get() (float64, error) {
  85. cache := b.p.Float(b.key)
  86. b.cache.Store(cache)
  87. return cache, nil
  88. }
  89. func (b *prefBoundFloat) Set(v float64) error {
  90. b.p.SetFloat(b.key, v)
  91. b.lock.RLock()
  92. defer b.lock.RUnlock()
  93. b.trigger()
  94. return nil
  95. }
  96. func (b *prefBoundFloat) checkForChange() {
  97. val := b.cache.Load()
  98. if val != nil {
  99. cache := val.(float64)
  100. if b.p.Float(b.key) == cache {
  101. return
  102. }
  103. }
  104. b.trigger()
  105. }
  106. func (b *prefBoundFloat) replaceProvider(p fyne.Preferences) {
  107. b.p = p
  108. }
  109. type prefBoundInt struct {
  110. base
  111. key string
  112. p fyne.Preferences
  113. cache atomic.Value // int
  114. }
  115. // BindPreferenceInt returns a bindable int value that is managed by the application preferences.
  116. // Changes to this value will be saved to application storage and when the app starts the previous values will be read.
  117. //
  118. // Since: 2.0
  119. func BindPreferenceInt(key string, p fyne.Preferences) Int {
  120. binds := prefBinds.getBindings(p)
  121. if binds != nil {
  122. if listen := binds.getItem(key); listen != nil {
  123. if l, ok := listen.(Int); ok {
  124. return l
  125. }
  126. fyne.LogError(keyTypeMismatchError+key, nil)
  127. }
  128. }
  129. listen := &prefBoundInt{key: key, p: p}
  130. binds = prefBinds.ensurePreferencesAttached(p)
  131. binds.setItem(key, listen)
  132. return listen
  133. }
  134. func (b *prefBoundInt) Get() (int, error) {
  135. cache := b.p.Int(b.key)
  136. b.cache.Store(cache)
  137. return cache, nil
  138. }
  139. func (b *prefBoundInt) Set(v int) error {
  140. b.p.SetInt(b.key, v)
  141. b.lock.RLock()
  142. defer b.lock.RUnlock()
  143. b.trigger()
  144. return nil
  145. }
  146. func (b *prefBoundInt) checkForChange() {
  147. val := b.cache.Load()
  148. if val != nil {
  149. cache := val.(int)
  150. if b.p.Int(b.key) == cache {
  151. return
  152. }
  153. }
  154. b.trigger()
  155. }
  156. func (b *prefBoundInt) replaceProvider(p fyne.Preferences) {
  157. b.p = p
  158. }
  159. type prefBoundString struct {
  160. base
  161. key string
  162. p fyne.Preferences
  163. cache atomic.Value // string
  164. }
  165. // BindPreferenceString returns a bindable string value that is managed by the application preferences.
  166. // Changes to this value will be saved to application storage and when the app starts the previous values will be read.
  167. //
  168. // Since: 2.0
  169. func BindPreferenceString(key string, p fyne.Preferences) String {
  170. binds := prefBinds.getBindings(p)
  171. if binds != nil {
  172. if listen := binds.getItem(key); listen != nil {
  173. if l, ok := listen.(String); ok {
  174. return l
  175. }
  176. fyne.LogError(keyTypeMismatchError+key, nil)
  177. }
  178. }
  179. listen := &prefBoundString{key: key, p: p}
  180. binds = prefBinds.ensurePreferencesAttached(p)
  181. binds.setItem(key, listen)
  182. return listen
  183. }
  184. func (b *prefBoundString) Get() (string, error) {
  185. cache := b.p.String(b.key)
  186. b.cache.Store(cache)
  187. return cache, nil
  188. }
  189. func (b *prefBoundString) Set(v string) error {
  190. b.p.SetString(b.key, v)
  191. b.lock.RLock()
  192. defer b.lock.RUnlock()
  193. b.trigger()
  194. return nil
  195. }
  196. func (b *prefBoundString) checkForChange() {
  197. val := b.cache.Load()
  198. if val != nil {
  199. cache := val.(string)
  200. if b.p.String(b.key) == cache {
  201. return
  202. }
  203. }
  204. b.trigger()
  205. }
  206. func (b *prefBoundString) replaceProvider(p fyne.Preferences) {
  207. b.p = p
  208. }