Files
MirrorBar/preview.go
T

387 lines
9.6 KiB
Go

package main
// Hover preview: rest the mouse on an app icon and a small window with a live
// picture of that app appears next to the bar. Windows draws the picture
// itself (a DWM thumbnail), so it stays live and costs almost nothing. A
// minimized window has nothing live to show, so it gets its icon instead.
import (
"syscall"
"time"
"unsafe"
)
var (
pDwmRegisterThumbnail = dwmapi.NewProc("DwmRegisterThumbnail")
pDwmUnregisterThumbnail = dwmapi.NewProc("DwmUnregisterThumbnail")
pDwmUpdateThumbnailProperties = dwmapi.NewProc("DwmUpdateThumbnailProperties")
pDwmQueryThumbnailSourceSize = dwmapi.NewProc("DwmQueryThumbnailSourceSize")
pSetWindowPos = user32.NewProc("SetWindowPos")
pInvalidateRect = user32.NewProc("InvalidateRect")
)
const (
previewTimerID = 2
previewDelay = 350 * time.Millisecond
hwndTopmost = ^uintptr(0) // HWND_TOPMOST is (HWND)-1
swpNoActivate = 0x0010
swpShowWindow = 0x0040
wmMouseActivate = 0x0021
maNoActivate = 3
dtEndEllipsis = 0x8000
vkLButton = 0x01
dwmTnpDest = 0x1
dwmTnpOpacity = 0x4
dwmTnpVisible = 0x8
)
type dwmThumbProps struct {
Flags uint32
Dest rect
Src rect
Opacity byte
_ [3]byte
Visible int32
ClientOnly int32
}
var (
pvHwnd uintptr // the preview window itself
pvThumb uintptr // the live picture registered on it, if any
pvSrc uintptr // the window being previewed; 0 means nothing is showing
pvTitle string //
pvIcon uintptr //
pvIconic bool // true: no live picture, draw the icon instead
pvThumbR rect // picture area, in the preview's own coordinates
pvTitleR rect // title row
pvCand uintptr // the icon the mouse has been resting on
pvSince time.Time // since when
pvSupp uintptr // an icon that was just clicked: no preview until the mouse leaves it
pvTimer bool
pvBlock bool // a menu is open
)
func previewInit(inst uintptr) {
cursor, _, _ := pLoadCursor.Call(0, idcArrow)
className := u16("MirrorBarPreview")
wc := wndClassEx{
WndProc: syscall.NewCallback(previewProc),
Instance: inst,
Cursor: cursor,
ClassName: className,
}
wc.Size = uint32(unsafe.Sizeof(wc))
pRegisterClassEx.Call(uintptr(unsafe.Pointer(&wc)))
pvHwnd, _, _ = pCreateWindowEx.Call(
wsExToolWindow|wsExTopmost|wsExNoActivate,
uintptr(unsafe.Pointer(className)),
uintptr(unsafe.Pointer(u16(""))),
wsPopup,
0, 0, 10, 10,
0, 0, inst, 0)
}
func previewProc(hwnd, m, wparam, lparam uintptr) (result uintptr) {
defer func() {
if r := recover(); r != nil {
result = 0 // the preview is optional; never take the bar down with it
}
}()
switch uint32(m) {
case wmEraseBkgnd:
return 1
case wmMouseActivate:
return maNoActivate
case wmPaint:
previewPaint(hwnd)
return 0
case wmLButtonUp:
previewActivate()
return 0
}
r, _, _ := pDefWindowProc.Call(hwnd, m, wparam, lparam)
return r
}
func previewPaint(hwnd uintptr) {
var ps paintStruct
hdc, _, _ := pBeginPaint.Call(hwnd, uintptr(unsafe.Pointer(&ps)))
var rc rect
pGetClientRect.Call(hwnd, uintptr(unsafe.Pointer(&rc)))
edge, _, _ := pCreateSolidBrush.Call(rgb(78, 78, 92))
pFillRect.Call(hdc, uintptr(unsafe.Pointer(&rc)), edge)
pDeleteObject.Call(edge)
inner := rect{rc.Left + 1, rc.Top + 1, rc.Right - 1, rc.Bottom - 1}
body, _, _ := pCreateSolidBrush.Call(rgb(28, 28, 34))
pFillRect.Call(hdc, uintptr(unsafe.Pointer(&inner)), body)
pDeleteObject.Call(body)
isz := px(16)
if pvIcon != 0 {
y := pvTitleR.Top + (pvTitleR.Bottom-pvTitleR.Top-isz)/2
pDrawIconEx.Call(hdc, uintptr(pvTitleR.Left), uintptr(y), styledIcon(pvIcon), uintptr(isz), uintptr(isz), 0, 0, diNormal)
}
pSetBkMode.Call(hdc, transparent)
pSetTextColor.Call(hdc, rgb(240, 240, 245))
old, _, _ := pSelectObject.Call(hdc, fontApp)
tr := rect{pvTitleR.Left + isz + px(6), pvTitleR.Top, pvTitleR.Right, pvTitleR.Bottom}
pDrawText.Call(hdc, uintptr(unsafe.Pointer(u16(pvTitle))), ^uintptr(0), uintptr(unsafe.Pointer(&tr)),
dtLeft|dtVCenter|dtSingleLine|dtNoPrefix|dtEndEllipsis)
pSelectObject.Call(hdc, old)
if pvIconic && pvIcon != 0 {
big := px(48)
cx := (pvThumbR.Left + pvThumbR.Right) / 2
cy := (pvThumbR.Top + pvThumbR.Bottom) / 2
pDrawIconEx.Call(hdc, uintptr(cx-big/2), uintptr(cy-big/2), styledIcon(pvIcon), uintptr(big), uintptr(big), 0, 0, diNormal)
}
pEndPaint.Call(hwnd, uintptr(unsafe.Pointer(&ps)))
}
func previewDropThumb() {
if pvThumb != 0 {
pDwmUnregisterThumbnail.Call(pvThumb)
pvThumb = 0
}
}
func previewHide() {
previewDropThumb()
if pvSrc != 0 && pvHwnd != 0 {
pShowWindow.Call(pvHwnd, swHide)
}
pvSrc = 0
}
// previewIconRect is where an app icon sits on the screen.
func previewIconRect(idx int, wr rect) rect {
w, h := clientSize(hwndMain)
rs := appRects(w, h)
if idx < 0 || idx >= len(rs) {
return wr
}
r := rs[idx]
if vertical {
r = mapRect(r, barHeight)
}
return rect{wr.Left + r.Left, wr.Top + r.Top, wr.Left + r.Right, wr.Top + r.Bottom}
}
func previewShow(idx int) {
if pvHwnd == 0 || idx < 0 || idx >= len(apps) {
return
}
a := apps[idx]
a.hwnd = memberHwnd(a)
previewDropThumb()
iconic, _, _ := pIsIconic.Call(a.hwnd)
pvIconic = iconic != 0
var tw, th int32
if !pvIconic {
var t uintptr
hr, _, _ := pDwmRegisterThumbnail.Call(pvHwnd, a.hwnd, uintptr(unsafe.Pointer(&t)))
if hr == 0 && t != 0 {
pvThumb = t
var sz size
hr2, _, _ := pDwmQueryThumbnailSourceSize.Call(t, uintptr(unsafe.Pointer(&sz)))
if hr2 == 0 && sz.CX > 0 && sz.CY > 0 {
f := min(float64(px(260))/float64(sz.CX), float64(px(160))/float64(sz.CY), 1.0)
tw = max(int32(float64(sz.CX)*f), 1)
th = max(int32(float64(sz.CY)*f), 1)
} else {
previewDropThumb()
pvIconic = true
}
} else {
pvIconic = true
}
}
if pvIconic {
tw, th = px(200), px(110)
}
pad, titleH, gap := px(8), px(22), px(4)
cw := max(px(180), tw+2*pad)
ch := pad + titleH + gap + th + pad
pvTitleR = rect{pad, pad, cw - pad, pad + titleH}
tx, ty := (cw-tw)/2, pad+titleH+gap
pvThumbR = rect{tx, ty, tx + tw, ty + th}
var wr rect
pGetWindowRect.Call(hwndMain, uintptr(unsafe.Pointer(&wr)))
ir := previewIconRect(idx, wr)
sw, sh := screenSize()
space := px(8)
var x, y int32
if vertical {
x = wr.Left - cw - space
if x < 0 {
x = wr.Right + space
}
y = (ir.Top+ir.Bottom)/2 - ch/2
} else {
x = (ir.Left+ir.Right)/2 - cw/2
y = wr.Top - ch - space
if y < 0 {
y = wr.Bottom + space
}
}
x = clampI(x, 0, sw-cw)
y = clampI(y, 0, sh-ch)
pvSrc, pvTitle, pvIcon = a.hwnd, a.title, a.icon
pSetWindowPos.Call(pvHwnd, hwndTopmost, uintptr(x), uintptr(y), uintptr(cw), uintptr(ch), swpNoActivate|swpShowWindow)
if pvThumb != 0 {
props := dwmThumbProps{Flags: dwmTnpDest | dwmTnpOpacity | dwmTnpVisible, Dest: pvThumbR, Opacity: 255, Visible: 1}
pDwmUpdateThumbnailProperties.Call(pvThumb, uintptr(unsafe.Pointer(&props)))
}
pInvalidateRect.Call(pvHwnd, 0, 1)
}
func inRect(r rect, p point) bool {
return p.X >= r.Left && p.X < r.Right && p.Y >= r.Top && p.Y < r.Bottom
}
// previewKick starts the short timer that watches where the mouse is.
func previewKick() {
if pvHwnd == 0 || pvTimer {
return
}
pSetTimer.Call(hwndMain, previewTimerID, 60, 0)
pvTimer = true
}
func previewStopTimer() {
if pvTimer {
pKillTimer.Call(hwndMain, previewTimerID)
pvTimer = false
}
}
// previewTick decides what the preview should be doing right now, from where
// the mouse is. It runs only while the mouse is near the bar.
func previewTick() {
defer func() {
if r := recover(); r != nil {
previewHide()
}
}()
if pvHwnd == 0 {
return
}
var cp point
pGetCursorPos.Call(uintptr(unsafe.Pointer(&cp)))
var wr rect
pGetWindowRect.Call(hwndMain, uintptr(unsafe.Pointer(&wr)))
overBar := inRect(wr, cp)
btn, _, _ := pGetAsyncKeyState.Call(vkLButton)
if btn&0x8000 != 0 || pvBlock || dragging {
previewHide()
pvCand = 0
if !overBar {
previewStopTimer()
}
return
}
idx := -1
var raw uintptr
if overBar {
x := cp.X - wr.Left
if vertical {
x = cp.Y - wr.Top
}
idx = appIndexAt(hwndMain, x)
if idx >= 0 && idx < len(apps) {
raw = memberHwnd(apps[idx])
}
}
target := raw
if raw != 0 && raw == pvSupp {
target = 0 // just clicked: stay quiet until the mouse moves off this icon
} else {
pvSupp = 0
}
if target != 0 {
if pvSrc == target {
if apps[idx].title != pvTitle {
pvTitle = apps[idx].title
pInvalidateRect.Call(pvHwnd, 0, 1)
}
return
}
if pvCand != target {
pvCand = target
pvSince = time.Now()
}
if pvSrc != 0 || time.Since(pvSince) >= previewDelay {
previewShow(idx)
}
return
}
pvCand = 0
if pvSrc != 0 {
var pr rect
pGetWindowRect.Call(pvHwnd, uintptr(unsafe.Pointer(&pr)))
g := px(10) // covers the small gap between the icon and the preview
pr = rect{pr.Left - g, pr.Top - g, pr.Right + g, pr.Bottom + g}
if inRect(pr, cp) {
return
}
previewHide()
}
if !overBar {
previewStopTimer()
}
}
// previewSuppress hides the preview because the icon was clicked, and keeps it
// away until the mouse leaves that icon.
func previewSuppress() {
if pvSrc != 0 {
pvSupp = pvSrc
} else {
pvSupp = pvCand
}
pvCand = 0
previewHide()
}
// previewBlock keeps the preview down while a menu is open.
func previewBlock(on bool) {
pvBlock = on
if on {
previewSuppress()
}
}
// previewActivate runs when the preview itself is clicked: bring that window forward.
func previewActivate() {
h := pvSrc
previewHide()
if h == 0 {
return
}
iconic, _, _ := pIsIconic.Call(h)
if iconic != 0 {
pShowWindow.Call(h, swRestore)
}
pSetForegroundWindow.Call(h)
}
func previewShutdown() {
previewStopTimer()
previewHide()
if pvHwnd != 0 {
pDestroyWindow.Call(pvHwnd)
pvHwnd = 0
}
}