use crate::client::Client;
use crate::ifs::wl_surface::WlSurface;
use crate::leaks::Tracker;
use crate::object::Version;
use crate::wire::WpViewportId;
use crate::wire::wp_viewport::*;
use jay_proc::Object;
use std::rc::Rc;
use thiserror::Error;
#[derive(Object)]
pub struct WpViewport {
id: WpViewportId,
client: Rc<Client>,
surface: Rc<WlSurface>,
pub tracker: Tracker<Self>,
version: Version,
}
impl WpViewport {
pub fn new(id: WpViewportId, surface: &Rc<WlSurface>, version: Version) -> Self {
Self {
id,
client: surface.client.clone(),
surface: surface.clone(),
tracker: Default::default(),
version,
}
}
pub fn install(self: &Rc<Self>) -> Result<(), WpViewportError> {
if self.surface.viewporter.is_some() {
return Err(WpViewportError::ViewportExists);
}
self.surface.viewporter.set(Some(self.clone()));
Ok(())
}
}
impl WpViewportRequestHandler for WpViewport {
type Error = WpViewportError;
fn destroy(&self, _req: Destroy, _slf: &Rc<Self>) -> Result<(), Self::Error> {
let pending = &mut *self.surface.pending.borrow_mut();
pending.src_rect = Some(None);
pending.dst_size = Some(None);
self.surface.viewporter.take();
self.client.remove_obj(self);
Ok(())
}
fn set_source(&self, req: SetSource, _slf: &Rc<Self>) -> Result<(), Self::Error> {
let rect = if req.x == -1 && req.y == -1 && req.width == -1 && req.height == -1 {
None
} else {
let invalid = req.x < 0 || req.y < 0 || req.width <= 0 || req.height <= 0;
if invalid {
return Err(WpViewportError::InvalidSourceRect);
}
Some([req.x, req.y, req.width, req.height])
};
self.surface.pending.borrow_mut().src_rect = Some(rect);
Ok(())
}
fn set_destination(&self, req: SetDestination, _slf: &Rc<Self>) -> Result<(), Self::Error> {
let size = if req.width == -1 && req.height == -1 {
None
} else if req.width <= 0 || req.height <= 0 {
return Err(WpViewportError::InvalidDestRect);
} else {
Some((req.width, req.height))
};
self.surface.pending.borrow_mut().dst_size = Some(size);
Ok(())
}
}
#[derive(Debug, Error)]
pub enum WpViewportError {
#[error("The surface already has a viewport")]
ViewportExists,
#[error("Rectangle is empty or outside the first quadrant")]
InvalidSourceRect,
#[error("Rectangle is empty")]
InvalidDestRect,
}