mirror of
https://github.com/storytold/egui.git
synced 2026-11-29 17:54:17 +01:00
Merge craft/ime-delete-bytes
This commit is contained in:
commit
384c0942de
5 files changed
+220
-2
No files matched your search
+2
-2
@@ -4784,7 +4784,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd"
|
||||
dependencies = [
|
||||
"fastrand",
|
||||
"getrandom 0.3.4",
|
||||
"getrandom 0.4.3",
|
||||
"once_cell",
|
||||
"rustix 1.1.4",
|
||||
"windows-sys 0.61.2",
|
||||
@@ -6030,7 +6030,7 @@ checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
|
||||
[[package]]
|
||||
name = "winit"
|
||||
version = "0.30.13"
|
||||
source = "git+https://github.com/aiongg/winit?branch=craft%2Fsuite#5f3f0c68998edd3d069fc5bb009e14380edb90c1"
|
||||
source = "git+https://github.com/aiongg/winit?branch=craft%2Fsuite#04f273e0c4143911e5960a797113bdcf147db74d"
|
||||
dependencies = [
|
||||
"ahash",
|
||||
"android-activity",
|
||||
|
||||
@@ -438,6 +438,16 @@ impl State {
|
||||
}
|
||||
|
||||
WindowEvent::Ime(ime) => {
|
||||
if matches!(ime, winit::event::Ime::Commit(_))
|
||||
&& let Some((before_bytes, after_bytes)) = ime_delete_surrounding(window)
|
||||
{
|
||||
self.egui_input.events.push(egui::Event::Ime(
|
||||
egui::ImeEvent::DeleteSurroundingBytes {
|
||||
before_bytes,
|
||||
after_bytes,
|
||||
},
|
||||
));
|
||||
}
|
||||
self.on_ime(ime);
|
||||
|
||||
EventResponse {
|
||||
@@ -1632,6 +1642,30 @@ fn latin_letter(
|
||||
)
|
||||
}
|
||||
|
||||
/// The text around the cursor an input method deletes before the [`winit::event::Ime::Commit`]
|
||||
/// being handled, in bytes before and after the selection or pre-edit text.
|
||||
fn ime_delete_surrounding(window: &Window) -> Option<(usize, usize)> {
|
||||
#[cfg(target_os = "macos")]
|
||||
{
|
||||
use winit::platform::macos::WindowExtMacOS as _;
|
||||
window
|
||||
.take_ime_delete_surrounding()
|
||||
.map(|delete| (delete.before_bytes, delete.after_bytes))
|
||||
}
|
||||
#[cfg(all(feature = "wayland", target_os = "linux"))]
|
||||
{
|
||||
use winit::platform::wayland::WindowExtWayland as _;
|
||||
window
|
||||
.take_ime_delete_surrounding()
|
||||
.map(|delete| (delete.before_bytes, delete.after_bytes))
|
||||
}
|
||||
#[cfg(not(any(target_os = "macos", all(feature = "wayland", target_os = "linux"))))]
|
||||
{
|
||||
let _ = window;
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// With ⌘ held, letters come from the layout's ⌘ table, which differs from what the layout
|
||||
/// types on non-Latin layouts and on layouts like "Dvorak – QWERTY ⌘".
|
||||
#[cfg(target_os = "macos")]
|
||||
|
||||
@@ -31,6 +31,20 @@ pub enum ImeEvent {
|
||||
after_chars: usize,
|
||||
},
|
||||
|
||||
/// Notifies when the text surrounding the cursor should be deleted, counted in bytes of
|
||||
/// UTF-8 text, as winit reports it.
|
||||
///
|
||||
/// `before_bytes` ends where the selection or pre-edit text starts, and `after_bytes` starts
|
||||
/// where it ends. Integrations send it right before the [`Self::Commit`] it goes with, which
|
||||
/// may be empty.
|
||||
///
|
||||
/// The range is deleted exactly, not extended to whole grapheme clusters: an input method
|
||||
/// can replace the combining accent of a letter, or a letter typed after an emoji sequence.
|
||||
DeleteSurroundingBytes {
|
||||
before_bytes: usize,
|
||||
after_bytes: usize,
|
||||
},
|
||||
|
||||
/// Notifies when the IME was disabled.
|
||||
#[deprecated = "No longer used by egui"]
|
||||
Disabled,
|
||||
|
||||
@@ -1504,6 +1504,20 @@ fn events(
|
||||
} => Some(CursorMutation::ImeCompositionCursorRange(
|
||||
text.delete_surrounding_chars(cursor_range, *before_chars, *after_chars),
|
||||
)),
|
||||
ImeEvent::DeleteSurroundingBytes {
|
||||
before_bytes,
|
||||
after_bytes,
|
||||
} => {
|
||||
let [before_chars, after_chars] = surrounding_bytes_to_chars(
|
||||
text.as_str(),
|
||||
&cursor_range,
|
||||
*before_bytes,
|
||||
*after_bytes,
|
||||
);
|
||||
Some(CursorMutation::ImeCompositionCursorRange(
|
||||
text.delete_surrounding_chars(cursor_range, before_chars, after_chars),
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1546,6 +1560,33 @@ fn events(
|
||||
(any_change, cursor_range)
|
||||
}
|
||||
|
||||
/// The characters an input method deletes: those within `before_bytes` before the start of
|
||||
/// `cursor_range` and within `after_bytes` after its end. A count that ends inside a character
|
||||
/// leaves that character.
|
||||
fn surrounding_bytes_to_chars(
|
||||
text: &str,
|
||||
cursor_range: &CCursorRange,
|
||||
before_bytes: usize,
|
||||
after_bytes: usize,
|
||||
) -> [usize; 2] {
|
||||
use text_selection::text_cursor_state::byte_index_from_char_index;
|
||||
|
||||
let [min, max] = cursor_range.sorted_cursors();
|
||||
let min = byte_index_from_char_index(text, min.index).0;
|
||||
let max = byte_index_from_char_index(text, max.index).0;
|
||||
let from = min.saturating_sub(before_bytes);
|
||||
let before = text[..min]
|
||||
.char_indices()
|
||||
.rev()
|
||||
.take_while(|(index, _)| *index >= from)
|
||||
.count();
|
||||
let after = text[max..]
|
||||
.char_indices()
|
||||
.take_while(|(index, c)| index + c.len_utf8() <= after_bytes)
|
||||
.count();
|
||||
[before, after]
|
||||
}
|
||||
|
||||
/// Replace the selection with `text_to_insert`, the way a paste does: single-line fields turn
|
||||
/// newlines into spaces, and `char_limit` is respected. Returns the cursor after the insertion.
|
||||
fn insert_replacing_selection(
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
use egui::{Context, Event, Id, ImeEvent, Key, Modifiers, RawInput, Rect, TextEdit, pos2, vec2};
|
||||
|
||||
/// Text before a replaced range: a letter with a combining accent, a precomposed letter, a flag
|
||||
/// and a ZWJ family emoji. Deleting by grapheme cluster or by Backspace would cut into it.
|
||||
const PREFIX: &str = "e\u{301}é🇯🇵👨\u{200d}👩\u{200d}👧";
|
||||
|
||||
struct Field {
|
||||
ctx: Context,
|
||||
id: Id,
|
||||
text: String,
|
||||
time: f64,
|
||||
}
|
||||
|
||||
impl Field {
|
||||
fn new(text: &str) -> Self {
|
||||
let mut field = Self {
|
||||
ctx: Context::default(),
|
||||
id: Id::unique("field"),
|
||||
text: text.to_owned(),
|
||||
time: 0.0,
|
||||
};
|
||||
// Focus, then let the undoer settle on the starting text.
|
||||
field.frame(vec![]);
|
||||
field.time += 2.0;
|
||||
field.frame(vec![]);
|
||||
field
|
||||
}
|
||||
|
||||
fn frame(&mut self, events: Vec<Event>) {
|
||||
self.time += 0.1;
|
||||
let id = self.id;
|
||||
let text = &mut self.text;
|
||||
let mut output = self.ctx.run_ui(
|
||||
RawInput {
|
||||
screen_rect: Some(Rect::from_min_size(pos2(0.0, 0.0), vec2(400.0, 300.0))),
|
||||
time: Some(self.time),
|
||||
events,
|
||||
..Default::default()
|
||||
},
|
||||
|ui| {
|
||||
ui.memory_mut(|mem| mem.request_focus(id));
|
||||
ui.add(TextEdit::singleline(text).id(id));
|
||||
},
|
||||
);
|
||||
output.textures_delta.clear();
|
||||
}
|
||||
|
||||
fn undo(&mut self) {
|
||||
self.frame(vec![Event::Key {
|
||||
key: Key::Z,
|
||||
physical_key: None,
|
||||
pressed: true,
|
||||
repeat: false,
|
||||
modifiers: Modifiers::COMMAND,
|
||||
}]);
|
||||
}
|
||||
}
|
||||
|
||||
fn delete(before_bytes: usize, after_bytes: usize) -> Event {
|
||||
Event::Ime(ImeEvent::DeleteSurroundingBytes {
|
||||
before_bytes,
|
||||
after_bytes,
|
||||
})
|
||||
}
|
||||
|
||||
fn commit(text: &str) -> Event {
|
||||
Event::Ime(ImeEvent::Commit(text.to_owned()))
|
||||
}
|
||||
|
||||
fn preedit(text: &str) -> Event {
|
||||
let end = text.chars().count();
|
||||
Event::Ime(ImeEvent::Preedit {
|
||||
text: text.to_owned(),
|
||||
active_range_chars: (end > 0).then_some(end..end),
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn press_and_hold_replaces_only_the_letter_and_undoes_in_one_step() {
|
||||
let original = format!("{PREFIX}e");
|
||||
let mut field = Field::new(&original);
|
||||
|
||||
// The accent menu marks the letter it replaces: delete it, then show it as pre-edit text.
|
||||
field.frame(vec![delete(1, 0), commit(""), preedit("e")]);
|
||||
assert_eq!(field.text, original);
|
||||
// Picking an accent commits it in place of the pre-edit text.
|
||||
field.frame(vec![preedit(""), commit("è")]);
|
||||
assert_eq!(field.text, format!("{PREFIX}è"));
|
||||
|
||||
field.undo();
|
||||
assert_eq!(field.text, original);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_deletion_with_its_commit_removes_exact_bytes() {
|
||||
// An emoji sequence replaced as a whole.
|
||||
let mut field = Field::new(PREFIX);
|
||||
field.frame(vec![
|
||||
delete("👨\u{200d}👩\u{200d}👧".len(), 0),
|
||||
commit("🙂"),
|
||||
]);
|
||||
assert_eq!(field.text, "e\u{301}é🇯🇵🙂");
|
||||
field.undo();
|
||||
assert_eq!(field.text, PREFIX);
|
||||
|
||||
// A combining accent replaced without its letter.
|
||||
let mut field = Field::new(&format!("{PREFIX}e\u{301}"));
|
||||
field.frame(vec![delete('\u{301}'.len_utf8(), 0), commit("\u{300}")]);
|
||||
assert_eq!(field.text, format!("{PREFIX}e\u{300}"));
|
||||
|
||||
// One regional indicator of a flag.
|
||||
let mut field = Field::new("a🇯🇵");
|
||||
field.frame(vec![delete('🇵'.len_utf8(), 0), commit("")]);
|
||||
assert_eq!(field.text, "a🇯");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_count_ending_inside_a_character_leaves_it() {
|
||||
let mut field = Field::new("ab👧");
|
||||
field.frame(vec![delete(3, 0), commit("")]);
|
||||
assert_eq!(field.text, "ab👧");
|
||||
field.frame(vec![delete(5, 0), commit("")]);
|
||||
assert_eq!(field.text, "a");
|
||||
|
||||
// More bytes than the text holds delete all of it.
|
||||
let mut field = Field::new("ab");
|
||||
field.frame(vec![delete(10, 0), commit("x")]);
|
||||
assert_eq!(field.text, "x");
|
||||
}
|
||||
Reference in new issue
Block a user