crisol-core 1.2.0

Crisol core: an egui-free data model, themes, metrics, localization, commands, state, and storage.
Documentation
//! Size tokens for the workbench chrome, in points.
//!
//! Keeping every chrome size in one place stops the title bar, tabs, explorer, and
//! dialogs from drifting apart. The values are stable: they are part of the pixel
//! contract with Crisol 0.1.3 (PIX-MET-01).
//!
//! ```
//! use crisol_core::metrics;
//! ```

/// Glyphs inside dense menus, steppers, and window buttons.
/// # Example
///
/// ```
/// use crisol_core::metrics::ICON_SMALL;
/// ```
pub const ICON_SMALL: f32 = 14.0;
/// Glyphs of title-bar actions, tabs, and tree rows.
/// # Example
///
/// ```
/// use crisol_core::metrics::ICON_MEDIUM;
/// ```
pub const ICON_MEDIUM: f32 = 16.0;
/// The product mark.
/// # Example
///
/// ```
/// use crisol_core::metrics::ICON_LARGE;
/// ```
pub const ICON_LARGE: f32 = 20.0;

/// Height of the title bar.
/// # Example
///
/// ```
/// use crisol_core::metrics::TITLE_HEIGHT;
/// ```
pub const TITLE_HEIGHT: f32 = 34.0;
/// Height of the document tab strip.
/// # Example
///
/// ```
/// use crisol_core::metrics::TAB_HEIGHT;
/// ```
pub const TAB_HEIGHT: f32 = 34.0;
/// Left inset of the first tab in the strip.
/// # Example
///
/// ```
/// use crisol_core::metrics::TAB_STRIP_PADDING;
/// ```
pub const TAB_STRIP_PADDING: f32 = 8.0;
/// Height of the status bar.
/// # Example
///
/// ```
/// use crisol_core::metrics::STATUS_HEIGHT;
/// ```
pub const STATUS_HEIGHT: f32 = 24.0;
/// Height of an explorer tree row.
/// # Example
///
/// ```
/// use crisol_core::metrics::TREE_ROW_HEIGHT;
/// ```
pub const TREE_ROW_HEIGHT: f32 = 21.0;
/// Indentation per tree depth.
/// # Example
///
/// ```
/// use crisol_core::metrics::TREE_INDENT;
/// ```
pub const TREE_INDENT: f32 = 12.0;
/// Width of the expand/collapse slot in front of every tree icon.
/// # Example
///
/// ```
/// use crisol_core::metrics::CHEVRON_SIZE;
/// ```
pub const CHEVRON_SIZE: f32 = 14.0;
/// Default explorer width.
/// # Example
///
/// ```
/// use crisol_core::metrics::SIDEBAR_WIDTH;
/// ```
pub const SIDEBAR_WIDTH: f32 = 260.0;

/// Square title-bar action button.
/// # Example
///
/// ```
/// use crisol_core::metrics::ACTION_BUTTON;
/// ```
pub const ACTION_BUTTON: f32 = 28.0;
/// Square tab-strip tool button.
/// # Example
///
/// ```
/// use crisol_core::metrics::TOOL_BUTTON;
/// ```
pub const TOOL_BUTTON: f32 = 26.0;
/// Square explorer header button.
/// # Example
///
/// ```
/// use crisol_core::metrics::SMALL_BUTTON;
/// ```
pub const SMALL_BUTTON: f32 = 21.0;
/// Width of a window control cell (minimize, maximize, close).
/// # Example
///
/// ```
/// use crisol_core::metrics::WINDOW_BUTTON_WIDTH;
/// ```
pub const WINDOW_BUTTON_WIDTH: f32 = 44.0;
/// Height of a window control cell.
/// # Example
///
/// ```
/// use crisol_core::metrics::WINDOW_BUTTON_HEIGHT;
/// ```
pub const WINDOW_BUTTON_HEIGHT: f32 = 32.0;

