convert.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. // auto-generated
  2. // **** THIS FILE IS AUTO-GENERATED, PLEASE DO NOT EDIT IT **** //
  3. package binding
  4. import (
  5. "fmt"
  6. "fyne.io/fyne/v2"
  7. )
  8. type stringFromBool struct {
  9. base
  10. format string
  11. from Bool
  12. }
  13. // BoolToString creates a binding that connects a Bool data item to a String.
  14. // Changes to the Bool will be pushed to the String and setting the string will parse and set the
  15. // Bool if the parse was successful.
  16. //
  17. // Since: 2.0
  18. func BoolToString(v Bool) String {
  19. str := &stringFromBool{from: v}
  20. v.AddListener(str)
  21. return str
  22. }
  23. // BoolToStringWithFormat creates a binding that connects a Bool data item to a String and is
  24. // presented using the specified format. Changes to the Bool will be pushed to the String and setting
  25. // the string will parse and set the Bool if the string matches the format and its parse was successful.
  26. //
  27. // Since: 2.0
  28. func BoolToStringWithFormat(v Bool, format string) String {
  29. if format == "%t" { // Same as not using custom formatting.
  30. return BoolToString(v)
  31. }
  32. str := &stringFromBool{from: v, format: format}
  33. v.AddListener(str)
  34. return str
  35. }
  36. func (s *stringFromBool) Get() (string, error) {
  37. val, err := s.from.Get()
  38. if err != nil {
  39. return "", err
  40. }
  41. if s.format != "" {
  42. return fmt.Sprintf(s.format, val), nil
  43. }
  44. return formatBool(val), nil
  45. }
  46. func (s *stringFromBool) Set(str string) error {
  47. var val bool
  48. if s.format != "" {
  49. safe := stripFormatPrecision(s.format)
  50. n, err := fmt.Sscanf(str, safe+" ", &val) // " " denotes match to end of string
  51. if err != nil {
  52. return err
  53. }
  54. if n != 1 {
  55. return errParseFailed
  56. }
  57. } else {
  58. new, err := parseBool(str)
  59. if err != nil {
  60. return err
  61. }
  62. val = new
  63. }
  64. old, err := s.from.Get()
  65. if err != nil {
  66. return err
  67. }
  68. if val == old {
  69. return nil
  70. }
  71. if err = s.from.Set(val); err != nil {
  72. return err
  73. }
  74. s.DataChanged()
  75. return nil
  76. }
  77. func (s *stringFromBool) DataChanged() {
  78. s.lock.RLock()
  79. defer s.lock.RUnlock()
  80. s.trigger()
  81. }
  82. type stringFromFloat struct {
  83. base
  84. format string
  85. from Float
  86. }
  87. // FloatToString creates a binding that connects a Float data item to a String.
  88. // Changes to the Float will be pushed to the String and setting the string will parse and set the
  89. // Float if the parse was successful.
  90. //
  91. // Since: 2.0
  92. func FloatToString(v Float) String {
  93. str := &stringFromFloat{from: v}
  94. v.AddListener(str)
  95. return str
  96. }
  97. // FloatToStringWithFormat creates a binding that connects a Float data item to a String and is
  98. // presented using the specified format. Changes to the Float will be pushed to the String and setting
  99. // the string will parse and set the Float if the string matches the format and its parse was successful.
  100. //
  101. // Since: 2.0
  102. func FloatToStringWithFormat(v Float, format string) String {
  103. if format == "%f" { // Same as not using custom formatting.
  104. return FloatToString(v)
  105. }
  106. str := &stringFromFloat{from: v, format: format}
  107. v.AddListener(str)
  108. return str
  109. }
  110. func (s *stringFromFloat) Get() (string, error) {
  111. val, err := s.from.Get()
  112. if err != nil {
  113. return "", err
  114. }
  115. if s.format != "" {
  116. return fmt.Sprintf(s.format, val), nil
  117. }
  118. return formatFloat(val), nil
  119. }
  120. func (s *stringFromFloat) Set(str string) error {
  121. var val float64
  122. if s.format != "" {
  123. safe := stripFormatPrecision(s.format)
  124. n, err := fmt.Sscanf(str, safe+" ", &val) // " " denotes match to end of string
  125. if err != nil {
  126. return err
  127. }
  128. if n != 1 {
  129. return errParseFailed
  130. }
  131. } else {
  132. new, err := parseFloat(str)
  133. if err != nil {
  134. return err
  135. }
  136. val = new
  137. }
  138. old, err := s.from.Get()
  139. if err != nil {
  140. return err
  141. }
  142. if val == old {
  143. return nil
  144. }
  145. if err = s.from.Set(val); err != nil {
  146. return err
  147. }
  148. s.DataChanged()
  149. return nil
  150. }
  151. func (s *stringFromFloat) DataChanged() {
  152. s.lock.RLock()
  153. defer s.lock.RUnlock()
  154. s.trigger()
  155. }
  156. type stringFromInt struct {
  157. base
  158. format string
  159. from Int
  160. }
  161. // IntToString creates a binding that connects a Int data item to a String.
  162. // Changes to the Int will be pushed to the String and setting the string will parse and set the
  163. // Int if the parse was successful.
  164. //
  165. // Since: 2.0
  166. func IntToString(v Int) String {
  167. str := &stringFromInt{from: v}
  168. v.AddListener(str)
  169. return str
  170. }
  171. // IntToStringWithFormat creates a binding that connects a Int data item to a String and is
  172. // presented using the specified format. Changes to the Int will be pushed to the String and setting
  173. // the string will parse and set the Int if the string matches the format and its parse was successful.
  174. //
  175. // Since: 2.0
  176. func IntToStringWithFormat(v Int, format string) String {
  177. if format == "%d" { // Same as not using custom formatting.
  178. return IntToString(v)
  179. }
  180. str := &stringFromInt{from: v, format: format}
  181. v.AddListener(str)
  182. return str
  183. }
  184. func (s *stringFromInt) Get() (string, error) {
  185. val, err := s.from.Get()
  186. if err != nil {
  187. return "", err
  188. }
  189. if s.format != "" {
  190. return fmt.Sprintf(s.format, val), nil
  191. }
  192. return formatInt(val), nil
  193. }
  194. func (s *stringFromInt) Set(str string) error {
  195. var val int
  196. if s.format != "" {
  197. safe := stripFormatPrecision(s.format)
  198. n, err := fmt.Sscanf(str, safe+" ", &val) // " " denotes match to end of string
  199. if err != nil {
  200. return err
  201. }
  202. if n != 1 {
  203. return errParseFailed
  204. }
  205. } else {
  206. new, err := parseInt(str)
  207. if err != nil {
  208. return err
  209. }
  210. val = new
  211. }
  212. old, err := s.from.Get()
  213. if err != nil {
  214. return err
  215. }
  216. if val == old {
  217. return nil
  218. }
  219. if err = s.from.Set(val); err != nil {
  220. return err
  221. }
  222. s.DataChanged()
  223. return nil
  224. }
  225. func (s *stringFromInt) DataChanged() {
  226. s.lock.RLock()
  227. defer s.lock.RUnlock()
  228. s.trigger()
  229. }
  230. type stringFromURI struct {
  231. base
  232. from URI
  233. }
  234. // URIToString creates a binding that connects a URI data item to a String.
  235. // Changes to the URI will be pushed to the String and setting the string will parse and set the
  236. // URI if the parse was successful.
  237. //
  238. // Since: 2.1
  239. func URIToString(v URI) String {
  240. str := &stringFromURI{from: v}
  241. v.AddListener(str)
  242. return str
  243. }
  244. func (s *stringFromURI) Get() (string, error) {
  245. val, err := s.from.Get()
  246. if err != nil {
  247. return "", err
  248. }
  249. return uriToString(val)
  250. }
  251. func (s *stringFromURI) Set(str string) error {
  252. val, err := uriFromString(str)
  253. if err != nil {
  254. return err
  255. }
  256. old, err := s.from.Get()
  257. if err != nil {
  258. return err
  259. }
  260. if val == old {
  261. return nil
  262. }
  263. if err = s.from.Set(val); err != nil {
  264. return err
  265. }
  266. s.DataChanged()
  267. return nil
  268. }
  269. func (s *stringFromURI) DataChanged() {
  270. s.lock.RLock()
  271. defer s.lock.RUnlock()
  272. s.trigger()
  273. }
  274. type stringToBool struct {
  275. base
  276. format string
  277. from String
  278. }
  279. // StringToBool creates a binding that connects a String data item to a Bool.
  280. // Changes to the String will be parsed and pushed to the Bool if the parse was successful, and setting
  281. // the Bool update the String binding.
  282. //
  283. // Since: 2.0
  284. func StringToBool(str String) Bool {
  285. v := &stringToBool{from: str}
  286. str.AddListener(v)
  287. return v
  288. }
  289. // StringToBoolWithFormat creates a binding that connects a String data item to a Bool and is
  290. // presented using the specified format. Changes to the Bool will be parsed and if the format matches and
  291. // the parse is successful it will be pushed to the String. Setting the Bool will push a formatted value
  292. // into the String.
  293. //
  294. // Since: 2.0
  295. func StringToBoolWithFormat(str String, format string) Bool {
  296. if format == "%t" { // Same as not using custom format.
  297. return StringToBool(str)
  298. }
  299. v := &stringToBool{from: str, format: format}
  300. str.AddListener(v)
  301. return v
  302. }
  303. func (s *stringToBool) Get() (bool, error) {
  304. str, err := s.from.Get()
  305. if str == "" || err != nil {
  306. return false, err
  307. }
  308. var val bool
  309. if s.format != "" {
  310. n, err := fmt.Sscanf(str, s.format+" ", &val) // " " denotes match to end of string
  311. if err != nil {
  312. return false, err
  313. }
  314. if n != 1 {
  315. return false, errParseFailed
  316. }
  317. } else {
  318. new, err := parseBool(str)
  319. if err != nil {
  320. return false, err
  321. }
  322. val = new
  323. }
  324. return val, nil
  325. }
  326. func (s *stringToBool) Set(val bool) error {
  327. var str string
  328. if s.format != "" {
  329. str = fmt.Sprintf(s.format, val)
  330. } else {
  331. str = formatBool(val)
  332. }
  333. old, err := s.from.Get()
  334. if str == old {
  335. return err
  336. }
  337. if err = s.from.Set(str); err != nil {
  338. return err
  339. }
  340. s.DataChanged()
  341. return nil
  342. }
  343. func (s *stringToBool) DataChanged() {
  344. s.lock.RLock()
  345. defer s.lock.RUnlock()
  346. s.trigger()
  347. }
  348. type stringToFloat struct {
  349. base
  350. format string
  351. from String
  352. }
  353. // StringToFloat creates a binding that connects a String data item to a Float.
  354. // Changes to the String will be parsed and pushed to the Float if the parse was successful, and setting
  355. // the Float update the String binding.
  356. //
  357. // Since: 2.0
  358. func StringToFloat(str String) Float {
  359. v := &stringToFloat{from: str}
  360. str.AddListener(v)
  361. return v
  362. }
  363. // StringToFloatWithFormat creates a binding that connects a String data item to a Float and is
  364. // presented using the specified format. Changes to the Float will be parsed and if the format matches and
  365. // the parse is successful it will be pushed to the String. Setting the Float will push a formatted value
  366. // into the String.
  367. //
  368. // Since: 2.0
  369. func StringToFloatWithFormat(str String, format string) Float {
  370. if format == "%f" { // Same as not using custom format.
  371. return StringToFloat(str)
  372. }
  373. v := &stringToFloat{from: str, format: format}
  374. str.AddListener(v)
  375. return v
  376. }
  377. func (s *stringToFloat) Get() (float64, error) {
  378. str, err := s.from.Get()
  379. if str == "" || err != nil {
  380. return 0.0, err
  381. }
  382. var val float64
  383. if s.format != "" {
  384. n, err := fmt.Sscanf(str, s.format+" ", &val) // " " denotes match to end of string
  385. if err != nil {
  386. return 0.0, err
  387. }
  388. if n != 1 {
  389. return 0.0, errParseFailed
  390. }
  391. } else {
  392. new, err := parseFloat(str)
  393. if err != nil {
  394. return 0.0, err
  395. }
  396. val = new
  397. }
  398. return val, nil
  399. }
  400. func (s *stringToFloat) Set(val float64) error {
  401. var str string
  402. if s.format != "" {
  403. str = fmt.Sprintf(s.format, val)
  404. } else {
  405. str = formatFloat(val)
  406. }
  407. old, err := s.from.Get()
  408. if str == old {
  409. return err
  410. }
  411. if err = s.from.Set(str); err != nil {
  412. return err
  413. }
  414. s.DataChanged()
  415. return nil
  416. }
  417. func (s *stringToFloat) DataChanged() {
  418. s.lock.RLock()
  419. defer s.lock.RUnlock()
  420. s.trigger()
  421. }
  422. type stringToInt struct {
  423. base
  424. format string
  425. from String
  426. }
  427. // StringToInt creates a binding that connects a String data item to a Int.
  428. // Changes to the String will be parsed and pushed to the Int if the parse was successful, and setting
  429. // the Int update the String binding.
  430. //
  431. // Since: 2.0
  432. func StringToInt(str String) Int {
  433. v := &stringToInt{from: str}
  434. str.AddListener(v)
  435. return v
  436. }
  437. // StringToIntWithFormat creates a binding that connects a String data item to a Int and is
  438. // presented using the specified format. Changes to the Int will be parsed and if the format matches and
  439. // the parse is successful it will be pushed to the String. Setting the Int will push a formatted value
  440. // into the String.
  441. //
  442. // Since: 2.0
  443. func StringToIntWithFormat(str String, format string) Int {
  444. if format == "%d" { // Same as not using custom format.
  445. return StringToInt(str)
  446. }
  447. v := &stringToInt{from: str, format: format}
  448. str.AddListener(v)
  449. return v
  450. }
  451. func (s *stringToInt) Get() (int, error) {
  452. str, err := s.from.Get()
  453. if str == "" || err != nil {
  454. return 0, err
  455. }
  456. var val int
  457. if s.format != "" {
  458. n, err := fmt.Sscanf(str, s.format+" ", &val) // " " denotes match to end of string
  459. if err != nil {
  460. return 0, err
  461. }
  462. if n != 1 {
  463. return 0, errParseFailed
  464. }
  465. } else {
  466. new, err := parseInt(str)
  467. if err != nil {
  468. return 0, err
  469. }
  470. val = new
  471. }
  472. return val, nil
  473. }
  474. func (s *stringToInt) Set(val int) error {
  475. var str string
  476. if s.format != "" {
  477. str = fmt.Sprintf(s.format, val)
  478. } else {
  479. str = formatInt(val)
  480. }
  481. old, err := s.from.Get()
  482. if str == old {
  483. return err
  484. }
  485. if err = s.from.Set(str); err != nil {
  486. return err
  487. }
  488. s.DataChanged()
  489. return nil
  490. }
  491. func (s *stringToInt) DataChanged() {
  492. s.lock.RLock()
  493. defer s.lock.RUnlock()
  494. s.trigger()
  495. }
  496. type stringToURI struct {
  497. base
  498. from String
  499. }
  500. // StringToURI creates a binding that connects a String data item to a URI.
  501. // Changes to the String will be parsed and pushed to the URI if the parse was successful, and setting
  502. // the URI update the String binding.
  503. //
  504. // Since: 2.1
  505. func StringToURI(str String) URI {
  506. v := &stringToURI{from: str}
  507. str.AddListener(v)
  508. return v
  509. }
  510. func (s *stringToURI) Get() (fyne.URI, error) {
  511. str, err := s.from.Get()
  512. if str == "" || err != nil {
  513. return fyne.URI(nil), err
  514. }
  515. return uriFromString(str)
  516. }
  517. func (s *stringToURI) Set(val fyne.URI) error {
  518. str, err := uriToString(val)
  519. if err != nil {
  520. return err
  521. }
  522. old, err := s.from.Get()
  523. if str == old {
  524. return err
  525. }
  526. if err = s.from.Set(str); err != nil {
  527. return err
  528. }
  529. s.DataChanged()
  530. return nil
  531. }
  532. func (s *stringToURI) DataChanged() {
  533. s.lock.RLock()
  534. defer s.lock.RUnlock()
  535. s.trigger()
  536. }