Skip to main content

Rect

Struct Rect 

Source
pub struct Rect {
    pub x: f32,
    pub y: f32,
    pub w: f32,
    pub h: f32,
}
Expand description

A rectangle: its top left corner and size, in units.

Fields§

§x: f32

Left edge.

§y: f32

Top edge.

§w: f32

Width.

§h: f32

Height.

Implementations§

Source§

impl Rect

Source

pub const EMPTY: Rect

An empty rectangle at the origin.

Source

pub const EVERYTHING: Rect

Larger than anything ever drawn: “no clipping”.

Source

pub fn right(&self) -> f32

Right edge.

Examples found in repository?
examples/book.rs (line 136)
134fn mark(screen: &mut Surface, area: Rect) {
135    let [left, top] = [area.x.max(0.0) as usize, area.y.max(0.0) as usize];
136    let [right, bottom] = [(area.right() as usize).min(screen.width), (area.bottom() as usize).min(screen.height)];
137    for y in top..bottom {
138        for x in left..right {
139            if x < left + 2 || x + 2 >= right || y < top + 2 || y + 2 >= bottom {
140                screen.pixels[y * screen.width + x] = 0xffe0_3c3c;
141            }
142        }
143    }
144}
More examples
Hide additional examples
examples/report.rs (line 87)
76fn chart(paper: &mut Paper, r: taut::Rect) {
77    let (sold, back, line, dim) = (Color::rgb(0x2b6cb0), Color::rgb(0xdd6b20), Color::rgb(0xe2e8f0), Color::rgb(0x4a5568));
78    let small = Font::new(12.0);
79    let plot = rect(r.x + 48.0, r.y, r.w - 96.0, r.h - 64.0);
80    let (most, most_back) = (1000.0, 50.0);
81    for step in 0..=5 {
82        let y = plot.bottom() - plot.h * step as f32 / 5.0;
83        paper.rect(rect(plot.x, y, plot.w, 1.0), line);
84        let label = format!("{}", step * 200);
85        let wide = paper.measure(&label, small);
86        paper.text(&label, plot.x - 8.0 - wide, y - 8.0, small, dim);
87        paper.text(&format!("{}", step * 10), plot.right() + 8.0, y - 8.0, small, back);
88    }
89    let slot = plot.w / 12.0;
90    let mut path = Path::new();
91    for (month, name) in MONTHS.iter().enumerate() {
92        let x = plot.x + slot * month as f32;
93        let high = plot.h * SOLD[month] / most;
94        paper.rounded(rect(x + slot * 0.2, plot.bottom() - high, slot * 0.6, high), 3.0, sold);
95        let wide = paper.measure(name, small);
96        paper.text(name, x + (slot - wide) / 2.0, plot.bottom() + 8.0, small, dim);
97        let y = plot.bottom() - plot.h * BACK[month] / most_back;
98        match month {
99            0 => _ = path.move_to(x + slot / 2.0, y),
100            _ => _ = path.line_to(x + slot / 2.0, y),
101        }
102    }
103    paper.outline(&path, Pen::new(3.0), back);
104    for (month, came) in BACK.iter().enumerate() {
105        let mut dot = Path::new();
106        dot.circle(plot.x + slot * (month as f32 + 0.5), plot.bottom() - plot.h * came / most_back, 4.5);
107        paper.shape(&dot, back);
108    }
109    let key = plot.bottom() + 40.0;
110    paper.rounded(rect(plot.x, key, 14.0, 14.0), 3.0, sold);
111    paper.text("Sold (left)", plot.x + 22.0, key - 2.0, small, dim);
112    paper.rounded(rect(plot.x + 140.0, key + 5.0, 14.0, 4.0), 2.0, back);
113    paper.text("Came back (right)", plot.x + 162.0, key - 2.0, small, dim);
114}
examples/overlay.rs (line 127)
117pub unsafe extern "C" fn overlay_frame(overlay: *mut Overlay, changed: *mut [i32; 4]) -> *const u32 {
118    let Overlay { ui, surface, settings, waiting, since } = unsafe { &mut *overlay };
119    ui.set_time(since.elapsed());
120    // Something is moving (a switch sliding over): the interface asked to be run by now.
121    let due = ui.wake_in() == Some(Duration::ZERO);
122    let mut area = [0; 4];
123    if std::mem::take(waiting) || due {
124        let part = ui.run(&mut |ui| describe(settings, ui)).snap_to_pixels(ui.scale());
125        surface.redraw(ui, part);
126        let scale = ui.scale();
127        area = [(part.x * scale) as i32, (part.y * scale) as i32, (part.right() * scale) as i32, (part.bottom() * scale) as i32];
128    }
129    unsafe { *changed = area };
130    surface.pixels.as_ptr()
131}
Source

pub fn bottom(&self) -> f32

Bottom edge.

