URI: 
       Perf optimized new command system. - icy_draw - icy_draw is the successor to mystic draw. fork / mirror
  HTML git clone https://git.drkhsh.at/icy_draw.git
   DIR Log
   DIR Files
   DIR Refs
   DIR README
   DIR LICENSE
       ---
   DIR commit 0f78aaae5003b8329517f7e6e58ae7556ae246e0
   DIR parent 063c9f1f4fc13d3b214f46ae247374bec3dba278
  HTML Author: Mike Krüger <mkrueger@posteo.de>
       Date:   Mon, 18 Sep 2023 23:36:45 +0200
       
       Perf optimized new command system.
       
       + removed all string operations.
       
       Diffstat:
         M src/ui/commands.rs                  |      30 +++++++++++++++++++-----------
         M src/ui/main_window.rs               |       6 +++---
         M src/ui/top_bar.rs                   |     239 +++++++++----------------------
       
       3 files changed, 93 insertions(+), 182 deletions(-)
       ---
   DIR diff --git a/src/ui/commands.rs b/src/ui/commands.rs
       @@ -165,7 +165,7 @@ pub struct CommandWrapper {
            label: String,
            pub is_enabled: bool,
            pub is_checked: Option<bool>,
       -    state: String,
       +    state_key: u32,
        }
        
        mod modifier_keys {
       @@ -224,7 +224,7 @@ macro_rules! key {
        macro_rules! keys {
            ($( ($l:ident, $translation: expr, $message:ident, $cmd_state: ident$(, $key:ident, $modifier: ident)? ) ),* $(,)? ) => {
                pub struct Commands {
       -            state_map: HashMap<String, Box<dyn CommandState>>,
       +            state_map: HashMap<u32, Box<dyn CommandState>>,
                    $(
                        pub $l: CommandWrapper,
                    )*
       @@ -232,15 +232,15 @@ macro_rules! keys {
        
                impl Default for Commands {
                    fn default() -> Self {
       -                let mut state_map = HashMap::<String, Box<dyn CommandState>>::new();
       +                let mut state_map = HashMap::<u32, Box<dyn CommandState>>::new();
                        $(
       -                    state_map.insert(stringify!($cmd_state).into(), Box::<$cmd_state>::default());
       +                    state_map.insert(hash(stringify!($cmd_state)), Box::<$cmd_state>::default());
                        )*
       -                
       +
                        Self {
                            state_map,
                            $(
       -                        $l: CommandWrapper::new(key!($($key, $modifier)?), Message::$message, fl!(crate::LANGUAGE_LOADER, $translation), stringify!($cmd_state).into()),
       +                        $l: CommandWrapper::new(key!($($key, $modifier)?), Message::$message, fl!(crate::LANGUAGE_LOADER, $translation), hash(stringify!($cmd_state))),
                            )*
                        }
                    }
       @@ -273,26 +273,34 @@ macro_rules! keys {
            };
        }
        
       +fn hash(str: impl Into<String>) -> u32 {
       +    use std::collections::hash_map::DefaultHasher;
       +    use std::hash::{Hash, Hasher};
       +
       +    let mut hasher = DefaultHasher::new();
       +    str.into().hash(&mut hasher);
       +    hasher.finish() as u32
       +}
       +
        impl CommandWrapper {
            pub fn new(
                key: Option<(KeyOrPointer, Modifiers)>,
                message: Message,
                description: String,
       -        state: String,
       +        state_key: u32,
            ) -> Self {
       -
                Self {
                    key,
                    message,
                    label: description,
       -            state,
       +            state_key,
                    is_enabled: true,
                    is_checked: None,
                }
            }
        
       -    pub fn update_state(&mut self, result_map: &HashMap::<&String, (bool, Option<bool>)>) {
       -        let (is_enabled, is_checked) = result_map.get(&self.state).unwrap();
       +    pub fn update_state(&mut self, result_map: &HashMap<&u32, (bool, Option<bool>)>) {
       +        let (is_enabled, is_checked) = result_map.get(&self.state_key).unwrap();
                self.is_enabled = *is_enabled;
                self.is_checked = *is_checked;
            }
   DIR diff --git a/src/ui/main_window.rs b/src/ui/main_window.rs
       @@ -42,7 +42,7 @@ pub struct MainWindow {
            pub right_panel: bool,
            pub bottom_panel: bool,
        
       -    pub commands: Commands,
       +    pub commands: Vec<Box<Commands>>,
            pub is_fullscreen: bool,
        
            pub in_open_file_mode: bool,
       @@ -203,7 +203,7 @@ impl MainWindow {
                    right_panel: true,
                    bottom_panel: false,
                    top_bar: TopBar::new(&cc.egui_ctx),
       -            commands: Commands::default(),
       +            commands: vec![Box::<Commands>::default()],
                    is_closed: false,
                    is_fullscreen: false,
                    in_open_file_mode: false,
       @@ -795,7 +795,7 @@ impl eframe::App for MainWindow {
                }
        
                let mut msg = self.document_behavior.message.take();
       -        self.commands.check(ctx, &mut msg);
       +        self.commands[0].check(ctx, &mut msg);
                self.handle_message(msg);
                self.handle_message(read_outline_keys(ctx));
        
   DIR diff --git a/src/ui/top_bar.rs b/src/ui/top_bar.rs
       @@ -49,11 +49,10 @@ impl MainWindow {
        
                menu::bar(ui, |ui| {
                    let mut has_buffer = false;
       -            /*if let Some(mut cmd) = self.commands.take() {
       -                let pane = self.get_active_pane();
       -                cmd.update_states(pane);
       -                self.commands = Some(cmd);
       -            }*/
       +            let mut c = self.commands.pop().unwrap();
       +            let pane = self.get_active_pane();
       +            c.update_states(pane);
       +            self.commands.push(c);
        
                    if let Some(pane) = self.get_active_pane_mut() {
                        if let Ok(doc) = pane.doc.lock() {
       @@ -64,10 +63,8 @@ impl MainWindow {
                    ui.menu_button(fl!(crate::LANGUAGE_LOADER, "menu-file"), |ui| {
                        ui.set_min_width(300.0);
        
       -                self.commands.new_file.ui(ui, &mut result);
       -                self.commands
       -                    .open_file
       -                    .ui(ui, &mut result);
       +                self.commands[0].new_file.ui(ui, &mut result);
       +                self.commands[0].open_file.ui(ui, &mut result);
                        ui.menu_button(
                            fl!(crate::LANGUAGE_LOADER, "menu-open_recent"),
                            |ui| unsafe {
       @@ -84,23 +81,17 @@ impl MainWindow {
                                    }
                                    ui.separator();
                                }
       -                        self.commands
       -                            .clear_recent_open
       -                            .ui(ui, &mut result);
       +                        self.commands[0].clear_recent_open.ui(ui, &mut result);
                            },
                        );
                        ui.separator();
       -                self.commands.save.ui(ui, &mut result);
       -                self.commands.save_as.ui(ui, &mut result);
       -                self.commands.export.ui(ui, &mut result);
       +                self.commands[0].save.ui(ui, &mut result);
       +                self.commands[0].save_as.ui(ui, &mut result);
       +                self.commands[0].export.ui(ui, &mut result);
                        ui.separator();
       -                self.commands
       -                    .edit_font_outline
       -                    .ui(ui, &mut result);
       +                self.commands[0].edit_font_outline.ui(ui, &mut result);
                        ui.separator();
       -                self.commands
       -                    .close_window
       -                    .ui(ui, &mut result);
       +                self.commands[0].close_window.ui(ui, &mut result);
                    });
        
                    ui.menu_button(fl!(crate::LANGUAGE_LOADER, "menu-edit"), |ui| {
       @@ -125,7 +116,7 @@ impl MainWindow {
                                    ui.close_menu();
                                }
                            } else {
       -                        self.commands.undo.ui(ui, &mut result);
       +                        self.commands[0].undo.ui(ui, &mut result);
                            }
        
                            if doc.lock().unwrap().can_redo() {
       @@ -144,17 +135,17 @@ impl MainWindow {
                                    ui.close_menu();
                                }
                            } else {
       -                        self.commands.redo.ui(ui, &mut result);
       +                        self.commands[0].redo.ui(ui, &mut result);
                            }
                        } else {
       -                    self.commands.undo.ui(ui, &mut result);
       -                    self.commands.redo.ui(ui, &mut result);
       +                    self.commands[0].undo.ui(ui, &mut result);
       +                    self.commands[0].redo.ui(ui, &mut result);
                        }
                        ui.separator();
                        if self.get_active_document().is_some() {
       -                    self.commands.cut.ui(ui, &mut result);
       -                    self.commands.copy.ui(ui, &mut result);
       -                    self.commands.paste.ui(ui, &mut result);
       +                    self.commands[0].cut.ui(ui, &mut result);
       +                    self.commands[0].copy.ui(ui, &mut result);
       +                    self.commands[0].paste.ui(ui, &mut result);
                        }
        
                        ui.menu_button(fl!(crate::LANGUAGE_LOADER, "menu-paste-as"), |ui| {
       @@ -188,66 +179,30 @@ impl MainWindow {
                            ui.style_mut().wrap = Some(false);
                            ui.set_min_width(300.0);
        
       -                    self.commands
       -                        .justify_line_left
       -                        .ui(ui, &mut result);
       -                    self.commands
       -                        .justify_line_right
       -                        .ui(ui, &mut result);
       -                    self.commands
       -                        .justify_line_center
       -                        .ui(ui, &mut result);
       +                    self.commands[0].justify_line_left.ui(ui, &mut result);
       +                    self.commands[0].justify_line_right.ui(ui, &mut result);
       +                    self.commands[0].justify_line_center.ui(ui, &mut result);
                            ui.separator();
       -                    self.commands
       -                        .insert_row
       -                        .ui(ui, &mut result);
       -                    self.commands
       -                        .delete_row
       -                        .ui(ui, &mut result);
       +                    self.commands[0].insert_row.ui(ui, &mut result);
       +                    self.commands[0].delete_row.ui(ui, &mut result);
                            ui.separator();
       -                    self.commands
       -                        .insert_column
       -                        .ui(ui, &mut result);
       -                    self.commands
       -                        .delete_column
       -                        .ui(ui, &mut result);
       +                    self.commands[0].insert_column.ui(ui, &mut result);
       +                    self.commands[0].delete_column.ui(ui, &mut result);
                            ui.separator();
       -                    self.commands
       -                        .erase_row
       -                        .ui(ui, &mut result);
       -                    self.commands
       -                        .erase_row_to_start
       -                        .ui(ui, &mut result);
       -                    self.commands
       -                        .erase_row_to_end
       -                        .ui(ui, &mut result);
       +                    self.commands[0].erase_row.ui(ui, &mut result);
       +                    self.commands[0].erase_row_to_start.ui(ui, &mut result);
       +                    self.commands[0].erase_row_to_end.ui(ui, &mut result);
                            ui.separator();
       -                    self.commands
       -                        .erase_column
       -                        .ui(ui, &mut result);
       -                    self.commands
       -                        .erase_column_to_end
       -                        .ui(ui, &mut result);
       -                    self.commands
       -                        .erase_column_to_start
       -                        .ui(ui, &mut result);
       +                    self.commands[0].erase_column.ui(ui, &mut result);
       +                    self.commands[0].erase_column_to_end.ui(ui, &mut result);
       +                    self.commands[0].erase_column_to_start.ui(ui, &mut result);
                            ui.separator();
       -                    self.commands
       -                        .scroll_area_up
       -                        .ui(ui, &mut result);
       -                    self.commands
       -                        .scroll_area_down
       -                        .ui(ui, &mut result);
       -                    self.commands
       -                        .scroll_area_left
       -                        .ui(ui, &mut result);
       -                    self.commands
       -                        .scroll_area_right
       -                        .ui(ui, &mut result);
       +                    self.commands[0].scroll_area_up.ui(ui, &mut result);
       +                    self.commands[0].scroll_area_down.ui(ui, &mut result);
       +                    self.commands[0].scroll_area_left.ui(ui, &mut result);
       +                    self.commands[0].scroll_area_right.ui(ui, &mut result);
                        });
       -                self.commands
       -                    .mirror_mode
       -                    .ui(ui, &mut result);
       +                self.commands[0].mirror_mode.ui(ui, &mut result);
        
                        ui.separator();
                        if ui
       @@ -278,29 +233,17 @@ impl MainWindow {
                    ui.menu_button(fl!(crate::LANGUAGE_LOADER, "menu-selection"), |ui| {
                        ui.style_mut().wrap = Some(false);
                        ui.set_min_width(200.0);
       -                self.commands
       -                    .select_all
       -                    .ui(ui, &mut result);
       -                self.commands.deselect.ui(ui, &mut result);
       -                self.commands
       -                    .inverse_selection
       -                    .ui(ui, &mut result);
       +                self.commands[0].select_all.ui(ui, &mut result);
       +                self.commands[0].deselect.ui(ui, &mut result);
       +                self.commands[0].inverse_selection.ui(ui, &mut result);
                        ui.separator();
       -                self.commands
       -                    .erase_selection
       -                    .ui(ui, &mut result);
       -                self.commands.flip_x.ui(ui, &mut result);
       -                self.commands.flip_y.ui(ui, &mut result);
       -                self.commands
       -                    .justifycenter
       -                    .ui(ui, &mut result);
       -                self.commands
       -                    .justifyleft
       -                    .ui(ui, &mut result);
       -                self.commands
       -                    .justifyright
       -                    .ui(ui, &mut result);
       -                self.commands.crop.ui(ui, &mut result);
       +                self.commands[0].erase_selection.ui(ui, &mut result);
       +                self.commands[0].flip_x.ui(ui, &mut result);
       +                self.commands[0].flip_y.ui(ui, &mut result);
       +                self.commands[0].justifycenter.ui(ui, &mut result);
       +                self.commands[0].justifyleft.ui(ui, &mut result);
       +                self.commands[0].justifyright.ui(ui, &mut result);
       +                self.commands[0].crop.ui(ui, &mut result);
                    });
                    ui.menu_button(fl!(crate::LANGUAGE_LOADER, "menu-colors"), |ui| {
                        ui.style_mut().wrap = Some(false);
       @@ -385,56 +328,32 @@ impl MainWindow {
                            }
                            ui.separator();
                        }
       -                self.commands
       -                    .select_palette
       -                    .ui(ui, &mut result);
       -                self.commands
       -                    .open_palettes_directory
       -                    .ui(ui, &mut result);
       +                self.commands[0].select_palette.ui(ui, &mut result);
       +                self.commands[0].open_palettes_directory.ui(ui, &mut result);
                        ui.separator();
        
       -                self.commands
       -                    .next_fg_color
       -                    .ui(ui, &mut result);
       -                self.commands
       -                    .prev_fg_color
       -                    .ui(ui, &mut result);
       +                self.commands[0].next_fg_color.ui(ui, &mut result);
       +                self.commands[0].prev_fg_color.ui(ui, &mut result);
        
                        ui.separator();
        
       -                self.commands
       -                    .next_bg_color
       -                    .ui(ui, &mut result);
       -                self.commands
       -                    .prev_bg_color
       -                    .ui(ui, &mut result);
       +                self.commands[0].next_bg_color.ui(ui, &mut result);
       +                self.commands[0].prev_bg_color.ui(ui, &mut result);
        
       -                self.commands
       +                self.commands[0]
                            .pick_attribute_under_caret
                            .ui(ui, &mut result);
       -                self.commands
       -                    .toggle_color
       -                    .ui(ui, &mut result);
       -                self.commands
       -                    .switch_to_default_color
       -                    .ui(ui, &mut result);
       +                self.commands[0].toggle_color.ui(ui, &mut result);
       +                self.commands[0].switch_to_default_color.ui(ui, &mut result);
                    });
                    ui.menu_button(fl!(crate::LANGUAGE_LOADER, "menu-fonts"), |ui| {
                        ui.style_mut().wrap = Some(false);
                        ui.set_min_width(220.0);
       -                self.commands
       -                    .open_font_selector
       -                    .ui(ui, &mut result);
       -                self.commands
       -                    .open_font_manager
       -                    .ui(ui, &mut result);
       +                self.commands[0].open_font_selector.ui(ui, &mut result);
       +                self.commands[0].open_font_manager.ui(ui, &mut result);
                        ui.separator();
       -                self.commands
       -                    .open_font_directory
       -                    .ui(ui, &mut result);
       -                self.commands
       -                    .open_tdf_directory
       -                    .ui(ui, &mut result);
       +                self.commands[0].open_font_directory.ui(ui, &mut result);
       +                self.commands[0].open_tdf_directory.ui(ui, &mut result);
                    });
                    ui.menu_button(fl!(crate::LANGUAGE_LOADER, "menu-view"), |ui| {
                        ui.style_mut().wrap = Some(false);
       @@ -452,12 +371,10 @@ impl MainWindow {
                                ui.style_mut().wrap = Some(false);
                                ui.set_min_width(270.0);
        
       -                        self.commands
       -                            .zoom_reset
       -                            .ui(ui, &mut result);
       -                        self.commands.zoom_in.ui(ui, &mut result);
       +                        self.commands[0].zoom_reset.ui(ui, &mut result);
       +                        self.commands[0].zoom_in.ui(ui, &mut result);
        
       -                        self.commands.zoom_out.ui(ui, &mut result);
       +                        self.commands[0].zoom_out.ui(ui, &mut result);
                                ui.separator();
        
                                if ui.button("4:1 400%").clicked() {
       @@ -572,27 +489,15 @@ impl MainWindow {
                            }
                        });
        
       -                self.commands
       -                    .show_layer_borders
       -                    .ui(ui, &mut result);
       -                self.commands
       -                    .show_line_numbers
       -                    .ui(ui, &mut result);
       +                self.commands[0].show_layer_borders.ui(ui, &mut result);
       +                self.commands[0].show_line_numbers.ui(ui, &mut result);
        
       -                self.commands
       -                    .fullscreen
       -                    .ui(ui, &mut result);
       +                self.commands[0].fullscreen.ui(ui, &mut result);
        
                        ui.separator();
       -                self.commands
       -                    .set_reference_image
       -                    .ui(ui, &mut result);
       -                self.commands
       -                    .toggle_reference_image
       -                    .ui(ui, &mut result);
       -                self.commands
       -                    .clear_reference_image
       -                    .ui(ui, &mut result);
       +                self.commands[0].set_reference_image.ui(ui, &mut result);
       +                self.commands[0].toggle_reference_image.ui(ui, &mut result);
       +                self.commands[0].clear_reference_image.ui(ui, &mut result);
                    });
        
                    unsafe {
       @@ -614,9 +519,7 @@ impl MainWindow {
                                }
        
                                ui.separator();
       -                        self.commands
       -                            .open_plugin_directory
       -                            .ui(ui, &mut result);
       +                        self.commands[0].open_plugin_directory.ui(ui, &mut result);
                            });
                        }
                    }
       @@ -646,7 +549,7 @@ impl MainWindow {
                            ui.close_menu();
                        }
                        ui.separator();
       -                self.commands.about.ui(ui, &mut result);
       +                self.commands[0].about.ui(ui, &mut result);
                    });
                    self.top_bar_ui(ui, frame);
                });