1use crate::{Rect, Size, dimension};
2use xrack_runtime::{ButtonDisplay, ImageAsset, LayoutDirection, Node, Value, button_text};
3
4pub struct ButtonPlacement {
6 pub icon: Option<Rect>,
7 pub label: Rect,
8}
9
10pub struct ButtonContent<'a> {
12 pub label: &'a str,
13 pub icon: Option<(ImageAsset, f32)>,
14 pub padding: f32,
15 authored_padding: bool,
16 display: ButtonDisplay,
17 spacing: f32,
18}
19
20impl<'a> ButtonContent<'a> {
21 pub fn new(node: &'a Node) -> Self {
22 let display = match node.properties.get("display") {
23 Some(Value::ButtonDisplay(value)) => *value,
24 _ if node.boolean("icon-only", false) => ButtonDisplay::IconOnly,
25 _ => ButtonDisplay::default(),
26 };
27 let size = dimension(node, "icon-size").unwrap_or(18.0);
28 let icon = node
29 .image_asset("icon")
30 .filter(|image| {
31 display != ButtonDisplay::TextOnly && size > 0.0 && !image.size().contains(&0)
32 })
33 .map(|image| (image, size));
34 let label = if icon.is_some() && display == ButtonDisplay::IconOnly {
35 ""
36 } else {
37 button_text(node)
38 };
39 let padding = dimension(node, "padding");
40 Self {
41 label,
42 icon,
43 padding: padding.unwrap_or(if node.kind == "ToolButton" { 8.0 } else { 10.0 }),
44 authored_padding: padding.is_some(),
45 display,
46 spacing: dimension(node, "spacing").unwrap_or(6.0).max(0.0),
47 }
48 }
49
50 fn stacked(&self) -> bool {
51 self.display == ButtonDisplay::TextUnderIcon
52 }
53
54 fn gap(&self) -> f32 {
55 if self.icon.is_some() && !self.label.is_empty() {
56 self.spacing
57 } else {
58 0.0
59 }
60 }
61
62 pub fn natural_label_width(&self, width: f32) -> f32 {
63 if self.stacked() {
64 width.max(0.0)
65 } else {
66 (width - self.icon.as_ref().map_or(0.0, |(_, size)| *size) - self.gap()).max(0.0)
67 }
68 }
69
70 pub fn natural_size(&self, label: Size) -> Size {
71 let icon = self.icon.as_ref().map_or(0.0, |(_, size)| *size);
72 if self.stacked() {
73 Size::new(label.width.max(icon), label.height + icon + self.gap())
74 } else {
75 Size::new(label.width + icon + self.gap(), label.height.max(icon))
76 }
77 }
78
79 fn fitted_icon(&self, area: Rect, line_height: f32) -> f32 {
80 let mut height = area.height;
81 if self.stacked() && !self.label.is_empty() {
82 height -= line_height + self.gap();
83 }
84 self.icon
85 .as_ref()
86 .map_or(0.0, |(_, size)| size.min(area.width).min(height).max(0.0))
87 }
88
89 #[doc(hidden)]
91 pub fn scaled_label_metrics(&self, bounds: Rect, font_size: f32) -> (Rect, f32) {
92 let mut area = bounds.inset(self.padding);
93 if self.label.is_empty() {
94 return (area, font_size);
95 }
96 let line = font_size * 1.4;
97 let icon = if self.stacked() {
98 self.icon
99 .as_ref()
100 .map_or(0.0, |(_, size)| size.min(area.width))
101 } else {
102 0.0
103 };
104 let gap = if icon > 0.0 { self.gap() } else { 0.0 };
105 let preferred = line + icon + gap;
106 if preferred > area.height && !self.authored_padding {
107 let padding =
108 ((bounds.height - preferred) * 0.5).clamp(self.padding.min(4.0), self.padding);
109 area.y = bounds.y + padding;
110 area.height = (bounds.height - 2.0 * padding).max(0.0);
111 }
112 if preferred <= area.height {
113 return (area, font_size);
114 }
115 let label_height = (area.height - gap).max(0.0) * (line / (line + icon));
118 (area, (label_height / 1.4).max(1.0))
119 }
120
121 #[doc(hidden)]
122 pub fn label_height(&self, area: Rect, line_height: f32) -> f32 {
123 let icon = self.fitted_icon(area, line_height);
124 if self.stacked() && icon > 0.0 {
125 (area.height - icon - self.gap()).max(0.0)
126 } else {
127 area.height
128 }
129 }
130
131 pub fn label_width(&self, area: Rect, line_height: f32) -> f32 {
132 let size = self.fitted_icon(area, line_height);
133 if self.stacked() || size == 0.0 {
134 area.width
135 } else {
136 (area.width - size - self.gap()).max(0.0)
137 }
138 }
139
140 pub fn place(
141 &self,
142 area: Rect,
143 label: Size,
144 line_height: f32,
145 centered: bool,
146 direction: LayoutDirection,
147 ) -> ButtonPlacement {
148 let size = self.fitted_icon(area, line_height);
149 let right_to_left = direction == LayoutDirection::RightToLeft;
150 let align = |width: f32| {
151 if centered {
152 area.x + (area.width - width) * 0.5
153 } else if right_to_left {
154 area.x + area.width - width
155 } else {
156 area.x
157 }
158 };
159 if size == 0.0 {
160 return ButtonPlacement {
161 icon: None,
162 label: Rect::new(
163 area.x,
164 area.y + (area.height - label.height).max(0.0) * 0.5,
165 area.width,
166 area.height.min(label.height),
167 ),
168 };
169 }
170
171 let gap = self.gap();
172 if self.stacked() {
173 let label_height = label.height.min((area.height - size - gap).max(0.0));
174 let top = area.y + (area.height - size - gap - label_height).max(0.0) * 0.5;
175 ButtonPlacement {
176 icon: Some(Rect::new(align(size), top, size, size)),
177 label: Rect::new(area.x, top + size + gap, area.width, label_height),
178 }
179 } else {
180 let band = size + gap.min((area.width - size).max(0.0));
181 let available = (area.width - band).max(0.0);
182 let label_width = label.width.min(available);
183 let left = align(band + label_width);
184 let icon_left = if right_to_left {
185 left + label_width + band - size
186 } else {
187 left
188 };
189 let label_left = if right_to_left { left } else { left + band };
190 ButtonPlacement {
191 icon: Some(Rect::new(
192 icon_left,
193 area.y + (area.height - size) * 0.5,
194 size,
195 size,
196 )),
197 label: Rect::new(
198 label_left,
199 area.y + (area.height - label.height).max(0.0) * 0.5,
200 label_width,
201 area.height.min(label.height),
202 ),
203 }
204 }
205 }
206}