t_customComponent.nim 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. this.isOn.changed.connectTo this, val:
  11. echo this
  12. this.makeLayout:
  13. w = 40
  14. h = 20
  15. - MouseArea() as mouse:
  16. this.fill(parent)
  17. this.mouseDownAndUpInside.connectTo root:
  18. if root.enabled[]:
  19. root.isOn[] = not root.isOn[]
  20. - UiRectBorder():
  21. this.fill(parent)
  22. this.binding radius: min(this.w[], this.h[]) / 2 - 2
  23. borderWidth = 2
  24. color = color(0.7, 0.7, 0.7)
  25. - UiRect():
  26. centerY = parent.center
  27. this.binding w: min(parent.w[], parent.h[]) - 8
  28. this.binding h: this.w[]
  29. this.binding radius: this.w[] / 2
  30. this.binding x:
  31. if root.isOn[]:
  32. parent.w[] - this.w[] - 4
  33. else:
  34. 4'f32
  35. this.binding color: root.color[]
  36. - this.x.transition(0.4's):
  37. easing = outCubicEasing
  38. this.newChildsObject = mouse
  39. when isMainModule:
  40. preview(clearColor = color(1, 1, 1), margin = 20, withWindow = proc: Uiobj =
  41. var r = Switch()
  42. init r
  43. r
  44. )