From 9e6b24268f07444ba2ee84e043c11a5401518756 Mon Sep 17 00:00:00 2001 From: weishaupt <1+weishaupt@noreply.localhost> Date: Fri, 6 Nov 2026 16:00:43 +0000 Subject: [PATCH] Icon style (colorized or grayscale), part 2, README update --- README.md | 1 + iconstyle.go | 198 +++++++++++++++++++++++++++++++++++++++++++++++++++ preview.go | 4 +- 3 files changed, 201 insertions(+), 2 deletions(-) create mode 100644 iconstyle.go diff --git a/README.md b/README.md index 0bad1da..3d8c258 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ The real taskbar is hidden and a see-through pill takes its place. The pill is a - "Open notification center" is in the right-click menu. - Smooth, outline-free icons and text, even when the pill is see-through. - Pick the color of the Windows logo, the clock, and the battery/volume/wifi/arrow icons from the right-click menu (white, blue, sky blue, green, yellow, orange, red, pink, purple, black). +- "Icon style" in the right-click menu: colorized or grayscale. It applies to the app icons and to the logo, clock and tray icons. - Background see-through level: Clear, Light, Medium or Solid. - "Start MirrorBar when I sign in" in the right-click menu. - Scroll over the speaker to change the volume. Click the clock, battery, volume or wifi for the matching Windows panel. diff --git a/iconstyle.go b/iconstyle.go new file mode 100644 index 0000000..fe702a8 --- /dev/null +++ b/iconstyle.go @@ -0,0 +1,198 @@ +package main + +// Icon style: colorized or grayscale. It applies to the app +// icons (their pixels are filtered) and to everything MirrorBar draws itself +// (the logo, clock, battery, volume, wifi and arrow colors). + +import ( + "os" + "path/filepath" + "strconv" + "strings" + "unsafe" +) + +const ( + modeColor = 0 + modeGray = 1 + + menuStyleBase = 160 +) + +var ( + pGetIconInfo = user32.NewProc("GetIconInfo") + pCreateIconIndirect = user32.NewProc("CreateIconIndirect") + pDestroyIcon = user32.NewProc("DestroyIcon") + pGetObject = gdi32.NewProc("GetObjectW") + pGetDIBits = gdi32.NewProc("GetDIBits") + + iconMode = modeColor + styleCache = map[styleKey]uintptr{} +) + +type styleKey struct { + icon uintptr + mode int +} + +type iconInfo struct { + FIcon int32 + XHot uint32 + YHot uint32 + Mask uintptr + Color uintptr +} + +type bitmapObj struct { + Type int32 + Width int32 + Height int32 + WidthBytes int32 + Planes uint16 + BitsPixel uint16 + Bits uintptr +} + +var styleNames = []string{"Colorized", "Grayscale"} + +func styleFile() string { return filepath.Join(dataDir(), "iconstyle.txt") } + +func loadIconMode() { + b, err := os.ReadFile(styleFile()) + if err != nil { + return + } + if n, err := strconv.Atoi(strings.TrimSpace(string(b))); err == nil && n >= 0 && n < len(styleNames) { + iconMode = n + } +} + +func saveIconMode() { + _ = os.MkdirAll(dataDir(), 0o755) + _ = os.WriteFile(styleFile(), []byte(strconv.Itoa(iconMode)), 0o644) +} + +// styleCacheClear throws away the filtered copies of icons. +func styleCacheClear() { + for _, v := range styleCache { + if v != 0 { + pDestroyIcon.Call(v) + } + } + styleCache = map[styleKey]uintptr{} +} + +// styledIcon is the icon as it should be drawn in the current style. +func styledIcon(ic uintptr) uintptr { + if ic == 0 || iconMode == modeColor { + return ic + } + k := styleKey{ic, iconMode} + if v, ok := styleCache[k]; ok { + if v != 0 { + return v + } + return ic + } + v := makeStyledIcon(ic, iconMode) + if len(styleCache) > 300 { + styleCacheClear() + } + styleCache[k] = v + if v == 0 { + return ic + } + return v +} + +// makeStyledIcon returns a filtered copy of an icon, or 0 if it can't be done +// (the original is then drawn as it is). +func makeStyledIcon(ic uintptr, mode int) (result uintptr) { + defer func() { + if r := recover(); r != nil { + result = 0 + } + }() + var ii iconInfo + r, _, _ := pGetIconInfo.Call(ic, uintptr(unsafe.Pointer(&ii))) + if r == 0 { + return 0 + } + defer func() { + if ii.Mask != 0 { + pDeleteObject.Call(ii.Mask) + } + if ii.Color != 0 { + pDeleteObject.Call(ii.Color) + } + }() + if ii.Color == 0 { + return 0 // a two-color icon, leave it + } + var bm bitmapObj + pGetObject.Call(ii.Color, unsafe.Sizeof(bm), uintptr(unsafe.Pointer(&bm))) + w, h := int(bm.Width), int(bm.Height) + if w <= 0 || h <= 0 || w > 512 || h > 512 { + return 0 + } + + hdc, _, _ := pGetDC.Call(0) + defer pReleaseDC.Call(0, hdc) + bmi := bitmapInfoHeader{Width: int32(w), Height: int32(-h), Planes: 1, BitCount: 32} + bmi.Size = uint32(unsafe.Sizeof(bmi)) + pix := make([]uint32, w*h) + n, _, _ := pGetDIBits.Call(hdc, ii.Color, 0, uintptr(h), uintptr(unsafe.Pointer(&pix[0])), + uintptr(unsafe.Pointer(&bmi)), 0) + if n == 0 { + return 0 + } + + for i, p := range pix { + a := p >> 24 + cr, cg, cb := p>>16&0xFF, p>>8&0xFF, p&0xFF + y := (cr*299 + cg*587 + cb*114) / 1000 // works on premultiplied pixels too + pix[i] = a<<24 | y<<16 | y<<8 | y + } + + var bits unsafe.Pointer + hb, _, _ := pCreateDIBSection.Call(hdc, uintptr(unsafe.Pointer(&bmi)), 0, uintptr(unsafe.Pointer(&bits)), 0, 0) + if hb == 0 || bits == nil { + return 0 + } + copy(unsafe.Slice((*uint32)(bits), w*h), pix) + ni := iconInfo{FIcon: 1, Mask: ii.Mask, Color: hb} + nh, _, _ := pCreateIconIndirect.Call(uintptr(unsafe.Pointer(&ni))) + pDeleteObject.Call(hb) + return nh +} + +// styleColor applies the style to a color MirrorBar draws itself. +func styleColor(r, g, b int) (int, int, int) { + if iconMode == modeGray { + y := (r*299 + g*587 + b*114) / 1000 + return y, y, y + } + return r, g, b +} + +func addStyleMenu(menu uintptr) { + sub, _, _ := pCreatePopupMenu.Call() + for i, name := range styleNames { + var flags uintptr + if i == iconMode { + flags = mfChecked + } + pAppendMenu.Call(sub, flags, uintptr(menuStyleBase+i), uintptr(unsafe.Pointer(u16(name)))) + } + pAppendMenu.Call(menu, mfPopup, sub, uintptr(unsafe.Pointer(u16("Icon style")))) +} + +func handleStyleCommand(cmd uintptr) bool { + if cmd < menuStyleBase || cmd >= menuStyleBase+uintptr(len(styleNames)) { + return false + } + iconMode = int(cmd - menuStyleBase) + saveIconMode() + styleCacheClear() + return true +} diff --git a/preview.go b/preview.go index d5c3bb7..5196378 100644 --- a/preview.go +++ b/preview.go @@ -122,7 +122,7 @@ func previewPaint(hwnd uintptr) { isz := px(16) if pvIcon != 0 { y := pvTitleR.Top + (pvTitleR.Bottom-pvTitleR.Top-isz)/2 - pDrawIconEx.Call(hdc, uintptr(pvTitleR.Left), uintptr(y), pvIcon, uintptr(isz), uintptr(isz), 0, 0, diNormal) + 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)) @@ -136,7 +136,7 @@ func previewPaint(hwnd uintptr) { 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), pvIcon, uintptr(big), uintptr(big), 0, 0, diNormal) + 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))) }