Split Event::Text into Text and Paste (#1058)

* Split `Event::Text` into `Text` and `Paste`

* Added explicit Event::Paste change

See #1043

* Link to PR in changelog (not the issue)

Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
This commit is contained in:
Simon Werner 2022-01-11 11:12:30 +13:00 committed by GitHub
parent 650057dd4a
commit 225d2b506d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 15 additions and 3 deletions

View file

@ -12,6 +12,7 @@ NOTE: [`epaint`](epaint/CHANGELOG.md), [`eframe`](eframe/CHANGELOG.md), [`egui_w
### Changed 🔧
* Renamed `Ui::visible` to `Ui::is_visible`.
* Split `Event::Text` into `Event::Text` and `Event::Paste` ([#1058](https://github.com/emilk/egui/pull/1058)).
### Fixed 🐛
* Context menu now respects the theme ([#1043](https://github.com/emilk/egui/pull/1043))

View file

@ -484,7 +484,7 @@ impl State {
if let Some(contents) = self.clipboard.get() {
self.egui_input
.events
.push(egui::Event::Text(contents.replace("\r\n", "\n")));
.push(egui::Event::Paste(contents.replace("\r\n", "\n")));
}
}
}

View file

@ -148,7 +148,9 @@ pub enum Event {
Copy,
/// The integration detected a "cut" event (e.g. Cmd+X).
Cut,
/// Text input, e.g. via keyboard or paste action.
/// The integration detected a "paste" event (e.g. Cmd+V).
Paste(String),
/// Text input, e.g. via keyboard.
///
/// When the user presses enter/return, do not send a `Text` (just [`Key::Enter`]).
Text(String),

View file

@ -684,6 +684,15 @@ fn events(
Some(CCursorRange::one(delete_selected(text, &cursor_range)))
}
}
Event::Paste(text_to_insert) => {
if !text_to_insert.is_empty() {
let mut ccursor = delete_selected(text, &cursor_range);
insert_text(&mut ccursor, text, text_to_insert);
Some(CCursorRange::one(ccursor))
} else {
None
}
}
Event::Text(text_to_insert) => {
// Newlines are handled by `Key::Enter`.
if !text_to_insert.is_empty() && text_to_insert != "\n" && text_to_insert != "\r" {

View file

@ -622,7 +622,7 @@ fn install_document_events(runner_ref: &AppRunnerRef) -> Result<(), JsValue> {
.input
.raw
.events
.push(egui::Event::Text(text.replace("\r\n", "\n")));
.push(egui::Event::Paste(text.replace("\r\n", "\n")));
runner_lock.needs_repaint.set_true();
event.stop_propagation();
event.prevent_default();