-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.go
More file actions
250 lines (211 loc) · 5.24 KB
/
Copy pathapp.go
File metadata and controls
250 lines (211 loc) · 5.24 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
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 didvc
//go:build windows
package main
const (
controlClassName = "ScreenMaskControl"
panelWidth = int32(300)
handleSize = int32(8) // resize-handle hit radius in canvas px
)
// dragMode describes what an in-progress left-drag on the canvas is doing.
type dragMode int
const (
dragNone dragMode = iota
dragCreate
dragMove
dragResize
)
// App holds all mutable state for the single control window instance.
type App struct {
hInstance HINSTANCE
hwnd HWND
// virtual desktop bounds in real pixels
vsX, vsY, vsW, vsH int32
masks []*Mask
selected int // index into masks, -1 for none
// canvas placement (client coords) computed each layout
canvas RECT
scale float64 // canvas px per desktop px
// active drag
drag dragMode
dragStartX int32 // desktop px
dragStartY int32
dragOrigX int32 // mask origin at drag start
dragOrigY int32
dragOrigW int32
dragOrigH int32
resizeEdge int // bitmask: 1=left 2=top 4=right 8=bottom
// panel opacity slider drag
opacityDrag bool
// fonts/brushes cached
fontUI HFONT
fontSmall HFONT
fontBold HFONT
nullBrush HBRUSH
}
func (a *App) getStockNullBrush() HBRUSH {
if a.nullBrush == 0 {
a.nullBrush = HBRUSH(GetStockObject(NULL_BRUSH))
}
return a.nullBrush
}
func (a *App) cursorClient() POINT {
p := GetCursorPos()
ScreenToClient(a.hwnd, &p)
return p
}
var app *App
var maskPalette = []COLORREF{
RGB(0, 0, 0), // black
RGB(32, 32, 40), // near-black
RGB(70, 70, 78), // gray
RGB(20, 30, 60), // deep blue
RGB(60, 20, 20), // deep red
RGB(245, 245, 245), // white-ish
}
func newApp(hInst HINSTANCE) *App {
a := &App{
hInstance: hInst,
selected: -1,
}
a.vsX = GetSystemMetrics(SM_XVIRTUALSCREEN)
a.vsY = GetSystemMetrics(SM_YVIRTUALSCREEN)
a.vsW = GetSystemMetrics(SM_CXVIRTUALSCREEN)
a.vsH = GetSystemMetrics(SM_CYVIRTUALSCREEN)
if a.vsW <= 0 {
a.vsW = GetSystemMetrics(SM_CXSCREEN)
}
if a.vsH <= 0 {
a.vsH = GetSystemMetrics(SM_CYSCREEN)
}
a.fontUI = CreateFont(-15, FW_NORMAL, "Segoe UI")
a.fontSmall = CreateFont(-12, FW_NORMAL, "Segoe UI")
a.fontBold = CreateFont(-16, FW_BOLD, "Segoe UI")
return a
}
// ---- coordinate mapping between desktop px and canvas client px ----
func (a *App) desktopToCanvas(dx, dy int32) (int32, int32) {
cx := a.canvas.Left + int32(float64(dx-a.vsX)*a.scale)
cy := a.canvas.Top + int32(float64(dy-a.vsY)*a.scale)
return cx, cy
}
func (a *App) canvasToDesktop(cx, cy int32) (int32, int32) {
dx := a.vsX + int32(float64(cx-a.canvas.Left)/a.scale)
dy := a.vsY + int32(float64(cy-a.canvas.Top)/a.scale)
return dx, dy
}
func (a *App) pointInCanvas(x, y int32) bool {
return x >= a.canvas.Left && x < a.canvas.Right && y >= a.canvas.Top && y < a.canvas.Bottom
}
// computeLayout positions the canvas rect inside the client area, leaving room
// for the right control panel and a margin for ruler labels.
func (a *App) computeLayout() {
rc := GetClientRect(a.hwnd)
const margin = int32(28)
left := margin
top := margin
right := rc.Right - panelWidth - margin
bottom := rc.Bottom - margin
availW := right - left
availH := bottom - top
if availW < 50 {
availW = 50
}
if availH < 50 {
availH = 50
}
sx := float64(availW) / float64(a.vsW)
sy := float64(availH) / float64(a.vsH)
a.scale = sx
if sy < sx {
a.scale = sy
}
dw := int32(float64(a.vsW) * a.scale)
dh := int32(float64(a.vsH) * a.scale)
a.canvas = RECT{Left: left, Top: top, Right: left + dw, Bottom: top + dh}
}
func (a *App) selectedMask() *Mask {
if a.selected >= 0 && a.selected < len(a.masks) {
return a.masks[a.selected]
}
return nil
}
func (a *App) addMask(m *Mask) {
a.masks = append(a.masks, m)
a.selected = len(a.masks) - 1
a.createOverlay(m)
}
func (a *App) removeMask(i int) {
if i < 0 || i >= len(a.masks) {
return
}
a.destroyOverlay(a.masks[i])
a.masks = append(a.masks[:i], a.masks[i+1:]...)
// keep the selection pointing at the same mask when possible
if a.selected > i {
a.selected--
}
if a.selected >= len(a.masks) {
a.selected = len(a.masks) - 1
}
}
func (a *App) redraw() { InvalidateRect(a.hwnd, false) }
// ---- window procedure ----
func controlWndProc(hwnd HWND, msg uint32, wp WPARAM, lp LPARAM) LRESULT {
switch msg {
case WM_SIZE:
app.hwnd = hwnd
app.computeLayout()
app.redraw()
return 0
case WM_ERASEBKGND:
return 1
case WM_PAINT:
app.onPaint(hwnd)
return 0
case WM_LBUTTONDOWN:
app.onLButtonDown(lparamX(lp), lparamY(lp))
return 0
case WM_MOUSEMOVE:
app.onMouseMove(lparamX(lp), lparamY(lp))
return 0
case WM_LBUTTONUP:
app.onLButtonUp(lparamX(lp), lparamY(lp))
return 0
case WM_RBUTTONDOWN:
app.onRButtonDown(lparamX(lp), lparamY(lp))
return 0
case WM_SETCURSOR:
if app.updateCursor() {
return 1
}
case WM_KEYDOWN:
app.onKeyDown(uint32(wp))
return 0
case WM_CLOSE:
DestroyWindow(hwnd)
return 0
case WM_DESTROY:
for _, m := range app.masks {
app.destroyOverlay(m)
}
PostQuitMessage(0)
return 0
}
return DefWindowProc(hwnd, msg, wp, lp)
}
func (a *App) onKeyDown(vk uint32) {
switch vk {
case VK_DELETE:
if a.selected >= 0 {
a.removeMask(a.selected)
a.redraw()
}
case VK_ESCAPE:
if a.drag != dragNone {
a.drag = dragNone
ReleaseCapture()
a.redraw()
}
}
}