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

Add Ui.input_mut & InputState.ignore_key #1212

Merged
merged 15 commits into from
Feb 15, 2022
Merged

Add Ui.input_mut & InputState.ignore_key #1212

merged 15 commits into from
Feb 15, 2022

Conversation

cat-state
Copy link
Contributor

@cat-state cat-state commented Feb 5, 2022

This PR pipes through ctx.input_mut() through to the Uis, allowing control of what events subsequent widgets see, and adds a utility function to the InputState to do hotkeys/shortcuts.

Helps with #1124

Copy link
Owner

@emilk emilk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need some polish, but I think this is the right direction.

It would be good to use the new function in egui_demo_lib/src/easy_mark/easy_mark_editor.rs (fn shortcuts) as a demonstration/test!

egui/src/input_state.rs Outdated Show resolved Hide resolved
egui/src/input_state.rs Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
egui/src/ui.rs Outdated Show resolved Hide resolved
key,
modifiers: mods,
..
} if *key == ignore_key && *mods == modifiers
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will match both on press and release - I think we should only match on pressed: true

Copy link
Contributor Author

@cat-state cat-state Feb 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, what if code depended on presses always being matched with releases? then you'd have an unmatched release instead of as if the event "never happened"

CHANGELOG.md Outdated Show resolved Hide resolved
@cat-state cat-state requested a review from emilk February 5, 2022 18:43
}
egui::Modifiers {
command: true,
..Default::default()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can make this even nicer by switching the argument order and adding some helpers to Modifiers:

            if ui.input_mut().consume_key(egui::Modifiers::command(), key) {

Copy link
Contributor Author

@cat-state cat-state Feb 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice, i added it as a builder api but lmk if you prefer classmethods + operator overloading instead

            if ui.input_mut().consume_key(egui::Modifiers::new().command(true).shift(true), key) {

vs

            if ui.input_mut().consume_key(egui::Modifiers::command(true) | egui::Modifiers::shift(true), key) {

Copy link
Contributor Author

@cat-state cat-state Feb 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another way to do this more generally could be to have a consume_event + some helper to easily make the event from key and modifier combo like Event::from_key(modifier, key) or modifier.event_with_key(key) or viceversa. Not sure if that would need a bunch of refactoring of code that makes the convenience properties from the events to keep the invariants maintained

@cat-state cat-state requested a review from emilk February 7, 2022 11:45
}
}
}
toggle_surrounding(code, ccursor_range, surrounding);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the docs of consume_key, it will return true both when the key is pressed and when it is released, so this should toggle twice, which is not intentional. This is why I think consume_key should only detect presses and ignore releases (or the other way around)

Copy link
Contributor Author

@cat-state cat-state Feb 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It only returns true when the key was pressed but it will remove the release event too, so triggering only once. Also in the built app it seems to only trigger once (on Windows & Ubuntu) - do you see differently?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't run in since last review :)

But then the docs for consume_key should be changed to indicate it is only triggered on press, and also match for pressed: true.

}
}
}
toggle_surrounding(code, ccursor_range, surrounding);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't run in since last review :)

But then the docs for consume_key should be changed to indicate it is only triggered on press, and also match for pressed: true.

@emilk emilk merged commit c1569ed into emilk:master Feb 15, 2022
emilk added a commit that referenced this pull request Feb 21, 2022
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 this pull request may close these issues.

3 participants