Skip to content

Commit

Permalink
top_menu: Add button to add new layout and switch to it
Browse files Browse the repository at this point in the history
  • Loading branch information
crumblingstatue committed Oct 16, 2024
1 parent c58f98e commit c2ddacb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
8 changes: 7 additions & 1 deletion src/gui/top_menu/view.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use {
crate::{app::App, gui::Gui},
crate::{app::App, gui::Gui, meta::LayoutMapExt},
egui::Button,
};

Expand All @@ -11,6 +11,12 @@ pub fn ui(ui: &mut egui::Ui, gui: &mut Gui, app: &mut App) {
ui.close_menu();
}
}
ui.separator();
if ui.button("➕ Add new layout").clicked() {
app.hex_ui.current_layout = app.meta_state.meta.layouts.add_new_default();
gui.win.layouts.open.set(true);
ui.close_menu();
}
});
ui.menu_button("Ruler", |ui| {
ui.checkbox(&mut app.hex_ui.ruler.enabled, "enabled");
Expand Down
10 changes: 3 additions & 7 deletions src/gui/windows/layouts_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use {
super::{WinCtx, WindowOpen},
crate::{
app::App,
layout::{default_margin, Layout},
meta::{LayoutKey, MetaLow, NamedView, ViewKey, ViewMap},
meta::{LayoutKey, LayoutMapExt as _, MetaLow, NamedView, ViewKey, ViewMap},
view::{HexData, View, ViewKind},
},
slotmap::Key,
Expand All @@ -16,6 +15,7 @@ pub struct LayoutsWindow {
swap_a: ViewKey,
edit_name: bool,
}

impl super::Window for LayoutsWindow {
fn ui(&mut self, WinCtx { ui, gui, app, .. }: WinCtx) {
if self.open.just_now() {
Expand Down Expand Up @@ -173,11 +173,7 @@ impl super::Window for LayoutsWindow {
}
ui.separator();
if ui.button("New layout").clicked() {
let key = app.meta_state.meta.layouts.insert(Layout {
name: "New layout".into(),
view_grid: Vec::new(),
margin: default_margin(),
});
let key = app.meta_state.meta.layouts.add_new_default();
self.selected = key;
App::switch_layout(&mut app.hex_ui, &app.meta_state.meta, key);
}
Expand Down
14 changes: 14 additions & 0 deletions src/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ pub type LayoutMap = SlotMap<LayoutKey, Layout>;
pub type ScriptMap = SlotMap<ScriptKey, Script>;
pub type Bookmarks = Vec<Bookmark>;

pub trait LayoutMapExt {
fn add_new_default(&mut self) -> LayoutKey;
}

impl LayoutMapExt for LayoutMap {
fn add_new_default(&mut self) -> LayoutKey {
self.insert(Layout {
name: "New layout".into(),
view_grid: Vec::new(),
margin: crate::layout::default_margin(),
})
}
}

/// A bookmark for an offset in a file
#[derive(Serialize, Deserialize, Clone)]
pub struct Bookmark {
Expand Down

0 comments on commit c2ddacb

Please sign in to comment.