pinnacle/src/window.rs

61 lines
2.1 KiB
Rust
Raw Normal View History

2023-05-28 19:14:30 -05:00
use std::cell::RefCell;
2023-06-18 19:30:52 -05:00
use smithay::{
desktop::Window, reexports::wayland_server::protocol::wl_surface::WlSurface,
wayland::compositor,
};
2023-05-28 19:14:30 -05:00
2023-06-18 19:30:52 -05:00
use crate::{backend::Backend, layout::Layout, state::State};
2023-05-28 19:14:30 -05:00
use self::window_state::{Float, WindowState};
pub mod window_state;
pub trait SurfaceState: Default + 'static {
/// Access the [SurfaceState] associated with a [WlSurface]
2023-05-28 19:14:30 -05:00
fn with_state<F, T>(wl_surface: &WlSurface, function: F) -> T
where
F: FnOnce(&mut Self) -> T,
{
compositor::with_states(wl_surface, |states| {
states.data_map.insert_if_missing(RefCell::<Self>::default);
let state = states.data_map.get::<RefCell<Self>>().unwrap();
function(&mut state.borrow_mut())
})
}
}
2023-06-18 19:30:52 -05:00
pub fn toggle_floating<B: Backend>(state: &mut State<B>, window: &Window) {
tracing::info!("toggling floating");
WindowState::with_state(window, |window_state| {
2023-05-28 19:14:30 -05:00
match window_state.floating {
2023-06-18 19:30:52 -05:00
Float::Tiled(prev_loc_and_size) => {
2023-05-28 19:14:30 -05:00
if let Some((prev_loc, prev_size)) = prev_loc_and_size {
2023-06-18 19:30:52 -05:00
tracing::info!("changing size and loc");
2023-05-28 19:14:30 -05:00
window.toplevel().with_pending_state(|state| {
state.size = Some(prev_size);
});
window.toplevel().send_pending_configure();
2023-06-18 19:30:52 -05:00
state.space.map_element(window.clone(), prev_loc, false); // TODO: should it activate?
2023-05-28 19:14:30 -05:00
}
2023-06-18 19:30:52 -05:00
window_state.floating = Float::Floating;
2023-05-28 19:14:30 -05:00
}
Float::Floating => {
2023-06-18 19:30:52 -05:00
window_state.floating = Float::Tiled(Some((
state.space.element_location(window).unwrap(), // We get the location this way
2023-05-28 19:14:30 -05:00
// because window.geometry().loc doesn't seem to be the actual location
window.geometry().size,
)));
}
}
2023-06-18 19:30:52 -05:00
});
let windows = state.space.elements().cloned().collect::<Vec<_>>();
Layout::master_stack(state, windows, crate::layout::Direction::Left);
state.space.raise_element(window, true);
2023-05-28 19:14:30 -05:00
}