use egui::containers::{
PanelState,
menu::{MenuButton, MenuConfig},
};
use egui::{CentralPanel, Context, Panel, PopupCloseBehavior, Rect, ScrollArea, Ui, Vec2};
pub trait Pane: Copy + PartialEq + std::fmt::Debug + 'static {
const ALL: &'static [Self];
fn title(self) -> &'static str;
fn column(self) -> Column;
fn default_size(self) -> f32;
fn id(self) -> &'static str;
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[must_use]
pub enum Column {
Center,
Right,
Bottom,
}
impl Column {
pub const ALL: [Self; 3] = [Self::Bottom, Self::Right, Self::Center];
const fn default_size(self) -> f32 {
match self {
Self::Center | Self::Right => 380.0,
Self::Bottom => 200.0,
}
}
pub fn tiling<P: Pane>(self, open: &[P]) -> Option<(Vec<P>, P)> {
let mut panes = P::ALL
.iter()
.copied()
.filter(|pane| pane.column() == self && open.contains(pane))
.collect::<Vec<_>>();
let filling = panes.pop()?;
Some((panes, filling))
}
const fn id(self) -> &'static str {
match self {
Self::Center => "column_center",
Self::Right => "column_right",
Self::Bottom => "column_bottom",
}
}
fn scoped_id(self, window: &str) -> egui::Id {
egui::Id::new(window).with(self.id())
}
}
pub fn reset_layout<P: Pane>(ctx: &Context, window: &str) {
ctx.data_mut(|data| {
for pane in P::ALL {
data.remove::<PanelState>(egui::Id::new(window).with(pane.id()));
}
for column in Column::ALL {
data.remove::<PanelState>(column.scoped_id(window));
}
});
}
pub fn view_menu<P: Pane>(ui: &mut Ui, window: &str, open: &[P]) -> Option<(P, bool)> {
let mut toggled = None;
MenuButton::new("View")
.config(MenuConfig::new().close_behavior(PopupCloseBehavior::CloseOnClickOutside))
.ui(ui, |ui| {
for pane in P::ALL.iter().copied() {
let mut is_open = open.contains(&pane);
if ui.checkbox(&mut is_open, pane.title()).changed() {
toggled = Some((pane, is_open));
}
}
ui.separator();
if ui
.button("Reset layout")
.on_hover_text("Restore panes to their default sizes")
.clicked()
{
reset_layout::<P>(ui.ctx(), window);
ui.close();
}
});
toggled
}
pub fn columns<P: Pane>(
ui: &mut Ui,
window: &str,
open: &[P],
size: &dyn Fn(P) -> f32,
body: &mut dyn FnMut(&mut Ui, P),
) -> Option<P> {
let mut panes = Panes {
window,
open,
size,
body,
closed: None,
};
let fill = Column::ALL
.iter()
.copied()
.rev()
.find(|column| column.tiling(open).is_some());
let mut rest = ui.available_rect_before_wrap();
let bottom = column(ui, &mut panes, Column::Bottom, fill == Some(Column::Bottom));
if let Some(bottom) = bottom {
rest.max.y = bottom.top();
}
right_column(ui, &mut panes, rest.height(), fill == Some(Column::Right));
column(ui, &mut panes, Column::Center, true);
panes.closed
}
struct Panes<'a, P> {
window: &'a str,
open: &'a [P],
size: &'a dyn Fn(P) -> f32,
body: &'a mut dyn FnMut(&mut Ui, P),
closed: Option<P>,
}
fn column<P: Pane>(
ui: &mut Ui,
panes: &mut Panes<'_, P>,
column: Column,
fill: bool,
) -> Option<Rect> {
let (sized, filling) = column.tiling(panes.open)?;
let window = panes.window;
let size = panes.size;
let tile = |ui: &mut Ui, panes: &mut Panes<'_, P>| {
for pane in &sized {
let close = Panel::top(egui::Id::new(window).with(pane.id()))
.resizable(true)
.default_size(size(*pane))
.show(ui, |ui| heading(ui, *pane, panes.body))
.inner;
if close {
panes.closed = Some(*pane);
}
}
if CentralPanel::default()
.show(ui, |ui| heading(ui, filling, panes.body))
.inner
{
panes.closed = Some(filling);
}
};
match column {
Column::Center => tile(ui, panes),
Column::Bottom if fill => tile(ui, panes),
Column::Bottom => {
Panel::bottom(column.scoped_id(window))
.resizable(true)
.default_size(column.default_size())
.show(ui, |ui| tile(ui, panes));
}
Column::Right => unreachable!("the right column is drawn by `right_column`"),
}
if fill {
return None;
}
PanelState::load(ui.ctx(), column.scoped_id(window)).map(|state| state.outer_rect)
}
fn right_column<P: Pane>(ui: &mut Ui, panes: &mut Panes<'_, P>, height: f32, fill: bool) {
let column = Column::Right;
let Some((sized, filling)) = column.tiling(panes.open) else {
return;
};
let window = panes.window;
let size = panes.size;
let stacked = sized.into_iter().chain([filling]).collect::<Vec<_>>();
let stack = |ui: &mut Ui| {
ScrollArea::vertical()
.id_salt(column.scoped_id(window))
.max_height(height)
.auto_shrink([false, false])
.show(ui, |ui| {
let bar = ui.spacing().scroll.bar_width + ui.spacing().scroll.bar_outer_margin;
let last = stacked.len() - 1;
for (index, pane) in stacked.into_iter().enumerate() {
let height = if index == last {
ui.available_height().max(size(pane))
} else {
size(pane)
};
let width = ui.available_width() - bar;
ui.allocate_ui(Vec2::new(width, height), |ui| {
ui.set_min_height(height);
if heading(ui, pane, panes.body) {
panes.closed = Some(pane);
}
});
if index != last {
ui.separator();
}
}
});
};
if fill {
CentralPanel::default().show(ui, stack);
} else {
Panel::right(column.scoped_id(window))
.default_size(column.default_size())
.show(ui, stack);
}
}
fn heading<P: Pane>(ui: &mut Ui, pane: P, body: &mut dyn FnMut(&mut Ui, P)) -> bool {
let closed = ui
.horizontal(|ui| {
ui.strong(pane.title());
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
ui.small_button("✖").on_hover_text("Close pane").clicked()
})
.inner
})
.inner;
body(ui, pane);
closed
}