Skip to content

Commit

Permalink
utils/geometry: Deprecate ::from_loc_and_size in favor of a ::new
Browse files Browse the repository at this point in the history
This new function is the same as `from_loc_and_size`, except it takes
concrete types as arguments instead of `impl Into`.

This matches `euclid::Rect`'s API, but it also seems a little better to
have strong typing without implicit conversion like this. In a few cases
it's not obvious that the argument is variable of tuple type, so there
is an implied assertion that the coordinate space is right.

The use of `#[deprecated]` makes this not entirely urgent for
compositors, but uses of this should be updated to `::new`, or other
functions like `from_size`.
  • Loading branch information
ids1024 committed Dec 27, 2024
1 parent f1fde5a commit ef1a272
Show file tree
Hide file tree
Showing 31 changed files with 143 additions and 140 deletions.
20 changes: 10 additions & 10 deletions anvil/src/drawing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ where
for digit in value_str.chars().map(|d| d.to_digit(10).unwrap()) {
let digit_location = dst.loc.to_f64() + offset;
let digit_size = Size::<i32, Logical>::from((22, 35)).to_f64().to_physical(scale);
let dst = Rectangle::from_loc_and_size(
let dst = Rectangle::new(
digit_location.to_i32_round(),
((digit_size.to_point() + digit_location).to_i32_round() - digit_location.to_i32_round())
.to_size(),
Expand All @@ -227,15 +227,15 @@ where
.collect::<Vec<_>>();
let texture_src: Rectangle<i32, Buffer> = match digit {
9 => Rectangle::from_size((22, 35).into()),
6 => Rectangle::from_loc_and_size((22, 0), (22, 35)),
3 => Rectangle::from_loc_and_size((44, 0), (22, 35)),
1 => Rectangle::from_loc_and_size((66, 0), (22, 35)),
8 => Rectangle::from_loc_and_size((0, 35), (22, 35)),
0 => Rectangle::from_loc_and_size((22, 35), (22, 35)),
2 => Rectangle::from_loc_and_size((44, 35), (22, 35)),
7 => Rectangle::from_loc_and_size((0, 70), (22, 35)),
4 => Rectangle::from_loc_and_size((22, 70), (22, 35)),
5 => Rectangle::from_loc_and_size((44, 70), (22, 35)),
6 => Rectangle::new((22, 0), (22, 35)),
3 => Rectangle::new((44, 0), (22, 35)),
1 => Rectangle::new((66, 0), (22, 35)),
8 => Rectangle::new((0, 35), (22, 35)),
0 => Rectangle::new((22, 35), (22, 35)),
2 => Rectangle::new((44, 35), (22, 35)),
7 => Rectangle::new((0, 70), (22, 35)),
4 => Rectangle::new((22, 70), (22, 35)),
5 => Rectangle::new((44, 70), (22, 35)),
_ => unreachable!(),
};

Expand Down
2 changes: 1 addition & 1 deletion anvil/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ where
preview_padding + (preview_padding + preview_size.w) * column as i32,
preview_padding + (preview_padding + preview_size.h) * row as i32,
));
let constrain = Rectangle::from_loc_and_size(preview_location, preview_size);
let constrain = Rectangle::new(preview_location, preview_size);
constrain_space_element(
renderer,
window,
Expand Down
8 changes: 4 additions & 4 deletions anvil/src/shell/grabs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ impl<BackendData: Backend> PointerGrab<AnvilState<BackendData>> for PointerResiz
#[cfg(feature = "xwayland")]
WindowSurface::X11(x11) => {
let location = data.space.element_location(&self.window).unwrap();
x11.configure(Rectangle::from_loc_and_size(location, self.last_window_size))
x11.configure(Rectangle::new(location, self.last_window_size))
.unwrap();
}
}
Expand Down Expand Up @@ -505,7 +505,7 @@ impl<BackendData: Backend> PointerGrab<AnvilState<BackendData>> for PointerResiz

data.space.map_element(self.window.clone(), location, true);
}
x11.configure(Rectangle::from_loc_and_size(location, self.last_window_size))
x11.configure(Rectangle::new(location, self.last_window_size))
.unwrap();

