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