builtin1.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package core
  2. import (
  3. "kumachan/standalone/qt"
  4. "kumachan/standalone/ctn"
  5. "kumachan/standalone/util/pseudounion"
  6. )
  7. const NgIndex = 0
  8. const OkIndex = 1
  9. const T_InvalidType = "<InvalidType>"
  10. const T_GenericType = "<GenericType>"
  11. const T_IntoReflectType = "IntoReflectType"
  12. const T_IntoReflectValue = "IntoReflectValue"
  13. const T_DummyReflectType = "DummyReflectType"
  14. const T_Null = "Null"
  15. const T_Error = "Error"
  16. const T_Bool = "Bool"
  17. const T_Int = "Int"
  18. const T_Float = "Float"
  19. const T_Char = "Char"
  20. const T_String = "String"
  21. const T_RegExp = "RegExp"
  22. const T_Bytes = "Bytes"
  23. const T_Asset = "Asset"
  24. const T_List = "List"
  25. const T_Seq = "Seq"
  26. const T_Queue = "Queue"
  27. const T_Heap = "Heap"
  28. const T_Set = "Set"
  29. const T_Map = "Map"
  30. const T_Observable = "$"
  31. const T_Time = "Time"
  32. const T_File = "File"
  33. const T_Lambda = "Lambda"
  34. const T_Pair = "Pair"
  35. const T_Triple = "Triple"
  36. const T_Maybe = "Maybe"
  37. const T_Lens1 = "Lens1"
  38. const T_Lens2 = "Lens2"
  39. const T_Hook = "Hook"
  40. type Lens1 struct {
  41. Value Object
  42. Assign func(Object) Object
  43. }
  44. type Lens2 struct {
  45. Value ctn.Maybe[Object]
  46. Assign func(Object) Object
  47. }
  48. type Hook struct {
  49. Job Observable
  50. }
  51. func Just(o Object) Object {
  52. return Obj(Union {
  53. Index: OkIndex,
  54. Object: o,
  55. })
  56. }
  57. func Nothing() Object {
  58. return Obj(Union {
  59. Index: NgIndex,
  60. Object: nil,
  61. })
  62. }
  63. func UnwrapMaybe(o Object) (Object, bool) {
  64. var u = (*o).(Union)
  65. if u.Index == OkIndex {
  66. return u.Object, true
  67. } else if u.Index == NgIndex {
  68. return nil, false
  69. } else {
  70. panic("something went wrong")
  71. }
  72. }
  73. func UnwrapLens2(o Object) (Object, bool) {
  74. var l = FromObject[Lens2](o)
  75. return l.Value.Value()
  76. }
  77. func MakeHook[T any] (k func()(T,func())) Hook {
  78. return Hook { Observable(func(pub DataPublisher) {
  79. var t, c = k()
  80. var v = ToObject(t)
  81. if c != nil {
  82. pub.context.registerCleaner(c)
  83. }
  84. pub.observer.value(v)
  85. pub.observer.complete()
  86. })}
  87. }
  88. func MakeHookWithEffect[T any] (h RuntimeHandle, k func()(T,Observable,func())) Hook {
  89. return Hook { ObservableFlattenLast(Observable(func(pub DataPublisher) {
  90. var t, e, c = k()
  91. var o = ObservableSyncValue(ToObject(t)).With(e, ErrorLogger(h))
  92. if c != nil {
  93. pub.context.registerCleaner(c)
  94. }
  95. pub.observer.value(Obj(o))
  96. pub.observer.complete()
  97. }))}
  98. }
  99. func MapHook[A any, B any] (h Hook, f func(A)(B)) Hook {
  100. return Hook { h.Job.Map(func(obj Object) Object {
  101. var a = FromObject[A](obj)
  102. var b = f(a)
  103. return ToObject(b)
  104. })}
  105. }
  106. func UseHook[A any] (h Hook, f func(A)(Observable)) Hook {
  107. return Hook { retrieveObject[A](h.Job, nil, func(a A) Observable {
  108. return f(a)
  109. })}
  110. }
  111. type Widgets struct {
  112. pseudounion.Tag
  113. Widget Widget
  114. List [] Widget
  115. }
  116. func (w Widgets) Value() ([] Widget) {
  117. switch W := pseudounion.Load(w).(type) {
  118. case Widget:
  119. return [] Widget { W }
  120. case [] Widget:
  121. return W
  122. default:
  123. panic("impossible branch")
  124. }
  125. }
  126. func (w Widgets) Deref(h RuntimeHandle) ([] qt.Widget) {
  127. return ctn.MapEach(w.Value(), func(w Widget) qt.Widget {
  128. return w.Deref(h)
  129. })
  130. }
  131. type ItemInfo struct {
  132. Key string
  133. Pos Observable
  134. }
  135. type ItemPos struct {
  136. Index int
  137. Total int
  138. }
  139. func DistinctUntilItemPosChanged(o Observable) Observable {
  140. return o.DistinctUntilChanged(func(a Object, b Object) bool {
  141. var u = FromObject[ItemPos](a)
  142. var v = FromObject[ItemPos](b)
  143. return (u == v)
  144. })
  145. }
  146. type ItemView struct {
  147. Widgets Widgets
  148. Extension ctn.Maybe[Widget]
  149. }
  150. type ItemViewProvider =
  151. func(Observable,ItemInfo)(Hook)
  152. //
  153. type ItemEditView struct {
  154. Widgets Widgets
  155. Extension ctn.Maybe[Widget]
  156. EditOps func(key string)(Observable)
  157. }
  158. type ItemEditViewProvider =
  159. func(Object,ItemInfo)(Hook)
  160. //
  161. type ListEditOperation struct {
  162. pseudounion.Tag
  163. Prepend; Append
  164. Update; Delete
  165. MoveUp; MoveDown
  166. MoveTop; MoveBottom
  167. InsertAbove; InsertBelow
  168. Reorder
  169. }
  170. type Prepend struct { Value Object }
  171. type Append struct { Value Object }
  172. type Update struct { Key string; Value Object }
  173. type Delete struct { Key ctn.Maybe[string] }
  174. type MoveUp struct { Key ctn.Maybe[string] }
  175. type MoveDown struct { Key ctn.Maybe[string] }
  176. type MoveTop struct { Key ctn.Maybe[string] }
  177. type MoveBottom struct { Key ctn.Maybe[string] }
  178. type InsertAbove struct { PivotKey string; Value Object }
  179. type InsertBelow struct { PivotKey string; Value Object }
  180. type Reorder struct { Reorder func(List)(List) }