use std::io::Read;
use std::path::{Path, PathBuf};
use crate::AssetFileEntry;
use crate::error::PlatformError;
use super::device::{Device, DeviceHardware};
use super::file::FileService;
use super::location::Location;
use super::media_interaction::{MediaInteraction, MediaKind};
use super::media_runtime::MediaRuntime;
use super::network::Network;
use super::secure_store::SecureStore;
use super::ui::{SurfacePresenter, UIUpdate, UserFeedback};
use super::update::UpdateService;
use super::wifi::Wifi;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AnimationType {
None = 0,
Forward = 1,
Backward = 2,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum LxAppOpenMode {
#[default]
Normal = 0,
Panel = 1,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OpenUrlTarget {
External = 0,
SelfTarget = 1,
NewBrowserTab = 2,
}
impl OpenUrlTarget {
pub fn parse(raw: Option<&str>) -> Self {
match raw.map(|v| v.trim().to_ascii_lowercase()) {
Some(v) if v == "self" => Self::SelfTarget,
Some(v) if v == "new_browser_tab" => Self::NewBrowserTab,
Some(v) if v == "external" => Self::External,
Some(v) => {
log::warn!("Invalid openURL target='{}', fallback to external", v);
Self::External
}
None => Self::External,
}
}
}
#[derive(Debug, Clone)]
pub struct OpenUrlRequest {
pub owner_appid: String,
pub owner_session_id: u64,
pub url: String,
pub target: OpenUrlTarget,
}
#[cfg(test)]
mod tests {
use super::OpenUrlTarget;
#[test]
fn parse_supports_new_browser_tab() {
assert_eq!(
OpenUrlTarget::parse(Some("new_browser_tab")),
OpenUrlTarget::NewBrowserTab
);
}
#[test]
fn parse_unknown_falls_back_to_external() {
assert_eq!(
OpenUrlTarget::parse(Some("foobar")),
OpenUrlTarget::External
);
}
}
impl From<i32> for AnimationType {
fn from(value: i32) -> Self {
match value {
1 => AnimationType::Forward,
2 => AnimationType::Backward,
_ => AnimationType::None,
}
}
}
pub trait AppRuntime:
Send
+ Sync
+ MediaInteraction
+ MediaRuntime
+ Network
+ SurfacePresenter
+ Device
+ DeviceHardware
+ SecureStore
+ FileService
+ Location
+ UIUpdate
+ UpdateService
+ UserFeedback
+ Wifi
+ 'static
{
fn read_asset<'a>(&'a self, path: &str) -> Result<Box<dyn Read + 'a>, PlatformError>;
fn asset_dir_iter<'a>(
&'a self,
asset_dir: &str,
) -> Box<dyn Iterator<Item = Result<AssetFileEntry<'a>, PlatformError>> + 'a>;
fn app_data_dir(&self) -> PathBuf;
fn app_cache_dir(&self) -> PathBuf;
fn get_app_identifier(&self) -> Result<String, PlatformError>;
fn copy_album_media_to_file(
&self,
uri: &str,
dest_path: &Path,
kind: MediaKind,
) -> Result<(), PlatformError> {
MediaRuntime::copy_album_media_to_file(self, uri, dest_path, kind)
}
fn get_system_locale(&self) -> &str;
fn show_lxapp(
&self,
appid: String,
path: String,
session_id: u64,
open_mode: LxAppOpenMode,
panel_id: String,
) -> Result<(), PlatformError>;
fn hide_lxapp(&self, appid: String, session_id: u64) -> Result<(), PlatformError>;
fn exit(&self) -> Result<(), PlatformError>;
fn navigate(
&self,
appid: String,
path: String,
animation_type: AnimationType,
) -> Result<(), PlatformError>;
fn open_url(&self, req: OpenUrlRequest) -> Result<(), PlatformError>;
fn get_capsule_rect(
&self,
) -> impl std::future::Future<Output = Result<String, PlatformError>> + Send;
}