forked from richardwilkes/webapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
window.go
213 lines (186 loc) · 6.04 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
package webapp
import (
"github.com/richardwilkes/cef/cef"
"github.com/richardwilkes/toolbox/i18n"
"github.com/richardwilkes/toolbox/log/jot"
"github.com/richardwilkes/toolbox/xmath/geom"
)
// StyleMask controls the look and capabilities of a window.
type StyleMask int
// Possible values for the StyleMask.
const (
TitledWindowMask StyleMask = 1 << iota
ClosableWindowMask
MinimizableWindowMask
ResizableWindowMask
BorderlessWindowMask = 0
StdWindowMask = TitledWindowMask | ClosableWindowMask | MinimizableWindowMask | ResizableWindowMask
)
// Window holds window information.
type Window struct {
PlatformData interface{}
Browser *cef.Browser
style StyleMask
title string
// MayCloseCallback is called when the user has requested that the window
// be closed. Return true to permit it, false to cancel the operation.
// Defaults to always returning true.
MayCloseCallback func() bool
// WillCloseCallback is called just prior to the window closing.
WillCloseCallback func()
// GainedFocus is called when the keyboard focus is gained on this window.
GainedFocus func()
// LostFocus is called when the keyboard focus is lost from this window.
LostFocus func()
}
var windowList = make([]*Window, 0)
// Count returns the number of windows that are open.
func Count() int {
return len(windowList)
}
// Windows returns a slice containing the current set of open windows.
func Windows() []*Window {
list := make([]*Window, 0, len(windowList))
copy(list, windowList)
return list
}
// KeyWindow returns the window that currently has the keyboard focus, or nil
// if none of your application's windows has the keyboard focus.
func KeyWindow() *Window {
return driver.KeyWindow()
}
// AllWindowsToFront attempts to bring all of the application's windows to the
// foreground.
func AllWindowsToFront() {
driver.BringAllWindowsToFront()
}
// NewWindow creates a new window with a webview as its content.
func NewWindow(style StyleMask, bounds geom.Rect, title, url string) (*Window, error) {
window := &Window{
style: style,
title: title,
MayCloseCallback: func() bool { return true },
WillCloseCallback: func() {},
GainedFocus: func() {},
LostFocus: func() {},
}
if err := driver.WindowInit(window, style, bounds, title); err != nil {
return nil, err
}
bounds.Size = window.WindowContentSize()
bounds.X = 0
bounds.Y = 0
window.Browser = cef.BrowserHostCreateBrowserSync(cef.NewWindowInfo(driver.WindowBrowserParent(window), bounds), newClient(), url, cef.NewBrowserSettings(), nil)
windowList = append(windowList, window)
return window, nil
}
// ToggleDevTools toggles display of the Chrome development tools window.
func (window *Window) ToggleDevTools() {
if window.Browser != nil {
host := window.Browser.GetHost()
if host.HasDevTools() != 0 {
host.CloseDevTools()
} else {
wnd := &Window{
style: StdWindowMask,
title: i18n.Text("Development Tools"),
MayCloseCallback: func() bool { return true },
WillCloseCallback: func() {},
GainedFocus: func() {},
LostFocus: func() {},
}
bounds := MainDisplay().UsableBounds
bounds.Width /= 2
if err := driver.WindowInit(wnd, StdWindowMask, bounds, wnd.title); err != nil {
jot.Error(err)
return
}
bounds.Size = wnd.WindowContentSize()
bounds.X = 0
bounds.Y = 0
host.ShowDevTools(cef.NewWindowInfo(driver.WindowBrowserParent(wnd), bounds), newClient(), cef.NewBrowserSettings(), nil)
wnd.ToFront()
}
}
}
func (window *Window) String() string {
return window.title
}
// AttemptClose closes the window if permitted.
func (window *Window) AttemptClose() {
if window.MayCloseCallback() {
window.Dispose()
}
}
// Dispose of the window.
func (window *Window) Dispose() {
for i, wnd := range windowList {
if wnd == window { //nolint:gocritic
copy(windowList[i:], windowList[i+1:])
count := len(windowList) - 1
windowList[count] = nil
windowList = windowList[:count]
break
}
}
driver.WindowClose(window)
}
// Title returns the title of this window.
func (window *Window) Title() string {
return window.title
}
// SetTitle sets the title of this window.
func (window *Window) SetTitle(title string) {
window.title = title
driver.WindowSetTitle(window, title)
}
// Bounds returns the boundaries in display coordinates of the frame of this
// window (i.e. the area that includes both the content and its border and
// window controls).
func (window *Window) Bounds() geom.Rect {
return driver.WindowBounds(window)
}
// SetBounds sets the boundaries of the frame of this window.
func (window *Window) SetBounds(bounds geom.Rect) {
driver.WindowSetBounds(window, bounds)
}
// WindowContentSize returns the size of the windoe's content area.
func (window *Window) WindowContentSize() geom.Size {
return driver.WindowContentSize(window)
}
// Focused returns true if the window has the current keyboard focus.
func (window *Window) Focused() bool {
return window == KeyWindow()
}
// ToFront attempts to bring the window to the foreground and give it the
// keyboard focus.
func (window *Window) ToFront() {
driver.WindowToFront(window)
}
// Minimize performs the platform's minimize function on the window.
func (window *Window) Minimize() {
driver.WindowMinimize(window)
}
// Zoom performs the platform's zoom function on the window.
func (window *Window) Zoom() {
driver.WindowZoom(window)
}
// Closable returns true if the window was created with the
// ClosableWindowMask.
func (window *Window) Closable() bool {
return window.style&ClosableWindowMask != 0
}
// Minimizable returns true if the window was created with the
// MinimizableWindowMask.
func (window *Window) Minimizable() bool {
return window.style&MinimizableWindowMask != 0
}
// Resizable returns true if the window was created with the
// ResizableWindowMask.
func (window *Window) Resizable() bool {
return window.style&ResizableWindowMask != 0
}
// ThemeIsDark returns true if the window theme is a dark one.
func (window *Window) ThemeIsDark() bool {
return driver.WindowThemeIsDark(window)
}