From acc35f78fac297404fcd92e7c02dcac104c26be9 Mon Sep 17 00:00:00 2001 From: p6nj Date: Wed, 1 May 2024 20:49:48 +0200 Subject: [PATCH] named tabs --- Cargo.toml | 3 ++- src/app.rs | 11 +++++---- src/app/tabs.rs | 62 +++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 1 - src/tabs.rs | 43 ---------------------------------- 5 files changed, 71 insertions(+), 49 deletions(-) create mode 100644 src/app/tabs.rs delete mode 100644 src/tabs.rs diff --git a/Cargo.toml b/Cargo.toml index 02a1790..eb99195 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,10 +8,11 @@ license = "Unlicense" [dependencies] anyhow = "1.0" -derive-new = "0.6" +derive-new = "0.6.0" eframe = "0.27" egui_dock = "0.12" rfd = "0.14" +strum = { version = "0.26", features = ["derive"] } [workspace] members = ["bingus"] diff --git a/src/app.rs b/src/app.rs index ea759ad..475d97d 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,8 +1,11 @@ +use derive_new::new; use eframe::egui; use egui_dock::{DockArea, DockState, Style}; -use crate::tabs::{Tab, TabViewer}; +mod tabs; +use tabs::{chefs_dish, Tab, TabViewer}; +#[derive(new)] pub(super) struct App { tree: DockState, } @@ -11,14 +14,14 @@ impl eframe::App for App { fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { DockArea::new(&mut self.tree) .style(Style::from_egui(ctx.style().as_ref())) - .show(ctx, &mut TabViewer); + .show(ctx, &mut TabViewer::new()); } } impl Default for App { fn default() -> Self { - let tree = DockState::new(vec![Tab::new("dummy"), Tab::new("hi")]); + let tree = DockState::new(chefs_dish()); - Self { tree } + Self::new(tree) } } diff --git a/src/app/tabs.rs b/src/app/tabs.rs new file mode 100644 index 0000000..f3c2729 --- /dev/null +++ b/src/app/tabs.rs @@ -0,0 +1,62 @@ +use derive_new::new; +use eframe::egui::{Ui, WidgetText}; +use strum::{AsRefStr, EnumIter, IntoEnumIterator}; + +#[derive(Clone, AsRefStr, PartialEq, EnumIter)] +enum TabKind { + Input, + Decode, + Process, + Encode, + Output, + Summary, + Help, +} + +#[derive(new)] +pub(super) struct Tab { + #[allow(private_interfaces)] + pub(super) kind: TabKind, +} + +impl TabKind { + fn is_optional(&self) -> bool { + match self { + Self::Input | Self::Decode | Self::Process | Self::Encode | Self::Output => false, + _ => true, + } + } +} + +impl Tab { + fn is_optional(&self) -> bool { + self.kind.is_optional() + } +} + +pub(super) fn chefs_dish() -> Vec { + TabKind::iter().take(5).map(|kind| Tab::new(kind)).collect() +} + +#[derive(new)] +pub(super) struct TabViewer {} + +impl egui_dock::TabViewer for TabViewer { + type Tab = Tab; + + fn title(&mut self, tab: &mut Self::Tab) -> WidgetText { + tab.kind.as_ref().into() + } + + fn ui(&mut self, ui: &mut Ui, tab: &mut Self::Tab) { + ui.heading(tab.kind.as_ref()); + } + + fn allowed_in_windows(&self, tab: &mut Self::Tab) -> bool { + tab.is_optional() + } + + fn closeable(&mut self, tab: &mut Self::Tab) -> bool { + tab.is_optional() + } +} diff --git a/src/main.rs b/src/main.rs index 4890c9d..2dcc362 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,6 @@ use eframe::egui; mod app; use app::App; -mod tabs; fn main() -> Result<(), eframe::Error> { let options = eframe::NativeOptions { diff --git a/src/tabs.rs b/src/tabs.rs deleted file mode 100644 index ffc98f7..0000000 --- a/src/tabs.rs +++ /dev/null @@ -1,43 +0,0 @@ -use std::fmt::Display; - -use derive_new::new; -use eframe::egui::{Ui, WidgetText}; - -#[derive(new)] -pub(crate) struct Tab { - name: &'static str, -} - -impl Display for Tab { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self.name) - } -} - -pub(crate) struct TabViewer; - -impl egui_dock::TabViewer for TabViewer { - type Tab = Tab; - - fn title(&mut self, tab: &mut Self::Tab) -> WidgetText { - format!("{tab}").into() - } - fn ui(&mut self, ui: &mut Ui, tab: &mut Self::Tab) { - ui.label(format!("Content of {tab}")); - } - fn allowed_in_windows(&self, _tab: &mut Self::Tab) -> bool { - false - } - fn closeable(&mut self, _tab: &mut Self::Tab) -> bool { - false - } - fn context_menu( - &mut self, - _ui: &mut Ui, - _tab: &mut Self::Tab, - _surface: egui_dock::SurfaceIndex, - _node: egui_dock::NodeIndex, - ) { - } - fn on_add(&mut self, _surface: egui_dock::SurfaceIndex, _node: egui_dock::NodeIndex) {} -}