Files
MirrorBar/look.go
T
weishauptandClaude Sonnet 5.5 7ffd3c5e22 MirrorBar Plus 11: fix clock font zone width so other fonts are centered
The measured clock width was a fixed pixel count, so the supersampled drawing pass used a zone half as wide as it needed. It is now stored at 96 dpi and scaled with the bar.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JW57Zw8fvgzpeDkbUYT7sG
2026-11-20 15:55:55 +00:00

485 lines
11 KiB
Go

package main
// How the bar looks: the color of the bar itself, the shape of the Windows
// logo, the size and weight of the clock text, and which of the battery, volume
// and wifi icons are on the bar. All of it is changed in the settings window.
import (
"math"
"os"
"path/filepath"
"strconv"
"strings"
"unsafe"
)
var (
pRoundRect = gdi32.NewProc("RoundRect")
pGetTextExtentW = gdi32.NewProc("GetTextExtentPoint32W")
)
// ---------- the color of the bar ----------
type barColor struct {
name string
r, g, b int
}
// New colors go on the end so a saved choice keeps meaning the same color.
var barPalette = []barColor{
{"Dark (default)", 22, 22, 26},
{"Black", 0, 0, 0},
{"White", 245, 245, 245},
{"Red", 190, 35, 35},
{"Dark green", 20, 72, 42},
{"Navy", 16, 36, 92},
{"Purple", 72, 38, 112},
{"Gray", 92, 92, 100},
{"Charcoal", 44, 44, 50},
{"Slate", 52, 64, 80},
{"Burgundy", 92, 24, 40},
{"Forest", 30, 56, 30},
{"Brown", 84, 52, 30},
{"Teal", 16, 82, 86},
{"Olive", 74, 80, 32},
{"Midnight", 10, 14, 40},
}
var (
barIdx = 0
logoStyle = 0
clockSizeIx = 0
clockBold = false
clockFontIx = 0
chargeIx = 0
clockMeasured float64 // the width the time and date need in a font other than Segoe UI, at 96 dpi so it scales with the bar
)
var logoStyleNames = []string{
"Classic squares",
"Rounded squares",
"Four dots",
"Tilted right",
"Tilted left",
"Diamonds",
"Waving flag",
"Single circle",
"Hexagon",
"Plus sign",
"Three bars",
"Pills",
"Triangle",
}
// The clock fonts. New ones go on the end so a saved choice keeps meaning the same font.
// A font that is not installed on this PC quietly shows as a default font.
var clockFonts = []string{
"Segoe UI",
"Tahoma",
"Verdana",
"Arial",
"Calibri",
"Cambria",
"Consolas",
"Cascadia Mono",
"Courier New",
"Georgia",
"Trebuchet MS",
"Bahnschrift",
"Tw Cen MT",
"Segoe Script",
"Impact",
"Comic Sans MS",
}
// The color of the filled part of the battery icon while it charges. New ones go on the end.
type chargeChoice struct {
name string
r, g, b int
}
var chargeColors = []chargeChoice{
{"Bright green (default)", 90, 205, 120},
{"Soft green", 105, 165, 120},
{"Dark green", 40, 115, 70},
{"Muted teal", 80, 145, 150},
{"Slate blue", 110, 135, 170},
{"Dim gold", 190, 160, 80},
{"Gray", 150, 150, 158},
{"Same as the icon color", -1, -1, -1},
}
// chargeColor is the fill color of the battery icon while it is charging or plugged in.
func chargeColor() uintptr {
i := chargeIx
if i < 0 || i >= len(chargeColors) {
i = 0
}
c := chargeColors[i]
if c.r < 0 {
return iconColor()
}
return rgb(c.r, c.g, c.b)
}
type clockSizeChoice struct {
name string
px int
}
var clockSizes = []clockSizeChoice{
{"Normal", 13},
{"Large", 15},
{"Larger", 17},
{"Largest", 19},
}
// barRGB is the bar color as it sits in a bitmap (0xRRGGBB).
func barRGB() uint32 {
i := barIdx
if i < 0 || i >= len(barPalette) {
i = 0
}
c := barPalette[i]
return uint32(c.r)<<16 | uint32(c.g)<<8 | uint32(c.b)
}
// barIsLight is true for a bar bright enough that the dark hover fills would look wrong on it.
func barIsLight() bool {
i := barIdx
if i < 0 || i >= len(barPalette) {
i = 0
}
c := barPalette[i]
return (c.r*299+c.g*587+c.b*114)/1000 > 140
}
// hoverTint is the fill used for a hovered button, matched to the bar color.
func hoverTint(c uint32, light bool) uint32 {
if light {
return 200<<16 | 200<<8 | 208
}
return c
}
// ---------- the clock text ----------
func clockPx() int {
i := clockSizeIx
if i < 0 || i >= len(clockSizes) {
i = 0
}
return clockSizes[i].px
}
// clockWidth is the room the time and date need, which grows with the text.
func clockWidth() int32 {
if clockFontIx > 0 && clockMeasured > 0 {
return px(clockMeasured + 24)
}
w := 140.0 * float64(clockPx()) / 13.0
if clockBold {
w *= 1.08
}
return px(w)
}
// rebuildClockFonts makes the clock fonts again (the normal one and the double
// size one used for the smooth pass) after the size or weight changed.
func rebuildClockFonts() {
weight := 400
if clockBold {
weight = 700
}
face := clockFonts[0]
if clockFontIx > 0 && clockFontIx < len(clockFonts) {
face = clockFonts[clockFontIx]
}
oldA, oldB := fontTime, fontTime2
fontTime = makeFontFace(face, px(float64(clockPx())), weight)
scale *= superSample
fontTime2 = makeFontFace(face, px(float64(clockPx())), weight)
scale /= superSample
clockMeasured = float64(measureClockText()) / scale
if oldA != 0 {
pDeleteObject.Call(oldA)
}
if oldB != 0 {
pDeleteObject.Call(oldB)
}
}
// measureClockText is how wide the longest time and date are in the clock font, in real pixels.
func measureClockText() int32 {
hdc, _, _ := pGetDC.Call(0)
if hdc == 0 {
return 0
}
old, _, _ := pSelectObject.Call(hdc, fontTime)
var sz struct{ cx, cy int32 }
sample := "12:59 PM 12/30/2026"
pGetTextExtentW.Call(hdc, uintptr(unsafe.Pointer(u16(sample))), uintptr(len(sample)), uintptr(unsafe.Pointer(&sz)))
pSelectObject.Call(hdc, old)
pReleaseDC.Call(0, hdc)
return sz.cx
}
// ---------- remembering the choices ----------
func lookFile() string { return filepath.Join(dataDir(), "look.txt") }
func loadLook() {
b, err := os.ReadFile(lookFile())
if err != nil {
return
}
for _, ln := range strings.Split(string(b), "\n") {
k, v, ok := strings.Cut(strings.TrimSpace(ln), "=")
if !ok {
continue
}
n, err := strconv.Atoi(strings.TrimSpace(v))
if err != nil {
continue
}
switch strings.TrimSpace(k) {
case "barcolor":
if n >= 0 && n < len(barPalette) {
barIdx = n
}
case "logostyle":
if n >= 0 && n < len(logoStyleNames) {
logoStyle = n
}
case "clocksize":
if n >= 0 && n < len(clockSizes) {
clockSizeIx = n
}
case "clockbold":
clockBold = n != 0
case "clockfont":
if n >= 0 && n < len(clockFonts) {
clockFontIx = n
}
case "chargecolor":
if n >= 0 && n < len(chargeColors) {
chargeIx = n
}
}
}
}
func saveLook() {
s := "barcolor=" + strconv.Itoa(barIdx) +
"\nlogostyle=" + strconv.Itoa(logoStyle) +
"\nclocksize=" + strconv.Itoa(clockSizeIx) +
"\nclockbold=" + b01(clockBold) +
"\nclockfont=" + strconv.Itoa(clockFontIx) +
"\nchargecolor=" + strconv.Itoa(chargeIx) + "\n"
_ = os.MkdirAll(dataDir(), 0o755)
_ = os.WriteFile(lookFile(), []byte(s), 0o644)
}
// ---------- the settings window calls these ----------
func setBarColor(i int) {
if i < 0 || i >= len(barPalette) || i == barIdx {
return
}
barIdx = i
saveLook()
redraw(hwndMain)
}
func setLogoStyle(i int) {
if i < 0 || i >= len(logoStyleNames) || i == logoStyle {
return
}
logoStyle = i
saveLook()
redraw(hwndMain)
}
func setClockSize(i int) {
if i < 0 || i >= len(clockSizes) || i == clockSizeIx {
return
}
clockSizeIx = i
rebuildClockFonts()
saveLook()
positionBar(hwndMain)
}
func setClockBold(on bool) {
if clockBold == on {
return
}
clockBold = on
rebuildClockFonts()
saveLook()
positionBar(hwndMain)
}
func setClockFont(i int) {
if i < 0 || i >= len(clockFonts) || i == clockFontIx {
return
}
clockFontIx = i
rebuildClockFonts()
saveLook()
positionBar(hwndMain)
}
func setChargeColor(i int) {
if i < 0 || i >= len(chargeColors) || i == chargeIx {
return
}
chargeIx = i
saveLook()
redraw(hwndMain)
}
func setShowEmoji(on bool) {
if showEmoji == on {
return
}
showEmoji = on
saveFeatures()
positionBar(hwndMain)
}
func setShowBattery(on bool) {
if showBattery == on {
return
}
showBattery = on
saveFeatures()
positionBar(hwndMain)
}
func setShowVolume(on bool) {
if showVolume == on {
return
}
showVolume = on
saveFeatures()
positionBar(hwndMain)
}
func setShowWifi(on bool) {
if showWifi == on {
return
}
showWifi = on
saveFeatures()
positionBar(hwndMain)
}
// ---------- the Windows logo ----------
// roundRectRaw paints a filled rounded square with no outline.
func roundRectRaw(hdc uintptr, l, t, r, b, rad int32, color uintptr) {
brush, _, _ := pCreateSolidBrush.Call(color)
nullPen, _, _ := pGetStockObject.Call(8) // NULL_PEN
oldBrush, _, _ := pSelectObject.Call(hdc, brush)
oldPen, _, _ := pSelectObject.Call(hdc, nullPen)
pRoundRect.Call(hdc, uintptr(l), uintptr(t), uintptr(r), uintptr(b), uintptr(rad*2), uintptr(rad*2))
pSelectObject.Call(hdc, oldPen)
pSelectObject.Call(hdc, oldBrush)
pDeleteObject.Call(brush)
}
// drawLogoStyle draws the Windows logo in one of the shapes other than the
// classic four squares. The four panes are laid out around the center of r.
func drawLogoStyle(hdc uintptr, r rect) {
if !layerFg {
return
}
cx, cy := center(r)
c := logoColor()
sq := float64(px(8)) // one pane
gap := float64(px(2)) // the space between panes
if logoStyle == 5 {
sq, gap = float64(px(6)), float64(px(2)) // a diamond is wider than it looks, so shrink it
}
o := (sq + gap) / 2 // how far each pane's middle is from the logo's middle
h := sq / 2
offs := [4][2]float64{{-o, -o}, {o, -o}, {-o, o}, {o, o}}
// pane builds one pane's corners, moved by the transform for this style.
pane := func(ox, oy float64, tf func(x, y float64) (float64, float64)) []point {
corners := [4][2]float64{{ox - h, oy - h}, {ox + h, oy - h}, {ox + h, oy + h}, {ox - h, oy + h}}
pts := make([]point, 4)
for i, p := range corners {
x, y := tf(p[0], p[1])
pts[i] = point{cx + int32(math.Round(x)), cy + int32(math.Round(y))}
}
return pts
}
turn := func(deg float64) func(x, y float64) (float64, float64) {
a := deg * math.Pi / 180
s, co := math.Sin(a), math.Cos(a)
return func(x, y float64) (float64, float64) { return x*co - y*s, x*s + y*co }
}
switch logoStyle {
case 1: // rounded squares
rad := px(2)
for _, f := range offs {
l, t := cx+int32(math.Round(f[0]-h)), cy+int32(math.Round(f[1]-h))
roundRectRaw(hdc, l, t, l+int32(sq), t+int32(sq), rad, c)
}
case 2: // four dots
rad := int32(math.Round(sq * 0.46))
for _, f := range offs {
circleRaw(hdc, cx+int32(math.Round(f[0])), cy+int32(math.Round(f[1])), rad, c)
}
case 3: // tilted right
for _, f := range offs {
polygon(hdc, pane(f[0], f[1], turn(15)), c)
}
case 4: // tilted left
for _, f := range offs {
polygon(hdc, pane(f[0], f[1], turn(-15)), c)
}
case 5: // diamonds
for _, f := range offs {
polygon(hdc, pane(f[0], f[1], turn(45)), c)
}
case 6: // waving flag: every pane leans, and the left side sits lower than the right
wave := func(x, y float64) (float64, float64) {
return x, y - 0.22*x + 0.9*math.Sin(x*0.55)
}
for _, f := range offs {
polygon(hdc, pane(f[0], f[1], wave), c)
}
case 7: // single circle
circleRaw(hdc, cx, cy, px(9), c)
case 8: // hexagon
pts := make([]point, 6)
for k := range pts {
a := (float64(k)*60 - 30) * math.Pi / 180
pts[k] = point{cx + int32(math.Round(float64(px(10))*math.Cos(a))), cy + int32(math.Round(float64(px(10))*math.Sin(a)))}
}
polygon(hdc, pts, c)
case 9: // plus sign
l, t := px(10), px(3)
fillRaw(hdc, rect{cx - l, cy - t, cx + l, cy + t}, c)
fillRaw(hdc, rect{cx - t, cy - l, cx + t, cy + l}, c)
case 10: // three bars
for _, dy := range []int32{-px(6), 0, px(6)} {
fillRaw(hdc, rect{cx - px(9), cy + dy - px(1.5), cx + px(9), cy + dy + px(1.5)}, c)
}
case 11: // pills, two by two
for _, ox := range []int32{-px(10), px(1)} {
for _, oy := range []int32{-px(7), px(1)} {
roundRectRaw(hdc, cx+ox, cy+oy, cx+ox+px(9), cy+oy+px(6), px(3), c)
}
}
case 12: // triangle
polygon(hdc, []point{{cx, cy - px(9)}, {cx + px(10), cy + px(8)}, {cx - px(10), cy + px(8)}}, c)
}
}