Layout.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package giu
  2. const (
  3. // Auto is used to widget.Size to indicate height or width to occupy available spaces.
  4. Auto float32 = -1
  5. )
  6. // Widget is a base unit of giu rendering system.
  7. // each widget just needs to implement Build method which is called,
  8. // when widget needs to be rendered.
  9. type Widget interface {
  10. Build()
  11. }
  12. var (
  13. _ Widget = Layout{}
  14. _ Splitable = Layout{}
  15. )
  16. // Layout is a set of widgets. It implements Widget interface so
  17. // Layout can be used as a widget.
  18. type Layout []Widget
  19. // Build implements Widget interface.
  20. func (l Layout) Build() {
  21. for _, w := range l {
  22. if w != nil {
  23. w.Build()
  24. }
  25. }
  26. }
  27. // Splitable is implemented by widgets, which can be split (ranged)
  28. // Layout implements Splitable.
  29. type Splitable interface {
  30. Range(func(w Widget))
  31. }
  32. // Range ranges ofer the Layout, calling rangeFunc
  33. // on each loop iteration.
  34. func (l Layout) Range(rangeFunc func(Widget)) {
  35. for _, w := range l {
  36. if splitable, canRange := w.(Splitable); canRange {
  37. splitable.Range(rangeFunc)
  38. continue
  39. }
  40. rangeFunc(w)
  41. }
  42. }