Skip to content

RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process

ui: add raised relief to scrollbar thumbs

Petar Margetić396ccc67c584
diff --git a/src/ui/cairo.zig b/src/ui/cairo.zig
index 64c74c1..c611bdc 100644
--- a/src/ui/cairo.zig
+++ b/src/ui/cairo.zig
@@ -160,12 +160,12 @@ pub fn drawScrollbar(cr_any: *anyopaque, look: scrollbar.Look) void {
const track = look.track;
const thumb = look.rect;
- // 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;
+ // An inset track or raised thumb relief needs the channel around the thumb.
+ const needs_track_layer = look.track_relief != .flat or look.thumb_relief == .raised;
+ const left = if (needs_track_layer) @min(track.x, thumb.x) else thumb.x;
+ const top = if (needs_track_layer) @min(track.y, thumb.y) else thumb.y;
+ const right = if (needs_track_layer) @max(track.x + track.w, thumb.x + thumb.w) else thumb.x + thumb.w;
+ const bottom = if (needs_track_layer) @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
@@ -353,12 +353,37 @@ test "a scrollbar thumb paints its look through cairo" {
// 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));
+ var needs_track_layer = palette;
+ needs_track_layer.scrollbar_track_relief = .inset;
+ drawScrollbar(cr, scrollbar.look(g, .{}, 8, needs_track_layer));
c.cairo_surface_flush(surface);
try std.testing.expect(data[45 * 40 + 27] != 0);
+ // A raised thumb gets a layer large enough for its shadows, even with
+ // no groove. The relief remains confined to the channel.
+ c.cairo_set_operator(cr, c.CAIRO_OPERATOR_CLEAR);
+ c.cairo_paint(cr);
+ c.cairo_set_operator(cr, c.CAIRO_OPERATOR_OVER);
+ var raised = palette;
+ raised.scrollbar_glow = 0;
+ raised.scrollbar_thumb_relief = .raised;
+ raised.relief_depth = 2;
+ raised.relief_softness = 3;
+ raised.relief_dark = .{ 0, 0, 0, 0.6 };
+ raised.relief_light = .{ 1, 1, 1, 0.6 };
+
+ // The Cairo layer obtains its renderer palette from theme.global.
+ const saved_theme = theme.global;
+ defer theme.global = saved_theme;
+ theme.global = raised;
+
+ drawScrollbar(cr, scrollbar.look(g, .{}, 8, raised));
+ c.cairo_surface_flush(surface);
+ // The channel starts at x=21; the thumb starts at x=24.
+ try std.testing.expect(data[12 * 40 + 23] != 0);
+ try std.testing.expectEqual(@as(u32, 0), data[12 * 40 + 20]);
+ try std.testing.expectEqual(@as(u32, 0), data[12 * 40 + 33]);
+
// Engaged thumbs share the shell raster, including fractional placement,
// Cairo translation and clipping. The highlight remains inside the thumb.
c.cairo_set_operator(cr, c.CAIRO_OPERATOR_CLEAR);
diff --git a/src/ui/theme.zig b/src/ui/theme.zig
index 88b18cc..23919dc 100644
--- a/src/ui/theme.zig
+++ b/src/ui/theme.zig
@@ -257,6 +257,8 @@ pub const Theme = struct {
/// 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,
+ /// Relief on the scrollbar thumb; raised shadows stay inside its channel.
+ scrollbar_thumb_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.zig
index d8561a7..a8f71f5 100644
--- a/src/ui/widgets/scrollbar.zig
+++ b/src/ui/widgets/scrollbar.zig
@@ -149,6 +149,7 @@ pub const Look = struct {
track_radius: f32,
track_color: [4]f32,
track_relief: theme.Relief = .flat,
+ thumb_relief: theme.Relief = .flat,
rect: Rect,
radius: f32,
color: [4]f32,
@@ -193,7 +194,7 @@ pub fn look(g: Geometry, state: Appearance, configured: f32, palette: theme.Them
}
// 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) };
+ return .{ .track = track_rect, .track_radius = track_radius, .track_color = track_color, .track_relief = palette.scrollbar_track_relief, .thumb_relief = palette.scrollbar_thumb_relief, .rect = rect, .radius = cornerRadius(thickness), .color = color, .axis = g.axis, .highlight = highlight, .shine = state.shine, .glow = @max(palette.scrollbar_glow, 0) };
}
/// Whether `l`'s thumb gets the lit-edge raster. A flat thumb (`scrollbar_glow`
@@ -224,6 +225,7 @@ pub fn paint(r: *@import("../paint.zig").Renderer, l: Look) void {
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 });
+ paintThumbRelief(r, l);
return;
}
const box = l.rect;
@@ -268,6 +270,25 @@ pub fn paintThumb(r: *@import("../paint.zig").Renderer, l: Look) void {
text.blendPixel(&r.pixels[@intCast(py * r.width + px)], .{ .r = color[0], .g = color[1], .b = color[2], .a = color[3] }, coverage);
}
}
+ paintThumbRelief(r, l);
+}
+
+fn paintThumbRelief(r: *@import("../paint.zig").Renderer, l: Look) void {
+ r.paintShapeRelief(l.thumb_relief, .{
+ .x = l.rect.x,
+ .y = l.rect.y,
+ .w = l.rect.w,
+ .h = l.rect.h,
+ .radius = l.radius,
+ }, .{
+ .within = .{
+ .x = l.track.x,
+ .y = l.track.y,
+ .w = l.track.w,
+ .h = l.track.h,
+ .radius = l.track_radius,
+ },
+ });
}
/// Presentation only: callers retain their scroll mechanics and fixed hit area.
@@ -445,6 +466,7 @@ test "the thumb is centred in its gutter, widens and tints with feedback" {
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.expectEqual(theme.Relief.flat, rest.thumb_relief);
try std.testing.expect(rest.track_color[3] > 0);
state.wide = 1;
state.accent = 1;
@@ -457,6 +479,30 @@ test "the thumb is centred in its gutter, widens and tints with feedback" {
try std.testing.expectEqual(@as(f32, 6), look(narrow, state, 8, palette).rect.w);
}
+test "raised thumb relief stays within its channel" {
+ const Renderer = @import("../paint.zig").Renderer;
+ var palette: theme.Theme = .{};
+ palette.scrollbar_thumb = .{ 0.4, 0.4, 0.4, 1 };
+ palette.scrollbar_glow = 0;
+ palette.scrollbar_thumb_relief = .raised;
+ 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;
+ paint(&r, look(g, .{}, 8, palette));
+
+ // The thumb begins at x=4. Its raised edge shades the channel beside it.
+ try std.testing.expect(pixels[12 * 14 + 3] != 0);
+ // The channel is inset one pixel from the gutter's outer edges.
+ try std.testing.expectEqual(@as(u32, 0), pixels[12 * 14]);
+ try std.testing.expectEqual(@as(u32, 0), pixels[12 * 14 + 13]);
+}
+
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 = .{};