pinnacle/src/tag.rs

148 lines
3.7 KiB
Rust
Raw Normal View History

2023-08-01 11:06:35 -05:00
// SPDX-License-Identifier: GPL-3.0-or-later
2023-06-30 21:34:07 -05:00
2023-07-09 17:48:46 -05:00
use std::{
cell::RefCell,
2023-07-09 17:48:46 -05:00
hash::Hash,
rc::Rc,
2023-07-09 17:48:46 -05:00
sync::atomic::{AtomicU32, Ordering},
};
use smithay::{
backend::renderer::{
element::{surface::WaylandSurfaceRenderElement, AsRenderElements},
ImportAll, ImportMem, Renderer,
},
desktop::{space::SpaceElement, Space},
output::Output,
utils::Scale,
};
use crate::{
2023-07-11 21:07:51 -05:00
layout::Layout,
state::{State, WithState},
window::WindowElement,
};
2023-07-09 17:48:46 -05:00
static TAG_ID_COUNTER: AtomicU32 = AtomicU32::new(0);
2023-06-30 21:34:07 -05:00
#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy, serde::Serialize, serde::Deserialize)]
2023-07-09 17:48:46 -05:00
pub struct TagId(u32);
impl TagId {
fn next() -> Self {
Self(TAG_ID_COUNTER.fetch_add(1, Ordering::Relaxed))
}
2023-07-19 18:55:22 -05:00
2023-08-28 22:53:24 -05:00
pub fn tag(&self, state: &State) -> Option<Tag> {
2023-07-19 18:55:22 -05:00
state
.space
.outputs()
.flat_map(|op| op.with_state(|state| state.tags.clone()))
.find(|tag| &tag.id() == self)
}
2023-08-15 17:26:17 -05:00
/// Reset the global TagId counter.
///
/// This is used, for example, when a config is reloaded and you want to keep
/// windows on the same tags.
pub fn reset() {
TAG_ID_COUNTER.store(0, Ordering::SeqCst);
}
2023-07-09 17:48:46 -05:00
}
2023-06-30 21:34:07 -05:00
#[derive(Debug)]
struct TagInner {
2023-07-09 17:48:46 -05:00
/// The internal id of this tag.
id: TagId,
2023-07-09 17:48:46 -05:00
/// The name of this tag.
name: String,
2023-07-09 17:48:46 -05:00
/// Whether this tag is active or not.
active: bool,
2023-07-11 21:07:51 -05:00
/// What layout this tag has.
layout: Layout,
2023-06-30 21:34:07 -05:00
}
impl PartialEq for TagInner {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}
impl Eq for TagInner {}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Tag(Rc<RefCell<TagInner>>);
// RefCell Safety: These methods should never panic because they are all self-contained or Copy.
impl Tag {
pub fn id(&self) -> TagId {
self.0.borrow().id
}
pub fn name(&self) -> String {
self.0.borrow().name.clone()
}
pub fn active(&self) -> bool {
self.0.borrow().active
}
2023-07-12 18:50:41 -05:00
pub fn set_active(&self, active: bool) {
self.0.borrow_mut().active = active;
}
2023-07-11 21:07:51 -05:00
pub fn layout(&self) -> Layout {
self.0.borrow().layout
}
2023-07-12 18:50:41 -05:00
pub fn set_layout(&self, layout: Layout) {
self.0.borrow_mut().layout = layout;
}
}
2023-07-09 17:48:46 -05:00
impl Tag {
2023-07-11 11:59:38 -05:00
pub fn new(name: String) -> Self {
Self(Rc::new(RefCell::new(TagInner {
2023-07-09 17:48:46 -05:00
id: TagId::next(),
name,
active: false,
2023-07-12 18:50:41 -05:00
layout: Layout::MasterStack, // TODO: get from config
})))
}
2023-08-28 22:53:24 -05:00
pub fn output(&self, state: &State) -> Option<Output> {
2023-07-19 18:55:22 -05:00
state
.space
.outputs()
2023-07-19 18:55:22 -05:00
.find(|output| output.with_state(|state| state.tags.iter().any(|tg| tg == self)))
.cloned()
2023-06-30 21:34:07 -05:00
}
/// Get the render_elements for the provided tags.
pub fn tag_render_elements<R, C>(
windows: &[WindowElement],
space: &Space<WindowElement>,
renderer: &mut R,
) -> Vec<C>
where
R: Renderer + ImportAll + ImportMem,
<R as Renderer>::TextureId: 'static,
C: From<WaylandSurfaceRenderElement<R>>,
{
let elements = windows
.iter()
2023-09-02 20:26:11 -05:00
.rev() // rev because I treat the focus stack backwards vs how the renderer orders it
.filter(|win| win.is_on_active_tag(space.outputs()))
.flat_map(|win| {
2023-09-02 16:51:39 -05:00
// subtract win.geometry().loc to align decorations correctly
let loc = (space.element_location(win).unwrap_or((0, 0).into())
- win.geometry().loc)
.to_physical(1);
win.render_elements::<C>(renderer, loc, Scale::from(1.0), 1.0)
})
.collect::<Vec<_>>();
elements
}
2023-06-30 21:34:07 -05:00
}