wayland_bridge/
viewporter.rsuse crate::client::Client;
use crate::compositor::{Surface, SurfaceCommand, ViewportCropParams, ViewportScaleParams};
use crate::object::{NewObjectExt, ObjectRef, RequestReceiver};
use anyhow::Error;
use wp_viewporter_server_protocol::{
WpViewport, WpViewportRequest, WpViewporter, WpViewporterRequest,
};
pub struct Viewporter;
impl Viewporter {
pub fn new() -> Self {
Viewporter
}
}
impl RequestReceiver<WpViewporter> for Viewporter {
fn receive(
this: ObjectRef<Self>,
request: WpViewporterRequest,
client: &mut Client,
) -> Result<(), Error> {
match request {
WpViewporterRequest::Destroy => {
client.delete_id(this.id())?;
}
WpViewporterRequest::GetViewport { id, surface } => {
let surface: ObjectRef<Surface> = surface.into();
id.implement(client, Viewport::new(surface))?;
}
}
Ok(())
}
}
struct Viewport {
surface_ref: ObjectRef<Surface>,
}
impl Viewport {
pub fn new(surface_ref: ObjectRef<Surface>) -> Self {
Viewport { surface_ref }
}
}
impl RequestReceiver<WpViewport> for Viewport {
fn receive(
this: ObjectRef<Self>,
request: WpViewportRequest,
client: &mut Client,
) -> Result<(), Error> {
match request {
WpViewportRequest::Destroy => {
let surface_ref = this.get(client)?.surface_ref;
if let Ok(surface) = surface_ref.get_mut(client) {
surface.enqueue(SurfaceCommand::ClearViewportCropParams);
surface.enqueue(SurfaceCommand::ClearViewportScaleParams);
}
client.delete_id(this.id())?;
}
WpViewportRequest::SetSource { x, y, width, height } => {
let (x, y, width, height) =
(x.to_float(), y.to_float(), width.to_float(), height.to_float());
let surface_ref = this.get(client)?.surface_ref;
let surface = surface_ref.get_mut(client)?;
if x == -1.0 && y == -1.0 && width == -1.0 && height == -1.0 {
surface.enqueue(SurfaceCommand::ClearViewportCropParams);
} else {
surface.enqueue(SurfaceCommand::SetViewportCropParams(ViewportCropParams {
x,
y,
width,
height,
}));
}
}
WpViewportRequest::SetDestination { width, height } => {
let surface_ref = this.get(client)?.surface_ref;
let surface = surface_ref.get_mut(client)?;
if width == -1 && height == -1 {
surface.enqueue(SurfaceCommand::ClearViewportScaleParams);
} else {
surface.enqueue(SurfaceCommand::SetViewportScaleParams(ViewportScaleParams {
width,
height,
}));
}
}
}
Ok(())
}
}