native-theme 0.7.1

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(80.0); // Button_MinWidth
    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

    // 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();
    // platform-facts.md:1261-1262 (§2.6): MenuItem_MarginWidth and
    // MenuItem_MarginHeight, 4; kept although, in a menu without icons or
    // checkable items, Breeze starts the text 16 from the highlight (:1261),
    // and puts a 16 px icon's text 4 after the icon (:1267): the padding and
    // the gap change together (docs/todo.md, KDE values that wait for a
    // connector design)
    border.padding_left = Some(4.0); // MenuItem_MarginWidth
    border.padding_right = Some(4.0); // MenuItem_MarginWidth
    border.padding_top = Some(4.0); // MenuItem_MarginHeight
    border.padding_bottom = Some(4.0); // MenuItem_MarginHeight
    // Kept, see above: MenuItem_TextLeftMargin is the inset before the icon
    // column, not this gap (platform-facts.md:1267)
    variant.menu.icon_text_gap = Some(8.0);

    // 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(80.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"
        );
    }

    #[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_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"
        );
    }
}