|
| 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 | +// ¤t_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 | +// } |
0 commit comments