use std::num::NonZeroU32;
use lune_terminal::{CellPosition, PixelPosition, PixelSize};
use lune_text::WidthPolicy;
use unicode_segmentation::UnicodeSegmentation;
use winit::keyboard::{Key, NamedKey, PhysicalKey};
use crate::PixelRect;
const MAX_PREEDIT_BYTES: usize = 1024;
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub(super) struct Preedit {
pub(super) text: String,
pub(super) cursor: Option<(usize, usize)>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(super) struct PreeditCell<'a> {
pub(super) position: CellPosition,
pub(super) width: usize,
pub(super) text: &'a str,
pub(super) selected: bool,
}
impl Preedit {
pub(super) fn is_empty(&self) -> bool {
self.text.is_empty()
}
pub(super) fn text(&self) -> &str {
&self.text
}
pub(super) fn clear(&mut self) -> bool {
if self.text.is_empty() && self.cursor.is_none() {
return false;
}
self.text.clear();
self.cursor = None;
true
}
pub(super) fn set(&mut self, mut text: String, cursor: Option<(usize, usize)>) -> bool {
text.truncate(text.floor_char_boundary(MAX_PREEDIT_BYTES));
let cursor = cursor.map(|(start, end)| {
let start = clamp_char_boundary(&text, start);
let end = clamp_char_boundary(&text, end);
(start, end)
});
if self.text == text && self.cursor == cursor {
return false;
}
self.text = text;
self.cursor = cursor;
true
}
pub(super) fn cursor_cell(
&self,
origin: CellPosition,
columns: usize,
rows: usize,
) -> Option<CellPosition> {
if self.text.is_empty() || columns == 0 || rows == 0 {
return None;
}
let target_byte = self.cursor.map_or(self.text.len(), |(_, end)| end);
let mut col = origin.column.min(columns - 1);
let mut row = origin.row.min(rows - 1);
for (byte_start, _, width) in graphemes_with_widths(&self.text) {
if byte_start >= target_byte {
break;
}
if width == 0 {
continue;
}
let width = width.min(columns);
if col.saturating_add(width) > columns {
col = 0;
row = row.saturating_add(1);
}
if row >= rows {
return Some(CellPosition {
row: rows - 1,
column: columns - 1,
});
}
col = col.saturating_add(width);
if col >= columns {
col = 0;
row = row.saturating_add(1);
}
}
Some(CellPosition {
row: row.min(rows - 1),
column: col.min(columns - 1),
})
}
pub(super) fn layout_cells(
&self,
origin: CellPosition,
columns: usize,
rows: usize,
) -> Vec<PreeditCell<'_>> {
if self.text.is_empty() || columns == 0 || rows == 0 {
return Vec::new();
}
let selection = self
.cursor
.map(|(start, end)| (start.min(end), start.max(end)))
.filter(|(start, end)| start != end);
let mut cells = Vec::new();
let mut col = origin.column.min(columns - 1);
let mut row = origin.row.min(rows - 1);
for (byte_start, byte_end, width) in graphemes_with_widths(&self.text) {
if width == 0 {
continue;
}
let width = width.min(columns);
if col.saturating_add(width) > columns {
col = 0;
row = row.saturating_add(1);
}
if row >= rows {
break;
}
let selected =
selection.is_some_and(|(start, end)| byte_start < end && byte_end > start);
cells.push(PreeditCell {
position: CellPosition { row, column: col },
width,
text: &self.text[byte_start..byte_end],
selected,
});
col = col.saturating_add(width);
if col >= columns {
col = 0;
row = row.saturating_add(1);
}
}
cells
}
}
pub(super) fn display_width(text: &str) -> usize {
graphemes_with_widths(text)
.into_iter()
.map(|(_, _, width)| width)
.sum()
}
#[derive(Debug, Default)]
pub(super) struct ImeAreaCache {
last: Option<(PixelPosition, PixelSize, u64)>,
}
impl ImeAreaCache {
pub(super) fn record_if_changed(
&mut self,
origin: PixelPosition,
size: PixelSize,
scale_factor: f64,
) -> bool {
let next = (origin, size, scale_factor.to_bits());
if self.last == Some(next) {
return false;
}
self.last = Some(next);
true
}
pub(super) fn invalidate(&mut self) {
self.last = None;
}
}
pub(super) fn is_modifier_key(key: &Key) -> bool {
matches!(
key,
Key::Named(
NamedKey::Shift
| NamedKey::Control
| NamedKey::Alt
| NamedKey::AltGraph
| NamedKey::Super
| NamedKey::Meta
| NamedKey::CapsLock
| NamedKey::NumLock
| NamedKey::ScrollLock
)
)
}
#[derive(Debug)]
pub(super) struct ImeAllowGate {
applied: bool,
desired: bool,
active_repeat_key: Option<PhysicalKey>,
swallowed_keys: Vec<PhysicalKey>,
}
impl ImeAllowGate {
pub(super) fn new(initial: bool) -> Self {
Self {
applied: initial,
desired: initial,
active_repeat_key: None,
swallowed_keys: Vec::new(),
}
}
pub(super) fn on_key_press(
&mut self,
key: PhysicalKey,
is_modifier: bool,
repeat: bool,
) -> bool {
if repeat {
return self.swallowed_keys.contains(&key);
}
self.swallowed_keys.retain(|held| *held != key);
if !self.applied && !is_modifier {
self.active_repeat_key = Some(key);
}
false
}
pub(super) fn swallow_until_release(&mut self, key: PhysicalKey) {
if !self.swallowed_keys.contains(&key) {
self.swallowed_keys.push(key);
}
}
pub(super) fn on_key_release(&mut self, key: PhysicalKey) -> (bool, Option<bool>) {
let mut swallowed = false;
self.swallowed_keys.retain(|held| {
if *held == key {
swallowed = true;
false
} else {
true
}
});
if self.active_repeat_key == Some(key) {
self.active_repeat_key = None;
}
let ime_update = if !self.applied && self.desired && self.active_repeat_key.is_none() {
self.applied = true;
Some(true)
} else {
None
};
(swallowed, ime_update)
}
pub(super) fn sync(&mut self, desired: bool) -> Option<bool> {
self.desired = desired;
if !desired {
if self.applied {
self.applied = false;
return Some(false);
}
return None;
}
if !self.applied && self.active_repeat_key.is_none() {
self.applied = true;
return Some(true);
}
None
}
pub(super) fn force_disable(&mut self, active_key: Option<PhysicalKey>) -> bool {
if let Some(key) = active_key {
self.active_repeat_key = Some(key);
}
if self.applied {
self.applied = false;
return true;
}
false
}
pub(super) fn clear_keys(&mut self) {
self.active_repeat_key = None;
self.swallowed_keys.clear();
}
}
pub(super) fn clamp_to_surface(
origin: PixelPosition,
size: PixelSize,
surface_width: u32,
surface_height: u32,
) -> Option<PixelRect> {
if surface_width == 0 || surface_height == 0 {
return None;
}
let width = size.width.get().min(surface_width);
let height = size.height.get().min(surface_height);
let x = origin.x.min(surface_width - width);
let y = origin.y.min(surface_height - height);
Some(PixelRect {
origin: PixelPosition { x, y },
size: PixelSize {
width: NonZeroU32::new(width)?,
height: NonZeroU32::new(height)?,
},
})
}
fn clamp_char_boundary(text: &str, byte: usize) -> usize {
text.floor_char_boundary(byte.min(text.len()))
}
fn graphemes_with_widths(text: &str) -> Vec<(usize, usize, usize)> {
text.grapheme_indices(true)
.filter(|(_, grapheme)| !grapheme.chars().all(char::is_control))
.map(|(start, grapheme)| {
let width = WidthPolicy::Narrow.columns(grapheme).min(2);
(start, start + grapheme.len(), width)
})
.collect()
}
#[cfg(test)]
mod tests {
use winit::keyboard::KeyCode;
use super::*;
#[test]
fn cursor_cell_advances_across_ascii_and_wide_preedit_with_wrapping() {
let mut preedit = Preedit::default();
assert!(preedit.set("ä½ å¥½ab".into(), Some((8, 8))));
let caret = preedit
.cursor_cell(CellPosition { row: 0, column: 4 }, 6, 4)
.unwrap();
assert_eq!(caret, CellPosition { row: 1, column: 4 });
assert!(preedit.set("ä½ å¥½ab".into(), Some((3, 3))));
let mid = preedit
.cursor_cell(CellPosition { row: 0, column: 4 }, 6, 4)
.unwrap();
assert_eq!(mid, CellPosition { row: 1, column: 0 });
}
#[test]
fn layout_cells_marks_selected_subrange_and_respects_bounds() {
let mut preedit = Preedit::default();
assert!(preedit.set("ni hao".into(), Some((3, 6))));
let cells = preedit.layout_cells(CellPosition { row: 2, column: 1 }, 10, 4);
assert_eq!(cells.len(), 6);
assert!(!cells[0].selected);
assert!(!cells[2].selected);
assert!(cells[3].selected);
assert!(cells[5].selected);
assert_eq!(cells[5].position, CellPosition { row: 2, column: 6 });
}
#[test]
fn cache_invalidates_on_scale_change_and_clamps_to_surface() {
let mut cache = ImeAreaCache::default();
let origin = PixelPosition { x: 100, y: 200 };
let size = PixelSize {
width: NonZeroU32::new(10).unwrap(),
height: NonZeroU32::new(20).unwrap(),
};
assert!(cache.record_if_changed(origin, size, 1.0));
assert!(!cache.record_if_changed(origin, size, 1.0));
assert!(cache.record_if_changed(origin, size, 1.5));
cache.invalidate();
assert!(cache.record_if_changed(origin, size, 1.5));
let clamped = clamp_to_surface(
PixelPosition { x: 995, y: 590 },
PixelSize {
width: NonZeroU32::new(10).unwrap(),
height: NonZeroU32::new(20).unwrap(),
},
1000,
600,
)
.unwrap();
assert_eq!(clamped.origin, PixelPosition { x: 990, y: 580 });
}
#[test]
fn vi_exit_defers_ime_enable_until_key_release_and_swallows_repeats() {
let mut gate = ImeAllowGate::new(true);
assert_eq!(gate.sync(false), Some(false));
assert!(!gate.applied);
let key_j = PhysicalKey::Code(KeyCode::KeyJ);
let key_q = PhysicalKey::Code(KeyCode::KeyQ);
assert!(!gate.on_key_press(key_j, false, false));
assert!(!gate.on_key_press(key_j, false, true));
assert_eq!(gate.on_key_release(key_j), (false, None));
assert!(!gate.on_key_press(key_q, false, false));
gate.swallow_until_release(key_q);
assert_eq!(gate.sync(true), None);
assert!(!gate.applied);
assert!(gate.on_key_press(key_q, false, true));
assert_eq!(gate.on_key_release(key_q), (true, Some(true)));
assert!(gate.applied);
assert!(!gate.on_key_press(key_q, false, false));
}
#[test]
fn rollover_key_defers_ime_enable_until_latest_repeat_key_releases() {
let mut gate = ImeAllowGate::new(true);
assert_eq!(gate.sync(false), Some(false));
let key_q = PhysicalKey::Code(KeyCode::KeyQ);
let key_l = PhysicalKey::Code(KeyCode::KeyL);
assert!(!gate.on_key_press(key_q, false, false));
gate.swallow_until_release(key_q);
assert_eq!(gate.sync(true), None);
assert!(!gate.on_key_press(key_l, false, false));
assert_eq!(gate.on_key_release(key_q), (true, None));
assert!(!gate.applied);
assert_eq!(gate.on_key_release(key_l), (false, Some(true)));
assert!(gate.applied);
}
#[test]
fn ctrl_shift_space_exit_ignores_modifiers_and_enables_on_space_release() {
let mut gate = ImeAllowGate::new(true);
assert_eq!(gate.sync(false), Some(false));
let ctrl = PhysicalKey::Code(KeyCode::ControlLeft);
let shift = PhysicalKey::Code(KeyCode::ShiftLeft);
let space = PhysicalKey::Code(KeyCode::Space);
assert!(!gate.on_key_press(ctrl, true, false));
assert!(!gate.on_key_press(shift, true, false));
assert!(!gate.on_key_press(space, false, false));
gate.swallow_until_release(space);
assert_eq!(gate.sync(true), None);
assert!(!gate.applied);
assert_eq!(gate.on_key_release(space), (true, Some(true)));
assert!(gate.applied);
}
#[test]
fn focus_loss_clears_deferred_key_and_swallowed_state() {
let mut gate = ImeAllowGate::new(true);
assert_eq!(gate.sync(false), Some(false));
let key_q = PhysicalKey::Code(KeyCode::KeyQ);
assert!(!gate.on_key_press(key_q, false, false));
gate.swallow_until_release(key_q);
assert_eq!(gate.sync(true), None);
gate.clear_keys();
assert_eq!(gate.sync(false), None);
assert_eq!(gate.sync(true), Some(true));
assert!(gate.applied);
}
}