╭ input.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
1 const xit = @import("xit"); 2 const xitui = xit.xitui; 3 const Key = xitui.input.Key; 4 const Mouse = xitui.input.Mouse; 5 const Focus = xitui.focus.Focus; 6 7 /// the vertical direction of a navigation key press, including mouse scroll 8 pub const Direction = enum { up, down, none }; 9 10 pub fn vertDirection(key: Key) Direction { 11 return switch (key) { 12 .arrow_up, .scroll_up => .up, 13 .arrow_down, .scroll_down => .down, 14 else => .none, 15 }; 16 } 17 18 /// how many rows a navigation key press moves the selection (negative is up), 19 /// or null if the key doesn't move the selection. `count` is the row count, 20 /// so home/end jump past either end and get clamped by the caller. 21 pub fn rowDelta(key: Key, count: isize) ?isize { 22 switch (key) { 23 .arrow_up => return -1, 24 .arrow_down => return 1, 25 .page_up => return -10, 26 .page_down => return 10, 27 .home => return -count, 28 .end => return count, 29 .scroll_up => return -1, 30 .scroll_down => return 1, 31 else => {}, 32 } 33 return null; 34 } 35 36 /// whether `mouse` is a left press inside the rect of `focus_id`'s focus entry 37 pub fn leftClickOn(root_focus: *Focus, focus_id: usize, mouse: Mouse) bool { 38 if (mouse.button != .left) return false; 39 const entry = root_focus.children.get(focus_id) orelse return false; 40 const r = entry.rect; 41 return mouse.x >= r.x and mouse.y >= r.y and 42 mouse.x < r.x + r.size.width and mouse.y < r.y + r.size.height; 43 } 44 45 /// the tab index after a left/right arrow press, clamped to the ends, 46 /// or null when the key doesn't move the selection 47 pub fn moveTab(key: Key, current_tab: usize, tab_count: usize) ?usize { 48 var new_tab = current_tab; 49 switch (key) { 50 .arrow_left => new_tab -|= 1, 51 .arrow_right => if (new_tab + 1 < tab_count) { 52 new_tab += 1; 53 }, 54 else => {}, 55 } 56 return if (new_tab != current_tab) new_tab else null; 57 } 58 59 /// enter counts only on the focused button; a click counts anywhere on it 60 pub fn activated(root_focus: *Focus, button_id: usize, key: Key) bool { 61 return switch (key) { 62 .enter => root_focus.grandchild_id == button_id, 63 .mouse => |mouse| leftClickOn(root_focus, button_id, mouse), 64 else => false, 65 }; 66 } 67