use {
    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,
        scale::Scale,
        state::State,
        text::TextTexture,
        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,
            errorfmt::ErrorFmt,
            event_listener::LazyEventSource,
            hash_map_ext::HashMapExt,
            linkedlist::{LinkedList, LinkedNode, NodeRef},
            numcell::NumCell,
            on_drop_event::OnDropEvent,
            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, Sub},
        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<Axis> for ContainerSplit {
    fn from(a: Axis) -> Self {
        match a {
            Axis::Horizontal => Self::Horizontal,
            Axis::Vertical => Self::Vertical,
        }
    }
}

impl Into<Axis> 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<Rect>,
}

pub struct ContainerNode {
    pub id: ContainerNodeId,
    pub split: Cell<ContainerSplit>,
    pub mono_child: CloneCell<Option<NodeRef<ContainerChild>>>,
    pub mono_body: Cell<Rect>,
    pub mono_content: Cell<Rect>,
    pub abs_x1: Cell<i32>,
    pub abs_y1: Cell<i32>,
    pub width: Cell<i32>,
    pub height: Cell<i32>,
    pub content_width: Cell<i32>,
    pub content_height: Cell<i32>,
    pub sum_factors: Cell<f64>,
    pub layout_scheduled: Cell<bool>,
    compute_render_positions_scheduled: Cell<bool>,
    num_children: NumCell<usize>,
    pub children: LinkedList<ContainerChild>,
    focus_history: LinkedList<NodeRef<ContainerChild>>,
    child_nodes: RefCell<AHashMap<NodeId, LinkedNode<ContainerChild>>>,
    workspace: CloneCell<Rc<WorkspaceNode>>,
    location: Cell<NodeLocation>,
    cursors: RefCell<AHashMap<CursorType, CursorState>>,
    state: Rc<State>,
    pub render_data: RefCell<ContainerRenderData>,
    toplevel_data: ToplevelData,
    attention_requests: ThresholdCounter,
    pub layout_complete: Rc<LazyEventSource>,
    pub child_added: Rc<LazyEventSource>,
    pub child_removed: Rc<LazyEventSource>,
    pub all_children_resized: Rc<LazyEventSource>,
    pub tab_bar: RefCell<Option<TabBar>>,
    pub update_tab_textures_scheduled: Cell<bool>,
    pub ephemeral: Cell<Ephemeral>,
}

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<dyn ToplevelNode>,
    pub active: Cell<bool>,
    pub attention_requested: Cell<bool>,
    focus_history: Cell<Option<LinkedNode<NodeRef<ContainerChild>>>>,

    // fields below only valid in tabbed layout
    pub body: Cell<Rect>,
    pub content: Cell<Rect>,
    factor: Cell<f64>,
    pub border_color_is_focused: Cell<bool>,
}

#[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<SeatOp>,
}

impl ContainerChild {
    fn position_content(&self) {
        let mut content = self.content.get();
        let body = self.body.get();
        let width = content.width();
        let height = content.height();
        // let x1 = body.x1() + (body.width() - width) / 2;
        // let y1 = body.y1() + (body.height() - height) / 2;
        let x1 = body.x1();
        let y1 = body.y1();
        content = Rect::new_sized_saturating(x1, y1, width, height);
        // log::debug!("body: {:?}", body);
        // log::debug!("content: {:?}", content);
        self.content.set(content);
    }
}

