Correct IME position calculation with window padding (#4949)

This PR addresses two IME-related issues:

1. Fix IME position calculation with window padding, fixed
https://github.com/ghostty-org/ghostty/issues/4933

- Add padding offset to IME position calculation to correctly position
the IME window
   
- This ensures IME window appears at the right position when
`window-padding-x/window-padding-y` are set

2. Clear selection when IME input starts

- Previously, when using CJK IME with text selected, the IME window
would appear near the selection
   
   - Now it clears selection and shows IME window at cursor position
   
   - This matches the behavior of iTerm2 and Terminal.app


## Before


https://github.com/user-attachments/assets/cd5c7b55-2083-40ce-a528-9a98898a6498

## After


https://github.com/user-attachments/assets/9f2c17c6-e885-45f3-9ab1-a9c3858690ec
This commit is contained in:
Mitchell Hashimoto
2025-01-11 14:19:04 -08:00
committed by GitHub

View File

@ -1316,8 +1316,8 @@ pub fn imePoint(self: *const Surface) apprt.IMEPos {
const content_scale = self.rt_surface.getContentScale() catch .{ .x = 1, .y = 1 }; const content_scale = self.rt_surface.getContentScale() catch .{ .x = 1, .y = 1 };
const x: f64 = x: { const x: f64 = x: {
// Simple x * cell width gives the top-left corner // Simple x * cell width gives the top-left corner, then add padding offset
var x: f64 = @floatFromInt(cursor.x * self.size.cell.width); var x: f64 = @floatFromInt(cursor.x * self.size.cell.width + self.size.padding.left);
// We want the midpoint // We want the midpoint
x += @as(f64, @floatFromInt(self.size.cell.width)) / 2; x += @as(f64, @floatFromInt(self.size.cell.width)) / 2;
@ -1329,8 +1329,8 @@ pub fn imePoint(self: *const Surface) apprt.IMEPos {
}; };
const y: f64 = y: { const y: f64 = y: {
// Simple x * cell width gives the top-left corner // Simple y * cell height gives the top-left corner, then add padding offset
var y: f64 = @floatFromInt(cursor.y * self.size.cell.height); var y: f64 = @floatFromInt(cursor.y * self.size.cell.height + self.size.padding.top);
// We want the bottom // We want the bottom
y += @floatFromInt(self.size.cell.height); y += @floatFromInt(self.size.cell.height);
@ -1591,6 +1591,15 @@ pub fn preeditCallback(self: *Surface, preedit_: ?[]const u8) !void {
self.renderer_state.mutex.lock(); self.renderer_state.mutex.lock();
defer self.renderer_state.mutex.unlock(); defer self.renderer_state.mutex.unlock();
// We clear our selection when ANY OF:
// 1. We have an existing preedit
// 2. We have preedit text
if (self.renderer_state.preedit != null or
preedit_ != null)
{
self.setSelection(null) catch {};
}
// We always clear our prior preedit // We always clear our prior preedit
if (self.renderer_state.preedit) |p| { if (self.renderer_state.preedit) |p| {
self.alloc.free(p.codepoints); self.alloc.free(p.codepoints);