use hyprforge_popup::kit::{Rect, Tabs};
use hyprforge_popup::Stack;
pub const POPUP_WIDTH: f64 = 600.0;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Layout {
pub width: f64,
pub padding: f64,
pub search: Rect,
pub tabs: Tabs,
pub body_top: f64,
pub body_height: f64,
pub list_width: f64,
pub list_inset: f64,
pub header_height: f64,
pub row_height: f64,
pub row_spacing: f64,
pub pane_padding: f64,
pub preview: Rect,
pub pane_gap: f64,
pub meta_line: f64,
pub meta_gap: f64,
pub paste_button: Rect,
pub pin_button: Rect,
pub delete_button: Rect,
pub footer_top: f64,
pub footer_height: f64,
pub clear_button: Rect,
pub height: f64,
}
mod design {
pub const PADDING: f64 = 10.0;
pub const SEARCH: f64 = 30.0;
pub const SEARCH_GAP: f64 = 8.0;
pub const TABS: f64 = 26.0;
pub const BODY: f64 = 318.0;
pub const LIST: f64 = 330.0;
pub const LIST_INSET: f64 = 6.0;
pub const HEADER: f64 = 26.0;
pub const ROW: f64 = 34.0;
pub const PANE_PADDING: f64 = 12.0;
pub const PREVIEW: f64 = 110.0;
pub const PANE_GAP: f64 = 10.0;
pub const BUTTON: f64 = 30.0;
pub const BUTTON_GAP: f64 = 6.0;
pub const FOOTER: f64 = 33.0;
pub const CLEAR: f64 = 150.0;
}
impl Layout {
pub fn for_font_size(font_size: f32) -> Layout {
let fs = font_size as f64;
let line = |size: f64| size * 1.2;
let width = POPUP_WIDTH;
let padding = design::PADDING;
let search_height = design::SEARCH.max(line(fs) + 12.0);
let search = Rect { x: padding, y: padding, width: width - padding * 2.0, height: search_height };
let tabs_height = design::TABS.max(line(fs * 0.96) + 10.0);
let tabs = Tabs {
x: padding,
y: search.bottom() + design::SEARCH_GAP,
width: width - padding * 2.0,
height: tabs_height,
count: crate::kind::Filter::ALL.len(),
};
let body_top = tabs.y + tabs.height + padding + 1.0;
let row_height = design::ROW.max(line(fs) + 14.0);
let grow = row_height / design::ROW;
let body_height = (design::BODY * grow).round();
let header_height = design::HEADER.max(line(fs * 0.81) + 13.0);
let list_width = design::LIST;
let pane_padding = design::PANE_PADDING;
let pane_x = list_width + 1.0 + pane_padding;
let pane_width = width - pane_x - pane_padding;
let preview = Rect { x: pane_x, y: body_top + pane_padding, width: pane_width, height: design::PREVIEW };
let button = design::BUTTON.max(line(fs) + 12.0);
let pane_bottom = body_top + body_height - pane_padding;
let half = (pane_width - design::BUTTON_GAP) / 2.0;
let pin_button = Rect { x: pane_x, y: pane_bottom - button, width: half, height: button };
let delete_button = Rect { x: pane_x + half + design::BUTTON_GAP, y: pin_button.y, width: half, height: button };
let paste_button = Rect { x: pane_x, y: pin_button.y - design::BUTTON_GAP - button, width: pane_width, height: button };
let footer_top = body_top + body_height + 1.0;
let footer_height = design::FOOTER.max(line(fs * 0.88) + 16.0);
let clear_button = Rect { x: width - 12.0 - design::CLEAR, y: footer_top, width: design::CLEAR, height: footer_height };
Layout {
width,
padding,
search,
tabs,
body_top,
body_height,
list_width,
list_inset: design::LIST_INSET,
header_height,
row_height,
row_spacing: 0.0,
pane_padding,
preview,
pane_gap: design::PANE_GAP,
meta_line: (fs * 0.88 * 1.3).ceil(),
meta_gap: 6.0,
paste_button,
pin_button,
delete_button,
footer_top,
footer_height,
clear_button,
height: footer_top + footer_height,
}
}
pub fn viewport_height(&self) -> f64 {
(self.body_height - self.list_inset).max(0.0)
}
pub fn scrollbar(&self) -> hyprforge_popup::Scrollbar {
let track_x = self.list_width - hyprforge_popup::Scrollbar::WIDTH - 1.0;
hyprforge_popup::Scrollbar::new(track_x, self.body_top, self.viewport_height())
}
pub fn hit(&self, position: (f64, f64), stack: &Stack, offset: f64) -> Option<Hit> {
if let Some(tab) = self.tabs.tab_at(position) {
return Some(Hit::Tab(tab));
}
for (rect, hit) in [
(self.paste_button, Hit::Paste),
(self.pin_button, Hit::Pin),
(self.delete_button, Hit::Delete),
(self.clear_button, Hit::Clear),
] {
if rect.contains(position) {
return Some(hit);
}
}
let (x, y) = position;
let inside_list = x >= self.list_inset
&& x <= self.list_width - self.list_inset
&& y >= self.body_top
&& y <= self.body_top + self.viewport_height();
if !inside_list {
return None;
}
stack.line_at(y - self.body_top + offset).map(Hit::Line)
}
pub fn chars_that_fit(font_size: f32, available: f64) -> usize {
let average = (font_size as f64 * 0.6).max(1.0);
((available / average).floor() as usize).max(1)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Hit {
Tab(usize),
Line(usize),
Paste,
Pin,
Delete,
Clear,
}
#[cfg(test)]
mod tests {
use super::*;
fn layout() -> Layout {
Layout::for_font_size(13.0)
}
#[test]
fn at_the_designs_own_font_size_the_layout_is_the_designs() {
let l = layout();
assert_eq!(l.search.height, 30.0);
assert_eq!(l.tabs.height, 26.0);
assert_eq!(l.row_height, 34.0);
assert_eq!(l.header_height, 26.0);
assert_eq!(l.body_height, 318.0);
assert_eq!(l.list_width, 330.0);
assert_eq!(l.preview.height, 110.0);
}
#[test]
fn a_bigger_font_grows_the_rows_rather_than_overflowing_them() {
let big = Layout::for_font_size(24.0);
assert!(big.row_height >= 24.0 * 1.2, "a row must hold a line of its own text");
assert!(big.height > layout().height);
}
#[test]
fn the_regions_stack_without_overlapping_and_fit_the_popup() {
for fs in [9.0, 13.0, 15.0, 20.0] {
let l = Layout::for_font_size(fs);
assert!(l.tabs.y >= l.search.bottom());
assert!(l.body_top >= l.tabs.y + l.tabs.height);
assert!(l.preview.bottom() <= l.paste_button.y, "the preview must end above the buttons at {fs}");
assert!(l.paste_button.bottom() <= l.pin_button.y);
assert!(l.pin_button.right() <= l.delete_button.x);
assert!(l.delete_button.right() <= l.width - l.pane_padding + 0.001);
assert!(l.delete_button.bottom() <= l.footer_top);
assert!(l.clear_button.bottom() <= l.height + 0.001);
}
}
#[test]
fn a_click_on_each_control_is_that_control() {
let l = layout();
let stack = Stack::new([], 0.0);
let middle = |r: Rect| (r.x + r.width / 2.0, r.y + r.height / 2.0);
assert_eq!(l.hit(middle(l.paste_button), &stack, 0.0), Some(Hit::Paste));
assert_eq!(l.hit(middle(l.pin_button), &stack, 0.0), Some(Hit::Pin));
assert_eq!(l.hit(middle(l.delete_button), &stack, 0.0), Some(Hit::Delete));
assert_eq!(l.hit(middle(l.clear_button), &stack, 0.0), Some(Hit::Clear));
assert_eq!(l.hit((l.tabs.x + 20.0, l.tabs.y + 10.0), &stack, 0.0), Some(Hit::Tab(0)));
}
#[test]
fn a_list_line_is_hit_where_it_is_drawn_scrolled_or_not() {
let l = layout();
let stack = Stack::new([l.header_height, l.row_height, l.row_height, l.row_height], l.row_spacing);
let x = l.list_width / 2.0;
let at = |line: usize, offset: f64| l.body_top + stack.top(line) - offset + stack.height(line) / 2.0;
assert_eq!(l.hit((x, at(0, 0.0)), &stack, 0.0), Some(Hit::Line(0)));
assert_eq!(l.hit((x, at(2, 0.0)), &stack, 0.0), Some(Hit::Line(2)));
assert_eq!(l.hit((x, at(3, 20.0)), &stack, 20.0), Some(Hit::Line(3)));
}
#[test]
fn the_preview_pane_and_the_header_are_not_the_list() {
let l = layout();
let stack = Stack::new([l.row_height; 20], 0.0);
assert_eq!(l.hit((l.list_width + 50.0, l.body_top + 40.0), &stack, 0.0), None);
assert_eq!(l.hit((50.0, l.body_top - 2.0), &stack, 0.0), None);
}
#[test]
fn past_the_last_line_nothing_is_hit() {
let l = layout();
let stack = Stack::new([l.row_height; 2], 0.0);
assert_eq!(l.hit((50.0, l.body_top + 3.0 * l.row_height), &stack, 0.0), None);
}
#[test]
fn the_scrollbar_sits_in_the_lists_own_gutter() {
let l = layout();
let bar = l.scrollbar();
assert!(bar.track_x + bar.width <= l.list_width, "the bar must not cross into the preview pane");
assert!(bar.track_x >= l.list_width - l.list_inset - 1.0, "and must not sit over the rows' text");
}
#[test]
fn more_room_fits_more_characters_and_never_zero() {
assert!(Layout::chars_that_fit(13.0, 300.0) > Layout::chars_that_fit(13.0, 60.0));
assert_eq!(Layout::chars_that_fit(13.0, 0.0), 1);
}
}