package main // Painting the bar smoothly on a see-through background. // // Drawing icons and text straight onto a dark bitmap leaves a dark fringe // around them once the background is made see-through. So the bar is painted // in layers: one layer with only the backgrounds, and the icons, text and // symbols drawn twice (once on black, once on white) at double size. Comparing // the two copies shows exactly how see-through every edge pixel is, and // shrinking back down gives smooth edges with no dark outline. import ( "math" "os" "path/filepath" "strconv" "strings" "unsafe" ) const ( superSample = 2 mfPopup = 0x0010 menuLogoColorBase = 300 // 40 apart so the color menus can grow past 20 colors menuClockColorBase = 340 menuIconColorBase = 380 ) var ( fontTime2 uintptr fontApp2 uintptr ) // initSuperFonts makes the double size fonts used for the smooth pass. func initSuperFonts() { scale *= superSample fontTime2 = makeFont(px(13), 400) fontApp2 = makeFont(px(12), 600) scale /= superSample } // ---------- colors ---------- type namedColor struct { name string r, g, b int } var palette = []namedColor{ {"White", 245, 245, 245}, {"Blue", 0, 120, 212}, {"Sky blue", 90, 190, 255}, {"Green", 70, 200, 110}, {"Yellow", 255, 205, 60}, {"Orange", 255, 140, 40}, {"Red", 235, 70, 70}, {"Pink", 255, 125, 195}, {"Purple", 175, 115, 240}, {"Black", 0, 0, 0}, {"Dark green", 20, 120, 55}, {"Teal", 0, 160, 160}, {"Navy", 30, 60, 160}, {"Maroon", 140, 30, 50}, {"Gray", 140, 140, 150}, {"Lime", 150, 220, 40}, {"Brown", 150, 100, 60}, {"Mint", 150, 230, 200}, {"Lavender", 190, 170, 240}, {"Gold", 210, 170, 50}, {"Coral", 255, 120, 100}, {"Silver", 200, 200, 210}, {"Slate", 90, 105, 125}, {"Olive", 130, 140, 50}, } var ( logoIdx = 1 // Blue, like the real Windows logo clockIdx = 0 // White iconIdx = 0 // White: battery, volume, wifi and the hidden-icons arrow ) func colorOf(i int) uintptr { if i < 0 || i >= len(palette) { i = 0 } c := palette[i] r, g, b := styleColor(c.r, c.g, c.b) return rgb(r, g, b) } func logoColor() uintptr { return colorOf(logoIdx) } func clockColor() uintptr { return colorOf(clockIdx) } func iconColor() uintptr { return colorOf(iconIdx) } // iconDim is the color of the "off" bars on the volume and wifi icons. func iconDim() uintptr { c := palette[iconIdx] if c.r+c.g+c.b < 300 { // a dark icon color: the off bars are lighter return rgb(140, 140, 150) } return rgb(110, 110, 120) } func colorsFile() string { return filepath.Join(dataDir(), "colors.txt") } func loadColors() { b, err := os.ReadFile(colorsFile()) if err != nil { return } parts := strings.Split(strings.TrimSpace(string(b)), ",") if len(parts) < 2 || len(parts) > 3 { return } vals := []int{0, 0, 0} for i, p := range parts { n, err := strconv.Atoi(p) if err != nil || n < 0 || n >= len(palette) { return } vals[i] = n } logoIdx, clockIdx, iconIdx = vals[0], vals[1], vals[2] } func saveColors() { _ = os.MkdirAll(dataDir(), 0o755) _ = os.WriteFile(colorsFile(), []byte(strconv.Itoa(logoIdx)+","+strconv.Itoa(clockIdx)+","+strconv.Itoa(iconIdx)), 0o644) } // addColorMenus adds the two color pickers as sub menus. func addColorMenus(menu uintptr) { build := func(base uintptr, cur int, title string) { sub, _, _ := pCreatePopupMenu.Call() for i, c := range palette { var flags uintptr if i == cur { flags = mfChecked } pAppendMenu.Call(sub, flags, base+uintptr(i), uintptr(unsafe.Pointer(u16(c.name)))) } pAppendMenu.Call(menu, mfPopup, sub, uintptr(unsafe.Pointer(u16(title)))) } build(menuLogoColorBase, logoIdx, "Windows logo color") build(menuClockColorBase, clockIdx, "Clock color") build(menuIconColorBase, iconIdx, "Tray icon color") addStyleMenu(menu) } func handleColorCommand(cmd uintptr) bool { n := uintptr(len(palette)) switch { case cmd >= menuLogoColorBase && cmd < menuLogoColorBase+n: logoIdx = int(cmd - menuLogoColorBase) case cmd >= menuClockColorBase && cmd < menuClockColorBase+n: clockIdx = int(cmd - menuClockColorBase) case cmd >= menuIconColorBase && cmd < menuIconColorBase+n: iconIdx = int(cmd - menuIconColorBase) case handleStyleCommand(cmd): return true default: return false } saveColors() return true } // ---------- painting ---------- // renderLayer paints one layer of the bar (w by h in the horizontal layout) // and returns its pixels at the real orientation. fills and fg choose which // parts are painted; base is the color everything is painted onto. func renderLayer(screen uintptr, w, h int32, fills, fg bool, base uintptr) []uint32 { layerFills, layerFg = fills, fg defer func() { layerFills, layerFg = true, true }() 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) sr := startRect(w, h) if hoverStart { fill(mem, sr, rgb(58, 58, 68)) } drawLogo(mem, sr) drawApps(mem, w, h) drawTray(mem, w, h) pGdiFlush.Call() src := unsafe.Slice((*uint32)(bits), int(w)*int(h)) if !vertical { out := make([]uint32, len(src)) copy(out, src) return out } // Turn a quarter turn, then put the icons and symbols back on upright. fw, fh := h, w bmiV := bitmapInfoHeader{Width: fw, Height: -fh, Planes: 1, BitCount: 32} bmiV.Size = uint32(unsafe.Sizeof(bmiV)) var bitsV unsafe.Pointer bmpV, _, _ := pCreateDIBSection.Call(screen, uintptr(unsafe.Pointer(&bmiV)), 0, uintptr(unsafe.Pointer(&bitsV)), 0, 0) if bmpV == 0 || bitsV == nil { return nil } defer pDeleteObject.Call(bmpV) memV, _, _ := pCreateCompatibleDC.Call(screen) defer pDeleteDC.Call(memV) oldV, _, _ := pSelectObject.Call(memV, bmpV) defer pSelectObject.Call(memV, oldV) dst := unsafe.Slice((*uint32)(bitsV), int(w)*int(h)) for y := int32(0); y < h; y++ { for x := int32(0); x < w; x++ { dst[int(x)*int(fw)+int(h-1-y)] = src[int(y)*int(w)+int(x)] } } if fg { if logoStyle != 0 { drawLogoStyle(memV, mapRect(sr, h)) } for i, r := range appRects(w, h) { if i < len(apps) { drawAppGlyph(memV, mapRect(rect{r.Left + px(2), r.Top + px(3), r.Right - px(2), r.Bottom - px(3)}, h), apps[i]) } } for _, it := range trayItems(w, h) { r := mapRect(it.r, h) switch it.kind { case trayBattery: drawBattery(memV, r) case trayVolume: drawVolume(memV, r) case trayWifi: drawNetwork(memV, r) case trayChevron: drawChevron(memV, r) case trayWeather: drawWeatherIcon(memV, r) case trayNews: drawNewsIcon(memV, r) case traySys: drawSysIcon(memV, r) case trayGM: drawGMIcon(memV, r) case trayEmoji: drawEmojiIcon(memV, r) case trayClip: drawClipIcon(memV, r) case trayAI: drawAIIcon(memV, r) case trayMusic: drawMusicIcon(memV, r) } } pGdiFlush.Call() } out := make([]uint32, len(dst)) copy(out, dst) return out } // avg4 averages the superSample x superSample block of pixels that makes up // one real pixel, one color channel at a time. func avg4(p []uint32, stride, x, y int) (r, g, b uint32) { var sr, sg, sb uint32 for dy := 0; dy < superSample; dy++ { for dx := 0; dx < superSample; dx++ { c := p[(y*superSample+dy)*stride+x*superSample+dx] sr += c >> 16 & 0xFF sg += c >> 8 & 0xFF sb += c & 0xFF } } n := uint32(superSample * superSample) return sr / n, sg / n, sb / n } // redraw paints the whole bar and hands it to Windows as a layered window // with a transparency value for every pixel. func redraw(hwnd uintptr) { if hwnd == 0 { return } // Shrink or grow the pill to fit what is in it, staying centered where it is. pw, ph := pillSize() var crc rect pGetClientRect.Call(hwnd, uintptr(unsafe.Pointer(&crc))) if (crc.Right != pw || crc.Bottom != ph) && !dragging { x, y := pillPos(pw, ph) pMoveWindow.Call(hwnd, uintptr(x), uintptr(y), uintptr(pw), uintptr(ph), 0) } w, h := clientSize(hwnd) if w <= 0 || h <= 0 { return } fw, fh := w, h if vertical { fw, fh = h, w } screen, _, _ := pGetDC.Call(0) defer pReleaseDC.Call(0, screen) // Layer 1: backgrounds, at real size. bgPix := renderLayer(screen, w, h, true, false, rgb(22, 22, 26)) // Layers 2 and 3: icons, text and symbols on black and on white, at double size. savedScale, savedTime, savedApp := scale, fontTime, fontApp scale *= superSample fontTime, fontApp = fontTime2, fontApp2 onBlack := renderLayer(screen, w*superSample, h*superSample, false, true, rgb(0, 0, 0)) onWhite := renderLayer(screen, w*superSample, h*superSample, false, true, rgb(255, 255, 255)) scale, fontTime, fontApp = savedScale, savedTime, savedApp n := int(fw) * int(fh) if len(bgPix) != n || len(onBlack) != n*superSample*superSample || len(onWhite) != len(onBlack) { return } // The finished bitmap that Windows will show. mem, _, _ := pCreateCompatibleDC.Call(screen) defer pDeleteDC.Call(mem) bmi := bitmapInfoHeader{Width: fw, Height: -fh, 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 := uint32(bgAlpha) hoverA := max(a, 120) barC := barRGB() lightBar := barIsLight() length := float64(max(fw, fh)) rad := float64(min(fw, fh)) / 2 stride := int(fw) * superSample for i := range pix { x, y := i%int(fw), i/int(fw) // Background layer: the pill color or a hover highlight, at its own see-through level. c := bgPix[i] & 0x00FFFFFF bgA := a if c != colBg { bgA = hoverA c = hoverTint(c, lightBar) } else { c = barC } bgR := (c >> 16 & 0xFF) * bgA / 255 bgG := (c >> 8 & 0xFF) * bgA / 255 bgB := (c & 0xFF) * bgA / 255 // Foreground: how solid is this pixel, and what color is it. br, bgn, bb := avg4(onBlack, stride, x, y) wr, wg, wb := avg4(onWhite, stride, x, y) d := (sub0(wr, br) + sub0(wg, bgn) + sub0(wb, bb)) / 3 fa := 255 - min(d, 255) // 255 where the two copies match (solid), 0 where they differ fully fr, fg, fb := min(br, fa), min(bgn, fa), min(bb, fa) // Foreground over background. inv := 255 - fa oa := fa + bgA*inv/255 or := fr + bgR*inv/255 og := fg + bgG*inv/255 ob := fb + bgB*inv/255 // Rounded ends: how much of this pixel is inside the pill. along := float64(x) + 0.5 across := float64(y) + 0.5 if vertical { along, across = across, along } da := 0.0 if along < rad { da = rad - along } else if along > length-rad { da = along - (length - rad) } cov := math.Max(0, math.Min(1, rad-math.Hypot(da, across-rad)+0.5)) sc := func(v uint32) uint32 { return uint32(float64(v) * cov) } pix[i] = sc(oa)<<24 | sc(or)<<16 | sc(og)<<8 | sc(ob) } var wr rect pGetWindowRect.Call(hwnd, uintptr(unsafe.Pointer(&wr))) dst := point{wr.Left, wr.Top} sz := size{fw, fh} 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) } func sub0(a, b uint32) uint32 { if a > b { return a - b } return 0 }