-
Notifications
You must be signed in to change notification settings - Fork 0
/
window.go
358 lines (300 loc) · 8.78 KB
/
window.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package dull
import (
"github.com/pekim/dull/geometry"
"math"
"time"
"github.com/go-gl/gl/v3.3-core/gl"
"github.com/go-gl/glfw/v3.2/glfw"
"github.com/pekim/dull/internal/font"
"github.com/pkg/errors"
)
const defaultFontSize = 16
const fontZoomDelta = 0.75
type keybinding struct {
key Key
mods ModifierKey
fn func()
}
// Window represents an X window.
//
// Use Application.NewWindow to create a Window.
type Window struct {
*Application
dpi float32
scale float64
fontSize float64
fontFamily *font.Family
glfwWindow *glfw.Window
glTerminated bool
program uint32
lastRenderDuration time.Duration
windowedBounds geometry.Rect
keybindings []keybinding
// When true char event callbacks will not be called.
// Used to prevent char events associated with window key binding from
// being emitted.
//
// For example consider CTRL+0 (to reset zoom)
// key down : CTRL
// key down : 0 // zoom reset processed
// char : "0" // do not want "0" handled; block it
// key up : 0
// key up : CTRL
blockCharEvents bool
bg Color
fg Color
bgDirty bool
width int
height int
viewportCellHeightPixel int
viewportCellWidthPixel int
viewportCellHeight float32
viewportCellWidth float32
borders *Borders
cursors *Cursors
grid *CellGrid
vertices []float32
gridSizeCallback GridSizeCallback
keyCallback KeyCallback
charCallback CharCallback
}
// WindowOptions is used when creating new windows to provide
// some initial window values.
type WindowOptions struct {
Width int // initial window width, in pixels
Height int // initial window height, in pixels
Bg *Color // default background color for cells
Fg *Color // default foreground color for cells
}
func (o *WindowOptions) applyDefaults() {
if o.Width == 0 {
o.Width = 800
}
if o.Height == 0 {
o.Height = 600
}
if o.Bg == nil {
color := NewColor(0.0, 0.0, 0.0, 1.0) // black
o.Bg = &color
}
if o.Fg == nil {
color := NewColor(1.0, 1.0, 1.0, 1.0) // white
o.Fg = &color
}
}
func newWindow(application *Application, options *WindowOptions) (*Window, error) {
if options == nil {
options = &WindowOptions{}
}
options.applyDefaults()
w := &Window{
Application: application,
bg: *options.Bg,
fg: *options.Fg,
bgDirty: true,
fontSize: defaultFontSize,
}
w.borders = NewBorders()
w.grid = NewCellGrid(0, 0, w.bg, w.fg)
w.cursors = NewCursors(w)
w.setKeybindings()
err := w.createWindow(options)
if err != nil {
return nil, err
}
err = w.glInit()
if err != nil {
return nil, err
}
w.dpi, w.scale = w.getDpiAndScale()
w.setFontSize(0)
w.glfwWindow.SetKeyCallback(w.callKeyCallback)
w.glfwWindow.SetCharModsCallback(w.callCharCallback)
w.glfwWindow.SetSizeCallback(func(_ *glfw.Window, width, height int) {
w.resized()
})
w.glfwWindow.SetRefreshCallback(func(_ *glfw.Window) {
w.drawAll()
})
return w, nil
}
func (w *Window) createWindow(options *WindowOptions) error {
glfw.WindowHint(glfw.ContextVersionMajor, 3)
glfw.WindowHint(glfw.ContextVersionMinor, 3)
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)
glfw.WindowHint(glfw.Resizable, glfw.True)
glfw.WindowHint(glfw.Visible, glfw.False)
glfwWindow, err := glfw.CreateWindow(options.Width, options.Height, "", nil, nil)
if err != nil {
return errors.Wrap(err, "Failed to create window")
}
w.glfwWindow = glfwWindow
return nil
}
func (w *Window) glInit() error {
w.glfwWindow.MakeContextCurrent()
err := gl.Init()
if err != nil {
return errors.Wrap(err, "Failed to initialise OpenGL")
}
// Swap buffers immediately when requested.
// Avoids flickering and jumping of content, such as when resizing the window.
glfw.SwapInterval(0)
w.program, err = newProgram()
if err != nil {
return errors.Wrap(err, "Failed to create gl program")
}
gl.Enable(gl.BLEND)
gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
return nil
}
func (*Window) getDpiAndScale() (float32, float64) {
monitor := glfw.GetPrimaryMonitor()
mode := monitor.GetVideoMode()
widthMm, _ := monitor.GetPhysicalSize()
dpi := float32(mode.Width) / float32(widthMm) * 25.4
// Round down, to limit excesive scaling on high dpi screens.
scale := math.Floor(float64(dpi / 96))
// Ensure scaling is never less 1.
scale = math.Max(scale, 1.0)
return dpi, scale
}
func (w *Window) setFontSize(delta float64) {
w.fontSize += delta
w.fontFamily = font.NewFamily(w.Application.fontRenderer.new(), int(w.dpi), w.scale*w.fontSize)
w.setResizeIncrement()
w.resized()
}
// Grid is the grid of Cells.
//
// The Cells in the grid may be manipulated in a callback.
func (w *Window) Grid() *CellGrid {
return w.grid
}
// Borders are rectangular borders that may be displayed around
// a rectangle of cells.
//
// The borders may be manipulated in a callback.
func (w *Window) Borders() *Borders {
return w.borders
}
// Cursors are cursors that may be displayed in a cell.
//
// The cursors may be manipulated in a callback.
func (w *Window) Cursors() *Cursors {
return w.cursors
}
// Show makes the window visible.
// See also Hide.
//
// This function may only be called from the main thread.
func (w *Window) Show() {
w.glfwWindow.Show()
w.drawAll()
}
// Hide hides the window.
// It does not destroy the window, and the window may be made
// visible again by calling Show.
//
// This function may only be called from the main thread.
func (w *Window) Hide() {
w.glfwWindow.Hide()
w.draw()
}
// SetPosition sets the position, in screen coordinates, of the upper-left
// corner of the client area of the window.
//
// It is very rarely a good idea to move an already visible window, as it will
// confuse and annoy the user.
//
// The window manager may put limits on what positions are allowed.
//
// This function may only be called from the main thread.
func (w *Window) SetPosition(top, left int) {
w.glfwWindow.SetPos(top, left)
}
// SetTitle sets the window title.
//
// This function may only be called from the main thread.
func (w *Window) SetTitle(title string) {
w.glfwWindow.SetTitle(title)
}
// Destroy destroys the window, and removes it from the Application.
//
// This function may only be called from the main thread.
func (w *Window) Destroy() {
w.glTerminated = true
w.removeWindow(w)
w.glfwWindow.Destroy()
glfw.PostEmptyEvent()
}
func (w *Window) resized() {
w.width, w.height = w.glfwWindow.GetSize()
if w.width == 0 || w.height == 0 {
return
}
w.glfwWindow.MakeContextCurrent()
gl.Viewport(0, 0, int32(w.width), int32(w.height))
w.viewportCellWidthPixel = w.fontFamily.CellWidth
w.viewportCellHeightPixel = w.fontFamily.CellHeight
if w.viewportCellWidthPixel == 0 || w.viewportCellHeightPixel == 0 {
return
}
w.viewportCellWidth = float32(w.fontFamily.CellWidth) / float32(w.width) * 2
w.viewportCellHeight = float32(w.fontFamily.CellHeight) / float32(w.height) * 2
columns := w.width / w.viewportCellWidthPixel
rows := w.height / w.viewportCellHeightPixel
w.grid = NewCellGrid(columns, rows, w.bg, w.fg)
w.callGridSizeCallback()
w.drawAll()
}
func (w *Window) drawAll() {
w.bgDirty = true
w.grid.markAllDirty()
w.draw()
}
// Do is used to make updates to cells, and have the changes
// drawn to the window.
// Make all of the cell updates in the callback function,
// which will run on the main thread.
//
// Threading and synchronisation issues are taken care off.
// As this results in some small overheads, take care that
// batches of changes are made in a single use of Do.
// This will also avoid a brief appearance of a partial set of changes.
// Take care to avoid any long running or blocking
// operations in the callback function.
//
// Does not block; it does not wait for the function fn to run.
func (w *Window) Do(fn func()) {
DoNoWait(func() {
fn()
w.draw()
})
}
// LastRenderDuration returns the duration of the last render of cells.
// It is provided for informational purpose only.
func (w *Window) LastRenderDuration() time.Duration {
return w.lastRenderDuration
}
func (w *Window) setKeybindings() {
w.keybindings = []keybinding{
// zoom
{key: Key0, mods: ModControl, fn: w.windowZoomReset},
{key: KeyKP0, mods: ModControl, fn: w.windowZoomReset},
{key: KeyEqual, mods: ModControl, fn: w.windowZoomIn},
{key: KeyKPAdd, mods: ModControl, fn: w.windowZoomIn},
{key: KeyMinus, mods: ModControl, fn: w.windowZoomOut},
{key: KeyKPSubtract, mods: ModControl, fn: w.windowZoomOut},
// fullscreen
{key: KeyF, mods: ModAlt | ModControl, fn: w.ToggleFullscreen},
{key: KeyF11, mods: 0, fn: w.ToggleFullscreen},
}
}
func (w *Window) GetClipboard() (string, error) {
return w.glfwWindow.GetClipboardString()
}
func (w *Window) SetClipboard(text string) {
w.glfwWindow.SetClipboardString(text)
}