RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process
Make app-client proximity glow repaint only what it changes
oxydizer78130c996366
diff --git a/src/editor/app.zig b/src/editor/app.zigindex 69b898b..e3a5272 100644--- a/src/editor/app.zig+++ b/src/editor/app.zig@@ -87,6 +87,9 @@ pub const App = struct {caret_painted: caret_mod.Rect = .{},caret_snap: bool = true,caret_dirty: bool = false,+ /// Only proximity glow changed: a full repaint, but paced by the frame+ /// callback instead of following every pointer motion.+ glow_dirty: bool = false,caret_activity: usize = 0,menu_open: bool = false,menu_selected: ?usize = null,diff --git a/src/editor/app_input.zig b/src/editor/app_input.zigindex 30eed9a..1e0fc12 100644--- a/src/editor/app_input.zig+++ b/src/editor/app_input.zig@@ -25,7 +25,7 @@ pub fn handleMotion(self: *App, x: f64, y: f64) void {const old_y = self.py;self.px = x;self.py = y;- if (self.proximityChanged(old_x, old_y, x, y)) self.dirty = true;+ if (self.proximityChanged(old_x, old_y, x, y)) self.glow_dirty = true;if (self.menu_open) {const hovered = self.menuHit(x, y);if (hovered != self.menu_selected) {diff --git a/src/editor/client_render.zig b/src/editor/client_render.zigindex d4bc026..69c1246 100644--- a/src/editor/client_render.zig+++ b/src/editor/client_render.zig@@ -3,6 +3,7 @@const std = @import("std");const c = @import("../files/c.zig").api;const shell_ui = @import("ui").cairo;+const wl = @import("wayland").client.wl;const ui_theme = @import("ui").theme;const button_widget = @import("ui").widgets.button;const IconId = @import("ui").layout.IconId;@@ -29,7 +30,9 @@ pub fn acquireBuffer(self: *Client, w: i32, h: i32) !?*Buffer {pub fn render(self: *Client) !void {self.updateCursor();- if (!self.configured or (!self.app.dirty and !self.app.caret_dirty) or self.outstanding >= 2) return;+ if (!self.configured or (!self.app.dirty and !self.app.caret_dirty and !self.app.glow_dirty) or self.outstanding >= 2) return;+ if (!self.app.dirty and !self.app.caret_dirty and self.frame_callback != null) return;+ const full = self.app.dirty or self.app.glow_dirty;const tbh = self.titlebarHeight();const w = self.app.w;@@ -77,17 +80,29 @@ pub fn render(self: *Client) !void {self.xdg_surface.setWindowGeometry(0, 0, w, h);buffer.busy = true;self.outstanding += 1;+ if (self.frame_callback == null) {+ if (self.surface.frame()) |callback| {+ callback.setListener(*Client, frameDone, self);+ self.frame_callback = callback;+ } else |_| {}+ }self.surface.attach(buffer.wl_buffer, 0, 0);- if (self.app.dirty) self.surface.damageBuffer(0, 0, w * self.scale, h * self.scale) else {+ if (full) self.surface.damageBuffer(0, 0, w * self.scale, h * self.scale) else {const rect = self.app.caretDamage();self.surface.damageBuffer(rect.x * self.scale, (rect.y + tbh) * self.scale, rect.width * self.scale, rect.height * self.scale);}self.surface.commit();self.app.dirty = false;self.app.caret_dirty = false;+ self.app.glow_dirty = false;self.app.caret_painted = self.app.caret.box;}+fn frameDone(callback: *wl.Callback, _: wl.Callback.Event, self: *Client) void {+ callback.destroy();+ self.frame_callback = null;+}+pub fn renderTitlebar(self: *Client, cr: *c.cairo_t, w: i32, h: i32) void {const w_f: f64 = @floatFromInt(w);const h_f: f64 = @floatFromInt(h);diff --git a/src/editor/main.zig b/src/editor/main.zigindex 10aa604..a6cec6f 100644--- a/src/editor/main.zig+++ b/src/editor/main.zig@@ -67,6 +67,9 @@ pub const Client = struct {outstanding: usize = 0,buffers: [2]?*Buffer = @splat(null),applied_scale: i32 = 0,+ /// Pending `wl_surface.frame` callback of the last commit. Glow-only+ /// frames wait for it, so they follow the display, not the pointer rate.+ frame_callback: ?*wl.Callback = null,xkb_context: ?*c.xkb_context = null,xkb_state: ?*c.xkb_state = null,key_repeat: key_repeat.KeyRepeat = .{},diff --git a/src/images/app.zig b/src/images/app.zigindex a9843b0..8c7eb8f 100644--- a/src/images/app.zig+++ b/src/images/app.zig@@ -51,6 +51,10 @@ pub const App = struct {/// Only the toolbar strip changed (its buttons' proximity glow): the/// client may repaint and damage just that strip instead of the window.toolbar_dirty: bool = false,+ /// What pointer motion changed outside the toolbar strip (proximity glow+ /// of content items, the hover hint), in app coordinates: like+ /// `toolbar_dirty`, a frame with only this repaints just these rects.+ pointer_damage: @import("ui").repaint.Region = .{},title_changed: bool = true,title: [1024:0]u8 = @splat(0),closed: bool = false,@@ -227,11 +231,24 @@ pub fn checker(cr: *c.cairo_t, r: Rect) void {c.cairo_paint(cr);setSource(cr, ui_theme.global.image_checker_light);const cell: f64 = ui_theme.global.image_checker_size;- var y: f64 = r.y;- var row: usize = 0;- while (y < r.y + r.h) : (y += cell) {- var x = r.x + @as(f64, @floatFromInt(row % 2)) * cell;- while (x < r.x + r.w) : (x += cell * 2) c.cairo_rectangle(cr, x, y, cell, cell);+ if (cell <= 0) return;+ // Only the cells inside the clip: the grid stays anchored at `r`, and a+ // partial repaint builds a few rectangles instead of thousands.+ var x0: f64 = 0;+ var y0: f64 = 0;+ var x1: f64 = 0;+ var y1: f64 = 0;+ c.cairo_clip_extents(cr, &x0, &y0, &x1, &y1);+ const right = @min(r.x + r.w, x1);+ const bottom = @min(r.y + r.h, y1);+ const first_col: usize = @intFromFloat(@max(0, @floor((x0 - r.x) / cell)));+ var row: usize = @intFromFloat(@max(0, @floor((y0 - r.y) / cell)));+ var y: f64 = r.y + @as(f64, @floatFromInt(row)) * cell;+ while (y < bottom) : (y += cell) {+ // Light cells sit where column and row parity match.+ const col = first_col + (first_col + row) % 2;+ var x = r.x + @as(f64, @floatFromInt(col)) * cell;+ while (x < right) : (x += cell * 2) c.cairo_rectangle(cr, x, y, cell, cell);row += 1;}c.cairo_fill(cr);diff --git a/src/images/app_dialogs.zig b/src/images/app_dialogs.zigindex d58bef8..31b4db7 100644--- a/src/images/app_dialogs.zig+++ b/src/images/app_dialogs.zig@@ -240,7 +240,8 @@ pub fn drawDialog(self: *App, cr: *c.cairo_t) void {const label = if (i == 0) tr("Cancel") else if (self.dialog == .save or i == 2) tr("Save as…") else if (self.dialog == .trash) tr("Move to Trash") else tr("Discard");const proximity = ui_button.sampleProximity(.{ .x = @floatCast(b.x), .y = @floatCast(b.y), .w = @floatCast(b.w), .h = @floatCast(b.h) },- @floatCast(self.px), @floatCast(self.py),+ @floatCast(self.px),+ @floatCast(self.py),.{ .x = @floatCast(r.x), .y = @floatCast(r.y), .w = @floatCast(r.w), .h = @floatCast(r.h) },);ui_button.paint(lr, .{ .x = @floatCast(b.x - r.x), .y = @floatCast(b.y - r.y), .w = @floatCast(b.w), .h = @floatCast(b.h) }, .{diff --git a/src/images/app_input.zig b/src/images/app_input.zigindex 8038cd7..e18488f 100644--- a/src/images/app_input.zig+++ b/src/images/app_input.zig@@ -81,81 +81,55 @@ fn toolbarProximityChanged(return false;}-fn contentRectProximityChanged(+/// Adds `rect` to the glow damage when the pointer moving from old to new+/// can change its glow (either position within reach).+fn damageRectProximity(+ self: *App,rect: Rect,old_x: f64,old_y: f64,new_x: f64,new_y: f64,-) bool {+) void {const box = button_widget.Rect{.x = @floatCast(rect.x),.y = @floatCast(rect.y),.w = @floatCast(rect.w),.h = @floatCast(rect.h),};- const before = button_widget.sampleProximity(- box,- @floatCast(old_x),- @floatCast(old_y),- box,- );- const after = button_widget.sampleProximity(- box,- @floatCast(new_x),- @floatCast(new_y),- box,- );- return @min(before.distance, after.distance) <= ui_theme.global.border_glow_reach;+ const before = button_widget.sampleProximity(box, @floatCast(old_x), @floatCast(old_y), box);+ const after = button_widget.sampleProximity(box, @floatCast(new_x), @floatCast(new_y), box);+ if (@min(before.distance, after.distance) <= ui_theme.global.border_glow_reach)+ self.pointer_damage.add(.around(rect.x, rect.y, rect.w, rect.h));}-fn contentProximityChanged(- self: *const App,+/// Records the content items (dialog controls, visible thumbnails, the strip+/// scrollbar) whose glow the motion changes, so the client repaints only those.+fn damageContentProximity(+ self: *App,old_x: f64,old_y: f64,new_x: f64,new_y: f64,-) bool {- if (old_x == new_x and old_y == new_y) return false;- if (!button_widget.proximityGlowVisible(ui_theme.global)) return false;+) void {+ if (old_x == new_x and old_y == new_y) return;+ if (!button_widget.proximityGlowVisible(ui_theme.global)) return;- if (self.dialog == .save) {- if (contentRectProximityChanged(- self.filenameRect(), old_x, old_y, new_x, new_y,- )) return true;- }+ if (self.dialog == .save) damageRectProximity(self, self.filenameRect(), old_x, old_y, new_x, new_y);if (self.dialog != .none and self.dialog != .trash) {const button_count: usize = if (self.dialog == .discard) 3 else 2;- for (0..button_count) |index| {- if (contentRectProximityChanged(- self.dialogButton(index), old_x, old_y, new_x, new_y,- )) return true;- }- return false;+ for (0..button_count) |index| damageRectProximity(self, self.dialogButton(index), old_x, old_y, new_x, new_y);+ return;}- if (self.dialog != .none or self.operation != null) return false;+ if (self.dialog != .none or self.operation != null) return;// Only thumbnails in the currently visible filmstrip participate.- for (0..self.thumbCount()) |slot| {- if (contentRectProximityChanged(- self.thumbRect(slot), old_x, old_y, new_x, new_y,- )) return true;- }+ for (0..self.thumbCount()) |slot| damageRectProximity(self, self.thumbRect(slot), old_x, old_y, new_x, new_y);if (self.stripBar()) |bar| {const track = bar.track;- if (contentRectProximityChanged(- .{- .x = track.x,- .y = track.y,- .w = track.w,- .h = track.h,- },- old_x, old_y, new_x, new_y,- )) return true;+ damageRectProximity(self, .{ .x = track.x, .y = track.y, .w = track.w, .h = track.h }, old_x, old_y, new_x, new_y);}-- return false;}pub fn hit(self: *const App, x: f64, y: f64) ?Action {@@ -305,10 +279,12 @@ pub fn handleMotion(self: *App, x: f64, y: f64) void {const hover = if (self.dialog == .none and self.operation == null) self.hit(x, y) else null;if (hover != self.hover) {self.hover = hover;- self.dirty = true;+ // Hover shows on the button and in the hint line under the toolbar.+ self.toolbar_dirty = true;+ self.pointer_damage.add(.around(0, self.toolbarBottom(), @floatFromInt(self.w), 26));}if (toolbarProximityChanged(self, old_x, old_y, x, y)) self.toolbar_dirty = true;- if (contentProximityChanged(self, old_x, old_y, x, y)) self.dirty = true;+ damageContentProximity(self, old_x, old_y, x, y);if (self.operation != null or (self.dialog != .none and !self.filename_drag)) {self.px = x;self.py = y;@@ -488,3 +464,41 @@ test "Images toolbar proximity drives redraws outside direct hover" {app.handleMotion(500, 500);try std.testing.expect(app.toolbar_dirty and !app.dirty);}++test "Images thumbnail glow damages only nearby cards, not the window" {+ const paths = [_][:0]const u8{ "/nonexistent/a.png", "/nonexistent/b.png", "/nonexistent/c.png" };+ var app: App = .{+ .paths = &paths,+ .selected = 0,+ .loader = undefined,+ .thumbnail_loader = undefined,+ };+ const saved_theme = ui_theme.global;+ defer ui_theme.global = saved_theme;+ ui_theme.global.button_hover_glow_enabled = true;+ ui_theme.global.border_glow_reach = 56;++ const first = app.thumbRect(0);+ const last = app.thumbRect(app.thumbCount() - 1);+ app.px = first.x - 200;+ app.py = 300;+ app.dirty = false;+ app.handleMotion(first.x - 10, first.y + first.h / 2);+ try std.testing.expect(!app.dirty);+ try std.testing.expect(!app.pointer_damage.isEmpty() and !app.pointer_damage.full);+ var covered = false;+ for (app.pointer_damage.slice()) |r| {+ const x0: f64 = @floatFromInt(r.x);+ const x1: f64 = @floatFromInt(r.x + r.w);+ covered = covered or (x0 <= first.x and x1 >= first.x + first.w);+ try std.testing.expect(x1 < last.x);+ }+ try std.testing.expect(covered);++ // Far from every card and the toolbar: nothing to repaint.+ app.pointer_damage.clear();+ app.px = 500;+ app.py = 300;+ app.handleMotion(501, 300);+ try std.testing.expect(app.pointer_damage.isEmpty() and !app.dirty);+}diff --git a/src/images/app_render.zig b/src/images/app_render.zigindex 6d8dae6..0d8a3f1 100644--- a/src/images/app_render.zig+++ b/src/images/app_render.zig@@ -48,7 +48,7 @@ pub fn render(self: *App, cr: *c.cairo_t) void {else.{ .variant = .ghost, .icon = glyph(act), .label = label };const proximity = self.buttonProximity(act, self.px, self.py);- var layer = shell_ui.Layer.begin(cr, shellRect(r)) orelse continue;+ var layer = shell_ui.Layer.beginVisible(cr, shellRect(r)) orelse continue;ui_button.paint(&layer.renderer, layer.local(), opts, .{.pointer = if (!self.enabled(act) or self.operation != null) .disabled else if (self.hover == act) .hover else .idle,.proximity_distance = proximity.distance,@@ -61,34 +61,39 @@ pub fn render(self: *App, cr: *c.cairo_t) void {setSource(cr, t.window_fg);clippedText(cr, hint, .{ .x = 16, .y = self.toolbarBottom() + 2, .w = @floatFromInt(self.w - 32), .h = 22 }, shell_ui.statusSize(), false);const box = self.canvas();- c.cairo_save(cr);- rounded(cr, box, 8);- c.cairo_clip(cr);- setSource(cr, t.app_bg);- c.cairo_paint(cr);- if (self.image) |im| {- const z = self.view.scale(box, @floatFromInt(im.w), @floatFromInt(im.h));- const dims = self.view.dimensions(@floatFromInt(im.w), @floatFromInt(im.h));- const cx = box.x + box.w / 2 + self.view.pan_x;- const cy = box.y + box.h / 2 + self.view.pan_y;+ // A partial repaint (glow, hover) often leaves the canvas out entirely.+ if (shell_ui.clipTouches(cr, shellRect(box))) {c.cairo_save(cr);- c.cairo_rectangle(cr, cx - dims[0] * z / 2, cy - dims[1] * z / 2, dims[0] * z, dims[1] * z);+ rounded(cr, box, 8);c.cairo_clip(cr);- checker(cr, box);+ setSource(cr, t.app_bg);+ c.cairo_paint(cr);+ if (self.image) |im| {+ const z = self.view.scale(box, @floatFromInt(im.w), @floatFromInt(im.h));+ const dims = self.view.dimensions(@floatFromInt(im.w), @floatFromInt(im.h));+ const cx = box.x + box.w / 2 + self.view.pan_x;+ const cy = box.y + box.h / 2 + self.view.pan_y;+ c.cairo_save(cr);+ c.cairo_rectangle(cr, cx - dims[0] * z / 2, cy - dims[1] * z / 2, dims[0] * z, dims[1] * z);+ c.cairo_clip(cr);+ checker(cr, box);+ c.cairo_restore(cr);+ c.cairo_translate(cr, cx, cy);+ c.cairo_rotate(cr, @as(f64, @floatFromInt(self.view.rotation)) * std.math.pi / 2.0);+ paintImage(cr, im, .{ .x = -@as(f64, @floatFromInt(im.w)) * z / 2, .y = -@as(f64, @floatFromInt(im.h)) * z / 2, .w = @as(f64, @floatFromInt(im.w)) * z, .h = @as(f64, @floatFromInt(im.h)) * z });+ } else {+ const msg = self.error_message orelse tr("Loading image…");+ setSource(cr, t.window_fg);+ drawText(cr, msg, box.x + @max(12, (box.w - measureText(cr, msg, shell_ui.textSize(), false)) / 2), box.y + box.h / 2, shell_ui.textSize(), false);+ }c.cairo_restore(cr);- c.cairo_translate(cr, cx, cy);- c.cairo_rotate(cr, @as(f64, @floatFromInt(self.view.rotation)) * std.math.pi / 2.0);- paintImage(cr, im, .{ .x = -@as(f64, @floatFromInt(im.w)) * z / 2, .y = -@as(f64, @floatFromInt(im.h)) * z / 2, .w = @as(f64, @floatFromInt(im.w)) * z, .h = @as(f64, @floatFromInt(im.h)) * z });- } else {- const msg = self.error_message orelse tr("Loading image…");- setSource(cr, t.window_fg);- drawText(cr, msg, box.x + @max(12, (box.w - measureText(cr, msg, shell_ui.textSize(), false)) / 2), box.y + box.h / 2, shell_ui.textSize(), false);}- c.cairo_restore(cr);if (self.crop_mode) self.drawCrop(cr);for (0..self.thumbCount()) |slot| {- const idx = self.indexAt(self.thumbStart() + slot);const r = self.thumbRect(slot);+ // Cards outside a partial repaint's clip, border stroke included.+ if (!shell_ui.clipTouches(cr, shellRect(.{ .x = r.x - 2, .y = r.y - 2, .w = r.w + 4, .h = r.h + 4 }))) continue;+ const idx = self.indexAt(self.thumbStart() + slot);c.cairo_save(cr);rounded(cr, r, 7);c.cairo_clip(cr);diff --git a/src/images/main.zig b/src/images/main.zigindex 82d6ae2..8bc6e65 100644--- a/src/images/main.zig+++ b/src/images/main.zig@@ -8,6 +8,7 @@ const c = @import("../files/c.zig").api;const app_mod = @import("app.zig");const key_repeat = @import("ui").key_repeat;const shell_ui = @import("ui").cairo;+const repaint = @import("ui").repaint;const ui_theme = @import("ui").theme;const a = std.heap.c_allocator;@@ -82,11 +83,6 @@ pub const Client = struct {outstanding: usize = 0,buffers: [2]?*Buffer = @splat(null),applied_scale: i32 = 0,- /// Frames painted so far, and the last one whose content outside the- /// toolbar strip changed: a buffer holding that frame or a later one- /// needs only the strip repainted for a `toolbar_dirty` frame.- frames: u64 = 0,- content_frame: u64 = 0,/// Pending `wl_surface.frame` callback of the last commit. Glow-only/// frames wait for it, so they follow the display, not the pointer rate.frame_callback: ?*wl.Callback = null,@@ -636,26 +632,35 @@ pub const Client = struct {}fn render(self: *Client) !void {self.updateCursor();- if (!self.configured or !(self.app.dirty or self.app.toolbar_dirty) or self.outstanding >= 2) return;- if (!self.app.dirty and self.frame_callback != null) return;+ const glow_only = !self.app.dirty and (self.app.toolbar_dirty or !self.app.pointer_damage.isEmpty());+ if (!self.configured or !(self.app.dirty or glow_only) or self.outstanding >= 2) return;+ if (glow_only and self.frame_callback != null) return;const tbh = self.titlebarHeight();const w = self.app.w;const h = self.app.h + tbh;const buffer = (try self.acquireBuffer(w * self.scale, h * self.scale)) orelse return;- // A proximity-glow frame repaints only the toolbar strip when this- // buffer already holds everything else (see `content_frame`).- const partial = !self.app.dirty and buffer.frame != 0 and- buffer.frame >= self.content_frame and self.applied_scale == self.scale;- const strip_h: i32 = @intFromFloat(@ceil(self.app.toolbarBottom()));+ // A proximity-glow frame repaints only the toolbar strip and the+ // glowing items, plus whatever changed since this buffer was painted.+ var frame: repaint.Region = .{};+ if (!glow_only or self.applied_scale != self.scale) frame = .whole else {+ if (self.app.toolbar_dirty) frame.add(.{ .x = 0, .y = tbh, .w = w, .h = @intFromFloat(@ceil(self.app.toolbarBottom())) });+ for (self.app.pointer_damage.slice()) |r| frame.add(.{ .x = r.x, .y = r.y + tbh, .w = r.w, .h = r.h });+ }+ var paint = buffer.stale;+ paint.merge(&frame);+ if (paint.covers(w, h, 0.5)) paint = .whole;const cr = c.cairo_create(buffer.image) orelse return error.CairoFailed;defer c.cairo_destroy(cr);c.cairo_scale(cr, @floatFromInt(self.scale), @floatFromInt(self.scale));- if (partial) {- c.cairo_rectangle(cr, 0, @floatFromInt(tbh), @floatFromInt(w), @floatFromInt(strip_h));+ if (!paint.full) {+ for (paint.slice()) |r| c.cairo_rectangle(cr, @floatFromInt(r.x), @floatFromInt(r.y), @floatFromInt(r.w), @floatFromInt(r.h));+ // Content only: the titlebar repaints on full frames.+ c.cairo_clip(cr);+ c.cairo_rectangle(cr, 0, @floatFromInt(tbh), @floatFromInt(w), @floatFromInt(h - tbh));c.cairo_clip(cr);} else if (tbh > 0) self.renderTitlebar(cr, w, tbh);@@ -673,9 +678,8 @@ pub const Client = struct {self.xdg_surface.setWindowGeometry(0, 0, w, h);buffer.busy = true;self.outstanding += 1;- self.frames += 1;- buffer.frame = self.frames;- if (self.app.dirty) self.content_frame = self.frames;+ buffer.stale = .{};+ for (self.buffers) |slot| if (slot) |other| if (other != buffer) other.stale.merge(&frame);if (self.frame_callback == null) {if (self.surface.frame()) |callback| {callback.setListener(*Client, frameDone, self);@@ -683,10 +687,11 @@ pub const Client = struct {} else |_| {}}self.surface.attach(buffer.wl_buffer, 0, 0);- if (partial) self.surface.damage(0, tbh, w, strip_h) else self.surface.damage(0, 0, w, h);+ if (frame.full) self.surface.damage(0, 0, w, h) else for (frame.slice()) |r| self.surface.damage(r.x, r.y, r.w, r.h);self.surface.commit();self.app.dirty = false;self.app.toolbar_dirty = false;+ self.app.pointer_damage.clear();}fn frameDone(callback: *wl.Callback, _: wl.Callback.Event, self: *Client) void {@@ -747,8 +752,9 @@ const Buffer = struct {w: i32,h: i32,busy: bool = false,- /// `Client.frames` when this buffer was last painted; 0 = never.- frame: u64 = 0,+ /// What changed on screen since this buffer was last painted, in window+ /// coordinates (see `ui.repaint`).+ stale: repaint.Region = .whole,fn create(client: *Client, w: i32, h: i32) !*Buffer {if (w <= 0 or h <= 0 or w > 16384 or h > 16384) return error.InvalidSize;diff --git a/src/pdf/app.zig b/src/pdf/app.zigindex 36b4642..a0a6c62 100644--- a/src/pdf/app.zig+++ b/src/pdf/app.zig@@ -173,6 +173,10 @@ pub const App = struct {/// Only the toolbar strip changed (its buttons' proximity glow): the/// client may repaint and damage just that strip instead of the window.toolbar_dirty: bool = false,+ /// What pointer motion changed outside the toolbar strip (proximity glow+ /// of content items, the hover hint), in app coordinates: like+ /// `toolbar_dirty`, a frame with only this repaints just these rects.+ pointer_damage: @import("ui").repaint.Region = .{},title_changed: bool = true,title: [1024:0]u8 = @splat(0),closed: bool = false,diff --git a/src/pdf/app_paint.zig b/src/pdf/app_paint.zigindex 13bb145..f722c4b 100644--- a/src/pdf/app_paint.zig+++ b/src/pdf/app_paint.zig@@ -22,7 +22,6 @@ const toolbar_buttons = @import("app.zig").toolbar_buttons;const toolbar_button_y = @import("app.zig").toolbar_button_y;const toolbar_button_h = @import("app.zig").toolbar_button_h;-fn drawContentGlow(cr: *c.api.cairo_t,rect: app_mod.Rect,@@ -344,7 +343,9 @@ pub fn renderThumbnailCards(self: *App, cr: *c.api.cairo_t) void {drawContentGlow(cr,.{ .x = card_x, .y = card_y, .w = card_w, .h = card.card_h },- self.px, self.py, sb,+ self.px,+ self.py,+ sb,);// Highlight border if active or hover@@ -395,7 +396,9 @@ pub fn renderOutlineList(self: *App, cr: *c.api.cairo_t) void {drawContentGlow(cr,.{ .x = sb.x + 4.0, .y = row_y, .w = sb.w - 8.0, .h = row_h },- self.px, self.py, sb,+ self.px,+ self.py,+ sb,);const indent = @as(f64, @floatFromInt(it.level)) * 12.0;@@ -455,7 +458,9 @@ pub fn renderToolbar(self: *App, cr: *c.api.cairo_t) void {drawContentGlow(cr,.{ .x = 126, .y = 8, .w = 48, .h = 28 },- self.px, self.py, null,+ self.px,+ self.py,+ null,);shell_ui.setSource(cr, if (self.focus == .page_number) ui_theme.shellPalette().fieldFocusColor() else ui_theme.global.field_border);@@ -586,7 +591,8 @@ pub fn renderDialog(self: *App, cr: *c.api.cairo_t) void {drawContentGlow(cr,.{ .x = dx + 24, .y = dy + 80, .w = dw - 48, .h = 44 },- self.px, self.py,+ self.px,+ self.py,.{ .x = dx, .y = dy, .w = dw, .h = dh },);}@@ -602,11 +608,12 @@ pub fn renderButtonText(self: *App, cr: *c.api.cairo_t, x: f64, y: f64, w: f64,elseui_button.sampleProximity(.{ .x = @floatCast(x), .y = @floatCast(y), .w = @floatCast(w), .h = @floatCast(h) },- @floatCast(self.px), @floatCast(self.py),+ @floatCast(self.px),+ @floatCast(self.py),.{ .x = 0, .y = 0, .w = @floatFromInt(self.w), .h = @floatFromInt(self.h) },);const is_hover = self.px >= x and self.px <= x + w and self.py >= y and self.py <= y + h;- var layer = shell_ui.Layer.begin(cr, .{ .x = @floatCast(x), .y = @floatCast(y), .w = @floatCast(w), .h = @floatCast(h) }) orelse return;+ var layer = shell_ui.Layer.beginVisible(cr, .{ .x = @floatCast(x), .y = @floatCast(y), .w = @floatCast(w), .h = @floatCast(h) }) orelse return;defer layer.finish();ui_button.paint(&layer.renderer, layer.local(), .{.variant = .secondary,@@ -623,7 +630,7 @@ pub fn renderButtonText(self: *App, cr: *c.api.cairo_t, x: f64, y: f64, w: f64,pub fn renderToggle(self: *App, cr: *c.api.cairo_t, x: f64, y: f64, w: f64, h: f64, label: []const u8, selected: bool, action: Action) void {const proximity = self.buttonProximity(action, self.px, self.py);const is_hover = self.px >= x and self.px <= x + w and self.py >= y and self.py <= y + h;- var layer = shell_ui.Layer.begin(cr, .{ .x = @floatCast(x), .y = @floatCast(y), .w = @floatCast(w), .h = @floatCast(h) }) orelse return;+ var layer = shell_ui.Layer.beginVisible(cr, .{ .x = @floatCast(x), .y = @floatCast(y), .w = @floatCast(w), .h = @floatCast(h) }) orelse return;defer layer.finish();ui_button.paint(&layer.renderer, layer.local(), .{.variant = .ghost,@@ -640,7 +647,7 @@ pub fn renderToggle(self: *App, cr: *c.api.cairo_t, x: f64, y: f64, w: f64, h: fpub fn renderButtonIcon(self: *App, cr: *c.api.cairo_t, x: f64, y: f64, w: f64, h: f64, icon: IconId, tooltip: []const u8, action: Action) void {const proximity = self.buttonProximity(action, self.px, self.py);const is_hover = self.px >= x and self.px <= x + w and self.py >= y and self.py <= y + h;- var layer = shell_ui.Layer.begin(cr, .{ .x = @floatCast(x), .y = @floatCast(y), .w = @floatCast(w), .h = @floatCast(h) }) orelse return;+ var layer = shell_ui.Layer.beginVisible(cr, .{ .x = @floatCast(x), .y = @floatCast(y), .w = @floatCast(w), .h = @floatCast(h) }) orelse return;defer layer.finish();ui_button.paint(&layer.renderer, layer.local(), .{.variant = .ghost,diff --git a/src/pdf/app_pointer.zig b/src/pdf/app_pointer.zigindex 7b4508a..a526e55 100644--- a/src/pdf/app_pointer.zig+++ b/src/pdf/app_pointer.zig@@ -74,134 +74,133 @@ fn toolbarProximityChanged(return false;}-fn contentRectProximityChanged(+/// Adds `rect` to the glow damage when the pointer moving from old to new+/// can change its glow (either position within reach).+fn damageRectProximity(+ self: *App,rect: Rect,old_x: f64,old_y: f64,new_x: f64,new_y: f64,-) bool {+) void {const box = button_widget.Rect{.x = @floatCast(rect.x),.y = @floatCast(rect.y),.w = @floatCast(rect.w),.h = @floatCast(rect.h),};- const before = button_widget.sampleProximity(- box, @floatCast(old_x), @floatCast(old_y), box,- );- const after = button_widget.sampleProximity(- box, @floatCast(new_x), @floatCast(new_y), box,- );- return @min(before.distance, after.distance) <= ui_theme.global.border_glow_reach;+ const before = button_widget.sampleProximity(box, @floatCast(old_x), @floatCast(old_y), box);+ const after = button_widget.sampleProximity(box, @floatCast(new_x), @floatCast(new_y), box);+ if (@min(before.distance, after.distance) <= ui_theme.global.border_glow_reach)+ self.pointer_damage.add(.around(rect.x, rect.y, rect.w, rect.h));}-fn contentProximityChanged(- self: *const App,+/// Records the content items (dialog controls, sidebar tabs, cards and rows,+/// the page field, the search bar, the scrollbar) whose glow the motion+/// changes, so the client repaints only those.+fn damageContentProximity(+ self: *App,old_x: f64,old_y: f64,new_x: f64,new_y: f64,-) bool {- if (old_x == new_x and old_y == new_y) return false;- if (!button_widget.proximityGlowVisible(ui_theme.global)) return false;-+) void {+ if (old_x == new_x and old_y == new_y) return;+ if (!button_widget.proximityGlowVisible(ui_theme.global)) return;++ const ox = old_x;+ const oy = old_y;+ const nx = new_x;+ const ny = new_y;if (self.dialog != .none) {const dw: f64 = 440;const dh: f64 = 190;const dx = (@as(f64, @floatFromInt(self.w)) - dw) / 2.0;const dy = (@as(f64, @floatFromInt(self.h)) - dh) / 2.0;- if (self.dialog == .password and contentRectProximityChanged(- .{ .x = dx + 24, .y = dy + 80, .w = dw - 48, .h = 44 },- old_x, old_y, new_x, new_y,- )) return true;- if (contentRectProximityChanged(- .{ .x = dx + dw - 180, .y = dy + dh - 46, .w = 80, .h = 32 },- old_x, old_y, new_x, new_y,- )) return true;- return contentRectProximityChanged(- .{ .x = dx + dw - 90, .y = dy + dh - 46, .w = 70, .h = 32 },- old_x, old_y, new_x, new_y,- );+ if (self.dialog == .password) damageRectProximity(self, .{ .x = dx + 24, .y = dy + 80, .w = dw - 48, .h = 44 }, ox, oy, nx, ny);+ damageRectProximity(self, .{ .x = dx + dw - 180, .y = dy + dh - 46, .w = 80, .h = 32 }, ox, oy, nx, ny);+ damageRectProximity(self, .{ .x = dx + dw - 90, .y = dy + dh - 46, .w = 70, .h = 32 }, ox, oy, nx, ny);+ return;}if (self.sidebar_open) {const sb = self.sidebarRect();const tab_w = sb.w / 2.0;- if (contentRectProximityChanged(- .{ .x = sb.x + 4, .y = sb.y + 4, .w = tab_w - 6, .h = 24 },- old_x, old_y, new_x, new_y,- )) return true;- if (contentRectProximityChanged(- .{ .x = sb.x + tab_w + 2, .y = sb.y + 4, .w = tab_w - 6, .h = 24 },- old_x, old_y, new_x, new_y,- )) return true;+ damageRectProximity(self, .{ .x = sb.x + 4, .y = sb.y + 4, .w = tab_w - 6, .h = 24 }, ox, oy, nx, ny);+ damageRectProximity(self, .{ .x = sb.x + tab_w + 2, .y = sb.y + 4, .w = tab_w - 6, .h = 24 }, ox, oy, nx, ny);if (self.sidebar_tab == .pages and self.sidebar_layout != null) {const sbl = self.sidebar_layout.?;- const visible = layout.findVisibleSidebarCards(- sbl.cards, self.sidebar_scroll_y, sb.h - 36.0,- );- if (visible) |range| {- for (range.start_index..range.end_index + 1) |i| {- const card = sbl.cards[i];- if (contentRectProximityChanged(- .{- .x = sb.x + 16,- .y = sb.y + 36 + card.y_offset - self.sidebar_scroll_y,- .w = self.sidebar_w - 32,- .h = card.card_h,- },- old_x, old_y, new_x, new_y,- )) return true;- }+ if (layout.findVisibleSidebarCards(sbl.cards, self.sidebar_scroll_y, sb.h - 36.0)) |range| {+ for (sbl.cards[range.start_index .. range.end_index + 1]) |card| damageRectProximity(self, .{+ .x = sb.x + 16,+ .y = sb.y + 36 + card.y_offset - self.sidebar_scroll_y,+ .w = self.sidebar_w - 32,+ .h = card.card_h,+ }, ox, oy, nx, ny);}} else if (self.sidebar_tab == .outline and self.outline != null) {+ // Only the rows `renderOutlineList` paints: from the first one+ // reaching below the tabs to the last one starting above the end.const row_h = 28.0;- for (self.outline.?, 0..) |_, idx| {- const row_y = sb.y + 40.0 +- @as(f64, @floatFromInt(idx)) * row_h -- self.sidebar_scroll_y;- if (row_y + row_h < sb.y + 36 or row_y > sb.y + sb.h) continue;- if (contentRectProximityChanged(- .{ .x = sb.x + 4, .y = row_y, .w = sb.w - 8, .h = row_h },- old_x, old_y, new_x, new_y,- )) return true;+ const items = self.outline.?;+ var idx: usize = @intFromFloat(@max(0, @floor((self.sidebar_scroll_y - 32.0) / row_h)));+ while (idx < items.len) : (idx += 1) {+ const row_y = sb.y + 40.0 + @as(f64, @floatFromInt(idx)) * row_h - self.sidebar_scroll_y;+ if (row_y > sb.y + sb.h) break;+ if (row_y + row_h < sb.y + 36) continue;+ damageRectProximity(self, .{ .x = sb.x + 4, .y = row_y, .w = sb.w - 8, .h = row_h }, ox, oy, nx, ny);}}}// Page-number entry is a content redraw, not a toolbar-button redraw.- if (contentRectProximityChanged(- .{ .x = 126, .y = 8, .w = 48, .h = 28 },- old_x, old_y, new_x, new_y,- )) return true;+ damageRectProximity(self, .{ .x = 126, .y = 8, .w = 48, .h = 28 }, ox, oy, nx, ny);if (self.search_open) {const sr = self.searchBarRect();- if (contentRectProximityChanged(- .{ .x = sr.x + 10, .y = sr.y, .w = sr.w - 110, .h = sr.h },- old_x, old_y, new_x, new_y,- )) return true;+ damageRectProximity(self, .{ .x = sr.x + 10, .y = sr.y, .w = sr.w - 110, .h = sr.h }, ox, oy, nx, ny);const close_x = sr.x + sr.w - 28;const next_x = close_x - 28;const prev_x = next_x - 28;for ([_]f64{ prev_x, next_x, close_x }) |x| {- if (contentRectProximityChanged(- .{ .x = x, .y = sr.y, .w = 28, .h = sr.h },- old_x, old_y, new_x, new_y,- )) return true;+ damageRectProximity(self, .{ .x = x, .y = sr.y, .w = 28, .h = sr.h }, ox, oy, nx, ny);}}if (self.hbar()) |bar| {const track = bar.track;- if (contentRectProximityChanged(- .{ .x = track.x, .y = track.y, .w = track.w, .h = track.h },- old_x, old_y, new_x, new_y,- )) return true;+ damageRectProximity(self, .{ .x = track.x, .y = track.y, .w = track.w, .h = track.h }, ox, oy, nx, ny);}- return false;+}++/// Damages page card `index` in the sidebar (its highlight border, which+/// strokes the card's edge), or the whole window when it has no card.+fn damageCard(self: *App, index: usize) void {+ const sbl = self.sidebar_layout orelse {+ self.dirty = true;+ return;+ };+ if (!self.sidebar_open or index >= sbl.cards.len) {+ self.dirty = true;+ return;+ }+ const sb = self.sidebarRect();+ const card = sbl.cards[index];+ const y = sb.y + 36 + card.y_offset - self.sidebar_scroll_y;+ self.pointer_damage.add(.around(sb.x + 16, y, self.sidebar_w - 32, card.card_h));+}++/// Damages outline row `index` in the sidebar.+fn damageOutlineRow(self: *App, index: usize) void {+ if (!self.sidebar_open) {+ self.dirty = true;+ return;+ }+ const sb = self.sidebarRect();+ const y = sb.y + 40.0 + @as(f64, @floatFromInt(index)) * 28.0 - self.sidebar_scroll_y;+ self.pointer_damage.add(.around(sb.x + 4, y, sb.w - 8, 28));}pub fn handleMotion(self: *App, px: f64, py: f64) void {@@ -211,7 +210,7 @@ pub fn handleMotion(self: *App, px: f64, py: f64) void {self.py = py;if (toolbarProximityChanged(self, old_x, old_y, px, py)) self.toolbar_dirty = true;- if (contentProximityChanged(self, old_x, old_y, px, py)) self.dirty = true;+ damageContentProximity(self, old_x, old_y, px, py);if (self.hbar_drag.active) {if (self.hbar()) |bar| self.scroll_x = self.hbar_drag.offsetAt(bar, @floatCast(px), @floatCast(py));@@ -296,7 +295,9 @@ pub fn handleMotion(self: *App, px: f64, py: f64) void {{const sbl = self.sidebar_layout.?;const visible = layout.findVisibleSidebarCards(- sbl.cards, self.sidebar_scroll_y, sb.h - 36.0,+ sbl.cards,+ self.sidebar_scroll_y,+ sb.h - 36.0,);if (visible) |range| {for (range.start_index..range.end_index + 1) |i| {@@ -326,11 +327,12 @@ pub fn handleMotion(self: *App, px: f64, py: f64) void {}}if (self.hover_thumb != found_thumb or self.hover_outline != found_outline) {+ // Only the cards or rows whose highlight changes repaint.+ for ([_]?usize{ self.hover_thumb, found_thumb }) |maybe| if (maybe) |i| damageCard(self, i);+ for ([_]?usize{ self.hover_outline, found_outline }) |maybe| if (maybe) |i| damageOutlineRow(self, i);self.hover_thumb = found_thumb;self.hover_outline = found_outline;- self.dirty = true;}-}pub fn handleButton(self: *App, button: u32, pressed: bool) void {diff --git a/src/pdf/client_render.zig b/src/pdf/client_render.zigindex 65c3347..df2d258 100644--- a/src/pdf/client_render.zig+++ b/src/pdf/client_render.zig@@ -4,6 +4,7 @@ const std = @import("std");const c = @import("c.zig");const ui_theme = @import("ui").theme;const shell_ui = @import("ui").cairo;+const repaint = @import("ui").repaint;const main_mod = @import("main.zig");const Client = main_mod.Client;const Buffer = main_mod.Buffer;@@ -27,26 +28,35 @@ pub fn acquireBuffer(self: *Client, w: i32, h: i32) !?*Buffer {pub fn render(self: *Client) !void {self.updateCursor();- if (!self.configured or !(self.app.dirty or self.app.toolbar_dirty) or self.outstanding >= 2) return;- if (!self.app.dirty and self.frame_callback != null) return;+ const glow_only = !self.app.dirty and (self.app.toolbar_dirty or !self.app.pointer_damage.isEmpty());+ if (!self.configured or !(self.app.dirty or glow_only) or self.outstanding >= 2) return;+ if (glow_only and self.frame_callback != null) return;const tbh = self.titlebarHeight();const w = self.app.w;const h = self.app.h + tbh;const buffer = (try self.acquireBuffer(w * self.scale, h * self.scale)) orelse return;- // A proximity-glow frame repaints only the toolbar strip when this- // buffer already holds everything else (see `content_frame`).- const partial = !self.app.dirty and buffer.frame != 0 and- buffer.frame >= self.content_frame and self.applied_scale == self.scale;- const strip_h: i32 = @intFromFloat(@ceil(self.app.toolbarRect().h));+ // A proximity-glow frame repaints only the toolbar strip and the glowing+ // items, plus whatever changed since this buffer was painted.+ var frame: repaint.Region = .{};+ if (!glow_only or self.applied_scale != self.scale) frame = .whole else {+ if (self.app.toolbar_dirty) frame.add(.{ .x = 0, .y = tbh, .w = w, .h = @intFromFloat(@ceil(self.app.toolbarRect().h)) });+ for (self.app.pointer_damage.slice()) |r| frame.add(.{ .x = r.x, .y = r.y + tbh, .w = r.w, .h = r.h });+ }+ var paint = buffer.stale;+ paint.merge(&frame);+ if (paint.covers(w, h, 0.5)) paint = .whole;const cr = c.api.cairo_create(buffer.image) orelse return error.CairoFailed;defer c.api.cairo_destroy(cr);c.api.cairo_scale(cr, @floatFromInt(self.scale), @floatFromInt(self.scale));- if (partial) {- c.api.cairo_rectangle(cr, 0, @floatFromInt(tbh), @floatFromInt(w), @floatFromInt(strip_h));+ if (!paint.full) {+ for (paint.slice()) |r| c.api.cairo_rectangle(cr, @floatFromInt(r.x), @floatFromInt(r.y), @floatFromInt(r.w), @floatFromInt(r.h));+ // Content only: the titlebar repaints on full frames.+ c.api.cairo_clip(cr);+ c.api.cairo_rectangle(cr, 0, @floatFromInt(tbh), @floatFromInt(w), @floatFromInt(h - tbh));c.api.cairo_clip(cr);} else if (tbh > 0) self.renderTitlebar(cr, w, tbh);@@ -64,9 +74,8 @@ pub fn render(self: *Client) !void {self.xdg_surface.setWindowGeometry(0, 0, w, h);buffer.busy = true;self.outstanding += 1;- self.frames += 1;- buffer.frame = self.frames;- if (self.app.dirty) self.content_frame = self.frames;+ buffer.stale = .{};+ for (self.buffers) |slot| if (slot) |other| if (other != buffer) other.stale.merge(&frame);if (self.frame_callback == null) {if (self.surface.frame()) |callback| {callback.setListener(*Client, frameDone, self);@@ -74,10 +83,11 @@ pub fn render(self: *Client) !void {} else |_| {}}self.surface.attach(buffer.wl_buffer, 0, 0);- if (partial) self.surface.damage(0, tbh, w, strip_h) else self.surface.damage(0, 0, w, h);+ if (frame.full) self.surface.damage(0, 0, w, h) else for (frame.slice()) |r| self.surface.damage(r.x, r.y, r.w, r.h);self.surface.commit();self.app.dirty = false;self.app.toolbar_dirty = false;+ self.app.pointer_damage.clear();}fn frameDone(callback: *wl.Callback, _: wl.Callback.Event, self: *Client) void {diff --git a/src/pdf/main.zig b/src/pdf/main.zigindex 8147d9f..f9a6229 100644--- a/src/pdf/main.zig+++ b/src/pdf/main.zig@@ -75,11 +75,6 @@ pub const Client = struct {outstanding: usize = 0,buffers: [2]?*Buffer = @splat(null),applied_scale: i32 = 0,- /// Frames painted so far, and the last one whose content outside the- /// toolbar strip changed: a buffer holding that frame or a later one- /// needs only the strip repainted for a `toolbar_dirty` frame.- frames: u64 = 0,- content_frame: u64 = 0,/// Pending `wl_surface.frame` callback of the last commit. Glow-only/// frames wait for it, so they follow the display, not the pointer rate.frame_callback: ?*wl.Callback = null,@@ -165,8 +160,9 @@ pub const Buffer = struct {w: i32,h: i32,busy: bool = false,- /// `Client.frames` when this buffer was last painted; 0 = never.- frame: u64 = 0,+ /// What changed on screen since this buffer was last painted, in window+ /// coordinates (see `ui.repaint`).+ stale: @import("ui").repaint.Region = .whole,pub fn create(client: *Client, w: i32, h: i32) !*Buffer {if (w <= 0 or h <= 0 or w > 16384 or h > 16384) return error.InvalidSize;diff --git a/src/ui/AGENTS.md b/src/ui/AGENTS.mdindex a387fd0..30e1ffc 100644--- a/src/ui/AGENTS.md+++ b/src/ui/AGENTS.md@@ -76,3 +76,12 @@ text and colours, secrets) are in the root `AGENTS.md` ("Shared UI").or leave the damage bounds. `Renderer.reliefActive` tells it whether to dropthe hand-made rim or cast shadow the relief replaces; flat keys must leaveits pixels byte-identical.+* **Pointer effects in Cairo app clients repaint only what they change.**+ `cairo.drawProximityGlow` returns before allocating a layer when the glow is+ off, unlit, or outside the context's clip, and sizes its layer to the visible+ part; paint-only components use `Layer.beginVisible`. Images and PDF record+ glow and hover changes as rects in `pointer_damage` (`repaint.Region`) and+ repaint them clipped, paced by the frame callback, with each shm buffer's+ `stale` region catching up what it missed; never answer a glow change with+ `dirty` (a full, unpaced repaint at pointer rate). Hand-drawn content must+ stay inside the rects a damage helper adds for it.diff --git a/src/ui/cairo.zig b/src/ui/cairo.zigindex 75f6ab6..9b6e58b 100644--- a/src/ui/cairo.zig+++ b/src/ui/cairo.zig@@ -93,8 +93,25 @@ pub fn drawProximityGlow(clip: ?Rect,) void {const p = pointer orelse return;+ const t = &theme.global;+ // Callers paint this for every card or row on every repaint, so every+ // reason it would paint nothing is settled before a layer is allocated.+ if (!button.proximityGlowVisible(t.*)) return;+ const cr: *c.cairo_t = @ptrCast(cr_any);const clipping = clip orelse box;- const visible = button.intersectRect(box, clipping);+ var visible = button.intersectRect(box, clipping);+ // A partial repaint clips most items away entirely.+ var cx0: f64 = 0;+ var cy0: f64 = 0;+ var cx1: f64 = 0;+ var cy1: f64 = 0;+ c.cairo_clip_extents(cr, &cx0, &cy0, &cx1, &cy1);+ visible = button.intersectRect(visible, .{+ .x = @floatCast(cx0),+ .y = @floatCast(cy0),+ .w = @floatCast(cx1 - cx0),+ .h = @floatCast(cy1 - cy0),+ });if (visible.w <= 0 or visible.h <= 0) return;const sample = button.sampleProximity(@@ -104,17 +121,42 @@ pub fn drawProximityGlow(clipping,);if (sample.distance >= std.math.inf(f32)) return;+ if (!button.proximityGlowLit(box.w, box.h, radius, sample.x, sample.y, t.border_glow_reach)) return;++ // The layer covers only the visible part of the box, starting a whole+ // number of device pixels into it, so each pixel keeps the position it+ // would have in a layer over the whole box.+ const s: f64 = scaleOf(cr);+ var bx: f64 = box.x;+ var by: f64 = box.y;+ c.cairo_user_to_device(cr, &bx, &by);+ var vx: f64 = visible.x;+ var vy: f64 = visible.y;+ c.cairo_user_to_device(cr, &vx, &vy);+ const full_w = @ceil(box.w * s);+ const full_h = @ceil(box.h * s);+ const k0x = std.math.clamp(@floor(vx - @round(bx)), 0, full_w);+ const k0y = std.math.clamp(@floor(vy - @round(by)), 0, full_h);+ const k1x = std.math.clamp(@ceil(vx + visible.w * s - @round(bx)), k0x, full_w);+ const k1y = std.math.clamp(@ceil(vy + visible.h * s - @round(by)), k0y, full_h);+ if (k1x <= k0x or k1y <= k0y) return;+ const sub: Rect = .{+ .x = @floatCast(box.x + k0x / s),+ .y = @floatCast(box.y + k0y / s),+ .w = @floatCast((k1x - k0x) / s),+ .h = @floatCast((k1y - k0y) / s),+ };- var layer = Layer.begin(cr_any, box) orelse return;+ var layer = Layer.begin(cr_any, sub) orelse return;layer.renderer.clip = .{- .x = visible.x - box.x,- .y = visible.y - box.y,+ .x = visible.x - sub.x,+ .y = visible.y - sub.y,.w = visible.w,.h = visible.h,};button.paintProximityGlowAt(&layer.renderer,- layer.local(),+ .{ .x = box.x - sub.x, .y = box.y - sub.y, .w = box.w, .h = box.h },radius,sample.distance,sample.x,@@ -330,6 +372,16 @@ fn composite(cr: *c.cairo_t, pixels: []u32, w: i32, h: i32, device_x: f64, devicc.cairo_paint(cr);}+/// Whether any of `box` (user space) lies inside the context's clip.+pub fn clipTouches(cr_any: *anyopaque, box: Rect) bool {+ var x0: f64 = 0;+ var y0: f64 = 0;+ var x1: f64 = 0;+ var y1: f64 = 0;+ c.cairo_clip_extents(@ptrCast(cr_any), &x0, &y0, &x1, &y1);+ return box.w > 0 and box.h > 0 and box.x < x1 and box.x + box.w > x0 and box.y < y1 and box.y + box.h > y0;+}+/// An offscreen buffer over `box` (user space), snapped to device pixels./// Paint into `renderer` in box-local coordinates (`local()`), then `finish`/// composites it through the context's clip.@@ -359,6 +411,14 @@ pub const Layer = struct {return .{ .cr = cr, .pixels = pixels, .width = w, .height = h, .device_x = @round(dx), .device_y = @round(dy), .box = box, .renderer = renderer };}+ /// `begin`, or null when the context's clip leaves none of `box`+ /// visible. For components that only paint: a partial repaint then+ /// skips rasterizing everything outside its clip.+ pub fn beginVisible(cr_any: *anyopaque, box: Rect) ?Layer {+ if (!clipTouches(cr_any, box)) return null;+ return begin(cr_any, box);+ }+/// The layer's own box in the coordinates `renderer` paints in.pub fn local(self: *const Layer) Rect {return .{ .x = 0, .y = 0, .w = self.box.w, .h = self.box.h };@@ -442,6 +502,58 @@ test "Cairo proximity glow respects the visible clip" {try std.testing.expectEqual(@as(u32, 0), pixels[10 * 80 + 45]);}+test "Cairo proximity glow under a context clip matches the unclipped pixels" {+ const saved_theme = theme.global;+ defer theme.global = saved_theme;+ var glowing = theme.global;+ glowing.button_hover_glow_enabled = true;+ glowing.border_glow = .{ 1, 0, 0, 0.8 };+ glowing.border_glow_reach = 56;+ glowing.border_glow_length = 72;+ glowing.border_glow_depth = 3;+ theme.global = glowing;++ const W = 300;+ const H = 240;+ var images: [2]*c.cairo_surface_t = undefined;+ for (&images, 0..) |*image, pass| {+ image.* = c.cairo_image_surface_create(c.CAIRO_FORMAT_ARGB32, W, H) orelse return error.Cairo;+ const cr = c.cairo_create(image.*) orelse return error.Cairo;+ defer c.cairo_destroy(cr);+ c.cairo_scale(cr, 1.5, 1.5);+ c.cairo_translate(cr, 0, 7);+ // The second pass repaints only a band across the lit top edge.+ if (pass == 1) {+ c.cairo_rectangle(cr, 30, 30, 100, 30);+ c.cairo_clip(cr);+ }+ drawProximityGlow(cr, .{ .x = 20.3, .y = 25.6, .w = 150, .h = 110 }, 7, .{ .x = 60, .y = 22 }, null);+ // A pointer far outside the reach paints nothing.+ drawProximityGlow(cr, .{ .x = 20.3, .y = 25.6, .w = 30, .h = 20 }, 7, .{ .x = 190, .y = 140 }, null);+ c.cairo_surface_flush(image.*);+ }+ defer for (images) |image| c.cairo_surface_destroy(image);+ const full: [*]const u32 = @ptrCast(@alignCast(c.cairo_image_surface_get_data(images[0])));+ const part: [*]const u32 = @ptrCast(@alignCast(c.cairo_image_surface_get_data(images[1])));+ var lit: usize = 0;+ for (0..H) |y| for (0..W) |x| {+ const i = y * W + x;+ // Device pixels wholly inside the clip: x 45..195, y 55.5..100.5.+ const inside = x >= 45 and x < 195 and y >= 56 and y < 100;+ if (!inside) {+ if (y < 55 or y > 100 or x < 45 or x >= 195) try std.testing.expectEqual(@as(u32, 0), part[i]);+ continue;+ }+ if (part[i] != 0) lit += 1;+ inline for (.{ 0, 8, 16, 24 }) |shift| {+ const a: i32 = @intCast((full[i] >> shift) & 0xff);+ const b: i32 = @intCast((part[i] >> shift) & 0xff);+ try std.testing.expect(@abs(a - b) <= 1);+ }+ };+ try std.testing.expect(lit > 0);+}+test "Cairo scrollbar groove glow stays beneath the thumb" {const saved_theme = theme.global;defer theme.global = saved_theme;diff --git a/src/ui/mod.zig b/src/ui/mod.zigindex d399a2b..0d7abd1 100644--- a/src/ui/mod.zig+++ b/src/ui/mod.zig@@ -33,6 +33,7 @@ pub const widgets = struct {};pub const anim = @import("anim.zig");pub const cairo = @import("cairo.zig");+pub const repaint = @import("repaint.zig");pub const client_cursor = @import("client_cursor.zig");pub const compose = @import("compose.zig");pub const context_menu = @import("context_menu.zig");diff --git a/src/ui/repaint.zig b/src/ui/repaint.zignew file mode 100644index 0000000..b07bd2f--- /dev/null+++ b/src/ui/repaint.zig@@ -0,0 +1,156 @@+//! What part of a recycled shm buffer needs repainting, for app clients+//! (Images, PDF) whose pointer effects repaint only what changed.+//!+//! A frame's own damage (`Region`) is what changed since the frame before it,+//! which is what the surface is told. A buffer last painted several frames ago+//! also missed those frames' damage: every frame adds its damage to the other+//! buffers' `stale`, and a buffer repaints its stale region plus the frame's.+//! Rects are in whole logical pixels (the clients use integer buffer scales),+//! a handful at most: past that they merge into bounding boxes.+const std = @import("std");++pub const Rect = struct {+ x: i32,+ y: i32,+ w: i32,+ h: i32,++ /// The whole logical pixels covering a fractional box, one pixel wider+ /// on each side for antialiased edges.+ pub fn around(x: f64, y: f64, w: f64, h: f64) Rect {+ const x0: i32 = @intFromFloat(@floor(x) - 1);+ const y0: i32 = @intFromFloat(@floor(y) - 1);+ const x1: i32 = @intFromFloat(@ceil(x + w) + 1);+ const y1: i32 = @intFromFloat(@ceil(y + h) + 1);+ return .{ .x = x0, .y = y0, .w = x1 - x0, .h = y1 - y0 };+ }++ fn empty(r: Rect) bool {+ return r.w <= 0 or r.h <= 0;+ }++ fn join(a: Rect, b: Rect) Rect {+ const x0 = @min(a.x, b.x);+ const y0 = @min(a.y, b.y);+ return .{ .x = x0, .y = y0, .w = @max(a.x + a.w, b.x + b.w) - x0, .h = @max(a.y + a.h, b.y + b.h) - y0 };+ }++ fn touches(a: Rect, b: Rect) bool {+ return a.x <= b.x + b.w and b.x <= a.x + a.w and a.y <= b.y + b.h and b.y <= a.y + a.h;+ }++ fn area(r: Rect) i64 {+ return @as(i64, r.w) * r.h;+ }+};++pub const Region = struct {+ pub const capacity = 6;++ full: bool = false,+ len: usize = 0,+ rects: [capacity]Rect = undefined,++ pub const whole: Region = .{ .full = true };++ pub fn isEmpty(self: *const Region) bool {+ return !self.full and self.len == 0;+ }++ pub fn clear(self: *Region) void {+ self.* = .{};+ }++ pub fn add(self: *Region, rect: Rect) void {+ if (self.full or rect.empty()) return;+ var r = rect;+ // Fold in every rect it touches, repeating as the union grows.+ var i: usize = 0;+ while (i < self.len) {+ if (self.rects[i].touches(r)) {+ r = r.join(self.rects[i]);+ self.len -= 1;+ self.rects[i] = self.rects[self.len];+ i = 0;+ } else i += 1;+ }+ if (self.len < capacity) {+ self.rects[self.len] = r;+ self.len += 1;+ return;+ }+ // Full: merge with the rect whose union grows least.+ var best: usize = 0;+ var growth: i64 = std.math.maxInt(i64);+ for (self.rects[0..self.len], 0..) |other, j| {+ const g = r.join(other).area() - other.area();+ if (g < growth) {+ growth = g;+ best = j;+ }+ }+ const merged = r.join(self.rects[best]);+ self.len -= 1;+ self.rects[best] = self.rects[self.len];+ self.add(merged);+ }++ pub fn merge(self: *Region, other: *const Region) void {+ if (other.full) {+ self.full = true;+ self.len = 0;+ return;+ }+ for (other.rects[0..other.len]) |r| self.add(r);+ }++ pub fn slice(self: *const Region) []const Rect {+ return self.rects[0..self.len];+ }++ /// Whether the rects cover at least `fraction` of a `w`×`h` surface (by+ /// their summed areas): a repaint that large might as well be full.+ pub fn covers(self: *const Region, w: i32, h: i32, fraction: f64) bool {+ if (self.full) return true;+ var sum: i64 = 0;+ for (self.slice()) |r| sum += r.area();+ return @as(f64, @floatFromInt(sum)) >= fraction * @as(f64, @floatFromInt(@as(i64, w) * h));+ }+};++test "touching rects merge and disjoint ones stay apart" {+ var r: Region = .{};+ r.add(.{ .x = 0, .y = 0, .w = 10, .h = 10 });+ r.add(.{ .x = 10, .y = 0, .w = 10, .h = 10 });+ try std.testing.expectEqual(@as(usize, 1), r.len);+ try std.testing.expectEqual(Rect{ .x = 0, .y = 0, .w = 20, .h = 10 }, r.rects[0]);+ r.add(.{ .x = 100, .y = 100, .w = 5, .h = 5 });+ try std.testing.expectEqual(@as(usize, 2), r.len);+ // Bridging both folds all three together.+ r.add(.{ .x = 15, .y = 5, .w = 90, .h = 96 });+ try std.testing.expectEqual(@as(usize, 1), r.len);+ try std.testing.expectEqual(Rect{ .x = 0, .y = 0, .w = 105, .h = 105 }, r.rects[0]);+}++test "a full region absorbs everything and overflow keeps every pixel" {+ var r: Region = .{};+ for (0..Region.capacity + 3) |i| r.add(.{ .x = @intCast(i * 50), .y = 0, .w = 10, .h = 10 });+ try std.testing.expectEqual(@as(usize, Region.capacity), r.len);+ for (0..Region.capacity + 3) |i| {+ const x: i32 = @intCast(i * 50);+ var covered = false;+ for (r.slice()) |q| covered = covered or (q.x <= x and x + 10 <= q.x + q.w);+ try std.testing.expect(covered);+ }+ var all: Region = .{};+ all.merge(&r);+ all.merge(&Region.whole);+ try std.testing.expect(all.full and all.len == 0);+ all.add(.{ .x = 0, .y = 0, .w = 1, .h = 1 });+ try std.testing.expect(all.covers(10, 10, 1));+ try std.testing.expect(!r.covers(1000, 1000, 0.5));+}++test "around covers a fractional box with a pixel of margin" {+ try std.testing.expectEqual(Rect{ .x = 9, .y = 4, .w = 13, .h = 8 }, Rect.around(10.5, 5, 10.2, 5.5));+}diff --git a/src/ui/widgets/button.zig b/src/ui/widgets/button.zigindex 5a6fb6c..078752e 100644--- a/src/ui/widgets/button.zig+++ b/src/ui/widgets/button.zig@@ -319,6 +319,20 @@ pub fn proximityGlowVisible(t: theme.Theme) bool {t.border_glow_reach > 0 and t.border_glow_length > 0 and t.border_glow_depth > 0;}+/// Whether `paintProximityGlowAt` lights any edge of a `w`×`h` box for a+/// box-local pointer (the same edge anchors and falloff). Callers that paint+/// through an offscreen layer test this first and skip the layer otherwise.+pub fn proximityGlowLit(w: f32, h: f32, radius: f32, pointer_x: f32, pointer_y: f32, reach: f32) bool {+ if (w <= 0 or h <= 0) return false;+ const corner_radius = std.math.clamp(radius, 0, 0.5 * @min(w, h));+ const ax = std.math.clamp(pointer_x, corner_radius, w - corner_radius);+ const ay = std.math.clamp(pointer_y, corner_radius, h - corner_radius);+ return glowNear(pointer_x, pointer_y, ax, 0, reach) > 0 or+ glowNear(pointer_x, pointer_y, w, ay, reach) > 0 or+ glowNear(pointer_x, pointer_y, ax, h, reach) > 0 or+ glowNear(pointer_x, pointer_y, 0, ay, reach) > 0;+}+/// Shared edge-light painter for buttons and other interactive surfaces./// Pointer coordinates are local to `box`.pub fn paintProximityGlowAt(