1
0
Fork 0
forked from wry/wry

autocommit 2022-01-29 23:08:25 CET

This commit is contained in:
Julian Orth 2022-01-29 23:08:25 +01:00
parent 11d3604de4
commit f577f5feef
18 changed files with 484 additions and 173 deletions

View file

@ -3,10 +3,9 @@ mod types;
use crate::client::Client;
use crate::ifs::xdg_wm_base::XdgWmBaseObj;
use crate::object::{Interface, Object, ObjectId};
use crate::rect::Rect;
use crate::utils::buffd::MsgParser;
use bitflags::bitflags;
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;
use std::cell::RefCell;
use std::rc::Rc;
pub use types::*;
@ -24,41 +23,41 @@ const SET_PARENT_CONFIGURE: u32 = 9;
const INVALID_INPUT: u32 = 0;
#[derive(Debug, Eq, PartialEq, Copy, Clone, FromPrimitive)]
pub enum Anchor {
None = 0,
Top = 1,
Bottom = 2,
Left = 3,
Right = 4,
TopLeft = 5,
BottomLeft = 6,
TopRight = 7,
BottomRight = 8,
}
const NONE: u32 = 0;
const TOP: u32 = 1;
const BOTTOM: u32 = 2;
const LEFT: u32 = 3;
const RIGHT: u32 = 4;
const TOP_LEFT: u32 = 5;
const BOTTOM_LEFT: u32 = 6;
const TOP_RIGHT: u32 = 7;
const BOTTOM_RIGHT: u32 = 8;
impl Default for Anchor {
fn default() -> Self {
Self::None
bitflags::bitflags! {
#[derive(Default)]
pub struct Square: u32 {
const TOP = 1 << 0;
const BOTTOM = 1 << 1;
const LEFT = 1 << 2;
const RIGHT = 1 << 3;
}
}
#[derive(Debug, Eq, PartialEq, Copy, Clone, FromPrimitive)]
pub enum Gravity {
None = 0,
Top = 1,
Bottom = 2,
Left = 3,
Right = 4,
TopLeft = 5,
BottomLeft = 6,
TopRight = 7,
BottomRight = 8,
}
impl Default for Gravity {
fn default() -> Self {
Self::None
impl Square {
fn from_enum(e: u32) -> Option<Self> {
let s = match e {
NONE => Square::empty(),
TOP => Square::TOP,
BOTTOM => Square::BOTTOM,
LEFT => Square::LEFT,
RIGHT => Square::RIGHT,
TOP_LEFT => Square::TOP | Square::LEFT,
BOTTOM_LEFT => Square::BOTTOM | Square::LEFT,
TOP_RIGHT => Square::TOP | Square::RIGHT,
BOTTOM_RIGHT => Square::BOTTOM | Square::RIGHT,
_ => return None,
};
Some(s)
}
}
@ -86,23 +85,61 @@ pub struct XdgPositioner {
#[derive(Copy, Clone, Debug, Default)]
pub struct XdgPositioned {
pub size_width: u32,
pub size_height: u32,
pub ar_x: i32,
pub ar_y: i32,
pub ar_width: u32,
pub ar_height: u32,
pub anchor: Anchor,
pub gravity: Gravity,
pub size_width: i32,
pub size_height: i32,
pub ar: Rect,
pub anchor: Square,
pub gravity: Square,
pub ca: CA,
pub off_x: i32,
pub off_y: i32,
pub reactive: bool,
pub parent_width: u32,
pub parent_height: u32,
pub parent_width: i32,
pub parent_height: i32,
pub parent_serial: u32,
}
impl XdgPositioned {
pub fn is_complete(&self) -> bool {
self.size_height != 0 && self.size_width != 0
}
pub fn get_position(&self) -> Rect {
let mut x1 = self.off_x;
let mut y1 = self.off_x;
if self.anchor.contains(Square::LEFT) {
x1 += self.ar.x1();
} else if self.anchor.contains(Square::RIGHT) {
x1 += self.ar.x2();
} else {
x1 += self.ar.x1() + self.ar.width() / 2;
}
if self.anchor.contains(Square::TOP) {
y1 += self.ar.y1();
} else if self.anchor.contains(Square::BOTTOM) {
y1 += self.ar.y2();
} else {
y1 += self.ar.y1() + self.ar.height() / 2;
}
if self.gravity.contains(Square::LEFT) {
x1 -= self.size_width;
} else if !self.gravity.contains(Square::RIGHT) {
x1 -= self.size_width / 2;
}
if self.gravity.contains(Square::TOP) {
y1 -= self.size_height;
} else if !self.gravity.contains(Square::BOTTOM) {
y1 -= self.size_height / 2;
}
Rect::new_sized(x1, y1, self.size_width, self.size_height).unwrap()
}
}
impl XdgPositioner {
pub fn new(base: &Rc<XdgWmBaseObj>, id: XdgPositionerId, client: &Rc<Client>) -> Self {
Self {
@ -113,9 +150,8 @@ impl XdgPositioner {
}
}
#[allow(dead_code)]
pub fn clone(&self) -> Box<XdgPositioned> {
Box::new(*self.position.borrow())
pub fn value(&self) -> XdgPositioned {
*self.position.borrow()
}
fn destroy(&self, parser: MsgParser<'_, '_>) -> Result<(), DestroyError> {
@ -135,8 +171,8 @@ impl XdgPositioner {
return Err(SetSizeError::NonPositiveSize);
}
let mut position = self.position.borrow_mut();
position.size_width = req.width as u32;
position.size_height = req.height as u32;
position.size_width = req.width;
position.size_height = req.height;
Ok(())
}
@ -151,16 +187,13 @@ impl XdgPositioner {
return Err(SetAnchorRectError::NegativeAnchorRect);
}
let mut position = self.position.borrow_mut();
position.ar_x = req.x;
position.ar_y = req.y;
position.ar_width = req.width as u32;
position.ar_height = req.height as u32;
position.ar = Rect::new_sized(req.x, req.y, req.width, req.height).unwrap();
Ok(())
}
fn set_anchor(&self, parser: MsgParser<'_, '_>) -> Result<(), SetAnchorError> {
let req: SetAnchor = self.client.parse(self, parser)?;
let anchor = match Anchor::from_u32(req.anchor) {
let anchor = match Square::from_enum(req.anchor) {
Some(a) => a,
_ => return Err(SetAnchorError::UnknownAnchor(req.anchor)),
};
@ -170,7 +203,7 @@ impl XdgPositioner {
fn set_gravity(&self, parser: MsgParser<'_, '_>) -> Result<(), SetGravityError> {
let req: SetGravity = self.client.parse(self, parser)?;
let gravity = match Gravity::from_u32(req.gravity) {
let gravity = match Square::from_enum(req.gravity) {
Some(a) => a,
_ => return Err(SetGravityError::UnknownGravity(req.gravity)),
};
@ -220,8 +253,8 @@ impl XdgPositioner {
return Err(SetParentSizeError::NegativeParentSize);
}
let mut position = self.position.borrow_mut();
position.parent_width = req.parent_width as u32;
position.parent_height = req.parent_height as u32;
position.parent_width = req.parent_width;
position.parent_height = req.parent_height;
Ok(())
}