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) } // ---------- the left row: Start button and app icons, all draggable ---------- var ( pSetCapture = user32.NewProc("SetCapture") pReleaseCapture = user32.NewProc("ReleaseCapture") ) const ( wmLButtonDown = 0x0201 wmCaptureChanged = 0x0215 ) var ( startSlot int // where the Start button sits in the left row (0 is against the center line) dragSeq = -1 // row position of the item the mouse went down on, -1 for none dragIsStart bool // the item being pressed is the Start button dragStart int32 // where along the bar the mouse went down dragActive bool // true once the mouse has moved far enough to count as a drag ) func startSlotFile() string { return filepath.Join(dataDir(), "startslot.txt") } func loadStartSlot() { b, err := os.ReadFile(startSlotFile()) if err != nil { return } if n, err := strconv.Atoi(strings.TrimSpace(string(b))); err == nil && n >= 0 { startSlot = n } } func saveStartSlot() { _ = os.MkdirAll(dataDir(), 0o755) _ = os.WriteFile(startSlotFile(), []byte(strconv.Itoa(startSlot)), 0o644) } func endIconDrag() { dragSeq = -1 dragIsStart = false dragActive = false } // slot is the Start button's position, kept within the row. func slot() int { return max(0, min(startSlot, len(apps))) } // The row is the Start button plus every app, from the center line leftward. // A "row position" counts those items; app i sits at seqOfApp(i). func seqOfApp(i int) int { if i < slot() { return i } return i + 1 } func appOfSeq(t int) int { if t < slot() { return t } return t - 1 } // rowLayout gives the rectangle of every row position (Start included). func rowLayout(clientW, clientH int32) []rect { n := int32(len(apps)) sl := int32(slot()) cx := clientW / 2 bw := px(36) if n > 0 { avail := cx - startWidth() - px(8) if avail/n < bw { bw = avail / n } if bw < px(24) { bw = px(24) } } rects := make([]rect, n+1) x := cx for k := int32(0); k <= n; k++ { wd := bw if k == sl { wd = startWidth() } rects[k] = rect{x - wd, 0, x, clientH} x -= wd } return rects } func startRect(clientW, clientH int32) rect { return rowLayout(clientW, clientH)[slot()] } // seqAt is the row position under a spot along the bar, clamped to the ends. func seqAt(hwnd uintptr, x int32) int { w, h := clientSize(hwnd) rects := rowLayout(w, h) if x >= rects[0].Right { return 0 } if x < rects[len(rects)-1].Left { return len(rects) - 1 } for k, r := range rects { if x >= r.Left && x < r.Right { return k } } return -1 } // moveSeq moves one item of the row to another position, shifting the rest. func moveSeq(from, to int) { n := len(apps) if from == to || from < 0 || to < 0 || from > n || to > n { return } sl := slot() seq := make([]int, 0, n+1) // -1 stands for the Start button, otherwise an app index for k := 0; k <= n; k++ { if k == sl { seq = append(seq, -1) } else { seq = append(seq, appOfSeq(k)) } } v := seq[from] rest := append(append([]int{}, seq[:from]...), seq[from+1:]...) seq = append(append(append([]int{}, rest[:to]...), v), rest[to:]...) next := make([]appWin, 0, n) for k, a := range seq { if a == -1 { startSlot = k } else { next = append(next, apps[a]) } } apps = next }