lib-nested/nested/src/terminal/compositor.rs

91 lines
2.2 KiB
Rust
Raw Normal View History

use {
2021-01-06 21:35:46 +01:00
std::{
2021-05-13 16:22:30 +02:00
sync::{Arc}
2021-01-06 21:35:46 +01:00
},
std::sync::RwLock,
2021-01-06 21:35:46 +01:00
cgmath::Point2,
crate::{
core::{InnerViewPort, OuterViewPort, View, Observer, ObserverBroadcast},
index::{IndexArea, IndexView},
terminal::{TerminalAtom, TerminalView},
projection::ProjectionHelper
}
};
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
pub struct TerminalCompositor {
layers: Vec<Arc<dyn TerminalView>>,
cast: Arc<RwLock<ObserverBroadcast<dyn TerminalView>>>,
2021-08-24 23:15:18 +02:00
proj_helper: ProjectionHelper<usize, Self>
}
2021-01-06 21:35:46 +01:00
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
2021-01-06 21:35:46 +01:00
impl TerminalCompositor {
pub fn new(
port: InnerViewPort<dyn TerminalView>
) -> Arc<RwLock<Self>> {
let comp = Arc::new(RwLock::new(
TerminalCompositor {
layers: Vec::new(),
cast: port.get_broadcast(),
proj_helper: ProjectionHelper::new(port.0.update_hooks.clone())
2021-01-06 21:35:46 +01:00
}
));
2021-01-06 21:35:46 +01:00
comp.write().unwrap().proj_helper.set_proj(&comp);
port.set_view(Some(comp.clone()));
comp
2021-01-06 21:35:46 +01:00
}
pub fn push(&mut self, v: OuterViewPort<dyn TerminalView>) {
2021-08-24 23:15:18 +02:00
let idx = self.layers.len();
self.layers.push(
self.proj_helper.new_index_arg(
2021-08-24 23:15:18 +02:00
idx,
v,
|s: &mut Self, area| {
s.cast.notify(area);
}
)
);
2021-01-06 21:35:46 +01:00
}
}
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
impl View for TerminalCompositor {
type Msg = IndexArea<Point2<i16>>;
}
impl IndexView<Point2<i16>> for TerminalCompositor {
type Item = TerminalAtom;
2021-01-06 21:35:46 +01:00
fn get(&self, pos: &Point2<i16>) -> Option<TerminalAtom> {
let mut atom = None;
for layer in self.layers.iter() {
match (atom, layer.get(pos)) {
(None, next) => atom = next,
(Some(last), Some(next)) => atom = Some(next.add_style_back(last.style)),
_ => {}
}
}
2021-01-06 21:35:46 +01:00
atom
}
fn area(&self) -> IndexArea<Point2<i16>> {
let mut area = IndexArea::Empty;
for layer in self.layers.iter() {
area = area.union(layer.area());
}
area
}
2021-01-06 21:35:46 +01:00
}