Skip to content

Commit

Permalink
Split Event::Text into Text and Paste (#1058)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
psiphi75 and emilk committed Jan 10, 2022
1 parent 650057d commit 225d2b5
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion egui-winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")));
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion egui/src/data/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
9 changes: 9 additions & 0 deletions egui/src/widgets/text_edit/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
2 changes: 1 addition & 1 deletion egui_web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 225d2b5

Please sign in to comment.