Browse Source

Resize component: better handling of resize=false

readable-ids
Emil Ernerfeldt 5 years ago
parent
commit
ad0f6f6117
  1. 55
      emigui/src/containers/resize.rs

55
emigui/src/containers/resize.rs

@ -133,10 +133,6 @@ impl Resize {
// TODO: a common trait for Things that follow this pattern // TODO: a common trait for Things that follow this pattern
impl Resize { impl Resize {
pub fn show(mut self, ui: &mut Ui, add_contents: impl FnOnce(&mut Ui)) { pub fn show(mut self, ui: &mut Ui, add_contents: impl FnOnce(&mut Ui)) {
if !self.resizable {
return add_contents(ui);
}
let id = ui.make_child_id("scroll"); let id = ui.make_child_id("scroll");
self.min_size = self.min_size.min(ui.available_space()); self.min_size = self.min_size.min(ui.available_space());
self.max_size = self.max_size.min(ui.available_space()); self.max_size = self.max_size.min(ui.available_space());
@ -155,26 +151,31 @@ impl Resize {
let position = ui.cursor(); let position = ui.cursor();
// Resize-corner: let corner_interact = if self.resizable {
let corner_size = Vec2::splat(16.0); // TODO: style // Resize-corner:
let corner_rect = Rect::from_min_size( let corner_size = Vec2::splat(16.0); // TODO: style
position + state.size + self.handle_offset - corner_size, let corner_rect = Rect::from_min_size(
corner_size, position + state.size + self.handle_offset - corner_size,
); corner_size,
let corner_interact = ui.interact_rect(corner_rect, id.with("corner")); );
let corner_interact = ui.interact_rect(corner_rect, id.with("corner"));
if corner_interact.active {
if let Some(mouse_pos) = ui.input().mouse_pos { if corner_interact.active {
// This is the desired size. We may not be able to achieve it. if let Some(mouse_pos) = ui.input().mouse_pos {
// This is the desired size. We may not be able to achieve it.
state.size =
mouse_pos - position + 0.5 * corner_interact.rect.size() - self.handle_offset; state.size = mouse_pos - position + 0.5 * corner_interact.rect.size()
// We don't clamp to max size, because we want to be able to push against outer bounds. - self.handle_offset;
// For instance, if we are inside a bigger Resize region, we want to expand that. // We don't clamp to max size, because we want to be able to push against outer bounds.
// state.size = state.size.clamp(self.min_size..=self.max_size); // For instance, if we are inside a bigger Resize region, we want to expand that.
state.size = state.size.max(self.min_size); // state.size = state.size.clamp(self.min_size..=self.max_size);
state.size = state.size.max(self.min_size);
}
} }
} Some(corner_interact)
} else {
None
};
// ------------------------------ // ------------------------------
@ -224,10 +225,12 @@ impl Resize {
// ------------------------------ // ------------------------------
paint_resize_corner(ui, &corner_interact); if let Some(corner_interact) = corner_interact {
paint_resize_corner(ui, &corner_interact);
if corner_interact.hovered || corner_interact.active { if corner_interact.hovered || corner_interact.active {
ui.ctx().output().cursor_icon = CursorIcon::ResizeNwSe; ui.ctx().output().cursor_icon = CursorIcon::ResizeNwSe;
}
} }
ui.memory().resize.insert(id, state); ui.memory().resize.insert(id, state);

Loading…
Cancel
Save