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

View File

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