t_todoapp.nim 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 = newSiwinGlobals().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.new as app
  26. - UiText.new 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.new 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.new 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.new 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.new:
  57. this.centerIn parent
  58. text = "+"
  59. font = typeface.withSize(32)
  60. color = color(1, 1, 1)
  61. - MouseArea.new 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.new:
  70. this.fillHorizontal(parent, 20)
  71. bottom = parent.bottom - 20
  72. top = taskAdder.bottom + 20
  73. app.layout --- Layout.new:
  74. <--- Layout.new: app.tasksChanged[]
  75. this.col(gap = 5)
  76. this.binding w: parent.w[]
  77. for i in 0..app.tasks.high:
  78. template task: auto = app.tasks[i]
  79. - DestroyLogger.new as logger:
  80. this.inner.message = "destroyed: " & $i
  81. - Layout.row(gap = 10):
  82. align = center
  83. - Switch(isOn: task.complete[].property):
  84. color = color(0.43, 0.15, 0.76)
  85. isOn := task.complete[]
  86. this.bindingValue task.complete[]: this.isOn[]
  87. - UiText.new:
  88. text = task.name
  89. this.binding font:
  90. let it = typeface.withSize(24)
  91. it.strikethrough = task.complete[]
  92. it
  93. this.binding color:
  94. if mouse.pressed[]: color(0.2, 0.2, 0.2)
  95. elif mouse.hovered[]: color(0.4, 0.4, 0.4)
  96. else: color(0, 0, 0)
  97. - MouseArea.new as mouse:
  98. this.fill parent
  99. on this.mouseDownAndUpInside:
  100. task.complete[] = not task.complete[]
  101. echo "made sure ", logger.inner.message, " works"
  102. run window.siwinWindow