t_todoapp.nim 3.5 KB

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