Skip to main content

lingxia_platform/traits/
file.rs

1use std::future::Future;
2use std::path::Path;
3
4use crate::error::PlatformError;
5
6#[derive(Debug, Clone)]
7pub struct OpenFileRequest {
8    pub path: String,
9    pub mime_type: Option<String>,
10    pub show_menu: Option<bool>,
11}
12
13impl OpenFileRequest {
14    pub fn is_pdf_like(&self) -> bool {
15        self.mime_type
16            .as_deref()
17            .map(|mime| mime.eq_ignore_ascii_case("application/pdf"))
18            .unwrap_or(false)
19            || Path::new(&self.path)
20                .extension()
21                .and_then(|ext| ext.to_str())
22                .map(|ext| ext.eq_ignore_ascii_case("pdf"))
23                .unwrap_or(false)
24    }
25}
26
27#[derive(Debug, Clone)]
28pub struct RevealInFileManagerRequest {
29    pub path: String,
30}
31
32#[derive(Debug, Clone)]
33pub struct FileDialogFilter {
34    pub name: Option<String>,
35    pub extensions: Vec<String>,
36}
37
38#[derive(Debug, Clone)]
39pub struct ChooseFileRequest {
40    pub multiple: bool,
41    pub filters: Vec<FileDialogFilter>,
42    pub title: Option<String>,
43    pub default_path: Option<String>,
44}
45
46#[derive(Debug, Clone)]
47pub struct ChooseDirectoryRequest {
48    pub title: Option<String>,
49    pub default_path: Option<String>,
50}
51
52#[derive(Debug, Clone)]
53pub struct FileDialogResult {
54    pub canceled: bool,
55    pub paths: Vec<String>,
56}
57
58pub trait FileService: Send + Sync + 'static {
59    fn review_file(
60        &self,
61        _request: OpenFileRequest,
62    ) -> impl Future<Output = Result<(), PlatformError>> + Send;
63
64    fn open_external(
65        &self,
66        _request: OpenFileRequest,
67    ) -> impl Future<Output = Result<(), PlatformError>> + Send {
68        async {
69            Err(PlatformError::NotSupported(
70                "open_external is not supported on this platform".into(),
71            ))
72        }
73    }
74
75    fn reveal_in_file_manager(
76        &self,
77        _request: RevealInFileManagerRequest,
78    ) -> impl Future<Output = Result<(), PlatformError>> + Send {
79        async {
80            Err(PlatformError::NotSupported(
81                "reveal_in_file_manager is not supported on this platform".into(),
82            ))
83        }
84    }
85
86    fn choose_file(
87        &self,
88        _request: ChooseFileRequest,
89    ) -> impl Future<Output = Result<FileDialogResult, PlatformError>> + Send {
90        async {
91            Err(PlatformError::NotSupported(
92                "choose_file is not supported on this platform".into(),
93            ))
94        }
95    }
96
97    fn choose_directory(
98        &self,
99        _request: ChooseDirectoryRequest,
100    ) -> impl Future<Output = Result<FileDialogResult, PlatformError>> + Send {
101        async {
102            Err(PlatformError::NotSupported(
103                "choose_directory is not supported on this platform".into(),
104            ))
105        }
106    }
107}