From 1d51da4ed3de279128666271f9151bc533831679 Mon Sep 17 00:00:00 2001 From: Victoria Brekenfeld Date: Wed, 31 May 2023 14:15:41 +0200 Subject: [PATCH] stack: Ability to modify a stacks windows --- src/shell/element/stack.rs | 42 ++++++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/src/shell/element/stack.rs b/src/shell/element/stack.rs index 9ecd1b34..5dd470b9 100644 --- a/src/shell/element/stack.rs +++ b/src/shell/element/stack.rs @@ -95,15 +95,16 @@ pub enum Focus { } impl CosmicStack { - pub fn new( - window: impl Into, + pub fn new>( + windows: impl Iterator, handle: LoopHandle<'static, crate::state::Data>, ) -> CosmicStack { - let window = window.into(); - let width = window.geometry().size.w; + let windows = windows.map(Into::into).collect::>(); + assert!(!windows.is_empty()); + let width = windows[0].geometry().size.w; CosmicStack(IcedElement::new( CosmicStackInternal { - windows: Arc::new(Mutex::new(vec![window])), + windows: Arc::new(Mutex::new(windows)), active: Arc::new(AtomicUsize::new(0)), previous_keyboard: Arc::new(AtomicUsize::new(0)), pointer_entered: None, @@ -115,9 +116,34 @@ impl CosmicStack { )) } - //pub fn add_window() - //pub fn remove_window() - //pub fn len + pub fn add_window(&self, window: CosmicSurface) { + self.0 + .with_program(|p| p.windows.lock().unwrap().push(window)); + } + + pub fn remove_window(&self, window: &CosmicSurface) { + self.0.with_program(|p| { + let mut windows = p.windows.lock().unwrap(); + let Some(idx) = windows.iter().position(|w| w == window) else { return }; + windows.remove(idx); + p.active.fetch_min(windows.len() - 1, Ordering::SeqCst); + }) + } + + pub fn remove_idx(&self, idx: usize) { + self.0.with_program(|p| { + let mut windows = p.windows.lock().unwrap(); + if windows.len() >= idx { + return; + } + windows.remove(idx); + p.active.fetch_min(windows.len(), Ordering::SeqCst); + }) + } + + pub fn len(&self) -> usize { + self.0.with_program(|p| p.windows.lock().unwrap().len()) + } pub fn active(&self) -> CosmicSurface { self.0