property.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. // Copyright 2012 The Walk Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build windows
  5. package walk
  6. import (
  7. "errors"
  8. )
  9. var (
  10. ErrPropertyReadOnly = errors.New("read-only property")
  11. ErrPropertyNotValidatable = errors.New("property not validatable")
  12. )
  13. type Property interface {
  14. ReadOnly() bool
  15. Get() interface{}
  16. Set(value interface{}) error
  17. Changed() *Event
  18. Source() interface{}
  19. SetSource(source interface{}) error
  20. Validatable() bool
  21. Validator() Validator
  22. SetValidator(validator Validator) error
  23. }
  24. type property struct {
  25. get func() interface{}
  26. set func(v interface{}) error
  27. changed *Event
  28. source interface{}
  29. sourceChangedHandle int
  30. validator Validator
  31. }
  32. func NewProperty(get func() interface{}, set func(v interface{}) error, changed *Event) Property {
  33. return &property{get: get, set: set, changed: changed}
  34. }
  35. func (p *property) ReadOnly() bool {
  36. return p.set == nil
  37. }
  38. func (p *property) Get() interface{} {
  39. return p.get()
  40. }
  41. func (p *property) Set(value interface{}) error {
  42. if p.ReadOnly() {
  43. return ErrPropertyReadOnly
  44. }
  45. if oldValue := p.get(); value == oldValue {
  46. return nil
  47. }
  48. return p.set(value)
  49. }
  50. func (p *property) Changed() *Event {
  51. return p.changed
  52. }
  53. func (p *property) Source() interface{} {
  54. return p.source
  55. }
  56. func (p *property) SetSource(source interface{}) error {
  57. if p.ReadOnly() {
  58. return ErrPropertyReadOnly
  59. }
  60. if source != nil {
  61. switch source := source.(type) {
  62. case string:
  63. // nop
  64. case Property:
  65. if err := checkPropertySource(p, source); err != nil {
  66. return err
  67. }
  68. if source != nil {
  69. p.Set(source.Get())
  70. p.sourceChangedHandle = source.Changed().Attach(func() {
  71. p.Set(source.Get())
  72. })
  73. }
  74. default:
  75. return newError("invalid source type")
  76. }
  77. }
  78. if oldProp, ok := p.source.(Property); ok {
  79. oldProp.Changed().Detach(p.sourceChangedHandle)
  80. }
  81. p.source = source
  82. return nil
  83. }
  84. func (p *property) Validatable() bool {
  85. return true
  86. }
  87. func (p *property) Validator() Validator {
  88. return p.validator
  89. }
  90. func (p *property) SetValidator(validator Validator) error {
  91. if p.ReadOnly() {
  92. return ErrPropertyReadOnly
  93. }
  94. p.validator = validator
  95. return nil
  96. }
  97. type readOnlyProperty struct {
  98. get func() interface{}
  99. changed *Event
  100. }
  101. func NewReadOnlyProperty(get func() interface{}, changed *Event) Property {
  102. return &readOnlyProperty{get: get, changed: changed}
  103. }
  104. func (*readOnlyProperty) ReadOnly() bool {
  105. return true
  106. }
  107. func (rop *readOnlyProperty) Get() interface{} {
  108. return rop.get()
  109. }
  110. func (*readOnlyProperty) Set(value interface{}) error {
  111. return ErrPropertyReadOnly
  112. }
  113. func (rop *readOnlyProperty) Changed() *Event {
  114. return rop.changed
  115. }
  116. func (*readOnlyProperty) Source() interface{} {
  117. return nil
  118. }
  119. func (*readOnlyProperty) SetSource(source interface{}) error {
  120. return ErrPropertyReadOnly
  121. }
  122. func (*readOnlyProperty) Validatable() bool {
  123. return false
  124. }
  125. func (*readOnlyProperty) Validator() Validator {
  126. return nil
  127. }
  128. func (*readOnlyProperty) SetValidator(validator Validator) error {
  129. return ErrPropertyReadOnly
  130. }
  131. type boolProperty struct {
  132. get func() bool
  133. set func(v bool) error
  134. changed *Event
  135. source interface{}
  136. sourceChangedHandle int
  137. }
  138. func NewBoolProperty(get func() bool, set func(b bool) error, changed *Event) Property {
  139. return &boolProperty{get: get, set: set, changed: changed}
  140. }
  141. func (bp *boolProperty) ReadOnly() bool {
  142. return bp.set == nil
  143. }
  144. func (bp *boolProperty) Get() interface{} {
  145. return bp.get()
  146. }
  147. func (bp *boolProperty) Set(value interface{}) error {
  148. if bp.ReadOnly() {
  149. return ErrPropertyReadOnly
  150. }
  151. /* FIXME: Visible property doesn't like this.
  152. if oldValue := bp.get(); value == oldValue {
  153. return nil
  154. }*/
  155. return bp.set(value.(bool))
  156. }
  157. func (bp *boolProperty) Changed() *Event {
  158. return bp.changed
  159. }
  160. func (bp *boolProperty) Source() interface{} {
  161. return bp.source
  162. }
  163. func (bp *boolProperty) SetSource(source interface{}) error {
  164. if bp.ReadOnly() {
  165. return ErrPropertyReadOnly
  166. }
  167. if source != nil {
  168. switch source := source.(type) {
  169. case string:
  170. // nop
  171. case Condition:
  172. if err := checkPropertySource(bp, source); err != nil {
  173. return err
  174. }
  175. if err := bp.Set(source.Satisfied()); err != nil {
  176. return err
  177. }
  178. bp.sourceChangedHandle = source.Changed().Attach(func() {
  179. bp.Set(source.Satisfied())
  180. })
  181. default:
  182. return newError("invalid source type")
  183. }
  184. }
  185. if oldCond, ok := bp.source.(Condition); ok {
  186. oldCond.Changed().Detach(bp.sourceChangedHandle)
  187. }
  188. bp.source = source
  189. return nil
  190. }
  191. func (bp *boolProperty) Validatable() bool {
  192. return false
  193. }
  194. func (*boolProperty) Validator() Validator {
  195. return nil
  196. }
  197. func (*boolProperty) SetValidator(validator Validator) error {
  198. return ErrPropertyNotValidatable
  199. }
  200. func (bp *boolProperty) Satisfied() bool {
  201. return bp.get()
  202. }
  203. type readOnlyBoolProperty struct {
  204. get func() bool
  205. changed *Event
  206. }
  207. func NewReadOnlyBoolProperty(get func() bool, changed *Event) Property {
  208. return &readOnlyBoolProperty{get: get, changed: changed}
  209. }
  210. func (*readOnlyBoolProperty) ReadOnly() bool {
  211. return true
  212. }
  213. func (robp *readOnlyBoolProperty) Get() interface{} {
  214. return robp.get()
  215. }
  216. func (*readOnlyBoolProperty) Set(value interface{}) error {
  217. return ErrPropertyReadOnly
  218. }
  219. func (robp *readOnlyBoolProperty) Changed() *Event {
  220. return robp.changed
  221. }
  222. func (*readOnlyBoolProperty) Source() interface{} {
  223. return nil
  224. }
  225. func (*readOnlyBoolProperty) SetSource(source interface{}) error {
  226. return ErrPropertyReadOnly
  227. }
  228. func (*readOnlyBoolProperty) Validatable() bool {
  229. return false
  230. }
  231. func (*readOnlyBoolProperty) Validator() Validator {
  232. return nil
  233. }
  234. func (*readOnlyBoolProperty) SetValidator(validator Validator) error {
  235. return ErrPropertyNotValidatable
  236. }
  237. func (robp *readOnlyBoolProperty) Satisfied() bool {
  238. return robp.get()
  239. }
  240. func checkPropertySource(prop Property, source interface{}) error {
  241. switch source := source.(type) {
  242. case Property:
  243. for cur := source; cur != nil; cur, _ = cur.Source().(Property) {
  244. if cur == prop {
  245. return newError("source cycle")
  246. }
  247. }
  248. }
  249. return nil
  250. }