use crate::client::Client;
use crate::client::LookupError;
use crate::globals::Global;
use crate::globals::GlobalName;
use crate::ifs::xdg_activation_token_v1::ActivationToken;
use crate::ifs::xdg_activation_token_v1::XdgActivationTokenV1;
use crate::leaks::Tracker;
use crate::object::Version;
use crate::utils::errorfmt::ErrorFmt;
use crate::utils::opaque::OpaqueError;
use crate::wire::XdgActivationV1Id;
use crate::wire::xdg_activation_v1::*;
use jay_proc::Global;
use jay_proc::Object;
use std::rc::Rc;
use thiserror::Error;
#[derive(Global)]
pub struct XdgActivationV1Global {
name: GlobalName,
}
impl XdgActivationV1Global {
pub fn new(name: GlobalName) -> Self {
Self { name }
}
fn bind_(
self: Rc<Self>,
id: XdgActivationV1Id,
client: &Rc<Client>,
version: Version,
) -> Result<(), XdgActivationV1Error> {
let mgr = Rc::new(XdgActivationV1 {
id,
client: client.clone(),
tracker: Default::default(),
version,
});
track!(client, mgr);
client.add_client_obj(&mgr);
Ok(())
}
}
impl Global for XdgActivationV1Global {
fn version(&self) -> u32 {
1
}
}
#[derive(Object)]
pub struct XdgActivationV1 {
id: XdgActivationV1Id,
client: Rc<Client>,
tracker: Tracker<Self>,
version: Version,
}
impl XdgActivationV1RequestHandler for XdgActivationV1 {
type Error = XdgActivationV1Error;
fn destroy(&self, _req: Destroy, _slf: &Rc<Self>) -> Result<(), Self::Error> {
self.client.remove_obj(self);
Ok(())
}
fn get_activation_token(
&self,
req: GetActivationToken,
_slf: &Rc<Self>,
) -> Result<(), Self::Error> {
let token = Rc::new(XdgActivationTokenV1::new(
req.id,
&self.client,
self.version,
));
track!(self.client, token);
self.client.add_client_obj(&token);
Ok(())
}
fn activate(&self, req: Activate<'_>, _slf: &Rc<Self>) -> Result<(), Self::Error> {
let token: ActivationToken = match req.token.parse() {
Ok(t) => t,
Err(e) => {
log::warn!("Could not parse client activation token: {}", ErrorFmt(e));
return Ok(());
}
};
let surface = self.client.lookup(req.surface)?;
if self.client.state.activation_tokens.remove(&token).is_none() {
log::warn!(
"Client requested activation with unknown token {}",
req.token
);
return Ok(());
}
surface.request_activation();
Ok(())
}
}
#[derive(Debug, Error)]
pub enum XdgActivationV1Error {
#[error(transparent)]
Lookup(#[from] LookupError),
#[error("Could not parse the activation token")]
ParseActivationToken(#[from] OpaqueError),
}