183 lines
4.2 KiB
Go
183 lines
4.2 KiB
Go
package main
|
|
|
|
// Moving the pill: the whole background of the bar is a drag area, the dragged
|
|
// spot is remembered, and "Reset position" in the menu goes back to the default
|
|
// (bottom center, a few pixels above the screen edge).
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"unsafe"
|
|
)
|
|
|
|
const (
|
|
menuResetPosID = 32
|
|
menuOrientID = 33
|
|
abeRight = 2
|
|
|
|
wmNcHitTest = 0x0084
|
|
wmNcLButtonDblClk = 0x00A3
|
|
wmNcRButtonUp = 0x00A5
|
|
wmEnterSizeMove = 0x0231
|
|
wmExitSizeMove = 0x0232
|
|
htClient = 1
|
|
htCaption = 2
|
|
)
|
|
|
|
var (
|
|
customPos bool // false means the default spot
|
|
customCX int32 // center of the pill when moved
|
|
customY int32 // vertical center of the pill when moved
|
|
vertical bool // true: the bar stands up and down at the right edge
|
|
dragging bool
|
|
appbarOn = true
|
|
)
|
|
|
|
func posFile() string { return filepath.Join(dataDir(), "pos.txt") }
|
|
|
|
func loadPos() {
|
|
b, err := os.ReadFile(posFile())
|
|
if err != nil {
|
|
return
|
|
}
|
|
parts := strings.Split(strings.TrimSpace(string(b)), ",")
|
|
if len(parts) != 2 {
|
|
return
|
|
}
|
|
cx, e1 := strconv.Atoi(parts[0])
|
|
y, e2 := strconv.Atoi(parts[1])
|
|
if e1 != nil || e2 != nil {
|
|
return
|
|
}
|
|
customPos, customCX, customY = true, int32(cx), int32(y)
|
|
}
|
|
|
|
func savePos() {
|
|
if !customPos {
|
|
_ = os.Remove(posFile())
|
|
return
|
|
}
|
|
_ = os.MkdirAll(dataDir(), 0o755)
|
|
_ = os.WriteFile(posFile(), []byte(strconv.Itoa(int(customCX))+","+strconv.Itoa(int(customY))), 0o644)
|
|
}
|
|
|
|
func orientFile() string { return filepath.Join(dataDir(), "orient.txt") }
|
|
|
|
func loadOrient() {
|
|
b, err := os.ReadFile(orientFile())
|
|
vertical = err == nil && strings.TrimSpace(string(b)) == "vertical"
|
|
}
|
|
|
|
func saveOrient() {
|
|
_ = os.MkdirAll(dataDir(), 0o755)
|
|
v := "horizontal"
|
|
if vertical {
|
|
v = "vertical"
|
|
}
|
|
_ = os.WriteFile(orientFile(), []byte(v), 0o644)
|
|
}
|
|
|
|
// Everything is laid out as if the bar were horizontal (a "length" along the
|
|
// bar and a "thickness" across it). Vertical mode turns the picture a quarter
|
|
// turn, so mouse positions are converted back to a position along the bar.
|
|
func alongClient(lparam uintptr) int32 {
|
|
x := int32(int16(lparam & 0xFFFF))
|
|
y := int32(int16(lparam >> 16 & 0xFFFF))
|
|
if vertical {
|
|
return y
|
|
}
|
|
return x
|
|
}
|
|
|
|
func alongScreen(lparam uintptr, wr rect) int32 {
|
|
x := int32(int16(lparam&0xFFFF)) - wr.Left
|
|
y := int32(int16(lparam>>16&0xFFFF)) - wr.Top
|
|
if vertical {
|
|
return y
|
|
}
|
|
return x
|
|
}
|
|
|
|
// pillSize is the real window size for the current orientation.
|
|
func pillSize() (int32, int32) {
|
|
l := pillWidth()
|
|
if vertical {
|
|
return barHeight, l
|
|
}
|
|
return l, barHeight
|
|
}
|
|
|
|
// mapRect turns a rectangle from the horizontal layout into the same spot on
|
|
// the turned bar (a clockwise quarter turn; t is the bar's thickness).
|
|
func mapRect(r rect, t int32) rect {
|
|
return rect{t - r.Bottom, r.Left, t - r.Top, r.Right}
|
|
}
|
|
|
|
func clampI(v, lo, hi int32) int32 {
|
|
if hi < lo {
|
|
hi = lo
|
|
}
|
|
return max(lo, min(hi, v))
|
|
}
|
|
|
|
// pillPos is where the top-left of a pill of width nw belongs.
|
|
func pillPos(pw, ph int32) (int32, int32) {
|
|
if !customPos {
|
|
return defaultPos(pw, ph)
|
|
}
|
|
sw, sh := screenSize()
|
|
x := clampI(customCX-pw/2, 0, sw-pw)
|
|
y := clampI(customY-ph/2, 0, sh-ph)
|
|
return x, y
|
|
}
|
|
|
|
func defaultPos(pw, ph int32) (int32, int32) {
|
|
sw, sh := screenSize()
|
|
if vertical {
|
|
return sw - pw - barGap(), (sh - ph) / 2
|
|
}
|
|
return (sw - pw) / 2, sh - ph - barGap()
|
|
}
|
|
|
|
// finishDrag runs when a drag ends: remember the new spot, or snap back to the
|
|
// default if it was dropped right on it.
|
|
func finishDrag(hwnd uintptr) {
|
|
dragging = false
|
|
var wr rect
|
|
pGetWindowRect.Call(hwnd, uintptr(unsafe.Pointer(&wr)))
|
|
dx, dy := defaultPos(wr.Right-wr.Left, wr.Bottom-wr.Top)
|
|
if abs32(wr.Left-dx) <= px(8) && abs32(wr.Top-dy) <= px(8) {
|
|
resetPos(hwnd)
|
|
return
|
|
}
|
|
customPos, customCX, customY = true, (wr.Left+wr.Right)/2, (wr.Top+wr.Bottom)/2
|
|
savePos()
|
|
if appbarOn {
|
|
// A floating bar no longer reserves screen space.
|
|
abd := appBarData{Hwnd: hwnd}
|
|
appBar(abmRemove, &abd)
|
|
appbarOn = false
|
|
}
|
|
redraw(hwnd)
|
|
}
|
|
|
|
func abs32(v int32) int32 {
|
|
if v < 0 {
|
|
return -v
|
|
}
|
|
return v
|
|
}
|
|
|
|
func resetPos(hwnd uintptr) {
|
|
customPos = false
|
|
savePos()
|
|
if !appbarOn {
|
|
abd := appBarData{Hwnd: hwnd, CallbackMsg: callbackMsg}
|
|
appBar(abmNew, &abd)
|
|
appbarOn = true
|
|
}
|
|
positionBar(hwnd)
|
|
}
|