let Some(surface) = self.window.wl_surface() else {
Expand Down Expand Up @@ -718,7 +718,7 @@ impl<BackendData: Backend> TouchGrab<AnvilState<BackendData>> for TouchResizeSur

data.space.map_element(self.window.clone(), location, true);
}
x11.configure(Rectangle::from_loc_and_size(location, self.last_window_size))
x11.configure(Rectangle::new(location, self.last_window_size))
.unwrap();

let Some(surface) = self.window.wl_surface() else {
Expand Down Expand Up @@ -817,7 +817,7 @@ impl<BackendData: Backend> TouchGrab<AnvilState<BackendData>> for TouchResizeSur
#[cfg(feature = "xwayland")]
WindowSurface::X11(x11) => {
let location = data.space.element_location(&self.window).unwrap();
x11.configure(Rectangle::from_loc_and_size(location, self.last_window_size))
x11.configure(Rectangle::new(location, self.last_window_size))
.unwrap();
}
}
Expand Down
4 changes: 2 additions & 2 deletions anvil/src/shell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ fn place_new_window(
let geo = space.output_geometry(&o)?;
let map = layer_map_for_output(&o);
let zone = map.non_exclusive_zone();
Some(Rectangle::from_loc_and_size(geo.loc + zone.loc, zone.size))
Some(Rectangle::new(geo.loc + zone.loc, zone.size))
})
.unwrap_or_else(|| Rectangle::from_size((800, 800).into()));

