t_todoapp.nim 3.7 KB

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