impl ContainerNode {
    pub fn new(
        state: &Rc<State>,
        workspace: &Rc<WorkspaceNode>,
        child: Rc<dyn ToplevelNode>,
        split: ContainerSplit,
    ) -> Rc<Self> {
        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),
            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<Self>, new: Rc<dyn ToplevelNode>) {
        if let Some(child) = self.children.first() {
            self.add_child_before_(&child, new);
        }
    }

    pub fn append_child(self: &Rc<Self>, new: Rc<dyn ToplevelNode>) {
        if let Some(child) = self.children.last() {
            self.add_child_after_(&child, new);
        }
    }

    pub fn add_child_after(self: &Rc<Self>, prev: &dyn Node, new: Rc<dyn ToplevelNode>) {
        self.add_child_x(prev, new, |prev, new| self.add_child_after_(prev, new));
    }

    pub fn add_child_before(self: &Rc<Self>, prev: &dyn Node, new: Rc<dyn ToplevelNode>) {
        self.add_child_x(prev, new, |prev, new| self.add_child_before_(prev, new));
    }

    fn add_child_x<F>(self: &Rc<Self>, prev: &dyn Node, new: Rc<dyn ToplevelNode>, f: F)
    where
        F: FnOnce(&NodeRef<ContainerChild>, Rc<dyn ToplevelNode>),
    {
        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<Self>,
        prev: &NodeRef<ContainerChild>,
        new: Rc<dyn ToplevelNode>,
    ) {
        self.add_child(|cc| prev.append(cc), new);
    }

    fn add_child_before_(
        self: &Rc<Self>,
        prev: &NodeRef<ContainerChild>,
        new: Rc<dyn ToplevelNode>,
    ) {
        self.add_child(|cc| prev.prepend(cc), new);
    }

    fn add_child<F>(self: &Rc<Self>, f: F, new: Rc<dyn ToplevelNode>)
    where
        F: FnOnce(ContainerChild) -> LinkedNode<ContainerChild>,
    {
        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;
        }
    }

    pub fn predict_child_body_size(&self) -> (i32, i32) {
        if self.mono_child.is_some() {
            let mb = self.mono_body.get();
            return (mb.width(), mb.height());
        }
        let nc = self.num_children.get() as i32 + 1;
        match self.split.get() {
            ContainerSplit::Horizontal => {
                let spacing = self.child_spacing();
                let content_w = self.width.get().sub((nc - 1) * spacing).max(0);
                (content_w / nc, self.height.get())
            }
            ContainerSplit::Vertical => {
                let spacing = self.child_spacing();
                let content_h = self.height.get().sub((nc - 1) * spacing).max(0);
                (self.width.get(), content_h / nc)
            }
        }
    }

    pub fn on_spaces_changed(self: &Rc<Self>) {
        self.update_content_size();
        // log::info!("on_spaces_changed");
        self.schedule_layout();
        self.schedule_compute_render_positions();
    }

    pub fn on_colors_changed(self: &Rc<Self>) {
        self.schedule_compute_render_positions();
    }

    fn damage(&self) {
        let bw = if self.state.theme.sizes.gap.get() != 0 {
            self.state.theme.sizes.border_width.get()
        } else {
            0
        };
        self.state.damage(Rect::new_sized_saturating(
            self.abs_x1.get() - bw,
            self.abs_y1.get() - bw,
            self.width.get() + 2 * bw,
            self.height.get() + 2 * bw,
        ));
    }

    fn child_spacing(&self) -> i32 {
        let gap = self.state.theme.sizes.gap.get();
        let bw = self.state.theme.sizes.border_width.get();
        if gap == 0 { bw } else { gap + 2 * bw }
    }

    fn schedule_layout(self: &Rc<Self>) {
        if !self.layout_scheduled.replace(true) {
            self.state.pending_container_layout.push(self.clone());
        }
    }

    fn schedule_layout_immediate(self: &Rc<Self>) {
        self.schedule_layout();
        if self.toplevel_data.visible.get() {
            self.damage();
        }
    }

    fn all_children_match_body(&self) -> bool {
        if let Some(mono) = self.mono_child.get() {
            let body = self.mono_body.get();
            let content = mono.content.get();
            return content.width() == body.width() && content.height() == body.height();
        }
        for child in self.children.iter() {
            let body = child.body.get();
            let content = child.content.get();
            if content.width() != body.width() || content.height() != body.height() {
                return false;
            }
        }
        true
    }

    fn perform_layout(self: &Rc<Self>) {
        self.layout_scheduled.set(false);
        if self.num_children.get() == 0 {
            return;
        }
        if let Some(child) = self.mono_child.get() {
            self.perform_mono_layout(&child);
        } else {
            self.perform_split_layout();
        }
        self.state.tree_changed();
        // log::info!("perform_layout");
        self.schedule_compute_render_positions();
        self.layout_complete.trigger();
        if self.all_children_match_body() {
            self.all_children_resized.trigger();
            if self.toplevel_data.visible.get() {
                self.damage();
            }
        }
    }

    fn perform_mono_layout(self: &Rc<Self>, child: &ContainerChild) {
        let mb = self.mono_body.get();
        child
            .node
            .clone()
            .tl_change_extents(&mb.move_(self.abs_x1.get(), self.abs_y1.get()));
        self.mono_content
            .set(child.content.get().at_point(mb.x1(), mb.y1()));
    }

    fn perform_split_layout(self: &Rc<Self>) {
        let sum_factors = self.sum_factors.get();
        let split = self.split.get();
        let spacing = self.child_spacing();
        let (content_size, other_content_size) = match split {
            ContainerSplit::Horizontal => (self.content_width.get(), self.content_height.get()),
            ContainerSplit::Vertical => (self.content_height.get(), self.content_width.get()),
        };
        let num_children = self.num_children.get();
        if num_children == 0 {
            return;
        }
        let mut pos = 0;
        let mut remaining_content_size = content_size;
        for child in self.children.iter() {
            let factor = child.factor.get() / sum_factors;
            child.factor.set(factor);
            let mut body_size = (content_size as f64 * factor).round() as i32;
            body_size = body_size.min(remaining_content_size);
            remaining_content_size -= body_size;
            let (x1, y1, width, height) = match split {
                ContainerSplit::Horizontal => (pos, 0, body_size, other_content_size),
                _ => (0, pos, other_content_size, body_size),
            };
            let body = Rect::new_sized_saturating(x1, y1, width, height);
            child.body.set(body);
            pos += body_size + spacing;
        }
        if remaining_content_size > 0 {
            let size_per = remaining_content_size / num_children as i32;
            let mut rem = remaining_content_size % num_children as i32;
            pos = 0;
            for child in self.children.iter() {
                let mut body = child.body.get();
                let mut add = size_per;
                if rem > 0 {
                    rem -= 1;
                    add += 1;
                }
                let (x1, y1, width, height, size) = match split {
                    ContainerSplit::Horizontal => {
                        let width = body.width() + add;
                        (pos, 0, width, other_content_size, width)
                    }
                    _ => {
                        let height = body.height() + add;
                        (0, pos, other_content_size, height, height)
                    }
                };
                body = Rect::new_sized_saturating(x1, y1, width, height);
                child.body.set(body);
                pos += size + spacing;
            }
        }
        self.sum_factors.set(1.0);
        for child in self.children.iter() {
            let body = child.body.get();
            let body = body.move_(self.abs_x1.get(), self.abs_y1.get());
            child.node.clone().tl_change_extents(&body);
            child.position_content();
        }
    }

    fn update_content_size(&self) {
        let nc = self.num_children.get();
        let spacing = self.child_spacing();
        match self.split.get() {
            ContainerSplit::Horizontal => {
                let new_content_size = self.width.get().sub((nc - 1) as i32 * spacing).max(0);
                self.content_width.set(new_content_size);
                self.content_height.set(self.height.get());
            }
            ContainerSplit::Vertical => {
                let new_content_size = self.height.get().sub((nc - 1) as i32 * spacing).max(0);
                self.content_height.set(new_content_size);
                self.content_width.set(self.width.get());
            }
        }
        let tab_bar_height = if self.mono_child.is_some() {
            // Tab bar sits above the window with a configurable gap.
            let tbh = self.state.theme.sizes.tab_bar_height.get();
            let gap = self.state.theme.sizes.tab_bar_gap.get();
            tbh + gap
        } else {
            0
        };
        self.mono_body.set(Rect::new_sized_saturating(
            0,
            tab_bar_height,
            self.width.get(),
            (self.height.get() - tab_bar_height).max(0),
        ));
    }

    fn pointer_move(
        self: &Rc<Self>,
        _seat: &Rc<WlSeatGlobal>,
        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.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<Self>) {
        if !self.compute_render_positions_scheduled.replace(true) {
            self.state
                .pending_container_render_positions
                .push(self.clone());
        }
    }

    fn schedule_update_tab_textures(self: &Rc<Self>) {
        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<Self>, child: &NodeRef<ContainerChild>) {
        self.activate_child2(child, false);
    }

    fn activate_child2(self: &Rc<Self>, child: &NodeRef<ContainerChild>, 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<Self>, 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());
        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<Self>, 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>) {
        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<NodeId>,
        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<Self>,
        override_id: Option<NodeId>,
        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 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);
            bar.entries.push(TabBarEntry {
                child_id,
                title,
                title_texture: Rc::new(RefCell::new(None)),
                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<Self>, split: ContainerSplit, ephemeral: bool) {
        let Some(focused) = self.focus_history.last() else {
            return;
        };
        if self.num_children.get() <= 1 {
            return;
        }
        let focused_node = focused.node.clone();
        // Record the sibling that comes AFTER the focused child so we can
        // insert the new group at the same position.
        let next_sibling: Option<Rc<dyn ToplevelNode>> = {
            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<Self>, 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<Self>) {
        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<Self>) {
        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<Self>) {
        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>) {
        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<Self>, 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<Rc<ContainerNode>> {
        self.toplevel_data
            .parent
            .get()
            .and_then(|p| p.node_into_container())
    }

    fn find_neighboring_output(&self, direction: Direction) -> Option<Rc<OutputNode>> {
        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<Self>,
        seat: &Rc<WlSeatGlobal>,
        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<Self>, child: Rc<dyn ToplevelNode>, direction: Direction) {
        let move_to_neighboring_output = |child: Rc<dyn ToplevelNode>| {
            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<Self>, node: Rc<dyn ToplevelNode>, 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<Self>,
        node: Rc<dyn ToplevelNode>,
        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<Self>,
        node: &NodeRef<ContainerChild>,
        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<ContainerChild>, 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<Self>, child: &NodeRef<ContainerChild>) {
        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<Self>) {
        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<Self>,
        id: CursorType,
        _seat: &Rc<WlSeatGlobal>,
        _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<Self>,
        mc: &ContainerChild,
        source: NodeId,
        abs_bounds: Rect,
        abs_x: i32,
        abs_y: i32,
    ) -> Option<TileDragDestination> {
        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<Self>,
        source: NodeId,
        abs_bounds: Rect,
        abs_x: i32,
        abs_y: i32,
    ) -> Option<TileDragDestination> {
        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<ContainerChild>,
    kind: SeatOpKind,
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
enum SeatOpKind {
    Resize { dist_left: i32, dist_right: i32 },
}

pub async fn container_layout(state: Rc<State>) {
    loop {
        let container = state.pending_container_layout.pop().await;
        if container.layout_scheduled.get() {
            container.perform_layout();
        }
    }
}

pub async fn container_render_positions(state: Rc<State>) {
    loop {
        let container = state.pending_container_render_positions.pop().await;
        if container.compute_render_positions_scheduled.get() {
            container.compute_render_positions();
        }
    }
}

pub async fn container_tab_render_textures(state: Rc<State>) {
    loop {
        let container = state.pending_container_tab_render_textures.pop().await;
        container.update_tab_textures_scheduled.set(false);
        let (event, textures) = container.update_tab_textures_phase1();
        event.triggered().await;
        container.update_tab_textures_phase2(&textures);
    }
}

impl ContainerNode {
    fn update_tab_textures_phase1(
        self: &Rc<Self>,
    ) -> (
        Rc<crate::utils::asyncevent::AsyncEvent>,
        Vec<Rc<RefCell<Option<TextTexture>>>>,
    ) {
        let on_completed = Rc::new(OnDropEvent::default());
        let (entries, bar_height, render_scale) = {
            let tab_bar = self.tab_bar.borrow();
            let Some(tb) = tab_bar.as_ref() else {
                return (on_completed.event(), vec![]);
            };
            let entries: Vec<_> = tb
                .entries
                .iter()
                .map(|e| {
                    (
                        e.title.clone(),
                        TabBar::entry_colors(&self.state, e),
                        e.title_texture.clone(),
                    )
                })
                .collect();
            (entries, tb.height, tb.render_scale)
        };
        let Some(ctx) = self.state.render_ctx.get() else {
            log::warn!("tab text phase1: no render context");
            return (on_completed.event(), vec![]);
        };
        let font = self.state.theme.title_font();
        let scale = if render_scale != Scale::from_int(1) {
            Some(render_scale.to_f64())
        } else {
            None
        };
        let mut texture_height = bar_height;
        if let Some(s) = scale {
            texture_height = (bar_height as f64 * s).round() as _;
        }
        let mut texture_refs = Vec::new();
        for (title, (_, _, text_color), title_texture) in &entries {
            let mut tex_ref = title_texture.borrow_mut();
            let tex = tex_ref.get_or_insert_with(|| TextTexture::new(&self.state, &ctx));
            tex.schedule_render_fitting(
                on_completed.clone(),
                Some(texture_height),
                &font,
                title,
                *text_color,
                false,
                scale,
            );
            texture_refs.push(title_texture.clone());
        }
        (on_completed.event(), texture_refs)
    }

    fn update_tab_textures_phase2(&self, textures: &[Rc<RefCell<Option<TextTexture>>>]) {
        for title_texture in textures {
            let tex_ref = title_texture.borrow();
            if let Some(tex) = tex_ref.as_ref() {
                if let Err(e) = tex.flip() {
                    log::warn!("Could not render tab text: {}", ErrorFmt(e));
                }
            }
        }
        self.damage();
    }
}

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<Self>, 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<Rc<OutputNode>> {
        self.toplevel_data.output_opt()
    }

    fn node_location(&self) -> Option<NodeLocation> {
        Some(self.location.get())
    }

    fn node_layer(&self) -> NodeLayerLink {
        self.toplevel_data.node_layer()
    }

    fn node_child_title_changed(self: Rc<Self>, 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<Self>, seat: &Rc<WlSeatGlobal>, 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<FoundNode>,
        usecase: FindTreeUsecase,
    ) -> FindTreeResult {
        let mut recurse = |content: Rect, child: NodeRef<ContainerChild>| {
            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<Self>, 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<Self>) -> Option<Rc<dyn crate::tree::ToplevelNode>> {
        Some(self)
    }

    fn node_make_visible(self: Rc<Self>) {
        self.toplevel_data.make_visible(&*self);
    }

    fn node_on_button(
        self: Rc<Self>,
        seat: &Rc<WlSeatGlobal>,
        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<Self>, _seat: &Rc<WlSeatGlobal>, 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<Self>, seat: &Rc<WlSeatGlobal>, 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<WlSeatGlobal>) {
        // 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<WlSeatGlobal>) {
        // 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<Self>, seat: &Rc<WlSeatGlobal>, 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<TabletTool>, _time_usec: u64) {
        let id = CursorType::TabletTool(tool.id);
        self.cursors.borrow_mut().remove(&id);
    }

    fn node_on_tablet_tool_enter(
        self: Rc<Self>,
        tool: &Rc<TabletTool>,
        _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<Self>,
        tool: &Rc<TabletTool>,
        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<Self>) -> Option<Rc<ContainerNode>> {
        Some(self.clone())
    }

    fn node_into_containing_node(self: Rc<Self>) -> Option<Rc<dyn ContainingNode>> {
        Some(self)
    }

    fn node_into_toplevel(self: Rc<Self>) -> Option<Rc<dyn ToplevelNode>> {
        Some(self)
    }

    fn node_is_container(&self) -> bool {
        true
    }
}

impl ContainingNode for ContainerNode {
    fn cnode_replace_child(self: Rc<Self>, old: &dyn Node, new: Rc<dyn ToplevelNode>) {
        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<Self>, 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();
        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<Self>, 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<Self>) -> Rc<WorkspaceNode> {
        self.workspace.get()
    }

    fn cnode_make_visible(self: Rc<Self>, 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<Self>, 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<Self>,
        child: &dyn Node,
        new_x1: Option<i32>,
        mut new_y1: Option<i32>,
        new_x2: Option<i32>,
        new_y2: Option<i32>,
    ) {
        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<Self>, pinned: bool) {
        self.tl_set_pinned(false, pinned);
    }

    fn cnode_get_float(self: Rc<Self>) -> Option<Rc<FloatNode>> {
        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<WorkspaceNode>) {
        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<Self>, 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);
                }
            }
            // 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<Self>) {
        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<Self>) -> Rc<dyn ToplevelNode> {
        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<Self>,
        source: NodeId,
        _split: Option<ContainerSplit>,
        abs_bounds: Rect,
        abs_x: i32,
        abs_y: i32,
    ) -> Option<TileDragDestination> {
        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<FloatNode>>) {
        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);
        }
    }
}

