-
Notifications
You must be signed in to change notification settings - Fork 0
/
cursor.go
169 lines (154 loc) · 3.98 KB
/
cursor.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
// Copyright (c) 2023, The GoKi Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package texteditor
import (
"fmt"
"image"
"image/draw"
"sync"
"time"
"goki.dev/gi/v2/gi"
"goki.dev/girl/states"
"goki.dev/ki/v2"
"goki.dev/mat32/v2"
"goki.dev/pi/v2/lex"
)
// ViewBlinkMu is mutex protecting ViewBlink updating and access
var ViewBlinkMu sync.Mutex
// ViewBlinker is the time.Ticker for blinking cursors for text fields,
// only one of which can be active at at a time
var ViewBlinker *time.Ticker
// BlinkingView is the text field that is blinking
var BlinkingView *Editor
// ViewSpriteName is the name of the window sprite used for the cursor
var ViewSpriteName = "texteditor.Editor.Cursor"
// ViewBlink is function that blinks text field cursor
func ViewBlink() {
for {
ViewBlinkMu.Lock()
if ViewBlinker == nil {
ViewBlinkMu.Unlock()
return // shutdown..
}
ViewBlinkMu.Unlock()
<-ViewBlinker.C
ViewBlinkMu.Lock()
if BlinkingView == nil || BlinkingView.This() == nil || BlinkingView.Is(ki.Deleted) {
ViewBlinkMu.Unlock()
continue
}
ed := BlinkingView
if ed.Sc == nil || !ed.StateIs(states.Focused) || !ed.This().(gi.Widget).IsVisible() {
ed.RenderCursor(false)
BlinkingView = nil
ViewBlinkMu.Unlock()
continue
}
ed.BlinkOn = !ed.BlinkOn
ed.RenderCursor(ed.BlinkOn)
ViewBlinkMu.Unlock()
}
}
// StartCursor starts the cursor blinking and renders it
func (ed *Editor) StartCursor() {
if ed == nil || ed.This() == nil {
return
}
if !ed.This().(gi.Widget).IsVisible() {
return
}
ed.BlinkOn = true
if gi.CursorBlinkTime == 0 {
ed.RenderCursor(true)
return
}
ViewBlinkMu.Lock()
if ViewBlinker == nil {
ViewBlinker = time.NewTicker(gi.CursorBlinkTime)
go ViewBlink()
}
ed.BlinkOn = true
ed.RenderCursor(true)
BlinkingView = ed
ViewBlinkMu.Unlock()
}
// StopCursor stops the cursor from blinking
func (ed *Editor) StopCursor() {
if ed == nil || ed.This() == nil {
return
}
if !ed.This().(gi.Widget).IsVisible() {
return
}
ed.RenderCursor(false)
ViewBlinkMu.Lock()
if BlinkingView == ed {
BlinkingView = nil
}
ViewBlinkMu.Unlock()
}
// CursorBBox returns a bounding-box for a cursor at given position
func (ed *Editor) CursorBBox(pos lex.Pos) image.Rectangle {
cpos := ed.CharStartPos(pos)
cbmin := cpos.SubScalar(ed.CursorWidth.Dots)
cbmax := cpos.AddScalar(ed.CursorWidth.Dots)
cbmax.Y += ed.FontHeight
curBBox := image.Rectangle{cbmin.ToPointFloor(), cbmax.ToPointCeil()}
return curBBox
}
// RenderCursor renders the cursor on or off, as a sprite that is either on or off
func (ed *Editor) RenderCursor(on bool) {
if ed == nil || ed.This() == nil {
return
}
if !ed.This().(gi.Widget).IsVisible() {
return
}
if ed.Renders == nil {
return
}
ed.CursorMu.Lock()
defer ed.CursorMu.Unlock()
sp := ed.CursorSprite(on)
if sp == nil {
return
}
sp.Geom.Pos = ed.CharStartPos(ed.CursorPos).ToPointFloor()
}
// CursorSpriteName returns the name of the cursor sprite
func (ed *Editor) CursorSpriteName() string {
spnm := fmt.Sprintf("%v-%v", ViewSpriteName, ed.FontHeight)
return spnm
}
// CursorSprite returns the sprite for the cursor, which is
// only rendered once with a vertical bar, and just activated and inactivated
// depending on render status.
func (ed *Editor) CursorSprite(on bool) *gi.Sprite {
sc := ed.Sc
if sc == nil {
return nil
}
ms := sc.MainStage()
if ms == nil {
return nil // only MainStage has sprites
}
spnm := ed.CursorSpriteName()
sp, ok := ms.Sprites.SpriteByName(spnm)
if !ok {
bbsz := image.Point{int(mat32.Ceil(ed.CursorWidth.Dots)), int(mat32.Ceil(ed.FontHeight))}
if bbsz.X < 2 { // at least 2
bbsz.X = 2
}
sp = gi.NewSprite(spnm, bbsz, image.Point{})
ibox := sp.Pixels.Bounds()
draw.Draw(sp.Pixels, ibox, &image.Uniform{ed.CursorColor.Solid}, image.Point{}, draw.Src)
ms.Sprites.Add(sp)
}
if on {
ms.Sprites.ActivateSprite(sp.Name)
} else {
ms.Sprites.InactivateSprite(sp.Name)
}
return sp
}