use super::Tree;
use crate::{
app::window::controls::{Bounds, WindowControlRegion, WindowControlRegions},
layout::{
layout_kind::{LayoutKind, Overflow},
layout_result::LayoutResult,
},
node::{HitTestBehavior, Node, WindowControl, window_controls_in_use},
};
impl Tree {
pub fn window_control_at(&self, x: f32, y: f32) -> Option<WindowControl> {
let scale = self.scale_factor();
self.window_control_regions().at(x / scale, y / scale)
}
#[cfg_attr(not(all(feature = "winit", any(windows, target_os = "macos"))), allow(dead_code))]
pub(crate) fn publish_window_controls(&mut self) {
self.publishes_window_controls = true;
self.leave_window_controls();
}
pub(super) fn leave_window_controls(&self) {
if self.publishes_window_controls {
self.window.set_control_regions(self.window_control_regions());
}
}
fn window_control_regions(&self) -> WindowControlRegions {
let mut regions = WindowControlRegions::default();
if !window_controls_in_use() {
return regions;
}
if let (Some(root), Some(layout)) = (&self.root, &self.last_layout) {
find(
root,
layout,
0.0,
0.0,
Bounds::EVERYWHERE,
&mut Vec::new(),
&mut regions,
);
}
regions
}
}
struct Step<'a> {
parent: &'a Node,
layout: &'a LayoutResult,
x: f32,
y: f32,
clip: Bounds,
child: usize,
}
fn takes_no_pointer(node: &Node, layout: &LayoutResult) -> bool {
node.hit_test_behavior() == HitTestBehavior::None || layout.dropped || node.is_inert()
}
fn children_clip(node: &Node, bounds: Bounds, clip: Bounds) -> Bounds {
if node.overflow == Overflow::Visible {
clip
} else {
clip.intersect(bounds)
}
}
fn find<'a>(
node: &'a Node,
layout: &'a LayoutResult,
x: f32,
y: f32,
clip: Bounds,
path: &mut Vec<Step<'a>>,
regions: &mut WindowControlRegions,
) {
if takes_no_pointer(node, layout) || node.is_disabled() {
return;
}
let bounds = Bounds::new(x, y, layout.size.width, layout.size.height);
if let Some(control) = node.window_control()
&& node.hit_test_behavior() == HitTestBehavior::Auto
{
let seen = bounds.intersect(clip);
if !seen.is_empty() {
let covers = covers(path, seen);
if !covers.iter().any(|cover| cover.holds(seen)) {
regions.push(WindowControlRegion {
control,
bounds: seen,
covers,
});
}
}
}
let clip = children_clip(node, bounds, clip);
if clip.is_empty() {
return;
}
for (child, (child_layout, child_node)) in layout.children.iter().zip(node.children()).enumerate() {
path.push(Step {
parent: node,
layout,
x,
y,
clip,
child,
});
find(
child_node,
&child_layout.result,
x + child_layout.offset.x,
y + child_layout.offset.y,
clip,
path,
regions,
);
path.pop();
}
}
fn covers(path: &[Step<'_>], within: Bounds) -> Vec<Bounds> {
let mut covers = Vec::new();
for step in path {
let children = step.parent.children();
let Some(own) = children.get(step.child) else {
continue;
};
let stack = matches!(step.parent.layout_kind(), LayoutKind::Stack { .. });
for (index, (child_layout, child_node)) in step.layout.children.iter().zip(children).enumerate() {
if index == step.child {
continue;
}
let before = if own.is_sticky() {
child_node.is_sticky() && index > step.child
} else {
child_node.is_sticky() || (stack && index > step.child)
};
if before {
boxes(
child_node,
&child_layout.result,
step.x + child_layout.offset.x,
step.y + child_layout.offset.y,
step.clip,
within,
&mut covers,
);
}
}
}
covers
}
fn boxes(node: &Node, layout: &LayoutResult, x: f32, y: f32, clip: Bounds, within: Bounds, out: &mut Vec<Bounds>) {
if takes_no_pointer(node, layout) {
return;
}
let bounds = Bounds::new(x, y, layout.size.width, layout.size.height);
let seen = bounds.intersect(clip).intersect(within);
if node.hit_test_behavior() == HitTestBehavior::Auto && !seen.is_empty() {
out.push(seen);
if node.overflow != Overflow::Visible {
return;
}
}
let clip = children_clip(node, bounds, clip);
if clip.intersect(within).is_empty() {
return;
}
for (child_layout, child_node) in layout.children.iter().zip(node.children()) {
boxes(
child_node,
&child_layout.result,
x + child_layout.offset.x,
y + child_layout.offset.y,
clip,
within,
out,
);
}
}