Skip to content

Commit

Permalink
ui: draw_text_txn() avoids overrunning grant bounds.
Browse files Browse the repository at this point in the history
I'm not sure exactly why this came up, and further tests would be wise,
but this will at least turn a panic into a truncation (and is desirable
anyway once we have fixed size text boxes that should truncate their
contents, though those will probably use truncation built into `Text`
instead of whole blocks).
  • Loading branch information
kpreid committed Dec 5, 2023
1 parent 5d00436 commit 575c84e
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions all-is-cubes-ui/src/vui/widgets/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use alloc::sync::Arc;

use all_is_cubes::arcstr::ArcStr;
use all_is_cubes::block::text::{self, Text as BlockText};
use all_is_cubes::block::Resolution::*;
use all_is_cubes::block::{self, Resolution::*};
use all_is_cubes::drawing::embedded_graphics::{
mono_font::{MonoFont, MonoTextStyle},
prelude::{Dimensions, Point},
Expand All @@ -11,7 +11,7 @@ use all_is_cubes::drawing::embedded_graphics::{
};
use all_is_cubes::drawing::{rectangle_to_aab, VoxelBrush};
use all_is_cubes::math::{GridAab, Gridgid};
use all_is_cubes::space::SpaceTransaction;
use all_is_cubes::space::{CubeTransaction, SpaceTransaction};

use crate::vui::{self, widgets, LayoutGrant, LayoutRequest, Layoutable, Widget, WidgetController};

Expand Down Expand Up @@ -160,9 +160,22 @@ fn text_for_widget(text: ArcStr, font: text::Font, gravity: vui::Gravity) -> tex
fn draw_text_txn(text: &BlockText, grant: &LayoutGrant) -> SpaceTransaction {
let text_aabb = text.bounding_blocks();
let grant = grant.shrink_to(text_aabb.size(), true);
text.installation(
Gridgid::from_translation(grant.bounds.lower_bounds() - text_aabb.lower_bounds()),
core::convert::identity,
let translation = grant.bounds.lower_bounds() - text_aabb.lower_bounds();

// This is like `BlockText::installation()` but if the text ends up too big it is truncated.
// TODO: But it shouldn't necessarily be truncated to the lower-left corner which is what
// our choice of translation calculation is doing
SpaceTransaction::filling(
grant.bounds, // doesn't over-fill because it's been shrunk
|cube| {
CubeTransaction::replacing(
None,
Some(block::Block::from_primitive(block::Primitive::Text {
text: text.clone(),
offset: cube.lower_bounds().to_vector() - translation,
})),
)
},
)
}

Expand Down Expand Up @@ -197,7 +210,7 @@ mod tests {
fn label_layout() {
let tree: vui::WidgetTree = vui::LayoutTree::leaf(Arc::new(Label::new(literal!("hi"))));

// to_space() serves as a transaction sanity check. TODO: make a proper tester
// to_space() serves as a widget building sanity check. TODO: make a proper widget tester
tree.to_space(
SpaceBuilder::default().physics(SpacePhysics::DEFAULT_FOR_BLOCK),
vui::Gravity::new(vui::Align::Center, vui::Align::Center, vui::Align::Low),
Expand Down

0 comments on commit 575c84e

Please sign in to comment.