/// Height of text fields, dialog buttons, combo boxes, and steppers.
/// # Example
///
/// ```
/// use crisol_core::metrics::CONTROL_HEIGHT;
/// ```
pub const CONTROL_HEIGHT: f32 = 30.0;
/// Height of a context-menu row.
/// # Example
///
/// ```
/// use crisol_core::metrics::MENU_ROW_HEIGHT;
/// ```
pub const MENU_ROW_HEIGHT: f32 = 22.0;
/// Height of a palette or keyboard-menu row.
/// # Example
///
/// ```
/// use crisol_core::metrics::LIST_ROW_HEIGHT;
/// ```
pub const LIST_ROW_HEIGHT: f32 = 26.0;
/// Pixels from the window edge that start a resize on an undecorated window.
/// # Example
///
/// ```
/// use crisol_core::metrics::RESIZE_EDGE;
/// ```
pub const RESIZE_EDGE: f32 = 4.0;

/// Every size token, by its stable name.
///
/// This is the table frozen in `tests/fixtures/metrics/golden-0.1.3.json`.
/// # Example
///
/// ```
/// use crisol_core::metrics::ALL;
/// ```
pub const ALL: [(&str, f32); 20] = [
    ("ICON_SMALL", ICON_SMALL),
    ("ICON_MEDIUM", ICON_MEDIUM),
    ("ICON_LARGE", ICON_LARGE),
    ("TITLE_HEIGHT", TITLE_HEIGHT),
    ("TAB_HEIGHT", TAB_HEIGHT),
    ("TAB_STRIP_PADDING", TAB_STRIP_PADDING),
    ("STATUS_HEIGHT", STATUS_HEIGHT),
    ("TREE_ROW_HEIGHT", TREE_ROW_HEIGHT),
    ("TREE_INDENT", TREE_INDENT),
    ("CHEVRON_SIZE", CHEVRON_SIZE),
    ("SIDEBAR_WIDTH", SIDEBAR_WIDTH),
    ("ACTION_BUTTON", ACTION_BUTTON),
    ("TOOL_BUTTON", TOOL_BUTTON),
    ("SMALL_BUTTON", SMALL_BUTTON),
    ("WINDOW_BUTTON_WIDTH", WINDOW_BUTTON_WIDTH),
    ("WINDOW_BUTTON_HEIGHT", WINDOW_BUTTON_HEIGHT),
    ("CONTROL_HEIGHT", CONTROL_HEIGHT),
    ("MENU_ROW_HEIGHT", MENU_ROW_HEIGHT),
    ("LIST_ROW_HEIGHT", LIST_ROW_HEIGHT),
    ("RESIZE_EDGE", RESIZE_EDGE),
];

/// Round a size in points to the nearest physical pixel, as `emath` does.
///
/// `round_to_pixels(x)` is `(x * pixels_per_point).round() / pixels_per_point`, so two
/// sizes that land on the same pixel stay equal when rasterized (PIX-MET-02).
/// # Example
///
/// ```
/// use crisol_core::metrics::round_to_pixels;
/// ```
pub fn round_to_pixels(points: f32, pixels_per_point: f32) -> f32 {
    if pixels_per_point <= 0.0 || !pixels_per_point.is_finite() {
        return points;
    }
    (points * pixels_per_point).round() / pixels_per_point
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn every_token_has_a_distinct_name_and_a_finite_value() {
        let mut names: Vec<&str> = ALL.iter().map(|(name, _)| *name).collect();
        names.sort_unstable();
        names.dedup();
        assert_eq!(names.len(), ALL.len());
        for (name, value) in ALL {
            assert!(value.is_finite(), "{name}");
        }
    }

    #[test]
    fn rounding_matches_the_known_cases() {
        assert_eq!(round_to_pixels(1.4, 1.0), 1.0);
        assert_eq!(round_to_pixels(1.6, 1.0), 2.0);
        assert_eq!(round_to_pixels(0.6, 2.0), 0.5);
        assert_eq!(round_to_pixels(0.3, 2.0), 0.5);
        assert_eq!(round_to_pixels(1.4, 0.0), 1.4);
    }
}