Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01JW57Zw8fvgzpeDkbUYT7sG
581 lines
14 KiB
Go
581 lines
14 KiB
Go
package main
|
|
|
|
// Small browser-window flyouts: the AI button (Claude and Grok) and the mp3
|
|
// player. They work like the Messages flyout: Microsoft Edge is started in "app"
|
|
// mode (no tabs, no address bar) with its own private profile folder, the window
|
|
// is kept just off the edge of the screen while the flyout is closed, clicking
|
|
// the button on the bar slides it above the bar, and clicking anywhere else
|
|
// slides it away again. Because the page keeps running while parked, music keeps
|
|
// playing after the player flyout is closed.
|
|
|
|
import (
|
|
"math"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync/atomic"
|
|
"syscall"
|
|
"time"
|
|
"unsafe"
|
|
)
|
|
|
|
type webFly struct {
|
|
name string // "Claude", "Grok", "Player"
|
|
kind int // the tray item that opens it
|
|
timerID uintptr
|
|
w, h float64 // size in 96 dpi units
|
|
profile string // folder name under the MirrorBar data folder
|
|
url func() string
|
|
title string // lower case text that the window title contains
|
|
hwnd uintptr
|
|
pid uint32
|
|
shown bool
|
|
prevBtn bool
|
|
prevFg uintptr
|
|
shownAt time.Time
|
|
launched time.Time
|
|
starting bool
|
|
wantOpen bool
|
|
}
|
|
|
|
var (
|
|
flyClaude = &webFly{name: "Claude", kind: trayAI, timerID: 4, w: 480, h: 720, profile: "claude",
|
|
url: func() string { return "https://claude.ai/new" }, title: "claude"}
|
|
flyGrok = &webFly{name: "Grok", kind: trayAI, timerID: 5, w: 480, h: 720, profile: "grok",
|
|
url: func() string { return "https://grok.com/" }, title: "grok"}
|
|
flyPlayer = &webFly{name: "Player", kind: trayMusic, timerID: 6, w: 440, h: 640, profile: "player",
|
|
url: playerURL, title: "mirrorbar player"}
|
|
|
|
allWebFlies = []*webFly{flyClaude, flyGrok, flyPlayer}
|
|
|
|
wfEnumCb uintptr
|
|
wfTarget *webFly
|
|
wfFound uintptr
|
|
pickBusy atomic.Bool // true while the player's folder or file dialog is open
|
|
)
|
|
|
|
func (f *webFly) profileDir() string { return filepath.Join(dataDir(), f.profile) }
|
|
|
|
func (f *webFly) windowOK() bool {
|
|
if f.hwnd == 0 {
|
|
return false
|
|
}
|
|
ok, _, _ := pIsWindow.Call(f.hwnd)
|
|
return ok != 0
|
|
}
|
|
|
|
// launch starts the app window, visible, because it only starts when the button is clicked.
|
|
func (f *webFly) launch() bool {
|
|
exe := gmBrowserPath()
|
|
if exe == "" {
|
|
return false
|
|
}
|
|
prof := f.profileDir()
|
|
if err := os.MkdirAll(prof, 0o755); err != nil {
|
|
return false
|
|
}
|
|
args := []string{
|
|
exe,
|
|
"--app=" + f.url(),
|
|
"--user-data-dir=" + prof,
|
|
"--no-first-run",
|
|
"--no-default-browser-check",
|
|
"--autoplay-policy=no-user-gesture-required",
|
|
"--disable-features=CalculateNativeWinOcclusion",
|
|
"--disable-renderer-backgrounding",
|
|
"--disable-background-timer-throttling",
|
|
"--disable-backgrounding-occluded-windows",
|
|
}
|
|
parts := make([]string, len(args))
|
|
for i, a := range args {
|
|
parts[i] = syscall.EscapeArg(a)
|
|
}
|
|
si := syscall.StartupInfo{Flags: 1}
|
|
si.Cb = uint32(unsafe.Sizeof(si))
|
|
si.ShowWindow = 1
|
|
var pi syscall.ProcessInformation
|
|
if err := syscall.CreateProcess(u16(exe), u16(strings.Join(parts, " ")), nil, nil, false, 0, nil, nil, &si, &pi); err != nil {
|
|
return false
|
|
}
|
|
f.pid = pi.ProcessId
|
|
syscall.CloseHandle(pi.Process)
|
|
syscall.CloseHandle(pi.Thread)
|
|
f.starting = true
|
|
f.launched = time.Now()
|
|
f.wantOpen = true
|
|
return true
|
|
}
|
|
|
|
func wfEnumProc(hwnd, lparam uintptr) uintptr {
|
|
f := wfTarget
|
|
var cls [64]uint16
|
|
n, _, _ := pGetClassName.Call(hwnd, uintptr(unsafe.Pointer(&cls[0])), 64)
|
|
if syscall.UTF16ToString(cls[:n]) != "Chrome_WidgetWin_1" {
|
|
return 1
|
|
}
|
|
var pid uint32
|
|
pGetWindowThreadProcessId.Call(hwnd, uintptr(unsafe.Pointer(&pid)))
|
|
t := strings.ToLower(gmWindowTitle(hwnd))
|
|
if f.pid != 0 && pid == f.pid && t != "" {
|
|
wfFound = hwnd
|
|
return 0
|
|
}
|
|
// Not the process we started (Edge handed the window to a copy that was already
|
|
// running with this profile): accept it only if it is Edge or Chrome and its
|
|
// title fits, so the Claude desktop app is never mistaken for it.
|
|
if wfFound == 0 && strings.Contains(t, f.title) && !strings.Contains(t, "microsoft edge") && !strings.Contains(t, "google chrome") {
|
|
exe := strings.ToLower(filepath.Base(windowExePath(hwnd)))
|
|
if exe == "msedge.exe" || exe == "chrome.exe" {
|
|
wfFound = hwnd
|
|
}
|
|
}
|
|
return 1
|
|
}
|
|
|
|
func (f *webFly) find() uintptr {
|
|
if wfEnumCb == 0 {
|
|
wfEnumCb = syscall.NewCallback(wfEnumProc)
|
|
}
|
|
wfTarget = f
|
|
wfFound = 0
|
|
pEnumWindows.Call(wfEnumCb, 0)
|
|
return wfFound
|
|
}
|
|
|
|
// adopt takes charge of the window once it exists: out of the app list and
|
|
// Alt+Tab, then shown above the bar.
|
|
func (f *webFly) adopt(h uintptr) {
|
|
f.hwnd = h
|
|
var pid uint32
|
|
pGetWindowThreadProcessId.Call(h, uintptr(unsafe.Pointer(&pid)))
|
|
if pid != 0 {
|
|
f.pid = pid
|
|
}
|
|
ex, _, _ := pGetWindowLongPtr.Call(h, neg(-20))
|
|
want := (ex | wsExToolWindow) &^ gmWsExAppWindow
|
|
if want != ex {
|
|
pSetWindowLongPtr.Call(h, neg(-20), want)
|
|
pSetWindowPos.Call(h, 0, 0, 0, 0, 0, gmSwpNoMove|gmSwpNoSize|gmSwpNoZOrder|swpNoActivate|gmSwpFrameChanged)
|
|
}
|
|
f.starting = false
|
|
if f.wantOpen {
|
|
f.wantOpen = false
|
|
f.show()
|
|
} else {
|
|
f.park()
|
|
}
|
|
}
|
|
|
|
func (f *webFly) size() (int32, int32) {
|
|
sw, sh := screenSize()
|
|
return min(px(f.w), sw-px(16)), min(px(f.h), sh-px(24))
|
|
}
|
|
|
|
func (f *webFly) move(x, y, w, h int32, topmost, activate bool) {
|
|
if !f.windowOK() {
|
|
return
|
|
}
|
|
if z, _, _ := pIsZoomed.Call(f.hwnd); z != 0 {
|
|
pShowWindow.Call(f.hwnd, swRestore)
|
|
}
|
|
if ic, _, _ := pIsIconic.Call(f.hwnd); ic != 0 {
|
|
cmd := uint32(gmSwShowNoActive)
|
|
if activate {
|
|
cmd = swRestore
|
|
}
|
|
wp := gmPlacementT{ShowCmd: cmd, Normal: rect{x, y, x + w, y + h}}
|
|
wp.Length = uint32(unsafe.Sizeof(wp))
|
|
pSetWindowPlace.Call(f.hwnd, uintptr(unsafe.Pointer(&wp)))
|
|
}
|
|
after := ^uintptr(1) // HWND_NOTOPMOST
|
|
if topmost {
|
|
after = hwndTopmost
|
|
}
|
|
var flags uintptr
|
|
if !activate {
|
|
flags = swpNoActivate
|
|
}
|
|
pSetWindowPos.Call(f.hwnd, after, uintptr(x), uintptr(y), uintptr(w), uintptr(h), flags|swpShowWindow)
|
|
}
|
|
|
|
func (f *webFly) park() {
|
|
cw, ch := f.size()
|
|
f.move(gmParkX, gmParkY, cw, ch, false, false)
|
|
}
|
|
|
|
func (f *webFly) place() (x, y, w, h int32) {
|
|
cw, ch := f.size()
|
|
ar := trayAnchor(f.kind)
|
|
var wr rect
|
|
pGetWindowRect.Call(hwndMain, uintptr(unsafe.Pointer(&wr)))
|
|
sw, sh := screenSize()
|
|
space := px(8)
|
|
if vertical {
|
|
x = wr.Left - cw - space
|
|
if x < 0 {
|
|
x = wr.Right + space
|
|
}
|
|
y = (ar.Top+ar.Bottom)/2 - ch/2
|
|
} else {
|
|
x = (ar.Left+ar.Right)/2 - cw/2
|
|
y = wr.Top - ch - space
|
|
if y < 0 {
|
|
y = wr.Bottom + space
|
|
}
|
|
}
|
|
return clampI(x, 0, sw-cw), clampI(y, 0, sh-ch), cw, ch
|
|
}
|
|
|
|
func (f *webFly) show() {
|
|
if !f.windowOK() {
|
|
return
|
|
}
|
|
webFlyHideOthers(f)
|
|
fg, _, _ := pGetForegroundWindow.Call()
|
|
if fg != f.hwnd {
|
|
f.prevFg = fg
|
|
}
|
|
f.shown = true
|
|
f.prevBtn = true
|
|
f.shownAt = time.Now()
|
|
x, y, w, h := f.place()
|
|
f.move(x, y, w, h, true, true)
|
|
pSetForegroundWindow.Call(f.hwnd)
|
|
pSetTimer.Call(hwndMain, f.timerID, 80, 0)
|
|
}
|
|
|
|
func (f *webFly) hide() {
|
|
if !f.shown {
|
|
return
|
|
}
|
|
f.shown = false
|
|
pKillTimer.Call(hwndMain, f.timerID)
|
|
if !f.windowOK() {
|
|
return
|
|
}
|
|
fg, _, _ := pGetForegroundWindow.Call()
|
|
f.park()
|
|
if fg == f.hwnd && f.prevFg != 0 && f.prevFg != hwndMain && f.prevFg != f.hwnd {
|
|
if ok, _, _ := pIsWindow.Call(f.prevFg); ok != 0 {
|
|
pSetForegroundWindow.Call(f.prevFg)
|
|
}
|
|
}
|
|
}
|
|
|
|
// open shows the flyout, starting the browser window first if needed.
|
|
func (f *webFly) open() {
|
|
webFlyHideOthers(f)
|
|
if f.windowOK() {
|
|
f.show()
|
|
return
|
|
}
|
|
f.hwnd = 0
|
|
if f.starting {
|
|
f.wantOpen = true
|
|
return
|
|
}
|
|
if h := f.find(); h != 0 {
|
|
f.wantOpen = true
|
|
f.adopt(h)
|
|
return
|
|
}
|
|
if !f.launch() {
|
|
shellOpen(f.url(), "") // no Edge or Chrome found: use the default browser
|
|
}
|
|
}
|
|
|
|
func (f *webFly) toggle() {
|
|
if f.shown {
|
|
f.hide()
|
|
return
|
|
}
|
|
f.open()
|
|
}
|
|
|
|
// closeWindow shuts the browser window down (right-click, "Close ...").
|
|
func (f *webFly) closeWindow() {
|
|
f.hide()
|
|
if f.windowOK() {
|
|
pPostMessage.Call(f.hwnd, wmClose, 0, 0)
|
|
}
|
|
f.hwnd = 0
|
|
f.starting = false
|
|
f.wantOpen = false
|
|
}
|
|
|
|
func (f *webFly) poll() {
|
|
if !f.shown {
|
|
return
|
|
}
|
|
if !f.windowOK() {
|
|
f.shown = false
|
|
pKillTimer.Call(hwndMain, f.timerID)
|
|
return
|
|
}
|
|
if pickBusy.Load() && f == flyPlayer {
|
|
f.shownAt = time.Now() // a folder or file dialog is open; keep the player up
|
|
return
|
|
}
|
|
l, _, _ := pGetAsyncKeyState.Call(vkLButton)
|
|
r, _, _ := pGetAsyncKeyState.Call(vkRButton)
|
|
down := (l|r)&0x8000 != 0
|
|
if down && !f.prevBtn && time.Since(f.shownAt) > 300*time.Millisecond {
|
|
var cp point
|
|
pGetCursorPos.Call(uintptr(unsafe.Pointer(&cp)))
|
|
var fr rect
|
|
pGetWindowRect.Call(f.hwnd, uintptr(unsafe.Pointer(&fr)))
|
|
inside := inRect(fr, cp)
|
|
if !inside {
|
|
h, _, _ := pWindowFromPoint.Call(uintptr(uint32(cp.X)) | uintptr(uint32(cp.Y))<<32)
|
|
if h != 0 {
|
|
root, _, _ := pGetAncestor.Call(h, gmGaRootOwner)
|
|
inside = root == f.hwnd
|
|
}
|
|
}
|
|
if !inside && !inRect(trayAnchor(f.kind), cp) {
|
|
f.hide()
|
|
}
|
|
}
|
|
f.prevBtn = down
|
|
}
|
|
|
|
// tick runs from the bar's 250 ms timer: it adopts a window that has just
|
|
// appeared and forgets one that was closed.
|
|
func (f *webFly) tick() {
|
|
if f.hwnd != 0 && !f.windowOK() {
|
|
f.hwnd = 0
|
|
f.shown = false
|
|
pKillTimer.Call(hwndMain, f.timerID)
|
|
}
|
|
if f.hwnd == 0 && f.starting {
|
|
if h := f.find(); h != 0 {
|
|
f.adopt(h)
|
|
} else if time.Since(f.launched) > gmLaunchMax {
|
|
f.starting = false
|
|
f.wantOpen = false
|
|
}
|
|
}
|
|
}
|
|
|
|
// ---------- shared hooks ----------
|
|
|
|
func webFlyTick() {
|
|
defer func() { _ = recover() }()
|
|
for _, f := range allWebFlies {
|
|
f.tick()
|
|
}
|
|
}
|
|
|
|
func webFlyHideAll() {
|
|
for _, f := range allWebFlies {
|
|
f.hide()
|
|
}
|
|
}
|
|
|
|
func webFlyHideOthers(except *webFly) {
|
|
previewHide()
|
|
weatherHide()
|
|
newsHide()
|
|
sysHide()
|
|
gmHide()
|
|
for _, f := range allWebFlies {
|
|
if f != except {
|
|
f.hide()
|
|
}
|
|
}
|
|
}
|
|
|
|
func webFlyTimer(id uintptr) bool {
|
|
for _, f := range allWebFlies {
|
|
if f.timerID == id {
|
|
f.poll()
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func webFlyOwns(h uintptr) bool {
|
|
for _, f := range allWebFlies {
|
|
if f.hwnd != 0 && f.hwnd == h {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func webFlyShutdown() {
|
|
for _, f := range allWebFlies {
|
|
pKillTimer.Call(hwndMain, f.timerID)
|
|
if f.windowOK() {
|
|
pPostMessage.Call(f.hwnd, wmClose, 0, 0)
|
|
}
|
|
f.hwnd = 0
|
|
}
|
|
}
|
|
|
|
// ---------- the AI button: a choice between Claude and Grok ----------
|
|
|
|
const (
|
|
menuAIClaudeID = 700
|
|
menuAIGrokID = 701
|
|
menuAICloseClaudeID = 702
|
|
menuAICloseGrokID = 703
|
|
menuPlayerShowID = 704
|
|
menuPlayerFolderID = 705
|
|
menuPlayerFileID = 706
|
|
menuPlayerCloseID = 707
|
|
menuClipID = 708
|
|
tpmCenterAlign = 0x0004
|
|
tpmRightAlign = 0x0008
|
|
tpmVCenterAlign = 0x0010
|
|
)
|
|
|
|
// aiClick: if one of the two is open, close it; otherwise pop up the choice.
|
|
func aiClick() {
|
|
if flyClaude.shown {
|
|
flyClaude.hide()
|
|
return
|
|
}
|
|
if flyGrok.shown {
|
|
flyGrok.hide()
|
|
return
|
|
}
|
|
menu, _, _ := pCreatePopupMenu.Call()
|
|
pAppendMenu.Call(menu, 0, menuAIClaudeID, uintptr(unsafe.Pointer(u16("Claude"))))
|
|
pAppendMenu.Call(menu, 0, menuAIGrokID, uintptr(unsafe.Pointer(u16("Grok"))))
|
|
ar := trayAnchor(trayAI)
|
|
flags := uintptr(tpmRightButton | tpmReturnCmd)
|
|
x, y := (ar.Left+ar.Right)/2, ar.Top
|
|
if vertical {
|
|
flags |= tpmRightAlign | tpmVCenterAlign
|
|
x, y = ar.Left, (ar.Top+ar.Bottom)/2
|
|
} else {
|
|
flags |= tpmBottomAlign | tpmCenterAlign
|
|
}
|
|
pSetForegroundWindow.Call(hwndMain)
|
|
menuOpen = true
|
|
cmd, _, _ := pTrackPopupMenu.Call(menu, flags, uintptr(x), uintptr(y), 0, hwndMain, 0)
|
|
menuOpen = false
|
|
ahLeftAt = time.Now()
|
|
pDestroyMenu.Call(menu)
|
|
switch cmd {
|
|
case menuAIClaudeID:
|
|
flyClaude.open()
|
|
case menuAIGrokID:
|
|
flyGrok.open()
|
|
}
|
|
}
|
|
|
|
func webFlyMenu(add func(flags uintptr, id uintptr, text string), sep func(), kind int) {
|
|
switch kind {
|
|
case trayAI:
|
|
add(0, menuAIClaudeID, "Open Claude")
|
|
add(0, menuAIGrokID, "Open Grok")
|
|
add(0, menuAICloseClaudeID, "Close Claude (sign-in is kept)")
|
|
add(0, menuAICloseGrokID, "Close Grok (sign-in is kept)")
|
|
sep()
|
|
case trayMusic:
|
|
add(0, menuPlayerShowID, "Show player")
|
|
add(0, menuPlayerFolderID, "Choose a music or audiobook folder...")
|
|
add(0, menuPlayerFileID, "Choose a single file...")
|
|
add(0, menuPlayerCloseID, "Close player (stops the music)")
|
|
sep()
|
|
case trayClip:
|
|
add(0, menuClipID, "Open clipboard history")
|
|
sep()
|
|
}
|
|
}
|
|
|
|
func webFlyCommand(cmd uintptr) bool {
|
|
switch cmd {
|
|
case menuAIClaudeID:
|
|
flyClaude.open()
|
|
case menuAIGrokID:
|
|
flyGrok.open()
|
|
case menuAICloseClaudeID:
|
|
flyClaude.closeWindow()
|
|
case menuAICloseGrokID:
|
|
flyGrok.closeWindow()
|
|
case menuPlayerShowID:
|
|
flyPlayer.open()
|
|
case menuPlayerFolderID:
|
|
flyPlayer.open()
|
|
go playerPick(true)
|
|
case menuPlayerFileID:
|
|
flyPlayer.open()
|
|
go playerPick(false)
|
|
case menuPlayerCloseID:
|
|
flyPlayer.closeWindow()
|
|
case menuClipID:
|
|
tapChord(vkLWin, 'V')
|
|
default:
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
// ---------- icons ----------
|
|
|
|
// drawAIIcon: a four pointed sparkle with a small one beside it.
|
|
func drawAIIcon(hdc uintptr, r rect) {
|
|
if !layerFg {
|
|
return
|
|
}
|
|
col := iconColor()
|
|
cx, cy := center(r)
|
|
sparkle := func(x, y int32, rad, inner float64, w int32) {
|
|
pts := make([]point, 0, 9)
|
|
for k := 0; k <= 8; k++ {
|
|
a := float64(k) * math.Pi / 4
|
|
rr := rad
|
|
if k%2 == 1 {
|
|
rr = inner
|
|
}
|
|
pts = append(pts, point{x + int32(math.Round(rr*math.Sin(a))), y - int32(math.Round(rr*math.Cos(a)))})
|
|
}
|
|
polyline(hdc, pts, w, col)
|
|
}
|
|
sparkle(cx-px(1.5), cy+px(1.5), float64(px(7.5)), float64(px(2)), px(1.2))
|
|
sparkle(cx+px(5.5), cy-px(5), float64(px(3.2)), float64(px(1)), px(1))
|
|
}
|
|
|
|
// drawClipIcon: a clipboard with a clip at the top and two lines of text.
|
|
func drawClipIcon(hdc uintptr, r rect) {
|
|
if !layerFg {
|
|
return
|
|
}
|
|
col := iconColor()
|
|
cx, cy := center(r)
|
|
e := px(1)
|
|
l, t, rt, b := cx-px(5.5), cy-px(5.5), cx+px(5.5), cy+px(7)
|
|
fillRaw(hdc, rect{l, t, rt, t + e}, col)
|
|
fillRaw(hdc, rect{l, b - e, rt, b}, col)
|
|
fillRaw(hdc, rect{l, t, l + e, b}, col)
|
|
fillRaw(hdc, rect{rt - e, t, rt, b}, col)
|
|
// the clip
|
|
fillRaw(hdc, rect{cx - px(2.5), t - px(2), cx + px(2.5), t + px(1.5)}, col)
|
|
// lines of text
|
|
fillRaw(hdc, rect{cx - px(3), cy - px(0.5), cx + px(3), cy - px(0.5) + e}, col)
|
|
fillRaw(hdc, rect{cx - px(3), cy + px(2.5), cx + px(1), cy + px(2.5) + e}, col)
|
|
}
|
|
|
|
// drawMusicIcon: two beamed notes.
|
|
func drawMusicIcon(hdc uintptr, r rect) {
|
|
if !layerFg {
|
|
return
|
|
}
|
|
col := iconColor()
|
|
cx, cy := center(r)
|
|
e := px(1.2)
|
|
h1x, h1y := cx-px(3.5), cy+px(4.5)
|
|
h2x, h2y := cx+px(4), cy+px(2.5)
|
|
circleRaw(hdc, h1x, h1y, px(2.4), col)
|
|
circleRaw(hdc, h2x, h2y, px(2.4), col)
|
|
s1x, s2x := h1x+px(2), h2x+px(2)
|
|
t1, t2 := cy-px(6.5), cy-px(8.5)
|
|
fillRaw(hdc, rect{s1x - e/2, t1, s1x - e/2 + e, h1y}, col)
|
|
fillRaw(hdc, rect{s2x - e/2, t2, s2x - e/2 + e, h2y}, col)
|
|
polyline(hdc, []point{{s1x, t1 + px(0.5)}, {s2x, t2 + px(0.5)}}, px(2), col)
|
|
}
|