@@ -99,7 +99,7 @@ pub struct LogicalPosition {
99
99
100
100
impl LogicalPosition {
101
101
#[ inline]
102
- pub fn new ( x : f64 , y : f64 ) -> Self {
102
+ pub const fn new ( x : f64 , y : f64 ) -> Self {
103
103
LogicalPosition { x, y }
104
104
}
105
105
@@ -160,7 +160,7 @@ pub struct PhysicalPosition {
160
160
161
161
impl PhysicalPosition {
162
162
#[ inline]
163
- pub fn new ( x : f64 , y : f64 ) -> Self {
163
+ pub const fn new ( x : f64 , y : f64 ) -> Self {
164
164
PhysicalPosition { x, y }
165
165
}
166
166
@@ -221,7 +221,7 @@ pub struct LogicalSize {
221
221
222
222
impl LogicalSize {
223
223
#[ inline]
224
- pub fn new ( width : f64 , height : f64 ) -> Self {
224
+ pub const fn new ( width : f64 , height : f64 ) -> Self {
225
225
LogicalSize { width, height }
226
226
}
227
227
@@ -235,7 +235,7 @@ impl LogicalSize {
235
235
assert ! ( validate_hidpi_factor( dpi_factor) ) ;
236
236
let width = self . width * dpi_factor;
237
237
let height = self . height * dpi_factor;
238
- PhysicalSize :: new ( width, height)
238
+ PhysicalSize :: new ( width. round ( ) as _ , height. round ( ) as _ )
239
239
}
240
240
}
241
241
@@ -269,20 +269,16 @@ impl Into<(u32, u32)> for LogicalSize {
269
269
}
270
270
271
271
/// A size represented in physical pixels.
272
- ///
273
- /// The size is stored as floats, so please be careful. Casting floats to integers truncates the fractional part,
274
- /// which can cause noticable issues. To help with that, an `Into<(u32, u32)>` implementation is provided which
275
- /// does the rounding for you.
276
- #[ derive( Debug , Copy , Clone , PartialEq ) ]
272
+ #[ derive( Debug , Copy , Clone , PartialEq , Eq , Hash ) ]
277
273
#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
278
274
pub struct PhysicalSize {
279
- pub width : f64 ,
280
- pub height : f64 ,
275
+ pub width : u32 ,
276
+ pub height : u32 ,
281
277
}
282
278
283
279
impl PhysicalSize {
284
280
#[ inline]
285
- pub fn new ( width : f64 , height : f64 ) -> Self {
281
+ pub const fn new ( width : u32 , height : u32 ) -> Self {
286
282
PhysicalSize { width, height }
287
283
}
288
284
@@ -294,37 +290,105 @@ impl PhysicalSize {
294
290
#[ inline]
295
291
pub fn to_logical ( & self , dpi_factor : f64 ) -> LogicalSize {
296
292
assert ! ( validate_hidpi_factor( dpi_factor) ) ;
297
- let width = self . width / dpi_factor;
298
- let height = self . height / dpi_factor;
293
+ let width = self . width as f64 / dpi_factor;
294
+ let height = self . height as f64 / dpi_factor;
299
295
LogicalSize :: new ( width, height)
300
296
}
301
297
}
302
298
303
- impl From < ( f64 , f64 ) > for PhysicalSize {
299
+ impl From < ( u32 , u32 ) > for PhysicalSize {
304
300
#[ inline]
305
- fn from ( ( width, height) : ( f64 , f64 ) ) -> Self {
301
+ fn from ( ( width, height) : ( u32 , u32 ) ) -> Self {
306
302
Self :: new ( width, height)
307
303
}
308
304
}
309
305
310
- impl From < ( u32 , u32 ) > for PhysicalSize {
306
+ impl Into < ( u32 , u32 ) > for PhysicalSize {
307
+ /// Note that this rounds instead of truncating.
311
308
#[ inline]
312
- fn from ( ( width , height ) : ( u32 , u32 ) ) -> Self {
313
- Self :: new ( width as f64 , height as f64 )
309
+ fn into ( self ) -> ( u32 , u32 ) {
310
+ ( self . width , self . height )
314
311
}
315
312
}
316
313
317
- impl Into < ( f64 , f64 ) > for PhysicalSize {
314
+ #[ derive( Debug , Copy , Clone , PartialEq ) ]
315
+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
316
+ pub enum Size {
317
+ Physical ( PhysicalSize ) ,
318
+ Logical ( LogicalSize ) ,
319
+ }
320
+
321
+ impl Size {
322
+ pub fn new < S : Into < Size > > ( size : S ) -> Size {
323
+ size. into ( )
324
+ }
325
+
326
+ pub fn to_logical ( & self , dpi_factor : f64 ) -> LogicalSize {
327
+ match * self {
328
+ Size :: Physical ( size) => size. to_logical ( dpi_factor) ,
329
+ Size :: Logical ( size) => size,
330
+ }
331
+ }
332
+
333
+ pub fn to_physical ( & self , dpi_factor : f64 ) -> PhysicalSize {
334
+ match * self {
335
+ Size :: Physical ( size) => size,
336
+ Size :: Logical ( size) => size. to_physical ( dpi_factor) ,
337
+ }
338
+ }
339
+ }
340
+
341
+ impl From < PhysicalSize > for Size {
318
342
#[ inline]
319
- fn into ( self ) -> ( f64 , f64 ) {
320
- ( self . width , self . height )
343
+ fn from ( size : PhysicalSize ) -> Size {
344
+ Size :: Physical ( size )
321
345
}
322
346
}
323
347
324
- impl Into < ( u32 , u32 ) > for PhysicalSize {
325
- /// Note that this rounds instead of truncating.
348
+ impl From < LogicalSize > for Size {
326
349
#[ inline]
327
- fn into ( self ) -> ( u32 , u32 ) {
328
- ( self . width . round ( ) as _ , self . height . round ( ) as _ )
350
+ fn from ( size : LogicalSize ) -> Size {
351
+ Size :: Logical ( size)
352
+ }
353
+ }
354
+
355
+ #[ derive( Debug , Copy , Clone , PartialEq ) ]
356
+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
357
+ pub enum Position {
358
+ Physical ( PhysicalPosition ) ,
359
+ Logical ( LogicalPosition ) ,
360
+ }
361
+
362
+ impl Position {
363
+ pub fn new < S : Into < Position > > ( position : S ) -> Position {
364
+ position. into ( )
365
+ }
366
+
367
+ pub fn to_logical ( & self , dpi_factor : f64 ) -> LogicalPosition {
368
+ match * self {
369
+ Position :: Physical ( position) => position. to_logical ( dpi_factor) ,
370
+ Position :: Logical ( position) => position,
371
+ }
372
+ }
373
+
374
+ pub fn to_physical ( & self , dpi_factor : f64 ) -> PhysicalPosition {
375
+ match * self {
376
+ Position :: Physical ( position) => position,
377
+ Position :: Logical ( position) => position. to_physical ( dpi_factor) ,
378
+ }
379
+ }
380
+ }
381
+
382
+ impl From < PhysicalPosition > for Position {
383
+ #[ inline]
384
+ fn from ( position : PhysicalPosition ) -> Position {
385
+ Position :: Physical ( position)
386
+ }
387
+ }
388
+
389
+ impl From < LogicalPosition > for Position {
390
+ #[ inline]
391
+ fn from ( position : LogicalPosition ) -> Position {
392
+ Position :: Logical ( position)
329
393
}
330
394
}
0 commit comments