ailloli_ui_widgets/controls/
icon_button.rs

1//! Legacy draw-command helper for a square icon button.
2//!
3//! New code should compose [`crate::controls::Button`] and
4//! [`crate::primitives::Icon`] instead.
5
6#![allow(deprecated)]
7
8use ailloli_ui_core::{Color, IconId, Rect};
9use ailloli_ui_runtime::{DrawCmd, DrawRRect};
10
11use crate::primitives::draw_icon;
12
13#[deprecated(note = "use Button::new().child(Icon::new(...)) instead")]
14#[derive(Debug, Clone, Copy)]
15/// Visual metrics and colors for [`draw_icon_button`].
16///
17/// All dimensions are logical pixels. The default geometry expects a
18/// `28 × 28` logical-pixel rectangle (`6 + 16 + 6`). Values are used as-is;
19/// this legacy helper does not clamp negative sizes or padding.
20///
21/// # Examples
22///
23/// ```
24/// #![allow(deprecated)]
25/// use ailloli_ui_widgets::controls::IconButtonStyle;
26/// let style = IconButtonStyle::default();
27/// assert_eq!(style.icon_size, 16.0);
28/// assert_eq!(style.padding, 6.0);
29/// ```
30pub struct IconButtonStyle {
31    /// Background color of the rounded rectangle.
32    pub bg: Color,
33    /// Corner radius in logical pixels.
34    pub radius: f32,
35    /// Width and height of the square icon in logical pixels.
36    pub icon_size: f32,
37    /// Offset from the rectangle's top-left corner in logical pixels.
38    pub padding: f32,
39}
40
41impl Default for IconButtonStyle {
42    fn default() -> Self {
43        Self {
44            bg: Color::rgba(31, 38, 55, 1.0),
45            radius: 8.0,
46            icon_size: 16.0,
47            padding: 6.0,
48        }
49    }
50}
51
52#[deprecated(note = "use Button::new().child(Icon::new(...)) instead")]
53/// Produces the background and icon commands for a legacy icon button.
54///
55/// The first command fills `rect`; the second draws a square icon at
56/// `(rect.x + padding, rect.y + padding)`. This function only paints the
57/// control: it provides no hit testing, focus handling, or click action.
58///
59/// # Examples
60///
61/// ```
62/// #![allow(deprecated)]
63/// use ailloli_ui_core::{Color, IconId, Rect};
64/// use ailloli_ui_widgets::controls::{draw_icon_button, IconButtonStyle};
65/// let commands = draw_icon_button(
66///     Rect::new(0.0, 0.0, 28.0, 28.0),
67///     IconId::Check,
68///     Color::WHITE,
69///     IconButtonStyle::default(),
70/// );
71/// assert_eq!(commands.len(), 2);
72/// ```
73pub fn draw_icon_button(
74    rect: Rect,
75    icon: IconId,
76    tint: Color,
77    style: IconButtonStyle,
78) -> Vec<DrawCmd> {
79    vec![
80        DrawCmd::RRect(DrawRRect {
81            rect,
82            radius: style.radius,
83            color: style.bg,
84        }),
85        draw_icon(
86            Rect::new(
87                rect.x + style.padding,
88                rect.y + style.padding,
89                style.icon_size,
90                style.icon_size,
91            ),
92            icon,
93            tint,
94        ),
95    ]
96}