Skip to main content

cranpose_ui/
lib.rs

1//! High level UI primitives built on top of the Compose core runtime.
2
3#![deny(unsafe_code)]
4
5use cranpose_core::{location_key, ApplierGuard, MemoryApplier, NodeError, NodeId, RuntimeHandle};
6pub use cranpose_core::{Composition, Key};
7pub use cranpose_macros::composable;
8use std::ops::{Deref, DerefMut};
9use std::rc::Rc;
10
11mod cursor_animation;
12mod debug;
13mod draw;
14pub mod fling_animation;
15mod focus_dispatch;
16mod interaction;
17mod key_event;
18pub mod layout;
19mod modifier;
20mod modifier_nodes;
21mod pointer_dispatch;
22mod primitives;
23mod render_state;
24mod renderer;
25pub mod scroll;
26mod subcompose_layout;
27pub mod text;
28pub mod text_field_focus;
29mod text_field_handler;
30mod text_field_input;
31mod text_field_modifier_node;
32pub mod text_layout_result;
33mod text_modifier_node;
34pub mod widgets;
35mod word_boundaries;
36
37// Export for cursor blink animation - AppShell checks this to continuously redraw
38pub use text_field_focus::has_focused_field;
39// Export cursor blink timing for WaitUntil scheduling
40pub use cursor_animation::{
41    is_cursor_visible, next_cursor_blink_time, reset_cursor_blink, start_cursor_blink,
42    stop_cursor_blink, tick_cursor_blink,
43};
44
45pub use cranpose_ui_graphics::{BlurredEdgeTreatment, ColorFilter, Dp, ImageBitmap, ImageSampling};
46pub use cranpose_ui_layout::IntrinsicSize;
47pub use draw::{execute_draw_commands, DrawCacheBuilder, DrawCommand};
48pub use focus_dispatch::{
49    active_focus_target, clear_focus_invalidations, has_pending_focus_invalidations,
50    process_focus_invalidations, schedule_focus_invalidation, set_active_focus_target,
51};
52pub use interaction::{
53    rememberMutableInteractionSource, Interaction, MutableInteractionSource, PressInteraction,
54    PressInteractionCancel, PressInteractionPress, PressInteractionRelease,
55};
56// Re-export FocusManager from cranpose-foundation to avoid duplication
57pub use cranpose_foundation::nodes::input::focus::FocusManager;
58pub use cranpose_foundation::{
59    DelegatableNode, ModifierNode, ModifierNodeElement, NodeCapabilities, NodeState,
60};
61pub use layout::{
62    build_layout_tree_from_applier, build_semantics_tree_from_applier,
63    build_semantics_tree_from_layout_tree,
64    core::{
65        Alignment, Arrangement, HorizontalAlignment, LinearArrangement, Measurable, Placeable,
66        VerticalAlignment,
67    },
68    measure_layout, measure_layout_with_options, tree_needs_layout, tree_needs_semantics,
69    LayoutAllocationDebugStats, LayoutBox, LayoutEngine, LayoutMeasurements, LayoutNodeData,
70    LayoutNodeKind, LayoutTree, MeasureLayoutOptions, SemanticsAction, SemanticsCallback,
71    SemanticsNode, SemanticsRole, SemanticsTree,
72};
73pub use modifier::{
74    collect_modifier_slices, collect_semantics_from_modifier, collect_slices_from_modifier,
75    BlendMode, Brush, Color, CompositingStrategy, CornerRadii, DpOffset, EdgeInsets,
76    FocusDirection, FocusRequester, GlassMaterial, GraphicsLayer, LayerShape, Modifier,
77    ModifierNodeSlices, ModifierNodeSlicesDebugStats, Point, PointerEvent, PointerEventKind,
78    PointerInputScope, Rect, RenderEffect, ResolvedBackground, ResolvedModifiers,
79    RoundedCornerShape, RuntimeShader, Shadow, ShadowScope, Size, TransformOrigin,
80};
81pub use modifier_nodes::{
82    AlphaElement, AlphaNode, BackgroundElement, BackgroundNode, ClickableElement, ClickableNode,
83    CornerShapeElement, CornerShapeNode, FillDirection, FillElement, FillNode, OffsetElement,
84    OffsetNode, PaddingElement, PaddingNode, SizeElement, SizeNode,
85};
86pub use pointer_dispatch::{
87    clear_pointer_repasses, has_pending_pointer_repasses, process_pointer_repasses,
88    schedule_pointer_repass,
89};
90pub use primitives::{
91    remember_svg, BasicText, BasicTextField, BasicTextFieldOptions, BasicTextWithOptions,
92    BitmapPainter, Box, BoxScope, BoxSpec, BoxWithConstraints, BoxWithConstraintsScope,
93    BoxWithConstraintsScopeImpl, Button, ButtonSpec, Canvas, Column, ColumnSpec, ContentScale,
94    ForEach, Image, Layout, LayoutNode, Painter, Row, RowSpec, Spacer, SubcomposeLayout,
95    SvgPainter, SvgPainterError, Text, TextWithOptions, DEFAULT_ALPHA,
96};
97// Lazy list exports - single source from cranpose-foundation
98pub use cranpose_foundation::lazy::{LazyListItemInfo, LazyListLayoutInfo, LazyListState};
99pub use key_event::{KeyCode, KeyEvent, KeyEventType, Modifiers};
100#[cfg(any(test, feature = "test-helpers"))]
101#[doc(hidden)]
102pub use render_state::reset_render_state_for_tests;
103pub use render_state::{
104    clear_transient_scroll_motion_contexts, current_density, debug_last_fling_velocity,
105    debug_reset_last_fling_velocity, has_current_app_context, has_pending_draw_repasses,
106    has_pending_layout_repasses, peek_focus_invalidation, peek_layout_invalidation,
107    peek_pointer_invalidation, peek_render_invalidation, pending_layout_repass_nodes_snapshot,
108    request_focus_invalidation, request_layout_invalidation, request_pointer_invalidation,
109    request_render_invalidation, schedule_draw_repass, schedule_layout_repass, set_density,
110    take_draw_repass_nodes, take_focus_invalidation, take_layout_invalidation,
111    take_layout_repass_nodes, take_pointer_invalidation, take_render_invalidation, AppContext,
112    AppContextScope,
113};
114pub use renderer::{HeadlessRenderer, PaintLayer, RecordedRenderScene, RenderOp};
115pub use scroll::{ScrollElement, ScrollNode, ScrollState};
116// Test utilities for fling velocity verification (only with test-helpers feature)
117#[cfg(feature = "test-helpers")]
118pub use modifier::{last_fling_velocity, reset_last_fling_velocity};
119pub use subcompose_layout::{
120    Constraints, MeasureResult, Placement, SubcomposeLayoutNode, SubcomposeLayoutScope,
121    SubcomposeMeasureScope, SubcomposeMeasureScopeImpl,
122};
123pub use text::{
124    get_cursor_x_for_offset, get_offset_for_position, layout_text, measure_text,
125    measure_text_for_node, measure_text_with_options, measure_text_with_options_for_node,
126    prepare_text_layout, prepare_text_layout_for_node, set_text_measurer, LinkAnnotation,
127    ParagraphStyle, PlatformParagraphStyle, PlatformSpanStyle, PlatformTextStyle,
128    PreparedTextLayout, SpanStyle, StringAnnotation, TextDrawStyle, TextLayoutOptions,
129    TextLayoutResult, TextLinePrefixWidths, TextMeasurer, TextMetrics, TextOptions, TextOverflow,
130    TextShaping, TextStyle,
131};
132pub use text_field_modifier_node::{TextFieldElement, TextFieldModifierNode};
133pub use text_modifier_node::{TextModifierElement, TextModifierNode};
134pub use widgets::clickable_text::ClickableText;
135pub use widgets::lazy_list::{LazyColumn, LazyColumnSpec, LazyRow, LazyRowSpec};
136pub use widgets::linked_text::LinkedText;
137
138// Debug utilities
139pub use debug::{
140    format_layout_tree, format_modifier_chain, format_render_scene, format_screen_summary,
141    install_modifier_chain_trace, log_layout_tree, log_modifier_chain, log_render_scene,
142    log_screen_summary, ModifierChainTraceGuard,
143};
144
145/// In-memory composition helper used by tests.
146pub struct TestComposition {
147    _scope: render_state::AppContextScope,
148    app_context: Rc<AppContext>,
149    composition: Composition<MemoryApplier>,
150}
151
152impl TestComposition {
153    pub fn root(&self) -> Option<NodeId> {
154        self.app_context.enter(|| self.composition.root())
155    }
156
157    pub fn runtime_handle(&self) -> RuntimeHandle {
158        self.app_context.enter(|| self.composition.runtime_handle())
159    }
160
161    pub fn should_render(&self) -> bool {
162        self.app_context.enter(|| self.composition.should_render())
163    }
164
165    pub fn take_root_render_request(&mut self) -> bool {
166        let app_context = Rc::clone(&self.app_context);
167        app_context.enter(|| self.composition.take_root_render_request())
168    }
169
170    pub fn flush_pending_node_updates(&mut self) -> Result<(), NodeError> {
171        let app_context = Rc::clone(&self.app_context);
172        app_context.enter(|| self.composition.flush_pending_node_updates())
173    }
174
175    pub fn process_invalid_scopes(&mut self) -> Result<bool, NodeError> {
176        let app_context = Rc::clone(&self.app_context);
177        app_context.enter(|| self.composition.process_invalid_scopes())
178    }
179
180    pub fn render(&mut self, root_key: Key, content: impl FnMut()) -> Result<(), NodeError> {
181        let app_context = Rc::clone(&self.app_context);
182        app_context.enter(|| self.composition.render(root_key, content))
183    }
184
185    pub fn applier_mut(&mut self) -> TestApplierGuard<'_> {
186        let scope = self.app_context.enter_scope();
187        let applier = self.composition.applier_mut();
188        TestApplierGuard {
189            _scope: scope,
190            applier,
191        }
192    }
193
194    pub fn with_app_context<R>(&self, block: impl FnOnce() -> R) -> R {
195        self.app_context.enter(block)
196    }
197}
198
199pub struct TestApplierGuard<'a> {
200    _scope: render_state::AppContextScope,
201    applier: ApplierGuard<'a, MemoryApplier>,
202}
203
204impl Deref for TestApplierGuard<'_> {
205    type Target = MemoryApplier;
206
207    fn deref(&self) -> &Self::Target {
208        &self.applier
209    }
210}
211
212impl DerefMut for TestApplierGuard<'_> {
213    fn deref_mut(&mut self) -> &mut Self::Target {
214        &mut self.applier
215    }
216}
217
218/// Build a composition with a simple in-memory applier and run the provided closure once.
219pub fn run_test_composition(build: impl FnMut()) -> TestComposition {
220    let app_context = AppContext::new();
221    app_context.enter(|| {
222        #[cfg(test)]
223        reset_render_state_for_tests();
224    });
225    let mut test_composition = TestComposition {
226        _scope: app_context.enter_scope(),
227        app_context,
228        composition: Composition::new(MemoryApplier::new()),
229    };
230    test_composition
231        .render(location_key(file!(), line!(), column!()), build)
232        .expect("initial render succeeds");
233    test_composition
234}
235
236pub use cranpose_core::MutableState as SnapshotState;
237
238#[cfg(test)]
239#[path = "tests/anchor_async_tests.rs"]
240mod anchor_async_tests;
241
242#[cfg(test)]
243#[path = "tests/async_runtime_full_layout_test.rs"]
244mod async_runtime_full_layout_test;
245
246#[cfg(test)]
247#[path = "tests/cursor_position_tests.rs"]
248mod cursor_position_tests;
249
250#[cfg(test)]
251#[path = "tests/tab_switching_tests.rs"]
252mod tab_switching_tests;
253
254#[cfg(test)]
255#[path = "tests/lazy_list_viewport_tests.rs"]
256mod lazy_list_viewport_tests;
257
258#[cfg(test)]
259#[path = "tests/lazy_list_recompose_tests.rs"]
260mod lazy_list_recompose_tests;