forked from callum-oakley/fgwm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid.go
205 lines (182 loc) · 4.75 KB
/
grid.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package grid
import (
"errors"
"sync"
"time"
"github.com/callum-oakley/fgwm/focus"
"github.com/callum-oakley/fgwm/view"
"github.com/callum-oakley/fgwm/wmutils"
)
type Direction int
const (
Left Direction = iota
Right
Up
Down
)
type FocusStrategy int
const (
Next FocusStrategy = iota
Prev
)
type Position struct {
X, Y int
}
type Size struct {
W, H int
}
type Rectangle struct {
TopLeft, BottomRight Position
}
func (p Position) Offset(s Size) Position {
return Position{p.X + s.W, p.Y + s.H}
}
func (a Position) Diff(b Position) Size {
return Size{a.X - b.X, a.Y - b.Y}
}
func (a Size) Add(b Size) Size {
return Size{a.W + b.W, a.H + b.H}
}
func (a Size) Scale(k int) Size {
return Size{k * a.W, k * a.H}
}
func (r Rectangle) Size() Size {
return r.BottomRight.Diff(r.TopLeft)
}
func (r Rectangle) Offset(s Size) Rectangle {
return Rectangle{r.TopLeft.Offset(s), r.BottomRight.Offset(s)}
}
func (r Rectangle) Grow(s Size) Rectangle {
return Rectangle{r.TopLeft.Offset(s.Scale(-1)), r.BottomRight.Offset(s)}
}
func (r Rectangle) Valid() bool {
return r.TopLeft.X < r.BottomRight.X && r.TopLeft.Y < r.BottomRight.Y
}
type Margins struct {
Top, Bottom, Left, Right wmutils.Pixels
}
type Grid struct {
// size of the screen
screen wmutils.Size
// margin at each edge of the screen
margins Margins
// padding around cells
pad wmutils.Size
// border around cells
border wmutils.Pixels
// size of each cell, including pad and border but excluding margin
cell wmutils.Size
// the pixel locations of the cell boundaries
points map[Position]wmutils.Position
// the size of the grid in cells
size Size
focus focus.Focus
view view.View
mux sync.Mutex
}
// The sizes that define the grid layout are made up as follows (bd is border).
// The Y direction is similar.
//
// | <-------------------------------- screen -------------------------------> |
// | | | | | | | | | | | | |
// | margin | pad | bd | | bd | pad | pad | bd | | bd | pad | margin |
// | | | | | | | | | | | | |
// | | <--------- cell ---------> | <--------- cell ---------> | |
//
type Options struct {
Border wmutils.Pixels
Margins Margins
Pad wmutils.Size
Size Size
InitialView int
FocusTimeout time.Duration
FocussedColour, UnfocussedColour wmutils.Colour
}
func New(opts *Options) (*Grid, error) {
wid, err := wmutils.Root()
if err != nil {
return nil, err
}
_, screen, err := wmutils.GetAttributes(wid)
if err != nil {
return nil, err
}
cell := wmutils.Size{
W: (screen.W - opts.Margins.Left - opts.Margins.Right) /
wmutils.Pixels(opts.Size.W),
H: (screen.H - opts.Margins.Top - opts.Margins.Bottom) /
wmutils.Pixels(opts.Size.H),
}
// Absorb any excess pixels resulting from rounding in to the margins
extraMarginW := screen.W - wmutils.Pixels(opts.Size.W)*cell.W -
opts.Margins.Left - opts.Margins.Right
extraMarginH := screen.H - wmutils.Pixels(opts.Size.H)*cell.H -
opts.Margins.Top - opts.Margins.Bottom
margins := Margins{
Top: opts.Margins.Top + extraMarginH/2,
Bottom: opts.Margins.Bottom + extraMarginH/2,
Left: opts.Margins.Left + extraMarginW/2,
Right: opts.Margins.Right + extraMarginW/2,
}
focus, err := focus.New(
opts.FocusTimeout,
opts.FocussedColour,
opts.UnfocussedColour,
)
if err != nil {
return nil, err
}
view, err := view.New(screen, opts.Border, opts.InitialView)
if err != nil {
return nil, err
}
return &Grid{
screen: screen,
margins: margins,
border: opts.Border,
pad: opts.Pad,
cell: cell,
size: opts.Size,
focus: focus,
view: view,
}, nil
}
func (g *Grid) WatchWindowEvents() error {
for ev := range wmutils.WatchEvents() {
switch ev.Type {
case wmutils.CreateNotifyEvent:
// Wait for a tick so that the window's self imposed size has a
// chance to settle
time.Sleep(100 * time.Millisecond)
g.mux.Lock()
if err := g.centerWID(ev.WID); err != nil {
return err
}
if err := wmutils.SetBorderWidth(ev.WID, g.border); err != nil {
return err
}
g.mux.Unlock()
case wmutils.DestroyNotifyEvent:
g.mux.Lock()
if err := g.focus.Unregister(ev.WID); err != nil {
return err
}
g.view.UnregisterAll(ev.WID)
g.mux.Unlock()
case wmutils.UnmapNotifyEvent:
g.mux.Lock()
if err := g.focus.Unset(ev.WID); err != nil {
return err
}
g.mux.Unlock()
case wmutils.MapNotifyEvent:
g.mux.Lock()
if err := g.focus.Register(ev.WID); err != nil {
return err
}
g.view.Register(ev.WID)
g.mux.Unlock()
}
}
return errors.New("Window event channel closed!")
}