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