Skip to content

Commit 894e4d5

Browse files
committed
feat: "always on top" toggle
Fixes #5 Fixes #6 Signed-off-by: Ahmet Alp Balkan <ahmetalpbalkan@gmail.com>
1 parent 43aa5fc commit 894e4d5

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ only using hotkeys:
3838
- **Center window** on the display: <kbd>Win</kbd>+<kbd>Alt</kbd>+<kbd>C</kbd>
3939

4040
- **Maximize window**: <kbd>Win</kbd>+<kbd>Shift</kbd>+<kbd>F</kbd>
41-
4241
(Obsolete since Windows natively supports <kbd>Win</kbd>+<kbd>&uarr;</kbd>)
4342

43+
- **Always On Top (toggle)**: <kbd>Win</kbd>+<kbd>Alt</kbd>+<kbd>A</kbd>
44+
4445
## Why?
4546

4647
It seems that no window snapping utility for Windows is capable of letting

main.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,14 @@ func main() {
105105
return
106106
}
107107
}}),
108+
(HotKey{id: 70, mod: MOD_ALT | MOD_WIN, vk: 0x41 /*A*/, callback: func() {
109+
hwnd := w32.GetForegroundWindow()
110+
if err := toggleAlwaysOnTop(hwnd); err != nil {
111+
fmt.Printf("warn: toggleAlwaysOnTop: %v\n", err)
112+
return
113+
}
114+
fmt.Printf("> toggled always on top: %v\n", hwnd)
115+
}}),
108116
}
109117

110118
var failedHotKeys []HotKey
@@ -228,6 +236,23 @@ func maximize() error {
228236
return nil
229237
}
230238

239+
func toggleAlwaysOnTop(hwnd w32.HWND) error {
240+
if !isZonableWindow(hwnd) {
241+
return errors.New("foreground window is not zonable")
242+
}
243+
244+
if w32.GetWindowLong(hwnd, w32.GWL_EXSTYLE)&w32.WS_EX_TOPMOST != 0 {
245+
if !w32.SetWindowPos(hwnd, w32.HWND_NOTOPMOST, 0, 0, 0, 0, w32.SWP_NOMOVE|w32.SWP_NOSIZE) {
246+
return fmt.Errorf("failed to SetWindowPos(HWND_NOTOPMOST): %v", w32.GetLastError())
247+
}
248+
} else {
249+
if !w32.SetWindowPos(hwnd, w32.HWND_TOPMOST, 0, 0, 0, 0, w32.SWP_NOMOVE|w32.SWP_NOSIZE) {
250+
return fmt.Errorf("failed to SetWindowPos(HWND_TOPMOST) :%v", w32.GetLastError())
251+
}
252+
}
253+
return nil
254+
}
255+
231256
func resizeForDpi(src w32.RECT, from, to int32) w32.RECT {
232257
return w32.RECT{
233258
Left: src.Left * to / from,

0 commit comments

Comments
 (0)