ailloli_ui_widgets/chrome/
ailloli_ui_titlebar.rs1use 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
17fn 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
54fn 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
91pub 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
111pub 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
186pub 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}