@@ -16,6 +16,7 @@ use crate::shelf_preview::ShelfPreview;
1616use crate :: shelf_view:: ShelfView ;
1717use crate :: theme:: Tokens ;
1818use crate :: thumbs:: Thumbs ;
19+ use crate :: tile_label:: TileLabel ;
1920use crate :: widgets:: dnd_release;
2021
2122/// The gap between tiles in points (their sizes are [`ShelfView`]'s)
@@ -546,7 +547,7 @@ fn entry_tile(app: &mut TextureCraftApp, ui: &mut Ui, ctx: &egui::Context, entry
546547 ShelfEntry :: SavedBrush { path : Some ( path) , .. } => Some ( thumbs. saved_brush_preview ( path, & session. images ) ) ,
547548 _ => entry. preview ( session) ,
548549 } ,
549- entry. label ( ) ,
550+ entry. tile_label ( ) ,
550551 asset. as_ref ( ) ,
551552 view,
552553 ) ;
@@ -585,22 +586,39 @@ pub(crate) fn tile(
585586 label : & str ,
586587 drag : Option < & Asset > ,
587588) -> egui:: Response {
588- tile_in_view ( ui, thumbs, texture, |_| Some ( preview ( ) ) , label, drag, ShelfView :: Medium )
589+ tile_in_view ( ui, thumbs, texture, |_| Some ( preview ( ) ) , TileLabel :: one_line ( label) , drag, ShelfView :: Medium )
589590}
590591
591- /// [`tile`] at `view`'s size: a grid tile with the name under the thumbnail, or a list row with it beside; `preview` runs on hover
592+ /// [`tile`] for an environment, whose name may wrap onto two lines ("Studio Small 08" and "Studio Small 03" differ at the end)
593+ pub ( crate ) fn environment_tile (
594+ ui : & mut Ui ,
595+ thumbs : & mut Thumbs ,
596+ texture : impl FnOnce ( & Ui , & mut Thumbs ) -> Option < egui:: TextureHandle > ,
597+ preview : impl FnOnce ( ) -> ShelfPreview ,
598+ label : & str ,
599+ ) -> egui:: Response {
600+ tile_in_view ( ui, thumbs, texture, |_| Some ( preview ( ) ) , TileLabel :: two_lines ( label) , None , ShelfView :: Medium )
601+ }
602+
603+ /// [`tile`] at `view`'s size: a grid tile with the name under the thumbnail (taller per extra line), or a list row with it beside
592604fn tile_in_view (
593605 ui : & mut Ui ,
594606 thumbs : & mut Thumbs ,
595607 texture : impl FnOnce ( & Ui , & mut Thumbs ) -> Option < egui:: TextureHandle > ,
596608 preview : impl FnOnce ( & mut Thumbs ) -> Option < ShelfPreview > ,
597- label : & str ,
609+ label : TileLabel < ' _ > ,
598610 drag : Option < & Asset > ,
599611 view : ShelfView ,
600612) -> egui:: Response {
601613 let tokens = Tokens :: get ( ui. ctx ( ) ) ;
602614 let sense = if drag. is_some ( ) { Sense :: click_and_drag ( ) } else { Sense :: click ( ) } ;
603- let ( rect, response) = ui. allocate_exact_size ( view. tile_size ( ui. available_width ( ) ) , sense) ;
615+ let TileLabel { text : label, lines } = label;
616+ let label_lines = if view. name_beside ( ) { 1 } else { lines. max ( 1 ) } ;
617+ let extra_lines = label_lines - 1 ;
618+ let extra_height =
619+ if extra_lines > 0 { ui. ctx ( ) . fonts_mut ( |fonts| fonts. row_height ( & egui:: FontId :: proportional ( LABEL_FONT_SIZE ) ) ) * extra_lines as f32 } else { 0.0 } ;
620+ let size = view. tile_size ( ui. available_width ( ) ) + egui:: vec2 ( 0.0 , extra_height) ;
621+ let ( rect, response) = ui. allocate_exact_size ( size, sense) ;
604622 response. widget_info ( || egui:: WidgetInfo :: labeled ( egui:: Role :: Button , true , label) ) ;
605623 let texture = if ui. is_rect_visible ( rect) { texture ( ui, thumbs) } else { None } ;
606624 if let Some ( asset) = drag
@@ -618,11 +636,13 @@ fn tile_in_view(
618636 thumbnail ( painter, texture. as_ref ( ) , image_rect, & tokens) ;
619637 if view. name_beside ( ) {
620638 let left = image_rect. right ( ) + crate :: shelf_view:: LIST_NAME_GAP ;
621- let galley = painter. layout_job ( one_line ( label, rect. right ( ) - left - LABEL_MARGIN , tokens. text ) ) ;
639+ let galley = painter. layout_job ( label_job ( label, rect. right ( ) - left - LABEL_MARGIN , 1 , tokens. text ) ) ;
622640 painter. galley ( egui:: pos2 ( left, rect. center ( ) . y - galley. size ( ) . y * 0.5 ) , galley, tokens. text ) ;
623641 } else {
624- let galley = painter. layout_job ( one_line ( label, rect. width ( ) - LABEL_MARGIN , tokens. text ) ) ;
625- painter. galley ( egui:: pos2 ( rect. center ( ) . x - galley. size ( ) . x * 0.5 , image_rect. bottom ( ) + LABEL_GAP ) , galley, tokens. text ) ;
642+ let galley = painter. layout_job ( label_job ( label, rect. width ( ) - LABEL_MARGIN , label_lines, tokens. text ) ) ;
643+ // A centred job's rows sit around x = 0, so its rect may start left of it
644+ let left = rect. center ( ) . x - galley. size ( ) . x * 0.5 - galley. rect . min . x ;
645+ painter. galley ( egui:: pos2 ( left, image_rect. bottom ( ) + LABEL_GAP ) , galley, tokens. text ) ;
626646 }
627647
628648 let big = if response. hovered ( ) { preview ( thumbs) . and_then ( |preview| big_preview ( ui, thumbs, || preview) ) } else { None } ;
@@ -637,10 +657,13 @@ fn randomness_badge(painter: &egui::Painter, thumbnail: egui::Rect) {
637657 }
638658}
639659
640- /// A tile label on one line, cut with "…" past `width` points (the hover popup shows the whole name)
641- fn one_line ( label : & str , width : f32 , color : Color32 ) -> egui:: text:: LayoutJob {
660+ /// A tile label on up to `lines` lines (centred when more than one) of `width` points, the last cut with "…" (the hover popup shows the whole name)
661+ fn label_job ( label : & str , width : f32 , lines : usize , color : Color32 ) -> egui:: text:: LayoutJob {
642662 let mut job = egui:: text:: LayoutJob :: single_section ( label. to_string ( ) , egui:: TextFormat :: simple ( egui:: FontId :: proportional ( LABEL_FONT_SIZE ) , color) ) ;
643- job. wrap = egui:: text:: TextWrapping :: truncate_at_width ( width) ;
663+ job. wrap = egui:: text:: TextWrapping { max_rows : lines, ..egui:: text:: TextWrapping :: truncate_at_width ( width) } ;
664+ if lines > 1 {
665+ job. halign = egui:: Align :: Center ;
666+ }
644667 job
645668}
646669
@@ -788,7 +811,7 @@ mod tests {
788811 let width = 60.0 ;
789812 let mut laid_out = None ;
790813 let mut output = egui:: Context :: default ( ) . run_ui ( egui:: RawInput :: default ( ) , |ui| {
791- laid_out = Some ( ui. painter ( ) . layout_job ( one_line ( "Steel Painted Rust Leaks Extra" , width, Color32 :: WHITE ) ) ) ;
814+ laid_out = Some ( ui. painter ( ) . layout_job ( label_job ( "Steel Painted Rust Leaks Extra" , width, 1 , Color32 :: WHITE ) ) ) ;
792815 } ) ;
793816
794817 // The font atlas upload is not under test
@@ -797,9 +820,30 @@ mod tests {
797820
798821 assert_eq ! ( galley. rows. len( ) , 1 ) ;
799822 assert ! ( galley. size( ) . x <= width) ;
823+ assert ! ( galley. rect. min. x >= 0.0 , "a one-line label starts where it is painted, as list rows expect" ) ;
800824 assert_eq ! ( galley. rows[ 0 ] . glyphs. last( ) . map( |glyph| glyph. chr) , Some ( '…' ) ) ;
801825 }
802826
827+ /// Environment names that differ only at the end ("Studio Small 08", "Studio Small 03") keep that end on a second line
828+ #[ test]
829+ fn an_environment_tile_label_wraps_instead_of_cutting_its_end ( ) {
830+ let width = ShelfView :: Medium . tile_size ( 0.0 ) . x - LABEL_MARGIN ;
831+ let mut laid_out = None ;
832+ let mut output = egui:: Context :: default ( ) . run_ui ( egui:: RawInput :: default ( ) , |ui| {
833+ let label = TileLabel :: two_lines ( "Studio Small 08" ) ;
834+ laid_out = Some ( ui. painter ( ) . layout_job ( label_job ( label. text , width, label. lines , Color32 :: WHITE ) ) ) ;
835+ } ) ;
836+
837+ // The font atlas upload is not under test
838+ output. textures_delta . clear ( ) ;
839+ let galley = laid_out. unwrap ( ) ;
840+
841+ assert ! ( galley. rows. len( ) > 1 ) ;
842+ assert ! ( galley. size( ) . x <= width) ;
843+ let shown: String = galley. rows . iter ( ) . flat_map ( |row| row. glyphs . iter ( ) . map ( |glyph| glyph. chr ) ) . collect ( ) ;
844+ assert ! ( shown. ends_with( "08" ) , "{shown}" ) ;
845+ }
846+
803847 /// Saved smart materials in a location added in Settings > Libraries are listed with the library folder's
804848 #[ cfg( not( target_arch = "wasm32" ) ) ]
805849 #[ test]
0 commit comments