1#![allow(non_snake_case)]
2pub mod adaptive;
106pub mod anim;
107pub mod anim_ext;
108pub mod color_picker;
109pub mod gestures;
110pub mod layout;
111pub use layout::IntrinsicSizeMode;
112pub mod lazy;
113pub mod selection;
114pub mod subcompose;
115pub use lazy::{
116 LazyColumn, LazyHorizontalGrid, LazyRow, LazyVerticalGrid, LazyVerticalStaggeredGrid,
117 SimpleList,
118};
119pub mod lazy_states;
120pub use lazy_states::{
121 ItemHeight, LazyColumnConfig, LazyColumnState, LazyGridConfig, LazyGridState, LazyRowConfig,
122 LazyRowState, LazyVerticalStaggeredGridConfig, LazyVerticalStaggeredGridState,
123};
124pub use subcompose::{
125 BoxWithConstraints, SubcomposeLayout, box_with_constraints_with_key, subcompose_hash_key,
126 subcompose_layout_with_slots, subcompose_with_key, subcompose_with_key_slots,
127};
128pub mod overlay;
129pub mod pager;
130pub mod scroll;
131pub mod windowing;
132
133use std::cell::RefCell;
134use std::collections::{HashMap, HashSet};
135use std::rc::Rc;
136use std::sync::atomic::{AtomicU64, Ordering};
137
138use repose_core::*;
139use taffy::style::FlexDirection;
140
141pub mod textfield;
142use repose_core::locals;
143pub use selection::SelectableText;
144pub use textfield::{
145 BasicSecureTextField, BasicTextField, KeyboardOptions, TextFieldConfig, TextFieldState,
146};
147
148thread_local! {
149 static LAYOUT_ENGINE: RefCell<layout::LayoutEngine> =
150 RefCell::new(layout::LayoutEngine::new());
151}
152
153#[derive(Default)]
154pub struct Interactions {
155 pub hover: Option<u64>,
156 pub pressed: HashSet<u64>,
157}
158
159pub fn Box(modifier: Modifier) -> View {
160 View::new(0, ViewKind::Box).modifier(modifier)
161}
162
163pub fn Row(modifier: Modifier) -> View {
164 View::new(0, ViewKind::Row).modifier(modifier)
165}
166
167pub fn Column(modifier: Modifier) -> View {
168 View::new(0, ViewKind::Column).modifier(modifier)
169}
170
171pub fn FlowRow(modifier: Modifier) -> View {
174 Row(modifier.flex_wrap(FlexWrap::Wrap))
175}
176
177pub fn FlowColumn(modifier: Modifier) -> View {
180 Column(modifier.flex_wrap(FlexWrap::Wrap))
181}
182
183pub fn Center(modifier: Modifier) -> View {
185 Box(modifier.align_self(AlignSelf::Center))
186}
187
188pub fn Stack(modifier: Modifier) -> View {
189 View::new(0, ViewKind::Stack).modifier(modifier)
190}
191
192pub fn ZStack(modifier: Modifier) -> View {
193 View::new(0, ViewKind::ZStack).modifier(modifier)
194}
195
196pub fn OverlayHost(modifier: Modifier) -> View {
197 View::new(0, ViewKind::OverlayHost).modifier(modifier)
198}
199
200#[deprecated = "Use Modifier::vertical_scroll instead"]
201pub fn Scroll(modifier: Modifier) -> View {
202 View::new(0, ViewKind::Box)
203 .modifier(modifier.vertical_scroll(ScrollAxisBinding {
204 show_scrollbar: true,
205 ..Default::default()
206 }))
207}
208
209pub fn Text(text: impl Into<String>) -> View {
210 View::new(
211 0,
212 ViewKind::Text {
213 text: text.into(),
214 color: locals::content_color(),
215 font_size: 16.0, soft_wrap: true,
217 max_lines: None,
218 overflow: TextOverflow::Visible,
219 font_family: None,
220 annotations: None,
221 text_align: TextAlign::Start,
222 font_weight: FontWeight::NORMAL,
223 font_style: FontStyle::Normal,
224 text_decoration: TextDecoration::default(),
225 letter_spacing: 0.0,
226 line_height: 0.0,
227 },
228 )
229}
230
231pub fn AnnotatedText(annotated: AnnotatedString) -> View {
235 let annotations: Option<std::sync::Arc<[TextSpan]>> = if annotated.spans.is_empty() {
236 None
237 } else {
238 Some(annotated.spans.clone())
239 };
240 View::new(
241 0,
242 ViewKind::Text {
243 text: annotated.text,
244 color: locals::content_color(),
245 font_size: 16.0,
246 soft_wrap: true,
247 max_lines: None,
248 overflow: TextOverflow::Visible,
249 font_family: None,
250 annotations,
251 text_align: TextAlign::Start,
252 font_weight: FontWeight::NORMAL,
253 font_style: FontStyle::Normal,
254 text_decoration: TextDecoration::default(),
255 letter_spacing: 0.0,
256 line_height: 0.0,
257 },
258 )
259}
260
261pub fn Spacer() -> View {
262 Box(Modifier::new().flex_grow(1.0))
263}
264
265pub fn Space(modifier: Modifier) -> View {
266 Box(modifier)
267}
268
269pub fn Grid(
270 columns: usize,
271 modifier: Modifier,
272 children: Vec<View>,
273 row_gap: f32,
274 column_gap: f32,
275) -> View {
276 Column(modifier.grid(columns, row_gap, column_gap)).with_children(children)
277}
278
279pub fn Expander(modifier: Modifier, expanded: bool, on_toggle: impl Fn() + 'static) -> View {
280 View::new(
281 0,
282 ViewKind::Expander {
283 expanded,
284 on_toggle: Some(Rc::new(on_toggle)),
285 },
286 )
287 .modifier(modifier)
288}
289
290pub fn TreeRow(
296 modifier: Modifier,
297 depth: usize,
298 has_children: bool,
299 is_expanded: bool,
300 is_selected: bool,
301 on_toggle: impl Fn() + 'static,
302 on_select: impl Fn() + 'static,
303) -> View {
304 View::new(
305 0,
306 ViewKind::TreeRow {
307 depth,
308 has_children,
309 is_expanded,
310 is_selected,
311 on_toggle: Some(Rc::new(on_toggle)),
312 on_select: Some(Rc::new(on_select)),
313 },
314 )
315 .modifier(modifier)
316}
317
318static DRAGVALUE_COUNTER: AtomicU64 = AtomicU64::new(0);
319
320pub fn DragValue(
325 value: f32,
326 range: (f32, f32),
327 speed: f32,
328 on_change: impl Fn(f32) + 'static,
329) -> View {
330 let id = DRAGVALUE_COUNTER.fetch_add(1, Ordering::Relaxed);
331 let drag_start_x = remember_state_with_key(format!("dv_dsx_{}", id), || 0.0f32);
332 let drag_start_val = remember_state_with_key(format!("dv_dsv_{}", id), || 0.0f32);
333 let is_dragging = remember_state_with_key(format!("dv_drg_{}", id), || false);
334
335 let oc = Rc::new(on_change);
336 let min = range.0;
337 let max = range.1;
338 let cur = value;
339
340 let th = locals::theme();
341
342 Box(Modifier::new()
343 .min_width(48.0)
344 .height(28.0)
345 .background(th.surface_container)
346 .border(1.0, th.outline, 4.0)
347 .clip_rounded(4.0)
348 .padding_values(PaddingValues {
349 left: 4.0,
350 right: 4.0,
351 top: 0.0,
352 bottom: 0.0,
353 })
354 .on_pointer_down({
355 let dsx = drag_start_x.clone();
356 let dsv = drag_start_val.clone();
357 let drg = is_dragging.clone();
358 move |pe: PointerEvent| {
359 *drg.borrow_mut() = true;
360 *dsx.borrow_mut() = pe.position.x;
361 *dsv.borrow_mut() = cur;
362 }
363 })
364 .on_pointer_move({
365 let dsx = drag_start_x.clone();
366 let dsv = drag_start_val.clone();
367 let drg = is_dragging.clone();
368 let oc = oc.clone();
369 move |pe: PointerEvent| {
370 if !*drg.borrow() {
371 return;
372 }
373 let dx = pe.position.x - *dsx.borrow();
374 let new_val = (*dsv.borrow() + dx * speed).clamp(min, max);
375 (oc)(new_val);
376 }
377 })
378 .on_pointer_up({
379 let drg = is_dragging.clone();
380 move |_pe: PointerEvent| {
381 *drg.borrow_mut() = false;
382 }
383 })
384 .cursor(CursorIcon::EwResize))
385 .child(
386 Text(format_value(value))
387 .size(13.0)
388 .color(th.on_surface)
389 .single_line()
390 .overflow_ellipsize(),
391 )
392}
393
394fn format_value(v: f32) -> String {
395 if (v - v.round()).abs() < 1e-6 {
396 format!("{}", v.round() as i64)
397 } else if (v * 10.0 - (v * 10.0).round()).abs() < 1e-6 {
398 format!("{:.1}", v)
399 } else {
400 format!("{:.2}", v)
401 }
402}
403
404pub fn Image(modifier: Modifier, handle: ImageHandle) -> View {
405 View::new(
406 0,
407 ViewKind::Image {
408 handle,
409 tint: Color::WHITE,
410 fit: ImageFit::Contain,
411 },
412 )
413 .modifier(modifier)
414}
415
416pub trait ImageExt {
417 fn image_tint(self, c: Color) -> View;
418 fn image_fit(self, fit: ImageFit) -> View;
419}
420impl ImageExt for View {
421 fn image_tint(mut self, c: Color) -> View {
422 if let ViewKind::Image { tint, .. } = &mut self.kind {
423 *tint = c;
424 }
425 self
426 }
427 fn image_fit(mut self, fit: ImageFit) -> View {
428 if let ViewKind::Image { fit: f, .. } = &mut self.kind {
429 *f = fit;
430 }
431 self
432 }
433}
434
435fn flex_dir_for(kind: &ViewKind, modifier: &Modifier) -> Option<FlexDirection> {
436 if let Some(ref scroll) = modifier.scroll {
437 return Some(match scroll.axis() {
438 ScrollAxis::Vertical => FlexDirection::Column,
439 ScrollAxis::Horizontal => FlexDirection::Row,
440 ScrollAxis::Both => FlexDirection::Column,
441 });
442 }
443 match kind {
444 ViewKind::Row => {
445 if repose_core::locals::text_direction() == repose_core::locals::TextDirection::Rtl {
446 Some(FlexDirection::RowReverse)
447 } else {
448 Some(FlexDirection::Row)
449 }
450 }
451 ViewKind::Column => Some(FlexDirection::Column),
452 _ => None,
453 }
454}
455
456pub trait ViewExt: Sized {
458 fn child(self, children: impl IntoChildren) -> Self;
459}
460
461impl ViewExt for View {
462 fn child(self, children: impl IntoChildren) -> Self {
463 self.with_children(children.into_children())
464 }
465}
466
467pub trait IntoChildren {
468 fn into_children(self) -> Vec<View>;
469}
470
471impl IntoChildren for View {
472 fn into_children(self) -> Vec<View> {
473 vec![self]
474 }
475}
476
477impl IntoChildren for Vec<View> {
478 fn into_children(self) -> Vec<View> {
479 self
480 }
481}
482
483impl<const N: usize> IntoChildren for [View; N] {
484 fn into_children(self) -> Vec<View> {
485 self.into()
486 }
487}
488
489macro_rules! impl_into_children_tuple {
491 ($($idx:tt $t:ident),+) => {
492 impl<$($t: IntoChildren),+> IntoChildren for ($($t,)+) {
493 fn into_children(self) -> Vec<View> {
494 let mut v = Vec::new();
495 $(v.extend(self.$idx.into_children());)+
496 v
497 }
498 }
499 };
500}
501
502impl_into_children_tuple!(0 A);
503impl_into_children_tuple!(0 A, 1 B);
504impl_into_children_tuple!(0 A, 1 B, 2 C);
505impl_into_children_tuple!(0 A, 1 B, 2 C, 3 D);
506impl_into_children_tuple!(0 A, 1 B, 2 C, 3 D, 4 E);
507impl_into_children_tuple!(0 A, 1 B, 2 C, 3 D, 4 E, 5 F);
508impl_into_children_tuple!(0 A, 1 B, 2 C, 3 D, 4 E, 5 F, 6 G);
509impl_into_children_tuple!(0 A, 1 B, 2 C, 3 D, 4 E, 5 F, 6 G, 7 H);
510
511pub fn layout_and_paint(
513 root: &View,
514 size_px_u32: (u32, u32),
515 textfield_states: &HashMap<u64, Rc<RefCell<TextFieldState>>>,
516 interactions: &Interactions,
517 focused: Option<u64>,
518) -> (Scene, Vec<HitRegion>, Vec<SemNode>) {
519 LAYOUT_ENGINE.with(|engine| {
520 engine
521 .borrow_mut()
522 .layout_frame(root, size_px_u32, textfield_states, interactions, focused)
523 })
524}
525
526pub trait TextStyle {
528 fn color(self, c: Color) -> View;
529 fn size(self, px: f32) -> View;
530 fn max_lines(self, n: usize) -> View;
531 fn single_line(self) -> View;
532 fn overflow_ellipsize(self) -> View;
533 fn overflow_clip(self) -> View;
534 fn overflow_visible(self) -> View;
535 fn font_family(self, family: &'static str) -> View;
536 fn text_align(self, align: TextAlign) -> View;
537 fn font_weight(self, weight: FontWeight) -> View;
538 fn font_style(self, style: FontStyle) -> View;
539 fn text_decoration(self, decoration: TextDecoration) -> View;
540 fn letter_spacing(self, spacing: f32) -> View;
541 fn line_height(self, height: f32) -> View;
542}
543impl TextStyle for View {
544 fn color(mut self, c: Color) -> View {
545 if let ViewKind::Text {
546 color: text_color, ..
547 } = &mut self.kind
548 {
549 *text_color = c;
550 }
551 self
552 }
553 fn size(mut self, dp_font: f32) -> View {
554 if let ViewKind::Text {
555 font_size: text_size_dp,
556 ..
557 } = &mut self.kind
558 {
559 *text_size_dp = dp_font;
560 }
561 self
562 }
563 fn max_lines(mut self, n: usize) -> View {
564 if let ViewKind::Text {
565 max_lines,
566 soft_wrap,
567 ..
568 } = &mut self.kind
569 {
570 *max_lines = Some(n);
571 *soft_wrap = true;
572 }
573 self
574 }
575 fn single_line(mut self) -> View {
576 if let ViewKind::Text {
577 soft_wrap,
578 max_lines,
579 ..
580 } = &mut self.kind
581 {
582 *soft_wrap = false;
583 *max_lines = Some(1);
584 }
585 self
586 }
587 fn overflow_ellipsize(mut self) -> View {
588 if let ViewKind::Text { overflow, .. } = &mut self.kind {
589 *overflow = TextOverflow::Ellipsis;
590 }
591 self
592 }
593 fn overflow_clip(mut self) -> View {
594 if let ViewKind::Text { overflow, .. } = &mut self.kind {
595 *overflow = TextOverflow::Clip;
596 }
597 self
598 }
599 fn overflow_visible(mut self) -> View {
600 if let ViewKind::Text { overflow, .. } = &mut self.kind {
601 *overflow = TextOverflow::Visible;
602 }
603 self
604 }
605 fn font_family(mut self, family: &'static str) -> View {
606 if let ViewKind::Text {
607 font_family: ff, ..
608 } = &mut self.kind
609 {
610 *ff = Some(family);
611 }
612 self
613 }
614 fn text_align(mut self, align: TextAlign) -> View {
615 if let ViewKind::Text { text_align, .. } = &mut self.kind {
616 *text_align = align;
617 }
618 self
619 }
620 fn font_weight(mut self, weight: FontWeight) -> View {
621 if let ViewKind::Text { font_weight, .. } = &mut self.kind {
622 *font_weight = weight;
623 }
624 self
625 }
626 fn font_style(mut self, style: FontStyle) -> View {
627 if let ViewKind::Text { font_style, .. } = &mut self.kind {
628 *font_style = style;
629 }
630 self
631 }
632 fn text_decoration(mut self, decoration: TextDecoration) -> View {
633 if let ViewKind::Text {
634 text_decoration, ..
635 } = &mut self.kind
636 {
637 *text_decoration = decoration;
638 }
639 self
640 }
641 fn letter_spacing(mut self, spacing: f32) -> View {
642 if let ViewKind::Text { letter_spacing, .. } = &mut self.kind {
643 *letter_spacing = spacing;
644 }
645 self
646 }
647 fn line_height(mut self, height: f32) -> View {
648 if let ViewKind::Text { line_height, .. } = &mut self.kind {
649 *line_height = height;
650 }
651 self
652 }
653}