use std::{
future::Future,
pin::Pin,
task::{Context as TaskContext, Poll},
};
use super::{
Answer, ImageSize,
task::{
Action, AnswerSlot, ClipboardImage, ClipboardItem, ClipboardRead, ClipboardWrite,
FileAnswer,
},
};
use crate::{
Command, ROOT_ID,
platform::{HttpHead, HttpRequest},
time::Instant,
};
#[must_use = "an answer does nothing unless awaited"]
#[derive(Debug)]
pub struct Prefetch(Answer<Result<ImageSize, String>>);
impl Future for Prefetch {
type Output = Result<(), String>;
fn poll(mut self: Pin<&mut Self>, cx: &mut TaskContext<'_>) -> Poll<Self::Output> {
Pin::new(&mut self.0).poll(cx).map(|size| size.map(|_| ()))
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct HttpResponse {
pub head: HttpHead,
pub body: Vec<u8>,
}
impl HttpResponse {
pub fn status(&self) -> u16 {
self.head.status
}
pub fn ok(&self) -> bool {
(200..300).contains(&self.head.status)
}
pub fn header(&self, name: &str) -> Option<&str> {
self.head
.headers
.iter()
.find(|(n, _)| n.eq_ignore_ascii_case(name))
.map(|(_, v)| v.as_str())
}
pub fn text(&self) -> String {
String::from_utf8_lossy(&self.body).into_owned()
}
}
#[must_use = "a request does nothing unless awaited"]
#[derive(Debug)]
pub struct Fetch(pub(crate) Answer<Result<HttpResponse, FetchFailure>>);
impl Fetch {
pub(crate) async fn detailed(self) -> Result<HttpResponse, FetchFailure> {
self.0.await
}
}
impl Future for Fetch {
type Output = Result<HttpResponse, String>;
fn poll(mut self: Pin<&mut Self>, cx: &mut TaskContext<'_>) -> Poll<Self::Output> {
Pin::new(&mut self.0)
.poll(cx)
.map(|response| response.map_err(FetchFailure::into_message))
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum FetchFailure {
Failed(String),
Refused { message: String, url: String },
}
impl FetchFailure {
fn into_message(self) -> String {
match self {
Self::Failed(message) | Self::Refused { message, .. } => message,
}
}
}
pub(crate) fn fetch(request: HttpRequest) -> (Fetch, Action) {
let (answer, slot) = Answer::new();
(Fetch(answer), Action::Fetch(Box::new(request), slot))
}
pub(crate) fn announce(message: String) -> Action {
Action::Command(ROOT_ID, Command::Announce(message))
}
pub(crate) fn haptic(haptic: crate::Haptic) -> Action {
Action::Command(ROOT_ID, Command::Haptic(haptic))
}
pub(crate) fn perf_monitor(enabled: bool) -> Action {
Action::Command(ROOT_ID, Command::PerfMonitor(enabled))
}
pub(crate) fn perf_monitor_mode(mode: crate::FpsMode) -> Action {
Action::Command(ROOT_ID, Command::PerfMonitorMode(mode))
}
pub(crate) fn set_clipboard(text: String) -> (Answer<ClipboardWrite>, Action) {
let (answer, slot) = Answer::new();
(
answer,
Action::WriteClipboard(Command::SetClipboard(text), slot),
)
}
pub(crate) fn set_clipboard_item(item: ClipboardItem) -> (Answer<ClipboardWrite>, Action) {
let (answer, slot) = Answer::new();
(
answer,
Action::WriteClipboard(Command::SetClipboardItem(item), slot),
)
}
pub(crate) fn clipboard_string() -> (Answer<String>, Action) {
let (answer, slot) = Answer::new();
(answer, Action::ReadClipboard(ClipboardRead::Text(slot)))
}
pub(crate) fn clipboard_checked_string() -> (Answer<Option<String>>, Action) {
let (answer, slot) = Answer::new();
(
answer,
Action::ReadClipboard(ClipboardRead::CheckedText(slot)),
)
}
pub(crate) fn clipboard_has_string() -> (Answer<bool>, Action) {
let (answer, slot) = Answer::new();
(answer, Action::ReadClipboard(ClipboardRead::Has(slot)))
}
pub(crate) fn set_clipboard_image(uri: String) -> Action {
Action::Command(ROOT_ID, Command::SetClipboardImage(uri))
}
pub(crate) fn clipboard_image() -> (Answer<Option<ClipboardImage>>, Action) {
let (answer, slot) = Answer::new();
(answer, Action::ReadClipboard(ClipboardRead::Image(slot)))
}
pub(crate) fn clipboard_checked_image() -> (Answer<Result<Option<ClipboardImage>, usize>>, Action) {
let (answer, slot) = Answer::new();
(
answer,
Action::ReadClipboard(ClipboardRead::CheckedImage(slot)),
)
}
pub(crate) fn clipboard_has_image() -> (Answer<bool>, Action) {
let (answer, slot) = Answer::new();
(answer, Action::ReadClipboard(ClipboardRead::HasImage(slot)))
}
pub(crate) fn clipboard_files() -> (Answer<Vec<String>>, Action) {
let (answer, slot) = Answer::new();
(answer, Action::ReadClipboard(ClipboardRead::Files(slot)))
}
pub(crate) fn register_font(data: Vec<u8>) -> (Answer<Result<String, String>>, Action) {
let (answer, slot) = Answer::new();
(answer, Action::RegisterFont(data, slot))
}
pub(crate) fn pick_images(
request: crate::picker::PickRequest,
) -> (Answer<Vec<crate::picker::PickedFile>>, Action) {
let (answer, slot) = Answer::new();
(answer, Action::PickImages(request, slot))
}
pub(crate) fn pick_files(
request: crate::picker::FilePickRequest,
) -> (Answer<Vec<crate::picker::PickedFile>>, Action) {
let (answer, slot) = Answer::new();
(answer, Action::PickFiles(request, slot))
}
pub(crate) fn host_call(
name: String,
args: crate::JsonValue,
) -> (
Answer<Result<crate::JsonValue, crate::host_channel::HostError>>,
Action,
) {
let (answer, slot) = Answer::new();
(
answer,
Action::HostCall {
name,
args,
answer: slot,
},
)
}
pub(crate) fn open_url(url: String) -> (Answer<bool>, Action) {
let (answer, slot) = Answer::new();
(answer, Action::OpenUrl(url, slot))
}
pub(crate) fn file_request<T>(
request: crate::file_dialog::FileRequest,
answer: impl FnOnce(AnswerSlot<T>) -> FileAnswer,
) -> (Answer<T>, Action) {
let (result, slot) = Answer::new();
(result, Action::FileRequest(request, answer(slot)))
}
pub(crate) fn render_canvas(
request: crate::canvas::render::RenderRequest,
) -> (Answer<Result<Vec<u8>, super::JsError>>, Action) {
let (answer, slot) = Answer::new();
(answer, Action::RenderCanvas(Box::new(request), slot))
}
pub(crate) fn pick_screen_color() -> (Answer<Option<crate::Hsla>>, Action) {
let (answer, slot) = Answer::new();
(answer, Action::PickScreenColor(slot))
}
pub(crate) fn image_size(uri: String) -> (Answer<Result<ImageSize, String>>, Action) {
let (answer, slot) = Answer::new();
(answer, Action::ImageInfo(uri, slot))
}
pub(crate) fn prefetch(uri: String) -> (Prefetch, Action) {
let (answer, action) = image_size(uri);
(Prefetch(answer), action)
}
pub(crate) fn frame() -> (Answer<Instant>, Action) {
let (answer, slot): (_, AnswerSlot<Instant>) = Answer::new();
(answer, Action::Frame(slot))
}