cosmic-comp/src/utils/prelude.rs

140 lines
4.5 KiB
Rust
Raw Normal View History

2022-11-17 20:32:54 +01:00
use std::{cell::RefCell, sync::Mutex, time::Duration};
2022-11-03 18:51:27 +01:00
use crate::{
backend::render::cursor::{CursorShape, CursorState},
2022-11-03 18:51:27 +01:00
input::{ActiveOutput, SeatId},
};
2022-07-04 15:27:08 +02:00
use smithay::{
2022-11-03 18:51:27 +01:00
desktop::utils::bbox_from_surface_tree,
input::{
pointer::{CursorIcon, CursorImageAttributes, CursorImageStatus},
2022-11-03 18:51:27 +01:00
Seat,
},
output::Output,
2022-11-17 20:32:54 +01:00
utils::{Buffer, IsAlive, Logical, Monotonic, Point, Rectangle, Time, Transform},
2022-11-03 18:51:27 +01:00
wayland::compositor::with_states,
2022-07-04 15:27:08 +02:00
};
2022-08-31 13:01:23 +02:00
pub use crate::shell::{Shell, Workspace};
pub use crate::state::{Common, State};
2022-07-05 18:46:38 +02:00
pub use crate::wayland::handlers::xdg_shell::popup::update_reactive_popups;
2022-07-04 15:27:08 +02:00
pub trait OutputExt {
fn geometry(&self) -> Rectangle<i32, Logical>;
}
impl OutputExt for Output {
fn geometry(&self) -> Rectangle<i32, Logical> {
2022-07-04 16:00:29 +02:00
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()
})
2022-07-04 15:27:08 +02:00
}
}
2022-07-08 14:00:13 +02:00
pub trait SeatExt {
fn id(&self) -> usize;
2022-09-28 12:01:29 +02:00
fn active_output(&self) -> Output;
fn set_active_output(&self, output: &Output);
2022-11-03 18:51:27 +01:00
fn cursor_geometry(
&self,
loc: impl Into<Point<f64, Buffer>>,
2022-11-17 20:32:54 +01:00
time: Time<Monotonic>,
2022-11-03 18:51:27 +01:00
) -> Option<(Rectangle<i32, Buffer>, Point<i32, Buffer>)>;
2022-07-08 14:00:13 +02:00
}
impl SeatExt for Seat<State> {
fn id(&self) -> usize {
self.user_data().get::<SeatId>().unwrap().0
}
2022-09-28 12:01:29 +02:00
fn active_output(&self) -> Output {
self.user_data()
.get::<ActiveOutput>()
.map(|x| x.0.borrow().clone())
.unwrap()
}
2022-07-04 15:27:08 +02:00
2022-09-28 12:01:29 +02:00
fn set_active_output(&self, output: &Output) {
*self
2022-07-04 15:27:08 +02:00
.user_data()
.get::<ActiveOutput>()
.unwrap()
.0
.borrow_mut() = output.clone();
}
2022-11-03 18:51:27 +01:00
fn cursor_geometry(
&self,
loc: impl Into<Point<f64, Buffer>>,
2022-11-17 20:32:54 +01:00
time: Time<Monotonic>,
2022-11-03 18:51:27 +01:00
) -> Option<(Rectangle<i32, Buffer>, Point<i32, Buffer>)> {
let location = loc.into().to_i32_round();
let cursor_status = self
.user_data()
.get::<RefCell<CursorImageStatus>>()
.map(|cell| {
let mut cursor_status = cell.borrow_mut();
if let CursorImageStatus::Surface(ref surface) = *cursor_status {
if !surface.alive() {
*cursor_status = CursorImageStatus::default_named();
2022-11-03 18:51:27 +01:00
}
}
cursor_status.clone()
})
.unwrap_or(CursorImageStatus::default_named());
2022-11-03 18:51:27 +01:00
match cursor_status {
CursorImageStatus::Surface(surface) => {
let hotspot = with_states(&surface, |states| {
states
.data_map
.get::<Mutex<CursorImageAttributes>>()
.unwrap()
.lock()
.unwrap()
.hotspot
});
let geo = bbox_from_surface_tree(&surface, (location.x, location.y));
let buffer_geo = Rectangle::from_loc_and_size(
(geo.loc.x, geo.loc.y),
geo.size.to_buffer(1, Transform::Normal),
);
Some((buffer_geo, (hotspot.x, hotspot.y).into()))
}
CursorImageStatus::Named(CursorIcon::Default) => {
2022-11-03 18:51:27 +01:00
let seat_userdata = self.user_data();
seat_userdata.insert_if_missing(CursorState::default);
let state = seat_userdata.get::<CursorState>().unwrap();
let frame = state
.cursors
.get(&CursorShape::Default)
.unwrap()
2022-11-17 20:32:54 +01:00
.get_image(1, Into::<Duration>::into(time).as_millis() as u32);
2022-11-03 18:51:27 +01:00
Some((
Rectangle::from_loc_and_size(
location,
(frame.width as i32, frame.height as i32),
),
(frame.xhot as i32, frame.yhot as i32).into(),
))
}
CursorImageStatus::Named(_) => {
// TODO: Handle for `cursor_shape_v1` protocol
None
}
2022-11-03 18:51:27 +01:00
CursorImageStatus::Hidden => None,
}
}
2022-07-04 15:27:08 +02:00
}