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
+29
View File
@@ -751,6 +751,25 @@ dependencies = [
"winit",
]
[[package]]
name = "egui_tiles"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ebfac3ca35f5e4fe217d3b03312111b234fe55ce059faf62b4cb47f7cf6d54f1"
dependencies = [
"ahash",
"egui",
"itertools",
"log",
"serde",
]
[[package]]
name = "either"
version = "1.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719"
[[package]]
name = "emath"
version = "0.33.2"
@@ -1300,6 +1319,15 @@ dependencies = [
"windows-sys 0.61.2",
]
[[package]]
name = "itertools"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285"
dependencies = [
"either",
]
[[package]]
name = "itoa"
version = "1.0.15"
@@ -2686,6 +2714,7 @@ dependencies = [
"eframe",
"egui",
"egui_extras",
"egui_tiles",
"log",
"pretty_env_logger",
"rand 0.9.2",
+1
View File
@@ -29,6 +29,7 @@ log = "0.4.27"
serde = { version = "1.0.219", features = ["derive"] }
egui_extras = "0.33.2"
rand = "0.9.2"
egui_tiles = "0.14.0"
# native:
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
@@ -1,30 +1,37 @@
use crate::{config::Config, flowchart::FlowChart};
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,
flowchart: FlowChart,
config: Config,
}
#[derive(serde::Deserialize, serde::Serialize, PartialEq, Eq)]
pub enum Tab {
Flowchart,
Test,
// 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 {
tab: Tab::Flowchart,
// Example stuff:
// label: "Hello World!".to_owned(),
// value: 2.7,
flowchart: FlowChart::new(),
config: Config::new(),
state: AppState::default(),
tree: egui_tiles::Tree::new_horizontal("tree_root", Vec::new()),
}
}
}
@@ -67,24 +74,14 @@ impl eframe::App for TemplateApp {
ui.menu_button("View", |ui| {
ui.label("View");
if ui
.selectable_label(self.tab == Tab::Flowchart, "Network")
.clicked()
{
self.tab = Tab::Flowchart;
}
if ui
.selectable_label(self.tab == Tab::Test, self.config.title())
.clicked()
{
self.tab = Tab::Test;
}
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
});
});
@@ -95,13 +92,8 @@ impl eframe::App for TemplateApp {
});
});
egui::CentralPanel::default().show(ctx, |ui| match self.tab {
Tab::Flowchart => {
self.flowchart.paint(ui);
}
Tab::Test => {
self.config.update(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
}
}
+11 -9
View File
@@ -15,8 +15,8 @@ enum ConfigState {
EditConfig(usize, PayloadConfig),
}
impl Config {
pub fn new() -> Self {
impl Default for Config {
fn default() -> Self {
Self {
state: ConfigState::Base,
current_payloads: vec![PayloadConfig {
@@ -26,14 +26,16 @@ impl Config {
}],
}
}
}
pub fn title(&self) -> &str {
match self.state {
ConfigState::Base => "Config",
ConfigState::NewConfig(..) => "Config/New",
ConfigState::EditConfig(..) => "Config/Edit",
}
}
impl Config {
// pub fn title(&self) -> &str {
// match self.state {
// ConfigState::Base => "Config",
// ConfigState::NewConfig(..) => "Config/New",
// ConfigState::EditConfig(..) => "Config/Edit",
// }
// }
pub fn update(&mut self, ui: &mut Ui) {
match &mut self.state {
-15
View File
@@ -35,10 +35,6 @@ impl DraggableContainer {
}
}
pub fn get_pos(&self, center: &Pos2) -> Pos2 {
center.clone() + self.pos
}
pub fn show<R>(
&mut self,
ui: &mut egui::Ui,
@@ -82,17 +78,6 @@ impl DraggableContainer {
let new_center = pointer_pos + self.drag_offset;
self.pos = new_center - clip_center;
}
// egui::Frame::default()
// .stroke(ui.visuals().widgets.noninteractive.bg_stroke)
// .corner_radius(ui.visuals().widgets.noninteractive.corner_radius)
// .show(ui, |ui| {
// ui.label(egui::RichText::new("Content").color(egui::Color32::WHITE));
// // self.frame.show(ui, |ui| {
// // ui.style_mut().wrap_mode = Some(egui::TextWrapMode::Extend);
// // ui.label(egui::RichText::new("Content").color(egui::Color32::WHITE));
// // });
// });
}
if response.drag_stopped() {
+9 -22
View File
@@ -1,8 +1,4 @@
use egui::Scene;
use egui::Shape;
use egui::Ui;
use egui::Vec2;
use egui::{Color32, Painter, Pos2, Rect};
use egui::{Color32, Painter, Pos2, Rect, Scene, Shape, Ui};
use crate::flowchart::CONNECTION_STROKE;
use crate::flowchart::GROUP_BORDER_MARGIN;
@@ -19,8 +15,8 @@ pub struct FlowChart {
pub groups: Vec<Vec<usize>>,
}
impl FlowChart {
pub fn new() -> Self {
impl Default for FlowChart {
fn default() -> Self {
let mut this = Self {
scene_rect: Rect::ZERO,
containers: vec![
@@ -41,7 +37,9 @@ impl FlowChart {
this
}
}
impl FlowChart {
fn paint_bg(rect: &Rect, painter: &Painter) {
let h_start = (rect.min.x / TARGET_LINE_GAP).round() as i32;
let h_end = ((rect.min.x + rect.width()) / TARGET_LINE_GAP).round() as i32 + 1;
@@ -56,19 +54,13 @@ impl FlowChart {
}
}
fn paint_groups(
rect: &Rect,
groups: &Vec<Vec<usize>>,
containers: &Vec<DraggableContainer>,
ui: &mut Ui,
) {
let center = rect.center();
fn paint_groups(groups: &Vec<Vec<usize>>, containers: &Vec<DraggableContainer>, ui: &mut Ui) {
for group in groups {
let mut points = Vec::new();
for n in group {
let container = &containers[*n];
let pos = container.get_pos(&Pos2::ZERO);
let pos = container.pos.to_pos2();
let size = container.size;
points.append(&mut vec![
Pos2 {
@@ -114,16 +106,11 @@ impl FlowChart {
let response = scene
.show(ui, &mut self.scene_rect, |mut ui| {
Self::paint_bg(rect, ui.painter());
Self::paint_groups(rect, groups, containers, &mut ui);
let center = Pos2::ZERO;
Self::paint_groups(groups, containers, &mut ui);
for (a, b) in &self.connections {
ui.painter().line_segment(
[
containers[*a].get_pos(&center),
containers[*b].get_pos(&center),
],
[containers[*a].pos.to_pos2(), containers[*b].pos.to_pos2()],
CONNECTION_STROKE,
);
}
+1 -1
View File
@@ -23,6 +23,6 @@ const GROUP_BORDER_MARGIN: f32 = 20.;
static REPULSION_STRENGTH: f32 = 100000.0; // repulsion_strength
static ATTRACTION_STRENGTH: f32 = 0.01; // attraction_strength
static CENTER_ATTRACTION_STRENGTH: f32 = 0.01; // attraction_strength
static GROUP_ATTRACTION_STRENGTH: f32 = 0.001; // attraction_strength
static GROUP_ATTRACTION_STRENGTH: f32 = 0.01; // attraction_strength
static REST_LENGTH: f32 = 50.0; // rest_length
static DAMPING: f32 = 0.9; // damping
+1 -4
View File
@@ -3,9 +3,6 @@
#[allow(unused_extern_crates)]
extern crate log;
mod app;
pub use app::TemplateApp;
pub mod app;
mod config;
mod flowchart;
+1 -1
View File
@@ -1,7 +1,7 @@
#![warn(clippy::all, rust_2018_idioms)]
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
use unshell_gui::TemplateApp;
use unshell_gui::app::TemplateApp;
// When compiling natively:
#[cfg(not(target_arch = "wasm32"))]