/
layouts.go
120 lines (92 loc) · 2.22 KB
/
layouts.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright 2012 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package declarative
import (
"github.com/lxn/walk"
)
type Orientation byte
const (
Horizontal Orientation = Orientation(walk.Horizontal)
Vertical Orientation = Orientation(walk.Vertical)
)
type Margins struct {
Left int
Top int
Right int
Bottom int
}
func (m Margins) isZero() bool {
return m.Left == 0 && m.Top == 0 && m.Right == 0 && m.Bottom == 0
}
func (m Margins) toW() walk.Margins {
return walk.Margins{m.Left, m.Top, m.Right, m.Bottom}
}
type Size struct {
Width int
Height int
}
func (s Size) toW() walk.Size {
return walk.Size{s.Width, s.Height}
}
func setLayoutMargins(layout walk.Layout, margins Margins, marginsZero bool) error {
if !marginsZero && margins.isZero() {
margins = Margins{9, 9, 9, 9}
}
return layout.SetMargins(margins.toW())
}
func setLayoutSpacing(layout walk.Layout, spacing int, spacingZero bool) error {
if !spacingZero && spacing == 0 {
spacing = 6
}
return layout.SetSpacing(spacing)
}
type HBox struct {
Margins Margins
Spacing int
MarginsZero bool
SpacingZero bool
}
func (hb HBox) Create() (walk.Layout, error) {
l := walk.NewHBoxLayout()
if err := setLayoutMargins(l, hb.Margins, hb.MarginsZero); err != nil {
return nil, err
}
if err := setLayoutSpacing(l, hb.Spacing, hb.SpacingZero); err != nil {
return nil, err
}
return l, nil
}
type VBox struct {
Margins Margins
Spacing int
MarginsZero bool
SpacingZero bool
}
func (vb VBox) Create() (walk.Layout, error) {
l := walk.NewVBoxLayout()
if err := setLayoutMargins(l, vb.Margins, vb.MarginsZero); err != nil {
return nil, err
}
if err := setLayoutSpacing(l, vb.Spacing, vb.SpacingZero); err != nil {
return nil, err
}
return l, nil
}
type Grid struct {
Columns int
Margins Margins
Spacing int
MarginsZero bool
SpacingZero bool
}
func (g Grid) Create() (walk.Layout, error) {
l := walk.NewGridLayout()
if err := setLayoutMargins(l, g.Margins, g.MarginsZero); err != nil {
return nil, err
}
if err := setLayoutSpacing(l, g.Spacing, g.SpacingZero); err != nil {
return nil, err
}
return l, nil
}