mod drag_destination; mod layout; mod tasks; pub use drag_destination::default_tile_drag_destination; pub use tasks::{container_layout, container_render_positions, container_tab_render_textures}; use { drag_destination::direction_to_split, crate::{ backend::ButtonState, cursor::KnownCursor, cursor_user::CursorUser, fixed::Fixed, ifs::wl_seat::{ BTN_LEFT, NodeSeatState, SeatId, WlSeatGlobal, collect_kb_foci, collect_kb_foci2, tablet::{TabletTool, TabletToolChanges, TabletToolId}, wl_pointer::PendingScroll, }, rect::Rect, renderer::Renderer, state::State, tree::{ ContainingNode, Direction, FindTreeResult, FindTreeUsecase, FloatNode, FoundNode, Node, NodeId, NodeLayerLink, NodeLocation, OutputNode, TddType, TileDragDestination, ToplevelData, ToplevelNode, ToplevelNodeBase, ToplevelType, WorkspaceNode, default_tile_drag_bounds, tab_bar::{TabBar, TabBarEntry}, toplevel_set_workspace, walker::NodeVisitor, }, utils::{ clonecell::CloneCell, event_listener::LazyEventSource, hash_map_ext::HashMapExt, linkedlist::{LinkedList, LinkedNode, NodeRef}, numcell::NumCell, rc_eq::rc_eq, threshold_counter::ThresholdCounter, }, }, ahash::AHashMap, jay_config::Axis, smallvec::SmallVec, std::{ cell::{Cell, RefCell}, fmt::{Debug, Formatter}, mem, ops::{Deref, DerefMut}, rc::Rc, }, }; #[derive(Copy, Clone, Debug, Eq, PartialEq)] pub enum ContainerSplit { Horizontal, Vertical, } impl ContainerSplit { pub fn other(self) -> Self { match self { ContainerSplit::Horizontal => ContainerSplit::Vertical, ContainerSplit::Vertical => ContainerSplit::Horizontal, } } } impl From for ContainerSplit { fn from(a: Axis) -> Self { match a { Axis::Horizontal => Self::Horizontal, Axis::Vertical => Self::Vertical, } } } impl Into for ContainerSplit { fn into(self) -> Axis { match self { ContainerSplit::Horizontal => Axis::Horizontal, ContainerSplit::Vertical => Axis::Vertical, } } } #[expect(dead_code)] #[derive(Copy, Clone, Debug, Eq, PartialEq)] pub enum ContainerFocus { None, Child, Yes, } tree_id!(ContainerNodeId); /// Ephemeral group state (hy3-style auto-collapse). /// /// An ephemeral container is a transient grouping that auto-collapses back to /// its single child when all but one child is removed. #[derive(Copy, Clone, Debug, PartialEq, Eq, Default)] pub enum Ephemeral { /// Normal container — never auto-collapses. #[default] Off, /// Ephemeral container — when child count drops to 1, collapse. On, } /// Actions for the `changegroup` operation. #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub enum ChangeGroupAction { /// Toggle between Horizontal and Vertical. Opposite, /// Toggle between tabbed (mono) and split mode. ToggleTab, } #[derive(Default)] pub struct ContainerRenderData { pub border_rects: Vec, } pub struct ContainerNode { pub id: ContainerNodeId, pub split: Cell, pub mono_child: CloneCell>>, pub mono_body: Cell, pub mono_content: Cell, pub abs_x1: Cell, pub abs_y1: Cell, pub width: Cell, pub height: Cell, pub content_width: Cell, pub content_height: Cell, pub sum_factors: Cell, pub layout_scheduled: Cell, animate_next_layout: Cell, pub mono_transition_animation_pending: Cell, compute_render_positions_scheduled: Cell, num_children: NumCell, pub children: LinkedList, focus_history: LinkedList>, child_nodes: RefCell>>, workspace: CloneCell>, location: Cell, cursors: RefCell>, state: Rc, pub render_data: RefCell, toplevel_data: ToplevelData, attention_requests: ThresholdCounter, pub layout_complete: Rc, pub child_added: Rc, pub child_removed: Rc, pub all_children_resized: Rc, pub tab_bar: RefCell>, pub update_tab_textures_scheduled: Cell, pub ephemeral: Cell, } impl Debug for ContainerNode { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { f.debug_struct("ContainerNode").finish_non_exhaustive() } } pub struct ContainerChild { pub node: Rc, pub active: Cell, pub attention_requested: Cell, focus_history: Cell>>>, // fields below only valid in tabbed layout pub body: Cell, pub content: Cell, factor: Cell, pub border_color_is_focused: Cell, } #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] enum CursorType { Seat(SeatId), TabletTool(TabletToolId), } struct CursorState { cursor: KnownCursor, target: bool, x: i32, y: i32, op: Option, } impl ContainerNode { pub fn new( state: &Rc, workspace: &Rc, child: Rc, split: ContainerSplit, ) -> Rc { let children = LinkedList::new(); let child_node = children.add_last(ContainerChild { node: child.clone(), active: Default::default(), body: Default::default(), content: Default::default(), factor: Cell::new(1.0), focus_history: Default::default(), attention_requested: Cell::new(false), border_color_is_focused: Default::default(), }); let child_node_ref = child_node.clone(); let mut child_nodes = AHashMap::new(); child_nodes.insert(child.node_id(), child_node); let id = state.node_ids.next(); let slf = Rc::new_cyclic(|weak| Self { id, split: Cell::new(split), mono_child: CloneCell::new(None), mono_body: Cell::new(Default::default()), mono_content: Cell::new(Default::default()), abs_x1: Cell::new(0), abs_y1: Cell::new(0), width: Cell::new(0), height: Cell::new(0), content_width: Cell::new(0), content_height: Cell::new(0), sum_factors: Cell::new(1.0), layout_scheduled: Cell::new(false), animate_next_layout: Cell::new(false), mono_transition_animation_pending: Cell::new(false), compute_render_positions_scheduled: Cell::new(false), num_children: NumCell::new(1), children, focus_history: Default::default(), child_nodes: RefCell::new(child_nodes), workspace: CloneCell::new(workspace.clone()), location: Cell::new(workspace.location()), cursors: RefCell::new(Default::default()), state: state.clone(), render_data: Default::default(), toplevel_data: ToplevelData::new( state, Default::default(), None, ToplevelType::Container, id, weak, ), attention_requests: Default::default(), layout_complete: state.post_layout_event_sources.create_source(), child_added: state.lazy_event_sources.create_source(), child_removed: state.lazy_event_sources.create_source(), all_children_resized: state.post_layout_event_sources.create_source(), tab_bar: RefCell::new(None), update_tab_textures_scheduled: Cell::new(false), ephemeral: Cell::new(Ephemeral::Off), }); child.tl_set_parent(slf.clone()); slf.pull_child_properties(&child_node_ref); slf } pub fn prepend_child(self: &Rc, new: Rc) { if let Some(child) = self.children.first() { self.add_child_before_(&child, new); } } pub fn append_child(self: &Rc, new: Rc) { if let Some(child) = self.children.last() { self.add_child_after_(&child, new); } } pub fn add_child_after(self: &Rc, prev: &dyn Node, new: Rc) { self.add_child_x(prev, new, |prev, new| self.add_child_after_(prev, new)); } pub fn add_child_before(self: &Rc, prev: &dyn Node, new: Rc) { self.add_child_x(prev, new, |prev, new| self.add_child_before_(prev, new)); } fn add_child_x(self: &Rc, prev: &dyn Node, new: Rc, f: F) where F: FnOnce(&NodeRef, Rc), { let node = self .child_nodes .borrow() .get(&prev.node_id()) .map(|n| n.to_ref()); if let Some(node) = node { f(&node, new); return; } log::error!( "Tried to add a child to a container but the preceding node is not in the container" ); } fn add_child_after_( self: &Rc, prev: &NodeRef, new: Rc, ) { self.add_child(|cc| prev.append(cc), new); } fn add_child_before_( self: &Rc, prev: &NodeRef, new: Rc, ) { self.add_child(|cc| prev.prepend(cc), new); } fn add_child(self: &Rc, f: F, new: Rc) where F: FnOnce(ContainerChild) -> LinkedNode, { let new_ref = { let mut links = self.child_nodes.borrow_mut(); if links.contains_key(&new.node_id()) { log::error!("Tried to add a child to a container that already contains the child"); return; } let link = f(ContainerChild { node: new.clone(), active: Default::default(), body: Default::default(), content: Default::default(), factor: Default::default(), focus_history: Default::default(), attention_requested: Default::default(), border_color_is_focused: Default::default(), }); let r = link.to_ref(); links.insert(new.node_id(), link); r }; new.tl_set_parent(self.clone()); self.pull_child_properties(&new_ref); new.tl_set_visible(self.toplevel_data.visible.get()); let num_children = self.num_children.fetch_add(1) + 1; self.update_content_size(); let new_child_factor = 1.0 / num_children as f64; let mut sum_factors = 0.0; for child in self.children.iter() { let factor = if rc_eq(&child.node, &new) { new_child_factor } else { child.factor.get() * (1.0 - new_child_factor) }; child.factor.set(factor); sum_factors += factor; } self.sum_factors.set(sum_factors); if self.mono_child.is_some() { self.activate_child(&new_ref); self.rebuild_tab_bar(); self.damage(); } // log::info!("add_child"); self.schedule_layout(); self.cancel_seat_ops(); self.child_added.trigger(); } fn cancel_seat_ops(&self) { let mut seats = self.cursors.borrow_mut(); for seat in seats.values_mut() { seat.op = None; } } fn pointer_move( self: &Rc, _seat: &Rc, id: CursorType, cursor: &CursorUser, x: Fixed, y: Fixed, target: bool, ) { let mut x = x.round_down(); let mut y = y.round_down(); let mut seats = self.cursors.borrow_mut(); let seat_state = seats.entry(id).or_insert_with(|| CursorState { cursor: KnownCursor::Default, target, x, y, op: None, }); let mut changed = false; changed |= mem::replace(&mut seat_state.x, x) != x; changed |= mem::replace(&mut seat_state.y, y) != y; if !changed { return; } if let Some(op) = &seat_state.op { match op.kind { SeatOpKind::Resize { dist_left, dist_right, } => { let Some(prev) = op.child.prev() else { return; }; let prev_body = prev.body.get(); let child_body = op.child.body.get(); let (prev_factor, child_factor) = match self.split.get() { ContainerSplit::Horizontal => { let cw = self.content_width.get(); x = x .max(prev_body.x1() + dist_left) .min(child_body.x2() - dist_right); let prev_factor = (x - prev_body.x1() - dist_left) as f64 / cw as f64; let child_factor = (child_body.x2() - x - dist_right) as f64 / cw as f64; (prev_factor, child_factor) } ContainerSplit::Vertical => { let ch = self.content_height.get(); y = y .max(prev_body.y1() + dist_left) .min(child_body.y2() - dist_right); let prev_factor = (y - prev_body.y1() - dist_left) as f64 / ch as f64; let child_factor = (child_body.y2() - y - dist_right) as f64 / ch as f64; (prev_factor, child_factor) } }; let sum_factors = self.sum_factors.get() - prev.factor.get() - op.child.factor.get() + prev_factor + child_factor; prev.factor.set(prev_factor); op.child.factor.set(child_factor); self.sum_factors.set(sum_factors); // log::info!("pointer_move"); self.state.suppress_animations_for_next_layout.set(true); self.schedule_layout_immediate(); } } return; } let new_cursor = if self.mono_child.is_some() { KnownCursor::Default } else if self.split.get() == ContainerSplit::Horizontal { KnownCursor::EwResize } else { let mut cursor = KnownCursor::Default; for child in self.children.iter() { let body = child.body.get(); if body.y1() > y { cursor = KnownCursor::NsResize; break; } } cursor }; if new_cursor != mem::replace(&mut seat_state.cursor, new_cursor) { if seat_state.target { cursor.set_known(new_cursor); } } } fn schedule_compute_render_positions(self: &Rc) { if !self.compute_render_positions_scheduled.replace(true) { self.state .pending_container_render_positions .push(self.clone()); } } fn schedule_update_tab_textures(self: &Rc) { if !self.update_tab_textures_scheduled.replace(true) { self.state .pending_container_tab_render_textures .push(self.clone()); } } fn compute_render_positions(&self) { self.compute_render_positions_scheduled.set(false); let mut rd = self.render_data.borrow_mut(); let rd = rd.deref_mut(); let theme = &self.state.theme; let bw = theme.sizes.border_width.get(); let cwidth = self.width.get(); let cheight = self.height.get(); rd.border_rects.clear(); let mono = self.mono_child.is_some(); let split = self.split.get(); let abs_x = self.abs_x1.get(); let abs_y = self.abs_y1.get(); let gap = self.state.theme.sizes.gap.get(); for (i, child) in self.children.iter().enumerate() { if gap != 0 && !mono && !child.node.node_is_container() { child.border_color_is_focused.set(child.active.get()); } else if i > 0 { let body = child.body.get(); let sep = if mono { // In mono mode, no separators needed without title tabs continue; } else if split == ContainerSplit::Horizontal { Rect::new_sized_saturating(body.x1() - bw, 0, bw, cheight) } else { Rect::new_sized_saturating(0, body.y1() - bw, cwidth, bw) }; if gap == 0 { rd.border_rects.push(sep); } } } if self.toplevel_data.visible.get() { self.state .damage(Rect::new_sized_saturating(abs_x, abs_y, cwidth, cheight)); } } fn activate_child(self: &Rc, child: &NodeRef) { self.activate_child2(child, false); } fn activate_child2(self: &Rc, child: &NodeRef, preserve_focus: bool) { if let Some(mc) = self.mono_child.get() { if mc.node.node_id() == child.node.node_id() { return; } if !preserve_focus { let seats = collect_kb_foci(mc.node.clone()); mc.node.tl_set_visible(false); for seat in seats { child .node .clone() .node_do_focus(&seat, Direction::Unspecified); } } self.mono_child.set(Some(child.clone())); // Keep focus_history in sync with mono_child so that // toggle_tab and other operations that use focus_history.last() // return the correct child. child .focus_history .set(Some(self.focus_history.add_last(child.clone()))); self.rebuild_tab_bar(); if self.toplevel_data.visible.get() { self.perform_layout(); child.node.tl_set_visible(true); } else { self.schedule_layout(); } child.node.tl_restack_popups(); // log::info!("activate_child2") } } pub fn set_mono(self: &Rc, child: Option<&dyn ToplevelNode>) { if self.mono_child.is_some() == child.is_some() { return; } let child = { let children = self.child_nodes.borrow(); match child { Some(c) => match children.get(&c.node_id()) { Some(c) => Some(c.to_ref()), _ => { log::warn!("set_mono called with a node that is not a child"); return; } }, _ => None, } }; if self.toplevel_data.visible.get() { if let Some(child) = &child { let child_id = child.node.node_id(); let mut seats = SmallVec::<[_; 3]>::new(); for other in self.children.iter() { if other.node.node_id() != child_id { collect_kb_foci2(other.node.clone(), &mut seats); other.node.tl_set_visible(false); } } for seat in seats { child .node .clone() .node_do_focus(&seat, Direction::Unspecified); } child.node.tl_restack_popups(); } else { for child in self.children.iter() { child.node.tl_set_visible(true); } } } self.mono_child.set(child.clone()); self.mono_transition_animation_pending.set(true); if child.is_some() { self.rebuild_tab_bar(); } else { *self.tab_bar.borrow_mut() = None; } self.update_content_size(); self.damage(); self.schedule_layout(); // Notify parent to rebuild its tab bar if we're a child of a mono container. // Our tab title prefix changes when we enter/leave mono mode ([T] vs [H]/[V]). if let Some(parent) = self.toplevel_data.parent.get() { if let Some(pc) = parent.node_into_container() { if pc.mono_child.is_some() { pc.rebuild_tab_bar(); pc.damage(); } } } } pub fn set_split(self: &Rc, split: ContainerSplit) { if self.split.replace(split) != split { self.update_content_size(); // log::info!("set_split"); self.schedule_layout(); } } /// Rebuild the tab bar entries from the current children. /// /// Called when entering mono mode or when children change while in mono mode. fn rebuild_tab_bar(self: &Rc) { self.rebuild_tab_bar_with_override(None, None); } /// Generate a tab title for a child node. For windows this is the window /// title or app_id. For container (group) children, we show a layout /// prefix like hy3: `[H] child_title`, `[V] child_title`, `[T] child_title`. fn get_child_tab_title( &self, child: &ContainerChild, override_id: Option, override_title: Option<&str>, ) -> String { let child_id = child.node.node_id(); // If this child is a container (group), show layout prefix + focused child title. if let Some(cn) = child.node.clone().node_into_container() { let prefix = if cn.mono_child.is_some() { "[T]" } else { match cn.split.get() { ContainerSplit::Horizontal => "[H]", ContainerSplit::Vertical => "[V]", } }; // Get the focused child's title recursively. let inner = if let Some(focused) = cn.focus_history.last() { let inner_child = ContainerChild { node: focused.node.clone(), active: Cell::new(false), body: Default::default(), content: Default::default(), factor: Cell::new(0.0), focus_history: Default::default(), attention_requested: Cell::new(false), border_color_is_focused: Default::default(), }; cn.get_child_tab_title(&inner_child, override_id, override_title) } else { "Group".to_string() }; return format!("{} {}", prefix, inner); } // Window node: use override, title, app_id, or "untitled". let raw_title = if override_id == Some(child_id) && override_title.is_some() { override_title.unwrap().to_string() } else { child.node.tl_data().title.borrow().clone() }; if !raw_title.is_empty() { return raw_title; } if override_id == Some(child_id) { let app = child.node.tl_data().app_id.borrow().clone(); if !app.is_empty() { return app; } } else { let app = child.node.tl_data().app_id.borrow().clone(); if !app.is_empty() { return app; } } "untitled".to_string() } /// Rebuild the tab bar. If `override_id` and `override_title` are provided, /// use the override title for that child instead of borrowing it (avoids /// RefCell double-borrow when called from node_child_title_changed). fn rebuild_tab_bar_with_override( self: &Rc, override_id: Option, override_title: Option<&str>, ) { let mono = self.mono_child.get(); if mono.is_none() { *self.tab_bar.borrow_mut() = None; return; } let mono_ref = mono.as_ref().unwrap(); let active_id = mono_ref.node.node_id(); let height = self.state.theme.sizes.tab_bar_height.get(); let render_scale = self .workspace .get() .output .get() .global .persistent .scale .get(); let old_textures: AHashMap<_, _> = self .tab_bar .borrow() .as_ref() .map(|bar| { bar.entries .iter() .map(|entry| (entry.child_id, entry.title_texture.clone())) .collect() }) .unwrap_or_default(); let mut bar = TabBar::new(height, render_scale); for child in self.children.iter() { let child_id = child.node.node_id(); let title = self.get_child_tab_title(&child, override_id, override_title); let title_texture = old_textures .get(&child_id) .cloned() .unwrap_or_else(|| Rc::new(RefCell::new(None))); bar.entries.push(TabBarEntry { child_id, title, title_texture, active: child_id == active_id, attention_requested: child.attention_requested.get(), x: Cell::new(0), width: Cell::new(0), }); } let padding = self.state.theme.sizes.tab_bar_padding.get(); bar.layout_entries(self.width.get(), padding); *self.tab_bar.borrow_mut() = Some(bar); self.schedule_update_tab_textures(); } /// Wrap the focused child in a new sub-container with the given split direction. /// /// This is hy3's `makegroup` operation. pub fn make_group(self: &Rc, split: ContainerSplit, ephemeral: bool) { let Some(focused) = self.focus_history.last() else { return; }; let focused_node = focused.node.clone(); let focused_active = focused_node.tl_data().active(); if self.num_children.get() == 1 { let sub = ContainerNode::new( &self.state, &self.workspace.get(), focused_node.clone(), split, ); let sub_id = sub.node_id(); if ephemeral { sub.ephemeral.set(Ephemeral::On); } self.clone().cnode_replace_child(&*focused_node, sub); if focused_active && let Some(group) = self.child_nodes.borrow().get(&sub_id).map(|n| n.to_ref()) { self.update_child_active(&group, true, 1); } return; } // Record the sibling that comes AFTER the focused child so we can // insert the new group at the same position. let next_sibling: Option> = { let nodes = self.child_nodes.borrow(); nodes .get(&focused_node.node_id()) .and_then(|ln| ln.next()) .map(|n| n.node.clone()) }; // Temporarily disable ephemeral collapse during make_group so // removing the focused child doesn't destroy this container. let was_ephemeral = self.ephemeral.replace(Ephemeral::Off); self.clone().cnode_remove_child2(&*focused_node, true); self.ephemeral.set(was_ephemeral); let focused_active = focused_node.tl_data().active(); let sub = ContainerNode::new(&self.state, &self.workspace.get(), focused_node, split); let sub_id = sub.node_id(); if ephemeral { sub.ephemeral.set(Ephemeral::On); } // Insert at the original position instead of appending to the end. if let Some(ref next) = next_sibling { self.add_child_before(&**next, sub); } else { // Was the last child — append. self.append_child(sub); } if focused_active && let Some(group) = self.child_nodes.borrow().get(&sub_id).map(|n| n.to_ref()) { self.update_child_active(&group, true, 1); } } /// Change this container's split direction (hy3's `changegroup`). /// /// `opposite` toggles H↔V, `toggletab` toggles between mono and split. pub fn change_group(self: &Rc, action: ChangeGroupAction) { match action { ChangeGroupAction::Opposite => { let new_split = match self.split.get() { ContainerSplit::Horizontal => ContainerSplit::Vertical, ContainerSplit::Vertical => ContainerSplit::Horizontal, }; self.set_split(new_split); } ChangeGroupAction::ToggleTab => { if self.mono_child.is_some() { // Exit mono/tabbed mode. self.set_mono(None); } else if let Some(focused) = self.focus_history.last() { // Enter mono/tabbed mode with the focused child. self.set_mono(Some(&*focused.node)); } } } } /// Reset all children's factors to equal (hy3's `equalize`). /// Traverse up to find the nearest ancestor with more than 1 child, /// then equalize that container's children. pub fn equalize(self: &Rc) { let mut c = self.clone(); while c.num_children.get() <= 1 { let parent = match c.toplevel_data.parent.get() { Some(p) => p, None => break, }; c = match parent.node_into_container() { Some(p) => p, None => break, }; } c.equalize_here(); } fn equalize_here(self: &Rc) { let n = self.num_children.get(); if n == 0 { return; } let factor = 1.0 / n as f64; let mut sum = 0.0; for child in self.children.iter() { child.factor.set(factor); sum += factor; } self.sum_factors.set(sum); self.schedule_layout(); } /// Recursively equalize all descendant containers. pub fn equalize_recursive(self: &Rc) { let mut c = self.clone(); while c.num_children.get() <= 1 { let parent = match c.toplevel_data.parent.get() { Some(p) => p, None => break, }; c = match parent.node_into_container() { Some(p) => p, None => break, }; } c.equalize_recursive_impl(); } fn equalize_recursive_impl(self: &Rc) { self.equalize_here(); for child in self.children.iter() { if let Some(cn) = child.node.clone().node_into_container() { cn.equalize_recursive_impl(); } } } /// Move the currently active tab left or right within the tab bar. /// /// This is the equivalent of hy3's `movewindow` within a tabbed group. pub fn move_tab(self: &Rc, right: bool) { let mc = match self.mono_child.get() { Some(mc) => mc, None => return, }; let child_nodes = self.child_nodes.borrow(); let Some(link) = child_nodes.get(&mc.node.node_id()) else { return; }; let active_ref = link.to_ref(); if right { if let Some(next) = active_ref.next() { // Move active tab after the next sibling (moves right). next.append_existing(&active_ref); } } else { if let Some(prev) = active_ref.prev() { // Move active tab before the previous sibling (moves left). prev.prepend_existing(&active_ref); } } drop(child_nodes); self.rebuild_tab_bar(); self.damage(); } fn parent_container(&self) -> Option> { self.toplevel_data .parent .get() .and_then(|p| p.node_into_container()) } fn find_neighboring_output(&self, direction: Direction) -> Option> { if self.toplevel_data.parent.is_none() { return None; } if self.toplevel_data.float.is_some() { return None; } self.state .find_output_in_direction(&self.workspace.get().output.get(), direction) } pub fn move_focus_from_child( self: Rc, seat: &Rc, child: &dyn ToplevelNode, direction: Direction, ) { let child = match self.child_nodes.borrow().get(&child.node_id()) { Some(c) => c.to_ref(), _ => return, }; let mc = self.mono_child.get(); let in_line = if mc.is_some() { matches!(direction, Direction::Left | Direction::Right) } else { match self.split.get() { ContainerSplit::Horizontal => { matches!(direction, Direction::Left | Direction::Right) } ContainerSplit::Vertical => matches!(direction, Direction::Up | Direction::Down), } }; let focus_in_parent = || { if let Some(parent) = self.toplevel_data.parent.get() { if let Some(c) = parent.node_into_container() { c.move_focus_from_child(seat, self.deref(), direction); } else if let Some(output) = self.find_neighboring_output(direction) { output.take_keyboard_navigation_focus(seat, direction); } } }; if !in_line { focus_in_parent(); return; } let prev = match direction { Direction::Left => true, Direction::Down => false, Direction::Up => true, Direction::Right => false, Direction::Unspecified => true, }; let sibling = match prev { true => child.prev(), false => child.next(), }; let sibling = match sibling { Some(s) => s, None => { focus_in_parent(); return; } }; if mc.is_some() { self.activate_child(&sibling); } else { sibling.node.clone().node_do_focus(seat, direction); } } // pub fn move_child(self: Rc, child: Rc, direction: Direction) { let move_to_neighboring_output = |child: Rc| { let Some(output) = self.find_neighboring_output(direction) else { return; }; let ws = output.ensure_workspace(); let mut foci = SmallVec::new(); let move_foci = !ws.container_visible(); if move_foci { collect_kb_foci2(child.clone(), &mut foci); } if let Some(c) = ws.container.get() { self.clone().cnode_remove_child2(&*child, true); c.insert_child(child, direction); } else { toplevel_set_workspace(&self.state, child, &ws); } if move_foci { for seat in foci { ws.clone().node_do_focus(&seat, Direction::Unspecified); } } }; // CASE 1: This is the only child of the container. Replace the container by the child. if self.num_children.get() == 1 { if let Some(parent) = self.toplevel_data.parent.get() && !self.toplevel_data.is_fullscreen.get() { if parent.cnode_accepts_child(&*child) { parent.cnode_replace_child(self.deref(), child.clone()); self.toplevel_data.parent.take(); self.child_nodes.borrow_mut().clear(); self.tl_destroy(); } else { move_to_neighboring_output(child); } } return; } let (split, prev) = direction_to_split(direction); if self.mono_child.is_some() && split == ContainerSplit::Horizontal { let cc = match self.child_nodes.borrow().get(&child.node_id()) { Some(l) => l.to_ref(), None => return, }; let neighbor = match prev { true => cc.prev(), false => cc.next(), }; if let Some(neighbor) = neighbor { if let Some(cn) = neighbor.node.clone().node_into_container() && cn.cnode_accepts_child(&*child) { if let Some(mc) = self.mono_child.get() && mc.node.node_id() == child.node_id() { self.activate_child2(&neighbor, true); } self.cnode_remove_child2(&*child, true); cn.insert_child_from_direction(child, direction); return; } match prev { true => neighbor.prepend_existing(&cc), false => neighbor.append_existing(&cc), } self.rebuild_tab_bar(); self.damage(); return; } } // CASE 2: We're moving the child within the container. if split == self.split.get() || (split == ContainerSplit::Horizontal && self.mono_child.is_some()) { let cc = match self.child_nodes.borrow().get(&child.node_id()) { Some(l) => l.to_ref(), None => return, }; let neighbor = match prev { true => cc.prev(), false => cc.next(), }; if let Some(neighbor) = neighbor { if let Some(cn) = neighbor.node.clone().node_into_container() && cn.cnode_accepts_child(&*child) { if let Some(mc) = self.mono_child.get() && mc.node.node_id() == child.node_id() { self.activate_child2(&neighbor, true); } self.cnode_remove_child2(&*child, true); cn.insert_child_from_direction(child, direction); return; } match prev { true => neighbor.prepend_existing(&cc), false => neighbor.append_existing(&cc), } // log::info!("move_child"); self.schedule_layout(); return; } } // CASE 3: We're moving the child out of the container. let mut neighbor = self.clone(); let mut parent_opt = self.parent_container(); while let Some(parent) = &parent_opt { if parent.split.get() == split { break; } neighbor = parent.clone(); parent_opt = parent.parent_container(); } let parent = match parent_opt { Some(p) => p, _ => { move_to_neighboring_output(child); return; } }; let was_ephemeral = self.ephemeral.replace(Ephemeral::Off); self.clone().cnode_remove_child2(&*child, true); match prev { true => parent.add_child_before(&*neighbor, child.clone()), false => parent.add_child_after(&*neighbor, child.clone()), } self.ephemeral.set(was_ephemeral); self.collapse_ephemeral(); } pub fn insert_child(self: &Rc, node: Rc, direction: Direction) { // Autotile: if the container would become too narrow/tall, wrap the // focused child and new node in a perpendicular sub-container. if self.state.theme.autotile_enabled.get() && self.mono_child.is_none() { let (pw, ph) = self.predict_child_body_size(); let opposite = match self.split.get() { ContainerSplit::Horizontal if pw > 0 && ph > 0 && pw < ph => { Some(ContainerSplit::Vertical) } ContainerSplit::Vertical if pw > 0 && ph > 0 && ph < pw => { Some(ContainerSplit::Horizontal) } _ => None, }; if let Some(opp_split) = opposite { if let Some(focused) = self.focus_history.last() { if self.num_children.get() <= 1 { // Single child, autotile not applicable. } else { let focused_node = focused.node.clone(); let was_ephemeral = self.ephemeral.replace(Ephemeral::Off); self.clone().cnode_remove_child2(&*focused_node, true); self.ephemeral.set(was_ephemeral); let sub = ContainerNode::new( &self.state, &self.workspace.get(), focused_node, opp_split, ); sub.ephemeral.set(Ephemeral::On); sub.append_child(node); self.append_child(sub); return; } } } } let (split, right) = direction_to_split(direction); if split != self.split.get() || right { self.append_child(node); } else { self.prepend_child(node); } } fn insert_child_from_direction( self: &Rc, node: Rc, direction: Direction, ) { match direction { Direction::Right | Direction::Down => self.prepend_child(node), Direction::Left | Direction::Up | Direction::Unspecified => self.append_child(node), } } fn update_child_active( self: &Rc, node: &NodeRef, active: bool, depth: u32, ) { if depth == 1 { node.active.set(active); } if active { node.focus_history .set(Some(self.focus_history.add_last(node.clone()))); } // log::info!("node_child_active_changed"); self.schedule_compute_render_positions(); if self.state.theme.sizes.gap.get() != 0 && self.toplevel_data.visible.get() { self.damage(); } if let Some(parent) = self.toplevel_data.parent.get() { parent.node_child_active_changed(self.deref(), active, depth + 1); } } fn update_child_size(&self, node: &NodeRef, width: i32, height: i32) { let rect = Rect::new_saturating(0, 0, width, height); node.content.set(rect); node.position_content(); if let Some(mono) = self.mono_child.get() && mono.node.node_id() == node.node.node_id() { let body = self.mono_body.get(); self.mono_content.set(rect.at_point(body.x1(), body.y1())); } } fn pull_child_properties(self: &Rc, child: &NodeRef) { let data = child.node.tl_data(); { let attention_requested = data.wants_attention.get(); child.attention_requested.set(attention_requested); if attention_requested { self.mod_attention_requests(true); } } self.update_child_active(child, data.active(), 1); { let pos = data.pos.get(); self.update_child_size(child, pos.width(), pos.height()); } } fn discard_child_properties(&self, child: &ContainerChild) { if child.attention_requested.get() { self.mod_attention_requests(false); } } fn collapse_ephemeral(self: &Rc) { if self.ephemeral.get() != Ephemeral::On || self.num_children.get() != 1 || self.toplevel_data.is_fullscreen.get() { return; } if let Some(parent) = self.toplevel_data.parent.get() && let Some(only_child) = self.children.first() { let child_node = only_child.node.clone(); if parent.cnode_accepts_child(&*child_node) { parent.cnode_replace_child(self.deref(), child_node); self.toplevel_data.parent.take(); self.child_nodes.borrow_mut().clear(); self.tl_destroy(); } } } fn mod_attention_requests(&self, set: bool) { let propagate = self.attention_requests.adj(set); if set || propagate { self.toplevel_data.set_wants_attention(set); } if propagate && let Some(parent) = self.toplevel_data.parent.get() { parent.cnode_child_attention_request_changed(self, set); } } fn button( self: Rc, id: CursorType, _seat: &Rc, _time_usec: u64, pressed: bool, button: u32, ) { let mut seat_datas = self.cursors.borrow_mut(); let seat_data = match seat_datas.get_mut(&id) { Some(s) => s, _ => return, }; if button != BTN_LEFT { return; } if seat_data.op.is_none() { if !pressed { return; } // Handle tab bar clicks in mono mode. if self.mono_child.is_some() { let tab_bar = self.tab_bar.borrow(); if let Some(tb) = tab_bar.as_ref() { if seat_data.y >= 0 && seat_data.y < tb.height { if let Some(idx) = tb.entry_at_x(seat_data.x) { let child_id = tb.entries[idx].child_id; drop(tab_bar); drop(seat_datas); let children = self.child_nodes.borrow(); if let Some(child) = children.get(&child_id) { let child_ref = child.to_ref(); drop(children); self.activate_child(&child_ref); } return; } } } } let (kind, child) = 'res: { let mono = self.mono_child.is_some(); for child in self.children.iter() { if !mono { if self.split.get() == ContainerSplit::Horizontal { if seat_data.x < child.body.get().x1() { let Some(prev) = child.prev() else { continue; }; break 'res ( SeatOpKind::Resize { dist_left: seat_data.x - prev.body.get().x2(), dist_right: child.body.get().x1() - seat_data.x, }, child, ); } } else { if seat_data.y < child.body.get().y1() { let Some(prev) = child.prev() else { continue; }; break 'res ( SeatOpKind::Resize { dist_left: seat_data.y - prev.body.get().y2(), dist_right: child.body.get().y1() - seat_data.y, }, child, ); } } } } return; }; seat_data.op = Some(SeatOp { child, kind }) } else if !pressed { seat_data.op = None; drop(seat_datas); } } fn tile_drag_destination_mono( self: &Rc, mc: &ContainerChild, source: NodeId, abs_bounds: Rect, abs_x: i32, abs_y: i32, ) -> Option { let body = self.mono_body.get(); let mut bounds = body .move_(self.abs_x1.get(), self.abs_y1.get()) .intersect(abs_bounds); if mc.node.node_id() != source && !mc.node.node_is_container() { let delta = bounds.width() / 5; for before in [true, false] { let (x1, x2); match before { true => { x1 = bounds.x1(); x2 = bounds.x1() + delta; } false => { x1 = bounds.x2() - delta; x2 = bounds.x2(); } } if abs_x >= x1 && abs_x < x2 { return Some(TileDragDestination { highlight: Rect::new(x1, bounds.y1(), x2, bounds.y2())?, ty: TddType::Insert { container: self.clone(), neighbor: mc.node.clone(), before, }, }); } } bounds = Rect::new( bounds.x1() + delta, bounds.y1(), bounds.x2() - delta, bounds.y2(), )?; } return mc .node .clone() .tl_tile_drag_destination(source, None, bounds, abs_x, abs_y); } pub fn tile_drag_destination( self: &Rc, source: NodeId, abs_bounds: Rect, abs_x: i32, abs_y: i32, ) -> Option { if source == self.node_id() { return None; } if let Some(mc) = self.mono_child.get() { return self.tile_drag_destination_mono(&mc, source, abs_bounds, abs_x, abs_y); } let mut prev_is_source = false; let mut prev_border_start = 0; let split = self.split.get(); for child in self.children.iter() { if child.node.node_id() == source { prev_is_source = true; continue; } let start_drag_bounds = child.node.tl_tile_drag_bounds(split, true); let end_drag_bounds = child.node.tl_tile_drag_bounds(split, false); let body = child.body.get(); let main_body_rect = { match split { ContainerSplit::Horizontal => Rect::new( body.x1() + start_drag_bounds, body.y1(), body.x2() - end_drag_bounds, body.y2(), )?, ContainerSplit::Vertical => Rect::new( body.x1(), body.y1() + start_drag_bounds, body.x2(), body.y2() - end_drag_bounds, )?, } .move_(self.abs_x1.get(), self.abs_y1.get()) .intersect(abs_bounds) }; if main_body_rect.contains(abs_x, abs_y) { return child.node.clone().tl_tile_drag_destination( source, Some(split), main_body_rect, abs_x, abs_y, ); } if !prev_is_source { let left_border_rect = { match split { ContainerSplit::Horizontal => Rect::new( prev_border_start, body.y1(), body.x1() + start_drag_bounds, body.y2(), )?, ContainerSplit::Vertical => Rect::new( body.x1(), prev_border_start, body.x2(), body.y1() + start_drag_bounds, )?, } .move_(self.abs_x1.get(), self.abs_y1.get()) .intersect(abs_bounds) }; if left_border_rect.contains(abs_x, abs_y) { return Some(TileDragDestination { highlight: left_border_rect, ty: TddType::Insert { container: self.clone(), neighbor: child.node.clone(), before: true, }, }); } } prev_is_source = false; prev_border_start = match split { ContainerSplit::Horizontal => body.x2() - end_drag_bounds, ContainerSplit::Vertical => body.y2() - end_drag_bounds, }; } if prev_is_source { return None; } let last = self.children.last()?; let body = last.body.get(); let right_border_rect = match split { ContainerSplit::Horizontal => { Rect::new(prev_border_start, body.y1(), body.x2(), body.y2())? } ContainerSplit::Vertical => { Rect::new(body.x1(), prev_border_start, body.x2(), body.y2())? } } .move_(self.abs_x1.get(), self.abs_y1.get()) .intersect(abs_bounds); if right_border_rect.contains(abs_x, abs_y) { return Some(TileDragDestination { highlight: right_border_rect, ty: TddType::Insert { container: self.clone(), neighbor: last.node.clone(), before: false, }, }); } None } } struct SeatOp { child: NodeRef, kind: SeatOpKind, } #[derive(Copy, Clone, Debug, Eq, PartialEq)] enum SeatOpKind { Resize { dist_left: i32, dist_right: i32 }, } impl Node for ContainerNode { fn node_id(&self) -> NodeId { self.id.into() } fn node_seat_state(&self) -> &NodeSeatState { &self.toplevel_data.seat_state } fn node_visit(self: Rc, visitor: &mut dyn NodeVisitor) { visitor.visit_container(&self); } fn node_visit_children(&self, visitor: &mut dyn NodeVisitor) { for child in self.children.iter() { child.node.clone().node_visit(visitor); } } fn node_visible(&self) -> bool { self.toplevel_data.visible.get() } fn node_absolute_position(&self) -> Rect { Rect::new_sized_saturating( self.abs_x1.get(), self.abs_y1.get(), self.width.get(), self.height.get(), ) } fn node_output(&self) -> Option> { self.toplevel_data.output_opt() } fn node_location(&self) -> Option { Some(self.location.get()) } fn node_layer(&self) -> NodeLayerLink { self.toplevel_data.node_layer() } fn node_child_title_changed(self: Rc, child: &dyn Node, title: &str) { self.rebuild_tab_bar_with_override(Some(child.node_id()), Some(title)); self.damage(); } fn node_do_focus(self: Rc, seat: &Rc, direction: Direction) { let node = if let Some(cn) = self.mono_child.get() { Some(cn) } else { let split = self.split.get(); match (direction, split) { (Direction::Left, ContainerSplit::Horizontal) => self.children.last(), (Direction::Down, ContainerSplit::Vertical) => self.children.first(), (Direction::Up, ContainerSplit::Vertical) => self.children.last(), (Direction::Right, ContainerSplit::Horizontal) => self.children.first(), _ => match self.focus_history.last() { Some(n) => Some(n.deref().clone()), None => self.children.last(), }, } }; if let Some(node) = node { node.node.clone().node_do_focus(seat, direction); } } fn node_active_changed(&self, active: bool) { self.toplevel_data.update_self_active(self, active); } fn node_find_tree_at( &self, x: i32, y: i32, tree: &mut Vec, usecase: FindTreeUsecase, ) -> FindTreeResult { let mut recurse = |content: Rect, child: NodeRef| { if content.contains(x, y) { let (x, y) = content.translate(x, y); tree.push(FoundNode { node: child.node.clone(), x, y, }); child.node.node_find_tree_at(x, y, tree, usecase); } }; if let Some(child) = self.mono_child.get() { recurse(self.mono_content.get(), child); } else { for child in self.children.iter() { if child.body.get().contains(x, y) { recurse(child.content.get(), child); break; } } } FindTreeResult::AcceptsInput } fn node_child_size_changed(&self, child: &dyn Node, width: i32, height: i32) { let cn = self.child_nodes.borrow(); if let Some(node) = cn.get(&child.node_id()) { self.update_child_size(node, width, height); } if self.all_children_match_body() { self.all_children_resized.trigger(); if self.toplevel_data.visible.get() { self.damage(); } } } fn node_child_active_changed(self: Rc, child: &dyn Node, active: bool, depth: u32) { if let Some(l) = self.child_nodes.borrow().get(&child.node_id()) { self.update_child_active(l, active, depth); } } fn node_render(&self, renderer: &mut Renderer, x: i32, y: i32, _bounds: Option<&Rect>) { renderer.render_container(self, x, y); } fn node_toplevel(self: Rc) -> Option> { Some(self) } fn node_make_visible(self: Rc) { self.toplevel_data.make_visible(&*self); } fn node_on_button( self: Rc, seat: &Rc, time_usec: u64, button: u32, state: ButtonState, _serial: u64, ) { let id = CursorType::Seat(seat.id()); self.button(id, seat, time_usec, state == ButtonState::Pressed, button); } fn node_on_axis_event(self: Rc, _seat: &Rc, event: &PendingScroll) { if self.mono_child.is_none() { return; } // Use vertical scroll (index 1) to switch tabs. let v = match event.v120[1].get() { Some(v) if v != 0 => v, _ => return, }; let mono = match self.mono_child.get() { Some(m) => m, None => return, }; let next = if v > 0 { // Scroll down → next tab. mono.next().or_else(|| self.children.first()) } else { // Scroll up → previous tab. mono.prev().or_else(|| self.children.last()) }; if let Some(next) = next { if next.node.node_id() != mono.node.node_id() { self.activate_child(&next); } } } fn node_on_leave(&self, seat: &WlSeatGlobal) { let mut seats = self.cursors.borrow_mut(); let id = CursorType::Seat(seat.id()); if let Some(seat_state) = seats.get_mut(&id) { seat_state.op = None; } } fn node_on_pointer_enter(self: Rc, seat: &Rc, x: Fixed, y: Fixed) { // log::info!("node_on_pointer_enter"); self.pointer_move( seat, CursorType::Seat(seat.id()), seat.pointer_cursor(), x, y, false, ); } fn node_on_pointer_unfocus(&self, seat: &Rc) { // log::info!("unfocus"); let mut seats = self.cursors.borrow_mut(); let id = CursorType::Seat(seat.id()); if let Some(seat_state) = seats.get_mut(&id) { seat_state.target = false; } } fn node_on_pointer_focus(&self, seat: &Rc) { // log::info!("container focus"); let mut seats = self.cursors.borrow_mut(); let id = CursorType::Seat(seat.id()); if let Some(seat_state) = seats.get_mut(&id) { seat_state.target = true; seat.pointer_cursor().set_known(seat_state.cursor); } } fn node_on_pointer_motion(self: Rc, seat: &Rc, x: Fixed, y: Fixed) { // log::info!("node_on_pointer_motion"); self.pointer_move( seat, CursorType::Seat(seat.id()), seat.pointer_cursor(), x, y, false, ); } fn node_on_tablet_tool_leave(&self, tool: &Rc, _time_usec: u64) { let id = CursorType::TabletTool(tool.id); self.cursors.borrow_mut().remove(&id); } fn node_on_tablet_tool_enter( self: Rc, tool: &Rc, _time_usec: u64, x: Fixed, y: Fixed, ) { tool.cursor().set_known(KnownCursor::Default); self.pointer_move( tool.seat(), CursorType::TabletTool(tool.id), tool.cursor(), x, y, true, ); } fn node_on_tablet_tool_apply_changes( self: Rc, tool: &Rc, time_usec: u64, changes: Option<&TabletToolChanges>, x: Fixed, y: Fixed, ) { let id = CursorType::TabletTool(tool.id); self.pointer_move(tool.seat(), id, tool.cursor(), x, y, false); if let Some(changes) = changes && let Some(pressed) = changes.down { self.button(id, tool.seat(), time_usec, pressed, BTN_LEFT); } } fn node_into_container(self: Rc) -> Option> { Some(self.clone()) } fn node_into_containing_node(self: Rc) -> Option> { Some(self) } fn node_into_toplevel(self: Rc) -> Option> { Some(self) } fn node_is_container(&self) -> bool { true } } impl ContainingNode for ContainerNode { fn cnode_replace_child(self: Rc, old: &dyn Node, new: Rc) { let node = match self.child_nodes.borrow_mut().remove(&old.node_id()) { Some(c) => c, None => { log::error!("Trying to replace a node that isn't a child of this container"); return; } }; let (have_mc, was_mc) = match self.mono_child.get() { None => (false, false), Some(mc) => (true, mc.node.node_id() == old.node_id()), }; self.discard_child_properties(&node); let link = node.append(ContainerChild { node: new.clone(), active: Cell::new(false), body: Cell::new(node.body.get()), content: Default::default(), factor: Cell::new(node.factor.get()), focus_history: Cell::new(None), attention_requested: Cell::new(false), border_color_is_focused: Default::default(), }); if let Some(fh) = node.focus_history.take() { link.focus_history.set(Some(fh.append(link.to_ref()))); } let visible = node.node.node_visible(); drop(node); let mut body = None; if was_mc { self.mono_child.set(Some(link.to_ref())); link.node.tl_restack_popups(); body = Some(self.mono_body.get()); } else if !have_mc { body = Some(link.body.get()); }; let link_ref = link.to_ref(); self.child_nodes.borrow_mut().insert(new.node_id(), link); new.tl_set_parent(self.clone()); self.pull_child_properties(&link_ref); new.tl_set_visible(visible); if let Some(body) = body { let body = body.move_(self.abs_x1.get(), self.abs_y1.get()); new.clone().tl_change_extents(&body); self.state.damage(body); } } fn cnode_remove_child2(self: Rc, child: &dyn Node, preserve_focus: bool) { let node = match self.child_nodes.borrow_mut().remove(&child.node_id()) { Some(c) => c, None => return, }; node.focus_history.set(None); self.discard_child_properties(&node); if let Some(mono) = self.mono_child.get() { if mono.node.node_id() == child.node_id() { let mut new = self.focus_history.last().map(|n| n.deref().clone()); if new.is_none() { new = node.next(); if new.is_none() { new = node.prev(); } } if let Some(child) = &new { self.activate_child2(child, preserve_focus); } } } let node = { let node = node; node.focus_history.set(None); node.to_ref() }; let num_children = self.num_children.fetch_sub(1) - 1; if num_children == 0 { self.damage(); self.tl_destroy(); return; } self.update_content_size(); let rem = 1.0 - node.factor.get(); let mut sum = 0.0; if rem <= 0.0 { let factor = 1.0 / num_children as f64; for child in self.children.iter() { child.factor.set(factor) } sum = 1.0; } else { for child in self.children.iter() { let factor = child.factor.get() / rem; child.factor.set(factor); sum += factor; } } self.sum_factors.set(sum); if self.ephemeral.get() == Ephemeral::On && num_children == 1 { self.collapse_ephemeral(); if self.toplevel_data.parent.is_none() { return; } } // log::info!("cnode_remove_child2"); self.rebuild_tab_bar(); if self.state.animations.enabled.get() && !self.state.suppress_animations_for_next_layout.get() { self.animate_next_layout.set(true); } self.schedule_layout(); self.cancel_seat_ops(); self.child_removed.trigger(); } fn cnode_accepts_child(&self, _node: &dyn Node) -> bool { true } fn cnode_child_attention_request_changed(self: Rc, child: &dyn Node, set: bool) { let children = self.child_nodes.borrow(); let child = match children.get(&child.node_id()) { Some(c) => c, _ => return, }; if child.attention_requested.replace(set) == set { return; } self.mod_attention_requests(set); drop(children); self.rebuild_tab_bar(); self.schedule_compute_render_positions(); } fn cnode_workspace(self: Rc) -> Rc { self.workspace.get() } fn cnode_make_visible(self: Rc, child: &dyn Node) { let Some(child) = self .child_nodes .borrow() .get(&child.node_id()) .map(|n| n.to_ref()) else { return; }; self.toplevel_data.make_visible(&*self); if !self.node_visible() { return; } let Some(cur) = self.mono_child.get() else { return; }; if cur.node.node_id() == child.node.node_id() { return; } self.activate_child(&child); } fn cnode_set_child_position(self: Rc, child: &dyn Node, x: i32, y: i32) { let Some(parent) = self.toplevel_data.parent.get() else { return; }; if self.mono_child.is_some() { parent.cnode_set_child_position(&*self, x, y); } else { let children = self.child_nodes.borrow(); let Some(child) = children.get(&child.node_id()) else { return; }; let pos = child.body.get(); let (x, y) = pos.translate(x, y); parent.cnode_set_child_position(&*self, x, y); } } fn cnode_resize_child( self: Rc, child: &dyn Node, new_x1: Option, mut new_y1: Option, new_x2: Option, new_y2: Option, ) { let theme = &self.state.theme; let mut left_outside = false; let mut right_outside = false; let mut top_outside = false; let mut bottom_outside = false; if self.mono_child.is_some() { // Adjust y1 for the tab bar offset. The window's y1 is offset // from the container's y1 by the tab bar height, so we must // subtract it before propagating upward. let tbh = theme.sizes.tab_bar_height.get(); let tbg = theme.sizes.tab_bar_gap.get(); new_y1 = new_y1.map(|v| v - tbh - tbg); top_outside = true; right_outside = true; bottom_outside = true; left_outside = true; } else { let children = self.child_nodes.borrow(); let Some(child) = children.get(&child.node_id()) else { return; }; let pos = child.body.get(); let split = self.split.get(); let mut changed_any = false; let (mut i1, mut i2, new_i1, new_i2, mut ci) = match split { ContainerSplit::Horizontal => { top_outside = true; bottom_outside = true; (pos.x1(), pos.x2(), new_x1, new_x2, self.content_width.get()) } ContainerSplit::Vertical => { right_outside = true; left_outside = true; ( pos.y1(), pos.y2(), new_y1, new_y2, self.content_height.get(), ) } }; if ci == 0 { ci = 1; } let between = self.child_spacing(); let (new_delta, between) = match split { ContainerSplit::Horizontal => (self.abs_x1.get(), between), ContainerSplit::Vertical => (self.abs_y1.get(), between), }; let new_i1 = new_i1.map(|v| v - new_delta); let new_i2 = new_i2.map(|v| v - new_delta); let (orig_i1, orig_i2) = (i1, i2); let mut sum_factors = self.sum_factors.get(); if let Some(new_i1) = new_i1 { if let Some(peer) = child.prev() { let peer_pos = peer.body.get(); let peer_i1 = match self.split.get() { ContainerSplit::Horizontal => peer_pos.x1(), ContainerSplit::Vertical => peer_pos.y1(), }; i1 = new_i1.max(peer_i1 + between).min(i2); if i1 != orig_i1 { let peer_factor = (i1 - between - peer_i1) as f64 / ci as f64; sum_factors = sum_factors - peer.factor.get() + peer_factor; peer.factor.set(peer_factor); changed_any = true; } } else { match split { ContainerSplit::Horizontal => left_outside = true, ContainerSplit::Vertical => top_outside = true, } } } if let Some(new_i2) = new_i2 { if let Some(peer) = child.next() { let peer_pos = peer.body.get(); let peer_i2 = match self.split.get() { ContainerSplit::Horizontal => peer_pos.x2(), ContainerSplit::Vertical => peer_pos.y2(), }; i2 = new_i2.min(peer_i2 - between).max(i1); if i2 != orig_i2 { let peer_factor = (peer_i2 - between - i2) as f64 / ci as f64; sum_factors = sum_factors - peer.factor.get() + peer_factor; peer.factor.set(peer_factor); changed_any = true; } } else { match split { ContainerSplit::Horizontal => right_outside = true, ContainerSplit::Vertical => bottom_outside = true, } } } if changed_any { let factor = (i2 - i1) as f64 / ci as f64; sum_factors = sum_factors - child.factor.get() + factor; child.factor.set(factor); self.sum_factors.set(sum_factors); self.schedule_layout_immediate(); } } let pos = self.node_absolute_position(); let mut x1 = None; let mut x2 = None; let mut y1 = None; let mut y2 = None; if left_outside { x1 = new_x1.map(|v| v.min(pos.x2())); } if right_outside { x2 = new_x2.map(|v| v.max(x1.unwrap_or(pos.x1()))); } if top_outside { y1 = new_y1.map(|v| v.min(pos.y2())); } if bottom_outside { y2 = new_y2.map(|v| v.max(y1.unwrap_or(pos.y1()))); } if ((x1.is_some() && x1 != Some(pos.x1())) || (x2.is_some() && x2 != Some(pos.x2())) || (y1.is_some() && y1 != Some(pos.y1())) || (y2.is_some() && y2 != Some(pos.y2()))) && let Some(parent) = self.toplevel_data.parent.get() { parent.cnode_resize_child(&*self, x1, y1, x2, y2); } } fn cnode_pinned(&self) -> bool { self.tl_pinned() } fn cnode_set_pinned(self: Rc, pinned: bool) { self.tl_set_pinned(false, pinned); } fn cnode_get_float(self: Rc) -> Option> { self.tl_data().float.get() } fn cnode_self_or_ancestor_fullscreen(&self) -> bool { self.tl_data().self_or_ancestor_is_fullscreen.get() } } impl ToplevelNodeBase for ContainerNode { fn tl_data(&self) -> &ToplevelData { &self.toplevel_data } fn tl_set_workspace_ext(&self, ws: &Rc) { self.workspace.set(ws.clone()); self.location.set(ws.location()); for child in self.children.iter() { child.node.clone().tl_set_workspace(ws); } } fn tl_change_extents_impl(self: Rc, rect: &Rect) { self.toplevel_data.pos.set(*rect); self.abs_x1.set(rect.x1()); self.abs_y1.set(rect.y1()); let mut size_changed = false; size_changed |= self.width.replace(rect.width()) != rect.width(); size_changed |= self.height.replace(rect.height()) != rect.height(); if size_changed { self.update_content_size(); // Re-layout tab bar entries when container size changes in mono mode. if self.mono_child.is_some() { if let Some(bar) = self.tab_bar.borrow().as_ref() { let padding = self.state.theme.sizes.tab_bar_padding.get(); bar.layout_entries(rect.width(), padding); } self.schedule_update_tab_textures(); } // log::info!("tl_change_extents"); self.perform_layout(); self.cancel_seat_ops(); if let Some(parent) = self.toplevel_data.parent.get() { parent.node_child_size_changed(self.deref(), rect.width(), rect.height()); } } else { if let Some(c) = self.mono_child.get() { let body = self .mono_body .get() .move_(self.abs_x1.get(), self.abs_y1.get()); c.node.clone().tl_change_extents(&body); } else { for child in self.children.iter() { let body = child.body.get().move_(self.abs_x1.get(), self.abs_y1.get()); child.node.clone().tl_change_extents(&body); } } } } fn tl_close(self: Rc) { for child in self.children.iter() { child.node.clone().tl_close(); } } fn tl_set_visible_impl(&self, visible: bool) { if let Some(mc) = self.mono_child.get() { mc.node.tl_set_visible(visible); } else { for child in self.children.iter() { child.node.tl_set_visible(visible); } } } fn tl_destroy_impl(&self) { mem::take(self.cursors.borrow_mut().deref_mut()); let mut cn = self.child_nodes.borrow_mut(); for n in cn.drain_values() { n.node.tl_destroy(); } } fn tl_last_active_child(self: Rc) -> Rc { if let Some(last) = self.focus_history.last() { return last.node.clone().tl_last_active_child(); } self } fn tl_restack_popups(&self) { if let Some(mc) = self.mono_child.get() { mc.node.tl_restack_popups(); } else { for child in self.children.iter() { child.node.tl_restack_popups(); } } } fn tl_admits_children(&self) -> bool { true } fn tl_tile_drag_destination( self: Rc, source: NodeId, _split: Option, abs_bounds: Rect, abs_x: i32, abs_y: i32, ) -> Option { self.tile_drag_destination(source, abs_bounds, abs_x, abs_y) } fn tl_tile_drag_bounds(&self, split: ContainerSplit, start: bool) -> i32 { if split != self.split.get() { return default_tile_drag_bounds(self, split); } let child = match start { true => self.children.first(), false => self.children.last(), }; let Some(child) = child else { return 0; }; child.node.tl_tile_drag_bounds(split, start) / 2 } fn tl_push_float(&self, float: Option<&Rc>) { for child in self.children.iter() { child.node.tl_set_float(float); } } fn tl_mark_ancestor_fullscreen_ext(&self, fullscreen: bool) { for child in self.children.iter() { child.node.tl_mark_ancestor_fullscreen(fullscreen); } } }