fn direction_to_split(dir: Direction) -> (ContainerSplit, bool) {
    match dir {
        Direction::Left => (ContainerSplit::Horizontal, true),
        Direction::Down => (ContainerSplit::Vertical, false),
        Direction::Up => (ContainerSplit::Vertical, true),
        Direction::Right => (ContainerSplit::Horizontal, false),
        Direction::Unspecified => (ContainerSplit::Horizontal, true),
    }
}

fn tile_drag_destination_in_mono(
    tl: Rc<dyn ToplevelNode>,
    abs_bounds: Rect,
    abs_x: i32,
    abs_y: i32,
) -> TileDragDestination {
    let mut x1 = abs_bounds.x1();
    let mut x2 = abs_bounds.x2();
    let mut y1 = abs_bounds.y1();
    let mut y2 = abs_bounds.y2();
    let dx = (x2 - x1) / 3;
    let dy = (y2 - y1) / 3;
    let mut split_before = true;
    let mut split = ContainerSplit::Horizontal;
    if abs_x < x1 + dx {
        x2 = x1 + dx;
    } else if abs_x > x2 - dx {
        split_before = false;
        x1 = x2 - dx;
    } else {
        split = ContainerSplit::Vertical;
        x1 += dx;
        x2 -= dx;
        if abs_y < y1 + dy {
            y2 = y1 + dy;
        } else if abs_y > y2 - dy {
            split_before = false;
            y1 = y2 - dy;
        } else {
            let rect = Rect::new_saturating(x1, y1 + dy, x2, y2 - dy);
            return TileDragDestination {
                highlight: rect,
                ty: TddType::Replace(tl),
            };
        }
    }
    let rect = Rect::new_saturating(x1, y1, x2, y2);
    TileDragDestination {
        highlight: rect,
        ty: TddType::Split {
            node: tl,
            split,
            before: split_before,
        },
    }
}

