parent
9478a6223b
commit
14c2e5d3d5
|
|
@ -59,8 +59,8 @@ pub struct Response {
|
||||||
|
|
||||||
/// The intrinsic / desired size of the widget.
|
/// The intrinsic / desired size of the widget.
|
||||||
///
|
///
|
||||||
/// For a button, this will be the size of the label + the frames padding,
|
/// This is the size that a non-wrapped, non-truncated, non-justified version of the widget
|
||||||
/// even if the button is laid out in a justified layout and the actual size will be larger.
|
/// would have.
|
||||||
///
|
///
|
||||||
/// If this is `None`, use [`Self::rect`] instead.
|
/// If this is `None`, use [`Self::rect`] instead.
|
||||||
///
|
///
|
||||||
|
|
|
||||||
|
|
@ -220,6 +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());
|
||||||
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);
|
||||||
|
|
@ -252,7 +253,8 @@ impl Label {
|
||||||
};
|
};
|
||||||
|
|
||||||
let galley = ui.fonts(|fonts| fonts.layout_job(layout_job));
|
let galley = ui.fonts(|fonts| fonts.layout_job(layout_job));
|
||||||
let (rect, 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());
|
||||||
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(),
|
||||||
|
|
|
||||||
|
|
@ -72,32 +72,39 @@ fn single_test(name: &str, mut f: impl FnMut(&mut Ui)) -> SnapshotResult {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_intrinsic_size() {
|
fn test_intrinsic_size() {
|
||||||
let mut intrinsic_size = None;
|
let widgets = [Ui::button, Ui::label];
|
||||||
for wrapping in [
|
|
||||||
TextWrapMode::Extend,
|
for widget in widgets {
|
||||||
TextWrapMode::Wrap,
|
let mut intrinsic_size = None;
|
||||||
TextWrapMode::Truncate,
|
for wrapping in [
|
||||||
] {
|
TextWrapMode::Extend,
|
||||||
_ = HarnessBuilder::default()
|
TextWrapMode::Wrap,
|
||||||
.with_size(Vec2::new(100.0, 100.0))
|
TextWrapMode::Truncate,
|
||||||
.build_ui(|ui| {
|
] {
|
||||||
ui.style_mut().wrap_mode = Some(wrapping);
|
_ = HarnessBuilder::default()
|
||||||
let response = ui.add(Button::new(
|
.with_size(Vec2::new(100.0, 100.0))
|
||||||
"Hello world this is a long text that should be wrapped.",
|
.build_ui(|ui| {
|
||||||
));
|
ui.style_mut().wrap_mode = Some(wrapping);
|
||||||
if let Some(current_intrinsic_size) = intrinsic_size {
|
let response = widget(
|
||||||
assert_eq!(
|
ui,
|
||||||
Some(current_intrinsic_size),
|
"Hello world this is a long text that should be wrapped.",
|
||||||
response.intrinsic_size,
|
|
||||||
"For wrapping: {wrapping:?}"
|
|
||||||
);
|
);
|
||||||
}
|
if let Some(current_intrinsic_size) = intrinsic_size {
|
||||||
assert!(
|
assert_eq!(
|
||||||
response.intrinsic_size.is_some(),
|
Some(current_intrinsic_size),
|
||||||
"intrinsic_size should be set for `Button`"
|
response.intrinsic_size,
|
||||||
);
|
"For wrapping: {wrapping:?}"
|
||||||
intrinsic_size = response.intrinsic_size;
|
);
|
||||||
});
|
}
|
||||||
|
assert!(
|
||||||
|
response.intrinsic_size.is_some(),
|
||||||
|
"intrinsic_size should be set for `Button`"
|
||||||
|
);
|
||||||
|
intrinsic_size = response.intrinsic_size;
|
||||||
|
if wrapping == TextWrapMode::Extend {
|
||||||
|
assert_eq!(Some(response.rect.size()), response.intrinsic_size);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
assert_eq!(intrinsic_size.unwrap().round(), Vec2::new(305.0, 18.0));
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue