@@ -27,13 +27,26 @@ fn scale_value(value: f32, factor: f64) -> f32 {
2727
2828/// Defines how min_size, size, and max_size affects the bounds of a text
2929/// block.
30- pub fn text_constraint ( min_size : Val , size : Val , max_size : Val , scale_factor : f64 ) -> f32 {
31- // Needs support for percentages
30+ pub fn text_constraint (
31+ min_size : Val ,
32+ size : Val ,
33+ max_size : Val ,
34+ scale_factor : f64 ,
35+ constraint : f32 ,
36+ ) -> f32 {
3237 match ( min_size, size, max_size) {
3338 ( _, _, Val :: Px ( max) ) => scale_value ( max, scale_factor) ,
39+ ( _, _, Val :: Percent ( max) ) => scale_value ( ( max / 100. ) * constraint, scale_factor) ,
3440 ( Val :: Px ( min) , _, _) => scale_value ( min, scale_factor) ,
41+ ( Val :: Percent ( min) , _, _) => scale_value ( ( min / 100. ) * constraint, scale_factor) ,
3542 ( Val :: Undefined , Val :: Px ( size) , Val :: Undefined ) => scale_value ( size, scale_factor) ,
3643 ( Val :: Auto , Val :: Px ( size) , Val :: Auto ) => scale_value ( size, scale_factor) ,
44+ ( Val :: Undefined , Val :: Percent ( size) , Val :: Undefined ) => {
45+ scale_value ( ( size / 100. ) * constraint, scale_factor)
46+ }
47+ ( Val :: Auto , Val :: Percent ( size) , Val :: Auto ) => {
48+ scale_value ( ( size / 100. ) * constraint, scale_factor)
49+ }
3750 _ => f32:: MAX ,
3851 }
3952}
@@ -54,11 +67,12 @@ pub fn text_system(
5467 Query < ( & Text , & Style , & mut CalculatedSize ) > ,
5568 ) > ,
5669) {
57- let scale_factor = if let Some ( window) = windows. get_primary ( ) {
58- window. scale_factor ( )
59- } else {
60- 1.
61- } ;
70+ let ( scale_factor, width_constraint, height_constraint) =
71+ if let Some ( window) = windows. get_primary ( ) {
72+ ( window. scale_factor ( ) , window. width ( ) , window. height ( ) )
73+ } else {
74+ ( 1. , f32:: MAX , f32:: MAX )
75+ } ;
6276
6377 let inv_scale_factor = 1. / scale_factor;
6478
@@ -82,12 +96,14 @@ pub fn text_system(
8296 style. size . width ,
8397 style. max_size . width ,
8498 scale_factor,
99+ width_constraint,
85100 ) ,
86101 text_constraint (
87102 style. min_size . height ,
88103 style. size . height ,
89104 style. max_size . height ,
90105 scale_factor,
106+ height_constraint,
91107 ) ,
92108 ) ;
93109
@@ -166,3 +182,71 @@ pub fn draw_text_system(
166182 }
167183 }
168184}
185+
186+ #[ cfg( test) ]
187+ mod tests {
188+ use super :: text_constraint;
189+ use crate :: Val ;
190+
191+ #[ test]
192+ fn should_constrain_based_on_pixel_values ( ) {
193+ assert_eq ! (
194+ text_constraint( Val :: Px ( 100. ) , Val :: Undefined , Val :: Undefined , 1. , 1. ) ,
195+ 100.
196+ ) ;
197+ assert_eq ! (
198+ text_constraint( Val :: Undefined , Val :: Undefined , Val :: Px ( 100. ) , 1. , 1. ) ,
199+ 100.
200+ ) ;
201+ assert_eq ! (
202+ text_constraint( Val :: Undefined , Val :: Px ( 100. ) , Val :: Undefined , 1. , 1. ) ,
203+ 100.
204+ ) ;
205+ }
206+
207+ #[ test]
208+ fn should_constrain_based_on_percent_values ( ) {
209+ assert_eq ! (
210+ text_constraint( Val :: Percent ( 33. ) , Val :: Undefined , Val :: Undefined , 1. , 1000. ) ,
211+ 330.
212+ ) ;
213+ assert_eq ! (
214+ text_constraint( Val :: Undefined , Val :: Undefined , Val :: Percent ( 33. ) , 1. , 1000. ) ,
215+ 330.
216+ ) ;
217+ assert_eq ! (
218+ text_constraint( Val :: Undefined , Val :: Percent ( 33. ) , Val :: Undefined , 1. , 1000. ) ,
219+ 330.
220+ ) ;
221+ }
222+
223+ #[ test]
224+ fn should_ignore_min_if_max_is_given ( ) {
225+ assert_eq ! (
226+ text_constraint(
227+ Val :: Percent ( 33. ) ,
228+ Val :: Undefined ,
229+ Val :: Percent ( 50. ) ,
230+ 1. ,
231+ 1000.
232+ ) ,
233+ 500. ,
234+ "min in percent and max in percent"
235+ ) ;
236+ assert_eq ! (
237+ text_constraint( Val :: Px ( 33. ) , Val :: Undefined , Val :: Px ( 50. ) , 1. , 1000. ) ,
238+ 50. ,
239+ "min in px and max in px"
240+ ) ;
241+ assert_eq ! (
242+ text_constraint( Val :: Px ( 33. ) , Val :: Undefined , Val :: Percent ( 50. ) , 1. , 1000. ) ,
243+ 500. ,
244+ "min in px and max in percent"
245+ ) ;
246+ assert_eq ! (
247+ text_constraint( Val :: Percent ( 33. ) , Val :: Undefined , Val :: Px ( 50. ) , 1. , 1000. ) ,
248+ 50. ,
249+ "min in percent and max in px"
250+ ) ;
251+ }
252+ }
0 commit comments