-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpanel.go
174 lines (158 loc) · 3.52 KB
/
panel.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"github.com/ziutek/mdtwm/xgb_patched"
)
// Box for WM window (panel)
type Panel struct {
commonBox
typ Orientation // panel type (vertical or horizontal)
ratio float64 // ratio of size of two neighboringsubwindows
first int16 // space for first subwindow (pixels)
}
// New Panel has parent set to nil and its window
// parent is root window.
// ratio == 1 means all subwindows in panel are equal in size.
func NewPanel(typ Orientation, first int16, ratio float64) *Panel {
var p Panel
p.init(
NewWindow(root.Window(), Geometry{0, 0, 1, 1, 0},
xgb.WindowClassInputOutput, 0),
xgb.EventMaskEnterWindow|xgb.EventMaskStructureNotify|
xgb.EventMaskSubstructureRedirect,
)
p.width = 1
p.height = 1
p.typ = typ
p.ratio = ratio
p.first = first
p.SetClass(cfg.Instance, cfg.Class)
p.SetName("mdtwm panel")
p.w.SetBackPixmap(xgb.BackPixmapParentRelative)
return &p
}
func (p *Panel) Geometry() Geometry {
return Geometry{
X: p.x, Y: p.y,
W: p.width, H: p.height,
}
}
func (p *Panel) SetPosSize(x, y, width, height int16) {
p.x, p.y, p.width, p.height = x, y, width, height
p.w.SetGeometry(Geometry{x, y, width, height, 0})
p.tile()
}
func (p *Panel) SetFocus(f bool, t xgb.Timestamp) {
if f {
p.w.SetInputFocus(t)
}
}
// Inserts b next to mark. Can use x, y (coordinates in mark) for decision.
func (p *Panel) InsertNextTo(b, mark Box, x, y int16) {
var ok bool
mg := mark.Geometry()
if p.typ == Vertical && y < mg.H/2 || p.typ == Horizontal && x < mg.W/2 {
ok = p.children.InsertBefore(b, mark)
} else {
ok = p.children.InsertAfter(b, mark)
}
if !ok {
p.children.PushBack(b)
}
p.insertCommon(b)
}
func (p *Panel) InsertBefore(b, mark Box) {
if !p.children.InsertBefore(b, mark) {
p.children.PushBack(b)
}
p.insertCommon(b)
}
func (p *Panel) Append(b Box) {
p.children.PushBack(b)
p.insertCommon(b)
}
// Inserts a box into panel
func (p *Panel) insertCommon(b Box) {
b.SetParent(p)
if !b.Float() {
// Rearange panel and show new box
p.tile()
}
b.Window().Map()
if w, ok := b.(*BoxedWindow); ok {
w.SetWmState(WmStateNormal)
w.UpdateNetWmDesktop()
}
}
func (p *Panel) Remove(b Box) {
b.SetParent(root)
p.children.Remove(b)
p.tile()
}
// Update geometry for boxes in panel
func (p *Panel) tile() {
var (
n int16
b Box
)
for b = p.children.Front(); b != nil; b = b.Next() {
if !b.Float() {
n++
}
}
if n == 0 {
return // there is no boxes in panel
}
if p.typ == Vertical {
d.Print("Tile V in: ", p)
hg := NewSizeGen(p.height, n, p.first, p.ratio)
y, w := int16(0), p.width
for b = p.children.Front(); n > 1; b, n = b.Next(), n-1 {
if b.Float() {
continue
}
h := hg.Next()
b.SetPosSize(0, y, w, h)
y += h
}
// Last window obtain all remaining space
b.SetPosSize(0, y, w, p.height-y)
} else {
d.Print("Tile H in:", p)
wg := NewSizeGen(p.width, n, p.first, p.ratio)
x, h := int16(0), p.height
for b = p.children.Front(); n > 1; b, n = b.Next(), n-1 {
if b.Float() {
continue
}
w := wg.Next()
b.SetPosSize(x, 0, w, h)
x += w
}
// Last window obtain all remaining space
b.SetPosSize(x, 0, p.width-x, h)
}
}
type SizeGen struct {
s, ratio float64
first int16
}
func NewSizeGen(allSpace, n, first int16, ratio float64) *SizeGen {
if first != 0 {
n--
}
d := 1.0
for ; n > 1; n-- {
d = d*ratio + 1
}
return &SizeGen{float64(allSpace-first) / d, ratio, first}
}
func (g *SizeGen) Next() (s int16) {
if g.first != 0 {
s = g.first
g.first = 0
return
}
s = int16(g.s)
g.s *= g.ratio
return
}