diff --git a/features.go b/features.go index 1f0a551..a2c61f4 100644 --- a/features.go +++ b/features.go @@ -36,12 +36,14 @@ func loadFeatures() { showNews = on case "sys": showSys = on + case "panelfade": + flyMatchBar = on } } } func saveFeatures() { - s := "weather=" + b01(showWeather) + "\nnews=" + b01(showNews) + "\nsys=" + b01(showSys) + "\n" + s := "weather=" + b01(showWeather) + "\nnews=" + b01(showNews) + "\nsys=" + b01(showSys) + "\npanelfade=" + b01(flyMatchBar) + "\n" _ = os.MkdirAll(dataDir(), 0o755) _ = os.WriteFile(featuresFile(), []byte(s), 0o644) } @@ -96,6 +98,17 @@ func setShowSys(on bool) { featuresChanged() } +// setFlyMatchBar is called by the settings window: should the panels be as +// see-through as the bar? +func setFlyMatchBar(on bool) { + if flyMatchBar == on { + return + } + flyMatchBar = on + saveFeatures() + flyRefreshAll() +} + func featuresChanged() { settingsZipStatusUpdate() redraw(hwndMain) diff --git a/flyrender.go b/flyrender.go new file mode 100644 index 0000000..b54830e --- /dev/null +++ b/flyrender.go @@ -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) + } +} diff --git a/settings.go b/settings.go index a8264d5..f12749c 100644 --- a/settings.go +++ b/settings.go @@ -71,6 +71,7 @@ const ( idShowWeather = 213 idShowNews = 214 idShowSys = 215 + idPanelFade = 216 ) var ( @@ -81,7 +82,7 @@ var ( ctlAlpha, ctlAlphaValue, ctlLogo, ctlClock, ctlTrayIcons, ctlStyle uintptr ctlVertical, ctlAutostart uintptr ctlZip, ctlZipStatus uintptr - ctlShowWeather, ctlShowNews, ctlShowSys uintptr + ctlShowWeather, ctlShowNews, ctlShowSys, ctlPanelFade uintptr zipSyncing bool // true while the program itself is filling in the ZIP box ) @@ -133,7 +134,7 @@ func openSettings() { settingsFont = makeFont(px(13), 400) } - r := rect{0, 0, px(400), px(460)} + r := rect{0, 0, px(400), px(488)} pAdjustWindowRectEx.Call(uintptr(unsafe.Pointer(&r)), wsWindowFrame, 0, 0) ow, oh := r.Right-r.Left, r.Bottom-r.Top sw, sh := screenSize() @@ -183,12 +184,13 @@ func openSettings() { ctlShowWeather = makeControl(hw, inst, "BUTTON", "Show weather on the bar", bsAutoCheckBox|wsTabStop, 16, 262, 368, 22, idShowWeather) ctlShowNews = makeControl(hw, inst, "BUTTON", "Show local news on the bar", bsAutoCheckBox|wsTabStop, 16, 290, 368, 22, idShowNews) ctlShowSys = makeControl(hw, inst, "BUTTON", "Show system stats on the bar", bsAutoCheckBox|wsTabStop, 16, 318, 368, 22, idShowSys) - ctlVertical = makeControl(hw, inst, "BUTTON", "Stand the bar up at the right edge (vertical)", bsAutoCheckBox|wsTabStop, 16, 346, 368, 22, idVertical) - ctlAutostart = makeControl(hw, inst, "BUTTON", "Start MirrorBar when I sign in", bsAutoCheckBox|wsTabStop, 16, 374, 368, 22, idAutostart) + ctlPanelFade = makeControl(hw, inst, "BUTTON", "Panels match the bar's transparency", bsAutoCheckBox|wsTabStop, 16, 346, 368, 22, idPanelFade) + ctlVertical = makeControl(hw, inst, "BUTTON", "Stand the bar up at the right edge (vertical)", bsAutoCheckBox|wsTabStop, 16, 374, 368, 22, idVertical) + ctlAutostart = makeControl(hw, inst, "BUTTON", "Start MirrorBar when I sign in", bsAutoCheckBox|wsTabStop, 16, 402, 368, 22, idAutostart) - makeControl(hw, inst, "BUTTON", "Reset position", bsPushButton|wsTabStop, 16, 414, 116, 28, idResetPos) - makeControl(hw, inst, "BUTTON", "Reset bar width", bsPushButton|wsTabStop, 140, 414, 116, 28, idResetWidth) - makeControl(hw, inst, "BUTTON", "Close", bsDefPushButton|wsTabStop, 292, 414, 92, 28, idClose) + makeControl(hw, inst, "BUTTON", "Reset position", bsPushButton|wsTabStop, 16, 442, 116, 28, idResetPos) + makeControl(hw, inst, "BUTTON", "Reset bar width", bsPushButton|wsTabStop, 140, 442, 116, 28, idResetWidth) + makeControl(hw, inst, "BUTTON", "Close", bsDefPushButton|wsTabStop, 292, 442, 92, 28, idClose) hwndSettings = hw settingsSync() @@ -210,6 +212,7 @@ func settingsSync() { pSendMessage.Call(ctlShowWeather, bmSetCheck, boolWP(showWeather), 0) pSendMessage.Call(ctlShowNews, bmSetCheck, boolWP(showNews), 0) pSendMessage.Call(ctlShowSys, bmSetCheck, boolWP(showSys), 0) + pSendMessage.Call(ctlPanelFade, bmSetCheck, boolWP(flyMatchBar), 0) pSendMessage.Call(ctlVertical, bmSetCheck, boolWP(vertical), 0) pSendMessage.Call(ctlAutostart, bmSetCheck, boolWP(autostartEnabled()), 0) @@ -289,6 +292,7 @@ func settingsCommand(id, note uintptr) { } saveColors() redraw(hwndMain) + flyRefreshAll() case idStyle: if note != cbnSelChange { return @@ -309,6 +313,8 @@ func settingsCommand(id, note uintptr) { setShowNews(checked(ctlShowNews)) case idShowSys: setShowSys(checked(ctlShowSys)) + case idPanelFade: + setFlyMatchBar(checked(ctlPanelFade)) case idVertical: vertical = checked(ctlVertical) saveOrient() @@ -343,6 +349,7 @@ func settingsProc(hwnd, m, wparam, lparam uintptr) (result uintptr) { bgAlpha = n pSetWindowText.Call(ctlAlphaValue, uintptr(unsafe.Pointer(u16(strconv.Itoa(bgAlpha*100/255)+"%")))) redraw(hwndMain) + flyRefreshAll() } if wparam&0xFFFF == tbEndTrack { saveAlpha()