MirrorBar+ part 3: local news flyout, system stats flyout with copy, feature switches
This commit is contained in:
+271
@@ -0,0 +1,271 @@
|
||||
package main
|
||||
|
||||
// The system stats flyout: a little monitor on the bar that opens a panel with
|
||||
// the sort of thing neofetch prints (OS, CPU, memory, disks, uptime...). Click a
|
||||
// line to copy it, or click the header to copy everything as plain text that can
|
||||
// be pasted into any other program.
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
const (
|
||||
sysW = 420.0
|
||||
sysHead = 44.0
|
||||
sysRow_ = 24.0
|
||||
sysFoot = 26.0
|
||||
|
||||
sysRefreshTimerID = 2
|
||||
)
|
||||
|
||||
var (
|
||||
sysHwnd uintptr
|
||||
sysShown bool
|
||||
sysPrevBtn bool
|
||||
sysHover = -1
|
||||
sysRows []sysRow
|
||||
sysCopied string // what the header says after a copy: "Copied" or "Copy failed"
|
||||
sysCopiedAt time.Time
|
||||
sysTick int
|
||||
)
|
||||
|
||||
// ---------- the button on the bar ----------
|
||||
|
||||
func drawSysIcon(hdc uintptr, r rect) {
|
||||
if !layerFg {
|
||||
return
|
||||
}
|
||||
col := iconColor()
|
||||
cx, cy := center(r)
|
||||
e := px(1)
|
||||
l, t, rt, b := cx-px(6), cy-px(5.5), cx+px(6), cy+px(2.5)
|
||||
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)
|
||||
fillRaw(hdc, rect{cx - px(1), b, cx + px(1), cy + px(4.5)}, col) // stand
|
||||
fillRaw(hdc, rect{cx - px(3), cy + px(4.5), cx + px(3), cy + px(5.5)}, col)
|
||||
fillRaw(hdc, rect{cx - px(3.5), cy - px(0.5), cx - px(2), cy + px(1.5)}, col) // little bars on the screen
|
||||
fillRaw(hdc, rect{cx - px(1), cy - px(2.5), cx + px(0.5), cy + px(1.5)}, col)
|
||||
fillRaw(hdc, rect{cx + px(2), cy - px(1.5), cx + px(3.5), cy + px(1.5)}, col)
|
||||
}
|
||||
|
||||
// ---------- the panel window ----------
|
||||
|
||||
func sysInit(inst uintptr) {
|
||||
cursor, _, _ := pLoadCursor.Call(0, idcArrow)
|
||||
className := u16("MirrorBarSys")
|
||||
wc := wndClassEx{
|
||||
WndProc: syscall.NewCallback(sysProc),
|
||||
Instance: inst,
|
||||
Cursor: cursor,
|
||||
ClassName: className,
|
||||
}
|
||||
wc.Size = uint32(unsafe.Sizeof(wc))
|
||||
pRegisterClassEx.Call(uintptr(unsafe.Pointer(&wc)))
|
||||
sysHwnd, _, _ = pCreateWindowEx.Call(
|
||||
wsExToolWindow|wsExTopmost|wsExNoActivate,
|
||||
uintptr(unsafe.Pointer(className)),
|
||||
uintptr(unsafe.Pointer(u16(""))),
|
||||
wsPopup,
|
||||
0, 0, 10, 10,
|
||||
0, 0, inst, 0)
|
||||
}
|
||||
|
||||
func sysProc(hwnd, m, wparam, lparam uintptr) (result uintptr) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
result = 0 // the flyout is optional; never take the bar down with it
|
||||
}
|
||||
}()
|
||||
switch uint32(m) {
|
||||
case wmEraseBkgnd:
|
||||
return 1
|
||||
case wmMouseActivate:
|
||||
return maNoActivate
|
||||
case wmPaint:
|
||||
flyPaint(hwnd, sysDraw)
|
||||
return 0
|
||||
case wmTimer:
|
||||
if wparam == sysRefreshTimerID {
|
||||
sysRows = sysGather() // uptime and memory move
|
||||
pInvalidateRect.Call(sysHwnd, 0, 0)
|
||||
return 0
|
||||
}
|
||||
sysPoll()
|
||||
return 0
|
||||
case wmSetCursor:
|
||||
if sysOverButton() {
|
||||
h, _, _ := pLoadCursor.Call(0, idcHandCursor)
|
||||
pSetCursor.Call(h)
|
||||
return 1
|
||||
}
|
||||
case wmLButtonUp:
|
||||
sysClick(int32(int16(lparam&0xFFFF)), int32(int16(lparam>>16&0xFFFF)))
|
||||
return 0
|
||||
}
|
||||
r, _, _ := pDefWindowProc.Call(hwnd, m, wparam, lparam)
|
||||
return r
|
||||
}
|
||||
|
||||
// sysOverButton is true when the mouse is on something clickable: a line or the header.
|
||||
func sysOverButton() bool {
|
||||
var cp point
|
||||
pGetCursorPos.Call(uintptr(unsafe.Pointer(&cp)))
|
||||
var fr rect
|
||||
pGetWindowRect.Call(sysHwnd, uintptr(unsafe.Pointer(&fr)))
|
||||
if !inRect(fr, cp) {
|
||||
return false
|
||||
}
|
||||
y := cp.Y - fr.Top
|
||||
return y < px(sysHead) || sysRowAt(y) >= 0
|
||||
}
|
||||
|
||||
func sysToggle() {
|
||||
if !showSys {
|
||||
return
|
||||
}
|
||||
if sysShown {
|
||||
sysHide()
|
||||
return
|
||||
}
|
||||
if sysHwnd == 0 {
|
||||
return
|
||||
}
|
||||
previewHide()
|
||||
weatherHide() // one panel at a time
|
||||
newsHide()
|
||||
sysRows = sysGather()
|
||||
sysShown = true
|
||||
sysPrevBtn = true // ignore a button that is still held from the click that opened this
|
||||
sysHover = -1
|
||||
sysCopied = ""
|
||||
sysPlace()
|
||||
pSetTimer.Call(sysHwnd, wxPollTimerID, 80, 0)
|
||||
pSetTimer.Call(sysHwnd, sysRefreshTimerID, 1500, 0)
|
||||
}
|
||||
|
||||
func sysHide() {
|
||||
if !sysShown {
|
||||
return
|
||||
}
|
||||
sysShown = false
|
||||
sysHover = -1
|
||||
pKillTimer.Call(sysHwnd, wxPollTimerID)
|
||||
pKillTimer.Call(sysHwnd, sysRefreshTimerID)
|
||||
pShowWindow.Call(sysHwnd, swHide)
|
||||
}
|
||||
|
||||
func sysShutdown() {
|
||||
sysHide()
|
||||
if sysHwnd != 0 {
|
||||
pDestroyWindow.Call(sysHwnd)
|
||||
sysHwnd = 0
|
||||
}
|
||||
}
|
||||
|
||||
func sysPlace() {
|
||||
if sysHwnd == 0 {
|
||||
return
|
||||
}
|
||||
lh := sysHead + float64(len(sysRows))*sysRow_ + sysFoot
|
||||
flyPlace(sysHwnd, trayAnchor(traySys), px(sysW), px(lh))
|
||||
pInvalidateRect.Call(sysHwnd, 0, 1)
|
||||
}
|
||||
|
||||
// sysRowAt is the line under a height in the panel (real pixels), or -1.
|
||||
func sysRowAt(y int32) int {
|
||||
ry := y - px(sysHead)
|
||||
if ry < 0 {
|
||||
return -1
|
||||
}
|
||||
i := int(ry / px(sysRow_))
|
||||
if i >= len(sysRows) {
|
||||
return -1
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
func sysPoll() {
|
||||
l, _, _ := pGetAsyncKeyState.Call(vkLButton)
|
||||
r, _, _ := pGetAsyncKeyState.Call(vkRButton)
|
||||
down := (l|r)&0x8000 != 0
|
||||
var cp point
|
||||
pGetCursorPos.Call(uintptr(unsafe.Pointer(&cp)))
|
||||
var fr rect
|
||||
pGetWindowRect.Call(sysHwnd, uintptr(unsafe.Pointer(&fr)))
|
||||
if down && !sysPrevBtn && !inRect(fr, cp) && !inRect(trayAnchor(traySys), cp) {
|
||||
sysHide()
|
||||
sysPrevBtn = down
|
||||
return
|
||||
}
|
||||
sysPrevBtn = down
|
||||
|
||||
h := -1
|
||||
if inRect(fr, cp) {
|
||||
h = sysRowAt(cp.Y - fr.Top)
|
||||
}
|
||||
changed := h != sysHover
|
||||
sysHover = h
|
||||
if sysCopied != "" && time.Since(sysCopiedAt) > 1500*time.Millisecond {
|
||||
sysCopied = ""
|
||||
changed = true
|
||||
}
|
||||
if changed {
|
||||
pInvalidateRect.Call(sysHwnd, 0, 0)
|
||||
}
|
||||
}
|
||||
|
||||
func sysCopy(text, what string) {
|
||||
if clipboardSet(text) {
|
||||
sysCopied = what
|
||||
} else {
|
||||
sysCopied = "Copy failed"
|
||||
}
|
||||
sysCopiedAt = time.Now()
|
||||
pInvalidateRect.Call(sysHwnd, 0, 0)
|
||||
}
|
||||
|
||||
func sysClick(x, y int32) {
|
||||
if y < px(sysHead) {
|
||||
sysCopy(sysText(sysRows), "Copied everything")
|
||||
return
|
||||
}
|
||||
if i := sysRowAt(y); i >= 0 {
|
||||
sysCopy(sysRows[i].label+": "+sysRows[i].value, "Copied "+sysRows[i].label)
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- painting ----------
|
||||
|
||||
func sysDraw(hdc uintptr, W, H int32) {
|
||||
u := px
|
||||
fillRaw(hdc, rect{0, 0, W, H}, iconColor()) // the outline follows the tray icon color
|
||||
fillRaw(hdc, rect{u(1), u(1), W - u(1), H - u(1)}, colBack)
|
||||
|
||||
pad := u(14)
|
||||
line := uintptr(dtLeft | dtVCenter | dtSingleLine | dtEndEllipsis)
|
||||
wxText(hdc, sysUserHost(), rect{pad, u(10), W - pad - u(150), u(34)}, fMed, colText, line)
|
||||
btn, col := "Copy all", colAccent
|
||||
if sysCopied != "" {
|
||||
btn, col = sysCopied, colDim
|
||||
}
|
||||
wxText(hdc, btn, rect{W - pad - u(150), u(12), W - pad, u(32)}, fBold, col, dtRight|dtVCenter|dtSingleLine|dtEndEllipsis)
|
||||
fillRaw(hdc, rect{pad, u(sysHead) - u(2), W - pad, u(sysHead) - u(1)}, colRule)
|
||||
|
||||
labelW := u(96)
|
||||
for i, r := range sysRows {
|
||||
y := u(sysHead) + int32(i)*u(sysRow_)
|
||||
val := colText
|
||||
if i == sysHover {
|
||||
fillRaw(hdc, rect{u(6), y, W - u(6), y + u(sysRow_)}, rgb(40, 40, 48))
|
||||
val = colAccent
|
||||
}
|
||||
wxText(hdc, r.label, rect{pad, y, pad + labelW, y + u(sysRow_)}, fBold, colAccent, line)
|
||||
wxText(hdc, r.value, rect{pad + labelW, y, W - pad, y + u(sysRow_)}, fText, val, line)
|
||||
}
|
||||
y := u(sysHead) + int32(len(sysRows))*u(sysRow_)
|
||||
wxText(hdc, "Click a line to copy it, or the top to copy everything.", rect{pad, y + u(4), W - pad, H - u(2)}, fTiny, colDim, line)
|
||||
}
|
||||
Reference in New Issue
Block a user