╭ Diff.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const evt = @import("../../event.zig"); 3 const ui = @import("../../ui.zig"); 4 const xit = @import("xit"); 5 const rp = xit.repo; 6 const df = xit.diff; 7 const wgt = xit.xitui.widget; 8 const layout = xit.xitui.layout; 9 const Key = xit.xitui.input.Key; 10 const Grid = xit.xitui.grid.Grid; 11 const Focus = xit.xitui.focus.Focus; 12 13 pub const page_size = 10; 14 pub const Hunk = struct { 15 path: ?[]const u8 = null, 16 text: []const u8, 17 // one byte per line of text: the edit's +/-/space prefix 18 prefixes: []const u8, 19 }; 20 pub const Window = struct { 21 hunks: []const Hunk = &.{}, 22 start: usize = 0, 23 has_more: bool = false, 24 }; 25 26 route: Route, 27 path: []const u8 = "", 28 window: Window, 29 30 const Self = @This(); 31 32 pub const Route = union(enum) { 33 repo: struct { identity: []const u8, ref_or_oid: ui.RoutablePage.RefOrOid, value: []const u8, base_oid: []const u8 }, 34 patchrev: struct { identity: []const u8, id: []const u8 }, 35 // `oid` names one commit's diff against `base_oid` ("" = the whole patch) 36 fork: struct { identity: []const u8, id: []const u8, oid: []const u8 = "", base_oid: []const u8 = "" }, 37 38 pub fn page(self: Route, start: usize, path: []const u8) ?ui.RoutablePage { 39 return switch (self) { 40 .repo => |r| ui.RoutablePage.repoDiffRoute(r.identity, r.ref_or_oid, r.value, start, path, r.base_oid), 41 .patchrev => |r| ui.RoutablePage.repoPatchRevDiffRoute(r.identity, r.id, start, path), 42 .fork => |f| ui.RoutablePage.forkDiffRoute(f.identity, f.id, f.oid, f.base_oid, start, path), 43 }; 44 } 45 46 fn link(self: Route, arena: *std.heap.ArenaAllocator, start: usize, path: []const u8) ![]const u8 { 47 const route = self.page(start, path) orelse return error.RouteTooLong; 48 return std.fmt.allocPrint(arena.allocator(), "a:{s}", .{try route.toUrl(arena)}); 49 } 50 }; 51 52 pub fn init( 53 comptime repo_kind: rp.RepoKind, 54 comptime repo_opts: rp.RepoOpts(repo_kind), 55 arena: *std.heap.ArenaAllocator, 56 repo: *rp.Repo(repo_kind, repo_opts), 57 io: std.Io, 58 gpa: std.mem.Allocator, 59 route: ui.RoutablePage.RepoDiffRoute, 60 ) !Self { 61 const aa = arena.allocator(); 62 const oid_len = comptime xit.hash.hexLen(repo_opts.hash); 63 const identity = try aa.dupe(u8, route.name.slice()); 64 const base_oid, const head_oid, const diff_route = if (route.patchrev_id.len != 0) blk: { 65 const id = evt.parseEventId(route.patchrev_id.slice()) catch return error.NotFound; 66 const record = (try evt.readFromRepo(evt.PatchRev, repo_kind, repo_opts, io, gpa, arena, repo, &id)) orelse return error.NotFound; 67 if (record.removed) return error.NotFound; 68 if (record.base_tree_oid.len != oid_len or record.head_tree_oid.len != oid_len) return error.NotFound; 69 break :blk .{ 70 record.base_tree_oid[0..oid_len].*, 71 record.head_tree_oid[0..oid_len].*, 72 Route{ .patchrev = .{ .identity = identity, .id = try aa.dupe(u8, route.patchrev_id.slice()) } }, 73 }; 74 } else blk: { 75 const resolved = (try ui.ResolvedRefOrOid(repo_kind, repo_opts).init(repo, io, aa, route.ref_or_oid, route.value.slice())) orelse return error.NotFound; 76 evt.PatchRev.validateOid(repo_opts.hash, &resolved.oid) catch return error.NotFound; 77 evt.PatchRev.validateOid(repo_opts.hash, route.base_oid.slice()) catch return error.NotFound; 78 79 var moment = try repo.core.latestMoment(); 80 const state = rp.Repo(repo_kind, repo_opts).State(.read_only){ .core = &repo.core, .extra = .{ .moment = &moment } }; 81 // peel annotated tags before comparing their trees 82 var tip = try xit.object.Object(repo_kind, repo_opts).initCommit(state, io, gpa, &resolved.oid); 83 defer tip.deinit(); 84 break :blk .{ 85 route.base_oid.slice()[0..oid_len].*, 86 tip.oid, 87 Route{ .repo = .{ 88 .identity = identity, 89 .ref_or_oid = resolved.ref_or_oid, 90 .value = resolved.value, 91 .base_oid = try aa.dupe(u8, route.base_oid.slice()), 92 } }, 93 }; 94 }; 95 const path = try aa.dupe(u8, route.path.slice()); 96 return .{ 97 .route = diff_route, 98 .path = path, 99 .window = try render(repo_kind, repo_opts, io, gpa, aa, repo, if (std.mem.allEqual(u8, &base_oid, '0')) null else &base_oid, head_oid, route.start, path), 100 }; 101 } 102 103 // render a window of the net diff between two commits or trees 104 pub fn render( 105 comptime repo_kind: rp.RepoKind, 106 comptime repo_opts: rp.RepoOpts(repo_kind), 107 io: std.Io, 108 gpa: std.mem.Allocator, 109 arena: std.mem.Allocator, 110 repo: *rp.Repo(repo_kind, repo_opts), 111 base_oid: ?*const [xit.hash.hexLen(repo_opts.hash)]u8, 112 head_oid: [xit.hash.hexLen(repo_opts.hash)]u8, 113 start: usize, 114 path: []const u8, 115 ) !Window { 116 var tree_diff = try repo.treeDiff(io, gpa, base_oid, &head_oid); 117 defer tree_diff.deinit(); 118 119 var file_iter = try repo.filePairs(io, gpa, .{ .tree = .{ .tree_diff = &tree_diff } }); 120 121 var hunks: std.ArrayList(Hunk) = .empty; 122 var index: usize = 0; // running hunk index across all files 123 var has_more = false; 124 125 file_loop: while (try file_iter.next()) |pair_val| { 126 var pair = pair_val; 127 defer pair.deinit(); 128 129 if (path.len != 0 and !std.mem.eql(u8, pair.path, path)) continue; 130 131 var hunk_iter = try df.HunkIterator(repo_kind, repo_opts).init(gpa, &pair.a, &pair.b); 132 defer hunk_iter.deinit(gpa); 133 134 // the path label rides on the first of this file's hunks we actually show. 135 var path_attached = false; 136 while (try hunk_iter.next(gpa)) |hunk_val| { 137 var hunk = hunk_val; 138 defer hunk.deinit(gpa); 139 140 const i = index; 141 index += 1; 142 if (i < start) continue; // before this window 143 if (hunks.items.len >= page_size) { 144 has_more = true; 145 break :file_loop; 146 } 147 const rendered = try renderHunk(repo_kind, repo_opts, arena, &hunk_iter, &hunk); 148 try hunks.append(arena, .{ 149 .path = if (path_attached) null else try arena.dupe(u8, pair.path), 150 .text = rendered.text, 151 .prefixes = rendered.prefixes, 152 }); 153 path_attached = true; 154 } 155 } 156 157 return .{ .hunks = try hunks.toOwnedSlice(arena), .start = start, .has_more = has_more }; 158 } 159 160 // render edits with right-aligned line numbers, alongside each line's prefix 161 fn renderHunk( 162 comptime repo_kind: rp.RepoKind, 163 comptime repo_opts: rp.RepoOpts(repo_kind), 164 arena: std.mem.Allocator, 165 hunk_iter: *df.HunkIterator(repo_kind, repo_opts), 166 hunk: *df.Hunk(repo_kind, repo_opts), 167 ) !struct { text: []const u8, prefixes: []const u8 } { 168 var out: std.Io.Writer.Allocating = .init(arena); 169 defer out.deinit(); 170 var prefixes: std.ArrayList(u8) = .empty; 171 var max_num: usize = 1; 172 for (hunk.edits.items) |edit| { 173 const n = editLineNum(edit) + 1; 174 if (n > max_num) max_num = n; 175 } 176 const width = std.fmt.count("{d}", .{max_num}); 177 for (hunk.edits.items, 0..) |edit, i| { 178 const text = switch (edit) { 179 .eql => |e| try hunk_iter.line_iter_b.get(e.new_line.num), 180 .ins => |e| try hunk_iter.line_iter_b.get(e.new_line.num), 181 .del => |e| try hunk_iter.line_iter_a.get(e.old_line.num), 182 }; 183 const prefix: u8 = switch (edit) { 184 .eql => ' ', 185 .ins => '+', 186 .del => '-', 187 }; 188 if (i != 0) try out.writer.writeByte('\n'); 189 const num = editLineNum(edit) + 1; 190 try out.writer.splatByteAll(' ', width - std.fmt.count("{d}", .{num})); 191 try out.writer.print("{d} {c}{s}", .{ num, prefix, text }); 192 try prefixes.append(arena, prefix); 193 } 194 return .{ .text = try out.toOwnedSlice(), .prefixes = try prefixes.toOwnedSlice(arena) }; 195 } 196 197 // the line number to show for an edit: the new-side number for kept and inserted 198 // lines, the old-side number for deletions (0-based in the diff machinery). 199 fn editLineNum(edit: df.Edit) usize { 200 return switch (edit) { 201 .eql => |e| e.new_line.num, 202 .ins => |e| e.new_line.num, 203 .del => |e| e.old_line.num, 204 }; 205 } 206 207 fn appendWindow(data: @This(), allocator: std.mem.Allocator, session: *ui.Session, box: *wgt.Box(ui.Widget)) !void { 208 if (data.path.len != 0) { 209 try addLink(allocator, box, data.path, ""); 210 try addLink(allocator, box, "← all files", try data.route.link(session.page_arena, 0, "")); 211 } 212 if (data.window.start > 0) try addLink(allocator, box, "← previous", try data.route.link(session.page_arena, data.window.start -| page_size, data.path)); 213 for (data.window.hunks) |hunk| { 214 if (data.path.len == 0) if (hunk.path) |path| try addLink(allocator, box, path, try data.route.link(session.page_arena, 0, path)); 215 if (hunk.text.len != 0) try addHunk(allocator, box, hunk); 216 } 217 if (data.window.has_more) try addLink(allocator, box, "next →", try data.route.link(session.page_arena, data.window.start + page_size, data.path)); 218 } 219 220 fn addLink(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), text: []const u8, link: []const u8) !void { 221 try addSpans(allocator, box, &.{.{ .text = text }}, link); 222 } 223 224 // one span per line, inserted lines green and deleted lines red. the text 225 // box copies the text, so the spans only live until init. 226 fn addHunk(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), hunk: Hunk) !void { 227 var spans: std.ArrayList(wgt.Span) = .empty; 228 defer spans.deinit(allocator); 229 var start: usize = 0; 230 for (hunk.prefixes) |prefix| { 231 // each span keeps its trailing newline so the text box still breaks there 232 const end = if (std.mem.indexOfScalarPos(u8, hunk.text, start, '\n')) |nl| nl + 1 else hunk.text.len; 233 const style: wgt.Style = switch (prefix) { 234 '+' => .{ .fg = .{ .ansi = .green } }, 235 '-' => .{ .fg = .{ .ansi = .red } }, 236 else => .{}, 237 }; 238 try spans.append(allocator, .{ .text = hunk.text[start..end], .style = style }); 239 start = end; 240 } 241 try addSpans(allocator, box, spans.items, ""); 242 } 243 244 fn addSpans(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), spans: []const wgt.Span, link: []const u8) !void { 245 var tb = try wgt.TextBox.initSpans(allocator, spans, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 246 errdefer tb.deinit(allocator); 247 tb.getFocus().mode = .all; 248 if (link.len != 0) tb.getFocus().kind = .{ .custom = link }; 249 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null, .flex = .shrink }); 250 } 251 252 pub const View = struct { 253 scroll: wgt.Scroll(ui.Widget), 254 session: *ui.Session, 255 256 pub fn initEmpty(allocator: std.mem.Allocator, session: *ui.Session) !View { 257 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 258 errdefer box.deinit(allocator); 259 return .{ 260 .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = box }, .{ .direction = .both, .web_native = !session.is_terminal, .fill = true }), 261 .session = session, 262 }; 263 } 264 265 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 266 var view = try initEmpty(allocator, session); 267 errdefer view.deinit(allocator); 268 const box = view.inner(); 269 try data.appendWindow(allocator, session, box); 270 if (box.children.count() == 0) try addLink(allocator, box, "no changes", ""); 271 box.getFocus().child_id = box.children.keys()[0]; 272 return view; 273 } 274 275 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 276 self.scroll.deinit(allocator); 277 } 278 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 279 self.clearGrid(); 280 try self.scroll.build(allocator, constraint, root_focus); 281 } 282 pub fn clearGrid(self: *View) void { 283 self.scroll.clearGrid(); 284 } 285 pub fn getGrid(self: View) ?Grid { 286 return self.scroll.getGrid(); 287 } 288 pub fn getFocus(self: *View) *Focus { 289 return self.scroll.getFocus(); 290 } 291 pub fn atTop(self: *View) bool { 292 const box = self.inner(); 293 return self.scroll.y == 0 and (box.children.count() == 0 or box.getFocus().child_id == box.children.keys()[0]); 294 } 295 fn inner(self: *View) *wgt.Box(ui.Widget) { 296 return &self.scroll.child.box; 297 } 298 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 299 _ = allocator; 300 const sc = &self.scroll; 301 switch (key) { 302 // scroll horizontally within the diff 303 .arrow_left => if (!self.moveInRow(root_focus, false) and sc.x > 0) { 304 sc.x -= 1; 305 self.scroll.clampToContent(); 306 }, 307 .arrow_right => if (!self.moveInRow(root_focus, true)) { 308 sc.x += 1; 309 self.scroll.clampToContent(); 310 }, 311 .arrow_up => self.moveDiff(root_focus, -1), 312 .arrow_down => self.moveDiff(root_focus, 1), 313 .page_up => self.pageDiff(root_focus, -10), 314 .page_down => self.pageDiff(root_focus, 10), 315 .home => self.jumpDiff(root_focus, false), 316 .end => self.jumpDiff(root_focus, true), 317 // Enter / a click on a "previous"/"next" row follow its "a:" link in 318 // the host (it reloads the adjacent window), so they don't reach here. 319 .mouse => |mouse| switch (mouse.action) { 320 .scroll => |dir| self.moveDiff(root_focus, if (dir == .up) -1 else 1), 321 else => {}, 322 }, 323 else => {}, 324 } 325 } 326 327 // step across the selected row's boxes, false when the row has none that way. 328 pub fn moveInRow(self: *View, root_focus: *Focus, right: bool) bool { 329 const box = self.inner(); 330 const selected = box.getFocus().child_id orelse return false; 331 return switch ((box.children.getPtr(selected) orelse return false).widget) { 332 .box => |*row| ui.widget.moveInRow(row, root_focus, right), 333 else => false, 334 }; 335 } 336 337 // move one step in `delta` (+ down, - up). in the terminal, `delta` is a 338 // line count unless there is a new hunk visible (in which case it is a 339 // hunk count). on the web `delta` is always a hunk count. 340 fn moveDiff(self: *View, root_focus: *Focus, delta: isize) void { 341 const box = self.inner(); 342 const keys = box.children.keys(); 343 if (keys.len == 0) return; 344 const cur: usize = if (box.getFocus().child_id) |c| (box.children.getIndex(c) orelse 0) else 0; 345 const target = @as(isize, @intCast(cur)) + delta; 346 const in_range = target >= 0 and target < @as(isize, @intCast(keys.len)); 347 348 if (self.session.is_terminal) { 349 if (in_range and self.hunkVisible(@intCast(target))) { 350 root_focus.setFocus(keys[@intCast(target)]); 351 return; 352 } 353 354 const sc = &self.scroll; 355 sc.y += delta * 5; // magnify because it's a line count 356 self.scroll.clampToContent(); 357 if (in_range and self.hunkVisible(@intCast(target))) { 358 root_focus.setFocus(keys[@intCast(target)]); 359 } 360 } else { 361 if (in_range) { 362 root_focus.setFocus(keys[@intCast(target)]); 363 } 364 } 365 } 366 367 // page a fixed number of lines, then focus the leading visible hunk 368 // (bottom-most when paging down, top-most when paging up). in the 369 // terminal, `delta` is a line count, and on the web it's a hunk count. 370 fn pageDiff(self: *View, root_focus: *Focus, delta: isize) void { 371 if (self.session.is_terminal) { 372 const sc = &self.scroll; 373 sc.y += delta * 5; // magnify because it's a line count 374 self.scroll.clampToContent(); 375 self.focusVisible(root_focus, delta > 0); 376 } else { 377 const box = self.inner(); 378 const keys = box.children.keys(); 379 if (keys.len == 0) return; 380 const cur: isize = if (box.getFocus().child_id) |c| @intCast(box.children.getIndex(c) orelse 0) else 0; 381 const target: usize = @intCast(std.math.clamp(cur + delta, 0, @as(isize, @intCast(keys.len - 1)))); 382 root_focus.setFocus(keys[target]); 383 } 384 } 385 386 // jump to the first or last hunk. on the web the browser scrolls to the 387 // focused hunk; on the terminal pin the scroll to the top/bottom too. 388 fn jumpDiff(self: *View, root_focus: *Focus, to_end: bool) void { 389 const box = self.inner(); 390 const keys = box.children.keys(); 391 if (keys.len == 0) return; 392 if (self.session.is_terminal) { 393 const sc = &self.scroll; 394 sc.y = if (to_end) std.math.maxInt(isize) else 0; 395 self.scroll.clampToContent(); 396 } 397 root_focus.setFocus(if (to_end) keys[keys.len - 1] else keys[0]); 398 } 399 400 // whether hunk `index` is at least partly within the diff viewport, per the 401 // last build's layout rects (content space) and the current scroll offset. 402 fn hunkVisible(self: *View, index: usize) bool { 403 const box = self.inner(); 404 const sc = &self.scroll; 405 const vp = sc.grid orelse return false; 406 const r = box.children.values()[index].rect orelse return false; 407 const top = sc.y; 408 const bottom = sc.y + @as(isize, @intCast(vp.size.height - sc.bar_h)); 409 return (r.y + @as(isize, @intCast(r.size.height))) > top and r.y < bottom; 410 } 411 412 // focus the visible hunk at the scroll's leading edge. `prefer_last` picks 413 // the bottom-most visible (for downward motion), else the top-most. 414 fn focusVisible(self: *View, root_focus: *Focus, prefer_last: bool) void { 415 const box = self.inner(); 416 const sc = &self.scroll; 417 const vp = sc.grid orelse return; 418 const top = sc.y; 419 const bottom = sc.y + @as(isize, @intCast(vp.size.height - sc.bar_h)); 420 var chosen: ?usize = null; 421 for (box.children.keys(), box.children.values()) |id, *child| { 422 const r = child.rect orelse continue; // content-space layout rect 423 const r_top = r.y; 424 const r_bot = r.y + @as(isize, @intCast(r.size.height)); 425 if (r_bot <= top or r_top >= bottom) continue; // not visible 426 chosen = id; 427 if (!prefer_last) break; // first visible 428 } 429 if (chosen) |id| root_focus.setFocus(id); 430 } 431 }; 432