t_todoapp.nim 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # note: incomplete
  2. import unittest
  3. import siwin
  4. import sigui
  5. import t_customComponent
  6. type
  7. App = ref object of Uiobj
  8. tasks: seq[tuple[name: string, complete: Property[bool]]]
  9. tasksChanged: Event[void]
  10. layout: ChangableChild[Layout]
  11. DestroyLoggerInner = object
  12. message: string
  13. DestroyLogger = ref object of Uiobj
  14. inner: DestroyLoggerInner
  15. registerComponent App
  16. proc `=destroy`(x: DestroyLoggerInner) =
  17. if x.message != "":
  18. echo x.message
  19. test "todo app":
  20. let window = newOpenglWindow(size = ivec2(500, 800), title = "todos").newUiWindow
  21. const typefaceFile = staticRead "Roboto-Regular.ttf"
  22. let typeface = parseTtf(typefaceFile)
  23. window.makeLayout:
  24. this.clearColor = color(1, 1, 1)
  25. - App() as app
  26. - UiText() as text:
  27. centerX = parent.center
  28. y = 20
  29. font = typeface.withSize(72)
  30. color = color(0.5, 0.5, 0.5)
  31. text = "todos"
  32. - UiRectBorder() as taskAdder:
  33. this.fillHorizontal(parent, 20)
  34. h = 40
  35. top = text.bottom + 20
  36. color = color(0.5, 0.5, 0.5)
  37. radius = 5
  38. - TextArea() as taskName:
  39. this.fill(parent, 4, 2)
  40. right = addTask.left - 10
  41. text = "sample task"
  42. this.textObj[].font[] = typeface.withSize(32)
  43. this.onKeyDown enter:
  44. mouse.mouseDownAndUpInside.emit()
  45. - UiRect() as addTask:
  46. right = parent.right - 5
  47. this.fillVertical(parent, 4)
  48. w := this.h[]
  49. radius = 5
  50. color = binding:
  51. if mouse.pressed[]: color(0.43, 0.15, 0.76).lighten(0.1)
  52. elif mouse.hovered[]: color(0.43, 0.15, 0.76).lighten(0.2)
  53. else: color(0.43, 0.15, 0.76)
  54. - this.color.transition(0.4's):
  55. easing = outCubicEasing
  56. - UiText():
  57. this.centerIn parent
  58. text = "+"
  59. font = typeface.withSize(32)
  60. color = color(1, 1, 1)
  61. - MouseArea() as mouse:
  62. this.fill parent
  63. on this.mouseDownAndUpInside:
  64. if taskName.text[] == "": return
  65. app.tasks.add((name: taskName.text[], complete: false.property))
  66. app.tasksChanged.emit()
  67. taskName.pushState()
  68. taskName.text[] = ""
  69. - ScrollArea():
  70. this.fillHorizontal(parent, 20)
  71. bottom = parent.bottom - 20
  72. top = taskAdder.bottom + 20
  73. app.layout --- Layout():
  74. <--- Layout(): app.tasksChanged[]
  75. this.binding w: parent.w[]
  76. orientation = vertical
  77. gap = 5
  78. for i in 0..app.tasks.high:
  79. template task: auto = app.tasks[i]
  80. - DestroyLogger() as logger:
  81. this.inner.message = "destroyed: " & $i
  82. - Layout():
  83. spacing = 10
  84. align = center
  85. - Switch(isOn: task.complete[].property):
  86. color = color(0.43, 0.15, 0.76)
  87. isOn := task.complete[]
  88. this.bindingValue task.complete[]: this.isOn[]
  89. - UiText():
  90. text = task.name
  91. this.binding font:
  92. let it = typeface.withSize(24)
  93. it.strikethrough = task.complete[]
  94. it
  95. this.binding color:
  96. if mouse.pressed[]: color(0.2, 0.2, 0.2)
  97. elif mouse.hovered[]: color(0.4, 0.4, 0.4)
  98. else: color(0, 0, 0)
  99. - MouseArea() as mouse:
  100. this.fill parent
  101. on this.mouseDownAndUpInside:
  102. task.complete[] = not task.complete[]
  103. echo "made sure ", logger.inner.message, " works"
  104. run window.siwinWindow