/
constraint.go
94 lines (83 loc) · 1.5 KB
/
constraint.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
package clients
import (
"github.com/thrisp/scpwm/euclid/rules"
)
type Constraint interface {
Min(string) uint16
Max(string) uint16
SetConstraint(*rules.Consequence)
}
type constraint struct {
minWidth uint16
maxWidth uint16
minHeight uint16
maxHeight uint16
}
func newConstraint(csq *rules.Consequence) Constraint {
c := &constraint{}
c.SetConstraint(csq)
return c
}
func (c *constraint) Min(dimension string) uint16 {
switch dimension {
case "height":
return c.minHeight
case "width":
return c.minWidth
}
return 0
}
func (c *constraint) Max(dimension string) uint16 {
switch dimension {
case "height":
return c.maxHeight
case "width":
return c.maxWidth
}
return 0
}
func (c *constraint) SetConstraint(csq *rules.Consequence) {
if csq.MinHeight != 0 {
c.minHeight = csq.MinHeight
}
if csq.MaxHeight != 0 {
c.maxHeight = csq.MaxHeight
}
if csq.MinWidth != 0 {
c.minWidth = csq.MinWidth
}
if csq.MaxWidth != 0 {
c.maxWidth = csq.MaxWidth
}
}
func RestrainWidth(c Constraint, w uint16) uint16 {
if w < 1 {
w = 1
}
mw := c.Min("width")
if mw > 0 && w < mw {
return mw
}
xw := c.Max("width")
if xw > 0 && w > xw {
return xw
}
return w
}
func RestrainHeight(c Constraint, h uint16) uint16 {
if h < 1 {
h = 1
}
mh := c.Min("height")
if mh > 0 && h < mh {
return mh
}
xh := c.Max("height")
if xh > 0 && h > xh {
return xh
}
return h
}
func RestrainSize(c Constraint, w, h uint16) (uint16, uint16) {
return RestrainWidth(c, w), RestrainHeight(c, h)
}