Skip to content

Commit 8177fcf

Browse files
committed
slop
1 parent f8d2059 commit 8177fcf

15 files changed

+550
-320
lines changed

packages/wm/src/commands/container/detach_container.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{
1111
/// If the container is a tiling container, the siblings will be resized to
1212
/// fill the freed up space. Will flatten empty parent split containers.
1313
#[allow(clippy::needless_pass_by_value)]
14-
pub fn detach_container(child_to_remove: Container) -> anyhow::Result<()> {
14+
pub fn detach_container(child_to_remove: Container) -> anyhow::Result<Container> {
1515
// Flatten the parent split container if it'll be empty after removing
1616
// the child.
1717
if let Some(split_parent) = child_to_remove
@@ -55,5 +55,5 @@ pub fn detach_container(child_to_remove: Container) -> anyhow::Result<()> {
5555
}
5656
}
5757

58-
Ok(())
58+
Ok(child_to_remove)
5959
}

packages/wm/src/commands/container/replace_container.rs

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ use crate::{
1010
/// Replaces a container at the specified index.
1111
///
1212
/// The replaced container will be detached from the tree.
13+
///
14+
/// TODO return container and call 2 replace_continaers in swap_container
1315
pub fn replace_container(
1416
replacement_container: &Container,
1517
target_parent: &Container,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
use anyhow::Context;
2+
use wm_common::{
3+
LengthValue, Rect, RectDelta, TilingDirection, WindowState,
4+
};
5+
use wm_platform::NativeWindow;
6+
7+
use crate::{
8+
commands::container::{attach_container, detach_container},
9+
models::{
10+
Container, SplitContainer, TilingWindow, WindowContainer, Workspace,
11+
},
12+
traits::{CommonGetters, TilingDirectionGetters},
13+
user_config::UserConfig,
14+
wm_state::WmState,
15+
};
16+
17+
pub fn add_dwindle_window(
18+
native_window: NativeWindow,
19+
target_parent: Option<Container>,
20+
window_state: &WindowState,
21+
target_workspace: Workspace,
22+
target_container: Container,
23+
config: &UserConfig,
24+
state: &mut WmState,
25+
) -> anyhow::Result<WindowContainer> {
26+
// Create a TilingWindow from NativeWindow.
27+
let border_delta = RectDelta::new(
28+
LengthValue::from_px(0),
29+
LengthValue::from_px(0),
30+
LengthValue::from_px(0),
31+
LengthValue::from_px(0),
32+
);
33+
let gaps_config = config.value.gaps.clone();
34+
let new_window = TilingWindow::new(
35+
None,
36+
native_window,
37+
None,
38+
border_delta,
39+
Rect {
40+
left: 0,
41+
top: 0,
42+
right: 0,
43+
bottom: 0,
44+
},
45+
false,
46+
gaps_config,
47+
Vec::new(),
48+
None,
49+
);
50+
51+
// Setup the initial dwindle layout.
52+
if target_workspace.child_count() == 0 {
53+
attach_container(
54+
&new_window.clone().into(),
55+
&target_workspace.clone().into(),
56+
None,
57+
)?;
58+
return new_window.as_window_container();
59+
} else if target_workspace.child_count() == 1 {
60+
let new_split_container = SplitContainer::new(
61+
TilingDirection::Vertical,
62+
config.value.gaps.clone(),
63+
);
64+
attach_container(
65+
&new_window.clone().into(),
66+
&new_split_container.clone().into(),
67+
None,
68+
)?;
69+
attach_container(
70+
&new_split_container.clone().into(),
71+
&target_workspace.clone().into(),
72+
None,
73+
)?;
74+
return new_window.as_window_container();
75+
}
76+
77+
// At this point, we have at least 2 windows.
78+
// This logic inserts the new window after the focused window, shifting
79+
// all deeper windows in the dwindle tree downwards to make room.
80+
let target_parent = target_container.parent().context("No parent.")?;
81+
let child_count = target_parent.child_count();
82+
let focused_index = target_container.index();
83+
84+
// Re-organize the containers
85+
let mut workspace_children = target_parent.borrow_children_mut();
86+
87+
// The 2nd container is always a SplitContainer, 1st is just a window.
88+
let mut split_container =
89+
workspace_children.back().context("No children.")?;
90+
let mut split_children = split_container.borrow_children_mut();
91+
let window_to_shift =
92+
detach_container(*split_children.front().unwrap()).ok();
93+
attach_container(
94+
&new_window.clone().into(),
95+
&split_container.clone().into(),
96+
None,
97+
)?;
98+
// let mut window_to_shift = split_children.pop_front();
99+
// split_children.insert(0, target_container.clone());
100+
//
101+
while let Some(window) = window_to_shift {
102+
// Get the back container using a temporary reference
103+
let back = {
104+
let current_children = &split_children;
105+
current_children.back().context("No children.")?
106+
};
107+
108+
// Clone the container since we need to use it after the borrow ends
109+
let next_container = back.clone();
110+
111+
// Get the children of the next container
112+
let mut next_children = next_container.children();
113+
114+
if next_children.iter().count() == 1 {
115+
// Create the new split container
116+
let new_split_direction = next_container
117+
.as_direction_container()?
118+
.tiling_direction()
119+
.inverse();
120+
121+
let new_split_container = SplitContainer::new(
122+
new_split_direction,
123+
config.value.gaps.clone(),
124+
);
125+
126+
// Add the window to the new container and attach
127+
new_split_container.borrow_children_mut().push_back(window);
128+
attach_container(
129+
&new_split_container.clone().into(),
130+
&next_container.clone().into(),
131+
None,
132+
)?;
133+
break;
134+
}
135+
136+
// Shift windows
137+
let next_window = next_children.pop_front();
138+
next_children.insert(0, window);
139+
140+
// Prepare for next iteration
141+
split_container = &next_container;
142+
split_children = split_container.children();
143+
window_to_shift = next_window;
144+
}
145+
new_window.as_window_container()
146+
}
147+
// ) -> anyhow::Result<(Container, usize)> {
148+
// let child_count = target_workspace.child_count();
149+
//
150+
// if child_count == 0 {
151+
// Ok((target_workspace.clone().into(), 0))
152+
// } else if child_count == 1 {
153+
// // Create a vertical split container for the stack
154+
// let new_container = SplitContainer::new(
155+
// TilingDirection::Vertical,
156+
// config.value.gaps.clone(),
157+
// );
158+
// {
159+
// let master_containers = target_workspace.borrow_children_mut();
160+
// let only_container = master_containers.front().unwrap();
161+
// state
162+
// .pending_sync
163+
// .queue_container_to_redraw(only_container.clone());
164+
// }
165+
// attach_container(
166+
// &new_container.clone().into(),
167+
// &target_workspace.clone().into(),
168+
// None,
169+
// )?;
170+
// Ok((new_container.clone().into(), 0))
171+
// } else if child_count == 2 {
172+
// let children = target_workspace.borrow_children_mut();
173+
// let back_container = children.back().context("No children.")?;
174+
// let back_clone = back_container.clone();
175+
// drop(children);
176+
//
177+
// // Now work with the cloned container
178+
// let mut current_container = back_clone;
179+
//
180+
// while current_container.children().iter().count() > 1 {
181+
// // Create a new borrow scope
182+
// let next_container = {
183+
// let children = current_container.borrow_children_mut();
184+
// let back = children.back().context("No children.")?;
185+
// back.clone() // Clone it so we can drop the borrow
186+
// };
187+
// current_container = next_container;
188+
// }
189+
//
190+
// let current_child_count =
191+
// current_container.children().iter().count(); if current_child_count
192+
// == 0 { Ok((target_workspace.clone().into(), 0))
193+
// } else if current_child_count == 1 {
194+
// let new_split_direction = current_container
195+
// .as_direction_container()?
196+
// .tiling_direction()
197+
// .inverse();
198+
// let split_container = SplitContainer::new(
199+
// new_split_direction,
200+
// config.value.gaps.clone(),
201+
// );
202+
// state
203+
// .pending_sync
204+
// .queue_container_to_redraw(current_container.clone());
205+
// attach_container(
206+
// &split_container.clone().into(),
207+
// &current_container.clone().into(),
208+
// None,
209+
// )?;
210+
// Ok((split_container.clone().into(), 0))
211+
// } else {
212+
// Err(anyhow::anyhow!("Unexpected child count"))
213+
// }
214+
// } else {
215+
// Err(anyhow::anyhow!("Unexpected child count"))
216+
// }
217+
// }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
use wm_common::{TilingDirection, WindowState};
2+
3+
use crate::{
4+
commands::container::attach_container,
5+
models::{Container, SplitContainer, Workspace},
6+
traits::CommonGetters,
7+
user_config::UserConfig,
8+
wm_state::WmState,
9+
};
10+
11+
pub fn add_grid_window(
12+
target_parent: Option<Container>,
13+
window_state: &WindowState,
14+
target_workspace: Workspace,
15+
target_container: Container,
16+
config: &UserConfig,
17+
state: &mut WmState,
18+
) -> anyhow::Result<(Container, usize)> {
19+
let child_c = target_workspace.child_count();
20+
let child_count = target_workspace.children().iter().count();
21+
assert_eq!(child_c, child_count);
22+
23+
let current_container = target_workspace.clone();
24+
let mut result = None;
25+
current_container.children().iter().for_each(|container| {
26+
if container.child_count() < child_count {
27+
result = Some((container.clone().into(), 0));
28+
return;
29+
}
30+
});
31+
32+
if let Some(result) = result {
33+
return Ok(result);
34+
}
35+
36+
let vert_container = SplitContainer::new(
37+
TilingDirection::Vertical,
38+
config.value.gaps.clone(),
39+
);
40+
{
41+
let master_containers = target_workspace.borrow_children_mut();
42+
state
43+
.pending_sync
44+
.queue_containers_to_redraw(master_containers.clone());
45+
}
46+
47+
attach_container(
48+
&vert_container.clone().into(),
49+
&target_workspace.clone().into(),
50+
None,
51+
)?;
52+
Ok((vert_container.clone().into(), 0))
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use anyhow::Context;
2+
use wm_common::WindowState;
3+
4+
use crate::{
5+
models::{Container, Workspace},
6+
traits::CommonGetters,
7+
};
8+
9+
pub fn add_manual_window(
10+
target_parent: Option<Container>,
11+
window_state: &WindowState,
12+
target_container: Container,
13+
target_workspace: Workspace,
14+
) -> anyhow::Result<(Container, usize)> {
15+
if let Some(target) = target_parent {
16+
return Ok((target.clone(), 0));
17+
}
18+
// For tiling windows, try to find a suitable tiling window to insert
19+
// next to.
20+
if *window_state == WindowState::Tiling {
21+
let sibling = match target_container {
22+
Container::TilingWindow(_) => Some(target_container),
23+
_ => target_workspace
24+
.descendant_focus_order()
25+
.find(Container::is_tiling_window),
26+
};
27+
28+
if let Some(sibling) = sibling {
29+
return Ok((
30+
sibling.parent().context("No parent.")?,
31+
sibling.index() + 1,
32+
));
33+
}
34+
}
35+
36+
// Default to appending to workspace.
37+
Ok((
38+
target_workspace.clone().into(),
39+
target_workspace.child_count(),
40+
))
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
use anyhow::Context;
2+
use wm_common::{TilingDirection, WindowState};
3+
4+
use crate::{
5+
commands::container::attach_container,
6+
models::{Container, SplitContainer, Workspace},
7+
traits::CommonGetters,
8+
user_config::UserConfig,
9+
wm_state::WmState,
10+
};
11+
12+
pub fn add_master_stack_window(
13+
target_parent: Option<Container>,
14+
window_state: &WindowState,
15+
target_workspace: Workspace,
16+
target_container: Container,
17+
config: &UserConfig,
18+
state: &mut WmState,
19+
) -> anyhow::Result<(Container, usize)> {
20+
let child_c = target_workspace.child_count();
21+
let child_count = target_workspace.children().iter().count();
22+
assert_eq!(child_c, child_count);
23+
if child_count == 0 {
24+
Ok((target_workspace.clone().into(), 0))
25+
} else if child_count == 1 {
26+
// Create a vertical split container for the stack
27+
let stack_container = SplitContainer::new(
28+
TilingDirection::Vertical,
29+
config.value.gaps.clone(),
30+
);
31+
{
32+
let master_containers = target_workspace.borrow_children_mut();
33+
let master_container = master_containers.front().unwrap();
34+
35+
// master_container
36+
// .as_tiling_container()?
37+
// .set_tiling_size(master_ratio);
38+
39+
state
40+
.pending_sync
41+
.queue_container_to_redraw(master_container.clone());
42+
}
43+
44+
attach_container(
45+
&stack_container.clone().into(),
46+
&target_workspace.clone().into(),
47+
None,
48+
)?;
49+
Ok((stack_container.clone().into(), 0))
50+
} else if child_count == 2 {
51+
let children = target_workspace.children();
52+
let stack_container = children.back().context("No children.")?;
53+
54+
Ok((stack_container.clone(), 0))
55+
} else {
56+
assert!(false);
57+
// If there are no children, just append to the workspace.
58+
Ok((target_workspace.clone().into(), 0))
59+
}
60+
}

0 commit comments

Comments
 (0)