bindlists.go 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786
  1. // auto-generated
  2. // **** THIS FILE IS AUTO-GENERATED, PLEASE DO NOT EDIT IT **** //
  3. package binding
  4. import (
  5. "bytes"
  6. "fyne.io/fyne/v2"
  7. )
  8. // BoolList supports binding a list of bool values.
  9. //
  10. // Since: 2.0
  11. type BoolList interface {
  12. DataList
  13. Append(value bool) error
  14. Get() ([]bool, error)
  15. GetValue(index int) (bool, error)
  16. Prepend(value bool) error
  17. Set(list []bool) error
  18. SetValue(index int, value bool) error
  19. }
  20. // ExternalBoolList supports binding a list of bool values from an external variable.
  21. //
  22. // Since: 2.0
  23. type ExternalBoolList interface {
  24. BoolList
  25. Reload() error
  26. }
  27. // NewBoolList returns a bindable list of bool values.
  28. //
  29. // Since: 2.0
  30. func NewBoolList() BoolList {
  31. return &boundBoolList{val: &[]bool{}}
  32. }
  33. // BindBoolList returns a bound list of bool values, based on the contents of the passed slice.
  34. // If your code changes the content of the slice this refers to you should call Reload() to inform the bindings.
  35. //
  36. // Since: 2.0
  37. func BindBoolList(v *[]bool) ExternalBoolList {
  38. if v == nil {
  39. return NewBoolList().(ExternalBoolList)
  40. }
  41. b := &boundBoolList{val: v, updateExternal: true}
  42. for i := range *v {
  43. b.appendItem(bindBoolListItem(v, i, b.updateExternal))
  44. }
  45. return b
  46. }
  47. type boundBoolList struct {
  48. listBase
  49. updateExternal bool
  50. val *[]bool
  51. }
  52. func (l *boundBoolList) Append(val bool) error {
  53. l.lock.Lock()
  54. defer l.lock.Unlock()
  55. *l.val = append(*l.val, val)
  56. return l.doReload()
  57. }
  58. func (l *boundBoolList) Get() ([]bool, error) {
  59. l.lock.RLock()
  60. defer l.lock.RUnlock()
  61. return *l.val, nil
  62. }
  63. func (l *boundBoolList) GetValue(i int) (bool, error) {
  64. l.lock.RLock()
  65. defer l.lock.RUnlock()
  66. if i < 0 || i >= l.Length() {
  67. return false, errOutOfBounds
  68. }
  69. return (*l.val)[i], nil
  70. }
  71. func (l *boundBoolList) Prepend(val bool) error {
  72. l.lock.Lock()
  73. defer l.lock.Unlock()
  74. *l.val = append([]bool{val}, *l.val...)
  75. return l.doReload()
  76. }
  77. func (l *boundBoolList) Reload() error {
  78. l.lock.Lock()
  79. defer l.lock.Unlock()
  80. return l.doReload()
  81. }
  82. func (l *boundBoolList) Set(v []bool) error {
  83. l.lock.Lock()
  84. defer l.lock.Unlock()
  85. *l.val = v
  86. return l.doReload()
  87. }
  88. func (l *boundBoolList) doReload() (retErr error) {
  89. oldLen := len(l.items)
  90. newLen := len(*l.val)
  91. if oldLen > newLen {
  92. for i := oldLen - 1; i >= newLen; i-- {
  93. l.deleteItem(i)
  94. }
  95. l.trigger()
  96. } else if oldLen < newLen {
  97. for i := oldLen; i < newLen; i++ {
  98. l.appendItem(bindBoolListItem(l.val, i, l.updateExternal))
  99. }
  100. l.trigger()
  101. }
  102. for i, item := range l.items {
  103. if i > oldLen || i > newLen {
  104. break
  105. }
  106. var err error
  107. if l.updateExternal {
  108. item.(*boundExternalBoolListItem).lock.Lock()
  109. err = item.(*boundExternalBoolListItem).setIfChanged((*l.val)[i])
  110. item.(*boundExternalBoolListItem).lock.Unlock()
  111. } else {
  112. item.(*boundBoolListItem).lock.Lock()
  113. err = item.(*boundBoolListItem).doSet((*l.val)[i])
  114. item.(*boundBoolListItem).lock.Unlock()
  115. }
  116. if err != nil {
  117. retErr = err
  118. }
  119. }
  120. return
  121. }
  122. func (l *boundBoolList) SetValue(i int, v bool) error {
  123. l.lock.RLock()
  124. len := l.Length()
  125. l.lock.RUnlock()
  126. if i < 0 || i >= len {
  127. return errOutOfBounds
  128. }
  129. l.lock.Lock()
  130. (*l.val)[i] = v
  131. l.lock.Unlock()
  132. item, err := l.GetItem(i)
  133. if err != nil {
  134. return err
  135. }
  136. return item.(Bool).Set(v)
  137. }
  138. func bindBoolListItem(v *[]bool, i int, external bool) Bool {
  139. if external {
  140. ret := &boundExternalBoolListItem{old: (*v)[i]}
  141. ret.val = v
  142. ret.index = i
  143. return ret
  144. }
  145. return &boundBoolListItem{val: v, index: i}
  146. }
  147. type boundBoolListItem struct {
  148. base
  149. val *[]bool
  150. index int
  151. }
  152. func (b *boundBoolListItem) Get() (bool, error) {
  153. b.lock.Lock()
  154. defer b.lock.Unlock()
  155. if b.index < 0 || b.index >= len(*b.val) {
  156. return false, errOutOfBounds
  157. }
  158. return (*b.val)[b.index], nil
  159. }
  160. func (b *boundBoolListItem) Set(val bool) error {
  161. b.lock.Lock()
  162. defer b.lock.Unlock()
  163. return b.doSet(val)
  164. }
  165. func (b *boundBoolListItem) doSet(val bool) error {
  166. (*b.val)[b.index] = val
  167. b.trigger()
  168. return nil
  169. }
  170. type boundExternalBoolListItem struct {
  171. boundBoolListItem
  172. old bool
  173. }
  174. func (b *boundExternalBoolListItem) setIfChanged(val bool) error {
  175. if val == b.old {
  176. return nil
  177. }
  178. (*b.val)[b.index] = val
  179. b.old = val
  180. b.trigger()
  181. return nil
  182. }
  183. // BytesList supports binding a list of []byte values.
  184. //
  185. // Since: 2.2
  186. type BytesList interface {
  187. DataList
  188. Append(value []byte) error
  189. Get() ([][]byte, error)
  190. GetValue(index int) ([]byte, error)
  191. Prepend(value []byte) error
  192. Set(list [][]byte) error
  193. SetValue(index int, value []byte) error
  194. }
  195. // ExternalBytesList supports binding a list of []byte values from an external variable.
  196. //
  197. // Since: 2.2
  198. type ExternalBytesList interface {
  199. BytesList
  200. Reload() error
  201. }
  202. // NewBytesList returns a bindable list of []byte values.
  203. //
  204. // Since: 2.2
  205. func NewBytesList() BytesList {
  206. return &boundBytesList{val: &[][]byte{}}
  207. }
  208. // BindBytesList returns a bound list of []byte values, based on the contents of the passed slice.
  209. // If your code changes the content of the slice this refers to you should call Reload() to inform the bindings.
  210. //
  211. // Since: 2.2
  212. func BindBytesList(v *[][]byte) ExternalBytesList {
  213. if v == nil {
  214. return NewBytesList().(ExternalBytesList)
  215. }
  216. b := &boundBytesList{val: v, updateExternal: true}
  217. for i := range *v {
  218. b.appendItem(bindBytesListItem(v, i, b.updateExternal))
  219. }
  220. return b
  221. }
  222. type boundBytesList struct {
  223. listBase
  224. updateExternal bool
  225. val *[][]byte
  226. }
  227. func (l *boundBytesList) Append(val []byte) error {
  228. l.lock.Lock()
  229. defer l.lock.Unlock()
  230. *l.val = append(*l.val, val)
  231. return l.doReload()
  232. }
  233. func (l *boundBytesList) Get() ([][]byte, error) {
  234. l.lock.RLock()
  235. defer l.lock.RUnlock()
  236. return *l.val, nil
  237. }
  238. func (l *boundBytesList) GetValue(i int) ([]byte, error) {
  239. l.lock.RLock()
  240. defer l.lock.RUnlock()
  241. if i < 0 || i >= l.Length() {
  242. return nil, errOutOfBounds
  243. }
  244. return (*l.val)[i], nil
  245. }
  246. func (l *boundBytesList) Prepend(val []byte) error {
  247. l.lock.Lock()
  248. defer l.lock.Unlock()
  249. *l.val = append([][]byte{val}, *l.val...)
  250. return l.doReload()
  251. }
  252. func (l *boundBytesList) Reload() error {
  253. l.lock.Lock()
  254. defer l.lock.Unlock()
  255. return l.doReload()
  256. }
  257. func (l *boundBytesList) Set(v [][]byte) error {
  258. l.lock.Lock()
  259. defer l.lock.Unlock()
  260. *l.val = v
  261. return l.doReload()
  262. }
  263. func (l *boundBytesList) doReload() (retErr error) {
  264. oldLen := len(l.items)
  265. newLen := len(*l.val)
  266. if oldLen > newLen {
  267. for i := oldLen - 1; i >= newLen; i-- {
  268. l.deleteItem(i)
  269. }
  270. l.trigger()
  271. } else if oldLen < newLen {
  272. for i := oldLen; i < newLen; i++ {
  273. l.appendItem(bindBytesListItem(l.val, i, l.updateExternal))
  274. }
  275. l.trigger()
  276. }
  277. for i, item := range l.items {
  278. if i > oldLen || i > newLen {
  279. break
  280. }
  281. var err error
  282. if l.updateExternal {
  283. item.(*boundExternalBytesListItem).lock.Lock()
  284. err = item.(*boundExternalBytesListItem).setIfChanged((*l.val)[i])
  285. item.(*boundExternalBytesListItem).lock.Unlock()
  286. } else {
  287. item.(*boundBytesListItem).lock.Lock()
  288. err = item.(*boundBytesListItem).doSet((*l.val)[i])
  289. item.(*boundBytesListItem).lock.Unlock()
  290. }
  291. if err != nil {
  292. retErr = err
  293. }
  294. }
  295. return
  296. }
  297. func (l *boundBytesList) SetValue(i int, v []byte) error {
  298. l.lock.RLock()
  299. len := l.Length()
  300. l.lock.RUnlock()
  301. if i < 0 || i >= len {
  302. return errOutOfBounds
  303. }
  304. l.lock.Lock()
  305. (*l.val)[i] = v
  306. l.lock.Unlock()
  307. item, err := l.GetItem(i)
  308. if err != nil {
  309. return err
  310. }
  311. return item.(Bytes).Set(v)
  312. }
  313. func bindBytesListItem(v *[][]byte, i int, external bool) Bytes {
  314. if external {
  315. ret := &boundExternalBytesListItem{old: (*v)[i]}
  316. ret.val = v
  317. ret.index = i
  318. return ret
  319. }
  320. return &boundBytesListItem{val: v, index: i}
  321. }
  322. type boundBytesListItem struct {
  323. base
  324. val *[][]byte
  325. index int
  326. }
  327. func (b *boundBytesListItem) Get() ([]byte, error) {
  328. b.lock.Lock()
  329. defer b.lock.Unlock()
  330. if b.index < 0 || b.index >= len(*b.val) {
  331. return nil, errOutOfBounds
  332. }
  333. return (*b.val)[b.index], nil
  334. }
  335. func (b *boundBytesListItem) Set(val []byte) error {
  336. b.lock.Lock()
  337. defer b.lock.Unlock()
  338. return b.doSet(val)
  339. }
  340. func (b *boundBytesListItem) doSet(val []byte) error {
  341. (*b.val)[b.index] = val
  342. b.trigger()
  343. return nil
  344. }
  345. type boundExternalBytesListItem struct {
  346. boundBytesListItem
  347. old []byte
  348. }
  349. func (b *boundExternalBytesListItem) setIfChanged(val []byte) error {
  350. if bytes.Equal(val, b.old) {
  351. return nil
  352. }
  353. (*b.val)[b.index] = val
  354. b.old = val
  355. b.trigger()
  356. return nil
  357. }
  358. // FloatList supports binding a list of float64 values.
  359. //
  360. // Since: 2.0
  361. type FloatList interface {
  362. DataList
  363. Append(value float64) error
  364. Get() ([]float64, error)
  365. GetValue(index int) (float64, error)
  366. Prepend(value float64) error
  367. Set(list []float64) error
  368. SetValue(index int, value float64) error
  369. }
  370. // ExternalFloatList supports binding a list of float64 values from an external variable.
  371. //
  372. // Since: 2.0
  373. type ExternalFloatList interface {
  374. FloatList
  375. Reload() error
  376. }
  377. // NewFloatList returns a bindable list of float64 values.
  378. //
  379. // Since: 2.0
  380. func NewFloatList() FloatList {
  381. return &boundFloatList{val: &[]float64{}}
  382. }
  383. // BindFloatList returns a bound list of float64 values, based on the contents of the passed slice.
  384. // If your code changes the content of the slice this refers to you should call Reload() to inform the bindings.
  385. //
  386. // Since: 2.0
  387. func BindFloatList(v *[]float64) ExternalFloatList {
  388. if v == nil {
  389. return NewFloatList().(ExternalFloatList)
  390. }
  391. b := &boundFloatList{val: v, updateExternal: true}
  392. for i := range *v {
  393. b.appendItem(bindFloatListItem(v, i, b.updateExternal))
  394. }
  395. return b
  396. }
  397. type boundFloatList struct {
  398. listBase
  399. updateExternal bool
  400. val *[]float64
  401. }
  402. func (l *boundFloatList) Append(val float64) error {
  403. l.lock.Lock()
  404. defer l.lock.Unlock()
  405. *l.val = append(*l.val, val)
  406. return l.doReload()
  407. }
  408. func (l *boundFloatList) Get() ([]float64, error) {
  409. l.lock.RLock()
  410. defer l.lock.RUnlock()
  411. return *l.val, nil
  412. }
  413. func (l *boundFloatList) GetValue(i int) (float64, error) {
  414. l.lock.RLock()
  415. defer l.lock.RUnlock()
  416. if i < 0 || i >= l.Length() {
  417. return 0.0, errOutOfBounds
  418. }
  419. return (*l.val)[i], nil
  420. }
  421. func (l *boundFloatList) Prepend(val float64) error {
  422. l.lock.Lock()
  423. defer l.lock.Unlock()
  424. *l.val = append([]float64{val}, *l.val...)
  425. return l.doReload()
  426. }
  427. func (l *boundFloatList) Reload() error {
  428. l.lock.Lock()
  429. defer l.lock.Unlock()
  430. return l.doReload()
  431. }
  432. func (l *boundFloatList) Set(v []float64) error {
  433. l.lock.Lock()
  434. defer l.lock.Unlock()
  435. *l.val = v
  436. return l.doReload()
  437. }
  438. func (l *boundFloatList) doReload() (retErr error) {
  439. oldLen := len(l.items)
  440. newLen := len(*l.val)
  441. if oldLen > newLen {
  442. for i := oldLen - 1; i >= newLen; i-- {
  443. l.deleteItem(i)
  444. }
  445. l.trigger()
  446. } else if oldLen < newLen {
  447. for i := oldLen; i < newLen; i++ {
  448. l.appendItem(bindFloatListItem(l.val, i, l.updateExternal))
  449. }
  450. l.trigger()
  451. }
  452. for i, item := range l.items {
  453. if i > oldLen || i > newLen {
  454. break
  455. }
  456. var err error
  457. if l.updateExternal {
  458. item.(*boundExternalFloatListItem).lock.Lock()
  459. err = item.(*boundExternalFloatListItem).setIfChanged((*l.val)[i])
  460. item.(*boundExternalFloatListItem).lock.Unlock()
  461. } else {
  462. item.(*boundFloatListItem).lock.Lock()
  463. err = item.(*boundFloatListItem).doSet((*l.val)[i])
  464. item.(*boundFloatListItem).lock.Unlock()
  465. }
  466. if err != nil {
  467. retErr = err
  468. }
  469. }
  470. return
  471. }
  472. func (l *boundFloatList) SetValue(i int, v float64) error {
  473. l.lock.RLock()
  474. len := l.Length()
  475. l.lock.RUnlock()
  476. if i < 0 || i >= len {
  477. return errOutOfBounds
  478. }
  479. l.lock.Lock()
  480. (*l.val)[i] = v
  481. l.lock.Unlock()
  482. item, err := l.GetItem(i)
  483. if err != nil {
  484. return err
  485. }
  486. return item.(Float).Set(v)
  487. }
  488. func bindFloatListItem(v *[]float64, i int, external bool) Float {
  489. if external {
  490. ret := &boundExternalFloatListItem{old: (*v)[i]}
  491. ret.val = v
  492. ret.index = i
  493. return ret
  494. }
  495. return &boundFloatListItem{val: v, index: i}
  496. }
  497. type boundFloatListItem struct {
  498. base
  499. val *[]float64
  500. index int
  501. }
  502. func (b *boundFloatListItem) Get() (float64, error) {
  503. b.lock.Lock()
  504. defer b.lock.Unlock()
  505. if b.index < 0 || b.index >= len(*b.val) {
  506. return 0.0, errOutOfBounds
  507. }
  508. return (*b.val)[b.index], nil
  509. }
  510. func (b *boundFloatListItem) Set(val float64) error {
  511. b.lock.Lock()
  512. defer b.lock.Unlock()
  513. return b.doSet(val)
  514. }
  515. func (b *boundFloatListItem) doSet(val float64) error {
  516. (*b.val)[b.index] = val
  517. b.trigger()
  518. return nil
  519. }
  520. type boundExternalFloatListItem struct {
  521. boundFloatListItem
  522. old float64
  523. }
  524. func (b *boundExternalFloatListItem) setIfChanged(val float64) error {
  525. if val == b.old {
  526. return nil
  527. }
  528. (*b.val)[b.index] = val
  529. b.old = val
  530. b.trigger()
  531. return nil
  532. }
  533. // IntList supports binding a list of int values.
  534. //
  535. // Since: 2.0
  536. type IntList interface {
  537. DataList
  538. Append(value int) error
  539. Get() ([]int, error)
  540. GetValue(index int) (int, error)
  541. Prepend(value int) error
  542. Set(list []int) error
  543. SetValue(index int, value int) error
  544. }
  545. // ExternalIntList supports binding a list of int values from an external variable.
  546. //
  547. // Since: 2.0
  548. type ExternalIntList interface {
  549. IntList
  550. Reload() error
  551. }
  552. // NewIntList returns a bindable list of int values.
  553. //
  554. // Since: 2.0
  555. func NewIntList() IntList {
  556. return &boundIntList{val: &[]int{}}
  557. }
  558. // BindIntList returns a bound list of int values, based on the contents of the passed slice.
  559. // If your code changes the content of the slice this refers to you should call Reload() to inform the bindings.
  560. //
  561. // Since: 2.0
  562. func BindIntList(v *[]int) ExternalIntList {
  563. if v == nil {
  564. return NewIntList().(ExternalIntList)
  565. }
  566. b := &boundIntList{val: v, updateExternal: true}
  567. for i := range *v {
  568. b.appendItem(bindIntListItem(v, i, b.updateExternal))
  569. }
  570. return b
  571. }
  572. type boundIntList struct {
  573. listBase
  574. updateExternal bool
  575. val *[]int
  576. }
  577. func (l *boundIntList) Append(val int) error {
  578. l.lock.Lock()
  579. defer l.lock.Unlock()
  580. *l.val = append(*l.val, val)
  581. return l.doReload()
  582. }
  583. func (l *boundIntList) Get() ([]int, error) {
  584. l.lock.RLock()
  585. defer l.lock.RUnlock()
  586. return *l.val, nil
  587. }
  588. func (l *boundIntList) GetValue(i int) (int, error) {
  589. l.lock.RLock()
  590. defer l.lock.RUnlock()
  591. if i < 0 || i >= l.Length() {
  592. return 0, errOutOfBounds
  593. }
  594. return (*l.val)[i], nil
  595. }
  596. func (l *boundIntList) Prepend(val int) error {
  597. l.lock.Lock()
  598. defer l.lock.Unlock()
  599. *l.val = append([]int{val}, *l.val...)
  600. return l.doReload()
  601. }
  602. func (l *boundIntList) Reload() error {
  603. l.lock.Lock()
  604. defer l.lock.Unlock()
  605. return l.doReload()
  606. }
  607. func (l *boundIntList) Set(v []int) error {
  608. l.lock.Lock()
  609. defer l.lock.Unlock()
  610. *l.val = v
  611. return l.doReload()
  612. }
  613. func (l *boundIntList) doReload() (retErr error) {
  614. oldLen := len(l.items)
  615. newLen := len(*l.val)
  616. if oldLen > newLen {
  617. for i := oldLen - 1; i >= newLen; i-- {
  618. l.deleteItem(i)
  619. }
  620. l.trigger()
  621. } else if oldLen < newLen {
  622. for i := oldLen; i < newLen; i++ {
  623. l.appendItem(bindIntListItem(l.val, i, l.updateExternal))
  624. }
  625. l.trigger()
  626. }
  627. for i, item := range l.items {
  628. if i > oldLen || i > newLen {
  629. break
  630. }
  631. var err error
  632. if l.updateExternal {
  633. item.(*boundExternalIntListItem).lock.Lock()
  634. err = item.(*boundExternalIntListItem).setIfChanged((*l.val)[i])
  635. item.(*boundExternalIntListItem).lock.Unlock()
  636. } else {
  637. item.(*boundIntListItem).lock.Lock()
  638. err = item.(*boundIntListItem).doSet((*l.val)[i])
  639. item.(*boundIntListItem).lock.Unlock()
  640. }
  641. if err != nil {
  642. retErr = err
  643. }
  644. }
  645. return
  646. }
  647. func (l *boundIntList) SetValue(i int, v int) error {
  648. l.lock.RLock()
  649. len := l.Length()
  650. l.lock.RUnlock()
  651. if i < 0 || i >= len {
  652. return errOutOfBounds
  653. }
  654. l.lock.Lock()
  655. (*l.val)[i] = v
  656. l.lock.Unlock()
  657. item, err := l.GetItem(i)
  658. if err != nil {
  659. return err
  660. }
  661. return item.(Int).Set(v)
  662. }
  663. func bindIntListItem(v *[]int, i int, external bool) Int {
  664. if external {
  665. ret := &boundExternalIntListItem{old: (*v)[i]}
  666. ret.val = v
  667. ret.index = i
  668. return ret
  669. }
  670. return &boundIntListItem{val: v, index: i}
  671. }
  672. type boundIntListItem struct {
  673. base
  674. val *[]int
  675. index int
  676. }
  677. func (b *boundIntListItem) Get() (int, error) {
  678. b.lock.Lock()
  679. defer b.lock.Unlock()
  680. if b.index < 0 || b.index >= len(*b.val) {
  681. return 0, errOutOfBounds
  682. }
  683. return (*b.val)[b.index], nil
  684. }
  685. func (b *boundIntListItem) Set(val int) error {
  686. b.lock.Lock()
  687. defer b.lock.Unlock()
  688. return b.doSet(val)
  689. }
  690. func (b *boundIntListItem) doSet(val int) error {
  691. (*b.val)[b.index] = val
  692. b.trigger()
  693. return nil
  694. }
  695. type boundExternalIntListItem struct {
  696. boundIntListItem
  697. old int
  698. }
  699. func (b *boundExternalIntListItem) setIfChanged(val int) error {
  700. if val == b.old {
  701. return nil
  702. }
  703. (*b.val)[b.index] = val
  704. b.old = val
  705. b.trigger()
  706. return nil
  707. }
  708. // RuneList supports binding a list of rune values.
  709. //
  710. // Since: 2.0
  711. type RuneList interface {
  712. DataList
  713. Append(value rune) error
  714. Get() ([]rune, error)
  715. GetValue(index int) (rune, error)
  716. Prepend(value rune) error
  717. Set(list []rune) error
  718. SetValue(index int, value rune) error
  719. }
  720. // ExternalRuneList supports binding a list of rune values from an external variable.
  721. //
  722. // Since: 2.0
  723. type ExternalRuneList interface {
  724. RuneList
  725. Reload() error
  726. }
  727. // NewRuneList returns a bindable list of rune values.
  728. //
  729. // Since: 2.0
  730. func NewRuneList() RuneList {
  731. return &boundRuneList{val: &[]rune{}}
  732. }
  733. // BindRuneList returns a bound list of rune values, based on the contents of the passed slice.
  734. // If your code changes the content of the slice this refers to you should call Reload() to inform the bindings.
  735. //
  736. // Since: 2.0
  737. func BindRuneList(v *[]rune) ExternalRuneList {
  738. if v == nil {
  739. return NewRuneList().(ExternalRuneList)
  740. }
  741. b := &boundRuneList{val: v, updateExternal: true}
  742. for i := range *v {
  743. b.appendItem(bindRuneListItem(v, i, b.updateExternal))
  744. }
  745. return b
  746. }
  747. type boundRuneList struct {
  748. listBase
  749. updateExternal bool
  750. val *[]rune
  751. }
  752. func (l *boundRuneList) Append(val rune) error {
  753. l.lock.Lock()
  754. defer l.lock.Unlock()
  755. *l.val = append(*l.val, val)
  756. return l.doReload()
  757. }
  758. func (l *boundRuneList) Get() ([]rune, error) {
  759. l.lock.RLock()
  760. defer l.lock.RUnlock()
  761. return *l.val, nil
  762. }
  763. func (l *boundRuneList) GetValue(i int) (rune, error) {
  764. l.lock.RLock()
  765. defer l.lock.RUnlock()
  766. if i < 0 || i >= l.Length() {
  767. return rune(0), errOutOfBounds
  768. }
  769. return (*l.val)[i], nil
  770. }
  771. func (l *boundRuneList) Prepend(val rune) error {
  772. l.lock.Lock()
  773. defer l.lock.Unlock()
  774. *l.val = append([]rune{val}, *l.val...)
  775. return l.doReload()
  776. }
  777. func (l *boundRuneList) Reload() error {
  778. l.lock.Lock()
  779. defer l.lock.Unlock()
  780. return l.doReload()
  781. }
  782. func (l *boundRuneList) Set(v []rune) error {
  783. l.lock.Lock()
  784. defer l.lock.Unlock()
  785. *l.val = v
  786. return l.doReload()
  787. }
  788. func (l *boundRuneList) doReload() (retErr error) {
  789. oldLen := len(l.items)
  790. newLen := len(*l.val)
  791. if oldLen > newLen {
  792. for i := oldLen - 1; i >= newLen; i-- {
  793. l.deleteItem(i)
  794. }
  795. l.trigger()
  796. } else if oldLen < newLen {
  797. for i := oldLen; i < newLen; i++ {
  798. l.appendItem(bindRuneListItem(l.val, i, l.updateExternal))
  799. }
  800. l.trigger()
  801. }
  802. for i, item := range l.items {
  803. if i > oldLen || i > newLen {
  804. break
  805. }
  806. var err error
  807. if l.updateExternal {
  808. item.(*boundExternalRuneListItem).lock.Lock()
  809. err = item.(*boundExternalRuneListItem).setIfChanged((*l.val)[i])
  810. item.(*boundExternalRuneListItem).lock.Unlock()
  811. } else {
  812. item.(*boundRuneListItem).lock.Lock()
  813. err = item.(*boundRuneListItem).doSet((*l.val)[i])
  814. item.(*boundRuneListItem).lock.Unlock()
  815. }
  816. if err != nil {
  817. retErr = err
  818. }
  819. }
  820. return
  821. }
  822. func (l *boundRuneList) SetValue(i int, v rune) error {
  823. l.lock.RLock()
  824. len := l.Length()
  825. l.lock.RUnlock()
  826. if i < 0 || i >= len {
  827. return errOutOfBounds
  828. }
  829. l.lock.Lock()
  830. (*l.val)[i] = v
  831. l.lock.Unlock()
  832. item, err := l.GetItem(i)
  833. if err != nil {
  834. return err
  835. }
  836. return item.(Rune).Set(v)
  837. }
  838. func bindRuneListItem(v *[]rune, i int, external bool) Rune {
  839. if external {
  840. ret := &boundExternalRuneListItem{old: (*v)[i]}
  841. ret.val = v
  842. ret.index = i
  843. return ret
  844. }
  845. return &boundRuneListItem{val: v, index: i}
  846. }
  847. type boundRuneListItem struct {
  848. base
  849. val *[]rune
  850. index int
  851. }
  852. func (b *boundRuneListItem) Get() (rune, error) {
  853. b.lock.Lock()
  854. defer b.lock.Unlock()
  855. if b.index < 0 || b.index >= len(*b.val) {
  856. return rune(0), errOutOfBounds
  857. }
  858. return (*b.val)[b.index], nil
  859. }
  860. func (b *boundRuneListItem) Set(val rune) error {
  861. b.lock.Lock()
  862. defer b.lock.Unlock()
  863. return b.doSet(val)
  864. }
  865. func (b *boundRuneListItem) doSet(val rune) error {
  866. (*b.val)[b.index] = val
  867. b.trigger()
  868. return nil
  869. }
  870. type boundExternalRuneListItem struct {
  871. boundRuneListItem
  872. old rune
  873. }
  874. func (b *boundExternalRuneListItem) setIfChanged(val rune) error {
  875. if val == b.old {
  876. return nil
  877. }
  878. (*b.val)[b.index] = val
  879. b.old = val
  880. b.trigger()
  881. return nil
  882. }
  883. // StringList supports binding a list of string values.
  884. //
  885. // Since: 2.0
  886. type StringList interface {
  887. DataList
  888. Append(value string) error
  889. Get() ([]string, error)
  890. GetValue(index int) (string, error)
  891. Prepend(value string) error
  892. Set(list []string) error
  893. SetValue(index int, value string) error
  894. }
  895. // ExternalStringList supports binding a list of string values from an external variable.
  896. //
  897. // Since: 2.0
  898. type ExternalStringList interface {
  899. StringList
  900. Reload() error
  901. }
  902. // NewStringList returns a bindable list of string values.
  903. //
  904. // Since: 2.0
  905. func NewStringList() StringList {
  906. return &boundStringList{val: &[]string{}}
  907. }
  908. // BindStringList returns a bound list of string values, based on the contents of the passed slice.
  909. // If your code changes the content of the slice this refers to you should call Reload() to inform the bindings.
  910. //
  911. // Since: 2.0
  912. func BindStringList(v *[]string) ExternalStringList {
  913. if v == nil {
  914. return NewStringList().(ExternalStringList)
  915. }
  916. b := &boundStringList{val: v, updateExternal: true}
  917. for i := range *v {
  918. b.appendItem(bindStringListItem(v, i, b.updateExternal))
  919. }
  920. return b
  921. }
  922. type boundStringList struct {
  923. listBase
  924. updateExternal bool
  925. val *[]string
  926. }
  927. func (l *boundStringList) Append(val string) error {
  928. l.lock.Lock()
  929. defer l.lock.Unlock()
  930. *l.val = append(*l.val, val)
  931. return l.doReload()
  932. }
  933. func (l *boundStringList) Get() ([]string, error) {
  934. l.lock.RLock()
  935. defer l.lock.RUnlock()
  936. return *l.val, nil
  937. }
  938. func (l *boundStringList) GetValue(i int) (string, error) {
  939. l.lock.RLock()
  940. defer l.lock.RUnlock()
  941. if i < 0 || i >= l.Length() {
  942. return "", errOutOfBounds
  943. }
  944. return (*l.val)[i], nil
  945. }
  946. func (l *boundStringList) Prepend(val string) error {
  947. l.lock.Lock()
  948. defer l.lock.Unlock()
  949. *l.val = append([]string{val}, *l.val...)
  950. return l.doReload()
  951. }
  952. func (l *boundStringList) Reload() error {
  953. l.lock.Lock()
  954. defer l.lock.Unlock()
  955. return l.doReload()
  956. }
  957. func (l *boundStringList) Set(v []string) error {
  958. l.lock.Lock()
  959. defer l.lock.Unlock()
  960. *l.val = v
  961. return l.doReload()
  962. }
  963. func (l *boundStringList) doReload() (retErr error) {
  964. oldLen := len(l.items)
  965. newLen := len(*l.val)
  966. if oldLen > newLen {
  967. for i := oldLen - 1; i >= newLen; i-- {
  968. l.deleteItem(i)
  969. }
  970. l.trigger()
  971. } else if oldLen < newLen {
  972. for i := oldLen; i < newLen; i++ {
  973. l.appendItem(bindStringListItem(l.val, i, l.updateExternal))
  974. }
  975. l.trigger()
  976. }
  977. for i, item := range l.items {
  978. if i > oldLen || i > newLen {
  979. break
  980. }
  981. var err error
  982. if l.updateExternal {
  983. item.(*boundExternalStringListItem).lock.Lock()
  984. err = item.(*boundExternalStringListItem).setIfChanged((*l.val)[i])
  985. item.(*boundExternalStringListItem).lock.Unlock()
  986. } else {
  987. item.(*boundStringListItem).lock.Lock()
  988. err = item.(*boundStringListItem).doSet((*l.val)[i])
  989. item.(*boundStringListItem).lock.Unlock()
  990. }
  991. if err != nil {
  992. retErr = err
  993. }
  994. }
  995. return
  996. }
  997. func (l *boundStringList) SetValue(i int, v string) error {
  998. l.lock.RLock()
  999. len := l.Length()
  1000. l.lock.RUnlock()
  1001. if i < 0 || i >= len {
  1002. return errOutOfBounds
  1003. }
  1004. l.lock.Lock()
  1005. (*l.val)[i] = v
  1006. l.lock.Unlock()
  1007. item, err := l.GetItem(i)
  1008. if err != nil {
  1009. return err
  1010. }
  1011. return item.(String).Set(v)
  1012. }
  1013. func bindStringListItem(v *[]string, i int, external bool) String {
  1014. if external {
  1015. ret := &boundExternalStringListItem{old: (*v)[i]}
  1016. ret.val = v
  1017. ret.index = i
  1018. return ret
  1019. }
  1020. return &boundStringListItem{val: v, index: i}
  1021. }
  1022. type boundStringListItem struct {
  1023. base
  1024. val *[]string
  1025. index int
  1026. }
  1027. func (b *boundStringListItem) Get() (string, error) {
  1028. b.lock.Lock()
  1029. defer b.lock.Unlock()
  1030. if b.index < 0 || b.index >= len(*b.val) {
  1031. return "", errOutOfBounds
  1032. }
  1033. return (*b.val)[b.index], nil
  1034. }
  1035. func (b *boundStringListItem) Set(val string) error {
  1036. b.lock.Lock()
  1037. defer b.lock.Unlock()
  1038. return b.doSet(val)
  1039. }
  1040. func (b *boundStringListItem) doSet(val string) error {
  1041. (*b.val)[b.index] = val
  1042. b.trigger()
  1043. return nil
  1044. }
  1045. type boundExternalStringListItem struct {
  1046. boundStringListItem
  1047. old string
  1048. }
  1049. func (b *boundExternalStringListItem) setIfChanged(val string) error {
  1050. if val == b.old {
  1051. return nil
  1052. }
  1053. (*b.val)[b.index] = val
  1054. b.old = val
  1055. b.trigger()
  1056. return nil
  1057. }
  1058. // UntypedList supports binding a list of interface{} values.
  1059. //
  1060. // Since: 2.1
  1061. type UntypedList interface {
  1062. DataList
  1063. Append(value interface{}) error
  1064. Get() ([]interface{}, error)
  1065. GetValue(index int) (interface{}, error)
  1066. Prepend(value interface{}) error
  1067. Set(list []interface{}) error
  1068. SetValue(index int, value interface{}) error
  1069. }
  1070. // ExternalUntypedList supports binding a list of interface{} values from an external variable.
  1071. //
  1072. // Since: 2.1
  1073. type ExternalUntypedList interface {
  1074. UntypedList
  1075. Reload() error
  1076. }
  1077. // NewUntypedList returns a bindable list of interface{} values.
  1078. //
  1079. // Since: 2.1
  1080. func NewUntypedList() UntypedList {
  1081. return &boundUntypedList{val: &[]interface{}{}}
  1082. }
  1083. // BindUntypedList returns a bound list of interface{} values, based on the contents of the passed slice.
  1084. // If your code changes the content of the slice this refers to you should call Reload() to inform the bindings.
  1085. //
  1086. // Since: 2.1
  1087. func BindUntypedList(v *[]interface{}) ExternalUntypedList {
  1088. if v == nil {
  1089. return NewUntypedList().(ExternalUntypedList)
  1090. }
  1091. b := &boundUntypedList{val: v, updateExternal: true}
  1092. for i := range *v {
  1093. b.appendItem(bindUntypedListItem(v, i, b.updateExternal))
  1094. }
  1095. return b
  1096. }
  1097. type boundUntypedList struct {
  1098. listBase
  1099. updateExternal bool
  1100. val *[]interface{}
  1101. }
  1102. func (l *boundUntypedList) Append(val interface{}) error {
  1103. l.lock.Lock()
  1104. defer l.lock.Unlock()
  1105. *l.val = append(*l.val, val)
  1106. return l.doReload()
  1107. }
  1108. func (l *boundUntypedList) Get() ([]interface{}, error) {
  1109. l.lock.RLock()
  1110. defer l.lock.RUnlock()
  1111. return *l.val, nil
  1112. }
  1113. func (l *boundUntypedList) GetValue(i int) (interface{}, error) {
  1114. l.lock.RLock()
  1115. defer l.lock.RUnlock()
  1116. if i < 0 || i >= l.Length() {
  1117. return nil, errOutOfBounds
  1118. }
  1119. return (*l.val)[i], nil
  1120. }
  1121. func (l *boundUntypedList) Prepend(val interface{}) error {
  1122. l.lock.Lock()
  1123. defer l.lock.Unlock()
  1124. *l.val = append([]interface{}{val}, *l.val...)
  1125. return l.doReload()
  1126. }
  1127. func (l *boundUntypedList) Reload() error {
  1128. l.lock.Lock()
  1129. defer l.lock.Unlock()
  1130. return l.doReload()
  1131. }
  1132. func (l *boundUntypedList) Set(v []interface{}) error {
  1133. l.lock.Lock()
  1134. defer l.lock.Unlock()
  1135. *l.val = v
  1136. return l.doReload()
  1137. }
  1138. func (l *boundUntypedList) doReload() (retErr error) {
  1139. oldLen := len(l.items)
  1140. newLen := len(*l.val)
  1141. if oldLen > newLen {
  1142. for i := oldLen - 1; i >= newLen; i-- {
  1143. l.deleteItem(i)
  1144. }
  1145. l.trigger()
  1146. } else if oldLen < newLen {
  1147. for i := oldLen; i < newLen; i++ {
  1148. l.appendItem(bindUntypedListItem(l.val, i, l.updateExternal))
  1149. }
  1150. l.trigger()
  1151. }
  1152. for i, item := range l.items {
  1153. if i > oldLen || i > newLen {
  1154. break
  1155. }
  1156. var err error
  1157. if l.updateExternal {
  1158. item.(*boundExternalUntypedListItem).lock.Lock()
  1159. err = item.(*boundExternalUntypedListItem).setIfChanged((*l.val)[i])
  1160. item.(*boundExternalUntypedListItem).lock.Unlock()
  1161. } else {
  1162. item.(*boundUntypedListItem).lock.Lock()
  1163. err = item.(*boundUntypedListItem).doSet((*l.val)[i])
  1164. item.(*boundUntypedListItem).lock.Unlock()
  1165. }
  1166. if err != nil {
  1167. retErr = err
  1168. }
  1169. }
  1170. return
  1171. }
  1172. func (l *boundUntypedList) SetValue(i int, v interface{}) error {
  1173. l.lock.RLock()
  1174. len := l.Length()
  1175. l.lock.RUnlock()
  1176. if i < 0 || i >= len {
  1177. return errOutOfBounds
  1178. }
  1179. l.lock.Lock()
  1180. (*l.val)[i] = v
  1181. l.lock.Unlock()
  1182. item, err := l.GetItem(i)
  1183. if err != nil {
  1184. return err
  1185. }
  1186. return item.(Untyped).Set(v)
  1187. }
  1188. func bindUntypedListItem(v *[]interface{}, i int, external bool) Untyped {
  1189. if external {
  1190. ret := &boundExternalUntypedListItem{old: (*v)[i]}
  1191. ret.val = v
  1192. ret.index = i
  1193. return ret
  1194. }
  1195. return &boundUntypedListItem{val: v, index: i}
  1196. }
  1197. type boundUntypedListItem struct {
  1198. base
  1199. val *[]interface{}
  1200. index int
  1201. }
  1202. func (b *boundUntypedListItem) Get() (interface{}, error) {
  1203. b.lock.Lock()
  1204. defer b.lock.Unlock()
  1205. if b.index < 0 || b.index >= len(*b.val) {
  1206. return nil, errOutOfBounds
  1207. }
  1208. return (*b.val)[b.index], nil
  1209. }
  1210. func (b *boundUntypedListItem) Set(val interface{}) error {
  1211. b.lock.Lock()
  1212. defer b.lock.Unlock()
  1213. return b.doSet(val)
  1214. }
  1215. func (b *boundUntypedListItem) doSet(val interface{}) error {
  1216. (*b.val)[b.index] = val
  1217. b.trigger()
  1218. return nil
  1219. }
  1220. type boundExternalUntypedListItem struct {
  1221. boundUntypedListItem
  1222. old interface{}
  1223. }
  1224. func (b *boundExternalUntypedListItem) setIfChanged(val interface{}) error {
  1225. if val == b.old {
  1226. return nil
  1227. }
  1228. (*b.val)[b.index] = val
  1229. b.old = val
  1230. b.trigger()
  1231. return nil
  1232. }
  1233. // URIList supports binding a list of fyne.URI values.
  1234. //
  1235. // Since: 2.1
  1236. type URIList interface {
  1237. DataList
  1238. Append(value fyne.URI) error
  1239. Get() ([]fyne.URI, error)
  1240. GetValue(index int) (fyne.URI, error)
  1241. Prepend(value fyne.URI) error
  1242. Set(list []fyne.URI) error
  1243. SetValue(index int, value fyne.URI) error
  1244. }
  1245. // ExternalURIList supports binding a list of fyne.URI values from an external variable.
  1246. //
  1247. // Since: 2.1
  1248. type ExternalURIList interface {
  1249. URIList
  1250. Reload() error
  1251. }
  1252. // NewURIList returns a bindable list of fyne.URI values.
  1253. //
  1254. // Since: 2.1
  1255. func NewURIList() URIList {
  1256. return &boundURIList{val: &[]fyne.URI{}}
  1257. }
  1258. // BindURIList returns a bound list of fyne.URI values, based on the contents of the passed slice.
  1259. // If your code changes the content of the slice this refers to you should call Reload() to inform the bindings.
  1260. //
  1261. // Since: 2.1
  1262. func BindURIList(v *[]fyne.URI) ExternalURIList {
  1263. if v == nil {
  1264. return NewURIList().(ExternalURIList)
  1265. }
  1266. b := &boundURIList{val: v, updateExternal: true}
  1267. for i := range *v {
  1268. b.appendItem(bindURIListItem(v, i, b.updateExternal))
  1269. }
  1270. return b
  1271. }
  1272. type boundURIList struct {
  1273. listBase
  1274. updateExternal bool
  1275. val *[]fyne.URI
  1276. }
  1277. func (l *boundURIList) Append(val fyne.URI) error {
  1278. l.lock.Lock()
  1279. defer l.lock.Unlock()
  1280. *l.val = append(*l.val, val)
  1281. return l.doReload()
  1282. }
  1283. func (l *boundURIList) Get() ([]fyne.URI, error) {
  1284. l.lock.RLock()
  1285. defer l.lock.RUnlock()
  1286. return *l.val, nil
  1287. }
  1288. func (l *boundURIList) GetValue(i int) (fyne.URI, error) {
  1289. l.lock.RLock()
  1290. defer l.lock.RUnlock()
  1291. if i < 0 || i >= l.Length() {
  1292. return fyne.URI(nil), errOutOfBounds
  1293. }
  1294. return (*l.val)[i], nil
  1295. }
  1296. func (l *boundURIList) Prepend(val fyne.URI) error {
  1297. l.lock.Lock()
  1298. defer l.lock.Unlock()
  1299. *l.val = append([]fyne.URI{val}, *l.val...)
  1300. return l.doReload()
  1301. }
  1302. func (l *boundURIList) Reload() error {
  1303. l.lock.Lock()
  1304. defer l.lock.Unlock()
  1305. return l.doReload()
  1306. }
  1307. func (l *boundURIList) Set(v []fyne.URI) error {
  1308. l.lock.Lock()
  1309. defer l.lock.Unlock()
  1310. *l.val = v
  1311. return l.doReload()
  1312. }
  1313. func (l *boundURIList) doReload() (retErr error) {
  1314. oldLen := len(l.items)
  1315. newLen := len(*l.val)
  1316. if oldLen > newLen {
  1317. for i := oldLen - 1; i >= newLen; i-- {
  1318. l.deleteItem(i)
  1319. }
  1320. l.trigger()
  1321. } else if oldLen < newLen {
  1322. for i := oldLen; i < newLen; i++ {
  1323. l.appendItem(bindURIListItem(l.val, i, l.updateExternal))
  1324. }
  1325. l.trigger()
  1326. }
  1327. for i, item := range l.items {
  1328. if i > oldLen || i > newLen {
  1329. break
  1330. }
  1331. var err error
  1332. if l.updateExternal {
  1333. item.(*boundExternalURIListItem).lock.Lock()
  1334. err = item.(*boundExternalURIListItem).setIfChanged((*l.val)[i])
  1335. item.(*boundExternalURIListItem).lock.Unlock()
  1336. } else {
  1337. item.(*boundURIListItem).lock.Lock()
  1338. err = item.(*boundURIListItem).doSet((*l.val)[i])
  1339. item.(*boundURIListItem).lock.Unlock()
  1340. }
  1341. if err != nil {
  1342. retErr = err
  1343. }
  1344. }
  1345. return
  1346. }
  1347. func (l *boundURIList) SetValue(i int, v fyne.URI) error {
  1348. l.lock.RLock()
  1349. len := l.Length()
  1350. l.lock.RUnlock()
  1351. if i < 0 || i >= len {
  1352. return errOutOfBounds
  1353. }
  1354. l.lock.Lock()
  1355. (*l.val)[i] = v
  1356. l.lock.Unlock()
  1357. item, err := l.GetItem(i)
  1358. if err != nil {
  1359. return err
  1360. }
  1361. return item.(URI).Set(v)
  1362. }
  1363. func bindURIListItem(v *[]fyne.URI, i int, external bool) URI {
  1364. if external {
  1365. ret := &boundExternalURIListItem{old: (*v)[i]}
  1366. ret.val = v
  1367. ret.index = i
  1368. return ret
  1369. }
  1370. return &boundURIListItem{val: v, index: i}
  1371. }
  1372. type boundURIListItem struct {
  1373. base
  1374. val *[]fyne.URI
  1375. index int
  1376. }
  1377. func (b *boundURIListItem) Get() (fyne.URI, error) {
  1378. b.lock.Lock()
  1379. defer b.lock.Unlock()
  1380. if b.index < 0 || b.index >= len(*b.val) {
  1381. return fyne.URI(nil), errOutOfBounds
  1382. }
  1383. return (*b.val)[b.index], nil
  1384. }
  1385. func (b *boundURIListItem) Set(val fyne.URI) error {
  1386. b.lock.Lock()
  1387. defer b.lock.Unlock()
  1388. return b.doSet(val)
  1389. }
  1390. func (b *boundURIListItem) doSet(val fyne.URI) error {
  1391. (*b.val)[b.index] = val
  1392. b.trigger()
  1393. return nil
  1394. }
  1395. type boundExternalURIListItem struct {
  1396. boundURIListItem
  1397. old fyne.URI
  1398. }
  1399. func (b *boundExternalURIListItem) setIfChanged(val fyne.URI) error {
  1400. if compareURI(val, b.old) {
  1401. return nil
  1402. }
  1403. (*b.val)[b.index] = val
  1404. b.old = val
  1405. b.trigger()
  1406. return nil
  1407. }