compo-platform-loop 0.1.3

Cross-platform event loop implementation for the Compo declarative and reactive component framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
//! Platform-specific event loop implementations for the Compo framework.
//!
//! This module provides cross-platform event loop integration that allows Compo
//! applications to run natively on different operating systems. Each platform
//! uses its native event loop mechanism:
//!
//! - **Windows**: Win32 message loop with PeekMessage for non-blocking processing
//! - **macOS**: NSApplication with NSRunLoop and NSTimer for periodic polling
//! - **iOS**: NSRunLoop with NSTimer for periodic polling (without NSApplication)
//! - **Android**: JNI integration with Java MainLoop for Android event system
//!
//! The module exports platform-appropriate `run` functions that initialize the
//! Compo runtime and integrate it with the platform's native event loop.

use compo::prelude::*;
#[cfg(windows)]
use windows::Win32::UI::WindowsAndMessaging::{
    DispatchMessageW, MSG, PM_REMOVE, PeekMessageW, TranslateMessage, WM_QUIT,
};
#[cfg(target_os = "ios")]
use {
    block2::RcBlock,
    objc2::{ClassType, msg_send},
    objc2_foundation::{NSRunLoop, NSString, NSTimer},
};
#[cfg(target_os = "android")]
use {
    jni::{
        AttachGuard, JNIEnv, JavaVM, NativeMethod,
        errors::{Error, JniError, Result as JniResult},
    },
    std::{
        any::Any,
        cell::{Cell, RefCell},
        ptr::null_mut,
    },
    tracing::error,
};

#[cfg(target_os = "macos")]
use {
    block2::RcBlock,
    objc2::{ClassType, msg_send},
    objc2_foundation::{NSRunLoop, NSString, NSTimer},
};

// Thread-local storage for Android runtime and component management.
//
// On Android, we use thread-local storage to maintain the Compo runtime and
// component instances. This is necessary because Android's JNI callbacks
// need access to the runtime from the same thread where it was created.
#[cfg(target_os = "android")]
thread_local! {
    /// The Compo runtime instance for processing async tasks
    static RT: Rc<Runtime<'static, ()>> = Rc::new(Runtime::new());
    /// Storage for the root component instance
    static COMPONENT: Cell<Rc<dyn Any>> = Cell::new(Rc::new(()));
    /// Storage for the JavaVM instance to allow JNI calls from any thread
    ///
    /// This is used to store the JavaVM instance obtained during JNI_OnLoad
    /// so that we can attach threads to the JVM when needed for callbacks.
    /// The RefCell allows mutable access to the JavaVM instance when attaching
    /// new threads or performing JNI operations.
    static JAVA_VM: RefCell<JniResult<JavaVM>> = RefCell::new(unsafe { JavaVM::from_raw(null_mut()) });
}

/// Handles Windows message processing for the event loop.
///
/// This function processes Windows messages using PeekMessage instead of GetMessage
/// to avoid blocking the event loop. It handles WM_QUIT messages for graceful shutdown
/// and dispatches other messages to their appropriate window procedures.
///
/// # Arguments
/// * `r#loop` - Reference to the Loop instance for controlling the event loop
#[cfg(windows)]
fn handle_windows_message(r#loop: &Loop) {
    // Use PeekMessage instead of GetMessage because GetMessage blocks until a message is available
    unsafe {
        let mut msg = MSG::default();
        // Check if there are messages in the queue without blocking
        while PeekMessageW(&mut msg, None, 0, 0, PM_REMOVE).as_bool() {
            // If it's a WM_QUIT message, exit the loop
            if msg.message == WM_QUIT {
                r#loop.quit();
                break;
            }

            // Translate virtual key messages
            let _ = TranslateMessage(&msg);
            // Dispatch message to window procedure
            DispatchMessageW(&msg);
        }
    }
}

