MirrorBar+ panels follow the bar transparency, part 2
This commit is contained in:
+166
@@ -0,0 +1,166 @@
|
||||
package main
|
||||
|
||||
// Painting the three panels (weather, news, system stats) as see-through
|
||||
// windows that follow the bar's own background opacity. The panels are painted
|
||||
// the same way the bar is: the content (text, pictures, lines) is drawn twice at
|
||||
// double size, once on black and once on white; comparing the two copies shows
|
||||
// how solid every pixel is. The panel's background color is then added back at
|
||||
// the bar's see-through level, and the result is handed to Windows as a layered
|
||||
// window with a transparency value for every pixel.
|
||||
|
||||
import (
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// flyMatchBar is the "panels match the bar's transparency" setting. When it is
|
||||
// off the panels are solid, as they were before.
|
||||
var flyMatchBar = true
|
||||
|
||||
func flyBgAlpha() uint32 {
|
||||
if flyMatchBar {
|
||||
return uint32(bgAlpha)
|
||||
}
|
||||
return 255
|
||||
}
|
||||
|
||||
// flyFrame draws the one pixel outline around a panel, in the tray icon color.
|
||||
// The background inside it is not painted here; flyRender adds it.
|
||||
func flyFrame(hdc uintptr, W, H int32) {
|
||||
t, col := px(1), iconColor()
|
||||
fillRaw(hdc, rect{0, 0, W, t}, col)
|
||||
fillRaw(hdc, rect{0, H - t, W, H}, col)
|
||||
fillRaw(hdc, rect{0, 0, t, H}, col)
|
||||
fillRaw(hdc, rect{W - t, 0, W, H}, col)
|
||||
}
|
||||
|
||||
// flyLayer paints the content onto a flat color and returns its pixels.
|
||||
func flyLayer(screen uintptr, w, h int32, base uintptr, draw func(hdc uintptr, W, H int32)) []uint32 {
|
||||
mem, _, _ := pCreateCompatibleDC.Call(screen)
|
||||
defer pDeleteDC.Call(mem)
|
||||
bmi := bitmapInfoHeader{Width: w, Height: -h, Planes: 1, BitCount: 32}
|
||||
bmi.Size = uint32(unsafe.Sizeof(bmi))
|
||||
var bits unsafe.Pointer
|
||||
bmp, _, _ := pCreateDIBSection.Call(screen, uintptr(unsafe.Pointer(&bmi)), 0, uintptr(unsafe.Pointer(&bits)), 0, 0)
|
||||
if bmp == 0 || bits == nil {
|
||||
return nil
|
||||
}
|
||||
defer pDeleteObject.Call(bmp)
|
||||
old, _, _ := pSelectObject.Call(mem, bmp)
|
||||
defer pSelectObject.Call(mem, old)
|
||||
|
||||
fillRaw(mem, rect{0, 0, w, h}, base)
|
||||
draw(mem, w, h)
|
||||
pGdiFlush.Call()
|
||||
out := make([]uint32, int(w)*int(h))
|
||||
copy(out, unsafe.Slice((*uint32)(bits), len(out)))
|
||||
return out
|
||||
}
|
||||
|
||||
// flyRender paints one panel and puts it on the screen.
|
||||
func flyRender(hwnd uintptr, draw func(hdc uintptr, W, H int32)) {
|
||||
if hwnd == 0 {
|
||||
return
|
||||
}
|
||||
if v, _, _ := pIsWindowVisible.Call(hwnd); v == 0 {
|
||||
return
|
||||
}
|
||||
var rc rect
|
||||
pGetClientRect.Call(hwnd, uintptr(unsafe.Pointer(&rc)))
|
||||
W, H := rc.Right-rc.Left, rc.Bottom-rc.Top
|
||||
if W <= 0 || H <= 0 {
|
||||
return
|
||||
}
|
||||
const ss = 2
|
||||
|
||||
screen, _, _ := pGetDC.Call(0)
|
||||
defer pReleaseDC.Call(0, screen)
|
||||
|
||||
saved := scale
|
||||
scale *= ss
|
||||
wxMakeFonts()
|
||||
onBlack := flyLayer(screen, W*ss, H*ss, rgb(0, 0, 0), draw)
|
||||
onWhite := flyLayer(screen, W*ss, H*ss, rgb(255, 255, 255), draw)
|
||||
scale = saved
|
||||
n := int(W) * int(H)
|
||||
if len(onBlack) != n*ss*ss || len(onWhite) != len(onBlack) {
|
||||
return
|
||||
}
|
||||
|
||||
mem, _, _ := pCreateCompatibleDC.Call(screen)
|
||||
defer pDeleteDC.Call(mem)
|
||||
bmi := bitmapInfoHeader{Width: W, Height: -H, Planes: 1, BitCount: 32}
|
||||
bmi.Size = uint32(unsafe.Sizeof(bmi))
|
||||
var bits unsafe.Pointer
|
||||
bmp, _, _ := pCreateDIBSection.Call(screen, uintptr(unsafe.Pointer(&bmi)), 0, uintptr(unsafe.Pointer(&bits)), 0, 0)
|
||||
if bmp == 0 || bits == nil {
|
||||
return
|
||||
}
|
||||
defer pDeleteObject.Call(bmp)
|
||||
old, _, _ := pSelectObject.Call(mem, bmp)
|
||||
defer pSelectObject.Call(mem, old)
|
||||
pix := unsafe.Slice((*uint32)(bits), n)
|
||||
|
||||
a := flyBgAlpha()
|
||||
bgR, bgG, bgB := uint32(28)*a/255, uint32(28)*a/255, uint32(34)*a/255 // colBack at its see-through level
|
||||
stride := int(W) * ss
|
||||
for i := range pix {
|
||||
x, y := i%int(W), i/int(W)
|
||||
var br, bg, bb, wr, wg, wb uint32
|
||||
for dy := 0; dy < ss; dy++ {
|
||||
for dx := 0; dx < ss; dx++ {
|
||||
k := (y*ss+dy)*stride + x*ss + dx
|
||||
c, w := onBlack[k], onWhite[k]
|
||||
br += c >> 16 & 0xFF
|
||||
bg += c >> 8 & 0xFF
|
||||
bb += c & 0xFF
|
||||
wr += w >> 16 & 0xFF
|
||||
wg += w >> 8 & 0xFF
|
||||
wb += w & 0xFF
|
||||
}
|
||||
}
|
||||
const cnt = ss * ss
|
||||
br, bg, bb, wr, wg, wb = br/cnt, bg/cnt, bb/cnt, wr/cnt, wg/cnt, wb/cnt
|
||||
d := (sub0(wr, br) + sub0(wg, bg) + sub0(wb, bb)) / 3
|
||||
fa := 255 - min(d, 255) // how solid the content is here
|
||||
fr, fg, fb := min(br, fa), min(bg, fa), min(bb, fa)
|
||||
inv := 255 - fa
|
||||
oa := fa + a*inv/255
|
||||
pix[i] = oa<<24 | (fr+bgR*inv/255)<<16 | (fg+bgG*inv/255)<<8 | (fb + bgB*inv/255)
|
||||
}
|
||||
|
||||
var wr rect
|
||||
pGetWindowRect.Call(hwnd, uintptr(unsafe.Pointer(&wr)))
|
||||
dst := point{wr.Left, wr.Top}
|
||||
sz := size{W, H}
|
||||
src := point{0, 0}
|
||||
blend := blendFunction{Op: 0, Flags: 0, Alpha: 255, Format: 1} // AC_SRC_OVER, AC_SRC_ALPHA
|
||||
pUpdateLayeredWindow.Call(hwnd, screen,
|
||||
uintptr(unsafe.Pointer(&dst)), uintptr(unsafe.Pointer(&sz)),
|
||||
mem, uintptr(unsafe.Pointer(&src)), 0,
|
||||
uintptr(unsafe.Pointer(&blend)), ulwAlpha)
|
||||
}
|
||||
|
||||
// flyRefresh repaints one of the panels.
|
||||
func flyRefresh(hwnd uintptr) {
|
||||
switch hwnd {
|
||||
case wxHwnd:
|
||||
flyRender(hwnd, wxDraw)
|
||||
case newsHwnd:
|
||||
flyRender(hwnd, newsDraw)
|
||||
case sysHwnd:
|
||||
flyRender(hwnd, sysDraw)
|
||||
}
|
||||
}
|
||||
|
||||
// flyRefreshAll repaints whichever panel is open (after a setting changed).
|
||||
func flyRefreshAll() {
|
||||
if wxShown {
|
||||
flyRefresh(wxHwnd)
|
||||
}
|
||||
if newsShown {
|
||||
flyRefresh(newsHwnd)
|
||||
}
|
||||
if sysShown {
|
||||
flyRefresh(sysHwnd)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user