1
0
Fork 0
forked from wry/wry

rect: safer construction

This commit is contained in:
Stipe Kotarac 2025-12-29 10:42:17 +01:00 committed by Julian Orth
parent 411af0ea18
commit a1dfc473a2
33 changed files with 245 additions and 159 deletions

View file

@ -185,7 +185,7 @@ impl ContainerChild {
// let y1 = body.y1() + (body.height() - height) / 2;
let x1 = body.x1();
let y1 = body.y1();
content = Rect::new_sized(x1, y1, width, height).unwrap();
content = Rect::new_sized_saturating(x1, y1, width, height);
// log::debug!("body: {:?}", body);
// log::debug!("content: {:?}", content);
self.content.set(content);
@ -382,15 +382,12 @@ impl ContainerNode {
}
fn damage(&self) {
self.state.damage(
Rect::new_sized(
self.abs_x1.get(),
self.abs_y1.get(),
self.width.get(),
self.height.get(),
)
.unwrap(),
);
self.state.damage(Rect::new_sized_saturating(
self.abs_x1.get(),
self.abs_y1.get(),
self.width.get(),
self.height.get(),
));
}
fn schedule_layout(self: &Rc<Self>) {
@ -442,7 +439,7 @@ impl ContainerNode {
}
child
.title_rect
.set(Rect::new_sized(pos, 0, width, th).unwrap());
.set(Rect::new_sized_saturating(pos, 0, width, th));
pos += width + bw;
}
}
@ -483,7 +480,7 @@ impl ContainerNode {
body_size,
),
};
let body = Rect::new_sized(x1, y1, width, height).unwrap();
let body = Rect::new_sized_saturating(x1, y1, width, height);
child.body.set(body);
pos += body_size + border_width;
if split == ContainerSplit::Vertical {
@ -523,7 +520,7 @@ impl ContainerNode {
)
}
};
body = Rect::new_sized(x1, y1, width, height).unwrap();
body = Rect::new_sized_saturating(x1, y1, width, height);
child.body.set(body);
pos += size + border_width;
if split == ContainerSplit::Vertical {
@ -534,15 +531,12 @@ impl ContainerNode {
self.sum_factors.set(1.0);
for child in self.children.iter() {
let body = child.body.get();
child.title_rect.set(
Rect::new_sized(
body.x1(),
body.y1() - title_plus_underline_height,
body.width(),
title_height_tmp,
)
.unwrap(),
);
child.title_rect.set(Rect::new_sized_saturating(
body.x1(),
body.y1() - title_plus_underline_height,
body.width(),
title_height_tmp,
));
let body = body.move_(self.abs_x1.get(), self.abs_y1.get());
child.node.clone().tl_change_extents(&body);
child.position_content();
@ -573,15 +567,12 @@ impl ContainerNode {
self.content_width.set(self.width.get());
}
}
self.mono_body.set(
Rect::new_sized(
0,
title_plus_underline_height,
self.width.get(),
self.height.get().sub(title_plus_underline_height).max(0),
)
.unwrap(),
);
self.mono_body.set(Rect::new_sized_saturating(
0,
title_plus_underline_height,
self.width.get(),
self.height.get() - title_plus_underline_height,
));
}
fn pointer_move(
@ -837,7 +828,7 @@ impl ContainerNode {
for (i, child) in self.children.iter().enumerate() {
let rect = child.title_rect.get();
if self.toplevel_data.visible.get() && !mono && split != ContainerSplit::Horizontal {
self.state.damage(Rect::new_sized_unchecked(
self.state.damage(Rect::new_sized_saturating(
abs_x,
abs_y + rect.y1(),
cwidth,
@ -846,13 +837,13 @@ impl ContainerNode {
}
if i > 0 {
let rect = if mono {
Rect::new_sized(rect.x1() - bw, 0, bw, th)
Rect::new_sized_saturating(rect.x1() - bw, 0, bw, th)
} else if split == ContainerSplit::Horizontal {
Rect::new_sized(rect.x1() - bw, 0, bw, cheight)
Rect::new_sized_saturating(rect.x1() - bw, 0, bw, cheight)
} else {
Rect::new_sized(0, rect.y1() - bw, cwidth, bw)
Rect::new_sized_saturating(0, rect.y1() - bw, cwidth, bw)
};
rd.border_rects.push(rect.unwrap());
rd.border_rects.push(rect);
}
if child.active.get() {
rd.active_title_rects.push(rect);
@ -864,7 +855,7 @@ impl ContainerNode {
rd.title_rects.push(rect);
}
if !mono {
let rect = Rect::new_sized(rect.x1(), rect.y2(), rect.width(), 1).unwrap();
let rect = Rect::new_sized_saturating(rect.x1(), rect.y2(), rect.width(), 1);
rd.underline_rects.push(rect);
}
let tt = &*child.title_tex.borrow();
@ -877,11 +868,11 @@ impl ContainerNode {
}
if mono {
rd.underline_rects
.push(Rect::new_sized(0, th, cwidth, tuh).unwrap());
.push(Rect::new_sized_saturating(0, th, cwidth, tuh));
}
if self.toplevel_data.visible.get() && (mono || split == ContainerSplit::Horizontal) {
self.state
.damage(Rect::new_sized_unchecked(abs_x, abs_y, cwidth, tpuh));
.damage(Rect::new_sized_saturating(abs_x, abs_y, cwidth, tpuh));
}
rd.titles.remove_if(|_, v| v.is_empty());
}
@ -1192,7 +1183,7 @@ impl ContainerNode {
}
fn update_child_size(&self, node: &NodeRef<ContainerChild>, width: i32, height: i32) {
let rect = Rect::new(0, 0, width, height).unwrap();
let rect = Rect::new_saturating(0, 0, width, height);
node.content.set(rect);
node.position_content();
if let Some(mono) = self.mono_child.get()
@ -1633,13 +1624,12 @@ impl Node for ContainerNode {
}
fn node_absolute_position(&self) -> Rect {
Rect::new_sized(
Rect::new_sized_saturating(
self.abs_x1.get(),
self.abs_y1.get(),
self.width.get(),
self.height.get(),
)
.unwrap()
}
fn node_output(&self) -> Option<Rc<OutputNode>> {
@ -2381,14 +2371,14 @@ fn tile_drag_destination_in_mono(
split_before = false;
y1 = y2 - dy;
} else {
let rect = Rect::new_unchecked(x1, y1 + dy, x2, y2 - dy);
let rect = Rect::new_saturating(x1, y1 + dy, x2, y2 - dy);
return TileDragDestination {
highlight: rect,
ty: TddType::Replace(tl),
};
}
}
let rect = Rect::new_unchecked(x1, y1, x2, y2);
let rect = Rect::new_saturating(x1, y1, x2, y2);
TileDragDestination {
highlight: rect,
ty: TddType::Split {
@ -2434,7 +2424,7 @@ fn tile_drag_destination_in_split(
x1 = x2 - dx;
}
swap!();
let rect = Rect::new(x1, y1, x2, y2).unwrap();
let rect = Rect::new_saturating(x1, y1, x2, y2);
let ty = if split_before || split_after {
TddType::Split {
node: tl,

View file

@ -66,7 +66,7 @@ impl DisplayNode {
y1 = 0;
y2 = 0;
}
self.extents.set(Rect::new(x1, y1, x2, y2).unwrap());
self.extents.set(Rect::new_saturating(x1, y1, x2, y2));
}
pub fn update_visible(&self, state: &State) {

View file

@ -179,14 +179,13 @@ impl FloatNode {
let bw = theme.sizes.border_width.get();
let th = theme.title_height();
let tpuh = theme.title_plus_underline_height();
let cpos = Rect::new_sized(
let cpos = Rect::new_sized_saturating(
pos.x1() + bw,
pos.y1() + bw + tpuh,
(pos.width() - 2 * bw).max(0),
(pos.height() - 2 * bw - tpuh).max(0),
)
.unwrap();
let tr = Rect::new_sized(bw, bw, (pos.width() - 2 * bw).max(0), th).unwrap();
pos.width() - 2 * bw,
pos.height() - 2 * bw - tpuh,
);
let tr = Rect::new_sized_saturating(bw, bw, pos.width() - 2 * bw, th);
child.clone().tl_change_extents(&cpos);
self.title_rect.set(tr);
self.layout_scheduled.set(false);
@ -261,7 +260,7 @@ impl FloatNode {
let pos = self.position.get();
if self.visible.get() && pos.width() >= 2 * bw {
let tr =
Rect::new_sized(pos.x1() + bw, pos.y1() + bw, pos.width() - 2 * bw, th).unwrap();
Rect::new_sized_saturating(pos.x1() + bw, pos.y1() + bw, pos.width() - 2 * bw, th);
self.state.damage(tr);
}
}
@ -349,7 +348,7 @@ impl FloatNode {
y2 = y2.max(y1 + 2 * bw + tpuh);
}
}
let new_pos = Rect::new(x1, y1, x2, y2).unwrap();
let new_pos = Rect::new_saturating(x1, y1, x2, y2);
self.position.set(new_pos);
if self.visible.get() {
self.state.damage(pos);
@ -463,7 +462,7 @@ impl FloatNode {
y1 = opos.y1();
y2 += y1 - pos.y1();
}
let new_pos = Rect::new(x1, y1, x2, y2).unwrap();
let new_pos = Rect::new_saturating(x1, y1, x2, y2);
self.position.set(new_pos);
if self.visible.get() {
self.state.damage(pos);
@ -970,7 +969,7 @@ impl ContainingNode for FloatNode {
if let Some(v) = new_y2 {
y2 = (v + bw).max(y1 + bw + tpuh + bw);
}
let new_pos = Rect::new(x1, y1, x2, y2).unwrap();
let new_pos = Rect::new_saturating(x1, y1, x2, y2);
if new_pos != pos {
self.position.set(new_pos);
if self.visible.get() {

View file

@ -619,7 +619,7 @@ impl OutputNode {
});
}
}
let rect = Rect::new_sized(pos, y1, title_width, bar_rect_rel.height()).unwrap();
let rect = Rect::new_sized_saturating(pos, y1, title_width, bar_rect_rel.height());
if Some(ws.id) == active_id {
rd.active_workspace = Some(OutputWorkspaceRenderData {
rect,
@ -768,9 +768,9 @@ impl OutputNode {
let x1 = rect.x1() + exclusive.left;
let width = (x2 - x1).max(0);
let height = (y2 - y1).max(0);
let non_exclusive_rect = Rect::new_sized_unchecked(x1, y1, width, height);
let non_exclusive_rect = Rect::new_sized_saturating(x1, y1, width, height);
let non_exclusive_rect_rel =
Rect::new_sized_unchecked(exclusive.left, exclusive.top, width, height);
Rect::new_sized_saturating(exclusive.left, exclusive.top, width, height);
let mut bar_rect = Rect::default();
let mut bar_rect_rel = Rect::default();
let mut bar_rect_with_separator = Rect::default();
@ -781,24 +781,19 @@ impl OutputNode {
let bar_separator_rect;
match self.state.theme.bar_position.get() {
BarPosition::Bottom => {
workspace_rect =
Rect::new_sized_unchecked(x1, y1, width, (height - bh - bsw).max(0));
workspace_rect = Rect::new_sized_saturating(x1, y1, width, height - bh - bsw);
bar_rect_with_separator =
Rect::new_sized_unchecked(x1, y1 + height - bh - bsw, width, bh + bsw);
Rect::new_sized_saturating(x1, y1 + height - bh - bsw, width, bh + bsw);
bar_separator_rect =
Rect::new_sized_unchecked(x1, y1 + height - bh - bsw, width, bsw);
bar_rect = Rect::new_sized_unchecked(x1, y1 + height - bh, width, bh);
Rect::new_sized_saturating(x1, y1 + height - bh - bsw, width, bsw);
bar_rect = Rect::new_sized_saturating(x1, y1 + height - bh, width, bh);
}
BarPosition::Top | _ => {
bar_rect = Rect::new_sized_unchecked(x1, y1, width, bh);
bar_separator_rect = Rect::new_sized_unchecked(x1, y1 + bh, width, bsw);
bar_rect_with_separator = Rect::new_sized_unchecked(x1, y1, width, bh + bsw);
workspace_rect = Rect::new_sized_unchecked(
x1,
y1 + bh + bsw,
width,
(height - bh - bsw).max(0),
);
bar_rect = Rect::new_sized_saturating(x1, y1, width, bh);
bar_separator_rect = Rect::new_sized_saturating(x1, y1 + bh, width, bsw);
bar_rect_with_separator = Rect::new_sized_saturating(x1, y1, width, bh + bsw);
workspace_rect =
Rect::new_sized_saturating(x1, y1 + bh + bsw, width, height - bh - bsw);
}
}
bar_rect_rel = bar_rect.move_(-rect.x1(), -rect.y1());
@ -888,7 +883,7 @@ impl OutputNode {
pos: (i32, i32),
) -> Rect {
let (width, height) = calculate_logical_size((mode.width, mode.height), transform, scale);
Rect::new_sized(pos.0, pos.1, width, height).unwrap()
Rect::new_sized_saturating(pos.0, pos.1, width, height)
}
fn change_extents_(self: &Rc<Self>, rect: &Rect) {
@ -1420,7 +1415,7 @@ impl OutputNode {
}
have_any = true;
right -= bar_rect.height();
let rel_pos = Rect::new_sized(right, 1, icon_size, icon_size).unwrap();
let rel_pos = Rect::new_sized_saturating(right, 1, icon_size, icon_size);
let abs_pos = rel_pos.move_(bar_rect.x1(), bar_rect.y1());
item.set_position(abs_pos, rel_pos);
}
@ -1431,13 +1426,12 @@ impl OutputNode {
if prev_right != right {
{
let min = prev_right.min(right);
let rect = Rect::new(
let rect = Rect::new_saturating(
bar_rect.x1() + min,
bar_rect.y1(),
bar_rect.x2(),
bar_rect.y2(),
)
.unwrap();
);
self.state.damage(rect);
}
self.schedule_update_render_data();