Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Response::intrinsic_size to enable better layout in 3rd party crates #5082

Merged
merged 2 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/egui/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,7 @@ impl Context {
is_pointer_button_down_on: false,
interact_pointer_pos: None,
changed: false,
intrinsic_size: None,
};

self.write(|ctx| {
Expand Down
6 changes: 6 additions & 0 deletions crates/egui/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ pub struct Response {
/// Always `false` for something like a [`Button`](crate::Button).
#[doc(hidden)]
pub changed: bool,

/// The intrinsic / desired size of the widget.
/// For a button, this will be the size of the label + the frames padding,
/// even if the button is laid out in a justified layout and the actual size will be larger.
pub intrinsic_size: Option<Vec2>,
}

impl Response {
Expand Down Expand Up @@ -1137,6 +1142,7 @@ impl Response {
|| other.is_pointer_button_down_on,
interact_pointer_pos: self.interact_pointer_pos.or(other.interact_pointer_pos),
changed: self.changed || other.changed,
intrinsic_size: None,
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion crates/egui/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,9 @@ impl Ui {
/// ```
pub fn allocate_response(&mut self, desired_size: Vec2, sense: Sense) -> Response {
let (id, rect) = self.allocate_space(desired_size);
self.interact(rect, id, sense)
let mut response = self.interact(rect, id, sense);
response.intrinsic_size = Some(desired_size);
response
}

/// Returns a [`Rect`] with exactly what you asked for.
Expand Down
Loading