-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsystemwindow.go
100 lines (90 loc) · 2.65 KB
/
systemwindow.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
// Copyright 2022 Ahmet Alp Balkan
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"strings"
"github.com/ahmetb/RectangleWin/w32ex"
"github.com/gonutz/w32/v2"
)
const (
GWL_EXSTYLE = -20
GWL_STYLE = -16
)
func isZonableWindow(hwnd w32.HWND) bool {
if hwnd == 0 {
return false
}
return isStandardWindow(hwnd) && hasNoVisibleOwner(hwnd)
}
func hasNoVisibleOwner(hwnd w32.HWND) bool {
owner := w32.GetWindow(hwnd, w32.GW_OWNER)
if owner == 0 {
return true
}
if !w32.IsWindowVisible(owner) {
return true
}
rect := w32.GetWindowRect(owner)
if rect == nil {
return false
}
return rect.Width() == 0 || rect.Height() == 0
}
func isStandardWindow(hwnd w32.HWND) bool {
// adapted from https://github.com/microsoft/PowerToys/blob/7d0304fd06939d9f552e75be9c830db22f8ff9e2/src/modules/fancyzones/FancyZonesLib/util.cpp#L403
if w32ex.GetAncestor(hwnd, w32ex.GA_ROOT) != hwnd ||
!w32.IsWindowVisible(hwnd) {
return false
}
for _, sysWindow := range []w32.HWND{w32.GetDesktopWindow(), w32ex.GetShellWindow()} {
if hwnd == sysWindow {
return false
}
}
style := w32.GetWindowLong(hwnd, GWL_STYLE)
// a window with think frame and minimize/maximize buttons
if uint32(style)&w32.WS_POPUP == w32.WS_POPUP &&
style&w32.WS_THICKFRAME == w32.WS_THICKFRAME &&
style&w32.WS_MINIMIZEBOX == 0 &&
style&w32.WS_MAXIMIZEBOX == 0 {
return false
}
exStyle := w32.GetWindowLong(hwnd, GWL_EXSTYLE)
if uint32(style)&w32.WS_CHILD == w32.WS_CHILD ||
style&w32.WS_DISABLED == w32.WS_DISABLED ||
exStyle&w32.WS_EX_TOOLWINDOW == w32.WS_EX_TOOLWINDOW ||
exStyle&w32.WS_EX_NOACTIVATE == w32.WS_EX_NOACTIVATE {
return false
}
className, ok := w32.GetClassName(hwnd)
if !ok {
panic("GetClassName failed")
}
return !isSystemClassName(className)
}
func isSystemClassName(className string) bool {
// adapted from https://github.com/microsoft/PowerToys/blob/7d0304fd06939d9f552e75be9c830db22f8ff9e2/tools/FancyZones_zonable_tester/main.cpp#L135
for _, c := range []string{
"SysListView32",
"WorkerW",
"Shell_TrayWnd",
"Shell_SecondaryTrayWnd",
"Progman",
} {
if strings.EqualFold(c, className) {
return true
}
}
return false
}