Skip to main content

compose_rust/
lib.rs

1#![deny(unsafe_op_in_unsafe_fn)]
2
3// `#[Composable]` expands into paths that start at `::compose_rust`, so that an application
4// that depends on this crate alone can use it. This makes the same paths resolve inside the
5// crate, where its own composables are written.
6extern crate self as compose_rust;
7
8pub mod animation;
9pub mod asset;
10pub mod boundary;
11/// Generated JNI shims. Compiled only for Android, where the Host is a cdylib that the
12/// Kotlin Activity loads.
13#[cfg(target_os = "android")]
14#[path = "boundary_jni.gen.rs"]
15mod boundary_jni;
16/// Generated wasm shims. Compiled only for the browser, where the page owns the loop and
17/// the Renderer's module owns the one linear memory both halves read.
18#[cfg(target_family = "wasm")]
19#[path = "boundary_wasm.gen.rs"]
20mod boundary_wasm;
21pub mod brush;
22pub mod clipboard;
23pub mod code;
24/// The rule the code colours of the two systems without a code editor scheme are derived
25/// by. Only the tests run it: the values themselves are literals in the token tables.
26#[cfg(test)]
27mod code_colours;
28pub mod contrast;
29#[cfg(target_family = "wasm")]
30#[doc(hidden)]
31pub use boundary_wasm::web_start as __web_start;
32#[doc(hidden)]
33pub mod codegen;
34pub mod design;
35pub mod drawing;
36mod extensions;
37pub mod file_dialog;
38pub mod fonts;
39pub mod highlight;
40pub mod input;
41pub mod measure;
42pub mod message;
43pub mod notification;
44pub mod palette;
45pub mod picked_file;
46pub mod protocol;
47pub mod runtime;
48pub mod schema;
49pub mod spans;
50pub mod storage;
51pub mod theme;
52pub mod tokens;
53pub mod ui;
54#[doc(hidden)]
55pub mod web_event_buffer;
56pub mod window;
57pub mod windows;
58mod writer;
59pub mod zoom;
60
61pub use animation::{
62    Animated, AnimationMoment, AnimationSpec, GraphicsLayer, animate_color_as_state,
63    animate_float_as_state, animate_offset_as_state, control_animation, reduced_motion,
64    start_animation, transition,
65};
66pub use asset::asset;
67pub use boundary::{
68    Host, LaunchBuilder, MutationBatch, RendererApi, RuntimeFactory, demo_theme, demo_theme_for,
69    install_renderer_api, launch_runtime, request_frame_from_worker,
70};
71pub use brush::{Brush, Stop, brush, release_brush};
72pub use code::{
73    CodeChange, CodeEditorColors, CodeHover, CodeMetrics, CodeRange, CodeScroll, Decoration,
74    Decorations, EditRejected, Fold, FoldChange, Folds, GutterDecoration, GutterDecorations,
75    Position, SaveRequest, SelectionChange, SyntaxSpan, SyntaxSpans, VisibleRange,
76};
77#[doc(hidden)]
78pub use compose_rust_macros::__compose_call_check;
79pub use design::design_system;
80pub use drawing::{DrawCommand, DrawList, DrawListBuilder};
81pub use file_dialog::{FileDialog, FileDialogResult, FileDialogSender};
82pub use fonts::{FontRef, FontRefs, FontStack};
83pub use input::{FileDrop, KeyEvent, PointerEvent, PointerKinds, RangeRequest, pointer_button};
84pub use message::{Message, show_message};
85pub use notification::{
86    Notification, NotificationActivation, NotificationSender, notification_permission,
87    request_notification_permission, withdraw_notification,
88};
89pub use palette::{Palette, PaletteViolation};
90pub use picked_file::{Delivery, FileError, PickedFile};
91pub use runtime::{Batch, Composable, ComposeHost, Recomposer, Runtime, application, launch};
92pub use schema::{
93    Alignment, Arrangement, AssetKind, ButtonKind, ButtonVariant, Chrome, Color, ColorRole,
94    ColorScheme, CompactNavigation, DecorationKind, DesignSystem, EventPayload, GenericFamily,
95    GutterDecorationKind, HoverPhase, IconRole, Key, LoopMode, MaterialRole, MessageDuration,
96    Modifier, MotionRole, NotificationImportance, NotificationPermission, NotificationPresentation,
97    OverflowWrap, Paint, PopoverAlign, PopoverPlacement, PropertyKind, RevealAlign, SCHEMA_HASH,
98    Selection, SemanticsRole, Severity, ShapeRole, SpaceRole, TextAlign, TextOverflow, Theme,
99    TileMode, TypeRole, WidgetKind, WindowHeightClass, WindowSizeClass, WordBreak,
100};
101pub use schema::{
102    AnimatedProperty, Animation, AnimationControl, AnimationEventKind, AnimationEvents,
103    ColorInterpolation, FillMode, Keyframe, KeyframeValue, PlayState, PlaybackDirection,
104    ReducedMotion, StepPosition, Timing, TransformFunction, TransformFunctionKind,
105};
106pub use schema::{Cursor, PointerKind, PointerType, UserSelect, WheelDeltaMode};
107pub use theme::ThemeHandle;
108pub use ui::{ScrollState, remember_scroll_state};
109pub use window::{NodeSize, WindowSafeArea, WindowSize, node_size, window_size};
110pub use windows::{
111    DragFromPointer, LastWindowClosed, WindowDragEvent, WindowDragOver, WindowDragOverKind,
112    WindowDragPhase, WindowEvent, WindowFrame, WindowHandle, WindowMode, WindowRequest,
113    current_window,
114};
115pub use zoom::{Zoom, ZoomHandle, zoom};
116
117/// Declares the entry point an iOS application starts at.
118///
119/// iOS is the one platform where the application is the library: the renderer is a
120/// Kotlin/Native archive and the two are linked into a single executable, so there is no
121/// Activity to load anything and no page to fetch anything. What there is instead is a
122/// `main`, and a `main` in an application bundle has to be C, so this exports the launch
123/// under a name that C can call.
124///
125/// ```ignore
126/// compose_rust::ios_main!(launch);
127/// ```
128///
129/// The export exists only in a build for iOS. Anywhere else nothing calls it, and an
130/// unconditional export would collide with the same name in every other application
131/// linked into one binary, which is what a benchmark that drives several of them does.
132#[macro_export]
133macro_rules! ios_main {
134    ($launch:path) => {
135        #[cfg(target_os = "ios")]
136        #[unsafe(no_mangle)]
137        pub extern "C" fn compose_rust_ios_main() -> i32 {
138            $launch();
139            0
140        }
141    };
142}
143
144/// Declares the Android entry point for an application whose root is a composable.
145///
146/// Android has no `main`: the Kotlin Activity owns the process and the frame loop, so the
147/// root is registered from `JNI_OnLoad`, which the generated shims reach through the
148/// symbol this defines.
149///
150/// ```ignore
151/// compose_rust::android_application!(app);
152/// ```
153///
154/// The export exists only in a build for Android, for the reason [`ios_main!`] gives.
155#[macro_export]
156macro_rules! android_application {
157    ($content:path) => {
158        $crate::android_application!($crate::LaunchBuilder::new(), $content);
159    };
160    ($builder:expr, $content:path) => {
161        #[cfg(target_os = "android")]
162        #[unsafe(no_mangle)]
163        pub extern "C" fn compose_rust_android_main() {
164            let _ = $builder
165                .with_mode($crate::LoopMode::Platform)
166                .try_launch($content);
167        }
168    };
169}
170
171/// Declares the browser entry point for an application whose root is a composable.
172///
173/// A page has no library loader and no `main` of its own: the Renderer's module owns the
174/// loop and calls this once both wasm modules exist. The export is defined where the root
175/// is because a browser refuses to instantiate a module with an import nobody supplies.
176///
177/// ```ignore
178/// compose_rust::web_application!(app);
179/// ```
180#[macro_export]
181macro_rules! web_application {
182    ($content:path) => {
183        $crate::web_application!($crate::LaunchBuilder::new(), $content);
184    };
185    ($builder:expr, $content:path) => {
186        #[cfg(target_family = "wasm")]
187        #[unsafe(no_mangle)]
188        pub extern "C" fn compose_rust_host_web_start() -> u32 {
189            $crate::__web_start($builder, $crate::runtime::recomposer_for($content))
190        }
191
192        /// Off the web nothing calls this, but the builder and the root are still type
193        /// checked. Not exported, so it collides with no other application in a binary.
194        #[cfg(not(target_family = "wasm"))]
195        #[allow(dead_code)]
196        fn __compose_rust_web_start_unused() -> u32 {
197            let _: fn() = $content;
198            let _ = $builder;
199            0
200        }
201    };
202}