mirror of
https://github.com/ghostty-org/ghostty.git
synced 2026-06-04 02:44:50 +00:00
macos: return valid selection range
This commit is contained in:
@@ -849,6 +849,38 @@ pub fn selectionString(self: *Surface, alloc: Allocator) !?[]const u8 {
|
||||
});
|
||||
}
|
||||
|
||||
/// This returns the selection range offset from the beginning of the
|
||||
/// viewport. If the selection is not entirely within the viewport then
|
||||
/// this will return null.
|
||||
///
|
||||
/// This is a oddly specific function that is used with macOS to enable
|
||||
/// NSTextInputClient to work properly for features such as the IME Emoji
|
||||
/// keyboard and QuickLook amongst other things.
|
||||
pub fn selectionRange(self: *Surface) ?struct {
|
||||
start: u32,
|
||||
len: u32,
|
||||
} {
|
||||
self.renderer_state.mutex.lock();
|
||||
defer self.renderer_state.mutex.unlock();
|
||||
|
||||
// Get the TL/BR pins for the selection
|
||||
const sel = self.io.terminal.screen.selection orelse return null;
|
||||
const tl = sel.topLeft(&self.io.terminal.screen);
|
||||
const br = sel.bottomRight(&self.io.terminal.screen);
|
||||
|
||||
// Convert the pins to coordinates (x,y)
|
||||
const tl_pt = self.io.terminal.screen.pages.pointFromPin(.viewport, tl) orelse return null;
|
||||
const br_pt = self.io.terminal.screen.pages.pointFromPin(.viewport, br) orelse return null;
|
||||
const tl_coord = tl_pt.coord();
|
||||
const br_coord = br_pt.coord();
|
||||
|
||||
// Utilize viewport sizing to convert to offsets
|
||||
const start = tl_coord.y * self.io.terminal.screen.pages.cols + tl_coord.x;
|
||||
const end = br_coord.y * self.io.terminal.screen.pages.cols + br_coord.x;
|
||||
|
||||
return .{ .start = start, .len = end - start };
|
||||
}
|
||||
|
||||
/// Returns the pwd of the terminal, if any. This is always copied because
|
||||
/// the pwd can change at any point from termio. If we are calling from the IO
|
||||
/// thread you should just check the terminal directly.
|
||||
|
||||
@@ -1803,6 +1803,26 @@ pub const CAPI = struct {
|
||||
return copy;
|
||||
}
|
||||
|
||||
/// This returns the start and length of the current selection range
|
||||
/// in viewport coordinates. If the selection is not visible in the
|
||||
/// viewport completely then this will return 0,0. This rather odd
|
||||
/// detail is due to the current usage of this in the macOS app where
|
||||
/// selections are only meaningful if they're in the viewport. We can
|
||||
/// change this behavior if we have something useful to do with the
|
||||
/// selection range outside of the viewport in the future.
|
||||
export fn ghostty_surface_selection_range(
|
||||
ptr: *Surface,
|
||||
start: *u32,
|
||||
len: *u32,
|
||||
) void {
|
||||
start.* = 0;
|
||||
len.* = 0;
|
||||
|
||||
const range = ptr.core_surface.selectionRange() orelse return;
|
||||
start.* = range.start;
|
||||
len.* = range.len;
|
||||
}
|
||||
|
||||
export fn ghostty_inspector_metal_init(ptr: *Inspector, device: objc.c.id) bool {
|
||||
return ptr.initMetal(objc.Object.fromId(device));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user