diff --git a/look.go b/look.go new file mode 100644 index 0000000..9160b71 --- /dev/null +++ b/look.go @@ -0,0 +1,325 @@ +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" +) + +var pRoundRect = gdi32.NewProc("RoundRect") + +// ---------- 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}, +} + +var ( + barIdx = 0 + logoStyle = 0 + clockSizeIx = 0 + clockBold = false +) + +var logoStyleNames = []string{ + "Classic squares", + "Rounded squares", + "Four dots", + "Tilted right", + "Tilted left", + "Diamonds", + "Waving flag", +} + +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 { + 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 + } + oldA, oldB := fontTime, fontTime2 + fontTime = makeFont(px(float64(clockPx())), weight) + scale *= superSample + fontTime2 = makeFont(px(float64(clockPx())), weight) + scale /= superSample + if oldA != 0 { + pDeleteObject.Call(oldA) + } + if oldB != 0 { + pDeleteObject.Call(oldB) + } +} + +// ---------- 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 + } + } +} + +func saveLook() { + s := "barcolor=" + strconv.Itoa(barIdx) + + "\nlogostyle=" + strconv.Itoa(logoStyle) + + "\nclocksize=" + strconv.Itoa(clockSizeIx) + + "\nclockbold=" + b01(clockBold) + "\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 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) + } + } +}