core: change mouse cursor over link

This commit is contained in:
Mitchell Hashimoto
2023-11-28 14:11:30 -08:00
parent 20cc369561
commit 172a91e95d

View File

@ -2006,11 +2006,13 @@ pub fn mouseButtonCallback(
} }
} }
/// Attempt to invoke the action of any link that is under the /// Returns the link at the given cursor position, if any.
/// given position. fn linkAtPos(
fn processLinks(self: *Surface, pos: apprt.CursorPos) !bool { self: *Surface,
pos: apprt.CursorPos,
) !?DerivedConfig.Link {
// If we have no configured links we can save a lot of work // If we have no configured links we can save a lot of work
if (self.config.links.len == 0) return false; if (self.config.links.len == 0) return null;
// Convert our cursor position to a screen point. // Convert our cursor position to a screen point.
const mouse_pt = mouse_pt: { const mouse_pt = mouse_pt: {
@ -2020,7 +2022,7 @@ fn processLinks(self: *Surface, pos: apprt.CursorPos) !bool {
// Get the line we're hovering over. // Get the line we're hovering over.
const line = self.io.terminal.screen.getLine(mouse_pt) orelse const line = self.io.terminal.screen.getLine(mouse_pt) orelse
return false; return null;
const strmap = try line.stringMap(self.alloc); const strmap = try line.stringMap(self.alloc);
defer strmap.deinit(self.alloc); defer strmap.deinit(self.alloc);
@ -2032,15 +2034,20 @@ fn processLinks(self: *Surface, pos: apprt.CursorPos) !bool {
defer match.deinit(); defer match.deinit();
const sel = match.selection(); const sel = match.selection();
if (!sel.contains(mouse_pt)) continue; if (!sel.contains(mouse_pt)) continue;
return link;
}
}
// Click! return null;
}
/// Attempt to invoke the action of any link that is under the
/// given position.
fn processLinks(self: *Surface, pos: apprt.CursorPos) !bool {
const link = try self.linkAtPos(pos) orelse return false;
log.info("link clicked action={}", .{link.action}); log.info("link clicked action={}", .{link.action});
return true; return true;
} }
}
return false;
}
pub fn cursorPosCallback( pub fn cursorPosCallback(
self: *Surface, self: *Surface,
@ -2121,6 +2128,15 @@ pub fn cursorPosCallback(
return; return;
} }
// Handle link hovering
// TODO: update render state with mouse pos
// TODO: unsure if resetting cursor logic is correct
if (try self.linkAtPos(pos)) |_| {
try self.rt_surface.setMouseShape(.pointer);
} else {
try self.rt_surface.setMouseShape(self.io.terminal.mouse_shape);
}
} }
/// Double-click dragging moves the selection one "word" at a time. /// Double-click dragging moves the selection one "word" at a time.