@@ -23,195 +23,73 @@ pub fn add_dwindle_window(
23
23
config : & UserConfig ,
24
24
state : & mut WmState ,
25
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
26
let new_window = TilingWindow :: new (
35
27
None ,
36
28
native_window,
37
29
None ,
38
- border_delta,
30
+ RectDelta :: new (
31
+ LengthValue :: from_px ( 0 ) ,
32
+ LengthValue :: from_px ( 0 ) ,
33
+ LengthValue :: from_px ( 0 ) ,
34
+ LengthValue :: from_px ( 0 ) ,
35
+ ) ,
39
36
Rect {
40
37
left : 0 ,
41
38
top : 0 ,
42
39
right : 0 ,
43
40
bottom : 0 ,
44
41
} ,
45
42
false ,
46
- gaps_config ,
43
+ config . value . gaps . clone ( ) ,
47
44
Vec :: new ( ) ,
48
45
None ,
49
46
) ;
50
47
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 ( ) ;
48
+ // Get starting point - use target's parent if it exists
49
+ let start_container = if target_workspace. child_count ( ) <= 1 {
50
+ target_workspace. clone ( ) . into ( )
51
+ } else {
52
+ target_container. parent ( ) . context ( "No parent." ) ?
53
+ } ;
83
54
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
- } ;
55
+ let mut current = start_container;
56
+ let mut window = new_window. clone ( ) . into ( ) ;
107
57
108
- // Clone the container since we need to use it after the borrow ends
109
- let next_container = back. clone ( ) ;
58
+ loop {
59
+ if current. child_count ( ) <= 1 {
60
+ if current. child_count ( ) == 0 {
61
+ attach_container ( & window, & current, Some ( 0 ) ) ?;
62
+ break ;
63
+ }
110
64
111
- // Get the children of the next container
112
- let mut next_children = next_container. children ( ) ;
65
+ let direction = if current. as_workspace ( ) . is_some ( ) {
66
+ TilingDirection :: Vertical
67
+ } else {
68
+ current
69
+ . as_direction_container ( ) ?
70
+ . tiling_direction ( )
71
+ . inverse ( )
72
+ } ;
113
73
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
- ) ?;
74
+ let new_split =
75
+ SplitContainer :: new ( direction, config. value . gaps . clone ( ) ) ;
76
+ attach_container ( & window, & new_split. clone ( ) . into ( ) , Some ( 0 ) ) ?;
77
+ attach_container ( & new_split. clone ( ) . into ( ) , & current, Some ( 1 ) ) ?;
133
78
break ;
134
79
}
135
80
136
- // Shift windows
137
- let next_window = next_children . pop_front ( ) ;
138
- next_children . insert ( 0 , window ) ;
81
+ // Get existing split and its window
82
+ let next_split = current . borrow_children ( ) . get ( 1 ) . unwrap ( ) . clone ( ) ;
83
+ let next_window = next_split . borrow_children ( ) . get ( 0 ) . unwrap ( ) . clone ( ) ;
139
84
140
- // Prepare for next iteration
141
- split_container = & next_container;
142
- split_children = split_container. children ( ) ;
143
- window_to_shift = next_window;
85
+ // Swap windows
86
+ let detached = detach_container ( next_window, true ) ?;
87
+ attach_container ( & window, & next_split. clone ( ) . into ( ) , Some ( 0 ) ) ?;
88
+
89
+ // Move to next container
90
+ current = next_split;
91
+ window = detached;
144
92
}
93
+
145
94
new_window. as_window_container ( )
146
95
}
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