-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow.go
More file actions
73 lines (62 loc) · 1.38 KB
/
window.go
File metadata and controls
73 lines (62 loc) · 1.38 KB
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
package main
import "github.com/kybin/tor/cell"
// Window is a area that follows a cursor.
// It used for clipping text.
// It's size should same as tor's main layout size.
type Window struct {
min cell.Pt
size cell.Pt
}
func NewWindow(size cell.Pt) *Window {
w := Window{cell.Pt{0, 0}, size}
return &w
}
func (w *Window) Min() cell.Pt {
return w.min
}
func (w *Window) Max() cell.Pt {
return w.min.Add(w.size)
}
func (w *Window) Move(t cell.Pt) {
w.min = w.min.Add(t)
}
func (w *Window) Contains(c *Cursor) bool {
cp := c.Position()
if (w.Min().O <= cp.O && cp.O < w.Max().O) && (w.Min().L <= cp.L && cp.L < w.Max().L) {
return true
}
return false
}
// Follow makes Window follows to Cursor c.
// It returns true if Window is really moved, or false.
func (w *Window) Follow(c *Cursor, margin int) bool {
var tl, to int
cp := c.Position()
minl := w.Min().L + margin
maxl := w.Max().L - margin
if cp.L < minl {
tl = cp.L - minl
} else if cp.L >= maxl {
tl = cp.L - maxl + 1
}
// tl should not smaller than -w.Min().l
if tl < -w.Min().L {
tl = -w.Min().L
}
mino := w.Min().O + margin
maxo := w.Max().O - margin
if cp.O < mino {
to = cp.O - mino
} else if cp.O >= maxo {
to = cp.O - maxo + 1
}
// to should not smaller than -w.Min().o
if to < -w.Min().O {
to = -w.Min().O
}
if tl == 0 && to == 0 {
return false
}
w.Move(cell.Pt{tl, to})
return true
}