t_customComponent.nim 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import sigui/[uibase, mouseArea, animations, dolars]
  2. type
  3. Switch* = ref object of Uiobj
  4. enabled*: Property[bool] = true.property
  5. isOn*: Property[bool]
  6. color*: Property[Col] = color(0, 0, 0).property
  7. registerComponent Switch
  8. method init*(this: Switch) =
  9. procCall this.super.init()
  10. when isMainModule:
  11. this.isOn.changed.connectTo this, val:
  12. echo this
  13. this.makeLayout:
  14. w = 40
  15. h = 20
  16. - MouseArea() as mouse:
  17. this.fill(parent)
  18. this.mouseDownAndUpInside.connectTo root:
  19. if root.enabled[]:
  20. root.isOn[] = not root.isOn[]
  21. - UiRectBorder():
  22. this.fill(parent)
  23. this.binding radius: min(this.w[], this.h[]) / 2 - 2
  24. borderWidth = 2
  25. color = "aaa"
  26. - UiRect():
  27. centerY = parent.center
  28. w := min(parent.w[], parent.h[]) - 8
  29. h := this.w[]
  30. radius := this.w[] / 2
  31. x = binding:
  32. if root.isOn[]:
  33. parent.w[] - this.w[] - 4
  34. else:
  35. 4'f32
  36. color := root.color[]
  37. - this.x.transition(0.4's):
  38. easing = outCubicEasing
  39. this.newChildsObject = mouse
  40. when isMainModule:
  41. preview(clearColor = color(1, 1, 1), margin = 20, withWindow = proc: Uiobj =
  42. var r = Switch()
  43. init r
  44. r
  45. )