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

@ -86,7 +86,7 @@ impl WlBuffer {
id,
destroyed: Cell::new(false),
client: client.clone(),
rect: Rect::new_sized(0, 0, width, height).unwrap(),
rect: Rect::new_sized_saturating(0, 0, width, height),
format,
width,
height,
@ -154,7 +154,7 @@ impl WlBuffer {
id,
destroyed: Cell::new(false),
client: client.clone(),
rect: Rect::new_sized(0, 0, width, height).unwrap(),
rect: Rect::new_sized_saturating(0, 0, width, height),
format,
dmabuf: None,
render_ctx_version: Cell::new(client.state.render_ctx_version.get()),
@ -183,7 +183,7 @@ impl WlBuffer {
id,
destroyed: Cell::new(false),
client: client.clone(),
rect: Rect::new_sized(0, 0, 1, 1).unwrap(),
rect: Rect::new_sized_saturating(0, 0, 1, 1),
format: ARGB8888,
dmabuf: None,
render_ctx_version: Cell::new(client.state.render_ctx_version.get()),

View file

@ -216,7 +216,7 @@ impl WlOutputGlobal {
name,
state: state.clone(),
connector: connector.clone(),
pos: Cell::new(Rect::new_sized(x, y, width, height).unwrap()),
pos: Cell::new(Rect::new_sized_saturating(x, y, width, height)),
output_id: output_id.clone(),
mode: Cell::new(connector_state.mode),
refresh_nsec: Cell::new(connector_state.mode.refresh_nsec()),
@ -343,7 +343,7 @@ impl WlOutputGlobal {
self.damage_matrix.set(matrix);
self.connector
.damage_intersect
.set(Rect::new_sized_unchecked(0, 0, mode.width, mode.height));
.set(Rect::new_sized_saturating(0, 0, mode.width, mode.height));
}
pub fn add_damage_area(&self, area: &Rect) {

View file

@ -47,7 +47,9 @@ impl WlRegionRequestHandler for WlRegion {
return Err(WlRegionError::NegativeExtents);
}
let mut region = self.region.borrow_mut();
region.add(Rect::new_sized(req.x, req.y, req.width, req.height).unwrap());
region.add(Rect::new_sized_saturating(
req.x, req.y, req.width, req.height,
));
Ok(())
}
@ -56,7 +58,9 @@ impl WlRegionRequestHandler for WlRegion {
return Err(WlRegionError::NegativeExtents);
}
let mut region = self.region.borrow_mut();
region.sub(Rect::new_sized(req.x, req.y, req.width, req.height).unwrap());
region.sub(Rect::new_sized_saturating(
req.x, req.y, req.width, req.height,
));
Ok(())
}
}

View file

@ -195,13 +195,12 @@ fn get_region(
.rects()
.iter()
.map(|r| {
Rect::new_sized(
Rect::new_sized_saturating(
r.x1() / scale,
r.y1() / scale,
r.width() / scale,
r.height() / scale,
)
.unwrap()
})
.collect();
region = Region::from_rects(&rects);

View file

@ -1352,7 +1352,7 @@ impl WlSurface {
if (width, height) != (old_width, old_height) {
self.need_extents_update.set(true);
self.buffer_abs_pos
.set(buffer_abs_pos.with_size(width, height).unwrap());
.set(buffer_abs_pos.with_size_saturating(width, height));
max_surface_size = (width.max(old_width), height.max(old_height));
damage_full = true;
buffer_abs_pos_size_changed = true;
@ -1426,9 +1426,8 @@ impl WlSurface {
self.commit_timeline.set_fifo_barrier();
}
if damage_full && (self.visible.get() || was_visible) {
let mut damage = buffer_abs_pos
.with_size(max_surface_size.0, max_surface_size.1)
.unwrap();
let mut damage =
buffer_abs_pos.with_size_saturating(max_surface_size.0, max_surface_size.1);
if let Some(tl) = self.toplevel.get() {
damage = damage.intersect(tl.node_absolute_position());
}
@ -1519,7 +1518,7 @@ impl WlSurface {
let y1 = damage.y1() / scale;
let x2 = (damage.x2() + scale - 1) / scale;
let y2 = (damage.y2() + scale - 1) / scale;
damage = Rect::new(x1, y1, x2, y2).unwrap();
damage = Rect::new_saturating(x1, y1, x2, y2);
}
damage = damage.intersect(bounds.unwrap_or(pos));
self.client.state.damage(damage);

View file

@ -123,13 +123,12 @@ impl Cursor for CursorSurface {
return rect;
}
let scale = scale.to_f64();
Rect::new(
Rect::new_saturating(
(rect.x1() as f64 * scale).ceil() as _,
(rect.y1() as f64 * scale).ceil() as _,
(rect.x2() as f64 * scale).ceil() as _,
(rect.y2() as f64 * scale).ceil() as _,
)
.unwrap()
}
fn set_output(&self, output: &Rc<OutputNode>) {

View file

@ -142,7 +142,7 @@ impl XwindowData {
let mut width = event.width as i32;
let mut height = event.height as i32;
client_wire_scale_to_logical!(client, x, y, width, height);
let extents = Rect::new_sized(x, y, width, height).unwrap();
let extents = Rect::new_sized_saturating(x, y, width, height);
// log::info!("xwin {} new {:?} or {}", event.window, extents, event.override_redirect);
Self {
state: state.clone(),

View file

@ -536,7 +536,7 @@ impl XdgSurfaceRequestHandler for XdgSurface {
if req.height <= 0 || req.width <= 0 {
return Err(XdgSurfaceError::NonPositiveWidthHeight);
}
let extents = Rect::new_sized(req.x, req.y, req.width, req.height).unwrap();
let extents = Rect::new_sized_saturating(req.x, req.y, req.width, req.height);
self.pending().geometry = Some(extents);
Ok(())
}

View file

@ -207,13 +207,12 @@ impl XdgPopup {
// use its position as is.
if let Some(maybe_abs_pos) = maybe_abs_pos {
abs_pos = maybe_abs_pos;
rel_pos = Rect::new_sized(
rel_pos = Rect::new_sized_saturating(
abs_pos.x1() - parent_abs.x1(),
abs_pos.y1() - parent_abs.y1(),
abs_pos.width(),
abs_pos.height(),
)
.unwrap();
);
}
}
}

View file

@ -514,7 +514,7 @@ impl ZwlrLayerSurfaceV1 {
} else if anchor.contains(BOTTOM) {
y1 = oheight - height - mb;
}
let a_rect = Rect::new_sized(x1 + rect.x1(), y1 + rect.y1(), width, height).unwrap();
let a_rect = Rect::new_sized_saturating(x1 + rect.x1(), y1 + rect.y1(), width, height);
let o_rect = a_rect.move_(-opos.x1(), -opos.y1());
self.output_extents.set(o_rect);
let a_rect_old = self.pos.replace(a_rect);

View file

@ -132,7 +132,7 @@ impl XdgPositioned {
y1 -= self.size_height / 2;
}
Rect::new_sized(x1, y1, self.size_width, self.size_height).unwrap()
Rect::new_sized_saturating(x1, y1, self.size_width, self.size_height)
}
}
@ -185,7 +185,7 @@ impl XdgPositionerRequestHandler for XdgPositioner {
return Err(XdgPositionerError::NegativeAnchorRect);
}
let mut position = self.position.borrow_mut();
position.ar = Rect::new_sized(req.x, req.y, req.width, req.height).unwrap();
position.ar = Rect::new_sized_saturating(req.x, req.y, req.width, req.height);
Ok(())
}

View file

@ -109,14 +109,14 @@ impl ZwlrScreencopyManagerV1 {
return Ok(());
};
let mode = global.mode.get();
let mut rect = Rect::new_sized(0, 0, mode.width, mode.height).unwrap();
let mut rect = Rect::new_sized_saturating(0, 0, mode.width, mode.height);
if let Some(region) = region {
let scale = global.persistent.scale.get().to_f64();
let x1 = (region.x1() as f64 * scale).round() as i32;
let y1 = (region.y1() as f64 * scale).round() as i32;
let x2 = (region.x2() as f64 * scale).round() as i32;
let y2 = (region.y2() as f64 * scale).round() as i32;
let region = Rect::new(x1, y1, x2, y2).unwrap();
let region = Rect::new_saturating(x1, y1, x2, y2);
rect = rect.intersect(region);
}
let frame = Rc::new(ZwlrScreencopyFrameV1 {