Expand Down Expand Up @@ -455,7 +455,7 @@ pub fn fixup_positions(space: &mut Space<WindowElement>, pointer_location: Point
let geo = space.output_geometry(o)?;
let map = layer_map_for_output(o);
let zone = map.non_exclusive_zone();
Some(Rectangle::from_loc_and_size(geo.loc + zone.loc, zone.size))
Some(Rectangle::new(geo.loc + zone.loc, zone.size))
})
.collect::<Vec<_>>();
for window in space.elements() {
Expand Down
10 changes: 2 additions & 8 deletions anvil/src/shell/x11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,10 +367,7 @@ impl<BackendData: Backend> AnvilState<BackendData> {
.and_then(|data| data.restore())
{
window
.configure(Rectangle::from_loc_and_size(
initial_window_location,
old_geo.size,
))
.configure(Rectangle::new(initial_window_location, old_geo.size))
.unwrap();
}
}
Expand Down Expand Up @@ -413,10 +410,7 @@ impl<BackendData: Backend> AnvilState<BackendData> {
.and_then(|data| data.restore())
{
window
.configure(Rectangle::from_loc_and_size(
initial_window_location,
old_geo.size,
))
.configure(Rectangle::new(initial_window_location, old_geo.size))
.unwrap();
}
}
Expand Down
4 changes: 2 additions & 2 deletions benches/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn criterion_benchmark(c: &mut Criterion) {
let mut rand = rand::thread_rng();
let x = rand.gen_range(0..max_x);
let y = rand.gen_range(0..max_y);
let test_element = Rectangle::from_loc_and_size((x, y), element_size);
let test_element = Rectangle::new((x, y).into(), element_size);

let x_min = (test_element.loc.x - element_size.w) + 1;
let x_max = (test_element.loc.x + element_size.w) - 1;
Expand All @@ -35,7 +35,7 @@ fn criterion_benchmark(c: &mut Criterion) {
.map(|_| {
let x = rand.gen_range(x_min..=x_max);
let y = rand.gen_range(y_min..=y_max);
Rectangle::from_loc_and_size((x, y), element_size)
Rectangle::new((x, y).into(), element_size)
})
.collect::<Vec<_>>();

Expand Down
6 changes: 3 additions & 3 deletions examples/buffer_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,19 +297,19 @@ where
frame
.clear(
Color32F::new(0.0, 1.0, 0.0, 1.0),
&[Rectangle::from_loc_and_size((w / 2, 0), (w / 2, h / 2))],
&[Rectangle::new((w / 2, 0).into(), (w / 2, h / 2).into())],
)
.expect("Render error");
frame
.clear(
Color32F::new(0.0, 0.0, 1.0, 1.0),
&[Rectangle::from_loc_and_size((0, h / 2), (w / 2, h / 2))],
&[Rectangle::new((0, h / 2).into(), (w / 2, h / 2).into())],
)
.expect("Render error");
frame
.clear(
Color32F::new(1.0, 1.0, 0.0, 1.0),
&[Rectangle::from_loc_and_size((w / 2, h / 2), (w / 2, h / 2))],
&[Rectangle::new((w / 2, h / 2).into(), (w / 2, h / 2).into())],
)
.expect("Render error");
frame
Expand Down
2 changes: 1 addition & 1 deletion smallvil/src/handlers/xdg_shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl XdgShellHandler for Smallvil {
start_data,
window,
edges.into(),
Rectangle::from_loc_and_size(initial_window_location, initial_window_size),
Rectangle::new(initial_window_location, initial_window_size),
);

pointer.set_grab(self, grab, serial, Focus::Clear);
Expand Down
2 changes: 1 addition & 1 deletion src/backend/drm/compositor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3533,7 +3533,7 @@ where
};

let src = Rectangle::from_size(cursor_buffer_size).to_f64();
let dst = Rectangle::from_loc_and_size(cursor_plane_location, cursor_plane_size);
let dst = Rectangle::new(cursor_plane_location, cursor_plane_size);

let config = PlaneConfig {
properties: PlaneProperties {
Expand Down
20 changes: 10 additions & 10 deletions src/backend/renderer/damage/shaper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl<const MIN_TILE_SIDE: i32> DamageShaper<MIN_TILE_SIDE> {
let bbox_w = x_max - x_min;
let bbox_h = y_max - y_min;

let damage_bbox = Rectangle::<i32, Physical>::from_loc_and_size((x_min, y_min), (bbox_w, bbox_h));
let damage_bbox = Rectangle::<i32, Physical>::new((x_min, y_min).into(), (bbox_w, bbox_h).into());

// Damage the current bounding box when there's a damage rect covering near all the area.
if max_damage_area as f32 / (damage_bbox.size.w * damage_bbox.size.h) as f32
Expand Down Expand Up @@ -200,7 +200,7 @@ impl<const MIN_TILE_SIDE: i32> DamageShaper<MIN_TILE_SIDE> {
for y in (bbox.loc.y..bbox.loc.y + bbox.size.h).step_by(tile_size.h as usize) {
// NOTE the in_damage is constrained to the `bbox`, so it can't go outside
// the tile, even though some tiles could go outside the `bbox`.
let bbox = Rectangle::<i32, Physical>::from_loc_and_size((x, y), tile_size);
let bbox = Rectangle::<i32, Physical>::new((x, y).into(), tile_size);
let mut tile = Tile {
bbox,
damage: None,
Expand Down Expand Up @@ -327,12 +327,12 @@ mod tests {
#[test]
fn tile_shaping() {
let mut damage = vec![
Rectangle::<i32, Physical>::from_loc_and_size((98, 406), (36, 48)),
Rectangle::<i32, Physical>::from_loc_and_size((158, 502), (828, 168)),
Rectangle::<i32, Physical>::from_loc_and_size((122, 694), (744, 528)),
Rectangle::<i32, Physical>::from_loc_and_size((194, 1318), (420, 72)),
Rectangle::<i32, Physical>::from_loc_and_size((146, 1414), (312, 48)),
Rectangle::<i32, Physical>::from_loc_and_size((32, 406), (108, 1152)),
Rectangle::<i32, Physical>::new((98, 406).into(), (36, 48).into()),
Rectangle::<i32, Physical>::new((158, 502).into(), (828, 168).into()),
Rectangle::<i32, Physical>::new((122, 694).into(), (744, 528).into()),
Rectangle::<i32, Physical>::new((194, 1318).into(), (420, 72).into()),
Rectangle::<i32, Physical>::new((146, 1414).into(), (312, 48).into()),
Rectangle::<i32, Physical>::new((32, 406).into(), (108, 1152).into()),
];

let mut shaper = shaper();
Expand Down Expand Up @@ -383,7 +383,7 @@ mod tests {

for x in 0..w {
for y in 0..h {
let rect = Rectangle::<i32, Physical>::from_loc_and_size((x, y), (1, 1));
let rect = Rectangle::<i32, Physical>::new((x, y).into(), (1, 1).into());
damage.push(rect);
}
}
Expand All @@ -398,7 +398,7 @@ mod tests {
let w1 = 216;
let h1 = 144;
let overlap1 = Rectangle::<i32, Physical>::from_size((w1, h1).into());
let overlap2 = Rectangle::<i32, Physical>::from_loc_and_size((w1, h1), (w - w1, h - h1));
let overlap2 = Rectangle::<i32, Physical>::new((w1, h1).into(), (w - w1, h - h1).into());
damage.push(overlap1);
damage.push(overlap2);

Expand Down
2 changes: 1 addition & 1 deletion src/backend/renderer/element/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ impl<R: Renderer> Element for MemoryRenderBufferRenderElement<R> {
}

fn geometry(&self, scale: Scale<f64>) -> Rectangle<i32, Physical> {
Rectangle::from_loc_and_size(self.location.to_i32_round(), self.physical_size(scale))
Rectangle::new(self.location.to_i32_round(), self.physical_size(scale))
}

fn damage_since(&self, scale: Scale<f64>, commit: Option<CommitCounter>) -> DamageSet<i32, Physical> {
Expand Down
2 changes: 1 addition & 1 deletion src/backend/renderer/element/solid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ impl SolidColorRenderElement {
alpha: f32,
kind: Kind,
) -> Self {
let geo = Rectangle::from_loc_and_size(location, buffer.size.to_physical_precise_round(scale));
let geo = Rectangle::new(location.into(), buffer.size.to_physical_precise_round(scale));
let color = buffer.color * alpha;
Self::new(buffer.id.clone(), geo, buffer.commit, color, kind)
}
Expand Down
4 changes: 2 additions & 2 deletions src/backend/renderer/element/surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ impl<R: Renderer + ImportAll> Element for WaylandSurfaceRenderElement<R> {
}

fn geometry(&self, scale: Scale<f64>) -> Rectangle<i32, Physical> {
Rectangle::from_loc_and_size(self.location.to_i32_round(), self.size(scale))
Rectangle::new(self.location.to_i32_round(), self.size(scale))
}

fn src(&self) -> Rectangle<f64, BufferCoords> {
Expand Down Expand Up @@ -511,7 +511,7 @@ impl<R: Renderer + ImportAll> Element for WaylandSurfaceRenderElement<R> {
let size = ((r.size.to_f64().to_physical(scale).to_point() + self.location).to_i32_round()
- self.location.to_i32_round())
.to_size();
Rectangle::from_loc_and_size(loc, size)
Rectangle::new(loc, size)
})
.collect::<OpaqueRegions<_, _>>()
}
Expand Down
2 changes: 1 addition & 1 deletion src/backend/renderer/element/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,7 @@ where
}

fn geometry(&self, scale: Scale<f64>) -> Rectangle<i32, Physical> {
Rectangle::from_loc_and_size(self.location.to_i32_round(), self.physical_size(scale))
Rectangle::new(self.location.to_i32_round(), self.physical_size(scale))
}

fn transform(&self) -> Transform {
Expand Down
Loading

0 comments on commit ef1a272

Please sign in to comment.