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