@@ -159,8 +159,20 @@ pub fn create_layout(
159
159
builder. add_text ( text) ;
160
160
}
161
161
162
+ let no_wrap = wrap == items:: TextWrap :: NoWrap || overflow == items:: TextOverflow :: Elide ;
163
+
162
164
let mut paragraph = builder. build ( ) ;
163
- paragraph. layout ( max_width. map_or ( f32:: MAX , |physical_width| physical_width. get ( ) ) ) ;
165
+ paragraph. layout (
166
+ max_width. filter ( |_| !no_wrap) . map_or ( f32:: MAX , |physical_width| physical_width. get ( ) ) ,
167
+ ) ;
168
+
169
+ // Layouting out with f32::max when wrapping is disabled, causes an overflow and breaks alignment compensation. Lay out again just wide enough
170
+ // to find the largest unwrapped line.
171
+ if no_wrap
172
+ && matches ! ( h_align, TextHorizontalAlignment :: Right | TextHorizontalAlignment :: Center )
173
+ {
174
+ paragraph. layout ( paragraph. longest_line ( ) + 1. ) ;
175
+ }
164
176
165
177
let layout_height = PhysicalLength :: new ( paragraph. height ( ) ) ;
166
178
@@ -170,7 +182,25 @@ pub fn create_layout(
170
182
i_slint_core:: items:: TextVerticalAlignment :: Bottom => max_height - layout_height,
171
183
} ;
172
184
173
- ( paragraph, PhysicalPoint :: from_lengths ( Default :: default ( ) , layout_top_y) )
185
+ let layout_top_x = if no_wrap {
186
+ // With no wrapping, the alignment is done against the layout width that's larger than the available width. Compensate for that
187
+ // by shifting rendering.
188
+ match h_align {
189
+ TextHorizontalAlignment :: Left => PhysicalLength :: zero ( ) ,
190
+ TextHorizontalAlignment :: Center => {
191
+ let available_width = max_width. unwrap_or ( PhysicalLength :: new ( f32:: MAX ) ) ;
192
+ ( PhysicalLength :: new ( -paragraph. max_width ( ) ) + available_width) / 2.
193
+ }
194
+ TextHorizontalAlignment :: Right => {
195
+ let available_width = max_width. unwrap_or ( PhysicalLength :: new ( f32:: MAX ) ) ;
196
+ PhysicalLength :: new ( -paragraph. max_width ( ) ) + available_width
197
+ }
198
+ }
199
+ } else {
200
+ PhysicalLength :: zero ( )
201
+ } ;
202
+
203
+ ( paragraph, PhysicalPoint :: from_lengths ( layout_top_x, layout_top_y) )
174
204
}
175
205
176
206
pub fn font_metrics (
0 commit comments