named tabs

This commit is contained in:
Ponj 2024-05-01 20:49:48 +02:00
parent b986200de1
commit acc35f78fa
Signed by: p6nj
SSH key fingerprint: SHA256:cWPAu6zlgzB/97Sa9i4h+BYrv/NQCDZaHWDy2/DInbc
5 changed files with 71 additions and 49 deletions

View file

@ -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"]

View file

@ -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<Tab>,
}
@ -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)
}
}

62
src/app/tabs.rs Normal file
View file

@ -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<Tab> {
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()
}
}

View file

@ -3,7 +3,6 @@ use eframe::egui;
mod app;
use app::App;
mod tabs;
fn main() -> Result<(), eframe::Error> {
let options = eframe::NativeOptions {

View file

@ -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) {}
}