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

Ctrl+Shift+Z should restore data to the state that was before like Ctrl+Y does #5255

Open
AngelicosPhosphoros opened this issue Oct 12, 2024 · 3 comments · May be fixed by #5258
Open

Ctrl+Shift+Z should restore data to the state that was before like Ctrl+Y does #5255

AngelicosPhosphoros opened this issue Oct 12, 2024 · 3 comments · May be fixed by #5258

Comments

@AngelicosPhosphoros
Copy link

AngelicosPhosphoros commented Oct 12, 2024

Is your feature request related to a problem? Please describe.

Well, I often use keybinding Ctrl+Shift+Z to restore text state after I accidentally undid too much changes using Ctrl+Z. In egui controls, e.g. text edit, Ctrl+Shift+Z doesn't behave like rest of such inputs in my system (Windows 10).

Describe the solution you'd like
Ctrl+Shift+Z keybinding should do redo operation, like Ctrl+Y does.

Describe alternatives you've considered
Do nothing and tell users to use Ctrl+Y. It works on egui controls.

Additional context
Well, I use computers the biggest part of my life and always used Ctrl+Shift+Z to cancel Ctrl+Z. Only when opening this issue, I checked if this is actually standardized and found that there exists a Ctrl+Y keybinding. I think, it is common for users to be like me so supporting both Ctrl+Y and Ctrl+Shift+Z would be convenient for them.

Maybe, it is a wrong repository to report this and I should report it to winit, I don't really know.

@YgorSouza
Copy link
Contributor

No, this has nothing to do with winit, and it's actually a bug in egui. The Ctrl+Shift+Z shortcut is supposed to work as redo as well:

Event::Key {
key,
pressed: true,
modifiers,
..
} if (modifiers.matches_logically(Modifiers::COMMAND) && *key == Key::Y)
|| (modifiers.matches_logically(Modifiers::SHIFT | Modifiers::COMMAND)
&& *key == Key::Z) =>
{
if let Some((redo_ccursor_range, redo_txt)) = state
.undoer
.lock()
.redo(&(cursor_range.as_ccursor_range(), text.as_str().to_owned()))
{
text.replace_with(redo_txt);
Some(*redo_ccursor_range)
} else {
None
}

But it doesn't, because the previous match arm (undo) is also triggered by this same key combination:

Event::Key {
key: Key::Z,
pressed: true,
modifiers,
..
} if modifiers.matches_logically(Modifiers::COMMAND) => {
if let Some((undo_ccursor_range, undo_txt)) = state
.undoer
.lock()
.undo(&(cursor_range.as_ccursor_range(), text.as_str().to_owned()))
{
text.replace_with(undo_txt);
Some(*undo_ccursor_range)
} else {
None
}
}

This matches_logically method is supposed to work like this, at least according to its doctests:

/// ## Behavior:
/// ```
/// # use egui::Modifiers;
/// assert!(Modifiers::CTRL.matches_logically(Modifiers::CTRL));
/// assert!(!Modifiers::CTRL.matches_logically(Modifiers::CTRL | Modifiers::SHIFT));
/// assert!((Modifiers::CTRL | Modifiers::SHIFT).matches_logically(Modifiers::CTRL));
/// assert!((Modifiers::CTRL | Modifiers::COMMAND).matches_logically(Modifiers::CTRL));
/// assert!((Modifiers::CTRL | Modifiers::COMMAND).matches_logically(Modifiers::COMMAND));
/// assert!((Modifiers::MAC_CMD | Modifiers::COMMAND).matches_logically(Modifiers::COMMAND));
/// assert!(!Modifiers::COMMAND.matches_logically(Modifiers::MAC_CMD));
/// ```
pub fn matches_logically(&self, pattern: Self) -> bool {
if pattern.alt && !self.alt {
return false;
}
if pattern.shift && !self.shift {
return false;
}

But it is being used incorrectly here, causing both Ctrl+Z and Ctrl+Shift+Z to work as undo.

@YgorSouza YgorSouza linked a pull request Oct 13, 2024 that will close this issue
1 task
@Mingun
Copy link
Contributor

Mingun commented Oct 13, 2024

This code looks awful

} if (modifiers.matches_logically(Modifiers::COMMAND) && *key == Key::Y)
|| (modifiers.matches_logically(Modifiers::SHIFT | Modifiers::COMMAND)
&& *key == Key::Z) =>

I would be better introduce a Shortcut struct which will incapsulate keys and modifiers. QShortcut is a good inspiration.

@YgorSouza
Copy link
Contributor

There is https://docs.rs/egui/0.29.1/egui/struct.KeyboardShortcut.html. Not sure why it isn't being used here. Maybe it was introduced later and this code was never refactored. But that is a separate issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants