RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process
ui: draw scrollbars as a groove with a flat handle
Claude2be0daf995aa
diff --git a/docs/wiki/Theme.md b/docs/wiki/Theme.mdindex b1ce07e..855cfde 100644--- a/docs/wiki/Theme.md+++ b/docs/wiki/Theme.md@@ -186,7 +186,7 @@ geometry retain their content or structural dimensions.| `display_tile_border` | `"rgba(255,255,255,0.16)"` || `scrollbar_thumb` | `"rgba(89,98,110,1)"` || `scrollbar_thumb_active` | `unset` |-| `scrollbar_glow` | `1.0` (strength of the thumb's light) |+| `scrollbar_glow` | `1.0` (strength of the thumb's light; `0` makes the thumb flat, with no highlight stripe) || `bg` | `"rgba(9,11,24,1)"` || `fg` | `"rgba(245,245,245,1)"` || `dim` | `"rgba(214,214,214,1)"` |@@ -451,6 +451,7 @@ as before. Raised shadows reach `relief_depth + relief_softness` past a box: use| `pill_relief` | `"flat"` | Slider value readout (pill) relief || `row_hover_relief` | `"flat"` | Hovered or pressed list row relief || `row_selected_relief` | `"flat"` | Selected list row relief |+| `scrollbar_track_relief` | `"flat"` | Scrollbar channel: `flat` draws no track, `inset` cuts a groove the thumb sits in. Pair with `scrollbar_glow = 0` for a flat thumb || Additional constraint | Range || --- | --- || `chrome_height` | 28–84 |diff --git a/src/ui/AGENTS.md b/src/ui/AGENTS.mdindex 1c8328a..a387fd0 100644--- a/src/ui/AGENTS.md+++ b/src/ui/AGENTS.md@@ -3,7 +3,8 @@The rules every caller follows (callback owners, one look per control, apptext and colours, secrets) are in the root `AGENTS.md` ("Shared UI").-* **The scrollbar** (`widgets/scrollbar.zig`, Files' look): a track-less thumb+* **The scrollbar** (`widgets/scrollbar.zig`, Files' look): a thumb (track-less+ unless `scrollbar_track_relief` cuts a groove; flat at `scrollbar_glow = 0`)centred in a `gutter(scrollbar_width)` strip, thin at rest, widening andtinting to the accent on hover/drag, holding a faint tint after a scroll.`Geometry.compute` gives the thumb for any axis and unit (pixels, or itemsdiff --git a/src/ui/cairo.zig b/src/ui/cairo.zigindex 05ec8c9..64c74c1 100644--- a/src/ui/cairo.zig+++ b/src/ui/cairo.zig@@ -153,17 +153,19 @@ pub fn drawSplitter(cr_any: *anyopaque, geometry: splitter.Geometry, engaged: boc.cairo_fill(cr);}-/// Fills a scrollbar thumb: `scrollbar.look` through Cairo, the counterpart of-/// `scrollbar.paint` for the widget tree.+/// Fills a scrollbar, groove and thumb: `scrollbar.look` through Cairo, the+/// counterpart of `scrollbar.paint` for the widget tree.pub fn drawScrollbar(cr_any: *anyopaque, look: scrollbar.Look) void {const cr: *c.cairo_t = @ptrCast(cr_any);const track = look.track;const thumb = look.rect;- const left = @min(track.x, thumb.x);- const top = @min(track.y, thumb.y);- const right = @max(track.x + track.w, thumb.x + thumb.w);- const bottom = @max(track.y + track.h, thumb.y + thumb.h);+ // Without a groove the layer is the thumb's alone, not the whole gutter.+ const grooved = look.track_relief != .flat;+ const left = if (grooved) @min(track.x, thumb.x) else thumb.x;+ const top = if (grooved) @min(track.y, thumb.y) else thumb.y;+ const right = if (grooved) @max(track.x + track.w, thumb.x + thumb.w) else thumb.x + thumb.w;+ const bottom = if (grooved) @max(track.y + track.h, thumb.y + thumb.h) else thumb.y + thumb.h;if (right <= left or bottom <= top) return;// Snap the combined layer origin to device pixels while preserving@@ -348,7 +350,13 @@ test "a scrollbar thumb paints its look through cairo" {// Six wide at rest, centred in the 14 wide gutter: x 24..30, top half.try std.testing.expectEqual(@as(u32, 0xff0000ff), data[15 * 40 + 27]);try std.testing.expectEqual(@as(u32, 0), data[15 * 40 + 22]);- // The recessed groove continues below the thumb.+ // No groove unless the theme cuts one: nothing below the thumb.+ try std.testing.expectEqual(@as(u32, 0), data[45 * 40 + 27]);+ // With one, the channel continues below it.+ var grooved = palette;+ grooved.scrollbar_track_relief = .inset;+ drawScrollbar(cr, scrollbar.look(g, .{}, 8, grooved));+ c.cairo_surface_flush(surface);try std.testing.expect(data[45 * 40 + 27] != 0);// Engaged thumbs share the shell raster, including fractional placement,diff --git a/src/ui/theme.zig b/src/ui/theme.zigindex 5578419..88b18cc 100644--- a/src/ui/theme.zig+++ b/src/ui/theme.zig@@ -119,7 +119,8 @@ pub const Theme = struct {scrollbar_thumb: [4]f32 = .{ 89.0 / 255.0, 98.0 / 255.0, 110.0 / 255.0, 1 }, // #59626e/// Unset follows the surface accent during hover/drag.scrollbar_thumb_active: ?[4]f32 = null,- /// Strength of the light on a hovered/dragged/scrolling thumb (1 = subtle).+ /// Strength of the light on a hovered/dragged/scrolling thumb (1 = subtle,+ /// 0 = a flat thumb: no highlight stripe, only the colour feedback).scrollbar_glow: f32 = 1.0,taskbar_title_size: f32 = 12.5,clock_bold: bool = false,@@ -253,6 +254,9 @@ pub const Theme = struct {pill_relief: Relief = .flat,row_hover_relief: Relief = .flat,row_selected_relief: Relief = .flat,+ /// The scrollbar's channel (`widgets/scrollbar.zig`): flat draws no track at+ /// all (the thumb alone), inset a groove the thumb sits in.+ scrollbar_track_relief: Relief = .flat,// 0 sizes the menu to the output (`start_menu/size.zig`); set either to pin it.start_menu_width: f32 = 0,start_menu_max_height: f32 = 0,diff --git a/src/ui/widgets/scrollbar.zig b/src/ui/widgets/scrollbar.zigindex 767b647..d8561a7 100644--- a/src/ui/widgets/scrollbar.zig+++ b/src/ui/widgets/scrollbar.zig@@ -1,6 +1,8 @@-// The shell's one scrollbar, as Files draws it: a track-less thumb that sits-// centred in a reserved gutter, thin at rest, widening and tinting toward the-// accent under the pointer, and holding a subtle tint while scrolling. Apps+// The shell's one scrollbar, as Files draws it: a thumb that sits centred in a+// reserved gutter, thin at rest, widening and tinting toward the accent under+// the pointer, and holding a subtle tint while scrolling. It has no track+// unless the theme sets `scrollbar_track_relief`, which cuts a groove for the+// thumb; `scrollbar_glow = 0` makes the thumb itself flat. Apps// keep their own scroll mechanics and ask this module for the geometry, the// paging and drag arithmetic, the look and the feedback animation, so every// scrollbar behaves alike: Files, Images, PDF and the editor through Cairo@@ -137,13 +139,16 @@ pub const Drag = struct {}};-/// What to draw: the thumb's rounded box, colour and illuminated edge.+/// What to draw: the groove (when the theme cuts one), then the thumb's+/// rounded box, colour and illuminated edge.pub const Look = struct {+ /// The channel the thumb runs in, inside the gutter. Drawn only when+ /// `track_relief` is not `.flat`; its shading comes from the theme's+ /// `relief_*` values, never from the accent or the frame's glow.track: Rect,track_radius: f32,track_color: [4]f32,- track_light: [4]f32,- track_dark: [4]f32,+ track_relief: theme.Relief = .flat,rect: Rect,radius: f32,color: [4]f32,@@ -172,14 +177,12 @@ pub fn look(g: Geometry, state: Appearance, configured: f32, palette: theme.Themvar highlight = palette.border_glow;// A pale reflection of the frame's glow reads as light on every palette.for (highlight[0..3]) |*channel| channel.* += (1 - channel.*) * 0.55;- // A narrow, inset-looking groove remains visible even when the thumb is idle.- // Use the theme surface as its base, with a restrained darkening for contrast.- var track_color = palette.surface;- for (track_color[0..3]) |*channel| channel.* *= 0.48;- track_color[3] = @max(track_color[3], 0.22);- const track_light = palette.relief_light;- const track_dark = palette.relief_dark;- const track_thickness: f32 = @min(6.0, gutter_thickness);+ // The groove fills the gutter less a pixel either side, so the thumb, at+ // its widest, still sits inside it with room to spare. Its floor takes the+ // theme's shadow tone; the relief pass shades the edges from `relief_*`.+ const track_thickness: f32 = @max(0, gutter_thickness - 2);+ var track_color = palette.relief_dark;+ track_color[3] = std.math.clamp(track_color[3] * 0.75, 0.12, 0.5);var track_rect = g.track;if (g.axis == .vertical) {track_rect.x += (track_rect.w - track_thickness) / 2;@@ -188,24 +191,38 @@ pub fn look(g: Geometry, state: Appearance, configured: f32, palette: theme.Themtrack_rect.y += (track_rect.h - track_thickness) / 2;track_rect.h = track_thickness;}- return .{ .track = track_rect, .track_radius = track_thickness / 2, .track_color = track_color, .track_light = track_light, .track_dark = track_dark, .rect = rect, .radius = cornerRadius(thickness), .color = color, .axis = g.axis, .highlight = highlight, .shine = state.shine, .glow = @max(palette.scrollbar_glow, 0) };+ // Nested corners: the groove's radius is the thumb's plus the margin between.+ const track_radius = @min(cornerRadius(track_thickness) + 1.5, track_thickness / 2);+ return .{ .track = track_rect, .track_radius = track_radius, .track_color = track_color, .track_relief = palette.scrollbar_track_relief, .rect = rect, .radius = cornerRadius(thickness), .color = color, .axis = g.axis, .highlight = highlight, .shine = state.shine, .glow = @max(palette.scrollbar_glow, 0) };}-/// The light stays inside the thumb, so ordinary scrollbar damage erases it.-/// Cairo clients use this same painter through `ui/cairo.zig`'s `drawScrollbar`.-pub fn paint(r: *@import("../paint.zig").Renderer, l: Look) void {+/// Whether `l`'s thumb gets the lit-edge raster. A flat thumb (`scrollbar_glow`+/// of 0) or an unlit one is a plain rounded fill, in the widget tree and Cairo alike.+pub fn lit(l: Look) bool {+ return l.shine > 0 and l.glow > 0;+}++/// The groove; nothing at all while the theme leaves the track flat. It stays+/// inside the track's own box, so the gutter's damage covers it.+pub fn paintTrack(r: *@import("../paint.zig").Renderer, l: Look) void {+ if (l.track_relief == .flat or l.track.w <= 0 or l.track.h <= 0) return;r.fillRect(l.track.x, l.track.y, l.track.w, l.track.h, .{.color = l.track_color,.radius = l.track_radius,+ .relief = l.track_relief,});- if (l.axis == .vertical) {- r.fillRect(l.track.x, l.track.y, 1, l.track.h, .{ .color = l.track_light });- r.fillRect(l.track.x + l.track.w - 1, l.track.y, 1, l.track.h, .{ .color = l.track_dark });- } else {- r.fillRect(l.track.x, l.track.y, l.track.w, 1, .{ .color = l.track_light });- r.fillRect(l.track.x, l.track.y + l.track.h - 1, l.track.w, 1, .{ .color = l.track_dark });- }- if (l.shine <= 0) {+}++/// The groove, then the thumb. Cairo clients use this same painter through+/// `ui/cairo.zig`'s `drawScrollbar`.+pub fn paint(r: *@import("../paint.zig").Renderer, l: Look) void {+ paintTrack(r, l);+ paintThumb(r, l);+}++/// The light stays inside the thumb, so ordinary scrollbar damage erases it.+pub fn paintThumb(r: *@import("../paint.zig").Renderer, l: Look) void {+ if (!lit(l)) {r.fillRect(l.rect.x, l.rect.y, l.rect.w, l.rect.h, .{ .color = l.color, .radius = l.radius });return;}@@ -424,11 +441,11 @@ test "the thumb is centred in its gutter, widens and tints with feedback" {try std.testing.expectEqual(@as(f32, 86 + 4), rest.rect.x);try std.testing.expectEqual(palette.scrollbar_thumb, rest.color);try std.testing.expectEqual(cornerRadius(6), rest.radius);- try std.testing.expectEqual(@as(f32, 6), rest.track.w);- try std.testing.expectEqual(@as(f32, 90), rest.track.x);+ // The groove is the gutter less a pixel either side, and off unless asked for.+ try std.testing.expectEqual(@as(f32, 12), rest.track.w);+ try std.testing.expectEqual(@as(f32, 87), rest.track.x);+ try std.testing.expectEqual(theme.Relief.flat, rest.track_relief);try std.testing.expect(rest.track_color[3] > 0);- try std.testing.expectEqual(palette.relief_light, rest.track_light);- try std.testing.expectEqual(palette.relief_dark, rest.track_dark);state.wide = 1;state.accent = 1;const hot = look(g, state, 8, palette);@@ -439,3 +456,40 @@ test "the thumb is centred in its gutter, widens and tints with feedback" {const narrow = Geometry.compute(.vertical, .{ .x = 0, .y = 0, .w = 8, .h = 100 }, 100, 400, 0).?;try std.testing.expectEqual(@as(f32, 6), look(narrow, state, 8, palette).rect.w);}++test "the groove shades from the theme's relief and a flat thumb carries no stripe" {+ const Renderer = @import("../paint.zig").Renderer;+ var palette: theme.Theme = .{};+ palette.scrollbar_thumb = .{ 0.4, 0.4, 0.4, 1 };+ // A red accent and glow must not reach either part of the bar.+ palette.accent = .{ 1, 0, 0, 1 };+ palette.border_glow = .{ 1, 0, 0, 1 };+ palette.scrollbar_glow = 0;+ palette.scrollbar_track_relief = .inset;+ palette.relief_depth = 2;+ palette.relief_softness = 3;+ palette.relief_dark = .{ 0, 0, 0, 0.6 };+ palette.relief_light = .{ 1, 1, 1, 0.6 };+ const g = Geometry.compute(.vertical, .{ .x = 0, .y = 0, .w = gutter(8), .h = 100 }, 100, 400, 0).?;+ var pixels = @as([14 * 100]u32, @splat(0));+ var r = Renderer.init(&pixels, 14, 100, 1);+ r.palette = palette;+ // Fully lit state: with a glow of 0 the thumb still paints as one colour.+ paint(&r, look(g, .{ .shine = 1 }, 8, palette));+ const row = 12 * 14;+ for (5..9) |x| {+ try std.testing.expectEqual(pixels[row + 5], pixels[row + x]);+ try std.testing.expectEqual((pixels[row + x] >> 16) & 0xff, pixels[row + x] & 0xff);+ }+ // Below the thumb the groove is dark where it faces the light and lit on+ // the far side, and neutral throughout.+ const groove = 60 * 14;+ try std.testing.expect((pixels[groove + 2] & 0xff) < (pixels[groove + 11] & 0xff));+ for ([_]usize{ 2, 7, 11 }) |x| try std.testing.expectEqual((pixels[groove + x] >> 16) & 0xff, pixels[groove + x] & 0xff);+ // Flat, as every theme but a neumorphic one leaves it: no channel at all.+ palette.scrollbar_track_relief = .flat;+ @memset(&pixels, 0);+ paint(&r, look(g, .{}, 8, palette));+ try std.testing.expectEqual(@as(u32, 0), pixels[groove + 7]);+ try std.testing.expect(pixels[row + 7] != 0);+}