use serde::{Deserialize, Serialize};
use crate::bidi::BiDi;
use crate::bidi::command::{BidiCommand, BidiEvent, Empty};
use crate::bidi::error::BidiError;
use crate::bidi::ids::{BrowsingContextId, NodeId, UserContextId};
#[derive(Debug, Clone, Serialize)]
pub struct PerformActions {
pub context: BrowsingContextId,
pub actions: Vec<serde_json::Value>,
}
impl BidiCommand for PerformActions {
const METHOD: &'static str = "input.performActions";
type Returns = Empty;
}
#[derive(Debug, Clone, Serialize)]
pub struct ReleaseActions {
pub context: BrowsingContextId,
}
impl BidiCommand for ReleaseActions {
const METHOD: &'static str = "input.releaseActions";
type Returns = Empty;
}
#[derive(Debug, Clone, Serialize)]
pub struct SetFiles {
pub context: BrowsingContextId,
pub element: serde_json::Value,
pub files: Vec<String>,
}
pub fn shared_reference(node: &NodeId) -> serde_json::Value {
serde_json::json!({"sharedId": node.as_str()})
}
impl BidiCommand for SetFiles {
const METHOD: &'static str = "input.setFiles";
type Returns = Empty;
}
#[derive(Debug)]
pub struct InputModule<'a> {
bidi: &'a BiDi,
}
impl<'a> InputModule<'a> {
pub(crate) fn new(bidi: &'a BiDi) -> Self {
Self {
bidi,
}
}
pub async fn perform_actions(
&self,
context: BrowsingContextId,
actions: Vec<serde_json::Value>,
) -> Result<(), BidiError> {
self.bidi
.send(PerformActions {
context,
actions,
})
.await?;
Ok(())
}
pub async fn release_actions(&self, context: BrowsingContextId) -> Result<(), BidiError> {
self.bidi
.send(ReleaseActions {
context,
})
.await?;
Ok(())
}
pub async fn set_files(
&self,
context: BrowsingContextId,
element: &NodeId,
files: Vec<String>,
) -> Result<(), BidiError> {
self.bidi
.send(SetFiles {
context,
element: shared_reference(element),
files,
})
.await?;
Ok(())
}
}
pub(crate) mod events {
use super::*;
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FileDialogOpened {
pub context: BrowsingContextId,
pub multiple: bool,
#[serde(default)]
pub user_context: Option<UserContextId>,
#[serde(default)]
pub element: Option<serde_json::Value>,
}
impl BidiEvent for FileDialogOpened {
const METHOD: &'static str = "input.fileDialogOpened";
}
}