/// Runs the platform-specific event loop with the given entry component.
///
/// This function initializes and starts the appropriate event loop for the current platform:
/// - **Windows**: Uses Win32 message loop with PeekMessage for non-blocking message processing
/// - **macOS**: Uses NSApplication with NSRunLoop and NSTimer for periodic runtime polling
/// - **iOS**: Uses NSRunLoop with NSTimer for periodic runtime polling (without NSApplication)
///
/// The function creates a Compo runtime, spawns the entry component as an async task,
/// and integrates with the platform's native event loop to ensure proper execution
/// of async components.
///
/// # Type Parameters
/// * `C` - The component type that implements `Component<'a>`
/// * `F` - The async function type that takes a `Weak<C>` and returns a future
///
/// # Arguments
/// * `entry` - The entry point async function that will be executed as the root component
///
/// # Platform-specific behavior
/// - **Windows**: Registers a message handler and runs the Loop with Windows message processing
/// - **macOS**: Creates NSApplication, sets up a timer for runtime polling, and runs the app loop
/// - **iOS**: Sets up NSRunLoop with a timer for runtime polling (suitable for iOS apps)
///
/// # Examples
/// ```rust
/// //! Platform-specific event loop implementations for the Compo framework.
/// //!
/// //! This module provides cross-platform event loop integration that allows Compo
/// //! applications to run natively on different operating systems. Each platform
/// //! uses its native event loop mechanism:
/// //!
/// //! - **Windows**: Win32 message loop with PeekMessage for non-blocking processing
/// //! - **macOS**: NSApplication with NSRunLoop and NSTimer for periodic polling
/// //! - **iOS**: NSRunLoop with NSTimer for periodic polling (without NSApplication)
/// //! - **Android**: JNI integration with Java MainLoop for Android event system
/// //!
/// //! The module exports platform-appropriate `run` functions that initialize the
/// //! Compo runtime and integrate it with the platform's native event loop.
///
/// use compo::prelude::*;
/// use compo_platform_loop::prelude::run;
///
/// #[component]
/// async fn app() {
///     println!("Hello from Compo!");
/// }
///
/// fn main() {
///     run(app);
/// }
/// ```
#[cfg(not(target_os = "android"))]
pub fn run<'a, C, F>(entry: F)
where
    C: Component<'a> + 'a,
    F: AsyncFn(Weak<C>) + 'a,
{
    #[cfg(windows)]
    Loop::new()
        .register_poll_handler(handle_windows_message)
        .run(entry);

    #[cfg(target_os = "ios")]
    {
        // 创建运行时和组件
        let rt = Rc::new(Runtime::new());
        let rt_weak = Rc::downgrade(&rt);
        let c = Rc::new(C::new(rt_weak.clone()));
        let c_weak = Rc::downgrade(&c);

        // 启动异步任务
        rt.spawn(async move { entry(c_weak).await });

        // 获取主线程的运行循环
        let run_loop = NSRunLoop::mainRunLoop();

        // 创建一个定时器,用于定期轮询 Runtime
        let poll_block = RcBlock::new(move || {
            // 轮询 Runtime 以推进异步任务
            if let Some(rt) = rt_weak.upgrade() {
                rt.poll_all();
            }
        });

        // 创建一个重复的定时器,每 0.01 秒轮询一次
        let timer: *mut NSTimer = unsafe {
            msg_send![NSTimer::class(),
                scheduledTimerWithTimeInterval: 0.01,
                repeats: true,
                block: &*poll_block
            ]
        };

        // 将定时器添加到运行循环中
        let mode = NSString::from_str("NSDefaultRunLoopMode");
        let _: () = unsafe { msg_send![&run_loop, addTimer: timer, forMode: &*mode] };

        // 运行主循环,这会阻塞当前线程
        #[cfg(not(feature = "application"))]
        run_loop.run();
        #[cfg(feature = "application")]
        if let Some(mtm) = objc2::MainThreadMarker::new() {
            objc2_ui_kit::UIApplication::main(None, None, mtm)
        } else {
            tracing::error!("Can't run on non-main-thread.")
        }
    }

    #[cfg(target_os = "macos")]
    {
        // 创建运行时和组件
        let rt = Rc::new(Runtime::new());
        let rt_weak = Rc::downgrade(&rt);
        let c = Rc::new(C::new(rt_weak.clone()));
        let c_weak = Rc::downgrade(&c);

        // 启动异步任务
        rt.spawn(async move { entry(c_weak).await });

        // 获取主线程的运行循环
        let run_loop = NSRunLoop::mainRunLoop();

        // 创建一个定时器,用于定期轮询 Runtime
        let poll_block = RcBlock::new(move || {
            // 轮询 Runtime 以推进异步任务
            if let Some(rt) = rt_weak.upgrade() {
                rt.poll_all();
            }
        });

        // 创建一个重复的定时器,每 0.01 秒轮询一次
        let timer: *mut NSTimer = unsafe {
            msg_send![NSTimer::class(),
                scheduledTimerWithTimeInterval: 0.01,
                repeats: true,
                block: &*poll_block
            ]
        };

        // 将定时器添加到运行循环中
        // 创建 NSDefaultRunLoopMode 字符串
        let mode = NSString::from_str("NSDefaultRunLoopMode");
        let _: () = unsafe { msg_send![&run_loop, addTimer: timer, forMode: &*mode] };

        #[cfg(not(feature = "application"))]
        run_loop.run();
        #[cfg(feature = "application")]
        if let Some(mtm) = objc2::MainThreadMarker::new() {
            let app = objc2_app_kit::NSApplication::sharedApplication(mtm);
            app.activate();

            // 运行应用程序主循环,这会阻塞当前线程
            app.run();
        } else {
            tracing::error!("Can't run on non-main-thread.")
        }
    }
}

