compo-platform-loop

Crates.iocompo-platform-loop
lib.rscompo-platform-loop
version0.1.3
created_at2025-10-17 04:12:46.968935+00
updated_at2025-10-22 19:33:14.562663+00
descriptionCross-platform event loop implementation for the Compo declarative and reactive component framework
homepage
repositoryhttps://github.com/mzdk100/compo-platform-loop.git
max_upload_size
id1887100
size53,299
SmileSky (mzdk100)

documentation

README

Compo Platform Loop

中文文档


A cross-platform event loop implementation for the Compo declarative and reactive component framework. This library provides platform-specific event loop integration for Windows, macOS, iOS, and Android, enabling Compo applications to run natively on different operating systems.

Features

  • Cross-Platform Support: Native event loop integration for Windows, macOS, iOS, and Android
  • Compo Integration: Seamlessly integrates with the Compo component framework
  • Platform-Specific Optimizations: Uses native APIs for optimal performance on each platform
  • Async Runtime: Built on Compo's single-threaded async runtime for high performance
  • Zero Dependencies: Minimal external dependencies, leveraging platform-native APIs

Platform Support

Platform Status Event Loop Implementation
Windows Win32 Message Loop
macOS NSRunLoop with NSTimer
iOS NSRunLoop with NSTimer
Android JNI with Java MainLoop

Quick Start

Installation

Add this to your Cargo.toml:

cargo add compo compo-platform-loop

Basic Usage

use compo::prelude::*;
use compo_platform_loop::prelude::run;
use tracing::info;

#[component]
async fn hello() {
    println!("Hello, world!");
    info!("Hello, world!");
}

#[cfg(not(target_os = "android"))]
fn main() {
    run(hello);
}

#[cfg(target_os = "android")]
fn main(vm: jni::JavaVM) {
    run(vm, hello);
}

Examples

The repository includes examples for different platforms:

Desktop (Windows/macOS/Linux)

cd examples/desktop
cargo run

Android

cd examples/android
# See examples/android/README.md for detailed setup instructions
./run.sh  # or run.bat on Windows

iOS

cd examples/ios
# See examples/ios/README.md for detailed setup instructions
# Open CompoPlatformLoopExample.xcodeproj in Xcode and run

Platform-Specific Details

Windows

  • Uses Win32 PeekMessageW for non-blocking message polling
  • Integrates with Windows message pump for native window handling
  • Supports WM_QUIT message for graceful shutdown

macOS

  • Uses NSRunLoop with NSTimer for periodic runtime polling
  • Integrates with NSApplication for native app lifecycle
  • Runs on the main thread with 0.01s polling interval

iOS

  • Similar to macOS but without NSApplication
  • Uses NSRunLoop with NSTimer for runtime polling
  • Designed for iOS app lifecycle integration

Android

  • Uses JNI to bridge between Rust and Java
  • Java MainLoop class handles the Android event loop
  • Native methods registered for runtime polling

API Reference

run Function

The main entry point for starting the platform-specific event loop:

// For most platforms
pub fn run<'a, C, F>(entry: F)
where
    C: Component<'a> + 'a,
    F: AsyncFn(Weak<C>) + 'a,

// For Android
pub fn run<C, F>(vm: JavaVM, entry: F)
where
    C: Component<'static> + 'static,
    F: AsyncFn(Weak<C>) + 'static,

Building for Different Platforms

Desktop

cargo build --release

Android

Requires Android NDK and appropriate toolchain setup:

cargo apk2 build --release

iOS

Requires Xcode and iOS toolchain:

cargo build --release --target aarch64-apple-ios

Contributing

Contributions are welcome! Please feel free to submit issues and pull requests.

License

Apache-2.0

Related Projects

  • Compo - The core declarative and reactive component framework
  • cargo-apk2 - A command-line tool for easily building Android applications with Cargo
Commit count: 0

cargo fmt