fn tile_drag_destination_in_split(
    tl: Rc<dyn ToplevelNode>,
    split: ContainerSplit,
    abs_bounds: Rect,
    mut abs_x: i32,
    mut abs_y: i32,
) -> TileDragDestination {
    let mut x1 = abs_bounds.x1();
    let mut x2 = abs_bounds.x2();
    let mut y1 = abs_bounds.y1();
    let mut y2 = abs_bounds.y2();
    macro_rules! swap {
        () => {
            if split == ContainerSplit::Horizontal {
                mem::swap(&mut x1, &mut y1);
                mem::swap(&mut x2, &mut y2);
                mem::swap(&mut abs_x, &mut abs_y);
            }
        };
    }
    swap!();
    let mut split_before = false;
    let mut split_after = false;
    let dx = (x2 - x1) / 3;
    if abs_x < x1 + dx {
        split_before = true;
        x2 = x1 + dx;
    } else if abs_x < x2 - dx {
        x1 += dx;
        x2 -= dx;
    } else {
        split_after = true;
        x1 = x2 - dx;
    }
    swap!();
    let rect = Rect::new_saturating(x1, y1, x2, y2);
    let ty = if split_before || split_after {
        TddType::Split {
            node: tl,
            split: split.other(),
            before: split_before,
        }
    } else {
        TddType::Replace(tl)
    };
    TileDragDestination {
        highlight: rect,
        ty,
    }
}

pub fn default_tile_drag_destination(
    tl: Rc<dyn ToplevelNode>,
    source: NodeId,
    split: Option<ContainerSplit>,
    abs_bounds: Rect,
    abs_x: i32,
    abs_y: i32,
) -> Option<TileDragDestination> {
    if tl.node_id() == source {
        return None;
    }
    Some(match split {
        None => tile_drag_destination_in_mono(tl, abs_bounds, abs_x, abs_y),
        Some(s) => tile_drag_destination_in_split(tl, s, abs_bounds, abs_x, abs_y),
    })
}
