Skip to content

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

feat: extend neumorphic relief to more controls

Petar Margetić0c098a7d518c
diff --git a/docs/wiki/Theme.md b/docs/wiki/Theme.md
index 2b51266..b1ce07e 100644
--- a/docs/wiki/Theme.md
+++ b/docs/wiki/Theme.md
@@ -422,7 +422,14 @@ When `relief_depth` is greater than zero, individual controls can opt into
that did not previously use explicit relief. The standard slider progress fill
overlays the start of the track, so an inset `slider_track_relief` can have its
left-side groove shadow covered by that fill. The shaded slider and toggle paths
-use their existing procedural lighting instead of these settings.
+(`slider_shaded`, `toggle_shaded`) keep their procedural lighting and add these
+shadows on top, dropping only what the relief would double: the toggle's hand-made
+track rim under a non-flat track relief, and the knob's cast shadow under a
+non-flat thumb relief. A groove's shadows skip its knob, and a knob's stay inside
+its track (toggle) or the slider's box, so neither crosses the thumb or outlives
+the control's damage bounds. A theme that leaves the keys `flat` renders exactly
+as before. Raised shadows reach `relief_depth + relief_softness` past a box: use
+`inset` for segments, rows and pills packed into a layout.
| Key | Built-in default | Description |
| --- | --- | --- |
@@ -433,10 +440,17 @@ use their existing procedural lighting instead of these settings.
| `select_relief` | `"flat"` | Resting select/dropdown control relief |
| `select_popup_relief` | `"flat"` | Select/dropdown popup container relief |
| `checkbox_relief` | `"flat"` | Checkbox frame relief |
-| `toggle_track_relief` | `"flat"` | Standard toggle track relief |
-| `toggle_thumb_relief` | `"flat"` | Standard toggle thumb relief |
-| `slider_track_relief` | `"flat"` | Standard slider track relief |
-| `slider_thumb_relief` | `"flat"` | Standard slider thumb relief |
+| `toggle_track_relief` | `"flat"` | Toggle track relief (plain and shaded) |
+| `toggle_thumb_relief` | `"flat"` | Toggle knob relief (plain and shaded) |
+| `slider_track_relief` | `"flat"` | Slider track relief (plain and shaded) |
+| `slider_thumb_relief` | `"flat"` | Slider thumb relief (plain and shaded) |
+| `segmented_relief` | `"flat"` | Segmented control frame relief |
+| `segmented_selected_relief` | `"flat"` | Selected segment relief |
+| `stepper_relief` | `"flat"` | Stepper frame relief |
+| `swatch_relief` | `"flat"` | Colour swatch relief |
+| `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 |
| Additional constraint | Range |
| --- | --- |
| `chrome_height` | 28–84 |
diff --git a/src/ui/AGENTS.md b/src/ui/AGENTS.md
index 95ef0bb..1c8328a 100644
--- a/src/ui/AGENTS.md
+++ b/src/ui/AGENTS.md
@@ -67,3 +67,11 @@ text and colours, secrets) are in the root `AGENTS.md` ("Shared UI").
`paint` draws it into any `Renderer`, so the two cannot drift. The caller
owns the data (hide it when there is no battery) and the connection
shimmer's clock (`Spec.shimmer`).
+* **Relief on hand-shaded controls.** A control drawn through `fillRect` opts in
+ with `RectStyle.relief` and a theme key. One that lights its own pixels (the
+ shaded toggle and slider) paints first, then calls `Renderer.paintShapeRelief`
+ with `ReliefLimits`: a groove passes `.outside` (the knob), a knob passes
+ `.within` (its track or the control's box), so shadows never cross the thumb
+ or leave the damage bounds. `Renderer.reliefActive` tells it whether to drop
+ the hand-made rim or cast shadow the relief replaces; flat keys must leave
+ its pixels byte-identical.
diff --git a/src/ui/paint.zig b/src/ui/paint.zig
index 5c09161..0b75c51 100644
--- a/src/ui/paint.zig
+++ b/src/ui/paint.zig
@@ -14,6 +14,7 @@ const Widget = layout.Widget;
const RectStyle = layout.RectStyle;
const TextStyle = layout.TextStyle;
const IconStyle = layout.IconStyle;
+const Relief = layout.Relief;
const paint_icons = @import("paint_icons.zig");
const paint_image = @import("paint_image.zig");
const paint_widgets = @import("paint_widgets.zig");
@@ -66,6 +67,18 @@ const taps_strip = 64;
/// A logical-pixel clip rectangle, intersected as scroll containers nest.
pub const ClipRect = struct { x: f32, y: f32, w: f32, h: f32 };
+/// A rounded box in logical pixels: the shape, or a limit on where its relief
+/// may land, for `Renderer.paintShapeRelief`.
+pub const ReliefBox = struct { x: f32, y: f32, w: f32, h: f32, radius: f32 = 0 };
+
+/// Where such relief may land: only `within` one box, never `outside` another.
+/// A knob's shadows stay within its track; a groove's skip the knob.
+pub const ReliefLimits = struct { within: ?ReliefBox = null, outside: ?ReliefBox = null };
+
+fn deviceBox(b: ReliefBox, s: f32) relief.Shape {
+ return .{ .x = b.x * s, .y = b.y * s, .w = b.w * s, .h = b.h * s, .radius = b.radius * s };
+}
+
/// Premultiplied 0xAARRGGBB pixels, row-major without padding.
pub const Image = struct { pixels: []const u32, width: i32, height: i32 };
@@ -187,16 +200,17 @@ pub const Renderer = struct {
if (style.relief == .flat or w <= 0 or h <= 0) return r.fillRectPlain(x, y, w, h, style);
const spec = relief.Spec.from(r.palette orelse theme.global, r.scale);
if (!spec.enabled()) return r.fillRectPlain(x, y, w, h, style);
- if (style.relief == .raised) r.paintRelief(x, y, w, h, style, spec);
+ if (style.relief == .raised) r.paintRelief(x, y, w, h, style, spec, null, null);
r.fillRectPlain(x, y, w, h, style);
- if (style.relief == .inset) r.paintRelief(x, y, w, h, style, spec);
+ if (style.relief == .inset) r.paintRelief(x, y, w, h, style, spec, null, null);
}
/// The shadow pair of `style.relief` over the pixels it can touch: the
/// fill's box grown by the shadows' reach when raised, the box itself when
/// inset. Honors the clip, so a scroll container cuts a raised control's
- /// shadows at its edge.
- fn paintRelief(r: *Renderer, x: f32, y: f32, w: f32, h: f32, style: RectStyle, spec: relief.Spec) void {
+ /// shadows at its edge. `within` and `outside` (device pixels) confine the
+ /// shadows to one shape and keep them off another, antialiased at the edge.
+ fn paintRelief(r: *Renderer, x: f32, y: f32, w: f32, h: f32, style: RectStyle, spec: relief.Spec, within: ?relief.Shape, outside: ?relief.Shape) void {
const s = r.scale;
const raised = style.relief == .raised;
const grow: f32 = if (raised) spec.reach() else 0;
@@ -227,14 +241,41 @@ pub const Renderer = struct {
var px = x0;
while (px < x1) : (px += 1) {
const sx = @as(f32, @floatFromInt(px)) + 0.5;
+ var keep: f32 = 1;
+ if (within) |m| keep = sdf.coverage(sdf.sdRoundedBox(sx, sy, m.x, m.y, m.w, m.h, m.radius));
+ if (outside) |m| keep *= 1 - sdf.coverage(sdf.sdRoundedBox(sx, sy, m.x, m.y, m.w, m.h, m.radius));
+ if (keep <= 0) continue;
const pair = if (raised) relief.raised(spec, shape, sx, sy) else relief.inset(spec, shape, sx, sy);
const index = row + @as(usize, @intCast(px));
- if (pair.dark > 0) blendPixel(&r.pixels[index], dark, pair.dark);
- if (pair.light > 0) blendPixel(&r.pixels[index], light, pair.light);
+ if (pair.dark > 0) blendPixel(&r.pixels[index], dark, pair.dark * keep);
+ if (pair.light > 0) blendPixel(&r.pixels[index], light, pair.light * keep);
}
}
}
+ /// Whether `kind` would shade anything here: the theme has relief on and
+ /// the control asked for it. A painter that lights its own pixels (the
+ /// shaded toggle and slider) uses it to drop the hand-made lighting the
+ /// relief would double.
+ pub fn reliefActive(r: *const Renderer, kind: Relief) bool {
+ if (kind == .flat) return false;
+ return relief.Spec.from(r.palette orelse theme.global, r.scale).enabled();
+ }
+
+ /// The relief of `kind` for a shape some other painter fills itself. Call
+ /// it once the pixels are down: a raised shape's shadows lie outside it and
+ /// an inset one's inside, so neither touches the fill. Nothing is drawn
+ /// while the theme's `relief_depth` is 0.
+ pub fn paintShapeRelief(r: *Renderer, kind: Relief, shape: ReliefBox, limits: ReliefLimits) void {
+ if (kind == .flat or shape.w <= 0 or shape.h <= 0) return;
+ const spec = relief.Spec.from(r.palette orelse theme.global, r.scale);
+ if (!spec.enabled()) return;
+ const s = r.scale;
+ const within: ?relief.Shape = if (limits.within) |b| deviceBox(b, s) else null;
+ const outside: ?relief.Shape = if (limits.outside) |b| deviceBox(b, s) else null;
+ r.paintRelief(shape.x, shape.y, shape.w, shape.h, .{ .color = .{ 0, 0, 0, 0 }, .radius = shape.radius, .relief = kind }, spec, within, outside);
+ }
+
fn fillRectPlain(r: *Renderer, x: f32, y: f32, w: f32, h: f32, style: RectStyle) void {
if (w <= 0 or h <= 0) return;
const s = r.scale;
diff --git a/src/ui/paint_tests.zig b/src/ui/paint_tests.zig
index 780f72d..9ce679e 100644
--- a/src/ui/paint_tests.zig
+++ b/src/ui/paint_tests.zig
@@ -444,3 +444,186 @@ test "an inset fill shades the edge facing the light and lights the opposite one
try std.testing.expectEqual(@as(u32, 0xff808080), pixels[30 * 60 + 30]);
try std.testing.expectEqual(@as(u32, 0xff808080), pixels[30 * 60 + 15]);
}
+
+const paintSlider = @import("paint_widgets.zig").paintSlider;
+const paintSegmented = @import("paint_widgets.zig").paintSegmented;
+const paintSwatch = @import("paint_widgets.zig").paintSwatch;
+const paintRow = @import("paint_widgets.zig").paintRow;
+
+/// The blue channel at (`x`, `y`): shadows are black and highlights white, so
+/// it is enough to tell them from the mid-grey page.
+fn channel(pixels: []const u32, width: usize, x: usize, y: usize) u32 {
+ return pixels[y * width + x] & 0xff;
+}
+
+/// Inset: the edge facing the light is in shadow, the far one lit.
+fn expectInsetShading(flat: []const u32, cut: []const u32, width: usize, x: usize, top: usize, bottom: usize) !void {
+ try std.testing.expect(channel(cut, width, x, top) < channel(flat, width, x, top));
+ try std.testing.expect(channel(cut, width, x, bottom) > channel(flat, width, x, bottom));
+}
+
+/// A shaded toggle on a mid-grey page, painted with `palette`.
+fn shadedToggle(palette: theme.Theme, on: bool) [80 * 60]u32 {
+ var pixels = @as([80 * 60]u32, @splat(0xff808080));
+ var r = Renderer.init(&pixels, 80, 60, 1);
+ r.palette = palette;
+ @import("widgets/toggle.zig").paint(&r, .{ .x = 20, .y = 20, .w = 36, .h = 20 }, .{ .on = on });
+ return pixels;
+}
+
+test "shaded toggles take the relief keys and are untouched while they are flat" {
+ var flat = reliefTheme();
+ flat.toggle_shaded = true;
+ var stock: theme.Theme = .{};
+ stock.toggle_shaded = true;
+ // Relief on in the theme but off for the control: pixel for pixel as before.
+ try std.testing.expectEqualSlices(u32, &shadedToggle(stock, false), &shadedToggle(flat, false));
+
+ // Off, the knob rests at the left: 22.8 to 37.2 across and down the track.
+ const base = shadedToggle(flat, false);
+ var raised = flat;
+ raised.toggle_thumb_relief = .raised;
+ const lit = shadedToggle(raised, false);
+ // Light from the upper left: a shadow lower right of the knob, a glow upper left.
+ try std.testing.expect(channel(&lit, 80, 39, 33) < channel(&base, 80, 39, 33));
+ try std.testing.expect(channel(&lit, 80, 21, 30) > channel(&base, 80, 21, 30));
+ // The knob's shadows never leave the track.
+ try std.testing.expectEqual(@as(u32, 0xff808080), lit[42 * 80 + 30]);
+
+ var groove = flat;
+ groove.toggle_track_relief = .inset;
+ const cut = shadedToggle(groove, false);
+ try expectInsetShading(&base, &cut, 80, 40, 21, 38);
+ // The groove's shadows skip the knob.
+ try std.testing.expectEqual(base[30 * 80 + 30], cut[30 * 80 + 30]);
+}
+
+/// A shaded slider at half on a mid-grey page: the disc is 16 across, centred
+/// on (60, 22), in a box from (10, 10) that is 100 by 24.
+fn shadedSlider(palette: theme.Theme) [130 * 50]u32 {
+ var pixels = @as([130 * 50]u32, @splat(0xff808080));
+ var r = Renderer.init(&pixels, 130, 50, 1);
+ r.palette = palette;
+ const widget = Widget{
+ .kind = .{ .slider = .{
+ .style = .standard,
+ .value = 0.5,
+ .min = 0,
+ .max = 1,
+ .feel = .{},
+ .on_change = struct {
+ fn call(_: ?*anyopaque, _: usize, _: f32) void {}
+ }.call,
+ } },
+ .computed_x = 10,
+ .computed_y = 10,
+ .computed_width = 100,
+ .computed_height = 24,
+ };
+ paintSlider(&widget, widget.kind.slider, &r);
+ return pixels;
+}
+
+fn shadedSliderTheme(track_height: f32) theme.Theme {
+ var t = reliefTheme();
+ t.slider_shaded = true;
+ t.slider_thumb_size = 16;
+ t.slider_track_height = track_height;
+ return t;
+}
+
+test "shaded sliders take the relief keys, off the thumb and inside their box" {
+ var flat = shadedSliderTheme(6);
+ var stock: theme.Theme = .{};
+ stock.slider_shaded = true;
+ stock.slider_thumb_size = 16;
+ stock.slider_track_height = 6;
+ try std.testing.expectEqualSlices(u32, &shadedSlider(stock), &shadedSlider(flat));
+
+ const base = shadedSlider(flat);
+ flat.slider_thumb_relief = .raised;
+ const lit = shadedSlider(flat);
+ try std.testing.expect(channel(&lit, 130, 68, 26) < channel(&base, 130, 68, 26));
+ try std.testing.expect(channel(&lit, 130, 51, 17) > channel(&base, 130, 51, 17));
+ // Below the box (it ends at y = 34) the page is untouched.
+ try std.testing.expectEqual(@as(u32, 0xff808080), lit[36 * 130 + 60]);
+
+ // A rail tall enough for the groove's two shadows not to meet.
+ var groove = shadedSliderTheme(14);
+ const wide = shadedSlider(groove);
+ groove.slider_track_relief = .inset;
+ const cut = shadedSlider(groove);
+ try expectInsetShading(&wide, &cut, 130, 90, 16, 27);
+ // The groove's shadows skip the disc.
+ try std.testing.expectEqual(wide[22 * 130 + 60], cut[22 * 130 + 60]);
+}
+
+fn paintedSegmented(palette: theme.Theme) [80 * 60]u32 {
+ var pixels = @as([80 * 60]u32, @splat(0xff808080));
+ var r = Renderer.init(&pixels, 80, 60, 1);
+ r.palette = palette;
+ const widget = Widget{
+ .kind = .{ .segmented = .{ .labels = &.{}, .selected = 0, .on_change = struct {
+ fn call(_: ?*anyopaque, _: usize, _: usize) void {}
+ }.call } },
+ .computed_x = 20,
+ .computed_y = 20,
+ .computed_width = 40,
+ .computed_height = 24,
+ };
+ paintSegmented(&widget, widget.kind.segmented, &r);
+ return pixels;
+}
+
+fn paintedSwatch(palette: theme.Theme) [80 * 60]u32 {
+ var pixels = @as([80 * 60]u32, @splat(0xff808080));
+ var r = Renderer.init(&pixels, 80, 60, 1);
+ r.palette = palette;
+ const widget = Widget{
+ .kind = .{ .swatch = .{ .color = .{ 0.5, 0.5, 0.5, 1 }, .selected = false, .on_select = struct {
+ fn call(_: ?*anyopaque, _: usize, _: [4]f32) void {}
+ }.call } },
+ .computed_x = 20,
+ .computed_y = 20,
+ .computed_width = 24,
+ .computed_height = 24,
+ };
+ paintSwatch(&widget, widget.kind.swatch, &r);
+ return pixels;
+}
+
+fn paintedRow(palette: theme.Theme, data: layout.RowData) [80 * 60]u32 {
+ var pixels = @as([80 * 60]u32, @splat(0xff808080));
+ var r = Renderer.init(&pixels, 80, 60, 1);
+ r.palette = palette;
+ const widget = Widget{ .kind = .{ .row = data }, .computed_x = 10, .computed_y = 10, .computed_width = 40, .computed_height = 30 };
+ paintRow(&widget, data, &r);
+ return pixels;
+}
+
+test "segmented frames, swatches and rows take their own relief keys" {
+ const stock: theme.Theme = .{};
+ const flat = reliefTheme();
+ // Relief on in the theme, every key flat: the same pixels as a theme without it.
+ try std.testing.expectEqualSlices(u32, &paintedSegmented(stock), &paintedSegmented(flat));
+ try std.testing.expectEqualSlices(u32, &paintedSwatch(stock), &paintedSwatch(flat));
+ try std.testing.expectEqualSlices(u32, &paintedRow(stock, .{ .selected = true }), &paintedRow(flat, .{ .selected = true }));
+
+ var inset = flat;
+ inset.segmented_relief = .inset;
+ try expectInsetShading(&paintedSegmented(flat), &paintedSegmented(inset), 80, 40, 21, 42);
+
+ var raised = flat;
+ raised.swatch_relief = .raised;
+ const base = paintedSwatch(flat);
+ const lit = paintedSwatch(raised);
+ // The swatch is 20 to 44 across and down: shadow to its lower right, glow upper left.
+ try std.testing.expect(channel(&lit, 80, 45, 33) < channel(&base, 80, 45, 33));
+ try std.testing.expect(channel(&lit, 80, 18, 33) > channel(&base, 80, 18, 33));
+
+ var rows = flat;
+ rows.row_selected_relief = .inset;
+ try expectInsetShading(&paintedRow(flat, .{ .selected = true }), &paintedRow(rows, .{ .selected = true }), 80, 35, 11, 38);
+ rows.row_hover_relief = .inset;
+ try expectInsetShading(&paintedRow(flat, .{ .state = .hover }), &paintedRow(rows, .{ .state = .hover }), 80, 35, 11, 38);
+}
diff --git a/src/ui/paint_widgets.zig b/src/ui/paint_widgets.zig
index d18acfe..681d03a 100644
--- a/src/ui/paint_widgets.zig
+++ b/src/ui/paint_widgets.zig
@@ -45,6 +45,7 @@ pub fn paintRow(widget: *const Widget, data: RowData, renderer: *Renderer) void
.radius = t.radius,
.border_width = 1,
.border_color = t.start_menu_selected_border,
+ .relief = t.row_selected_relief,
});
const marker_h = @max(10, widget.computed_height - 18);
const marker_y = widget.computed_y + (widget.computed_height - marker_h) / 2;
@@ -58,6 +59,7 @@ pub fn paintRow(widget: *const Widget, data: RowData, renderer: *Renderer) void
.radius = t.radius,
.border_width = 1,
.border_color = t.border_soft,
+ .relief = t.row_hover_relief,
});
}
}
@@ -271,6 +273,7 @@ pub fn paintPill(widget: *const Widget, style: TextStyle, renderer: *Renderer) v
renderer.fillRect(x, y, w, h, .{
.color = wash,
.radius = h / 2,
+ .relief = t.pill_relief,
.border_width = 1,
.border_color = lerp4(t.border, .{ t.accent[0], t.accent[1], t.accent[2], t.pill_border_active_alpha * t.accent[3] }, glow),
});
@@ -296,6 +299,7 @@ pub fn paintSegmented(widget: *const Widget, data: SegmentedData, renderer: *Ren
.radius = t.radius,
.border_width = 1,
.border_color = t.border,
+ .relief = t.segmented_relief,
});
if (data.labels.len == 0) return;
const cell_w = widget.computed_width / @as(f32, @floatFromInt(data.labels.len));
@@ -307,6 +311,7 @@ pub fn paintSegmented(widget: *const Widget, data: SegmentedData, renderer: *Ren
renderer.fillRect(cell_x + 2, widget.computed_y + 2, cell_w - 4, widget.computed_height - 4, .{
.color = accent_wash,
.radius = @max(0, t.radius - 2),
+ .relief = t.segmented_selected_relief,
.border_width = 1,
.border_color = t.accent,
});
@@ -327,6 +332,7 @@ pub fn paintSwatch(widget: *const Widget, data: SwatchData, renderer: *Renderer)
renderer.fillRect(widget.computed_x, widget.computed_y, widget.computed_width, widget.computed_height, .{
.color = data.color,
.radius = t.swatch_radius,
+ .relief = t.swatch_relief,
.border_width = border_width,
.border_color = border_color,
});
@@ -353,6 +359,7 @@ pub fn paintStepper(widget: *const Widget, data: StepperData, renderer: *Rendere
.radius = t.radius,
.border_width = 1,
.border_color = t.border,
+ .relief = t.stepper_relief,
});
renderer.drawIcon(widget.computed_x, widget.computed_y, stepper_button_width, widget.computed_height, .{ .id = .minimize, .color = t.fg });
renderer.drawIcon(widget.computed_x + widget.computed_width - stepper_button_width, widget.computed_y, stepper_button_width, widget.computed_height, .{ .id = .plus, .color = t.fg });
diff --git a/src/ui/theme.zig b/src/ui/theme.zig
index 53cac7e..5578419 100644
--- a/src/ui/theme.zig
+++ b/src/ui/theme.zig
@@ -241,6 +241,18 @@ pub const Theme = struct {
toggle_thumb_relief: Relief = .flat,
slider_track_relief: Relief = .flat,
slider_thumb_relief: Relief = .flat,
+ // Relief for the controls that had none: the segmented control (frame and
+ // selected cell), stepper, swatch, a slider's value pill, and list rows
+ // (hovered, selected). A raised box casts shadows `relief_depth +
+ // relief_softness` past its edge, so cells and rows packed into a layout
+ // want `inset`.
+ segmented_relief: Relief = .flat,
+ segmented_selected_relief: Relief = .flat,
+ stepper_relief: Relief = .flat,
+ swatch_relief: Relief = .flat,
+ pill_relief: Relief = .flat,
+ row_hover_relief: Relief = .flat,
+ row_selected_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/theme_tests.zig b/src/ui/theme_tests.zig
index 575f0cb..02c3877 100644
--- a/src/ui/theme_tests.zig
+++ b/src/ui/theme_tests.zig
@@ -349,3 +349,41 @@ test "component relief theme serialization round-trips and omits defaults" {
try std.testing.expectEqual(original.toggle_track_relief, reloaded.toggle_track_relief);
try std.testing.expectEqual(original.field_relief, reloaded.field_relief);
}
+
+test "relief keys of the remaining widgets parse, default to flat and round-trip" {
+ const Relief = theme.Relief;
+ const keys = [_][]const u8{ "segmented_relief", "segmented_selected_relief", "stepper_relief", "swatch_relief", "pill_relief", "row_hover_relief", "row_selected_relief" };
+ for (keys) |key| try std.testing.expect(theme_file.knownKey(key));
+
+ const defaults: Theme = .{};
+ try std.testing.expectEqual(Relief.flat, defaults.segmented_relief);
+ try std.testing.expectEqual(Relief.flat, defaults.segmented_selected_relief);
+ try std.testing.expectEqual(Relief.flat, defaults.stepper_relief);
+ try std.testing.expectEqual(Relief.flat, defaults.swatch_relief);
+ try std.testing.expectEqual(Relief.flat, defaults.pill_relief);
+ try std.testing.expectEqual(Relief.flat, defaults.row_hover_relief);
+ try std.testing.expectEqual(Relief.flat, defaults.row_selected_relief);
+
+ const toml =
+ \\[theme]
+ \\segmented_relief = "inset"
+ \\segmented_selected_relief = "raised"
+ \\row_selected_relief = "inset"
+ ;
+ var t: Theme = .{};
+ try theme_file.overlay(std.testing.allocator, &t, toml);
+ try std.testing.expectEqual(Relief.inset, t.segmented_relief);
+ try std.testing.expectEqual(Relief.raised, t.segmented_selected_relief);
+ try std.testing.expectEqual(Relief.inset, t.row_selected_relief);
+ try std.testing.expectEqual(Relief.flat, t.stepper_relief);
+
+ var buf: std.ArrayList(u8) = .empty;
+ defer buf.deinit(std.testing.allocator);
+ try theme_file.writeDiff(std.testing.allocator, &buf, t, defaults, "", false);
+ var reloaded: Theme = .{};
+ try theme_file.overlay(std.testing.allocator, &reloaded, buf.items);
+ try std.testing.expectEqual(t.segmented_relief, reloaded.segmented_relief);
+ try std.testing.expectEqual(t.segmented_selected_relief, reloaded.segmented_selected_relief);
+ try std.testing.expectEqual(t.row_selected_relief, reloaded.row_selected_relief);
+ try std.testing.expectEqual(t.stepper_relief, reloaded.stepper_relief);
+}
diff --git a/src/ui/widgets/slider.zig b/src/ui/widgets/slider.zig
index d03b2db..8425783 100644
--- a/src/ui/widgets/slider.zig
+++ b/src/ui/widgets/slider.zig
@@ -7,6 +7,7 @@ const layout = @import("../layout.zig");
const scrollbar = @import("scrollbar.zig");
const Widget = layout.Widget;
const Renderer = @import("../paint.zig").Renderer;
+const ReliefBox = @import("../paint.zig").ReliefBox;
const theme = @import("../theme.zig");
const sdf = @import("../sdf.zig");
const text = @import("../text.zig");
@@ -47,6 +48,10 @@ pub fn paintShaded(widget: *const Widget, data: layout.SliderData, r: *Renderer)
const fill_radius = @min(cx, track_h) / 2;
// Include the shadow's 0.4 px offset, its 0.8 px falloff and device AA.
const disc_extent = radius + @max(0.4 + 0.8, 0.5 / s);
+ // With relief on for the thumb, its shadows replace the hand-made contact shadow.
+ const track_relief = t.slider_track_relief;
+ const thumb_relief = t.slider_thumb_relief;
+ const cast_weight: f32 = if (r.reliefActive(thumb_relief)) 0 else 1;
var py = y0;
while (py < y1) : (py += 1) {
const y = (@as(f32, @floatFromInt(py)) + 0.5) / s - widget.computed_y;
@@ -82,7 +87,7 @@ pub fn paintShaded(widget: *const Widget, data: layout.SliderData, r: *Renderer)
// The shadow and metallic reflections are local to the knob.
if (@abs(dx) > disc_extent or @abs(dy) > disc_extent) continue;
const shadow = @sqrt(dx * dx + (dy - 0.4) * (dy - 0.4)) - radius;
- blend(pixel, .{ 0, 0, 0, 0.12 }, std.math.clamp(1 - shadow / 0.8, 0, 1) * edge_fade);
+ blend(pixel, .{ 0, 0, 0, 0.12 * cast_weight }, std.math.clamp(1 - shadow / 0.8, 0, 1) * edge_fade);
const distance = @sqrt(dx * dx + dy * dy) - radius;
const coverage = sdf.coverage(distance * s);
if (coverage <= 0) continue;
@@ -98,6 +103,20 @@ pub fn paintShaded(widget: *const Widget, data: layout.SliderData, r: *Renderer)
blend(pixel, knob, coverage);
}
}
+ // Relief goes on last. The groove's shadows skip the disc; the disc's stay
+ // inside the slider's box, so partial repaints still erase them.
+ const rail_box: ReliefBox = .{ .x = widget.computed_x, .y = widget.computed_y + cy - track_h / 2, .w = w, .h = track_h, .radius = track_h / 2 };
+ const disc_box: ReliefBox = .{ .x = widget.computed_x + cx - radius, .y = widget.computed_y + cy - radius, .w = 2 * radius, .h = 2 * radius, .radius = radius };
+ r.paintShapeRelief(track_relief, rail_box, .{
+ .within = .{
+ .x = widget.computed_x,
+ .y = widget.computed_y,
+ .w = w,
+ .h = h,
+ },
+ .outside = disc_box,
+ });
+ r.paintShapeRelief(thumb_relief, disc_box, .{ .within = .{ .x = widget.computed_x, .y = widget.computed_y, .w = w, .h = h } });
}
fn falloff(value: f32) f32 {
diff --git a/src/ui/widgets/toggle.zig b/src/ui/widgets/toggle.zig
index 824e84d..0a61c87 100644
--- a/src/ui/widgets/toggle.zig
+++ b/src/ui/widgets/toggle.zig
@@ -6,6 +6,7 @@ const layout = @import("../layout.zig");
const Widget = layout.Widget;
const Renderer = @import("../paint.zig").Renderer;
+const ReliefBox = @import("../paint.zig").ReliefBox;
const anim = @import("../anim.zig");
const theme = @import("../theme.zig");
const Rect = @import("field.zig").Rect;
@@ -80,6 +81,12 @@ fn paintShaded(r: *Renderer, box: Rect, opts: Options, t: theme.Theme) void {
const track = trackColor(t, opts);
const thumb = t.toggle_thumb orelse t.control_thumb;
const opacity: f32 = if (opts.disabled) 0.5 else 1;
+ // Neumorphic relief, when the theme asks for it, takes over the hand-made
+ // lighting it would double: the track's rim and the knob's cast shadow.
+ const track_relief: layout.Relief = if (opts.disabled) .flat else t.toggle_track_relief;
+ const thumb_relief: layout.Relief = if (opts.disabled) .flat else t.toggle_thumb_relief;
+ const rim_weight: f32 = if (r.reliefActive(track_relief)) 0 else 1;
+ const cast_weight: f32 = if (r.reliefActive(thumb_relief)) 0 else 1;
var py = y0;
while (py < y1) : (py += 1) {
const y = (@as(f32, @floatFromInt(py)) + 0.5) / scale - box.y;
@@ -92,11 +99,11 @@ fn paintShaded(r: *Renderer, box: Rect, opts: Options, t: theme.Theme) void {
if (coverage <= 0) continue;
// Dark upper lip fades into the track; the lower lip catches light.
const rim = 1 - std.math.clamp(-distance / 1.4, 0, 1);
- var color = shade(track, -0.07 + 0.10 * vertical + rim * (-0.22 + 0.32 * vertical));
+ var color = shade(track, -0.07 + 0.10 * vertical + rim * rim_weight * (-0.22 + 0.32 * vertical));
// The knob casts a short, soft shadow down into the recess.
const shadow_distance = sdf.sdRoundedBox(x, y, thumb_x, inset + 0.8, side, side, thumb_radius);
const shadow = std.math.clamp(1 - shadow_distance / 1.6, 0, 1);
- color = shade(color, -0.24 * shadow * shadow);
+ color = shade(color, -0.24 * cast_weight * shadow * shadow);
if (opts.focused and !opts.disabled) {
const focus = sdf.coverage((distance + 2) * scale);
color = mix(color, t.controlFocusColor(), 1 - focus);
@@ -118,6 +125,12 @@ fn paintShaded(r: *Renderer, box: Rect, opts: Options, t: theme.Theme) void {
}, coverage);
}
}
+ // Relief goes on last. The groove's shadows skip the knob; the knob's stay
+ // inside the track, so they cannot outlive the control's own damage.
+ const track_box: ReliefBox = .{ .x = box.x, .y = box.y, .w = box.w, .h = box.h, .radius = radius };
+ const knob_box: ReliefBox = .{ .x = box.x + thumb_x, .y = box.y + inset, .w = side, .h = side, .radius = thumb_radius };
+ r.paintShapeRelief(track_relief, track_box, .{ .outside = knob_box });
+ r.paintShapeRelief(thumb_relief, knob_box, .{ .within = track_box });
}
fn restPosition(opts: Options) f32 {