Examples found in repository?
examples/book.rs (line 136)
134fn mark(screen: &mut Surface, area: Rect) {
135    let [left, top] = [area.x.max(0.0) as usize, area.y.max(0.0) as usize];
136    let [right, bottom] = [(area.right() as usize).min(screen.width), (area.bottom() as usize).min(screen.height)];
137    for y in top..bottom {
138        for x in left..right {
139            if x < left + 2 || x + 2 >= right || y < top + 2 || y + 2 >= bottom {
140                screen.pixels[y * screen.width + x] = 0xffe0_3c3c;
141            }
142        }
143    }
144}
More examples
Hide additional examples
examples/report.rs (line 82)
76fn chart(paper: &mut Paper, r: taut::Rect) {
77    let (sold, back, line, dim) = (Color::rgb(0x2b6cb0), Color::rgb(0xdd6b20), Color::rgb(0xe2e8f0), Color::rgb(0x4a5568));
78    let small = Font::new(12.0);
79    let plot = rect(r.x + 48.0, r.y, r.w - 96.0, r.h - 64.0);
80    let (most, most_back) = (1000.0, 50.0);
81    for step in 0..=5 {
82        let y = plot.bottom() - plot.h * step as f32 / 5.0;
83        paper.rect(rect(plot.x, y, plot.w, 1.0), line);
84        let label = format!("{}", step * 200);
85        let wide = paper.measure(&label, small);
86        paper.text(&label, plot.x - 8.0 - wide, y - 8.0, small, dim);
87        paper.text(&format!("{}", step * 10), plot.right() + 8.0, y - 8.0, small, back);
88    }
89    let slot = plot.w / 12.0;
90    let mut path = Path::new();
91    for (month, name) in MONTHS.iter().enumerate() {
92        let x = plot.x + slot * month as f32;
93        let high = plot.h * SOLD[month] / most;
94        paper.rounded(rect(x + slot * 0.2, plot.bottom() - high, slot * 0.6, high), 3.0, sold);
95        let wide = paper.measure(name, small);
96        paper.text(name, x + (slot - wide) / 2.0, plot.bottom() + 8.0, small, dim);
97        let y = plot.bottom() - plot.h * BACK[month] / most_back;
98        match month {
99            0 => _ = path.move_to(x + slot / 2.0, y),
100            _ => _ = path.line_to(x + slot / 2.0, y),
101        }
102    }
103    paper.outline(&path, Pen::new(3.0), back);
104    for (month, came) in BACK.iter().enumerate() {
105        let mut dot = Path::new();
106        dot.circle(plot.x + slot * (month as f32 + 0.5), plot.bottom() - plot.h * came / most_back, 4.5);
107        paper.shape(&dot, back);
108    }
109    let key = plot.bottom() + 40.0;
110    paper.rounded(rect(plot.x, key, 14.0, 14.0), 3.0, sold);
111    paper.text("Sold (left)", plot.x + 22.0, key - 2.0, small, dim);
112    paper.rounded(rect(plot.x + 140.0, key + 5.0, 14.0, 4.0), 2.0, back);
113    paper.text("Came back (right)", plot.x + 162.0, key - 2.0, small, dim);
114}
examples/overlay.rs (line 127)
117pub unsafe extern "C" fn overlay_frame(overlay: *mut Overlay, changed: *mut [i32; 4]) -> *const u32 {
118    let Overlay { ui, surface, settings, waiting, since } = unsafe { &mut *overlay };
119    ui.set_time(since.elapsed());
120    // Something is moving (a switch sliding over): the interface asked to be run by now.
121    let due = ui.wake_in() == Some(Duration::ZERO);
122    let mut area = [0; 4];
123    if std::mem::take(waiting) || due {
124        let part = ui.run(&mut |ui| describe(settings, ui)).snap_to_pixels(ui.scale());
125        surface.redraw(ui, part);
126        let scale = ui.scale();
127        area = [(part.x * scale) as i32, (part.y * scale) as i32, (part.right() * scale) as i32, (part.bottom() * scale) as i32];
128    }
129    unsafe { *changed = area };
130    surface.pixels.as_ptr()
131}
Source

pub fn size(&self) -> Size

Width and height.

Source

pub fn is_empty(&self) -> bool

Whether it has no width or no height.

Examples found in repository?
examples/gallery.rs (line 408)
390fn tool_bar(app: &mut App, ui: &mut Ui) {
391    let bar = ui.tool_bar();
392    if ui.tool(PLUS, "Add an item\tCtrl+N").clicked {
393        ui.notice("An item was added");
394    }
395    let tint = ui.tool(DROP, "The colour that stands out");
396    ui.tool_toggle(&mut app.starred, STAR, "Starred\tCtrl+D");
397    ui.tool_group();
398    if ui.tool(COPY, "Copy the notes").clicked {
399        ui.copy(&app.notes);
400    }
401    if ui.tool(SYNC, "Start over").clicked {
402        app.presses = 0;
403    }
404    ui.tool(GRID, "A tool that does nothing");
405    ui.close();
406    // (Opened from the overflow menu, it points at the bar.)
407    if tint.clicked {
408        ui.open_popup(tint.id, if tint.rect.is_empty() { bar.rect } else { tint.rect });
409    }
410    if ui.popover(tint.id) {
411        ui.label("The colour that stands out");
412        ui.color_picker(&mut app.tint);
413        ui.close();
414    }
415}
Source

pub fn contains(&self, p: Point) -> bool

Whether the point is inside. All four sides are tested at once, without branches: this runs for every box the pointer could be over, on every move.

Source

pub fn intersect(&self, o: Rect) -> Rect

The overlap of both; empty if they do not touch.

Source

pub fn union(&self, o: Rect) -> Rect

The smallest rectangle holding both. An empty one adds nothing.

Examples found in repository?
examples/book.rs (line 100)
88fn take(ui: &mut Ui, describe: &mut dyn FnMut(&mut Ui), screen: &mut Surface, step: Step) -> Rect {
89    let events = match step {
90        Step::Click(name) => vec![Event::Move(middle(find(ui, name))), Event::Press(Button::Left, Mods::NONE), Event::Release(Button::Left)],
91        Step::ClickAt(x, y) => vec![Event::Move(pt(x, y)), Event::Press(Button::Left, Mods::NONE), Event::Release(Button::Left)],
92        Step::RightClick(name) => vec![Event::Move(middle(find(ui, name))), Event::Press(Button::Right, Mods::NONE), Event::Release(Button::Right)],
93        Step::Hover(name) => vec![Event::Move(middle(find(ui, name)))],
94        Step::Type(text) => vec![Event::Text(text.into())],
95        Step::Press(key, mods) => vec![Event::Key(key, mods)],
96        Step::Wheel(name, down) => vec![Event::Move(middle(find(ui, name))), Event::Wheel(0.0, down)],
97    };
98    let mut changed = Rect::EMPTY;
99    for event in events {
100        changed = changed.union(happens(ui, describe, screen, event));
101    }
102    changed
103}
Source

pub fn inset(&self, by: Insets) -> Rect

Shrunk by the insets (grown, with negative ones).

Source

pub fn snap_to_pixels(&self, scale: f32) -> Rect

Grown just enough for its edges to lie on whole pixels at scale pixels per unit: the area to redraw.

Examples found in repository?
examples/book.rs (line 123)
107fn shoot(folder: &Path, mut shot: Shot) {
108    let mut ui = Ui::new(taut::platform::system_text(), Box::new(LocalClipboard::default()));
109    ui.resize(Size { w: shot.size[0], h: shot.size[1] }, shot.scale);
110    ui.set_screen_reader(true);
111    ui.run(&mut shot.describe);
112    ui.run(&mut shot.describe);
113    let mut screen = Surface::draw(&mut ui, 64);
114    let mut changed = Rect::EMPTY;
115    for (at, step) in shot.steps.iter().enumerate() {
116        ui.set_time(Duration::from_secs(at as u64 + 1));
117        changed = take(&mut ui, &mut shot.describe, &mut screen, *step);
118    }
119    ui.set_time(Duration::from_secs(shot.steps.len() as u64 + 2));
120    let rest = ui.run(&mut shot.describe);
121    screen.redraw(&mut ui, rest);
122    if shot.marked {
123        mark(&mut screen, changed.snap_to_pixels(shot.scale));
124    }
125    if shot.over_a_game {
126        lay_over_a_game(&mut screen);
127    }
128    let file = folder.join(format!("{}.png", shot.name));
129    std::fs::write(&file, screen.to_png()).expect("the picture could not be written");
130    println!("{} ({} by {})", file.display(), screen.width, screen.height);
131}
More examples
Hide additional examples
examples/overlay.rs (line 124)
117pub unsafe extern "C" fn overlay_frame(overlay: *mut Overlay, changed: *mut [i32; 4]) -> *const u32 {
118    let Overlay { ui, surface, settings, waiting, since } = unsafe { &mut *overlay };
119    ui.set_time(since.elapsed());
120    // Something is moving (a switch sliding over): the interface asked to be run by now.
121    let due = ui.wake_in() == Some(Duration::ZERO);
122    let mut area = [0; 4];
123    if std::mem::take(waiting) || due {
124        let part = ui.run(&mut |ui| describe(settings, ui)).snap_to_pixels(ui.scale());
125        surface.redraw(ui, part);
126        let scale = ui.scale();
127        area = [(part.x * scale) as i32, (part.y * scale) as i32, (part.right() * scale) as i32, (part.bottom() * scale) as i32];
128    }
129    unsafe { *changed = area };
130    surface.pixels.as_ptr()
131}
Source

pub fn grow(&self, by: f32) -> Rect

Grown by by on every side (shrunk, if negative).

Source

pub fn moved(&self, dx: f32, dy: f32) -> Rect

Moved right dx and down dy.

Trait Implementations§

Source§

impl Clone for Rect

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Rect

Source§

impl Debug for Rect

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Rect

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl PartialEq for Rect

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Rect

Auto Trait Implementations§

§

impl Forget for Rect

§

impl Freeze for Rect

§

impl Move for Rect

§

impl RefUnwindSafe for Rect

§

impl Send for Rect

§

impl Sync for Rect

§

impl Unpin for Rect

§

impl UnsafeUnpin for Rect

§

impl UnwindSafe for Rect

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.