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

Implement Copy, Cut, and Paste in EventFilter #5618

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions crates/egui/src/data/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1200,20 +1200,35 @@ pub struct EventFilter {
/// If `true`, pressing horizontal arrows will act on the
/// widget, and NOT move focus away from the focused widget.
///
/// Default: `false`
/// Defaults to `false`, but is always `true` in the `TextEdit`.
pub horizontal_arrows: bool,

/// If `true`, pressing vertical arrows will act on the
/// widget, and NOT move focus away from the focused widget.
///
/// Default: `false`
/// Defaults to `false`, but is always `true` in the `TextEdit`.
pub vertical_arrows: bool,

/// If `true`, pressing escape will act on the widget,
/// and NOT surrender focus from the focused widget.
///
/// Default: `false`
pub escape: bool,

/// If `true`, a copy action (like Ctrl+C) will be handled by the widget.
///
/// Default: `true`
pub copy: bool,

/// If `true`, a cut action (like Ctrl+X) will be handled by the widget.
///
/// Default: `true`
pub cut: bool,

/// If `true`, a paste action (like Ctrl+V) will be handled by the widget.
///
/// Default: `true`
pub paste: bool,
}

#[allow(clippy::derivable_impls)] // let's be explicit
Expand All @@ -1224,22 +1239,27 @@ impl Default for EventFilter {
horizontal_arrows: false,
vertical_arrows: false,
escape: false,
copy: true,
cut: true,
paste: true,
}
}
}

impl EventFilter {
pub fn matches(&self, event: &Event) -> bool {
if let Event::Key { key, .. } = event {
match key {
match event {
Event::Key { key, .. } => match key {
crate::Key::Tab => self.tab,
crate::Key::ArrowUp | crate::Key::ArrowDown => self.vertical_arrows,
crate::Key::ArrowRight | crate::Key::ArrowLeft => self.horizontal_arrows,
crate::Key::Escape => self.escape,
_ => true,
}
} else {
true
},
Event::Copy => self.copy,
Event::Cut => self.cut,
Event::Paste(_) => self.paste,
_ => true,
}
}
}
9 changes: 9 additions & 0 deletions crates/egui/src/widgets/text_edit/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,15 @@ impl<'t> TextEdit<'t> {
self.return_key = return_key.into();
self
}

/// Sets the event filter for this [`TextEdit`] instance.
#[inline]
pub fn event_filter(mut self, event_filter: EventFilter) -> Self {
self.event_filter = event_filter;
self.event_filter.horizontal_arrows = true;
self.event_filter.vertical_arrows = true;
self
}
}

// ----------------------------------------------------------------------------
Expand Down
Loading