gardesk/gar / 5584e18

Browse files

tree: add get/set_split_ratio helpers

Authored by mfwolffe <wolffemf@dukes.jmu.edu>
SHA
5584e18bbd073f8a03b9b5b01c0ccc5bf632a5d6
Parents
608f72b
Tree
da54969

1 changed file

StatusFile+-
M gar/src/core/tree.rs 69 0
gar/src/core/tree.rsmodified
@@ -256,6 +256,75 @@ impl Node {
256
         }
256
         }
257
     }
257
     }
258
 
258
 
259
+    /// Get the split ratio affecting a window in the given direction.
260
+    /// Returns None if no such split exists.
261
+    pub fn get_split_ratio(&self, window: XWindow, direction: Direction) -> Option<f32> {
262
+        match self {
263
+            Node::Leaf { .. } => None,
264
+            Node::Internal {
265
+                split,
266
+                ratio,
267
+                left,
268
+                right,
269
+            } => {
270
+                // Check if this split is in the right orientation for the direction
271
+                let dominated = match (split, direction) {
272
+                    (SplitDirection::Vertical, Direction::Left | Direction::Right) => true,
273
+                    (SplitDirection::Horizontal, Direction::Up | Direction::Down) => true,
274
+                    _ => false,
275
+                };
276
+
277
+                if dominated && (left.contains(window) || right.contains(window)) {
278
+                    return Some(*ratio);
279
+                }
280
+
281
+                // Recurse into children
282
+                if left.contains(window) {
283
+                    left.get_split_ratio(window, direction)
284
+                } else if right.contains(window) {
285
+                    right.get_split_ratio(window, direction)
286
+                } else {
287
+                    None
288
+                }
289
+            }
290
+        }
291
+    }
292
+
293
+    /// Set the split ratio affecting a window in the given direction.
294
+    /// Returns true if the ratio was set.
295
+    pub fn set_split_ratio(&mut self, window: XWindow, direction: Direction, new_ratio: f32) -> bool {
296
+        match self {
297
+            Node::Leaf { .. } => false,
298
+            Node::Internal {
299
+                split,
300
+                ratio,
301
+                left,
302
+                right,
303
+            } => {
304
+                // Check if this split is in the right orientation for the direction
305
+                let dominated = match (split, direction) {
306
+                    (SplitDirection::Vertical, Direction::Left | Direction::Right) => true,
307
+                    (SplitDirection::Horizontal, Direction::Up | Direction::Down) => true,
308
+                    _ => false,
309
+                };
310
+
311
+                if dominated && (left.contains(window) || right.contains(window)) {
312
+                    *ratio = new_ratio.clamp(0.1, 0.9);
313
+                    return true;
314
+                }
315
+
316
+                // Recurse into children
317
+                if left.contains(window) {
318
+                    left.set_split_ratio(window, direction, new_ratio)
319
+                } else if right.contains(window) {
320
+                    right.set_split_ratio(window, direction, new_ratio)
321
+                } else {
322
+                    false
323
+                }
324
+            }
325
+        }
326
+    }
327
+
259
     /// Swap two windows in the tree.
328
     /// Swap two windows in the tree.
260
     pub fn swap(&mut self, a: XWindow, b: XWindow) -> bool {
329
     pub fn swap(&mut self, a: XWindow, b: XWindow) -> bool {
261
         // Find and swap the windows
330
         // Find and swap the windows