diff options
| -rw-r--r-- | src/fonts.zig | 28 | ||||
| -rw-r--r-- | src/gui/gui.zig | 147 |
2 files changed, 161 insertions, 14 deletions
diff --git a/src/fonts.zig b/src/fonts.zig index 48fa7179..84ce5307 100644 --- a/src/fonts.zig +++ b/src/fonts.zig @@ -76,6 +76,20 @@ pub fn list(arena: std.mem.Allocator, want_name: ?[]const u8) []const Font { return scan(arena, null, true); } +/// A remote config's absolute font path belongs to its machine. Resolve the +/// same face by name on this machine, retaining the filename as a fallback. +pub fn remote(arena: std.mem.Allocator, path: []const u8, name: []const u8) ?Font { + if (name.len > 0) { + const matches = list(arena, name); + if (matches.len > 0) return matches[0]; + } + const base = std.Io.Dir.path.basename(path); + const stem = base[0 .. base.len - std.Io.Dir.path.extension(base).len]; + if (stem.len == 0 or std.mem.eql(u8, stem, name)) return null; + const matches = list(arena, stem); + return if (matches.len > 0) matches[0] else null; +} + /// Installed members of `fallback_names`, returned in preference order. This /// is runtime discovery only: no local font path or exploration result enters /// the build, and a machine with none simply uses embedded Adwaita Mono. @@ -406,3 +420,17 @@ test "the walk finds this machine's monospace faces" { try std.testing.expect(std.mem.endsWith(u8, menlo.path, ".ttc")); } } + +test "a remote font config resolves the installed face despite another machine's path" { + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + const alloc = arena.allocator(); + const installed = list(alloc, null); + if (installed.len == 0) return error.SkipZigTest; + const face = installed[0]; + const foreign = try std.fmt.allocPrint(alloc, "/other-machine/fonts/{s}", .{std.Io.Dir.path.basename(face.path)}); + const by_name = remote(alloc, foreign, face.name) orelse return error.MissingLocalFace; + try std.testing.expectEqualStrings(face.path, by_name.path); + const by_file = remote(alloc, foreign, "") orelse return error.MissingLocalFace; + try std.testing.expectEqualStrings(face.path, by_file.path); +} diff --git a/src/gui/gui.zig b/src/gui/gui.zig index 3eae658a..e16d0576 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -553,6 +553,9 @@ const Gui = struct { font_bytes: []u8 = &.{}, font_name: [255]u8 = @splat(0), font_name_len: u8 = 0, + /// A replica client's: the session's font request it last followed + /// (syncReplicaFont), as a hash of the request. + remote_font_request: ?u64 = null, fallbacks: [max_fallback_fonts]?LoadedFallback = @splat(null), fallback_count: usize = 0, px: f32, @@ -709,6 +712,43 @@ fn acknowledgeGuiFont(g: *const Gui, core: *pardes.Pardes) void { _ = core.acknowledgeFont(g.font_name[0..g.font_name_len], size, .pixels); } +/// A replica client's font is the window's raster state: it follows the +/// session's request by the face's name on this machine (fonts.remote), +/// never acknowledging it or otherwise changing the replicated core. +fn syncReplicaFont(g: *Gui, core: *const pardes.Pardes, gpa: std.mem.Allocator) bool { + const f = &core.settings.font; + const path = f.requested_path.get(); + const name = f.requested_name.get(); + if (path.len == 0 and name.len == 0) return false; + var hash = std.hash.Wyhash.init(0); + hash.update(path); + hash.update(name); + hash.update(std.mem.asBytes(&f.requested_size_hundredths)); + const request = hash.final(); + if (g.remote_font_request == request) return false; + g.remote_font_request = request; + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + const face = fonts.remote(arena.allocator(), path, name) orelse { + log.warn("remote font is not installed here: {s}", .{name}); + return false; + }; + const bytes = filesystem.readFile(gpa, face.path) catch return false; + const font = c.ui_font_new(bytes.ptr, @intCast(bytes.len)) orelse { + gpa.free(bytes); + return false; + }; + c.ui_font_free(g.font); + if (g.font_bytes.len != 0) gpa.free(g.font_bytes); + g.font = font; + g.font_bytes = bytes; // FreeType borrows them for the face lifetime + if (f.requested_size_hundredths != 0) + g.px = @as(f32, @floatFromInt(f.requested_size_hundredths)) / 100; + setGuiFontName(g, face.name); + refitFont(g, null); + return true; +} + fn loadFallbackFonts(g: *Gui, gpa: std.mem.Allocator) void { if (c.ui_font_new(font_ttf.ptr, @intCast(font_ttf.len))) |face| { g.fallbacks[0] = .{ .face = face }; @@ -1540,7 +1580,7 @@ const Client = struct { core.frames.mark_render = core.frames.mark_update; r.draw(.{ .ctx = s, .f = present }); cl.link.owed = core.present_skipped; - core.frames.record(pardes.FrameStats.nowNs(), false, cl.link.owed); + core.frames.record(pardes.FrameStats.nowNs(), if (s.gui) |g| frontendMoving(g) else false, cl.link.owed); core.present_skipped = skipped; core.needs_frame = needs; cl.link.drawn(s.presented); @@ -1571,6 +1611,8 @@ const OwnView = struct { fn run(v: *OwnView, core: *pardes.Pardes) anyerror!void { if (v.sev) |sev| return dispatch(v.g, v.in, sev); const g = v.g; + if (syncReplicaFont(g, core, v.s.gpa)) v.s.remote.?.link.dirty = true; + if (scrollRows(g, v.in, v.s.gpa)) v.s.remote.?.link.dirty = true; if (fitTaglineFont(g, core.settings.font.tagline_percent)) v.s.remote.?.link.dirty = true; const geom = windowCells(g); if (resizeOf(core.view(), geom.cols, geom.rows, g.cell_w, g.cell_h, g.tagline_width, g.tagline_height)) |ev| v.in.post(ev); @@ -1657,6 +1699,7 @@ fn remoteSession(init: std.process.Init, g: *Gui, opts_in: pardes.Options, test_ // session let it go to upgrade. The window stays as it is. while (true) { const core = r.core(); + g.remote_font_request = null; shell.core = core; shell.gview = joined.view; shell.surface = null; @@ -1691,16 +1734,31 @@ fn remoteSession(init: std.process.Init, g: *Gui, opts_in: pardes.Options, test_ client.draw(&shell); drew = shell.presented; } + // The window's own motions (its glide, its caret, a jump's + // trail) and its blink are on its clock: no unit need come. + const minimized = !test_mode and c.SDL_GetWindowFlags(g.window) & (c.SDL_WINDOW_MINIMIZED | c.SDL_WINDOW_OCCLUDED) != 0; + const moving = !minimized and frontendMoving(g); + const blink = if (minimized or shell.surface == null) null else blinkNow(g, core.settings.cursor_blink); const predicting = r.echo.n > 0 or r.echo.cursor != null or r.echo.nd > 0; var ms: c_int = if (test_mode) 16 else -1; if (demoDue(g)) |due| ms = if (ms < 0) due else @min(ms, due); if (predicting or link.owed) ms = if (ms < 0) frame_ms else @min(ms, frame_ms); + // A touchpad's fraction settling onto a row (scrollRows). + if (moving or g.scroll_lag != 0 or g.scroll_delta != 0) { + const frame_wait: c_int = @intCast(@max(1, (refreshNs(g) + std.time.ns_per_ms - 1) / std.time.ns_per_ms)); + ms = if (ms < 0) frame_wait else @min(ms, frame_wait); + } + if (blink) |b| if (if (b.edge) refreshNs(g) else b.next_ns) |ns| { + const wait: c_int = @intCast(@min(std.math.maxInt(c_int), ns / std.time.ns_per_ms + 1)); + ms = if (ms < 0) wait else @min(ms, wait); + }; if (link.quietCheck()) |wait| ms = if (ms < 0) @intCast(wait) else @min(ms, @as(c_int, @intCast(wait))); // Idle, its frame drawn: the next input needs no copy taken. if (ms != 0) r.ready(); var sev = std.mem.zeroes(c.SDL_Event); g.input_seen = false; var got = c.SDL_WaitEventTimeout(&sev, ms); + const timed_out = !got; while (got) : (got = c.SDL_PollEvent(&sev)) { if (sev.type == c.SDL_EVENT_USER) continue; var one: OwnView = .{ .g = g, .in = &in, .s = &shell, .sev = &sev }; @@ -1715,6 +1773,7 @@ fn remoteSession(init: std.process.Init, g: *Gui, opts_in: pardes.Options, test_ _ = c.SDL_SetWindowTitle(g.window, "pardes"); } if (link.owed or r.echo.changed) link.dirty = true; + if (moving or (blink != null and (blink.?.edge or timed_out))) link.dirty = true; if (r.diverged != null) link.quit = true; } if (init.environ_map.get("PARDES_REPLICA_STATS")) |file| link.writeStats(file); @@ -2386,13 +2445,17 @@ fn wheelAt(g: *Gui, in: *Input, w: WheelTurn, mc: MouseCell) void { while (g.program_wheel <= -1) : (g.program_wheel += 1) in.post(.{ .mouse = .{ .button = .wheel_up, .kind = .press, .col = mc.col, .row = mc.row, .tag_hit = mc.tag_hit, .body_hit = mc.body_hit, .ctrl = w.mods.ctrl, .shift = w.mods.shift, .alt = w.mods.alt } }); } else if (in.remote != null) { - // ponytail: a replica client's wheel moves the view a - // whole row a turn, with no glide and no touchpad's - // fraction: its core's clock is the session's, which - // moves only as its units come. + // A replica client's notch is whole rows at once, with no + // glide; a touchpad's fraction is the window's picture + // (scrollRows), its whole rows ordinary inputs. if (notchOf(w.y, w.integer_y)) |lines| { g.program_wheel = @floatFromInt(lines); - } else g.program_wheel = accumulateWheelDelta(g.program_wheel, w.y); + } else { + g.scroll_pane = id; + g.scroll_col = mc.col; + g.scroll_row = mc.row; + g.scroll_delta = accumulateWheelDelta(g.scroll_delta, w.y); + } while (g.program_wheel >= 1) : (g.program_wheel -= 1) in.post(.{ .mouse = .{ .button = .wheel_down, .kind = .press, .col = mc.col, .row = mc.row, .shift = true } }); while (g.program_wheel <= -1) : (g.program_wheel += 1) @@ -3915,10 +3978,19 @@ const settle_land: f32 = 0.02; fn stepScroll(g: *Gui, core: *pardes.Pardes, gpa: std.mem.Allocator) void { stepGlide(g, core); - const id = g.scroll_pane orelse return; + var in: Input = .{ .core = core }; + _ = scrollRows(g, &in, gpa); +} + +/// A touchpad's fraction: the picture moved by it, and each whole row it +/// crosses posted to `in` (a replica client's go to its link, applied +/// after its view is parked again). True when the picture moved. +fn scrollRows(g: *Gui, in: *Input, gpa: std.mem.Allocator) bool { + const core = in.core; + const id = g.scroll_pane orelse return false; const pane = core.panes[id] orelse { resetScroll(g); - return; + return false; }; // A fine wheel's or a touchpad's turns stopped (SDL tells no gesture's // end): the picture eases the rest of the way onto the core's row, on @@ -3937,12 +4009,12 @@ fn stepScroll(g: *Gui, core: *pardes.Pardes, gpa: std.mem.Allocator) void { g.scroll_delta = 0; if (queued_delta == 0) { if (g.scroll_lag == 0 and g.scroll_notch == 0 and !g.glide.moving()) resetScroll(g); - return; + return false; } const r = core.view().rects[id]; if (r.h <= pane.tag_rows) { resetScroll(g); - return; + return false; } const delta = boundScrollDelta(g.scroll_lag, queued_delta, r.h - pane.tag_rows); const st = applyScrollDelta(g.scroll_lag, delta); @@ -3951,7 +4023,7 @@ fn stepScroll(g: *Gui, core: *pardes.Pardes, gpa: std.mem.Allocator) void { const down = left > 0; left += if (down) -1 else 1; const was = pane.scroll(); - core.update(.{ + in.post(.{ .mouse = .{ .button = if (down) .wheel_down else .wheel_up, .kind = .press, @@ -3960,16 +4032,19 @@ fn stepScroll(g: *Gui, core: *pardes.Pardes, gpa: std.mem.Allocator) void { .shift = true, // pardes's own scroll, never a program's wheel }, }); - if (pane.scroll() == was) { + // A replica client's rows apply after its view is parked again: + // its scroll has not moved yet. + if (in.remote == null and pane.scroll() == was) { resetScroll(g); - return; + return false; } } // The picture moved, by a fraction or back onto the core's row with // the core where it was: that is a frame too, or the window rests on // the last one, a fraction of a row off, and the next turn's frame // jumps from there (against the turn when it reverses). - if (st.rows != 0 or st.lag != g.scroll_lag) core.needs_frame = true; + const moved = st.rows != 0 or st.lag != g.scroll_lag; + if (moved and in.remote == null) core.needs_frame = true; g.scroll_lag = st.lag; g.scroll_rect = r; const body_y = core.bodyTop(pane, r); @@ -3997,6 +4072,7 @@ fn stepScroll(g: *Gui, core: *pardes.Pardes, gpa: std.mem.Allocator) void { } } if (g.scroll_lag == 0) resetScroll(g); + return moved; } /// A scrolling body's picture: its rows moved by the lag, a glide's or a @@ -9016,3 +9092,46 @@ test "a detached GUI authority follows terminal directory changes with no window pollFrame(&shell); try std.testing.expectEqualStrings(expected, core.panes[0].?.cwd.owned); } + +test "detached trackpad scrolling keeps fractions local and queues whole rows for replication" { + const gpa = std.testing.allocator; + const core = try pardes.Pardes.init(gpa, .{ .cols = 60, .rows = 16 }); + defer core.deinit(); + _ = try core.setTestFile("one\ntwo\nthree\nfour\nfive\nsix\nseven\neight\nnine\nten\neleven\ntwelve\nthirteen\nfourteen\nfifteen\nsixteen\nseventeen\neighteen\nnineteen\ntwenty\n"); + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + _ = try core.render(arena.allocator()); + var client: Client = .{ .link = undefined, .io = std.testing.io, .gpa = gpa, .texts = .init(gpa) }; + defer client.deinit(); + var in: Input = .{ .core = core, .remote = &client }; + const id = core.view().active; + const r = core.view().rects[id]; + var g: Gui = undefined; + g.scroll_pane = id; + g.scroll_delta = 0.375; + g.scroll_lag = 0; + g.scroll_idle = 0; + g.scroll_notch = 0; + g.scroll_edge = &.{}; + g.scroll_edge_len = 0; + g.glide = .{}; + g.scroll_col = r.x + config.GUTTER + 2; + g.scroll_row = core.bodyTop(core.panes[id].?, r) + 2; + defer gpa.free(g.scroll_edge); + const before = core.panes[id].?.scroll(); + try std.testing.expect(scrollRows(&g, &in, gpa)); + try std.testing.expectEqual(@as(usize, 1), client.pending.items.len); + // The next row is fetched early; its inverse raster offset keeps the + // exact fraction under the finger until that row is fully shown. + try std.testing.expectEqual(@as(f32, 0.375), @as(f32, @floatFromInt(client.pending.items.len)) + g.scroll_lag); + try std.testing.expectEqual(before, core.panes[id].?.scroll()); + core.update(client.pending.items[0]); + client.pending.clearRetainingCapacity(); + g.scroll_delta = 0.75; + try std.testing.expect(scrollRows(&g, &in, gpa)); + try std.testing.expectEqual(@as(usize, 1), client.pending.items.len); + try std.testing.expectEqual(before + 1, core.panes[id].?.scroll()); + try std.testing.expect(client.pending.items[0].mouse.shift); + core.update(client.pending.items[0]); + try std.testing.expectEqual(@as(f32, 1.125), @as(f32, @floatFromInt(core.panes[id].?.scroll() - before)) + g.scroll_lag); +} |
