refactor(egui/action): `into_*` methods instead of wrapping `::new` calls

This commit is contained in:
Alain Emilia Anna Zscheile 2024-12-23 22:10:57 +01:00
parent fbc71e46dc
commit b47459b3d7
2 changed files with 62 additions and 46 deletions

View File

@ -7,6 +7,15 @@ pub struct Action {
shortcut: egui::KeyboardShortcut, shortcut: egui::KeyboardShortcut,
} }
pub struct Trigger {
action: Action,
triggered: bool,
}
pub struct Switch {
action: Action,
}
impl Action { impl Action {
pub fn new(name: String, modifiers: egui::Modifiers, key: egui::Key) -> Self { pub fn new(name: String, modifiers: egui::Modifiers, key: egui::Key) -> Self {
Self { Self {
@ -22,21 +31,22 @@ impl Action {
self.shortcut.format(&egui::ModifierNames::NAMES, false) self.shortcut.format(&egui::ModifierNames::NAMES, false)
) )
} }
}
pub struct Trigger { #[inline]
action: Action, pub fn into_trigger(self) -> Trigger {
triggered: bool, Trigger {
} action: self,
impl Trigger {
pub fn new(action: Action) -> Self {
Self {
action,
triggered: false, triggered: false,
} }
} }
#[inline(always)]
pub fn into_switch(self) -> Switch {
Switch { action: self }
}
}
impl Trigger {
pub fn button(&mut self, _ctx: &egui::Context, ui: &mut egui::Ui) { pub fn button(&mut self, _ctx: &egui::Context, ui: &mut egui::Ui) {
self.triggered = ui.button(self.action.widget_text()).clicked(); self.triggered = ui.button(self.action.widget_text()).clicked();
} }
@ -61,15 +71,7 @@ impl Trigger {
} }
} }
pub struct Switch {
action: Action,
}
impl Switch { impl Switch {
pub fn new(action: Action) -> Self {
Self { action }
}
pub fn toggle_widget(&mut self, _ctx: &egui::Context, ui: &mut egui::Ui, selected: &mut bool) { pub fn toggle_widget(&mut self, _ctx: &egui::Context, ui: &mut egui::Ui, selected: &mut bool) {
ui.toggle_value(selected, self.action.widget_text()); ui.toggle_value(selected, self.action.widget_text());
} }

View File

@ -18,31 +18,36 @@ pub struct FileActions {
impl FileActions { impl FileActions {
pub fn new(tr: &Translator) -> Self { pub fn new(tr: &Translator) -> Self {
Self { Self {
open_design: Trigger::new(Action::new( open_design: Action::new(
tr.text("tr-menu-file-open"), tr.text("tr-menu-file-open"),
egui::Modifiers::CTRL, egui::Modifiers::CTRL,
egui::Key::O, egui::Key::O,
)), )
export_session: Trigger::new(Action::new( .into_trigger(),
export_session: Action::new(
tr.text("tr-menu-file-export-session-file"), tr.text("tr-menu-file-export-session-file"),
egui::Modifiers::CTRL, egui::Modifiers::CTRL,
egui::Key::S, egui::Key::S,
)), )
import_history: Trigger::new(Action::new( .into_trigger(),
import_history: Action::new(
tr.text("tr-menu-file-import-history"), tr.text("tr-menu-file-import-history"),
egui::Modifiers::CTRL, egui::Modifiers::CTRL,
egui::Key::I, egui::Key::I,
)), )
export_history: Trigger::new(Action::new( .into_trigger(),
export_history: Action::new(
tr.text("tr-menu-file-export-history"), tr.text("tr-menu-file-export-history"),
egui::Modifiers::CTRL, egui::Modifiers::CTRL,
egui::Key::E, egui::Key::E,
)), )
quit: Trigger::new(Action::new( .into_trigger(),
quit: Action::new(
tr.text("tr-menu-file-quit"), tr.text("tr-menu-file-quit"),
egui::Modifiers::CTRL, egui::Modifiers::CTRL,
egui::Key::Q, egui::Key::Q,
)), )
.into_trigger(),
} }
} }
} }
@ -57,26 +62,30 @@ pub struct EditActions {
impl EditActions { impl EditActions {
pub fn new(tr: &Translator) -> Self { pub fn new(tr: &Translator) -> Self {
Self { Self {
undo: Trigger::new(Action::new( undo: Action::new(
tr.text("tr-menu-edit-undo"), tr.text("tr-menu-edit-undo"),
egui::Modifiers::CTRL, egui::Modifiers::CTRL,
egui::Key::Z, egui::Key::Z,
)), )
redo: Trigger::new(Action::new( .into_trigger(),
redo: Action::new(
tr.text("tr-menu-edit-redo"), tr.text("tr-menu-edit-redo"),
egui::Modifiers::CTRL, egui::Modifiers::CTRL,
egui::Key::Y, egui::Key::Y,
)), )
abort: Trigger::new(Action::new( .into_trigger(),
abort: Action::new(
tr.text("tr-menu-edit-abort"), tr.text("tr-menu-edit-abort"),
egui::Modifiers::NONE, egui::Modifiers::NONE,
egui::Key::Escape, egui::Key::Escape,
)), )
remove_bands: Trigger::new(Action::new( .into_trigger(),
remove_bands: Action::new(
tr.text("tr-menu-edit-remove-bands"), tr.text("tr-menu-edit-remove-bands"),
egui::Modifiers::NONE, egui::Modifiers::NONE,
egui::Key::Delete, egui::Key::Delete,
)), )
.into_trigger(),
} }
} }
} }
@ -88,11 +97,12 @@ pub struct PlaceActions {
impl PlaceActions { impl PlaceActions {
pub fn new(tr: &Translator) -> Self { pub fn new(tr: &Translator) -> Self {
Self { Self {
place_via: Switch::new(Action::new( place_via: Action::new(
tr.text("tr-menu-place-place-via"), tr.text("tr-menu-place-place-via"),
egui::Modifiers::CTRL, egui::Modifiers::CTRL,
egui::Key::P, egui::Key::P,
)), )
.into_switch(),
} }
} }
} }
@ -104,11 +114,12 @@ pub struct RouteActions {
impl RouteActions { impl RouteActions {
pub fn new(tr: &Translator) -> Self { pub fn new(tr: &Translator) -> Self {
Self { Self {
autoroute: Trigger::new(Action::new( autoroute: Action::new(
tr.text("tr-menu-route-autoroute"), tr.text("tr-menu-route-autoroute"),
egui::Modifiers::CTRL, egui::Modifiers::CTRL,
egui::Key::A, egui::Key::A,
)), )
.into_trigger(),
} }
} }
} }
@ -121,16 +132,18 @@ pub struct InspectActions {
impl InspectActions { impl InspectActions {
pub fn new(tr: &Translator) -> Self { pub fn new(tr: &Translator) -> Self {
Self { Self {
compare_detours: Trigger::new(Action::new( compare_detours: Action::new(
tr.text("tr-menu-inspect-compare-detours"), tr.text("tr-menu-inspect-compare-detours"),
egui::Modifiers::NONE, egui::Modifiers::NONE,
egui::Key::Minus, egui::Key::Minus,
)), )
measure_length: Trigger::new(Action::new( .into_trigger(),
measure_length: Action::new(
tr.text("tr-menu-inspect-measure-length"), tr.text("tr-menu-inspect-measure-length"),
egui::Modifiers::NONE, egui::Modifiers::NONE,
egui::Key::Plus, egui::Key::Plus,
)), )
.into_trigger(),
} }
} }
} }
@ -142,11 +155,12 @@ pub struct HelpActions {
impl HelpActions { impl HelpActions {
pub fn new(tr: &Translator) -> Self { pub fn new(tr: &Translator) -> Self {
Self { Self {
online_documentation: Trigger::new(Action::new( online_documentation: Action::new(
tr.text("tr-menu-help-online-documentation"), tr.text("tr-menu-help-online-documentation"),
egui::Modifiers::NONE, egui::Modifiers::NONE,
egui::Key::F1, egui::Key::F1,
)), )
.into_trigger(),
} }
} }
} }