builder_layout.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. //
  6. // BuilderLayoutHBox is builder for HBox layout
  7. //
  8. type BuilderLayoutHBox struct{}
  9. // BuildLayout builds and returns an HBoxLayout with the specified attributes
  10. func (bl *BuilderLayoutHBox) BuildLayout(b *Builder, am map[string]interface{}) (ILayout, error) {
  11. // Creates layout and sets optional spacing
  12. l := NewHBoxLayout()
  13. var spacing float32
  14. if sp := am[AttribSpacing]; sp != nil {
  15. spacing = sp.(float32)
  16. }
  17. l.SetSpacing(spacing)
  18. // Sets optional horizontal alignment
  19. if ah := am[AttribAlignh]; ah != nil {
  20. l.SetAlignH(ah.(Align))
  21. }
  22. // Sets optional minheight flag
  23. if mh := am[AttribAutoHeight]; mh != nil {
  24. l.SetAutoHeight(mh.(bool))
  25. }
  26. // Sets optional minwidth flag
  27. if mw := am[AttribAutoWidth]; mw != nil {
  28. l.SetAutoWidth(mw.(bool))
  29. }
  30. return l, nil
  31. }
  32. // BuildParams builds and returns a pointer to HBoxLayoutParams with the specified attributes
  33. func (bl *BuilderLayoutHBox) BuildParams(b *Builder, am map[string]interface{}) (interface{}, error) {
  34. // Creates layout parameters with default values
  35. params := HBoxLayoutParams{Expand: 0, AlignV: AlignNone}
  36. // Sets optional expand parameter
  37. if expand := am[AttribExpand]; expand != nil {
  38. params.Expand = expand.(float32)
  39. }
  40. // Sets optional align parameter
  41. if alignv := am[AttribAlignv]; alignv != nil {
  42. params.AlignV = alignv.(Align)
  43. }
  44. return &params, nil
  45. }
  46. //
  47. // BuilderLayoutVBox is builder for VBox layout
  48. //
  49. type BuilderLayoutVBox struct{}
  50. // BuildLayout builds and returns an VBoxLayout with the specified attributes
  51. func (bl *BuilderLayoutVBox) BuildLayout(b *Builder, am map[string]interface{}) (ILayout, error) {
  52. // Creates layout and sets optional spacing
  53. l := NewVBoxLayout()
  54. var spacing float32
  55. if sp := am[AttribSpacing]; sp != nil {
  56. spacing = sp.(float32)
  57. }
  58. l.SetSpacing(spacing)
  59. // Sets optional vertical alignment
  60. if av := am[AttribAlignh]; av != nil {
  61. l.SetAlignV(av.(Align))
  62. }
  63. // Sets optional minheight flag
  64. if mh := am[AttribAutoHeight]; mh != nil {
  65. l.SetAutoHeight(mh.(bool))
  66. }
  67. // Sets optional minwidth flag
  68. if mw := am[AttribAutoWidth]; mw != nil {
  69. l.SetAutoWidth(mw.(bool))
  70. }
  71. return l, nil
  72. }
  73. // BuildParams builds and returns a pointer to VBoxLayoutParams with the specified attributes
  74. func (bl *BuilderLayoutVBox) BuildParams(b *Builder, am map[string]interface{}) (interface{}, error) {
  75. // Creates layout parameters with default values
  76. params := VBoxLayoutParams{Expand: 0, AlignH: AlignNone}
  77. // Sets optional expand parameter
  78. if expand := am[AttribExpand]; expand != nil {
  79. params.Expand = expand.(float32)
  80. }
  81. // Sets optional align parameter
  82. if alignh := am[AttribAlignh]; alignh != nil {
  83. params.AlignH = alignh.(Align)
  84. }
  85. return &params, nil
  86. }
  87. //
  88. // BuilderLayoutGrid is builder for Grid layout
  89. //
  90. type BuilderLayoutGrid struct{}
  91. // BuildLayout builds and returns a GridLayout with the specified attributes
  92. func (bl *BuilderLayoutGrid) BuildLayout(b *Builder, am map[string]interface{}) (ILayout, error) {
  93. // Get number of columns
  94. v := am[AttribCols]
  95. if v == nil {
  96. return nil, b.err(am, AttribCols, "Number of columns must be supplied")
  97. }
  98. cols := v.(int)
  99. if cols <= 0 {
  100. return nil, b.err(am, AttribCols, "Invalid number of columns")
  101. }
  102. // Creates layout
  103. l := NewGridLayout(cols)
  104. // Sets optional horizontal alignment
  105. if ah := am[AttribAlignh]; ah != nil {
  106. l.SetAlignH(ah.(Align))
  107. }
  108. // Sets optional vertical alignment
  109. if av := am[AttribAlignv]; av != nil {
  110. l.SetAlignV(av.(Align))
  111. }
  112. // Sets optional horizontal expand flag
  113. if eh := am[AttribExpandh]; eh != nil {
  114. l.SetExpandH(eh.(bool))
  115. }
  116. // Sets optional vertical expand flag
  117. if ev := am[AttribExpandv]; ev != nil {
  118. l.SetExpandV(ev.(bool))
  119. }
  120. return l, nil
  121. }
  122. // BuildParams builds and returns a pointer to GridLayoutParams with the specified attributes
  123. func (bl *BuilderLayoutGrid) BuildParams(b *Builder, am map[string]interface{}) (interface{}, error) {
  124. // Creates layout parameters with default values
  125. params := GridLayoutParams{
  126. ColSpan: 0,
  127. AlignH: AlignNone,
  128. AlignV: AlignNone,
  129. }
  130. // Sets optional colspan
  131. if cs := am[AttribColSpan]; cs != nil {
  132. params.ColSpan = cs.(int)
  133. }
  134. // Sets optional alignh parameter
  135. if align := am[AttribAlignh]; align != nil {
  136. params.AlignH = align.(Align)
  137. }
  138. // Sets optional alignv parameter
  139. if align := am[AttribAlignv]; align != nil {
  140. params.AlignV = align.(Align)
  141. }
  142. return &params, nil
  143. }
  144. //
  145. // BuilderLayoutDock is builder for Dock layout
  146. //
  147. type BuilderLayoutDock struct{}
  148. // BuildLayout builds and returns a DockLayout with the specified attributes
  149. func (bl *BuilderLayoutDock) BuildLayout(b *Builder, am map[string]interface{}) (ILayout, error) {
  150. return NewDockLayout(), nil
  151. }
  152. // BuildParams builds and returns a pointer to DockLayoutParams with the specified attributes
  153. func (bl *BuilderLayoutDock) BuildParams(b *Builder, am map[string]interface{}) (interface{}, error) {
  154. edge := am[AttribEdge]
  155. if edge == nil {
  156. return nil, b.err(am, AttribEdge, "Edge name not found")
  157. }
  158. params := DockLayoutParams{Edge: edge.(int)}
  159. return &params, nil
  160. }