docklayout.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2016 The G3N Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package gui
  5. // DockLayout is the layout for docking panels to the internal edges of their parent.
  6. type DockLayout struct {
  7. }
  8. // DockLayoutParams specifies the edge to dock to.
  9. type DockLayoutParams struct {
  10. Edge int
  11. }
  12. const (
  13. DockTop = iota + 1
  14. DockRight
  15. DockBottom
  16. DockLeft
  17. DockCenter
  18. )
  19. // NewDockLayout returns a pointer to a new DockLayout.
  20. func NewDockLayout() *DockLayout {
  21. return new(DockLayout)
  22. }
  23. // Recalc (which satisfies the ILayout interface) recalculates the positions and sizes of the children panels.
  24. func (dl *DockLayout) Recalc(ipan IPanel) {
  25. pan := ipan.GetPanel()
  26. width := pan.Width()
  27. topY := float32(0)
  28. bottomY := pan.Height()
  29. leftX := float32(0)
  30. rightX := width
  31. // Top and bottom first
  32. for _, iobj := range pan.Children() {
  33. child := iobj.(IPanel).GetPanel()
  34. if child.layoutParams == nil {
  35. continue
  36. }
  37. params := child.layoutParams.(*DockLayoutParams)
  38. if params.Edge == DockTop {
  39. child.SetPosition(0, topY)
  40. topY += child.Height()
  41. child.SetWidth(width)
  42. continue
  43. }
  44. if params.Edge == DockBottom {
  45. child.SetPosition(0, bottomY-child.Height())
  46. bottomY -= child.Height()
  47. child.SetWidth(width)
  48. continue
  49. }
  50. }
  51. // Left and right
  52. for _, iobj := range pan.Children() {
  53. child := iobj.(IPanel).GetPanel()
  54. if child.layoutParams == nil {
  55. continue
  56. }
  57. params := child.layoutParams.(*DockLayoutParams)
  58. if params.Edge == DockLeft {
  59. child.SetPosition(leftX, topY)
  60. leftX += child.Width()
  61. child.SetHeight(bottomY - topY)
  62. continue
  63. }
  64. if params.Edge == DockRight {
  65. child.SetPosition(rightX-child.Width(), topY)
  66. rightX -= child.Width()
  67. child.SetHeight(bottomY - topY)
  68. continue
  69. }
  70. }
  71. // Center (only the first found)
  72. for _, iobj := range pan.Children() {
  73. child := iobj.(IPanel).GetPanel()
  74. if child.layoutParams == nil {
  75. continue
  76. }
  77. params := child.layoutParams.(*DockLayoutParams)
  78. if params.Edge == DockCenter {
  79. child.SetPosition(leftX, topY)
  80. child.SetSize(rightX-leftX, bottomY-topY)
  81. break
  82. }
  83. }
  84. }