/// Runs the Android-specific event loop with JNI integration.
///
/// This function sets up the event loop for Android applications using JNI (Java Native Interface).
/// It creates a Compo runtime in thread-local storage, spawns the entry component, and integrates
/// with the Android Java MainLoop class for proper event loop execution.
///
/// The function registers a native method `poll_all` that can be called from Java to advance
/// the async runtime, enabling proper integration with Android's event system.
///
/// # Type Parameters
/// * `C` - The component type that implements `Component<'static>` (must be 'static for Android)
/// * `F` - The async function type that takes a `Weak<C>` and returns a future (must be 'static)
///
/// # Arguments
/// * `vm` - The JavaVM instance provided by the Android runtime
/// * `entry` - The entry point async function that will be executed as the root component
///
/// # Android Integration
/// This function:
/// 1. Creates a thread-local Compo runtime and component
/// 2. Spawns the entry component as an async task
/// 3. Calls the Java `MainLoop.run()` method to start the Android event loop
/// 4. Registers the native `poll_all` method for runtime advancement
///
/// # Examples
/// ```rust
/// //! Platform-specific event loop implementations for the Compo framework.
/// //!
/// //! This module provides cross-platform event loop integration that allows Compo
/// //! applications to run natively on different operating systems. Each platform
/// //! uses its native event loop mechanism:
/// //!
/// //! - **Windows**: Win32 message loop with PeekMessage for non-blocking processing
/// //! - **macOS**: NSApplication with NSRunLoop and NSTimer for periodic polling
/// //! - **iOS**: NSRunLoop with NSTimer for periodic polling (without NSApplication)
/// //! - **Android**: JNI integration with Java MainLoop for Android event system
/// //!
/// //! The module exports platform-appropriate `run` functions that initialize the
/// //! Compo runtime and integrate it with the platform's native event loop.
///
/// use compo::prelude::*;
/// use compo_platform_loop::prelude::run;
/// use jni::JavaVM;
///
/// #[component]
/// async fn app() {
///     println!("Hello from Android!");
/// }
///
/// fn main(vm: JavaVM) {
///     run(vm, app);
/// }
/// ```
#[cfg(target_os = "android")]
pub fn run<C, F>(vm: JavaVM, entry: F)
where
    C: Component<'static> + 'static,
    F: AsyncFn(Weak<C>) + 'static,
{
    JAVA_VM.set(Ok(vm));
    RT.with(|rt| {
        let rt_weak = Rc::downgrade(rt);
        let c = Rc::new(C::new(rt_weak.clone()));
        let c_weak = Rc::downgrade(&c);

        // 启动异步任务
        rt.spawn(async move { entry(c_weak).await });
        COMPONENT.set(c);
    });

    if let Err(e) = vm_exec(|mut env| {
        const CLASS: &str = "rust/compo/MainLoop";
        env.call_static_method(CLASS, "run", "()V", &[])?;
        let method = NativeMethod {
            name: "poll_all".into(),
            sig: "()V".into(),
            fn_ptr: poll_all as *mut _,
        };
        env.register_native_methods(CLASS, &[method])
    }) {
        error!(?e, "Run failed.");
    }
}

