-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcols.go
128 lines (106 loc) · 2.21 KB
/
cols.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
package main
import (
"image"
"image/draw"
"github.com/aarzilli/yacco/config"
)
type Cols struct {
cols []*Col
wnd *Window
b draw.Image
r image.Rectangle
}
const COL_MIN_WIDTH = 40
func NewCols(wnd *Window, r image.Rectangle) *Cols {
return &Cols{[]*Col{}, wnd, wnd.img, r}
}
func (cs *Cols) SetRects(wnd *Window, b draw.Image, r image.Rectangle) {
cs.wnd = wnd
cs.r = r
cs.b = b
cs.RecalcRects()
}
// Create a new column after the specified one, specify -1 to create a new column at the end
func (cs *Cols) AddAfter(c *Col, n int, f float64) *Col {
//c := NewCol(cs.wnd, cs.r)
if len(cs.cols) == 0 {
cs.cols = append(cs.cols, c)
} else {
if n < 0 {
n = len(cs.cols) - 1
}
c.frac = cs.cols[n].frac * f
cs.cols[n].frac -= c.frac
cs.cols = append(cs.cols, nil)
copy(cs.cols[n+2:], cs.cols[n+1:])
cs.cols[n+1] = c
}
cs.RecalcRects()
cs.Redraw()
return c
}
func (cs *Cols) RecalcRects() {
w := cs.r.Max.X - cs.r.Min.X
minimizedw := 0
lastNonminimized := -1
totalFrac := 0.0001
for i := range cs.cols {
ew := int(cs.cols[i].frac / 10 * float64(w))
if ew < COL_MIN_WIDTH {
minimizedw += COL_MIN_WIDTH
} else {
lastNonminimized = i
totalFrac += cs.cols[i].frac
}
}
x := cs.r.Min.X
w -= minimizedw
remw := w
for i := range cs.cols {
var curw int
ew := int(cs.cols[i].frac / totalFrac * float64(w))
if ew < COL_MIN_WIDTH {
curw = COL_MIN_WIDTH
} else if i == lastNonminimized {
curw = remw
remw = 0
} else {
curw = ew
remw -= curw
}
r := cs.r
r.Min.X = x
r.Max.X = x + curw
x += curw
cs.cols[i].SetRects(cs.wnd, cs.b, cs.r.Intersect(r), i == (len(cs.cols)-1))
}
}
func (cs *Cols) Redraw() {
if len(cs.cols) <= 0 {
draw.Draw(cs.b, cs.r, &config.TheColorScheme.WindowBG, cs.r.Min, draw.Src)
}
for _, c := range cs.cols {
c.Redraw()
}
}
func (cs *Cols) IndexOf(c *Col) int {
for i := range cs.cols {
if cs.cols[i] == c {
return i
}
}
return -1
}
func (cs *Cols) Remove(i int) {
if i > 0 {
cs.cols[i-1].frac += cs.cols[i].frac
} else { // i == 0
if len(cs.cols) > 0 {
cs.cols[1].frac += cs.cols[0].frac
}
}
copy(cs.cols[i:], cs.cols[i+1:])
cs.cols = cs.cols[:len(cs.cols)-1]
cs.RecalcRects()
cs.Redraw()
}