Add tiling windows

This commit is contained in:
Michael Mikovsky
2025-11-28 00:09:00 -07:00
parent 2f780d36fd
commit 598068d3ee
11 changed files with 254 additions and 90 deletions
+99
View File
@@ -0,0 +1,99 @@
use egui::Frame;
use egui_tiles::Tree;
use crate::app::{AppState, windows::WindowWrapper};
/// We derive Deserialize/Serialize so we can persist app state on shutdown.
#[derive(serde::Deserialize, serde::Serialize)]
#[serde(default)] // if we add new fields, give them default values when deserializing old state
pub struct TemplateApp {
// tab: Tab,
state: AppState,
tree: Tree<WindowWrapper>,
}
impl Default for TemplateApp {
fn default() -> Self {
// let mut tiles = egui_tiles::Tiles::default();
// let invis_1 = tiles.insert_pane(WindowWrapper {
// nr: 0,
// window: AppWindow::None,
// });
// let invis_2 = tiles.insert_pane(WindowWrapper {
// nr: 0,
// window: AppWindow::None,
// });
// tiles.set_visible(invis_1, false);
// tiles.set_visible(invis_2, false);
// let root = tiles.insert_horizontal_tile(vec![invis_1]);
Self {
state: AppState::default(),
tree: egui_tiles::Tree::new_horizontal("tree_root", Vec::new()),
}
}
}
impl TemplateApp {
/// Called once before the first frame.
pub fn new(_cc: &eframe::CreationContext<'_>) -> Self {
// This is also where you can customize the look and feel of egui using
// `cc.egui_ctx.set_visuals` and `cc.egui_ctx.set_fonts`.
// Load previous app state (if any).
// Note that you must enable the `persistence` feature for this to work.
// if let Some(storage) = cc.storage {
// eframe::get_value(storage, eframe::APP_KEY).unwrap_or_default()
// } else {
Default::default()
// }
}
}
impl eframe::App for TemplateApp {
/// Called by the framework to save state before shutdown.
fn save(&mut self, storage: &mut dyn eframe::Storage) {
eframe::set_value(storage, eframe::APP_KEY, self);
}
/// Called each time the UI needs repainting, which may be many times per second.
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
// Put your widgets into a `SidePanel`, `TopBottomPanel`, `CentralPanel`, `Window` or `Area`.
// For inspiration and more examples, go to https://emilk.github.io/egui
egui::TopBottomPanel::top("tab_panel").show(ctx, |ui| {
// The top panel is often a good place for a menu bar:
egui::MenuBar::new().ui(ui, |ui| {
ui.menu_button("File", |ui| {
ui.label("File");
});
ui.menu_button("View", |ui| {
ui.label("View");
self.state.labels(&mut self.tree, ui);
});
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
egui::widgets::global_theme_preference_switch(ui);
});
// ui.style
});
});
egui::TopBottomPanel::bottom("tab_panel").show(ctx, |ui| {
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
ui.label(format!("UnShell UI {}", env!("CARGO_PKG_VERSION")));
egui::warn_if_debug_build(ui);
});
});
egui::CentralPanel::default()
.frame(Frame::central_panel(&ctx.style()).inner_margin(0))
.show(ctx, |ui| self.tree.ui(&mut self.state, ui));
}
}
+125
View File
@@ -0,0 +1,125 @@
mod app;
mod windows;
use std::collections::HashMap;
pub use app::TemplateApp;
use egui_tiles::{TileId, Tree};
use log::info;
use crate::{app::windows::WindowWrapper, config::Config, flowchart::FlowChart};
#[derive(Default, serde::Deserialize, serde::Serialize)]
struct AppState {
pub open_windows: HashMap<AppWindow, TileId>,
pub flowchart: FlowChart,
pub config: Config,
}
impl AppState {
pub fn labels(&mut self, tree: &mut Tree<WindowWrapper>, ui: &mut egui::Ui) {
for (i, (key, name)) in (vec![
(AppWindow::Flowchart, "Flowchart"),
(AppWindow::Config, "Config"),
])
.iter()
.enumerate()
{
let enabled = self.open_windows.contains_key(&key);
if ui.selectable_label(enabled, *name).clicked() {
// if enabled {
// let tid = *self.open_windows.get(&key).unwrap();
// tree.remove_recursively(tid);
// tree.tiles.remove(tid);
// self.open_windows.remove(&key);
// // if self.open_windows.is_empty()
// } else {
let tid = tree.tiles.insert_pane(WindowWrapper {
nr: i + 1,
window: *key,
});
match self.open_windows.len() {
0 => {
tree.root = Some(tid);
}
1 => {
let old_root = tree.root.unwrap();
let tab_id = tree.tiles.insert_tab_tile(vec![old_root, tid]);
tree.root = Some(tab_id);
}
_ => {
let pid = tree.tiles.insert_tab_tile(vec![tid]);
tree.move_tile_to_container(pid, tree.root.unwrap().clone(), 0, true);
}
}
self.open_windows.insert(key.clone(), tid);
}
}
// if ui
// .selectable_label(
// self.open_windows.contains_key(&AppWindow::Flowchart),
// "Network",
// )
// .clicked()
// {
// // self.open_windows. = Tab::Flowchart;
// }
// if ui
// .selectable_label(self.open_windows.contains_key(&AppWindow::Config), "Config")
// .clicked()
// {
// // self.open_windows. = Tab::Flowchart;
// }
// if ui
// .selectable_label(self.tab == Tab::Test, self.config.title())
// .clicked()
// {
// self.tab = Tab::Test;
// }
}
// fn contains(&self, key: &AppWindow)
// fn toggle(&mut self, key: &AppWindow) {
// if self.
// }
}
// impl Default for AppState {
// fn default() -> Self {
// Self {
// open_windows: HashMap::from([
// (AppWindow::Flowchart, false),
// (AppWindow::Config, false),
// ]),
// flowchart: Default::default(),
// config: Default::default(),
// }
// }
// }
#[derive(Clone, Copy, serde::Deserialize, serde::Serialize, PartialEq, Eq, Hash)]
enum AppWindow {
None,
Flowchart,
Config,
}
impl AppWindow {
fn update(&self, state: &mut AppState, ui: &mut egui::Ui) {
match self {
AppWindow::None => {}
AppWindow::Flowchart => state.flowchart.paint(ui),
AppWindow::Config => state.config.update(ui),
}
}
}
+46
View File
@@ -0,0 +1,46 @@
use crate::app::{AppState, AppWindow};
#[derive(serde::Deserialize, serde::Serialize)]
pub struct WindowWrapper {
pub nr: usize,
pub window: AppWindow,
}
impl egui_tiles::Behavior<WindowWrapper> for AppState {
fn tab_title_for_pane(&mut self, pane: &WindowWrapper) -> egui::WidgetText {
format!("Pane {}", pane.nr).into()
}
fn pane_ui(
&mut self,
ui: &mut egui::Ui,
_tile_id: egui_tiles::TileId,
pane: &mut WindowWrapper,
) -> egui_tiles::UiResponse {
let mut ret = egui_tiles::UiResponse::None;
ui.horizontal(|ui| {
let titlebar = ui.interact(
ui.max_rect(),
ui.id().with(&format!("Pane_{}_sense", pane.nr)),
egui::Sense::drag(),
);
if titlebar.drag_started() {
ret = egui_tiles::UiResponse::DragStarted;
}
if titlebar.hovered() {
ui.ctx().set_cursor_icon(egui::CursorIcon::Grab);
}
let color = egui::epaint::Hsva::new(0.103 * pane.nr as f32, 0.5, 0.5, 1.0);
ui.painter().rect_filled(ui.max_rect(), 0.0, color);
ui.label("Test");
});
pane.window.update(self, ui);
ret
}
}