native-theme 0.7.2

Cross-platform native theme detection and loading for Rust GUI applications
Documentation
// KDE Breeze widget metrics from breezemetrics.h constants
// Populates per-widget sizing fields directly on ThemeMode.

/// Populate per-widget sizing fields with Breeze metrics constants.
///
/// All values are in logical pixels (integers from breezemetrics.h, cast to f32).
/// Each value is annotated with the corresponding breezemetrics.h constant name.
#[cfg_attr(not(all(target_os = "linux", feature = "kde")), allow(dead_code))]
pub(crate) fn populate_widget_sizing(variant: &mut crate::ThemeMode) {
    // Button
    variant.button.min_width = Some(82.0); // Button_MinWidth 80 + 2 × (2 frame - 1 shadow inset)
    let border = variant.button.border.get_or_insert_default();
    // platform-facts.md:1184-1185 (§2.3): Button_MarginWidth = 6 on both axes
    // (breezestyle.cpp expands the button size by the margin on each side).
    border.padding_left = Some(6.0); // Button_MarginWidth
    border.padding_right = Some(6.0); // Button_MarginWidth
    border.padding_top = Some(6.0); // Button_MarginWidth
    border.padding_bottom = Some(6.0); // Button_MarginWidth
    variant.button.icon_text_gap = Some(4.0); // Button_ItemSpacing

    // Checkbox
    variant.checkbox.indicator_width = Some(20.0); // CheckBox_Size
    // platform-facts.md:1241 (§2.5): renderRadioButton's radius
    // (CheckBox_Size - 2 - 12) / 2 = 3
    variant.checkbox.radio_dot_diameter = Some(6.0);
    // platform-facts.md:1242 (§2.5): the check pen, PenWidth::Frame * 2 = 2.002
    variant.checkbox.check_mark_stroke_width = Some(2.0);
    variant.checkbox.label_gap = Some(4.0); // CheckBox_ItemSpacing

    // Input
    let border = variant.input.border.get_or_insert_default();
    // platform-facts.md:1213-1214 (§2.4): inside the 1px frame line,
    // LineEdit_FrameWidth 6 plus QLineEdit's horizontalMargin 2 / verticalMargin 1
    border.padding_left = Some(7.0); // LineEdit_FrameWidth + 2 - 1
    border.padding_right = Some(7.0); // LineEdit_FrameWidth + 2 - 1
    border.padding_top = Some(6.0); // LineEdit_FrameWidth + 1 - 1
    border.padding_bottom = Some(6.0); // LineEdit_FrameWidth + 1 - 1

    // Text area
    let border = variant.text_area.border.get_or_insert_default();
    // platform-facts.md:1714-1715 (§2.29): inside the 1px frame line,
    // Frame_FrameWidth 2 plus QTextDocument's documentMargin 4
    border.padding_left = Some(5.0); // Frame_FrameWidth + 4 - 1
    border.padding_right = Some(5.0); // Frame_FrameWidth + 4 - 1
    border.padding_top = Some(5.0); // Frame_FrameWidth + 4 - 1
    border.padding_bottom = Some(5.0); // Frame_FrameWidth + 4 - 1

    // Scrollbar
    variant.scrollbar.groove_width = Some(21.0); // ScrollBar_Extend
    variant.scrollbar.min_thumb_length = Some(20.0); // ScrollBar_MinSliderHeight
    variant.scrollbar.thumb_width = Some(8.0); // ScrollBar_SliderWidth

    // Slider
    variant.slider.track_height = Some(6.0); // Slider_GrooveThickness
    variant.slider.thumb_diameter = Some(20.0); // Slider_ControlThickness
    variant.slider.tick_mark_length = Some(8.0); // Slider_TickLength
    variant.slider.thumb_border_line_width = Some(1.0); // PenWidth::Frame 1.001, drawn as 1

    // Progress bar
    variant.progress_bar.track_height = Some(6.0); // ProgressBar_Thickness
    // min_width: KDE has no native minimum (platform-facts §2.10).
    // ProgressBar_BusyIndicatorSize (14) is the busy-indicator animation
    // segment width, not a widget minimum. Preset provides the value.

    // Tab
    variant.tab.min_width = Some(80.0); // TabBar_TabMinWidth
    variant.tab.min_height = Some(30.0); // TabBar_TabMinHeight
    // platform-facts.md:1360 (§2.11): QTabBar lays the tabs edge to edge
    variant.tab.item_gap = Some(0.0);
    let border = variant.tab.border.get_or_insert_default();
    // platform-facts.md:1352-1353 (§2.11)
    border.padding_left = Some(8.0); // TabBar_TabMarginWidth
    border.padding_right = Some(8.0); // TabBar_TabMarginWidth
    border.padding_top = Some(4.0); // TabBar_TabMarginHeight
    border.padding_bottom = Some(4.0); // TabBar_TabMarginHeight

    // Menu
    let border = variant.menu.border.get_or_insert_default();
    // A menu with icons and no checkable items, KDE's default
    // (docs/platform-facts.md §2.6; breezestyle.cpp:649-651, :3663-3667,
    // :6053-6065, :6091-6097 at breeze f0b1d75)
    border.padding_left = Some(14.0); // MenuItem_MarginWidth 4 + MenuItem_TextLeftMargin 8 + (20 - 16) / 2
    border.padding_right = Some(27.0); // MenuItem_MarginWidth 4 + MenuButton_IndicatorWidth 20 + MenuItem_ItemSpacing 2 + 1
    border.padding_top = Some(4.0); // MenuItem_MarginHeight
    border.padding_bottom = Some(4.0); // MenuItem_MarginHeight
    variant.menu.icon_text_gap = Some(4.0); // (20 - 16) / 2 + MenuItem_ItemSpacing 2
    variant.menu.panel_padding_horizontal = Some(3.0); // PM_MenuHMargin = MenuItem_HighlightGap 4, less the 1 px outline
    variant.menu.panel_padding_vertical = Some(3.0); // PM_MenuVMargin, the same
    variant.menu.min_row_height = Some(29.0); // max(text, 20, icon) + 1 + 2 * MenuItem_MarginHeight, text <= 20
    // A menu bar's title (docs/platform-facts.md §2.6)
    variant.menu.bar_padding_horizontal = Some(10.0); // MenuBarItem_MarginWidth
    variant.menu.bar_padding_vertical = Some(6.0); // MenuBarItem_MarginHeight

    // Tooltip
    let border = variant.tooltip.border.get_or_insert_default();
    // platform-facts.md:1283-1284 (§2.7)
    border.padding_left = Some(3.0); // ToolTip_FrameWidth
    border.padding_right = Some(3.0); // ToolTip_FrameWidth
    border.padding_top = Some(3.0); // ToolTip_FrameWidth
    border.padding_bottom = Some(3.0); // ToolTip_FrameWidth

    // List
    let border = variant.list.border.get_or_insert_default();
    // platform-facts.md:1433-1434 (§2.15)
    border.padding_left = Some(2.0); // ItemView_ItemMarginLeft
    border.padding_right = Some(2.0); // ItemView_ItemMarginLeft
    border.padding_top = Some(1.0); // ItemView_ItemMarginTop
    border.padding_bottom = Some(1.0); // ItemView_ItemMarginTop

    // Toolbar
    // platform-facts.md:1391 (§2.13)
    variant.toolbar.item_gap = Some(0.0); // ToolBar_ItemSpacing

    // Splitter
    variant.splitter.divider_width = Some(1.0); // Splitter_SplitterWidth
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use crate::ThemeMode;

    #[test]
    fn breeze_metrics_populates_button_min_width() {
        let mut v = ThemeMode::default();
        super::populate_widget_sizing(&mut v);
        assert_eq!(v.button.min_width, Some(82.0), "Button_MinWidth");
    }

    #[test]
    fn breeze_metrics_populates_checkbox_indicator_width() {
        let mut v = ThemeMode::default();
        super::populate_widget_sizing(&mut v);
        assert_eq!(v.checkbox.indicator_width, Some(20.0), "CheckBox_Size");
        assert_eq!(
            v.checkbox.radio_dot_diameter,
            Some(6.0),
            "renderRadioButton"
        );
    }

    #[test]
    fn breeze_metrics_populates_scrollbar_groove_width() {
        let mut v = ThemeMode::default();
        super::populate_widget_sizing(&mut v);
        assert_eq!(v.scrollbar.groove_width, Some(21.0), "ScrollBar_Extend");
    }

    #[test]
    fn breeze_metrics_populates_slider_thumb() {
        let mut v = ThemeMode::default();
        super::populate_widget_sizing(&mut v);
        assert_eq!(
            v.slider.thumb_diameter,
            Some(20.0),
            "Slider_ControlThickness"
        );
        assert_eq!(
            v.slider.thumb_border_line_width,
            Some(1.0),
            "PenWidth::Frame"
        );
    }

    #[test]
    fn breeze_metrics_populates_splitter() {
        let mut v = ThemeMode::default();
        super::populate_widget_sizing(&mut v);
        assert_eq!(
            v.splitter.divider_width,
            Some(1.0),
            "Splitter_SplitterWidth"
        );
    }

    #[test]
    fn breeze_metrics_populates_tab_item_gap() {
        let mut v = ThemeMode::default();
        super::populate_widget_sizing(&mut v);
        assert_eq!(v.tab.item_gap, Some(0.0), "QTabBar: tabs edge to edge");
    }

    #[test]
    fn breeze_metrics_populates_the_menu_bar_titles() {
        let mut v = ThemeMode::default();
        super::populate_widget_sizing(&mut v);
        assert_eq!(
            v.menu.bar_padding_horizontal,
            Some(10.0),
            "MenuBarItem_MarginWidth"
        );
        assert_eq!(
            v.menu.bar_padding_vertical,
            Some(6.0),
            "MenuBarItem_MarginHeight"
        );
    }

    #[test]
    fn breeze_metrics_populates_the_menu_geometry() {
        let mut v = ThemeMode::default();
        super::populate_widget_sizing(&mut v);
        let border = v.menu.border.as_ref();
        assert_eq!(
            border.and_then(|b| b.padding_left),
            Some(14.0),
            "MenuItem_MarginWidth + MenuItem_TextLeftMargin + (20 - 16) / 2"
        );
        assert_eq!(
            border.and_then(|b| b.padding_right),
            Some(27.0),
            "MenuItem_MarginWidth + MenuButton_IndicatorWidth + MenuItem_ItemSpacing + 1"
        );
        assert_eq!(
            border.and_then(|b| b.padding_top),
            Some(4.0),
            "MenuItem_MarginHeight"
        );
        assert_eq!(
            border.and_then(|b| b.padding_bottom),
            Some(4.0),
            "MenuItem_MarginHeight"
        );
        assert_eq!(
            v.menu.icon_text_gap,
            Some(4.0),
            "(20 - 16) / 2 + MenuItem_ItemSpacing"
        );
        assert_eq!(v.menu.panel_padding_horizontal, Some(3.0), "PM_MenuHMargin");
        assert_eq!(v.menu.panel_padding_vertical, Some(3.0), "PM_MenuVMargin");
        assert_eq!(v.menu.min_row_height, Some(29.0), "the least row");
    }

    #[test]
    fn breeze_metrics_variant_not_empty() {
        let mut v = ThemeMode::default();
        super::populate_widget_sizing(&mut v);
        assert!(
            !v.is_empty(),
            "variant should not be empty after widget sizing"
        );
    }
}