| 1 | // Copyright © SixtyFPS GmbH <info@slint.dev> |
| 2 | // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0 |
| 3 | |
| 4 | #![doc = include_str!("README.md" )] |
| 5 | #![doc (html_logo_url = "https://slint.dev/logo/slint-logo-square-light.svg" )] |
| 6 | #![cfg_attr (not(target_os = "android" ), allow(rustdoc::broken_intra_doc_links))] |
| 7 | #![cfg (target_os = "android" )] |
| 8 | |
| 9 | mod androidwindowadapter; |
| 10 | mod javahelper; |
| 11 | |
| 12 | #[cfg (all(not(feature = "aa-06" ), feature = "aa-05" ))] |
| 13 | pub use android_activity_05 as android_activity; |
| 14 | #[cfg (feature = "aa-06" )] |
| 15 | pub use android_activity_06 as android_activity; |
| 16 | |
| 17 | pub use android_activity::AndroidApp; |
| 18 | use android_activity::PollEvent; |
| 19 | use androidwindowadapter::AndroidWindowAdapter; |
| 20 | use core::ops::ControlFlow; |
| 21 | use core::time::Duration; |
| 22 | use i_slint_core::api::{EventLoopError, PlatformError}; |
| 23 | use i_slint_core::platform::{Clipboard, WindowAdapter}; |
| 24 | use i_slint_renderer_skia::SkiaRendererExt; |
| 25 | use std::cell::RefCell; |
| 26 | use std::rc::{Rc, Weak}; |
| 27 | use std::sync::{Arc, Mutex}; |
| 28 | |
| 29 | thread_local! { |
| 30 | static CURRENT_WINDOW: RefCell<Weak<AndroidWindowAdapter>> = RefCell::new(Default::default()); |
| 31 | } |
| 32 | |
| 33 | pub struct AndroidPlatform { |
| 34 | app: AndroidApp, |
| 35 | window: Rc<AndroidWindowAdapter>, |
| 36 | event_listener: Option<Box<dyn Fn(&PollEvent<'_>)>>, |
| 37 | } |
| 38 | |
| 39 | impl AndroidPlatform { |
| 40 | /// Instantiate a new Android backend given the [`android_activity::AndroidApp`] |
| 41 | /// |
| 42 | /// Pass the returned value to [`slint::platform::set_platform()`](`i_slint_core::platform::set_platform()`) |
| 43 | /// |
| 44 | /// # Example |
| 45 | /// ``` |
| 46 | /// #[cfg(target_os = "android")] |
| 47 | /// #[unsafe(no_mangle)] |
| 48 | /// fn android_main(app: i_slint_backend_android_activity::AndroidApp) { |
| 49 | /// slint::platform::set_platform(Box::new( |
| 50 | /// i_slint_backend_android_activity::AndroidPlatform::new(app), |
| 51 | /// )) |
| 52 | /// .unwrap(); |
| 53 | /// // ... your slint application ... |
| 54 | /// } |
| 55 | /// ``` |
| 56 | pub fn new(app: AndroidApp) -> Self { |
| 57 | let window = AndroidWindowAdapter::new(app.clone()); |
| 58 | CURRENT_WINDOW.set(Rc::downgrade(&window)); |
| 59 | Self { app, window, event_listener: None } |
| 60 | } |
| 61 | |
| 62 | /// Instantiate a new Android backend given the [`android_activity::AndroidApp`] |
| 63 | /// and a function to process the events. |
| 64 | /// |
| 65 | /// This is the same as [`AndroidPlatform::new()`], but it allow you to get notified |
| 66 | /// of events. |
| 67 | /// |
| 68 | /// Pass the returned value to [`slint::platform::set_platform()`](`i_slint_core::platform::set_platform()`) |
| 69 | /// |
| 70 | /// # Example |
| 71 | /// ``` |
| 72 | /// #[cfg(target_os = "android")] |
| 73 | /// #[unsafe(no_mangle)] |
| 74 | /// fn android_main(app: i_slint_backend_android_activity::AndroidApp) { |
| 75 | /// slint::platform::set_platform(Box::new( |
| 76 | /// i_slint_backend_android_activity::AndroidPlatform::new_with_event_listener( |
| 77 | /// app, |
| 78 | /// |event| { eprintln!("got event {event:?}") } |
| 79 | /// ), |
| 80 | /// )) |
| 81 | /// .unwrap(); |
| 82 | /// // ... your slint application ... |
| 83 | /// } |
| 84 | /// ``` |
| 85 | pub fn new_with_event_listener( |
| 86 | app: AndroidApp, |
| 87 | listener: impl Fn(&PollEvent<'_>) + 'static, |
| 88 | ) -> Self { |
| 89 | let mut this = Self::new(app); |
| 90 | this.event_listener = Some(Box::new(listener)); |
| 91 | this |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | impl i_slint_core::platform::Platform for AndroidPlatform { |
| 96 | fn create_window_adapter(&self) -> Result<Rc<dyn WindowAdapter>, PlatformError> { |
| 97 | Ok(self.window.clone()) |
| 98 | } |
| 99 | fn run_event_loop(&self) -> Result<(), PlatformError> { |
| 100 | loop { |
| 101 | let mut timeout = i_slint_core::platform::duration_until_next_timer_update(); |
| 102 | if self.window.window.has_active_animations() { |
| 103 | // FIXME: we should not hardcode a value here |
| 104 | let frame_duration = Duration::from_millis(10); |
| 105 | timeout = Some(match timeout { |
| 106 | Some(x) => x.min(frame_duration), |
| 107 | None => frame_duration, |
| 108 | }) |
| 109 | } |
| 110 | let mut r = Ok(ControlFlow::Continue(())); |
| 111 | self.app.poll_events(timeout, |e| { |
| 112 | i_slint_core::platform::update_timers_and_animations(); |
| 113 | r = self.window.process_event(&e); |
| 114 | if let Some(event_listener) = &self.event_listener { |
| 115 | event_listener(&e) |
| 116 | } |
| 117 | }); |
| 118 | if r?.is_break() { |
| 119 | break; |
| 120 | } |
| 121 | if self.window.pending_redraw.take() { |
| 122 | self.window.do_render()?; |
| 123 | } |
| 124 | } |
| 125 | Ok(()) |
| 126 | } |
| 127 | |
| 128 | fn new_event_loop_proxy(&self) -> Option<Box<dyn i_slint_core::platform::EventLoopProxy>> { |
| 129 | Some(Box::new(AndroidEventLoopProxy { |
| 130 | event_queue: self.window.event_queue.clone(), |
| 131 | waker: self.app.create_waker(), |
| 132 | })) |
| 133 | } |
| 134 | |
| 135 | fn set_clipboard_text(&self, text: &str, clipboard: Clipboard) { |
| 136 | if clipboard == Clipboard::DefaultClipboard { |
| 137 | self.window |
| 138 | .java_helper |
| 139 | .set_clipboard(text) |
| 140 | .unwrap_or_else(|e| javahelper::print_jni_error(&self.app, e)); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | fn clipboard_text(&self, clipboard: Clipboard) -> Option<String> { |
| 145 | if clipboard == Clipboard::DefaultClipboard { |
| 146 | Some( |
| 147 | self.window |
| 148 | .java_helper |
| 149 | .get_clipboard() |
| 150 | .unwrap_or_else(|e| javahelper::print_jni_error(&self.app, e)), |
| 151 | ) |
| 152 | } else { |
| 153 | None |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | fn long_press_interval(&self, _: i_slint_core::InternalToken) -> Duration { |
| 158 | self.window.java_helper.long_press_timeout().unwrap_or(Duration::from_millis(500)) |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | enum Event { |
| 163 | Quit, |
| 164 | Other(Box<dyn FnOnce() + Send + 'static>), |
| 165 | } |
| 166 | |
| 167 | type EventQueue = Arc<Mutex<Vec<Event>>>; |
| 168 | |
| 169 | struct AndroidEventLoopProxy { |
| 170 | event_queue: EventQueue, |
| 171 | waker: android_activity::AndroidAppWaker, |
| 172 | } |
| 173 | |
| 174 | impl i_slint_core::platform::EventLoopProxy for AndroidEventLoopProxy { |
| 175 | fn quit_event_loop(&self) -> Result<(), EventLoopError> { |
| 176 | self.event_queue.lock().unwrap().push(Event::Quit); |
| 177 | self.waker.wake(); |
| 178 | Ok(()) |
| 179 | } |
| 180 | |
| 181 | fn invoke_from_event_loop( |
| 182 | &self, |
| 183 | event: Box<dyn FnOnce() + Send>, |
| 184 | ) -> Result<(), EventLoopError> { |
| 185 | self.event_queue.lock().unwrap().push(Event::Other(event)); |
| 186 | self.waker.wake(); |
| 187 | Ok(()) |
| 188 | } |
| 189 | } |
| 190 | |