ailloli_ui_widgets/chrome/
ailloli_ui_titlebar.rs

1//! Default title bar for `WindowChrome::AilloliUi` (OS decorations disabled).
2
3use ailloli_ui_core::style::{
4    AlignItems, Background, BoxStyle, JustifyContent, Length, Radius, StateStyle,
5};
6use ailloli_ui_core::EdgeInsets;
7use ailloli_ui_core::{AppIcon, Color, FontId, IconId, TextStyle, Theme};
8use ailloli_ui_runtime::component::{IntoView, View};
9
10use crate::controls::button::ButtonStyle;
11use crate::controls::Button;
12use crate::layout::layout_ext::LayoutExt;
13use crate::layout::{Container, Row};
14use crate::primitives::Icon;
15use crate::text::Text;
16
17/// Builds the shared transparent minimize/maximize button style from `theme`.
18fn titlebar_chrome_control_style(theme: &Theme) -> ButtonStyle {
19    let radius = Radius::uniform(4.0);
20    ButtonStyle {
21        container: StateStyle {
22            normal: BoxStyle::new()
23                .background(Background::color(Color::TRANSPARENT))
24                .radius(radius),
25            hovered: Some(
26                BoxStyle::new()
27                    .background(Background::color(theme.titlebar_control_hover))
28                    .radius(radius),
29            ),
30            pressed: Some(
31                BoxStyle::new()
32                    .background(Background::color(theme.titlebar_control_pressed))
33                    .radius(radius),
34            ),
35            focused: None,
36            disabled: None,
37        },
38        text: StateStyle {
39            normal: TextStyle::new(FontId::Ui, 12, theme.icon_fg),
40            hovered: None,
41            pressed: None,
42            focused: None,
43            disabled: None,
44        },
45        height: 26.0,
46        horizontal_padding: 4.0,
47        vertical_padding: 2.0,
48        align_items: AlignItems::Center,
49        justify_content: JustifyContent::Center,
50        baseline_shift: 0.0,
51    }
52}
53
54/// Builds the close-button style with destructive hover and pressed colors.
55fn titlebar_close_style(theme: &Theme) -> ButtonStyle {
56    let radius = Radius::uniform(4.0);
57    ButtonStyle {
58        container: StateStyle {
59            normal: BoxStyle::new()
60                .background(Background::color(Color::TRANSPARENT))
61                .radius(radius),
62            hovered: Some(
63                BoxStyle::new()
64                    .background(Background::color(theme.close_bg_hover))
65                    .radius(radius),
66            ),
67            pressed: Some(
68                BoxStyle::new()
69                    .background(Background::color(theme.close_bg_pressed))
70                    .radius(radius),
71            ),
72            focused: None,
73            disabled: None,
74        },
75        text: StateStyle {
76            normal: TextStyle::new(FontId::Ui, 12, theme.icon_fg),
77            hovered: None,
78            pressed: None,
79            focused: None,
80            disabled: None,
81        },
82        height: 26.0,
83        horizontal_padding: 4.0,
84        vertical_padding: 2.0,
85        align_items: AlignItems::Center,
86        justify_content: JustifyContent::Center,
87        baseline_shift: 0.0,
88    }
89}
90
91/// Standard Ailloli UI title bar: `theme.titlebar_bg`, label, min / max / close icons.
92///
93/// `logical_window_id` must match the `Window::new(...)` id so min/max target the correct window.
94/// The returned view has a fixed height of 36 logical pixels and no application icon.
95///
96/// # Examples
97///
98/// ```
99/// use ailloli_ui_runtime::component::View;
100/// use ailloli_ui_widgets::chrome::ailloli_ui_default_titlebar;
101/// let titlebar: View<()> = ailloli_ui_default_titlebar("main", "Ailloli");
102/// let _ = titlebar;
103/// ```
104pub fn ailloli_ui_default_titlebar<A: 'static>(
105    logical_window_id: impl Into<String>,
106    title: impl Into<String>,
107) -> View<A> {
108    ailloli_ui_default_titlebar_with_icon(logical_window_id, title, None)
109}
110
111/// Standard Ailloli UI title bar with an optional static application icon.
112///
113/// `None` produces the same view as [`ailloli_ui_default_titlebar`]. The icon
114/// preserves its source colors and is rendered at 20 logical pixels.
115///
116/// # Examples
117///
118/// ```
119/// use ailloli_ui_core::AppIcon;
120/// use ailloli_ui_runtime::component::View;
121/// use ailloli_ui_widgets::chrome::ailloli_ui_default_titlebar_with_icon;
122/// let icon = AppIcon::from_static_svg(b"<svg/>", "icon.svg");
123/// let titlebar: View<()> =
124///     ailloli_ui_default_titlebar_with_icon("main", "Ailloli", Some(icon));
125/// let _ = titlebar;
126/// ```
127pub fn ailloli_ui_default_titlebar_with_icon<A: 'static>(
128    logical_window_id: impl Into<String>,
129    title: impl Into<String>,
130    app_icon: Option<AppIcon>,
131) -> View<A> {
132    let win_id = logical_window_id.into();
133    let title = title.into();
134    let theme = Theme::dark();
135    let chrome_style = titlebar_chrome_control_style(&theme);
136    let close_style = titlebar_close_style(&theme);
137    let icon = theme.icon_fg;
138
139    let win_min = win_id.clone();
140    let win_max = win_id;
141
142    Container::new()
143        .fill_width()
144        .height(Length::px(36.0))
145        .background(theme.titlebar_bg)
146        .child({
147            let mut row = Row::new()
148                .fill_width()
149                .fill_height()
150                .align_items(AlignItems::Center)
151                .gap(6.0);
152            if let Some(app_icon) = app_icon {
153                row = row.child(application_icon(app_icon, 20.0));
154            }
155            row = row
156                .child(
157                    Text::new(title)
158                        .style(TextStyle::new(FontId::Ui, 13, theme.icon_fg))
159                        .nowrap()
160                        .flex_grow(),
161                )
162                .child(
163                    Button::new()
164                        .button_style(chrome_style.clone())
165                        .on_click_ctx(move |ctx| ctx.request_minimize_window(&win_min))
166                        .child(Icon::new(IconId::Minimize).tint(icon).size(14.0)),
167                )
168                .child(
169                    Button::new()
170                        .button_style(chrome_style)
171                        .on_click_ctx(move |ctx| ctx.request_toggle_maximize_window(&win_max))
172                        .child(Icon::new(IconId::Maximize).tint(icon).size(14.0)),
173                )
174                .child(
175                    Button::new()
176                        .button_style(close_style)
177                        .on_click_ctx(move |ctx| ctx.request_close())
178                        .child(Icon::new(IconId::Close).tint(icon).size(14.0)),
179                );
180            row.layout_mut().padding = EdgeInsets::new(12.0, 4.0, 12.0, 4.0);
181            row
182        })
183        .into_view()
184}
185
186/// Static, color-preserving application icon suitable for custom chrome.
187///
188/// `size` is used directly as the square logical-pixel extent; callers should
189/// pass a finite non-negative value accepted by the normal layout pipeline.
190///
191/// # Examples
192///
193/// ```
194/// use ailloli_ui_core::AppIcon;
195/// use ailloli_ui_runtime::component::View;
196/// use ailloli_ui_widgets::chrome::application_icon;
197/// let icon = AppIcon::from_static_svg(b"<svg/>", "icon.svg");
198/// let view: View<()> = application_icon(icon, 20.0);
199/// let _ = view;
200/// ```
201pub fn application_icon<A: 'static>(icon: AppIcon, size: f32) -> View<A> {
202    Icon::new(IconId::Svg(icon.source().clone()))
203        .size(size)
204        .tint(Color::WHITE)
205        .interactive_tint(false)
206        .into_view()
207}