use std::{ops::RangeInclusive, sync::Arc};
use egui::{
Align2, CornerRadius, Galley, Id, NumExt, Painter, Pos2, Rect, Stroke, StrokeKind, Vec2,
Visuals, epaint::TextShape, vec2,
};
use crate::{
OverlayType, 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<'_>) {
let attached = if bar.folded {
Attached::Nothing
} else {
Attached::Below
};
painter.rect_filled(
bar.rect,
attached.corner_radius(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;
let attached = if dragged || tab.folded {
Attached::Nothing
} else {
Attached::Below
};
let corner_radius = attached.corner_radius(look.corner_radius);
painter.rect_filled(rect, corner_radius, look.bg_fill);
if let Some(close) = tab.close
&& (close.state.contains(PartState::HOVERED)
|| close.state.contains(PartState::FOCUSED))
{
painter.rect_filled(
close.rect,
corners_within(close.rect, rect, corner_radius),
tab.style.buttons.close_tab_bg_fill,
);
}
let stroke_rect = rect_stroke_box(rect, 1.0);
painter.rect_stroke(
stroke_rect,
corner_radius,
Stroke::new(1.0_f32, look.outline_color),
StrokeKind::Inside,
);
if attached == Attached::Below {
painter.hline(
RangeInclusive::new(
stroke_rect.min.x + f32::max(corner_radius.sw.into(), 1.5),
stroke_rect.max.x - f32::max(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);
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 attached == Attached::Below && (!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<'_>) {
if tail.folded {
return;
}
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 tab_bar_grip(&self, painter: &Painter, grip: &TabBarGripPaint<'_>) {
let buttons = &grip.style.buttons;
let active = grip.state.contains(PartState::HOVERED)
|| grip.state.contains(PartState::FOCUSED)
|| grip.state.contains(PartState::DRAGGED);
if active {
painter.rect_filled(grip.rect, grip.corner_radius, buttons.group_grip_bg_fill);
}
let color = if active {
buttons.group_grip_active_color
} else {
buttons.group_grip_color
};
let center = grip.rect.center();
for column in [-2.0, 2.0] {
for row in [-4.0, 0.0, 4.0] {
painter.circle_filled(center + vec2(column, row), 1.0, color);
}
}
}
fn tab_body(&self, painter: &Painter, body: &TabBodyPaint<'_>) {
let stroke = body.tab_style.tab_body.stroke;
if stroke.is_empty() {
return;
}
let effective_stroke_width = (stroke.width / 2.0).ceil() * 2.0;
let rect = Rect::from_min_max(
body.rect.min - vec2(0.0, effective_stroke_width),
body.rect.max,
);
painter.rect_stroke(
rect_stroke_box(rect, stroke.width),
body.corner_radius,
stroke,
StrokeKind::Inside,
);
}
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 {
style
.color_idle
.lerp_to_gamma(style.color_hovered, separator.highlight)
};
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 window_outline(&self, painter: &Painter, outline: &WindowOutlinePaint<'_>) {
let stroke = outline.style.overlay.zone_stroke;
let stroke = if outline.refused {
Stroke::new(stroke.width.max(1.0), outline.visuals.error_fg_color)
} else {
stroke
};
painter.rect_stroke(
outline.rect,
outline.style.overlay.zone_corner_radius,
stroke,
StrokeKind::Inside,
);
}
fn dragged_group(&self, painter: &Painter, group: &DraggedGroupPaint<'_>) {
let style = group.style;
let painter = {
let mut painter = painter.clone();
painter.multiply_opacity(GHOST_OPACITY);
painter
};
let corner_radius = style.tab_bar.corner_radius;
painter.add(
group
.visuals
.popup_shadow
.as_shape(group.rect, corner_radius),
);
if !group.body_painted {
painter.rect_filled(group.body, CornerRadius::ZERO, style.tab.tab_body.bg_fill);
}
self.tab_bar(
&painter,
&TabBarPaint {
rect: group.bar,
scrolled: 0.0,
folded: false,
style,
},
);
for tab in &group.tabs {
self.tab(
&painter,
&TabPaint {
rect: tab.rect,
bar: group.bar,
title: &tab.title,
state: tab.state,
close: None,
folded: false,
style,
tab_style: &style.tab,
},
);
}
if let Some(more) = &group.more {
painter.add(TextShape::new(
Align2::CENTER_CENTER.pos_in_rect(&more.rect) - more.label.size() / 2.0,
Arc::clone(&more.label),
group.visuals.weak_text_color(),
));
}
painter.rect_stroke(
group.rect,
corner_radius,
group.visuals.window_stroke,
StrokeKind::Inside,
);
}
fn dragged_tab(&self, painter: &Painter, tab: &DraggedGroupPaint<'_>) {
self.dragged_group(painter, tab);
}
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 overlay.overlay_type == OverlayType::Zones {
painter.rect(
drop.rect,
overlay.zone_corner_radius,
overlay.zone_fill,
overlay.zone_stroke,
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());
}
}
fn restore_band(&self, painter: &Painter, band: &RestoreBandPaint<'_>) {
let overlay = &band.style.overlay;
let color = overlay.selection_color;
let rect = band.rect;
if band.hovered {
painter.rect_filled(rect, CornerRadius::same(2), color);
return;
}
painter.rect_filled(rect, CornerRadius::same(2), color.gamma_multiply(0.25));
let along_x = rect.width() >= rect.height();
let (len, start) = if along_x {
(rect.width(), rect.left())
} else {
(rect.height(), rect.top())
};
let (dash, gap) = (6.0, 4.0);
let stroke = Stroke::new(2.0, color);
let mut at = 0.0;
while at < len {
let end = (at + dash).min(len);
let (a, b) = if along_x {
let y = rect.center().y;
(Pos2::new(start + at, y), Pos2::new(start + end, y))
} else {
let x = rect.center().x;
(Pos2::new(x, start + at), Pos2::new(x, start + end))
};
painter.line_segment([a, b], stroke);
at += dash + gap;
}
}
fn section_band(&self, painter: &Painter, band: &SectionBandPaint<'_>) {
let visuals = &band.style.visuals;
let state = band.state;
let rect = band.rect;
let look = if state.contains(PartState::PRESSED) {
&visuals.widgets.active
} else if state.contains(PartState::HOVERED) {
&visuals.widgets.hovered
} else {
&visuals.widgets.inactive
};
let fill = if state.contains(PartState::HOVERED) || state.contains(PartState::PRESSED) {
look.weak_bg_fill
} else {
visuals.faint_bg_color
};
painter.rect_filled(rect, CornerRadius::ZERO, fill);
let px = painter.pixels_per_point().recip();
painter.hline(
rect.x_range(),
rect.top() + px / 2.0,
(px, visuals.widgets.noninteractive.bg_stroke.color),
);
let padding = band.style.spacing.item_spacing.x.max(4.0);
let text_color = look.fg_stroke.color;
let side = (rect.height() * 0.4).clamp(4.0, 10.0);
let mark = Rect::from_center_size(
Pos2::new(rect.left() + padding + side / 2.0, rect.center().y),
Vec2::splat(side),
);
let points = if band.open {
vec![mark.left_top(), mark.right_top(), mark.center_bottom()]
} else {
vec![mark.left_top(), mark.right_center(), mark.left_bottom()]
};
painter.add(egui::Shape::convex_polygon(
points,
text_color,
Stroke::NONE,
));
let mut end = rect.right() - padding;
if let Some(count) = band.count_text() {
let font = egui::TextStyle::Small.resolve(band.style);
let galley = painter.layout_no_wrap(count, font, text_color);
let pill_size = galley.size() + vec2(8.0, 2.0);
let pill = Rect::from_min_size(
Pos2::new(end - pill_size.x, rect.center().y - pill_size.y / 2.0),
pill_size,
);
painter.rect_filled(pill, pill.height() / 2.0, visuals.widgets.inactive.bg_fill);
painter.galley(pill.center() - galley.size() / 2.0, galley, text_color);
end = pill.left() - padding;
}
let start = mark.right() + padding;
let font = egui::TextStyle::Button.resolve(band.style);
let galley = painter.layout_no_wrap(band.title.to_owned(), font, text_color);
let room = Rect::from_x_y_ranges(start..=end.max(start), rect.y_range());
painter
.with_clip_rect(room.intersect(painter.clip_rect()))
.galley(
Pos2::new(start, rect.center().y - galley.size().y / 2.0),
galley,
text_color,
);
if state.contains(PartState::FOCUSED) {
painter.rect_stroke(
rect.shrink(1.0),
CornerRadius::ZERO,
visuals.selection.stroke,
StrokeKind::Inside,
);
}
}
fn section_boundary(&self, painter: &Painter, boundary: &SectionBoundaryPaint<'_>) {
let visuals = &boundary.style.visuals;
let state = boundary.state;
if state.contains(PartState::FOCUSED) {
painter.rect_stroke(
boundary.rect,
CornerRadius::ZERO,
visuals.selection.stroke,
StrokeKind::Inside,
);
} else if boundary.highlight > 0.0 {
let stroke = visuals.widgets.hovered.bg_stroke;
painter.hline(
boundary.rect.x_range(),
boundary.line,
Stroke::new(
stroke.width,
stroke.color.gamma_multiply(boundary.highlight),
),
);
}
}
fn tool_item(&self, painter: &Painter, item: &ToolItemPaint<'_>) {
let visuals = &item.style.visuals;
let state = item.state;
let enabled = !state.contains(PartState::DISABLED);
let on = state.contains(PartState::SELECTED);
let radius = visuals.widgets.inactive.corner_radius;
let look = if state.contains(PartState::PRESSED) {
&visuals.widgets.active
} else {
&visuals.widgets.hovered
};
let lit =
enabled && (state.contains(PartState::HOVERED) || state.contains(PartState::PRESSED));
let framed = on && !item.in_menu;
if framed {
painter.rect(
item.rect,
radius,
visuals.selection.bg_fill,
Stroke::new(1.0, visuals.selection.stroke.color),
StrokeKind::Inside,
);
} else if lit {
painter.rect_filled(item.rect, radius, look.weak_bg_fill);
}
let color = if !enabled {
visuals.weak_text_color()
} else if framed {
visuals.selection.stroke.color
} else if lit {
look.fg_stroke.color
} else {
visuals.widgets.inactive.fg_stroke.color
};
if item.busy {
paint_spinner(painter, item.icon_rect, color);
} else if let Some(icon) = item.icon {
icon.paint(painter, item.icon_rect, color, enabled);
}
if let Some(label) = item.label {
painter.galley(item.label_pos, Arc::clone(label), color);
}
if let Some(shortcut) = item.shortcut {
let weak = if enabled {
visuals.weak_text_color()
} else {
color
};
painter.galley(item.shortcut_pos, Arc::clone(shortcut), weak);
}
if let Some(mark) = item.mark {
let stroke = Stroke::new(1.5, color);
if item.kind == crate::toolbar::ToolKind::Menu {
let points = if item.in_menu {
vec![mark.left_top(), mark.right_center(), mark.left_bottom()]
} else {
let m = mark.shrink2(vec2(0.0, mark.height() / 4.0));
vec![m.left_top(), m.right_top(), m.center_bottom()]
};
painter.add(egui::Shape::convex_polygon(points, color, Stroke::NONE));
} else if on {
let m = mark.shrink(mark.width() / 8.0);
painter.line(
vec![
m.lerp_inside(vec2(0.0, 0.55)),
m.lerp_inside(vec2(0.4, 0.9)),
m.lerp_inside(vec2(1.0, 0.1)),
],
stroke,
);
}
}
if state.contains(PartState::FOCUSED) {
painter.rect_stroke(
item.rect.shrink(1.0),
radius,
visuals.selection.stroke,
StrokeKind::Inside,
);
}
}
fn tool_more(&self, painter: &Painter, more: &ToolMorePaint<'_>) {
let visuals = &more.style.visuals;
let state = more.state;
let radius = visuals.widgets.inactive.corner_radius;
let open = state.contains(PartState::SELECTED);
let look = if state.contains(PartState::PRESSED) || open {
&visuals.widgets.active
} else {
&visuals.widgets.hovered
};
let lit = open || state.contains(PartState::HOVERED) || state.contains(PartState::PRESSED);
if lit {
painter.rect_filled(more.rect, radius, look.weak_bg_fill);
}
let color = if lit {
look.fg_stroke.color
} else {
visuals.widgets.inactive.fg_stroke.color
};
let r = (more.rect.height().min(more.rect.width()) / 14.0).max(1.0);
let step = if more.vertical {
vec2(0.0, 3.0 * r)
} else {
vec2(3.0 * r, 0.0)
};
let c = more.rect.center();
for at in [c - step, c, c + step] {
painter.circle_filled(at, r, color);
}
if state.contains(PartState::FOCUSED) {
painter.rect_stroke(
more.rect.shrink(1.0),
radius,
visuals.selection.stroke,
StrokeKind::Inside,
);
}
}
fn tool_separator(&self, painter: &Painter, separator: &ToolSeparatorPaint<'_>) {
let rect = separator.rect;
let stroke = separator.style.visuals.widgets.noninteractive.bg_stroke;
if separator.vertical {
let inset = rect.width() / 8.0;
painter.hline(
rect.left() + inset..=rect.right() - inset,
rect.center().y,
stroke,
);
} else {
let inset = rect.height() / 8.0;
painter.vline(
rect.center().x,
rect.top() + inset..=rect.bottom() - inset,
stroke,
);
}
}
}
fn paint_spinner(painter: &Painter, rect: Rect, color: egui::Color32) {
let ctx = painter.ctx();
ctx.request_repaint();
let time = ctx.input(|i| i.time);
let radius = rect.width().min(rect.height()) / 2.0 - 1.0;
let start = time * std::f64::consts::TAU;
let end = start + 240f64.to_radians() * time.sin();
let n = 20;
let points: Vec<Pos2> = (0..n)
.map(|i| {
let angle = start + (end - start) * i as f64 / n as f64;
let (sin, cos) = angle.sin_cos();
rect.center() + radius * vec2(cos as f32, sin as f32)
})
.collect();
painter.add(egui::Shape::line(points, Stroke::new(2.0, 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);
pub const DISABLED: Self = Self(1 << 6);
#[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)
}
}
}
pub(crate) fn line_highlight(ui: &egui::Ui, response: &egui::Response) -> f32 {
let active = response.is_pointer_button_down_on() || response.dragged() || response.has_focus();
ui.delayed_highlight(
response.id,
egui::HighlightState::new(response.hovered(), active),
egui::FadeOut::Instant,
)
}
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 folded: bool,
pub style: &'a Style,
}
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub struct TabBarTailPaint<'a> {
pub rect: Rect,
pub bar: Rect,
pub folded: bool,
pub style: &'a Style,
}
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub struct TabBarGripPaint<'a> {
pub rect: Rect,
pub corner_radius: CornerRadius,
pub state: PartState,
pub folded: bool,
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 folded: bool,
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 highlight: f32,
pub style: &'a Style,
}
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub struct TabBodyPaint<'a> {
pub rect: Rect,
pub corner_radius: CornerRadius,
pub state: PartState,
pub style: &'a Style,
pub tab_style: &'a TabStyle,
}
#[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 WindowOutlinePaint<'a> {
pub rect: Rect,
pub refused: bool,
pub style: &'a Style,
pub visuals: &'a Visuals,
}
pub(crate) const GHOST_OPACITY: f32 = 0.7;
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct DraggedGroupPaint<'a> {
pub rect: Rect,
pub bar: Rect,
pub body: Rect,
pub tabs: Vec<DraggedGroupTab>,
pub more: Option<DraggedGroupMore>,
pub count: usize,
pub body_painted: bool,
pub style: &'a Style,
pub visuals: &'a Visuals,
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct DraggedGroupTab {
pub rect: Rect,
pub title: Arc<Galley>,
pub state: PartState,
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct DraggedGroupMore {
pub rect: Rect,
pub hidden: usize,
pub label: Arc<Galley>,
}
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub struct RestoreBandPaint<'a> {
pub rect: Rect,
pub zone: &'a crate::layout::ZoneName,
pub hovered: bool,
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,
}
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub struct SectionBandPaint<'a> {
pub rect: Rect,
pub id: Id,
pub title: &'a str,
pub count: Option<u64>,
pub open: bool,
pub state: PartState,
pub style: &'a egui::Style,
}
impl SectionBandPaint<'_> {
pub fn count_text(&self) -> Option<String> {
self.count.map(|count| {
if count > 99 {
"99+".to_owned()
} else {
count.to_string()
}
})
}
}
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub struct SectionBoundaryPaint<'a> {
pub rect: Rect,
pub line: f32,
pub id: Id,
pub state: PartState,
pub highlight: f32,
pub style: &'a egui::Style,
}
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub struct ToolItemPaint<'a> {
pub rect: Rect,
pub id: Id,
pub kind: crate::toolbar::ToolKind,
pub icon: Option<&'a crate::toolbar::ToolIcon<'a>>,
pub icon_rect: Rect,
pub label: Option<&'a Arc<Galley>>,
pub label_pos: Pos2,
pub shortcut: Option<&'a Arc<Galley>>,
pub shortcut_pos: Pos2,
pub mark: Option<Rect>,
pub busy: bool,
pub in_menu: bool,
pub state: PartState,
pub style: &'a egui::Style,
}
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub struct ToolMorePaint<'a> {
pub rect: Rect,
pub id: Id,
pub vertical: bool,
pub state: PartState,
pub style: &'a egui::Style,
}
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub struct ToolSeparatorPaint<'a> {
pub rect: Rect,
pub vertical: bool,
pub style: &'a egui::Style,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Attached {
Below,
Above,
Nothing,
}
impl Attached {
pub(crate) fn corner_radius(self, radius: CornerRadius) -> CornerRadius {
match self {
Self::Below => CornerRadius {
sw: 0,
se: 0,
..radius
},
Self::Above => CornerRadius {
nw: 0,
ne: 0,
..radius
},
Self::Nothing => CornerRadius {
sw: radius.sw.max(radius.nw),
se: radius.se.max(radius.ne),
..radius
},
}
}
}
pub(crate) fn corners_within(part: Rect, outer: Rect, outer_radius: CornerRadius) -> CornerRadius {
let near = |a: f32, b: f32| (a - b).abs() < 0.5;
let (top, bottom) = (
near(part.top(), outer.top()),
near(part.bottom(), outer.bottom()),
);
let (left, right) = (
near(part.left(), outer.left()),
near(part.right(), outer.right()),
);
let pick = |on: bool, radius: u8| if on { radius } else { 0 };
CornerRadius {
nw: pick(top && left, outer_radius.nw),
ne: pick(top && right, outer_radius.ne),
sw: pick(bottom && left, outer_radius.sw),
se: pick(bottom && right, outer_radius.se),
}
}