-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathminwin.go
More file actions
570 lines (498 loc) · 17.1 KB
/
minwin.go
File metadata and controls
570 lines (498 loc) · 17.1 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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause
//go:build windows
// +build windows
package walk
import (
"fmt"
"os"
"unsafe"
"github.com/tailscale/win"
"golang.org/x/sys/windows"
)
const minWinClassName = "Walk MinWin"
var (
minWinProcCb uintptr
minWins = map[*MinWin]struct{}{} // Set of all MinWin instances that are currently associated with valid HWNDs.
)
// MinWinType is an enumeration specifying the type of the MinWin window.
type MinWinType uint
const (
MinWinTypeTopLevel MinWinType = iota // A top-level window with title bar. Must be initialized using [MinWinTopLevelOptions].
MinWinTypePopup // An ephemeral pop-up window (think menu, tooltip, or volume control).
MinWinTypeChild // A child window hosted within a parent window. Child windows must have a parent window.
MinWinTypeZeroSize // An invisible container used for hosting a parent XAML islands that spawn UI.
MinWinTypeMessageOnly // An invisible window that only processes messages and does not display content. Message-only windows cannot have a parent/owner.
)
// MinWinOptions specifies the common configuration values across all types of MinWin.
type MinWinOptions struct {
Type MinWinType // The type of the MinWin. If Type is [MinWinTypeTopLevel], the MinWinOptions must be embedded within a [MinWinTopLevelOptions] struct.
ParentOrOwner Win32Window // The parent or owner window. May be nil unless Type is [MinWinTypeChild].
Title string // The window text of the MinWin. When type is [MinWinTypeTopLevel], this text appears on the window's title bar.
BoundsPx Rectangle // The bounds of the MinWin in native pixels.
Size Size // The size of the MinWin at 100% DPI (ignored if BoundsPx is specified).
Centered bool // The MinWin is centered within the monitor when created.
Disabled bool // The MinWin is initially disabled.
NoDWMCompositing bool // The MinWin needs a redirection surface for painting its content.
Visible bool // The MinWin is initially visible.
}
// MinWinTopLevelOptions specifies options specific to windows of type [MinWinTypeTopLevel].
type MinWinTopLevelOptions struct {
MinWinOptions
AlwaysOnTop bool // The MinWin will be initialized as an always-on-top window.
NoMaximize bool // The MinWin will omit its maximize button.
NoMinimize bool // The MinWin will omit its minimize button.
NoResize bool // The MinWin will not be resizable by the user.
NoCaption bool // The MinWin will be initialized without a caption (implies NoSysmenu).
NoSysmenu bool // The MinWin will be initialized without a title bar icon.
SolidSurface bool // The MinWin will be drawn with a solid background surface provided by DWM.
}
// MinWin implements a minimal API for managing windows that host XAML islands.
// Because its windows are mere hosts for other content, MinWins do not paint
// any content themselves.
type MinWin struct {
Win32WindowImpl
minWinType MinWinType
activatePublisher EventPublisher
createPublisher ProceedEventPublisher
destroyPublisher EventPublisher
deactivatePublisher EventPublisher
dpiChangedPublisher IntEventPublisher
movePublisher GenericEventPublisher[Point]
sizePublisher GenericEventPublisher[Size]
textChangedPublisher GenericEventPublisher[string]
visibilityChangedPublisher GenericEventPublisher[bool]
}
type minWinCreateContext struct {
mw *MinWin
err error // Error to return out of CreateWindowEx if WM_NCCREATE or WM_CREATE fails.
size Size // Desired size at 100% DPI.
doSize bool // Resize the window during WM_CREATE.
doCenter bool // Center the window during WM_CREATE.
solidSurface bool // Use DWM APIs to extend the window frame to cover the entire client area.
}
// MinWinOptionTypes is a type constraint limiting an argument to be either
// a [MinWinOptions] or a [MinWinTopLevelOptions].
type MinWinOptionTypes interface {
MinWinOptions | MinWinTopLevelOptions
}
// InitMinWin initializes an already allocated MinWin structure according to
// the options supplied by opts.
//
// It returns an error if some combination of options are invalid for the
// requested [MinWinType].
func InitMinWin[O MinWinOptionTypes](mw *MinWin, opts O) error {
App().AssertUIThread()
if mw == nil {
return os.ErrInvalid
}
var mainOpts *MinWinOptions
var topLevelOpts *MinWinTopLevelOptions
switch v := any(opts).(type) {
case MinWinOptions:
mainOpts = &v
case MinWinTopLevelOptions:
mainOpts = &v.MinWinOptions
topLevelOpts = &v
}
if mainOpts.Type == MinWinTypeTopLevel && topLevelOpts == nil {
return fmt.Errorf("%w: MinWinTopLevelOptions must be provided to create a top-level MinWin", os.ErrInvalid)
}
if mainOpts.Type == MinWinTypeMessageOnly && mainOpts.ParentOrOwner != nil {
return fmt.Errorf("%w: message-only MinWin must not have a parent or owner", os.ErrInvalid)
}
if mainOpts.Type == MinWinTypeChild && mainOpts.ParentOrOwner == nil {
return fmt.Errorf("%w: child MinWin must have a parent", os.ErrInvalid)
}
className, err := registerMinWinClass()
if err != nil {
return err
}
title, err := windows.UTF16PtrFromString(mainOpts.Title)
if err != nil {
return err
}
var style, exStyle uint32
if mainOpts.Disabled {
style |= win.WS_DISABLED
}
if mainOpts.Visible {
style |= win.WS_VISIBLE
}
if !mainOpts.NoDWMCompositing || mainOpts.Type == MinWinTypeMessageOnly {
exStyle |= win.WS_EX_NOREDIRECTIONBITMAP
}
switch mainOpts.Type {
case MinWinTypeTopLevel:
style |= win.WS_OVERLAPPEDWINDOW
if topLevelOpts.AlwaysOnTop {
exStyle |= win.WS_EX_TOPMOST
}
if topLevelOpts.NoMaximize {
style &^= win.WS_MAXIMIZEBOX
}
if topLevelOpts.NoMinimize {
style &^= win.WS_MINIMIZEBOX
}
if topLevelOpts.NoResize {
style &^= win.WS_THICKFRAME
}
if topLevelOpts.NoSysmenu {
style &^= win.WS_SYSMENU
}
if topLevelOpts.NoCaption {
// Implies NoSysmenu
style &^= win.WS_CAPTION | win.WS_SYSMENU
}
case MinWinTypeChild:
style |= win.WS_CHILD
exStyle |= win.WS_EX_CONTROLPARENT
case MinWinTypePopup, MinWinTypeZeroSize:
style |= win.WS_POPUP
}
mw.Win32WindowImpl.defWindowProc = win.DefWindowProc
createCtx := minWinCreateContext{
mw: mw,
size: mainOpts.Size,
doSize: mainOpts.BoundsPx.IsZero() && !mainOpts.Size.IsZero() && mainOpts.ParentOrOwner == nil, // The caller specified a size but we don't know which monitor we're going to be on until WM_CREATE.
doCenter: mainOpts.BoundsPx.IsZero() && mainOpts.Centered && mainOpts.ParentOrOwner == nil, // The caller requested that we center but we don't know which monitor we're going to be on until WM_CREATE.
solidSurface: topLevelOpts != nil && topLevelOpts.SolidSurface,
}
var x, y, w, h int32
switch {
case mainOpts.Type == MinWinTypeZeroSize:
// Just break, leaving x, y, w, h as zero values.
case !mainOpts.BoundsPx.IsZero():
x = int32(mainOpts.BoundsPx.X)
y = int32(mainOpts.BoundsPx.Y)
w = int32(mainOpts.BoundsPx.Width)
h = int32(mainOpts.BoundsPx.Height)
case !mainOpts.Size.IsZero() && mainOpts.ParentOrOwner != nil:
// Since we have a parent, we can position prior to calling CreateWindowEx
// because we're going to reside on the same monitor as our parent.
parent := mainOpts.ParentOrOwner
dpi := parent.DPI()
sizePx := SizeFrom96DPI(mainOpts.Size, dpi)
w = int32(sizePx.Width)
h = int32(sizePx.Height)
if mainOpts.Centered {
var parentRc win.RECT
if !win.GetWindowRect(parent.Handle(), &parentRc) {
x = win.CW_USEDEFAULT
y = win.CW_USEDEFAULT
break
}
cx := int32(sizePx.Width)
cy := int32(sizePx.Height)
if cx > parentRc.Width() || cy > parentRc.Height() {
wa := parent.Monitor().WorkArea()
// Clamp size to work area
cx = min(cx, int32(wa.Width))
cy = min(cy, int32(wa.Height))
}
x = parentRc.Left + ((parentRc.Width() - cx) / 2)
y = parentRc.Top + ((parentRc.Height() - cy) / 2)
w = cx
h = cy
} else {
x = win.CW_USEDEFAULT
y = win.CW_USEDEFAULT
}
default:
x = win.CW_USEDEFAULT
y = win.CW_USEDEFAULT
w = win.CW_USEDEFAULT
h = win.CW_USEDEFAULT
}
var parentHWND win.HWND
if mainOpts.ParentOrOwner != nil {
parentHWND = mainOpts.ParentOrOwner.Handle()
} else if mainOpts.Type == MinWinTypeMessageOnly {
parentHWND = win.HWND_MESSAGE
}
hwnd := win.CreateWindowEx(
exStyle,
className,
title,
style,
x,
y,
w,
h,
parentHWND,
0, // HMENU
0, // HINSTANCE
unsafe.Pointer(&createCtx),
)
if hwnd == 0 {
if createCtx.err == nil {
return lastError("CreateWindowEx")
}
return createCtx.err
}
mw.minWinType = mainOpts.Type
return nil
}
func (mw *MinWin) Dispose() {
if mw.hWnd != 0 {
win.DestroyWindow(mw.hWnd)
}
}
// Activated returns the event that will be published when mw is activated.
func (mw *MinWin) Activated() *Event {
return mw.activatePublisher.Event()
}
// Created returns the event that will be published when mw is created.
// Event handlers should return true to allow the window to finish its creation.
func (mw *MinWin) Created() *ProceedEvent {
return mw.createPublisher.Event()
}
// Deactivated returns the event that will be published when mw is deactivated.
func (mw *MinWin) Deactivated() *Event {
return mw.deactivatePublisher.Event()
}
// Destroyed returns the event that will be published when mw is destroyed.
func (mw *MinWin) Destroyed() *Event {
return mw.destroyPublisher.Event()
}
// DPIChanged returns the event that will be published when mw's DPI has been
// changed. The handler's argument contains the new DPI.
func (mw *MinWin) DPIChanged() *IntEvent {
return mw.dpiChangedPublisher.Event()
}
// Moved returns the event that will be published when mw is moved. The
// handler's argument contains the new screen coordinates of mw's top-left corner.
func (mw *MinWin) Moved() *GenericEvent[Point] {
return mw.movePublisher.Event()
}
// Sized returns the event that will be published when mw is resized. The
// handler's argument contains mw's new size at 100% DPI.
func (mw *MinWin) Sized() *GenericEvent[Size] {
return mw.sizePublisher.Event()
}
// TextChanged returns the event that will be published when mw's text has
// changed. The handler's argument contains the new window text.
func (mw *MinWin) TextChanged() *GenericEvent[string] {
return mw.textChangedPublisher.Event()
}
// Type returns the [MinWinType] used for creating mw.
func (mw *MinWin) Type() MinWinType {
return mw.minWinType
}
// VisibilityChanged returns the event that will be published when mw's
// visibility changes. The handler's argument is true for visible, false for
// hidden.
func (mw *MinWin) VisibilityChanged() *GenericEvent[bool] {
return mw.visibilityChangedPublisher.Event()
}
// Show makes mw visible.
func (mw *MinWin) Show() {
win.ShowWindow(mw.hWnd, win.SW_SHOW)
}
// Hide hides mw.
func (mw *MinWin) Hide() {
win.ShowWindow(mw.hWnd, win.SW_HIDE)
}
// SetText sets mw's window text to the value of text. For top-level windows,
// this is the text that appears in the title bar.
func (mw *MinWin) SetText(text string) error {
text16, err := windows.UTF16PtrFromString(text)
if err != nil {
return err
}
return win.SetWindowText(mw.hWnd, text16)
}
// Text retrieves mw's window text.
func (mw *MinWin) Text() (string, error) {
win.SetLastError(0)
lenExclNul := win.GetWindowTextLength(mw.hWnd)
if lenExclNul == 0 {
if e := win.GetLastError(); e != 0 {
return "", windows.Errno(e)
}
}
lenInclNul := lenExclNul + 1
buf := make([]uint16, lenInclNul)
win.SetLastError(0)
actualLenExclNul := win.GetWindowText(mw.hWnd, unsafe.SliceData(buf), lenInclNul)
if actualLenExclNul == 0 {
if e := win.GetLastError(); e != 0 {
return "", windows.Errno(e)
}
}
return windows.UTF16ToString(buf[:actualLenExclNul]), nil
}
// SetEnabled enables or disables mw depending on the value of enable.
func (mw *MinWin) SetEnabled(enable bool) {
win.EnableWindow(mw.hWnd, enable)
}
func (mw *MinWin) setUserData() error {
win.SetLastError(0)
prev := win.SetWindowLongPtr(mw.hWnd, win.GWLP_USERDATA, uintptr(unsafe.Pointer(mw)))
if prev != 0 {
return nil
}
if le := win.GetLastError(); le != 0 {
return windows.Errno(le)
}
return nil
}
func resolveMinWin(hwnd win.HWND) *MinWin {
val := win.GetWindowLongPtr(hwnd, win.GWLP_USERDATA)
return (*MinWin)(unsafe.Pointer(val))
}
func minWinProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr) uintptr {
defer appSingleton.HandlePanicFromNativeCallback()
if msg == win.WM_NCCREATE {
cs := (*win.CREATESTRUCT)(unsafe.Pointer(lParam))
createCtx := (*minWinCreateContext)(unsafe.Pointer(cs.CreateParams))
if createCtx == nil || createCtx.mw == nil {
return 0
}
mw := createCtx.mw
mw.hWnd = hwnd
if err := mw.setUserData(); err != nil {
createCtx.err = err
return 0
}
minWins[mw] = struct{}{}
return mw.defWindowProc(hwnd, msg, wParam, lParam)
} else if mw := resolveMinWin(hwnd); mw != nil {
return mw.WndProc(hwnd, msg, wParam, lParam)
}
return win.DefWindowProc(hwnd, msg, wParam, lParam)
}
func (mw *MinWin) WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr) uintptr {
switch msg {
case win.WM_CREATE:
cs := (*win.CREATESTRUCT)(unsafe.Pointer(lParam))
createCtx := (*minWinCreateContext)(unsafe.Pointer(cs.CreateParams))
if createCtx == nil {
return ^uintptr(0)
}
if doSize, doCenter := createCtx.doSize, createCtx.doCenter; doSize || doCenter {
swpFlags := uint32(win.SWP_NOZORDER | win.SWP_NOACTIVATE)
var sizePx win.SIZE
if doSize {
sizePx = SizeFrom96DPI(createCtx.size, mw.DPI()).toSIZE()
} else {
swpFlags |= win.SWP_NOSIZE
}
if doCenter && createCtx.size.IsZero() {
// We need the size to compute position even if we're not resizing.
var rc win.RECT
if win.GetWindowRect(hwnd, &rc) {
sizePx.CX = rc.Width()
sizePx.CY = rc.Height()
}
}
mon := mw.Monitor()
wa := mon.WorkArea()
cx := min(sizePx.CX, int32(wa.Width))
cy := min(sizePx.CY, int32(wa.Height))
var x, y int32
if doCenter {
x = int32(wa.X) + ((int32(wa.Width) - cx) / 2)
y = int32(wa.Y) + ((int32(wa.Height) - cy) / 2)
} else {
swpFlags |= win.SWP_NOMOVE
}
win.SetWindowPos(hwnd, 0, x, y, cx, cy, swpFlags)
}
if createCtx.solidSurface {
mw.SetSolidSurface()
}
if !mw.createPublisher.Publish() {
return ^uintptr(0)
}
return 0
case win.WM_ACTIVATE:
if wParam > 0 {
mw.activatePublisher.Publish()
} else {
mw.deactivatePublisher.Publish()
}
return 0
case win.WM_ERASEBKGND:
return 0
case win.WM_DPICHANGED:
dpi := int(win.LOWORD(uint32(wParam)))
mw.dpiChangedPublisher.Publish(dpi)
newRect := (*win.RECT)(unsafe.Pointer(lParam))
win.SetWindowPos(
hwnd,
0,
newRect.Left,
newRect.Top,
newRect.Width(),
newRect.Height(),
win.SWP_NOACTIVATE|win.SWP_NOZORDER,
)
return 0
case win.WM_DESTROY:
mw.destroyPublisher.Publish()
return 0
case win.WM_NCDESTROY:
mw.hWnd = 0
delete(minWins, mw)
case win.WM_WINDOWPOSCHANGED:
wp := (*win.WINDOWPOS)(unsafe.Pointer(lParam))
if (wp.Flags & win.SWP_SHOWWINDOW) != 0 {
mw.visibilityChangedPublisher.Publish(true)
} else if (wp.Flags & win.SWP_HIDEWINDOW) != 0 {
mw.visibilityChangedPublisher.Publish(false)
}
if (wp.Flags & win.SWP_NOMOVE) == 0 {
mw.movePublisher.Publish(Point{X: int(wp.X), Y: int(wp.X)})
}
if (wp.Flags & win.SWP_NOSIZE) == 0 {
sizePx := Size{Width: int(wp.Cx), Height: int(wp.Cy)}
mw.sizePublisher.Publish(SizeTo96DPI(sizePx, mw.DPI()))
}
return 0
case win.WM_SETTEXT:
result := mw.defWindowProc(hwnd, msg, wParam, lParam)
if result != 0 {
newText := windows.UTF16PtrToString((*uint16)(unsafe.Pointer(lParam)))
mw.textChangedPublisher.Publish(newText)
}
return result
}
return mw.defWindowProc(hwnd, msg, wParam, lParam)
}
func registerMinWinClass() (className16 *uint16, err error) {
className16, err = windows.UTF16PtrFromString(minWinClassName)
if err != nil {
return nil, err
}
if registeredWindowClasses[minWinClassName] != nil {
return className16, nil
}
if minWinProcCb == 0 {
minWinProcCb = windows.NewCallback(minWinProc)
}
wc := win.WNDCLASSEX{
CbSize: uint32(unsafe.Sizeof(win.WNDCLASSEX{})),
Style: win.CS_DBLCLKS, // We don't bother with redrawing since we're just a host window.
LpfnWndProc: minWinProcCb,
HCursor: win.LoadCursor(0, win.MAKEINTRESOURCE(win.IDC_ARROW)),
LpszClassName: className16,
}
if win.RegisterClassEx(&wc) == 0 {
return nil, lastError("RegisterClassEx")
}
registeredWindowClasses[minWinClassName] = newWndClassInfo()
return className16, err
}
// SetFocus sets keyboard focus to focusTo, a window contained within topLevel.
// It tries to respect walk focus-setting conventions when possible, otherwise
// it falls back to calling [win.SetFocus] directly.
func (mw *MinWin) SetFocus(topLevel, focusTo win.HWND) {
if form, ok := windowFromHandle(topLevel).(Form); ok {
if focusToWindow := windowFromHandle(focusTo); focusToWindow != nil {
form.SetFocusToWindow(focusToWindow)
return
}
}
win.SetFocus(focusTo)
}