flutter_engine_context/
lib.rs

1#![allow(clippy::new_without_default)]
2
3use std::{cell::Cell, marker::PhantomData, sync::MutexGuard};
4
5#[cfg(target_os = "android")]
6#[path = "android.rs"]
7pub mod platform;
8
9#[cfg(target_os = "windows")]
10#[path = "windows.rs"]
11pub mod platform;
12
13#[cfg(target_os = "linux")]
14#[path = "linux.rs"]
15pub mod platform;
16
17#[cfg(any(target_os = "ios", target_os = "macos"))]
18#[path = "darwin.rs"]
19pub mod platform;
20
21pub type FlutterEngineContextError = platform::Error;
22pub type FlutterEngineContextResult<T> = Result<T, FlutterEngineContextError>;
23
24pub type FlutterView = platform::FlutterView;
25pub type FlutterTextureRegistry = platform::FlutterTextureRegistry;
26pub type FlutterBinaryMessenger = platform::FlutterBinaryMessenger;
27#[cfg(target_os = "android")]
28pub type Activity = platform::Activity;
29
30type PhantomUnsync = PhantomData<Cell<()>>;
31type PhantomUnsend = PhantomData<MutexGuard<'static, ()>>;
32
33pub struct FlutterEngineContext {
34    platform_context: platform::PlatformContext,
35    _unsync: PhantomUnsync,
36    _unsend: PhantomUnsend,
37}
38
39impl FlutterEngineContext {
40    #[cfg(not(target_os = "android"))]
41    /// Creates new FlutterEngineContext instance.
42    /// Must be called on platform thread.
43    pub fn new() -> Self {
44        Self {
45            platform_context: platform::PlatformContext::new(),
46            _unsync: PhantomData,
47            _unsend: PhantomData,
48        }
49    }
50
51    #[cfg(target_os = "android")]
52    /// Creates new FlutterEngineContext instance.
53    /// Must be called on platform thread.
54    pub fn new(
55        env: &jni::JNIEnv,
56        class_loader: jni::objects::JObject,
57    ) -> FlutterEngineContextResult<Self> {
58        Ok(Self {
59            platform_context: platform::PlatformContext::new(env, class_loader)?,
60            _unsync: PhantomData,
61            _unsend: PhantomData,
62        })
63    }
64
65    /// Returns flutter view for given engine handle.
66    pub fn get_flutter_view(
67        &self,
68        handle: i64,
69    ) -> FlutterEngineContextResult<platform::FlutterView> {
70        self.platform_context.get_flutter_view(handle)
71    }
72
73    /// Returns texture registry for given engine handle.
74    pub fn get_texture_registry(
75        &self,
76        handle: i64,
77    ) -> FlutterEngineContextResult<FlutterTextureRegistry> {
78        self.platform_context.get_texture_registry(handle)
79    }
80
81    /// Returns binary messenger for given engine handle.
82    pub fn get_binary_messenger(
83        &self,
84        handle: i64,
85    ) -> FlutterEngineContextResult<FlutterBinaryMessenger> {
86        self.platform_context.get_binary_messenger(handle)
87    }
88
89    /// Returns android activity for given handle.
90    #[cfg(target_os = "android")]
91    pub fn get_activity(&self, handle: i64) -> FlutterEngineContextResult<Activity> {
92        self.platform_context.get_activity(handle)
93    }
94}