/// Native method called from Java to advance the Compo runtime.
///
/// This function is registered as a JNI native method and called by the Android
/// Java MainLoop to poll and advance all pending async tasks in the Compo runtime.
/// It accesses the thread-local runtime and calls `poll_all()` to process any
/// ready futures.
///
/// # Safety
/// This function is marked as `unsafe` because it's a C-style callback function
/// that will be called from Java via JNI. The JNI environment parameter is
/// currently unused but required by the JNI interface.
///
/// # Arguments
/// * `_env` - The JNI environment (unused in current implementation)
#[cfg(target_os = "android")]
unsafe extern "C" fn poll_all(_env: JNIEnv) {
    RT.with(|rt| rt.poll_all());
}

/// Executes a closure with an attached JNI environment, handling all possible JNI errors.
///
/// This function provides a safe wrapper around JNI operations by:
/// 1. Borrowing the JavaVM instance from thread-local storage
/// 2. Attaching the current thread to the JVM
/// 3. Executing the provided closure with the JNI environment
/// 4. Properly propagating any JNI errors that occur
///
/// # Type Parameters
/// * `F` - The closure type that takes an `AttachGuard` and returns a `JniResult<R>`
/// * `R` - The result type returned by the closure
///
/// # Arguments
/// * `f` - The closure to execute with the JNI environment
///
/// # Error Handling
/// Converts all possible JNI error variants into the unified `JniResult` type,
/// including thread attachment failures and JNI method invocation errors.
#[cfg(target_os = "android")]
pub fn vm_exec<F, R>(f: F) -> JniResult<R>
where
    F: for<'a> FnOnce(AttachGuard<'a>) -> JniResult<R>,
{
    JAVA_VM.with_borrow_mut(move |vm| match vm {
        Ok(vm) => match vm.attach_current_thread() {
            Ok(env) => f(env),
            Err(e) => Err(e),
        },
        Err(e) => Err(match e {
            Error::WrongJValueType(e1, e2) => Error::WrongJValueType(e1, e2),
            Error::InvalidCtorReturn => Error::InvalidCtorReturn,
            Error::InvalidArgList(e) => Error::InvalidArgList(e.to_owned()),
            Error::MethodNotFound { name, sig } => Error::MethodNotFound {
                name: name.to_owned(),
                sig: sig.to_owned(),
            },
            Error::FieldNotFound { name, sig } => Error::FieldNotFound {
                name: name.to_owned(),
                sig: sig.to_owned(),
            },
            Error::JavaException => Error::JavaException,
            Error::JNIEnvMethodNotFound(e) => Error::JNIEnvMethodNotFound(e),
            Error::NullPtr(e) => Error::NullPtr(e),
            Error::NullDeref(e) => Error::NullDeref(e),
            Error::TryLock => Error::TryLock,
            Error::JavaVMMethodNotFound(e) => Error::JavaVMMethodNotFound(e),
            Error::FieldAlreadySet(e) => Error::FieldAlreadySet(e.to_owned()),
            Error::ThrowFailed(e) => Error::ThrowFailed(e.to_owned()),
            Error::ParseFailed(e1, e2) => Error::ParseFailed(e1.to_owned(), e2.to_owned()),
            Error::JniCall(e) => Error::JniCall(match e {
                JniError::Unknown => JniError::Unknown,
                JniError::ThreadDetached => JniError::ThreadDetached,
                JniError::WrongVersion => JniError::WrongVersion,
                JniError::NoMemory => JniError::NoMemory,
                JniError::AlreadyCreated => JniError::AlreadyCreated,
                JniError::InvalidArguments => JniError::InvalidArguments,
                JniError::Other(e) => JniError::Other(*e),
            }),
        }),
    })
}