1use std::hash::Hash;
4use std::rc::Rc;
5
6use crate::accessibility::{Node, Role};
7use crate::color::Color;
8use crate::context::{Context, Id, Response, Sense};
9use crate::geometry::{Rect, Vec2, vec2};
10use crate::material::{Corner, Glass, Material};
11use crate::paint::Painter;
12use crate::text::{TextLayout, TextStyle, Weight};
13use crate::theme::Surface;
14
15#[derive(Clone, Copy, Debug, PartialEq, Eq)]
17pub enum Align {
18 Start,
20 Center,
22 End,
24}
25
26#[derive(Clone, Copy, Debug, PartialEq)]
27enum Dir {
28 Vertical,
29 Horizontal {
31 row: f32,
32 },
33}
34
35pub trait Widget {
37 fn ui(self, ui: &mut Ui) -> Response;
39}
40
41pub struct Ui<'a> {
48 pub ctx: &'a mut Context,
50 pub layer: usize,
52 pub clip: Rect,
54 pub mask: u32,
56 pub opacity: f32,
58 pub max_rect: Rect,
60 pub cursor: Vec2,
62 pub spacing: f32,
64 pub align: Align,
66 pub enabled: bool,
68 pub surface: Surface,
70 pub id: Id,
72 dir: Dir,
73 content: Rect,
74 auto: u64,
75 first: bool,
76}
77
78#[derive(Clone, Copy, Default)]
79struct SizeMem(Vec2);
80
81impl<'a> Ui<'a> {
82 pub fn new(ctx: &'a mut Context, layer: usize, id: Id, max: Rect) -> Self {
86 let spacing = ctx.theme.spacing;
87 let surface = Surface::Content { dark: ctx.theme.dark };
88 Ui {
89 ctx,
90 layer,
91 clip: Rect::EVERYTHING,
92 mask: 0,
93 opacity: 1.0,
94 max_rect: max,
95 cursor: max.min,
96 spacing,
97 align: Align::Start,
98 enabled: true,
99 surface,
100 id,
101 dir: Dir::Vertical,
102 content: Rect::from_min_size(max.min, Vec2::ZERO),
103 auto: 0,
104 first: true,
105 }
106 }
107
108 fn sub(&mut self, id: Id, max: Rect, dir: Dir) -> Ui<'_> {
109 Ui {
110 layer: self.layer,
111 clip: self.clip,
112 mask: self.mask,
113 opacity: self.opacity,
114 max_rect: max,
115 cursor: max.min,
116 spacing: self.spacing,
117 align: self.align,
118 enabled: self.enabled,
119 surface: self.surface,
120 id,
121 dir,
122 content: Rect::from_min_size(max.min, Vec2::ZERO),
123 auto: 0,
124 first: true,
125 ctx: self.ctx,
126 }
127 }
128
129 pub fn next_id(&mut self) -> Id {
132 self.auto += 1;
133 self.id.with(self.auto)
134 }
135 pub fn make_id(&self, source: impl Hash) -> Id {
137 self.id.with(source)
138 }
139
140 pub fn available_width(&self) -> f32 {
142 (self.max_rect.max.x - self.cursor.x).max(0.0)
143 }
144 pub fn available_height(&self) -> f32 {
146 (self.max_rect.max.y - self.cursor.y).max(0.0)
147 }
148 pub fn available(&self) -> Rect {
150 Rect { min: self.cursor, max: self.max_rect.max.max(self.cursor) }
151 }
152 pub fn used(&self) -> Rect {
154 self.content
155 }
156 pub fn is_horizontal(&self) -> bool {
158 matches!(self.dir, Dir::Horizontal { .. })
159 }
160
161 pub fn allocate(&mut self, size: Vec2) -> Rect {
163 let gap = if self.first { 0.0 } else { self.spacing };
164 self.first = false;
165 let r = match self.dir {
166 Dir::Vertical => {
167 self.cursor.y += gap;
168 let free = (self.max_rect.width() - size.x).max(0.0);
169 let x = match self.align {
170 Align::Start => self.max_rect.min.x,
171 Align::Center => self.max_rect.min.x + free * 0.5,
172 Align::End => self.max_rect.min.x + free,
173 };
174 let r = Rect::from_min_size(vec2(x, self.cursor.y), size);
175 self.cursor.y += size.y;
176 r
177 }
178 Dir::Horizontal { row } => {
179 self.cursor.x += gap;
180 let y = self.max_rect.min.y + ((row - size.y) * 0.5).max(0.0);
181 let r = Rect::from_min_size(vec2(self.cursor.x, y), size);
182 self.cursor.x += size.x;
183 r
184 }
185 };
186 self.content = self.content.union(r);
187 let p = self.ctx.pixels_per_point;
189 let snap = |v: f32| (v * p).round() / p;
190 Rect { min: vec2(snap(r.min.x), snap(r.min.y)), max: vec2(snap(r.max.x), snap(r.max.y)) }
191 }
192
193 pub fn next_pos(&self) -> Vec2 {
195 let gap = if self.first { 0.0 } else { self.spacing };
196 match self.dir {
197 Dir::Vertical => vec2(self.max_rect.min.x, self.cursor.y + gap),
198 Dir::Horizontal { .. } => vec2(self.cursor.x + gap, self.max_rect.min.y),
199 }
200 }
201
202 pub fn allocate_response(&mut self, size: Vec2, sense: Sense) -> Response {
204 let r = self.allocate(size);
205 let id = self.next_id();
206 self.interact(id, r, sense)
207 }
208
209 pub fn interact(&mut self, id: Id, rect: Rect, sense: Sense) -> Response {
211 if !self.enabled {
212 let mut res = self.ctx.interact(id, rect, self.clip, self.layer, Sense::Hover);
213 res.hovered = false;
214 return res;
215 }
216 self.ctx.interact(id, rect, self.clip, self.layer, sense)
217 }
218
219 pub fn reserve(&mut self, size: Vec2) {
221 self.content.max = self.content.max.max(self.max_rect.min + size);
222 match self.dir {
223 Dir::Vertical => self.cursor.y = self.cursor.y.max(self.content.max.y),
224 Dir::Horizontal { .. } => self.cursor.x = self.cursor.x.max(self.content.max.x),
225 }
226 }
227
228 pub fn space(&mut self, amount: f32) {
230 match self.dir {
231 Dir::Vertical => self.cursor.y += amount,
232 Dir::Horizontal { .. } => self.cursor.x += amount,
233 }
234 self.content.max = self.content.max.max(vec2(
235 if self.is_horizontal() { self.cursor.x } else { self.content.max.x },
236 if self.is_horizontal() { self.content.max.y } else { self.cursor.y },
237 ));
238 }
239
240 pub fn painter(&mut self) -> Painter<'_> {
242 let (l, c, m, o) = (self.layer, self.clip, self.mask, self.opacity);
243 let o = if self.enabled { o } else { o * 0.4 };
244 let mut p = self.ctx.painter(l, c);
245 p.mask = m;
246 p.opacity = o;
247 p
248 }
249
250 pub fn layout_text(&mut self, text: &str, style: &TextStyle) -> Rc<TextLayout> {
252 self.ctx.text.layout(text, style)
253 }
254
255 pub fn add(&mut self, widget: impl Widget) -> Response {
258 widget.ui(self)
259 }
260
261 pub fn access(&mut self, id: Id, role: Role, rect: Rect) -> Option<&mut Node> {
273 let enabled = self.enabled;
274 let n = self.ctx.access_node(id, role, rect, self.layer)?;
275 n.disabled = !enabled;
276 Some(n)
277 }
278
279 pub fn add_enabled<R>(&mut self, enabled: bool, f: impl FnOnce(&mut Ui) -> R) -> R {
281 let old = self.enabled;
282 self.enabled &= enabled;
283 let r = f(self);
284 self.enabled = old;
285 r
286 }
287
288 pub(crate) fn size_of(&mut self, id: Id) -> Vec2 {
291 match self.ctx.try_get::<SizeMem>(id) {
292 Some(s) => s.0,
293 None => {
294 self.ctx.request_relayout();
295 Vec2::ZERO
296 }
297 }
298 }
299 pub(crate) fn remember(&mut self, id: Id, size: Vec2) {
300 let old = self.ctx.try_get::<SizeMem>(id).map(|s| s.0);
301 if old.is_none_or(|o| (o - size).length() > 0.5) {
302 if old.is_some() {
303 self.ctx.request_repaint();
304 }
305 self.ctx.set(id, SizeMem(size));
306 } else {
307 self.ctx.set(id, SizeMem(old.unwrap()));
308 }
309 }
310
311 pub fn horizontal<R>(&mut self, f: impl FnOnce(&mut Ui) -> R) -> R {
313 self.row(Align::Start, f)
314 }
315 pub fn horizontal_centered<R>(&mut self, f: impl FnOnce(&mut Ui) -> R) -> R {
317 self.row(Align::Center, f)
318 }
319 pub fn horizontal_right<R>(&mut self, f: impl FnOnce(&mut Ui) -> R) -> R {
321 self.row(Align::End, f)
322 }
323
324 fn row<R>(&mut self, align: Align, f: impl FnOnce(&mut Ui) -> R) -> R {
325 let id = self.next_id();
326 let prev = self.size_of(id);
327 let avail = self.available_width();
328 let gap = if self.first { 0.0 } else { self.spacing };
329 let start = self.cursor + if self.is_horizontal() { vec2(gap, 0.0) } else { vec2(0.0, gap) };
330 let x = match align {
331 Align::Start => start.x,
332 Align::Center => start.x + ((avail - prev.x) * 0.5).max(0.0),
333 Align::End => start.x + (avail - prev.x).max(0.0),
334 };
335 let row = prev.y;
336 let y = match self.dir {
338 Dir::Horizontal { row: outer } => self.max_rect.min.y + ((outer - row) * 0.5).max(0.0),
339 Dir::Vertical => start.y,
340 };
341 let max = Rect { min: vec2(x, y), max: vec2(self.max_rect.max.x.max(x), self.max_rect.max.y) };
342 let mut ui = self.sub(id, max, Dir::Horizontal { row });
343 ui.align = Align::Start;
344 let out = f(&mut ui);
345 let used = ui.content.size();
346 self.remember(id, used);
347 let taken = match (align, self.dir) {
348 (Align::Start, _) | (_, Dir::Horizontal { .. }) => used,
349 _ => vec2(avail, used.y),
350 };
351 self.allocate(taken);
352 out
353 }
354
355 pub fn vertical<R>(&mut self, width: f32, f: impl FnOnce(&mut Ui) -> R) -> R {
357 let id = self.next_id();
358 let prev = self.size_of(id);
359 let gap = if self.first { 0.0 } else { self.spacing };
360 let start = match self.dir {
361 Dir::Horizontal { row } => {
362 vec2(self.cursor.x + gap, self.max_rect.min.y + ((row - prev.y) * 0.5).max(0.0))
363 }
364 Dir::Vertical => self.cursor + vec2(0.0, gap),
365 };
366 let max = Rect { min: start, max: vec2(start.x + width, self.max_rect.max.y) };
367 let mut ui = self.sub(id, max, Dir::Vertical);
368 let out = f(&mut ui);
369 let used = ui.content.size();
370 self.remember(id, used);
371 self.allocate(vec2(width, used.y));
372 out
373 }
374
375 pub fn scope<R>(&mut self, f: impl FnOnce(&mut Ui) -> R) -> (R, Rect) {
378 let id = self.next_id();
379 self.scope_id(id, f)
380 }
381
382 pub fn scope_id<R>(&mut self, id: Id, f: impl FnOnce(&mut Ui) -> R) -> (R, Rect) {
385 let prev = self.size_of(id);
386 let gap = if self.first { 0.0 } else { self.spacing };
387 let start = match self.dir {
388 Dir::Horizontal { row } => {
389 vec2(self.cursor.x + gap, self.max_rect.min.y + ((row - prev.y) * 0.5).max(0.0))
390 }
391 Dir::Vertical => self.cursor + vec2(0.0, gap),
392 };
393 let max = Rect { min: start, max: self.max_rect.max.max(start) };
394 let mut ui = self.sub(id, max, Dir::Vertical);
395 let out = f(&mut ui);
396 let used = ui.content.size();
397 self.remember(id, used);
398 let align = std::mem::replace(&mut self.align, Align::Start);
399 let r = self.allocate(used);
400 self.align = align;
401 (out, r)
402 }
403
404 pub fn grid(&mut self, columns: usize, gap: f32, mut f: impl FnMut(&mut Ui, usize)) {
407 let n = columns;
408 let id = self.next_id();
409 let w = ((self.available_width() - gap * (n as f32 - 1.0)) / n as f32).max(1.0);
410 let g = if self.first { 0.0 } else { self.spacing };
411 let top = self.cursor.y + g;
412 let left = self.cursor.x;
413 let mut h: f32 = 0.0;
414 for i in 0..n {
415 let min = vec2(left + i as f32 * (w + gap), top);
416 let max = Rect { min, max: vec2(min.x + w, self.max_rect.max.y) };
417 let mut ui = self.sub(id.with(i), max, Dir::Vertical);
418 f(&mut ui, i);
419 h = h.max(ui.content.height());
420 }
421 self.allocate(vec2(w * n as f32 + gap * (n as f32 - 1.0), h));
422 }
423
424 pub fn padded<R>(&mut self, padding: Vec2, f: impl FnOnce(&mut Ui) -> R) -> R {
426 let pad = padding;
427 let id = self.next_id();
428 let gap = if self.first || self.is_horizontal() { 0.0 } else { self.spacing };
429 let start = self.cursor + vec2(0.0, gap);
430 let max = Rect { min: start + pad, max: vec2(self.max_rect.max.x - pad.x, self.max_rect.max.y) };
431 let mut ui = self.sub(id, max, Dir::Vertical);
432 let out = f(&mut ui);
433 let used = ui.content.size();
434 self.allocate(vec2(self.available_width(), used.y + pad.y * 2.0));
435 out
436 }
437
438 pub fn in_rect<R>(&mut self, id: impl Hash, rect: Rect, f: impl FnOnce(&mut Ui) -> R) -> R {
440 let r = rect;
441 let id = self.id.with(id);
442 let mut ui = self.sub(id, r, Dir::Vertical);
443 f(&mut ui)
444 }
445
446 pub fn in_rect_sized<R>(&mut self, id: impl Hash, rect: Rect, f: impl FnOnce(&mut Ui) -> R) -> (R, Vec2) {
448 let r = rect;
449 let id = self.id.with(id);
450 let mut ui = self.sub(id, r, Dir::Vertical);
451 let out = f(&mut ui);
452 (out, ui.content.size())
453 }
454
455 pub fn row_in_rect<R>(&mut self, id: impl Hash, rect: Rect, f: impl FnOnce(&mut Ui) -> R) -> R {
458 let r = rect;
459 let id = self.id.with(id);
460 let mut ui = self.sub(id, r, Dir::Horizontal { row: r.height() });
461 f(&mut ui)
462 }
463
464 pub(crate) fn row_sized<R>(&mut self, id: Id, r: Rect, f: impl FnOnce(&mut Ui) -> R) -> (R, Vec2) {
465 let mut ui = self.sub(id, r, Dir::Horizontal { row: r.height() });
466 let out = f(&mut ui);
467 (out, ui.content.size())
468 }
469
470 pub fn card<R>(&mut self, f: impl FnOnce(&mut Ui) -> R) -> R {
473 let id = self.next_id();
474 let w = self.available_width();
475 let gap = if self.first || self.is_horizontal() { 0.0 } else { self.spacing };
476 let start = self.cursor + vec2(0.0, gap);
477 let h = self.size_of(id.with("card")).y;
478 let r = Rect::from_min_size(start, vec2(w, h));
479 let fill =
480 if self.surface.darkness() > 0.5 { Color::gray(0.13).alpha(0.88) } else { Color::white(0.86) };
481 self.painter().fill(r, 20.0, fill);
482 let max = Rect { min: start + vec2(18.0, 16.0), max: vec2(start.x + w - 18.0, self.max_rect.max.y) };
483 let mut ui = self.sub(id, max, Dir::Vertical);
484 let out = f(&mut ui);
485 let used = ui.content.height() + 32.0;
486 self.remember(id.with("card"), vec2(w, used));
487 self.allocate(vec2(w, used));
488 out
489 }
490
491 pub fn glass_panel<R>(&mut self, material: Material, f: impl FnOnce(&mut Ui) -> R) -> R {
494 let id = self.next_id();
495 let w = self.available_width();
496 let gap = if self.first || self.is_horizontal() { 0.0 } else { self.spacing };
497 let start = self.cursor + vec2(0.0, gap);
498 let h = self.size_of(id.with("card")).y;
499 let r = Rect::from_min_size(start, vec2(w, h));
500 let surface = self.glass(id, r, Corner::Radius(24.0), material, false);
501 let max = Rect { min: start + vec2(18.0, 16.0), max: vec2(start.x + w - 18.0, self.max_rect.max.y) };
502 let mut ui = self.sub(id, max, Dir::Vertical);
503 ui.surface = surface;
504 let out = f(&mut ui);
505 let used = ui.content.height() + 32.0;
506 self.remember(id.with("card"), vec2(w, used));
507 self.allocate(vec2(w, used));
508 out
509 }
510
511 pub fn glass(
514 &mut self,
515 id: Id,
516 rect: Rect,
517 corner: impl Into<Corner>,
518 material: Material,
519 adaptive: bool,
520 ) -> Surface {
521 let mut g = Glass::new(rect, corner, material);
522 let a = self.ctx.prepare_glass(id, &mut g, adaptive);
523 self.painter().glass(&g);
524 a.surface()
525 }
526
527 pub fn label(&mut self, text: &str) -> Response {
531 let c = self.surface.text_color();
532 self.styled_text(text, TextStyle::new(self.ctx.theme.body_size), c)
533 }
534 pub fn secondary(&mut self, text: &str) -> Response {
536 let c = self.surface.secondary_color();
537 self.styled_text(text, TextStyle::new(self.ctx.theme.body_size), c)
538 }
539 pub fn caption(&mut self, text: &str) -> Response {
541 let c = self.surface.secondary_color();
542 self.styled_text(text, TextStyle::new(self.ctx.theme.caption_size), c)
543 }
544 pub fn strong(&mut self, text: &str) -> Response {
546 let c = self.surface.text_color();
547 self.styled_text(text, TextStyle::new(self.ctx.theme.body_size).weight(Weight::Semibold), c)
548 }
549 pub fn heading(&mut self, text: &str) -> Response {
551 let c = self.surface.text_color();
552 let st = TextStyle::new(self.ctx.theme.title_size).weight(Weight::Bold);
553 self.text_role(text, st, c, Role::Heading)
554 }
555 pub fn subheading(&mut self, text: &str) -> Response {
557 let c = self.surface.text_color();
558 self.styled_text(text, TextStyle::new(17.0).weight(Weight::Semibold), c)
559 }
560
561 pub fn styled_text(&mut self, text: &str, style: TextStyle, color: Color) -> Response {
563 self.text_role(text, style, color, Role::Label)
564 }
565
566 fn text_role(&mut self, text: &str, mut st: TextStyle, c: Color, role: Role) -> Response {
567 if !self.is_horizontal() && st.wrap.is_none() {
568 st.wrap = Some(self.max_rect.width().max(10.0));
569 }
570 let l = self.layout_text(text, &st);
571 let r = self.allocate(l.size);
572 self.painter().text(r.min, &l, c);
573 let id = self.next_id();
574 if let Some(n) = self.access(id, role, r) {
575 n.label(text);
576 }
577 Response {
578 id,
579 rect: r,
580 hovered: self.ctx.input.pointer.is_some_and(|p| r.contains(p)),
581 held: false,
582 pressed: false,
583 released: false,
584 clicked: false,
585 double_clicked: false,
586 secondary_clicked: false,
587 focused: false,
588 changed: false,
589 delta: Vec2::ZERO,
590 pointer: self.ctx.input.pointer,
591 }
592 }
593
594 pub fn separator(&mut self) {
596 let c = self.surface.separator_color();
597 match self.dir {
598 Dir::Vertical => {
599 let w = self.available_width();
600 let r = self.allocate(vec2(w, 1.0));
601 self.painter().fill(r, 0.0, c);
602 }
603 Dir::Horizontal { row } => {
604 let h = row.max(20.0) * 0.6;
605 let r = self.allocate(vec2(1.0, h));
606 self.painter().fill(r, 0.0, c);
607 }
608 }
609 }
610
611 pub fn focus_ring(&mut self, id: Id, rect: Rect, corner: impl Into<Corner>) {
614 if self.ctx.shows_focus(id) {
615 let c = self.ctx.theme.accent;
616 self.painter().stroke(rect.expand(3.5), corner, 2.5, c.alpha(0.85));
617 }
618 }
619}