use crate::ui::theme;
pub const HANDLE: f32 = theme::space::S;
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub struct Dragged {
pub shut: Option<f32>,
pub open: Option<f32>,
}
impl Dragged {
pub fn shown(self, window: f32, floor: f32) -> (f32, f32) {
let open = self
.open
.unwrap_or_else(|| super::open_width(window))
.min(ceiling(window))
.max(floor);
let shut = self.shut.unwrap_or(super::SHUT).min(open).max(floor);
(shut, open)
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Drag {
pub open: bool,
pub from: f32,
}
pub fn ceiling(window: f32) -> f32 {
window * 2.0 / 5.0
}
pub fn floor(ctx: &egui::Context) -> f32 {
let body = egui::TextStyle::Body.resolve(&ctx.style());
let cell = ctx.fonts(|fonts| fonts.row_height(&body));
cell + margin().sum().x
}
pub fn margin() -> egui::Margin {
egui::Margin {
left: theme::space::XS,
right: theme::space::XS + HANDLE,
top: theme::space::XS,
bottom: theme::space::XS,
}
}
pub(super) fn edge(ui: &mut egui::Ui, bar: &mut super::Bar, open: bool, width: f32) {
let screen = ui.ctx().screen_rect();
let strip = egui::Rect::from_min_max(
egui::pos2(screen.left() + width - HANDLE, screen.top()),
egui::pos2(screen.left() + width, screen.bottom()),
);
let sense = egui::Sense {
click: false,
drag: true,
focusable: false,
};
let seat = ui
.interact(strip, egui::Id::new("bar edge"), sense)
.on_hover_cursor(egui::CursorIcon::ResizeHorizontal);
if seat.hovered() || seat.dragged() {
ui.painter().rect_filled(strip, 0.0, theme::hover());
}
let floor = floor(ui.ctx());
let (shut, wide) = bar.dragged.shown(screen.width(), floor);
if seat.drag_started() {
let from = if open { wide } else { shut };
bar.drag = Some(Drag { open, from });
}
let origin = ui.ctx().input(|i| i.pointer.press_origin());
if let (Some(drag), Some(origin), Some(at)) = (bar.drag, origin, seat.interact_pointer_pos()) {
let asked = drag.from + at.x - origin.x;
if drag.open {
bar.dragged.open = Some(asked.min(ceiling(screen.width())).max(shut));
} else {
bar.dragged.shut = Some(asked.min(wide).max(floor));
}
}
if seat.drag_stopped() {
bar.drag = None;
}
}