mirror of
https://github.com/emilk/egui.git
synced 2026-09-02 14:50:03 -04:00
Shrink the byte-size of Response slightly (#8011)
Small optimization! 96 -> 88 bytes, so no huge win.
This commit is contained in:
@@ -318,8 +318,9 @@ impl<'a> AtomLayout<'a> {
|
|||||||
let (_, rect) = ui.allocate_space(frame_size);
|
let (_, rect) = ui.allocate_space(frame_size);
|
||||||
let mut response = ui.interact(rect, id, sense);
|
let mut response = ui.interact(rect, id, sense);
|
||||||
|
|
||||||
response.intrinsic_size =
|
response.set_intrinsic_size(
|
||||||
Some((Vec2::new(intrinsic_width, intrinsic_height) + margin.sum()).at_least(min_size));
|
(Vec2::new(intrinsic_width, intrinsic_height) + margin.sum()).at_least(min_size),
|
||||||
|
);
|
||||||
|
|
||||||
AllocatedAtomLayout {
|
AllocatedAtomLayout {
|
||||||
sized_atoms: sized_items,
|
sized_atoms: sized_items,
|
||||||
|
|||||||
@@ -1378,8 +1378,8 @@ impl Context {
|
|||||||
interact_rect,
|
interact_rect,
|
||||||
sense,
|
sense,
|
||||||
flags: Flags::empty(),
|
flags: Flags::empty(),
|
||||||
interact_pointer_pos: None,
|
interact_pointer_pos_or_nan: Pos2::NAN,
|
||||||
intrinsic_size: None,
|
intrinsic_size_or_nan: Vec2::NAN,
|
||||||
};
|
};
|
||||||
|
|
||||||
res.flags.set(Flags::ENABLED, enabled);
|
res.flags.set(Flags::ENABLED, enabled);
|
||||||
@@ -1470,14 +1470,11 @@ impl Context {
|
|||||||
|| res.long_touched()
|
|| res.long_touched()
|
||||||
|| clicked
|
|| clicked
|
||||||
|| res.drag_stopped();
|
|| res.drag_stopped();
|
||||||
if is_interacted_with {
|
if is_interacted_with && let Some(mut pos) = input.pointer.interact_pos() {
|
||||||
res.interact_pointer_pos = input.pointer.interact_pos();
|
if let Some(to_global) = memory.to_global.get(&res.layer_id) {
|
||||||
if let (Some(to_global), Some(pos)) = (
|
pos = to_global.inverse() * pos;
|
||||||
memory.to_global.get(&res.layer_id),
|
|
||||||
&mut res.interact_pointer_pos,
|
|
||||||
) {
|
|
||||||
*pos = to_global.inverse() * *pos;
|
|
||||||
}
|
}
|
||||||
|
res.interact_pointer_pos_or_nan = pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
if input.pointer.any_down() && !is_interacted_with {
|
if input.pointer.any_down() && !is_interacted_with {
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ pub struct Response {
|
|||||||
/// Where the pointer (mouse/touch) were when this widget was clicked or dragged.
|
/// Where the pointer (mouse/touch) were when this widget was clicked or dragged.
|
||||||
/// `None` if the widget is not being interacted with.
|
/// `None` if the widget is not being interacted with.
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub interact_pointer_pos: Option<Pos2>,
|
pub interact_pointer_pos_or_nan: Pos2,
|
||||||
|
|
||||||
/// The intrinsic / desired size of the widget.
|
/// The intrinsic / desired size of the widget.
|
||||||
///
|
///
|
||||||
@@ -67,12 +67,22 @@ pub struct Response {
|
|||||||
/// At the time of writing, this is only used by external crates
|
/// At the time of writing, this is only used by external crates
|
||||||
/// for improved layouting.
|
/// for improved layouting.
|
||||||
/// See for instance [`egui_flex`](https://github.com/lucasmerlin/hello_egui/tree/main/crates/egui_flex).
|
/// See for instance [`egui_flex`](https://github.com/lucasmerlin/hello_egui/tree/main/crates/egui_flex).
|
||||||
pub intrinsic_size: Option<Vec2>,
|
#[doc(hidden)]
|
||||||
|
pub intrinsic_size_or_nan: Vec2,
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub flags: Flags,
|
pub flags: Flags,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_response_size() {
|
||||||
|
assert_eq!(
|
||||||
|
std::mem::size_of::<Response>(),
|
||||||
|
88,
|
||||||
|
"Keep Response small, because we create them often, and we want to keep it lean and fast"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/// A bit set for various boolean properties of `Response`.
|
/// A bit set for various boolean properties of `Response`.
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
#[derive(Copy, Clone, Debug)]
|
#[derive(Copy, Clone, Debug)]
|
||||||
@@ -489,7 +499,26 @@ impl Response {
|
|||||||
/// `None` if the widget is not being interacted with.
|
/// `None` if the widget is not being interacted with.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn interact_pointer_pos(&self) -> Option<Pos2> {
|
pub fn interact_pointer_pos(&self) -> Option<Pos2> {
|
||||||
self.interact_pointer_pos
|
let pos = self.interact_pointer_pos_or_nan;
|
||||||
|
if pos.any_nan() { None } else { Some(pos) }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The intrinsic / desired size of the widget.
|
||||||
|
///
|
||||||
|
/// This is the size that a non-wrapped, non-truncated, non-justified version of the widget
|
||||||
|
/// would have.
|
||||||
|
///
|
||||||
|
/// If this is `None`, use [`Self::rect`] instead.
|
||||||
|
#[inline]
|
||||||
|
pub fn intrinsic_size(&self) -> Option<Vec2> {
|
||||||
|
let size = self.intrinsic_size_or_nan;
|
||||||
|
if size.any_nan() { None } else { Some(size) }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set the intrinsic / desired size of the widget.
|
||||||
|
#[inline]
|
||||||
|
pub fn set_intrinsic_size(&mut self, size: Vec2) {
|
||||||
|
self.intrinsic_size_or_nan = size;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// If it is a good idea to show a tooltip, where is pointer?
|
/// If it is a good idea to show a tooltip, where is pointer?
|
||||||
@@ -1007,8 +1036,10 @@ impl Response {
|
|||||||
interact_rect: self.interact_rect.union(other.interact_rect),
|
interact_rect: self.interact_rect.union(other.interact_rect),
|
||||||
sense: self.sense.union(other.sense),
|
sense: self.sense.union(other.sense),
|
||||||
flags: self.flags | other.flags,
|
flags: self.flags | other.flags,
|
||||||
interact_pointer_pos: self.interact_pointer_pos.or(other.interact_pointer_pos),
|
interact_pointer_pos_or_nan: self
|
||||||
intrinsic_size: None,
|
.interact_pointer_pos()
|
||||||
|
.unwrap_or(other.interact_pointer_pos_or_nan),
|
||||||
|
intrinsic_size_or_nan: Vec2::NAN,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1281,7 +1281,7 @@ impl Ui {
|
|||||||
pub fn allocate_response(&mut self, desired_size: Vec2, sense: Sense) -> Response {
|
pub fn allocate_response(&mut self, desired_size: Vec2, sense: Sense) -> Response {
|
||||||
let (id, rect) = self.allocate_space(desired_size);
|
let (id, rect) = self.allocate_space(desired_size);
|
||||||
let mut response = self.interact(rect, id, sense);
|
let mut response = self.interact(rect, id, sense);
|
||||||
response.intrinsic_size = Some(desired_size);
|
response.set_intrinsic_size(desired_size);
|
||||||
response
|
response
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -220,7 +220,7 @@ impl Label {
|
|||||||
.rect_without_leading_space()
|
.rect_without_leading_space()
|
||||||
.translate(pos.to_vec2());
|
.translate(pos.to_vec2());
|
||||||
let mut response = ui.allocate_rect(rect, sense);
|
let mut response = ui.allocate_rect(rect, sense);
|
||||||
response.intrinsic_size = Some(galley.intrinsic_size());
|
response.set_intrinsic_size(galley.intrinsic_size());
|
||||||
for placed_row in galley.rows.iter().skip(1) {
|
for placed_row in galley.rows.iter().skip(1) {
|
||||||
let rect = placed_row.rect().translate(pos.to_vec2());
|
let rect = placed_row.rect().translate(pos.to_vec2());
|
||||||
response |= ui.allocate_rect(rect, sense);
|
response |= ui.allocate_rect(rect, sense);
|
||||||
@@ -256,7 +256,7 @@ impl Label {
|
|||||||
|
|
||||||
let galley = ui.fonts_mut(|fonts| fonts.layout_job(layout_job));
|
let galley = ui.fonts_mut(|fonts| fonts.layout_job(layout_job));
|
||||||
let (rect, mut response) = ui.allocate_exact_size(galley.size(), sense);
|
let (rect, mut response) = ui.allocate_exact_size(galley.size(), sense);
|
||||||
response.intrinsic_size = Some(galley.intrinsic_size());
|
response.set_intrinsic_size(galley.intrinsic_size());
|
||||||
let galley_pos = match galley.job.halign {
|
let galley_pos = match galley.job.halign {
|
||||||
Align::LEFT => rect.left_top(),
|
Align::LEFT => rect.left_top(),
|
||||||
Align::Center => rect.center_top(),
|
Align::Center => rect.center_top(),
|
||||||
|
|||||||
@@ -119,6 +119,11 @@ impl Pos2 {
|
|||||||
/// Same as `Pos2::default()`.
|
/// Same as `Pos2::default()`.
|
||||||
pub const ZERO: Self = Self { x: 0.0, y: 0.0 };
|
pub const ZERO: Self = Self { x: 0.0, y: 0.0 };
|
||||||
|
|
||||||
|
pub const NAN: Self = Self {
|
||||||
|
x: f32::NAN,
|
||||||
|
y: f32::NAN,
|
||||||
|
};
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub const fn new(x: f32, y: f32) -> Self {
|
pub const fn new(x: f32, y: f32) -> Self {
|
||||||
Self { x, y }
|
Self { x, y }
|
||||||
|
|||||||
@@ -92,17 +92,17 @@ fn test_intrinsic_size() {
|
|||||||
if let Some(current_intrinsic_size) = intrinsic_size {
|
if let Some(current_intrinsic_size) = intrinsic_size {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
Some(current_intrinsic_size),
|
Some(current_intrinsic_size),
|
||||||
response.intrinsic_size,
|
response.intrinsic_size(),
|
||||||
"For wrapping: {wrapping:?}"
|
"For wrapping: {wrapping:?}"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
assert!(
|
assert!(
|
||||||
response.intrinsic_size.is_some(),
|
response.intrinsic_size().is_some(),
|
||||||
"intrinsic_size should be set for `Button`"
|
"intrinsic_size should be set for `Button`"
|
||||||
);
|
);
|
||||||
intrinsic_size = response.intrinsic_size;
|
intrinsic_size = response.intrinsic_size();
|
||||||
if wrapping == TextWrapMode::Extend {
|
if wrapping == TextWrapMode::Extend {
|
||||||
assert_eq!(Some(response.rect.size()), response.intrinsic_size);
|
assert_eq!(Some(response.rect.size()), response.intrinsic_size());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user