refactor(terminal): add click-to-focus handling for terminal

This commit is contained in:
CJ van den Berg 2026-02-25 10:56:45 +01:00
parent 558c59368b
commit 7d51b09aac
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
2 changed files with 32 additions and 2 deletions

View file

@ -33,6 +33,7 @@ focused: bool = false,
cwd: std.ArrayListUnmanaged(u8) = .empty,
title: std.ArrayListUnmanaged(u8) = .empty,
input_mode: Mode,
hover: bool = false,
pub fn create(allocator: Allocator, parent: Plane) !Widget {
return create_with_args(allocator, parent, .{});
@ -129,7 +130,20 @@ pub fn receive(self: *Self, from: tp.pid_ref, m: tp.message) error{Exit}!bool {
if (try m.match(.{ "terminal_view", "output" })) {
tui.need_render(@src());
return true;
} else if (!(try m.match(.{ "I", tp.more })
} else if (try m.match(.{ "H", tp.extract(&self.hover) })) {
tui.rdr().request_mouse_cursor_default(self.hover);
tui.need_render(@src());
return true;
}
if (try m.match(.{ "B", input.event.press, @intFromEnum(input.mouse.BUTTON1), tp.more }) or
try m.match(.{ "B", input.event.press, @intFromEnum(input.mouse.BUTTON2), tp.more }) or
try m.match(.{ "B", input.event.press, @intFromEnum(input.mouse.BUTTON3), tp.more }))
switch (tui.set_focus_by_mouse_event()) {
.changed => return true,
.same, .notfound => {},
};
if (!(try m.match(.{ "I", tp.more })
// or
// try m.match(.{ "B", tp.more }) or
// try m.match(.{ "D", tp.more }) or

View file

@ -833,13 +833,23 @@ pub const FocusAction = enum { same, changed, notfound };
pub fn set_focus_by_widget(w: Widget) FocusAction {
const mv = mainview() orelse return .notfound;
clear_keyboard_focus();
return mv.focus_view_by_widget(w);
}
pub fn set_focus_by_mouse_event() FocusAction {
const self = current();
const mv = mainview() orelse return .notfound;
return mv.focus_view_by_widget(self.hover_focus orelse return .notfound);
const hover_focus = self.hover_focus orelse return .notfound;
const keyboard_focus = if (self.keyboard_focus) |prev| prev.ptr else null;
if (hover_focus.ptr == keyboard_focus) return .same;
clear_keyboard_focus();
switch (mv.focus_view_by_widget(hover_focus)) {
.notfound => {},
else => |action| return action,
}
hover_focus.focus();
return .changed;
}
pub fn set_keyboard_focus(w: Widget) void {
@ -855,6 +865,12 @@ pub fn release_keyboard_focus(w: Widget) void {
};
}
pub fn clear_keyboard_focus() void {
const self = current();
if (self.keyboard_focus) |prev| prev.unfocus();
self.keyboard_focus = null;
}
fn send_widgets(self: *Self, from: tp.pid_ref, m: tp.message) error{Exit}!bool {
const frame = tracy.initZone(@src(), .{ .name = "tui widgets" });
defer frame.deinit();