forked from callum-oakley/fgwm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
94 lines (82 loc) · 2.13 KB
/
utils.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 grid
import (
"fmt"
"github.com/callum-oakley/fgwm/wmutils"
)
func (g *Grid) getRectangle(wid wmutils.WindowID) (Rectangle, error) {
pPos, pSize, err := wmutils.GetAttributes(wid)
if err != nil {
return Rectangle{}, fmt.Errorf(
"error getting rectangle for wid %v: %v",
wid,
err,
)
}
return Rectangle{
g.closestPoint(pPos.Offset(g.pad.Scale(-1))),
g.closestPoint(pPos.Offset(pSize.Add(
g.pad.Add(wmutils.Size{g.border, g.border}),
))),
}, nil
}
func (g *Grid) closestPoint(p wmutils.Position) Position {
return Position{
X: round((float64(p.X - g.margins.Left)) / float64(g.cell.W)),
Y: round((float64(p.Y - g.margins.Top)) / float64(g.cell.H)),
}
}
func round(x float64) int {
return int(x + 0.5)
}
func (g *Grid) pInGrid(p Position) bool {
return 0 <= p.X && p.X <= g.size.W && 0 <= p.Y && p.Y <= g.size.H
}
func (g *Grid) inGrid(r Rectangle) bool {
return g.pInGrid(r.TopLeft) && g.pInGrid(r.BottomRight)
}
func (g *Grid) pixelSize(size Size) wmutils.Size {
return wmutils.Size{
W: wmutils.Pixels(size.W) * g.cell.W,
H: wmutils.Pixels(size.H) * g.cell.H,
}
}
func (g *Grid) pixelPosition(pos Position) wmutils.Position {
return wmutils.Position{
X: g.margins.Left + wmutils.Pixels(pos.X)*g.cell.W,
Y: g.margins.Top + wmutils.Pixels(pos.Y)*g.cell.H,
}
}
func index(wids []wmutils.WindowID, wid wmutils.WindowID) (int, error) {
for i := 0; i < len(wids); i++ {
if wids[i] == wid {
return i, nil
}
}
return 0, fmt.Errorf("can't find %v in %v", wid, wids)
}
func (g *Grid) centerWID(wid wmutils.WindowID) error {
center := Position{g.size.W / 2, g.size.H / 2}
r, err := g.getRectangle(wid)
if err != nil {
return nil
}
size := r.Size()
offset := Size{size.W / 2, size.H / 2}
return g.teleportWID(wid, Rectangle{
center.Offset(offset.Scale(-1)),
center.Offset(offset),
})
}
func (g *Grid) teleportWID(wid wmutils.WindowID, r Rectangle) error {
g.view.Unfullscreen(wid)
if !g.inGrid(r) || !r.Valid() {
return nil
}
return wmutils.Teleport(
wid,
g.pixelPosition(r.TopLeft).Offset(g.pad),
g.pixelSize(r.Size()).Add(
g.pad.Add(wmutils.Size{g.border, g.border}).Scale(-2),
),
)
}