use std::{ops::RangeInclusive, sync::Arc};
use egui::{
Align2, CornerRadius, Galley, NumExt, Painter, Pos2, Rect, Stroke, StrokeKind, Vec2, Visuals,
epaint::TextShape, vec2,
};
use crate::{
Split, Style, TabDestination, TabStyle,
utils::{rect_set_size_centered, rect_stroke_box},
};
pub trait DockPainter {
fn tab_width(&self, tab: &TabMeasure<'_>) -> f32 {
let tab_style = tab.tab_style;
let text_width = tab.title.size().x + 2.0 * tab_style.padding.at_least(0.0);
tab_style
.minimum_width
.unwrap_or(0.0)
.at_least(text_width + tab.close_width)
}
fn tab_bar(&self, painter: &Painter, bar: &TabBarPaint<'_>) {
painter.rect_filled(
bar.rect,
bar.style.tab_bar.corner_radius,
bar.style.tab_bar.bg_fill,
);
}
fn tab(&self, painter: &Painter, tab: &TabPaint<'_>) {
let state = tab.state;
let selected = state.contains(PartState::SELECTED) || state.contains(PartState::DRAGGED);
let dragged = state.contains(PartState::DRAGGED);
let kb_focus = state.contains(PartState::FOCUSED);
let tab_style = tab.tab_style;
let look = if (selected && state.contains(PartState::LEAF_FOCUSED)) || dragged {
if kb_focus {
&tab_style.focused_with_kb_focus
} else {
&tab_style.focused
}
} else if selected {
if kb_focus {
&tab_style.active_with_kb_focus
} else {
&tab_style.active
}
} else if state.contains(PartState::HOVERED) {
&tab_style.hovered
} else if kb_focus {
&tab_style.inactive_with_kb_focus
} else {
&tab_style.inactive
};
let rect = tab.rect;
painter.rect_filled(rect, look.corner_radius, look.bg_fill);
let stroke_rect = rect_stroke_box(rect, 1.0);
painter.rect_stroke(
stroke_rect,
look.corner_radius,
Stroke::new(1.0_f32, look.outline_color),
StrokeKind::Inside,
);
if !dragged {
painter.hline(
RangeInclusive::new(
stroke_rect.min.x + f32::max(look.corner_radius.sw.into(), 1.5),
stroke_rect.max.x - f32::max(look.corner_radius.se.into(), 1.5),
),
stroke_rect.bottom(),
Stroke::new(2.0_f32, look.bg_fill),
);
}
let close_width = if tab.close.is_some() {
Style::TAB_CLOSE_BUTTON_SIZE.min(tab.style.tab_bar.height)
} else {
0.0
};
let mut text_rect = rect;
text_rect.set_width(text_rect.width() - close_width);
let x_spacing = tab_style.padding.at_least(0.0);
let text_pos = Align2::CENTER_CENTER.pos_in_rect(&text_rect.shrink2(vec2(x_spacing, 0.0)))
- tab.title.size() / 2.0;
painter.add(TextShape::new(
text_pos,
Arc::clone(tab.title),
look.text_color,
));
if let Some(close) = tab.close {
let buttons = &tab.style.buttons;
let lit = close.state.contains(PartState::HOVERED)
|| close.state.contains(PartState::FOCUSED);
if lit {
let mut corner_radius = look.corner_radius;
corner_radius.nw = 0;
corner_radius.sw = 0;
painter.rect_filled(close.rect, corner_radius, buttons.close_tab_bg_fill);
}
let color = if lit {
buttons.close_tab_active_color
} else {
buttons.close_tab_color
};
let mut x_rect = close.rect;
rect_set_size_centered(&mut x_rect, Vec2::splat(Style::TAB_CLOSE_X_SIZE));
painter.line_segment(
[x_rect.left_top(), x_rect.right_bottom()],
Stroke::new(1.0_f32, color),
);
painter.line_segment(
[x_rect.right_top(), x_rect.left_bottom()],
Stroke::new(1.0_f32, color),
);
}
if !dragged && (!selected || tab_style.hline_below_active_tab_name) {
let px = painter.pixels_per_point().recip();
painter.hline(
rect.x_range(),
tab.bar.bottom() - px,
(px, tab.style.tab_bar.hline_color),
);
}
}
fn tab_bar_tail(&self, painter: &Painter, tail: &TabBarTailPaint<'_>) {
let px = painter.pixels_per_point().recip();
painter.hline(
tail.rect.x_range(),
tail.rect.bottom() - px,
(px, tail.style.tab_bar.hline_color),
);
}
fn separator(&self, painter: &Painter, separator: &SeparatorPaint<'_>) {
let style = &separator.style.separator;
let state = separator.state;
let color = if state.contains(PartState::DRAGGED) {
style.color_dragged
} else if state.contains(PartState::HOVERED) || state.contains(PartState::FOCUSED) {
style.color_hovered
} else {
style.color_idle
};
painter.rect_filled(separator.rect, CornerRadius::ZERO, color);
}
fn drop_target(&self, painter: &Painter, target: &DropTargetPaint<'_>) {
let highlight = &target.style.overlay.hovered_leaf_highlight;
painter.rect(
target.rect.expand(highlight.expansion),
highlight.corner_radius,
highlight.color,
highlight.stroke,
StrokeKind::Inside,
);
}
fn drop_button(&self, painter: &Painter, button: &DropButtonPaint<'_>) {
let visuals = &button.style.overlay;
let button_stroke = Stroke::new(1.0_f32, visuals.button_color);
painter.rect_stroke(
button.rect,
0.0,
visuals.button_border_stroke,
StrokeKind::Inside,
);
let rect = button.rect.shrink(button.rect.width() * 0.1);
painter.rect_stroke(rect, 0.0, button_stroke, StrokeKind::Inside);
let rim = Rect::from_two_pos(rect.min, rect.lerp_inside(vec2(1.0, 0.1)));
painter.rect(
rim,
0.0,
visuals.button_color,
Stroke::NONE,
StrokeKind::Inside,
);
if let Some(split) = button.split {
for line in DASHED_LINE_ALPHAS.chunks(2) {
let start = rect.lerp_inside(lerp_vec(split, line[0]));
let end = rect.lerp_inside(lerp_vec(split, line[1]));
painter.line_segment([start, end], button_stroke);
}
}
if button.hovered {
let vertical_alphas = vec2(1.0, 0.5);
let horizontal_alphas = vec2(0.5, 1.0);
let rect = match button.split {
Some(Split::Above) => Rect::from_min_size(rect.min, rect.size() * vertical_alphas),
Some(Split::Left) => Rect::from_min_size(rect.min, rect.size() * horizontal_alphas),
Some(Split::Below) => {
let min = rect.lerp_inside(lerp_vec(Split::Below, 0.0));
Rect::from_min_size(min, rect.size() * vertical_alphas)
}
Some(Split::Right) => {
let min = rect.lerp_inside(lerp_vec(Split::Right, 0.0));
Rect::from_min_size(min, rect.size() * horizontal_alphas)
}
None => rect,
};
painter.rect_filled(rect, 0.0, visuals.selection_color);
}
}
fn drop_preview(&self, painter: &Painter, drop: &DropPaint<'_>) {
let overlay = &drop.style.overlay;
if let TabDestination::Window(_) = drop.destination {
painter.rect_stroke(
drop.rect,
0.0,
Stroke::new(overlay.selection_stroke_width, overlay.selection_color),
StrokeKind::Inside,
);
} else if !drop.from_buttons {
painter.rect_filled(drop.rect, 0.0, overlay.selection_color);
}
let Some(refusal) = drop.refusal else {
return;
};
let visuals = drop.visuals;
let color = visuals.error_fg_color;
painter.rect(
drop.rect,
0.0,
color.gamma_multiply(0.25),
Stroke::new(overlay.selection_stroke_width.max(1.0), color),
StrokeKind::Inside,
);
if let Some(reason) = &refusal.reason {
let margin = vec2(6.0, 4.0);
let min = refusal.pointer + vec2(16.0, 16.0);
let frame = Rect::from_min_size(min, reason.size() + 2.0 * margin);
painter.rect(
frame,
visuals.menu_corner_radius,
visuals.window_fill(),
Stroke::new(1.0, color),
StrokeKind::Inside,
);
painter.galley(min + margin, Arc::clone(reason), visuals.text_color());
}
}
}
#[derive(Clone, Copy, Debug, Default)]
pub struct StylePainter;
impl DockPainter for StylePainter {}
const DASHED_LINE_ALPHAS: [f32; 8] = [
0.0625, 0.1875, 0.3125, 0.4375, 0.5625, 0.6875, 0.8125, 0.9375,
];
#[inline(always)]
const fn lerp_vec(split: Split, alpha: f32) -> Vec2 {
if split.is_top_bottom() {
vec2(alpha, 0.5)
} else {
vec2(0.5, alpha)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct PartState(u8);
impl PartState {
pub const NONE: Self = Self(0);
pub const SELECTED: Self = Self(1);
pub const HOVERED: Self = Self(1 << 1);
pub const PRESSED: Self = Self(1 << 2);
pub const FOCUSED: Self = Self(1 << 3);
pub const DRAGGED: Self = Self(1 << 4);
pub const LEAF_FOCUSED: Self = Self(1 << 5);
#[inline]
pub const fn contains(self, other: Self) -> bool {
self.0 & other.0 == other.0
}
#[inline]
#[must_use]
pub const fn with(self, flag: Self, on: bool) -> Self {
if on {
Self(self.0 | flag.0)
} else {
Self(self.0 & !flag.0)
}
}
}
impl std::ops::BitOr for PartState {
type Output = Self;
#[inline]
fn bitor(self, rhs: Self) -> Self {
Self(self.0 | rhs.0)
}
}
impl std::ops::BitOrAssign for PartState {
#[inline]
fn bitor_assign(&mut self, rhs: Self) {
self.0 |= rhs.0;
}
}
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub struct TabMeasure<'a> {
pub title: &'a Arc<Galley>,
pub state: PartState,
pub close_width: f32,
pub style: &'a Style,
pub tab_style: &'a TabStyle,
}
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub struct TabBarPaint<'a> {
pub rect: Rect,
pub scrolled: f32,
pub style: &'a Style,
}
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub struct TabBarTailPaint<'a> {
pub rect: Rect,
pub bar: Rect,
pub style: &'a Style,
}
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub struct TabPaint<'a> {
pub rect: Rect,
pub bar: Rect,
pub title: &'a Arc<Galley>,
pub state: PartState,
pub close: Option<TabClose>,
pub style: &'a Style,
pub tab_style: &'a TabStyle,
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub struct TabClose {
pub rect: Rect,
pub state: PartState,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum SeparatorAxis {
Vertical,
Horizontal,
}
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub struct SeparatorPaint<'a> {
pub rect: Rect,
pub axis: SeparatorAxis,
pub state: PartState,
pub style: &'a Style,
}
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub struct DropTargetPaint<'a> {
pub rect: Rect,
pub style: &'a Style,
}
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub struct DropButtonPaint<'a> {
pub rect: Rect,
pub split: Option<Split>,
pub hovered: bool,
pub style: &'a Style,
}
#[derive(Clone, Copy)]
#[non_exhaustive]
pub struct DropPaint<'a> {
pub rect: Rect,
pub destination: &'a TabDestination,
pub from_buttons: bool,
pub refusal: Option<&'a DropRefusal>,
pub style: &'a Style,
pub visuals: &'a Visuals,
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct DropRefusal {
pub reason: Option<Arc<Galley>>,
pub pointer: Pos2,
}