74 lines
2.8 KiB
Rust
74 lines
2.8 KiB
Rust
//! Side-card geometry and hostile persisted panel settings.
|
|
|
|
use egui::{Pos2, Rect, vec2};
|
|
use notescraft_ui_egui::{
|
|
layout::{self, CardVisibility, LayoutOptions},
|
|
state::UiState,
|
|
};
|
|
|
|
const RAIL: f32 = notescraft_ui_egui::panel_resize::RAIL_WIDTH;
|
|
|
|
fn rects(width: f32, state: &UiState) -> layout::LayoutRects {
|
|
layout::compute_rects_with_options(
|
|
Rect::from_min_size(Pos2::ZERO, vec2(width, 900.0)),
|
|
CardVisibility { sidebar: state.sidebar_visible, right: state.right_visible },
|
|
LayoutOptions { sidebar_width: state.sidebar_width, right_width: state.right_width, inset: 0.0 },
|
|
)
|
|
}
|
|
|
|
#[test]
|
|
fn collapsed_cards_leave_rails_and_grow_editor() {
|
|
let mut state = UiState::new();
|
|
let expanded = rects(1440.0, &state).editor.map(|rect| rect.width());
|
|
state.sidebar_visible = false;
|
|
state.right_visible = false;
|
|
let collapsed = rects(1440.0, &state);
|
|
assert_eq!(collapsed.sidebar.map(|rect| rect.width()), Some(RAIL));
|
|
assert_eq!(collapsed.right.map(|rect| rect.width()), Some(RAIL));
|
|
assert!(collapsed.editor.map(|rect| rect.width()) > expanded);
|
|
}
|
|
|
|
#[test]
|
|
fn narrow_cards_auto_collapse_without_changing_user_choice() {
|
|
let state = UiState::new();
|
|
assert_eq!(rects(800.0, &state).right.map(|rect| rect.width()), Some(RAIL));
|
|
assert_eq!(rects(500.0, &state).sidebar.map(|rect| rect.width()), Some(RAIL));
|
|
assert_eq!(rects(1440.0, &state).sidebar.map(|rect| rect.width()), Some(300.0));
|
|
assert!(state.sidebar_visible && state.right_visible);
|
|
}
|
|
|
|
#[test]
|
|
fn oversized_cards_preserve_centre_minimum() {
|
|
let mut state = UiState::new();
|
|
state.sidebar_width = 520.0;
|
|
state.right_width = 560.0;
|
|
for width in [640.0, 900.0, 1000.0, 1200.0, 1440.0] {
|
|
assert!(rects(width, &state).editor.is_some_and(|rect| rect.width() >= 360.0));
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn restored_panel_widths_are_finite_and_bounded() {
|
|
for text in [
|
|
r#"{"sidebar_width":-50,"right_width":1e30}"#,
|
|
r#"{"sidebar_width":null,"right_width":null,"sidebar_visible":false}"#,
|
|
r#"{"sidebar_width":1e99,"right_width":-1e99}"#,
|
|
] {
|
|
let state = UiState::from_json(text);
|
|
assert!((220.0..=520.0).contains(&state.sidebar_width));
|
|
assert!((240.0..=560.0).contains(&state.right_width));
|
|
}
|
|
let restored = UiState::from_json(r#"{"sidebar_width":null,"sidebar_visible":false}"#);
|
|
assert!(!restored.sidebar_visible);
|
|
for value in [f32::NAN, f32::INFINITY, f32::NEG_INFINITY, f32::MAX] {
|
|
let mut state = UiState::new();
|
|
state.sidebar_width = value;
|
|
state.right_width = value;
|
|
state.sidebar_visible = false;
|
|
let restored = UiState::from_json(&state.to_json());
|
|
assert!((220.0..=520.0).contains(&restored.sidebar_width));
|
|
assert!((240.0..=560.0).contains(&restored.right_width));
|
|
assert!(!restored.sidebar_visible);
|
|
}
|
|
}
|