2023-06-30 21:34:07 -05:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2023-07-09 17:48:46 -05:00
|
|
|
use std::{
|
2023-07-11 16:10:31 -05:00
|
|
|
cell::RefCell,
|
2023-07-09 17:48:46 -05:00
|
|
|
hash::Hash,
|
2023-07-11 16:10:31 -05:00
|
|
|
rc::Rc,
|
2023-07-09 17:48:46 -05:00
|
|
|
sync::atomic::{AtomicU32, Ordering},
|
|
|
|
};
|
|
|
|
|
2023-07-11 16:10:31 -05:00
|
|
|
use smithay::output::Output;
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
backend::Backend,
|
2023-07-11 21:07:51 -05:00
|
|
|
layout::Layout,
|
2023-07-11 16:10:31 -05:00
|
|
|
state::{State, WithState},
|
|
|
|
};
|
|
|
|
|
2023-07-09 17:48:46 -05:00
|
|
|
static TAG_ID_COUNTER: AtomicU32 = AtomicU32::new(0);
|
2023-06-30 21:34:07 -05:00
|
|
|
|
2023-07-11 16:10:31 -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
|
|
|
|
|
|
|
pub fn tag<B: Backend>(&self, state: &State<B>) -> Option<Tag> {
|
|
|
|
state
|
|
|
|
.space
|
|
|
|
.outputs()
|
|
|
|
.flat_map(|op| op.with_state(|state| state.tags.clone()))
|
|
|
|
.find(|tag| &tag.id() == self)
|
|
|
|
}
|
2023-07-09 17:48:46 -05:00
|
|
|
}
|
2023-06-30 21:34:07 -05:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
2023-07-11 16:10:31 -05:00
|
|
|
struct TagInner {
|
2023-07-09 17:48:46 -05:00
|
|
|
/// The internal id of this tag.
|
2023-07-11 16:10:31 -05:00
|
|
|
id: TagId,
|
2023-07-09 17:48:46 -05:00
|
|
|
/// The name of this tag.
|
2023-07-11 16:10:31 -05:00
|
|
|
name: String,
|
2023-07-09 17:48:46 -05:00
|
|
|
/// Whether this tag is active or not.
|
2023-07-11 16:10:31 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-07-11 16:10:31 -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>>);
|
|
|
|
|
|
|
|
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) {
|
2023-07-11 16:10:31 -05:00
|
|
|
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-11 16:10:31 -05:00
|
|
|
}
|
|
|
|
|
2023-07-09 17:48:46 -05:00
|
|
|
impl Tag {
|
2023-07-11 11:59:38 -05:00
|
|
|
pub fn new(name: String) -> Self {
|
2023-07-11 16:10:31 -05:00
|
|
|
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-07-11 16:10:31 -05:00
|
|
|
})))
|
|
|
|
}
|
2023-07-19 18:55:22 -05:00
|
|
|
pub fn output<B: Backend>(&self, state: &State<B>) -> Option<Output> {
|
|
|
|
state
|
|
|
|
.space
|
2023-07-11 16:10:31 -05:00
|
|
|
.outputs()
|
2023-07-19 18:55:22 -05:00
|
|
|
.find(|output| output.with_state(|state| state.tags.iter().any(|tg| tg == self)))
|
2023-07-11 16:10:31 -05:00
|
|
|
.cloned()
|
2023-06-30 21:34:07 -05:00
|
|
|
}
|
|
|
|
}
|