1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-30 05:10:03 -04:00

Add ui.allocate_at_least and ui.allocate_exact_size

This commit is contained in:
Emil Ernerfeldt
2021-01-06 11:03:29 +01:00
parent f68c30e0c7
commit b4871e2aef
15 changed files with 74 additions and 72 deletions

View File

@@ -294,10 +294,10 @@ impl ColorTest {
fn vertex_gradient(ui: &mut Ui, bg_fill: Color32, gradient: &Gradient) -> Response {
use egui::paint::*;
let response = ui.allocate_response(GRADIENT_SIZE, Sense::hover());
let (rect, response) = ui.allocate_at_least(GRADIENT_SIZE, Sense::hover());
if bg_fill != Default::default() {
let mut triangles = Triangles::default();
triangles.add_colored_rect(response.rect, bg_fill);
triangles.add_colored_rect(rect, bg_fill);
ui.painter().add(PaintCmd::triangles(triangles));
}
{
@@ -306,9 +306,9 @@ fn vertex_gradient(ui: &mut Ui, bg_fill: Color32, gradient: &Gradient) -> Respon
let mut triangles = Triangles::default();
for (i, &color) in gradient.0.iter().enumerate() {
let t = i as f32 / (n as f32 - 1.0);
let x = lerp(response.rect.x_range(), t);
triangles.colored_vertex(pos2(x, response.rect.top()), color);
triangles.colored_vertex(pos2(x, response.rect.bottom()), color);
let x = lerp(rect.x_range(), t);
triangles.colored_vertex(pos2(x, rect.top()), color);
triangles.colored_vertex(pos2(x, rect.bottom()), color);
if i < n - 1 {
let i = i as u32;
triangles.add_triangle(2 * i, 2 * i + 1, 2 * i + 2);

View File

@@ -89,10 +89,10 @@ impl DemoWindow {
ui.horizontal(|ui| {
ui.label("You can pretty easily paint your own small icons:");
use std::f32::consts::TAU;
let response = ui.allocate_response(Vec2::splat(16.0), Sense::hover());
let (rect, _response) = ui.allocate_at_least(Vec2::splat(16.0), Sense::hover());
let painter = ui.painter();
let c = response.rect.center();
let r = response.rect.width() / 2.0 - 1.0;
let c = rect.center();
let r = rect.width() / 2.0 - 1.0;
let color = Color32::from_gray(128);
let stroke = Stroke::new(1.0, color);
painter.circle_stroke(c, r, stroke);
@@ -207,9 +207,9 @@ impl BoxPainting {
ui.horizontal_wrapped(|ui| {
for _ in 0..self.num_boxes {
let response = ui.allocate_response(self.size, Sense::hover());
let (rect, _response) = ui.allocate_at_least(self.size, Sense::hover());
ui.painter().rect(
response.rect,
rect,
self.corner_radius,
Color32::from_gray(64),
Stroke::new(self.stroke_width, Color32::WHITE),

View File

@@ -47,7 +47,7 @@ pub fn drop_target<R>(
let mut content_ui = ui.child_ui(inner_rect, *ui.layout());
let ret = body(&mut content_ui);
let outer_rect = Rect::from_min_max(outer_rect_bounds.min, content_ui.min_rect().max + margin);
let response = ui.allocate_response(outer_rect.size(), Sense::hover());
let (rect, response) = ui.allocate_at_least(outer_rect.size(), Sense::hover());
let style = if is_being_dragged && can_accept_what_is_being_dragged && response.hovered {
ui.style().visuals.widgets.active
@@ -65,7 +65,7 @@ pub fn drop_target<R>(
corner_radius: style.corner_radius,
fill: style.bg_fill,
stroke: style.bg_stroke,
rect: response.rect,
rect,
},
);

View File

@@ -24,7 +24,7 @@ pub fn toggle(ui: &mut egui::Ui, on: &mut bool) -> egui::Response {
// 2. Allocating space:
// This is where we get a region of the screen assigned.
// We also tell the Ui to sense clicks in the allocated region.
let response = ui.allocate_response(desired_size, egui::Sense::click());
let (rect, response) = ui.allocate_exact_size(desired_size, egui::Sense::click());
// 3. Interact: Time to check for clicks!.
if response.clicked {
@@ -44,7 +44,6 @@ pub fn toggle(ui: &mut egui::Ui, on: &mut bool) -> egui::Response {
let on_bg_fill = egui::Rgba::from_rgb(0.0, 0.5, 0.25);
let bg_fill = egui::lerp(off_bg_fill..=on_bg_fill, how_on);
// All coordinates are in absolute screen coordinates so we use `rect` to place the elements.
let rect = response.rect;
let radius = 0.5 * rect.height();
ui.painter().rect(rect, radius, bg_fill, visuals.bg_stroke);
// Paint the circle, animating it from left to right with `how_on`:
@@ -62,7 +61,7 @@ pub fn toggle(ui: &mut egui::Ui, on: &mut bool) -> egui::Response {
#[allow(dead_code)]
fn toggle_compact(ui: &mut egui::Ui, on: &mut bool) -> egui::Response {
let desired_size = ui.style().spacing.interact_size;
let response = ui.allocate_response(desired_size, egui::Sense::click());
let (rect, response) = ui.allocate_exact_size(desired_size, egui::Sense::click());
*on ^= response.clicked; // toggle if clicked
let how_on = ui.ctx().animate_bool(response.id, *on);
@@ -70,7 +69,6 @@ fn toggle_compact(ui: &mut egui::Ui, on: &mut bool) -> egui::Response {
let off_bg_fill = egui::Rgba::TRANSPARENT;
let on_bg_fill = egui::Rgba::from_rgb(0.0, 0.5, 0.25);
let bg_fill = egui::lerp(off_bg_fill..=on_bg_fill, how_on);
let rect = response.rect;
let radius = 0.5 * rect.height();
ui.painter().rect(rect, radius, bg_fill, visuals.bg_stroke);
let circle_x = egui::lerp((rect.left() + radius)..=(rect.right() - radius), how_on);

View File

@@ -66,8 +66,7 @@ impl FrameHistory {
// TODO: we should not use `slider_width` as default graph width.
let height = ui.style().spacing.slider_width;
let size = vec2(ui.available_size_before_wrap_finite().x, height);
let response = ui.allocate_response(size, Sense::hover());
let rect = response.rect;
let (rect, response) = ui.allocate_at_least(size, Sense::hover());
let style = ui.style().noninteractive();
let mut cmds = vec![PaintCmd::Rect {