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