Refactor: pass Rect:s by value (cleaner)

This commit is contained in:
Emil Ernerfeldt 2020-05-05 19:12:00 +02:00
parent 9d1cf77aa7
commit 6aadf4128e
13 changed files with 39 additions and 39 deletions

View file

@ -138,7 +138,7 @@ fn paint_icon(region: &mut Region, state: &State, interact: &InteractInfo) {
let stroke_color = region.style().interact_stroke_color(&interact);
let stroke_width = region.style().interact_stroke_width(&interact);
let (mut small_icon_rect, _) = region.style().icon_rectangles(&interact.rect);
let (mut small_icon_rect, _) = region.style().icon_rectangles(interact.rect);
small_icon_rect.set_center(pos2(
interact.rect.left() + region.style().indent / 2.0,
interact.rect.center().y,

View file

@ -78,7 +78,7 @@ impl Floating {
let rect = Rect::from_min_size(state.pos, state.size);
let clip_rect = Rect::everything();
let move_interact = ctx.interact(layer, &clip_rect, &rect, Some(id.with("move")));
let move_interact = ctx.interact(layer, clip_rect, rect, Some(id.with("move")));
let input = ctx.input();
if move_interact.active {

View file

@ -160,7 +160,7 @@ impl Resize {
position + state.size + self.handle_offset - corner_size,
corner_size,
);
let corner_interact = region.interact_rect(&corner_rect, id.with("corner"));
let corner_interact = region.interact_rect(corner_rect, id.with("corner"));
if corner_interact.active {
if let Some(mouse_pos) = region.input().mouse_pos {
@ -181,7 +181,7 @@ impl Resize {
let desired_size = {
let mut content_clip_rect = region
.clip_rect()
.intersect(&inner_rect.expand(region.style().clip_rect_margin));
.intersect(inner_rect.expand(region.style().clip_rect_margin));
// If we pull the resize handle to shrink, we want to TRY to shink it.
// After laying out the contents, we might be much bigger.

View file

@ -80,7 +80,7 @@ impl ScrollArea {
outer_region.cursor() - state.offset,
vec2(inner_size.x, f32::INFINITY),
));
let mut content_clip_rect = outer_region.clip_rect().intersect(&inner_rect);
let mut content_clip_rect = outer_region.clip_rect().intersect(inner_rect);
content_clip_rect.max.x = outer_region.clip_rect().max.x - current_scroll_bar_width; // Nice handling of forced resizing beyond the possible
content_region.set_clip_rect(content_clip_rect);
@ -100,14 +100,14 @@ impl ScrollArea {
inner_rect.size() + vec2(current_scroll_bar_width, 0.0),
);
let content_interact = outer_region.interact_rect(&inner_rect, scroll_area_id.with("area"));
let content_interact = outer_region.interact_rect(inner_rect, scroll_area_id.with("area"));
if content_interact.active {
// Dragging scroll area to scroll:
state.offset.y -= ctx.input().mouse_move.y;
}
// TODO: check that nothing else is being inteacted with
if outer_region.contains_mouse(&outer_rect) && ctx.memory().active_id.is_none() {
if outer_region.contains_mouse(outer_rect) && ctx.memory().active_id.is_none() {
state.offset.y -= ctx.input().scroll_delta.y;
}
@ -134,7 +134,7 @@ impl ScrollArea {
// intentionally use same id for inside and outside of handle
let interact_id = scroll_area_id.with("vertical");
let handle_interact = outer_region.interact_rect(&handle_rect, interact_id);
let handle_interact = outer_region.interact_rect(handle_rect, interact_id);
if let Some(mouse_pos) = ctx.input().mouse_pos {
if handle_interact.active {
@ -145,7 +145,7 @@ impl ScrollArea {
} else {
// Check for mouse down outside handle:
let scroll_bg_interact =
outer_region.interact_rect(&outer_scroll_rect, interact_id);
outer_region.interact_rect(outer_scroll_rect, interact_id);
if scroll_bg_interact.active {
// Center scroll at mouse pos:

View file

@ -185,7 +185,7 @@ impl Context {
}
}
pub fn contains_mouse(&self, layer: Layer, clip_rect: &Rect, rect: &Rect) -> bool {
pub fn contains_mouse(&self, layer: Layer, clip_rect: Rect, rect: Rect) -> bool {
let rect = rect.intersect(clip_rect);
if let Some(mouse_pos) = self.input.mouse_pos {
rect.contains(mouse_pos) && layer == self.memory().layer_at(mouse_pos)
@ -197,11 +197,11 @@ impl Context {
pub fn interact(
&self,
layer: Layer,
clip_rect: &Rect,
rect: &Rect,
clip_rect: Rect,
rect: Rect,
interaction_id: Option<Id>,
) -> InteractInfo {
let hovered = self.contains_mouse(layer, clip_rect, &rect);
let hovered = self.contains_mouse(layer, clip_rect, rect);
let mut memory = self.memory();
let active = interaction_id.is_some() && memory.active_id == interaction_id;
@ -211,7 +211,7 @@ impl Context {
if memory.active_id.is_some() {
// Already clicked something else this frame
InteractInfo {
rect: *rect,
rect,
hovered,
clicked: false,
active: false,
@ -219,7 +219,7 @@ impl Context {
} else {
memory.active_id = interaction_id;
InteractInfo {
rect: *rect,
rect,
hovered,
clicked: false,
active: true,
@ -227,7 +227,7 @@ impl Context {
}
} else {
InteractInfo {
rect: *rect,
rect,
hovered,
clicked: false,
active: false,
@ -235,21 +235,21 @@ impl Context {
}
} else if self.input.mouse_released {
InteractInfo {
rect: *rect,
rect,
hovered,
clicked: hovered && active,
active,
}
} else if self.input.mouse_down {
InteractInfo {
rect: *rect,
rect,
hovered: hovered && active,
clicked: false,
active,
}
} else {
InteractInfo {
rect: *rect,
rect,
hovered,
clicked: false,
active,
@ -263,7 +263,7 @@ impl Context {
let text_style = TextStyle::Monospace;
let font = &self.fonts[text_style];
let (text, size) = font.layout_multiline(text, f32::INFINITY);
let rect = align_rect(&Rect::from_min_size(pos, size), align);
let rect = align_rect(Rect::from_min_size(pos, size), align);
self.add_paint_cmd(
layer,
PaintCmd::Rect {
@ -302,7 +302,7 @@ impl Context {
) -> Vec2 {
let font = &self.fonts[text_style];
let (text, size) = font.layout_multiline(text, f32::INFINITY);
let rect = align_rect(&Rect::from_min_size(pos, size), align);
let rect = align_rect(Rect::from_min_size(pos, size), align);
self.add_text(layer, rect.min, text_style, text, text_color);
size
}

View file

@ -216,7 +216,7 @@ impl Painting {
region.add_custom_contents(vec2(f32::INFINITY, 200.0), |region| {
let canvas_corner = region.cursor();
let interact = region.reserve_space(region.available_space(), Some(region.id()));
region.set_clip_rect(region.clip_rect().intersect(&interact.rect)); // Make sure we don't paint out of bounds
region.set_clip_rect(region.clip_rect().intersect(interact.rect)); // Make sure we don't paint out of bounds
if self.lines.is_empty() {
self.lines.push(vec![]);

View file

@ -76,7 +76,7 @@ impl Default for Align {
}
}
pub fn align_rect(rect: &Rect, align: (Align, Align)) -> Rect {
pub fn align_rect(rect: Rect, align: (Align, Align)) -> Rect {
let x = match align.0 {
Align::Min => rect.left(),
Align::Center => rect.left() - 0.5 * rect.width(),

View file

@ -376,7 +376,7 @@ impl Rect {
}
#[must_use]
pub fn intersect(self, other: &Rect) -> Self {
pub fn intersect(self, other: Rect) -> Self {
Self {
min: self.min.max(other.min),
max: self.max.min(other.max),

View file

@ -178,7 +178,7 @@ impl Path {
);
}
pub fn add_rectangle(&mut self, rect: &Rect) {
pub fn add_rectangle(&mut self, rect: Rect) {
let min = rect.min;
let max = rect.max;
self.add_point(pos2(min.x, min.y), vec2(-1.0, -1.0));
@ -187,7 +187,7 @@ impl Path {
self.add_point(pos2(min.x, max.y), vec2(-1.0, 1.0));
}
pub fn add_rounded_rectangle(&mut self, rect: &Rect, corner_radius: f32) {
pub fn add_rounded_rectangle(&mut self, rect: Rect, corner_radius: f32) {
let min = rect.min;
let max = rect.max;
@ -467,7 +467,7 @@ pub fn mesh_command(
rect,
} => {
let mut path = Path::default();
path.add_rounded_rectangle(&rect, corner_radius);
path.add_rounded_rectangle(rect, corner_radius);
if let Some(fill_color) = fill_color {
fill_closed_path(out_mesh, options, &path.0, fill_color);
}

View file

@ -235,8 +235,8 @@ impl Region {
// ------------------------------------------------------------------------
pub fn contains_mouse(&self, rect: &Rect) -> bool {
self.ctx.contains_mouse(self.layer, &self.clip_rect, rect)
pub fn contains_mouse(&self, rect: Rect) -> bool {
self.ctx.contains_mouse(self.layer, self.clip_rect, rect)
}
pub fn has_kb_focus(&self, id: Id) -> bool {
@ -277,12 +277,12 @@ impl Region {
/// Check for clicks on this entire region (rect())
pub fn interact_whole(&self) -> InteractInfo {
self.interact_rect(&self.rect(), self.id)
self.interact_rect(self.rect(), self.id)
}
pub fn interact_rect(&self, rect: &Rect, id: Id) -> InteractInfo {
pub fn interact_rect(&self, rect: Rect, id: Id) -> InteractInfo {
self.ctx
.interact(self.layer, &self.clip_rect, rect, Some(id))
.interact(self.layer, self.clip_rect, rect, Some(id))
}
pub fn response(&mut self, interact: InteractInfo) -> GuiResponse {
@ -356,7 +356,7 @@ impl Region {
}
self.ctx
.interact(self.layer, &self.clip_rect, &rect, interaction_id)
.interact(self.layer, self.clip_rect, rect, interaction_id)
}
/// Reserve this much space and move the cursor.
@ -442,7 +442,7 @@ impl Region {
) -> Vec2 {
let font = &self.fonts()[text_style];
let (text, size) = font.layout_multiline(text, f32::INFINITY);
let rect = align_rect(&Rect::from_min_size(pos, size), align);
let rect = align_rect(Rect::from_min_size(pos, size), align);
self.add_text(rect.min, text_style, text, text_color);
size
}

View file

@ -147,7 +147,7 @@ impl Style {
}
/// Returns small icon rectangle and big icon rectangle
pub fn icon_rectangles(&self, rect: &Rect) -> (Rect, Rect) {
pub fn icon_rectangles(&self, rect: Rect) -> (Rect, Rect) {
let box_side = self.start_icon_width;
let big_icon_rect = Rect::from_center_size(
pos2(rect.left() + box_side / 2.0, rect.center().y),

View file

@ -218,7 +218,7 @@ impl<'a> Widget for Checkbox<'a> {
if interact.clicked {
*self.checked = !*self.checked;
}
let (small_icon_rect, big_icon_rect) = region.style().icon_rectangles(&interact.rect);
let (small_icon_rect, big_icon_rect) = region.style().icon_rectangles(interact.rect);
region.add_paint_cmd(PaintCmd::Rect {
corner_radius: 3.0,
fill_color: region.style().interact_fill_color(&interact),
@ -294,7 +294,7 @@ impl Widget for RadioButton {
let fill_color = region.style().interact_fill_color(&interact);
let stroke_color = region.style().interact_stroke_color(&interact);
let (small_icon_rect, big_icon_rect) = region.style().icon_rectangles(&interact.rect);
let (small_icon_rect, big_icon_rect) = region.style().icon_rectangles(interact.rect);
region.add_paint_cmd(PaintCmd::Circle {
center: big_icon_rect.center(),

View file

@ -221,7 +221,7 @@ impl Painter {
let mut target = display.draw();
target.clear_color(0.0, 0.0, 0.0, 0.0);
for (clip_rect, mesh) in batches {
self.paint_batch(&mut target, display, &clip_rect, &mesh, texture)
self.paint_batch(&mut target, display, clip_rect, &mesh, texture)
}
target.finish().unwrap();
}
@ -231,7 +231,7 @@ impl Painter {
&mut self,
target: &mut Frame,
display: &glium::Display,
clip_rect: &Rect,
clip_rect: Rect,
mesh: &Mesh,
texture: &emigui::Texture,
) {