utils: Adding additional helpers

This commit is contained in:
Victoria Brekenfeld 2022-07-04 15:27:08 +02:00
parent 06d5989223
commit a088f7fd6e
4 changed files with 108 additions and 43 deletions

61
src/utils/prelude.rs Normal file
View file

@ -0,0 +1,61 @@
use std::cell::RefCell;
use smithay::{
wayland::{
output::Output,
seat::Seat,
},
utils::{Rectangle, Transform, Logical},
};
use crate::{
input::ActiveOutput,
state::Common,
};
pub use crate::{
state::State,
};
pub trait OutputExt {
fn geometry(&self) -> Rectangle<i32, Logical>;
}
impl OutputExt for Output {
fn geometry(&self) -> Rectangle<i32, Logical> {
Rectangle::from_loc_and_size(
self.current_location(),
{
Transform::from(self.current_transform()).transform_size(
self.current_mode().map(|m| m.size).unwrap_or_else(|| (0,0).into())
).to_f64().to_logical(self.current_scale().fractional_scale()).to_i32_round()
},
)
}
}
pub fn active_output(seat: &Seat<State>, state: &Common) -> Output {
seat.user_data()
.get::<ActiveOutput>()
.map(|x| x.0.borrow().clone())
.unwrap_or_else(|| {
state
.shell
.outputs()
.next()
.cloned()
.expect("Backend has no outputs?")
})
}
pub fn set_active_output(seat: &Seat<State>, output: &Output) {
if !seat
.user_data()
.insert_if_missing(|| ActiveOutput(RefCell::new(output.clone())))
{
*seat
.user_data()
.get::<ActiveOutput>()
.unwrap()
.0
.borrow_mut() = output.clone();
}
}