Expand description
An axis-aligned rectangular region
The region is defined by a point pos
and an extent size
, allowing easy
translations. It is empty unless size
is positive on both axes.
Fields§
§pos: Coord
§size: Size
Implementations§
source§impl Rect
impl Rect
sourcepub const fn new(pos: Coord, size: Size) -> Self
pub const fn new(pos: Coord, size: Size) -> Self
Examples found in repository?
src/geom.rs (line 546)
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
pub const ZERO: Self = Self::new(Coord::ZERO, Size::ZERO);
/// Construct from a [`Coord`] and [`Size`]
#[inline]
pub const fn new(pos: Coord, size: Size) -> Self {
Rect { pos, size }
}
/// Get the second point (pos + size)
#[inline]
pub fn pos2(&self) -> Coord {
self.pos + self.size
}
/// Check whether the given coordinate is contained within this rect
#[inline]
pub fn contains(&self, c: Coord) -> bool {
c.0 >= self.pos.0
&& c.0 < self.pos.0 + (self.size.0)
&& c.1 >= self.pos.1
&& c.1 < self.pos.1 + (self.size.1)
}
/// Calculate the intersection of two rects
#[inline]
pub fn intersection(&self, rhs: &Rect) -> Option<Rect> {
let (l1, l2) = (self.pos, self.pos2());
let (r1, r2) = (rhs.pos, rhs.pos2());
let pos = l1.max(r1);
let pos2 = l2.min(r2);
if pos.le(pos2) {
Some(Rect::new(pos, (pos2 - pos).cast()))
} else {
None
}
}
/// Shrink self in all directions by the given `n`
#[inline]
#[must_use = "method does not modify self but returns a new value"]
pub fn shrink(&self, n: i32) -> Rect {
let pos = self.pos + Offset::splat(n);
let size = self.size - Size::splat(n + n);
Rect { pos, size }
}
/// Expand self in all directions by the given `n`
///
/// In debug mode this asserts that `n` is non-negative.
#[inline]
#[must_use = "method does not modify self but returns a new value"]
pub fn expand(&self, n: i32) -> Rect {
debug_assert!(n >= 0);
let pos = self.pos - Offset::splat(n);
let size = self.size + Size::splat(n + n);
Rect { pos, size }
}
}
impl std::ops::Add<Offset> for Rect {
type Output = Self;
#[inline]
fn add(self, offset: Offset) -> Self {
Rect::new(self.pos + offset, self.size)
}
}
impl std::ops::AddAssign<Offset> for Rect {
#[inline]
fn add_assign(&mut self, offset: Offset) {
self.pos += offset;
}
}
impl std::ops::Sub<Offset> for Rect {
type Output = Self;
#[inline]
fn sub(self, offset: Offset) -> Self {
Rect::new(self.pos - offset, self.size)
}
More examples
src/root.rs (line 173)
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
fn resize_popup(&mut self, mgr: &mut ConfigMgr, index: usize) {
// Notation: p=point/coord, s=size, m=margin
// r=window/root rect, c=anchor rect
let r = self.core.rect;
let popup = &mut self.popups[index].1;
let c = find_rect(&self.w, popup.parent.clone()).unwrap();
let widget = self.w.find_widget_mut(&popup.id).unwrap();
let mut cache = layout::SolveCache::find_constraints(widget, mgr.size_mgr());
let ideal = cache.ideal(false);
let m = cache.margins();
let is_reversed = popup.direction.is_reversed();
let place_in = |rp, rs: i32, cp: i32, cs: i32, ideal, m: (u16, u16)| -> (i32, i32) {
let m: (i32, i32) = (m.0.into(), m.1.into());
let before: i32 = cp - (rp + m.1);
let before = before.max(0);
let after = (rs - (cs + before + m.0)).max(0);
if after >= ideal {
if is_reversed && before >= ideal {
(cp - ideal - m.1, ideal)
} else {
(cp + cs + m.0, ideal)
}
} else if before >= ideal {
(cp - ideal - m.1, ideal)
} else if before > after {
(rp, before)
} else {
(cp + cs + m.0, after)
}
};
#[allow(clippy::manual_clamp)]
let place_out = |rp, rs, cp: i32, cs, ideal: i32| -> (i32, i32) {
let pos = cp.min(rp + rs - ideal).max(rp);
let size = ideal.max(cs).min(rs);
(pos, size)
};
let rect = if popup.direction.is_horizontal() {
let (x, w) = place_in(r.pos.0, r.size.0, c.pos.0, c.size.0, ideal.0, m.horiz);
let (y, h) = place_out(r.pos.1, r.size.1, c.pos.1, c.size.1, ideal.1);
Rect::new(Coord(x, y), Size::new(w, h))
} else {
let (x, w) = place_out(r.pos.0, r.size.0, c.pos.0, c.size.0, ideal.0);
let (y, h) = place_in(r.pos.1, r.size.1, c.pos.1, c.size.1, ideal.1, m.vert);
Rect::new(Coord(x, y), Size::new(w, h))
};
cache.apply_rect(widget, mgr, rect, false, true);
}
sourcepub fn pos2(&self) -> Coord
pub fn pos2(&self) -> Coord
Get the second point (pos + size)
Examples found in repository?
More examples
src/layout/row_solver.rs (line 318)
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
pub fn for_children<W: Widget, F: FnMut(&mut W)>(
self,
widgets: &mut [W],
rect: Rect,
mut f: F,
) {
let (pos, end) = match self.direction.is_reversed() {
false => (rect.pos, rect.pos2()),
true => (rect.pos2(), rect.pos),
};
let start = match self.binary_search(widgets, pos) {
Ok(i) => i,
Err(i) if i > 0 => {
let j = i - 1;
let rect = widgets[j].rect();
let cond = match self.direction.as_direction() {
Direction::Right => pos.0 < rect.pos2().0,
Direction::Down => pos.1 < rect.pos2().1,
Direction::Left => rect.pos.0 <= pos.0,
Direction::Up => rect.pos.1 <= pos.1,
};
if cond {
j
} else {
i
}
}
Err(_) => 0,
};
for child in widgets[start..].iter_mut() {
let do_break = match self.direction.as_direction() {
Direction::Right => child.rect().pos.0 >= end.0,
Direction::Down => child.rect().pos.1 >= end.1,
Direction::Left => child.rect().pos2().0 < end.0,
Direction::Up => child.rect().pos2().1 < end.1,
};
if do_break {
break;
}
f(child);
}
}
sourcepub fn contains(&self, c: Coord) -> bool
pub fn contains(&self, c: Coord) -> bool
Check whether the given coordinate is contained within this rect
Examples found in repository?
src/root.rs (line 44)
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
fn find_id(&mut self, coord: Coord) -> Option<WidgetId> {
if !self.core.rect.contains(coord) {
return None;
}
for popup in self.popups.iter_mut().rev() {
if let Some(id) = self
.w
.find_widget_mut(&popup.1.id)
.and_then(|w| w.find_id(coord))
{
return Some(id);
}
}
self.w.find_id(coord).or_else(|| Some(self.id()))
}
More examples
src/layout/row_solver.rs (line 275)
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
pub fn find_child_index<W: Widget>(self, widgets: &[W], coord: Coord) -> Option<usize> {
match self.binary_search(widgets, coord) {
Ok(i) => Some(i),
Err(i) if self.direction.is_reversed() => {
if i == widgets.len() || !widgets[i].rect().contains(coord) {
None
} else {
Some(i)
}
}
Err(i) => {
if i == 0 || !widgets[i - 1].rect().contains(coord) {
None
} else {
Some(i - 1)
}
}
}
}
sourcepub fn intersection(&self, rhs: &Rect) -> Option<Rect>
pub fn intersection(&self, rhs: &Rect) -> Option<Rect>
Calculate the intersection of two rects
Trait Implementations§
source§impl AddAssign<Offset> for Rect
impl AddAssign<Offset> for Rect
source§fn add_assign(&mut self, offset: Offset)
fn add_assign(&mut self, offset: Offset)
Performs the
+=
operation. Read moresource§impl<'de> Deserialize<'de> for Rect
impl<'de> Deserialize<'de> for Rect
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
source§impl PartialEq<Rect> for Rect
impl PartialEq<Rect> for Rect
source§impl SubAssign<Offset> for Rect
impl SubAssign<Offset> for Rect
source§fn sub_assign(&mut self, offset: Offset)
fn sub_assign(&mut self, offset: Offset)
Performs the
-=
operation. Read moreimpl Copy for Rect
impl Eq for Rect
impl StructuralEq for Rect
impl StructuralPartialEq for Rect
Auto Trait Implementations§
impl RefUnwindSafe for Rect
impl Send for Rect
impl Sync for Rect
impl Unpin for Rect
impl UnwindSafe for Rect
Blanket Implementations§
source§impl<S, T> CastApprox<T> for Swhere
T: ConvApprox<S>,
impl<S, T> CastApprox<T> for Swhere
T: ConvApprox<S>,
source§fn try_cast_approx(self) -> Result<T, Error>
fn try_cast_approx(self) -> Result<T, Error>
source§fn cast_approx(self) -> T
fn cast_approx(self) -> T
source§impl<S, T> CastFloat<T> for Swhere
T: ConvFloat<S>,
impl<S, T> CastFloat<T> for Swhere
T: ConvFloat<S>,
source§fn cast_trunc(self) -> T
fn cast_trunc(self) -> T
Cast to integer, truncating Read more
source§fn cast_nearest(self) -> T
fn cast_nearest(self) -> T
Cast to the nearest integer Read more
source§fn cast_floor(self) -> T
fn cast_floor(self) -> T
Cast the floor to an integer Read more
source§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key
and return true
if they are equal.