use crate::camera::FrameData;
#[cfg(feature = "clipboard")]
use crate::clipboard::clipboard_error;
use crate::error::{Error, Missing};
#[cfg(feature = "clipboard")]
pub async fn clipboard_read() -> Result<String, Error> {
std::future::ready(()).await;
let mut clipboard = arboard::Clipboard::new().map_err(clipboard_error)?;
clipboard.get_text().map_err(clipboard_error)
}
#[cfg(feature = "clipboard")]
pub async fn clipboard_write(text: String) -> Result<(), Error> {
std::future::ready(()).await;
let mut clipboard = arboard::Clipboard::new().map_err(clipboard_error)?;
clipboard.set_text(text).map_err(clipboard_error)
}
pub use super::ShareData;
const OPEN_URL_POLL_INTERVAL_MS: u64 = 50;
const OPEN_URL_POLL_COUNT: u32 = 200;
fn has_desktop_session() -> bool {
std::env::var_os("DISPLAY").is_some() || std::env::var_os("WAYLAND_DISPLAY").is_some()
}
pub async fn open_url(url: String) -> Result<(), Error> {
std::future::ready(()).await;
if !has_desktop_session() {
return Err(Error::Missing(Missing::DesktopSession));
}
open_url_wait(&url)
}
fn await_exit(child: &mut std::process::Child) -> Result<Option<std::process::ExitStatus>, Error> {
(0..OPEN_URL_POLL_COUNT)
.find_map(|poll| {
if poll > 0 {
std::thread::sleep(std::time::Duration::from_millis(OPEN_URL_POLL_INTERVAL_MS));
}
child.try_wait().map_err(Error::from).transpose()
})
.transpose()
}
fn open_url_wait(url: &str) -> Result<(), Error> {
let mut child = std::process::Command::new("xdg-open")
.arg(url)
.stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.spawn()
.map_err(Error::from)?;
if let Some(status) = await_exit(&mut child)? {
status
.success()
.then_some(())
.ok_or_else(|| Error::OpenUrlFailed {
url: url.to_string(),
})
} else {
let _kill_outcome = child.kill();
let _wait_outcome = child.wait();
Err(Error::OpenUrlTimedOut {
url: url.to_string(),
})
}
}
#[cfg(feature = "clipboard")]
pub async fn share(data: ShareData) -> Result<(), Error> {
let full = format!("{}\n{}\n{}", data.title, data.text, data.url);
match clipboard_write(full).await {
Ok(()) => {
if data.url.starts_with("http")
&& let Err(open_error) = open_url(data.url).await
{
tracing::warn!("Share open_url failed: {open_error}");
}
Ok(())
}
Err(clipboard_error) => {
tracing::warn!("Share fallback clipboard failed: {clipboard_error}");
if data.url.starts_with("http") {
match open_url(data.url).await {
Ok(()) => Ok(()),
Err(open_error) => {
tracing::warn!("Share open_url failed: {open_error}");
Err(Error::ShareFailed(Box::new(clipboard_error)))
}
}
} else {
Err(Error::ShareFailed(Box::new(clipboard_error)))
}
}
}
}
#[cfg(feature = "files")]
pub async fn download(data: Vec<u8>, filename: &str) -> Result<String, Error> {
let handle = rfd::AsyncFileDialog::new()
.set_file_name(filename)
.save_file()
.await
.ok_or(Error::SaveCancelled)?;
handle.write(&data).await.map_err(Error::from)?;
Ok(filename.to_string())
}
pub async fn sleep(millis: u64) {
std::future::ready(()).await;
std::thread::sleep(std::time::Duration::from_millis(millis));
}
#[cfg(feature = "storage")]
#[must_use]
pub fn storage_get(app: &'static str, key: &str) -> Option<String> {
crate::storage::Storage::new(app).load(key)
}
#[cfg(not(feature = "storage"))]
#[must_use]
pub fn storage_get(_app: &'static str, _key: &str) -> Option<String> {
None
}
#[cfg(feature = "storage")]
pub fn storage_set(app: &'static str, key: &str, value: &str) -> Result<(), Error> {
crate::storage::Storage::new(app).persist(key, &value.to_owned());
Ok(())
}
#[cfg(not(feature = "storage"))]
pub fn storage_set(_app: &'static str, _key: &str, _value: &str) -> Result<(), Error> {
Ok(())
}
pub async fn check_camera() -> Result<(), Error> {
std::future::ready(()).await;
Err(Error::CameraNotAvailable(
"Camera not available on desktop – use file picker".into(),
))
}
pub async fn start_camera() -> Result<(), Error> {
std::future::ready(()).await;
Err(Error::CameraNotAvailable(
"Camera not available on desktop – use file picker".into(),
))
}
pub async fn capture_frame() -> Result<FrameData, Error> {
std::future::ready(()).await;
Err(Error::CameraNotAvailable(
"Camera not available on desktop – use file picker".into(),
))
}
pub fn begin_capture_session() {}
pub fn stop_capture_worker() {}
pub async fn stop_camera() -> Result<(), Error> {
std::future::ready(()).await;
Ok(())
}