Skip to content

Commit

Permalink
feat: "always on top" toggle
Browse files Browse the repository at this point in the history
Fixes #5
Fixes #6

Signed-off-by: Ahmet Alp Balkan <[email protected]>
  • Loading branch information
ahmetb committed Jan 12, 2023
1 parent 43aa5fc commit 894e4d5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ only using hotkeys:
- **Center window** on the display: <kbd>Win</kbd>+<kbd>Alt</kbd>+<kbd>C</kbd>

- **Maximize window**: <kbd>Win</kbd>+<kbd>Shift</kbd>+<kbd>F</kbd>

(Obsolete since Windows natively supports <kbd>Win</kbd>+<kbd>&uarr;</kbd>)

- **Always On Top (toggle)**: <kbd>Win</kbd>+<kbd>Alt</kbd>+<kbd>A</kbd>

## Why?

It seems that no window snapping utility for Windows is capable of letting
Expand Down
25 changes: 25 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ func main() {
return
}
}}),
(HotKey{id: 70, mod: MOD_ALT | MOD_WIN, vk: 0x41 /*A*/, callback: func() {
hwnd := w32.GetForegroundWindow()
if err := toggleAlwaysOnTop(hwnd); err != nil {
fmt.Printf("warn: toggleAlwaysOnTop: %v\n", err)
return
}
fmt.Printf("> toggled always on top: %v\n", hwnd)
}}),
}

var failedHotKeys []HotKey
Expand Down Expand Up @@ -228,6 +236,23 @@ func maximize() error {
return nil
}

func toggleAlwaysOnTop(hwnd w32.HWND) error {
if !isZonableWindow(hwnd) {
return errors.New("foreground window is not zonable")
}

if w32.GetWindowLong(hwnd, w32.GWL_EXSTYLE)&w32.WS_EX_TOPMOST != 0 {
if !w32.SetWindowPos(hwnd, w32.HWND_NOTOPMOST, 0, 0, 0, 0, w32.SWP_NOMOVE|w32.SWP_NOSIZE) {
return fmt.Errorf("failed to SetWindowPos(HWND_NOTOPMOST): %v", w32.GetLastError())
}
} else {
if !w32.SetWindowPos(hwnd, w32.HWND_TOPMOST, 0, 0, 0, 0, w32.SWP_NOMOVE|w32.SWP_NOSIZE) {
return fmt.Errorf("failed to SetWindowPos(HWND_TOPMOST) :%v", w32.GetLastError())
}
}
return nil
}

func resizeForDpi(src w32.RECT, from, to int32) w32.RECT {
return w32.RECT{
Left: src.Left * to / from,
Expand Down

0 comments on commit 894e4d5

Please sign in to comment.