Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split Event::Text into Text and Paste #1058

Merged
merged 4 commits into from Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
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` ([#1057](https://github.com/emilk/egui/issues/1057).
emilk marked this conversation as resolved.
Show resolved Hide resolved

### 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
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
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
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
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