@@ -759,6 +759,145 @@ impl<P: Pixel> From<LogicalPosition<P>> for Position {
759759 }
760760}
761761
762+ /// The logical distance between the edges of two rectangles.
763+ #[ derive( Debug , Copy , Clone , Eq , PartialEq , Ord , PartialOrd , Default , Hash ) ]
764+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
765+ pub struct LogicalInsets < P > {
766+ /// The distance to the top edge.
767+ pub top : P ,
768+ /// The distance to the left edge.
769+ pub left : P ,
770+ /// The distance to the bottom edge.
771+ pub bottom : P ,
772+ /// The distance to the right edge.
773+ pub right : P ,
774+ }
775+
776+ impl < P > LogicalInsets < P > {
777+ #[ inline]
778+ pub const fn new ( top : P ,
779+ left : P ,
780+ bottom : P ,
781+ right : P , ) -> Self {
782+ Self { top, left, bottom, right }
783+ }
784+ }
785+
786+ impl < P : Pixel > LogicalInsets < P > {
787+ #[ inline]
788+ pub fn from_physical < T : Into < LogicalInsets < X > > , X : Pixel > (
789+ physical : T ,
790+ scale_factor : f64 ,
791+ ) -> Self {
792+ physical. into ( ) . to_logical ( scale_factor)
793+ }
794+
795+ #[ inline]
796+ pub fn to_physical < X : Pixel > ( & self , scale_factor : f64 ) -> PhysicalInsets < X > {
797+ assert ! ( validate_scale_factor( scale_factor) ) ;
798+ let top = self . top . into ( ) * scale_factor;
799+ let left = self . left . into ( ) * scale_factor;
800+ let bottom = self . bottom . into ( ) * scale_factor;
801+ let right = self . right . into ( ) * scale_factor;
802+ PhysicalInsets :: new ( top, left, bottom, right) . cast ( )
803+ }
804+
805+ #[ inline]
806+ pub fn cast < X : Pixel > ( & self ) -> LogicalInsets < X > {
807+ LogicalInsets { top : self . top . cast ( ) , left : self . left . cast ( ) , bottom : self . bottom . cast ( ) , right : self . right . cast ( ) }
808+ }
809+ }
810+
811+ /// The physical distance between the edges of two rectangles.
812+ #[ derive( Debug , Copy , Clone , Eq , PartialEq , Ord , PartialOrd , Default , Hash ) ]
813+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
814+ pub struct PhysicalInsets < P > {
815+ /// The distance to the top edge.
816+ pub top : P ,
817+ /// The distance to the left edge.
818+ pub left : P ,
819+ /// The distance to the bottom edge.
820+ pub bottom : P ,
821+ /// The distance to the right edge.
822+ pub right : P ,
823+ }
824+
825+ impl < P > PhysicalInsets < P > {
826+ #[ inline]
827+ pub const fn new ( top : P ,
828+ left : P ,
829+ bottom : P ,
830+ right : P , ) -> Self {
831+ Self { top, left, bottom, right }
832+ }
833+ }
834+
835+ impl < P : Pixel > PhysicalInsets < P > {
836+ #[ inline]
837+ pub fn from_logical < T : Into < LogicalInsets < X > > , X : Pixel > ( logical : T , scale_factor : f64 ) -> Self {
838+ logical. into ( ) . to_physical ( scale_factor)
839+ }
840+
841+ #[ inline]
842+ pub fn to_logical < X : Pixel > ( & self , scale_factor : f64 ) -> LogicalInsets < X > {
843+ assert ! ( validate_scale_factor( scale_factor) ) ;
844+ let width = self . width . into ( ) / scale_factor;
845+ let height = self . height . into ( ) / scale_factor;
846+ let top = self . top . into ( ) / scale_factor;
847+ let left = self . left . into ( ) / scale_factor;
848+ let bottom = self . bottom . into ( ) / scale_factor;
849+ let right = self . right . into ( ) / scale_factor;
850+ LogicalInsets :: new ( top, left, bottom, right) . cast ( )
851+ }
852+
853+ #[ inline]
854+ pub fn cast < X : Pixel > ( & self ) -> PhysicalInsets < X > {
855+ PhysicalInsets { top : self . top . cast ( ) , left : self . left . cast ( ) , bottom : self . bottom . cast ( ) , right : self . right . cast ( ) }
856+ }
857+ }
858+
859+ /// Insets that are either physical or logical.
860+ #[ derive( Debug , Copy , Clone , PartialEq ) ]
861+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
862+ pub enum Insets {
863+ Physical ( PhysicalInsets < u32 > ) ,
864+ Logical ( LogicalInsets < f64 > ) ,
865+ }
866+
867+ impl Insets {
868+ pub fn new < S : Into < Self > > ( insets : S ) -> Self {
869+ insets. into ( )
870+ }
871+
872+ pub fn to_logical < P : Pixel > ( & self , scale_factor : f64 ) -> LogicalInsets < P > {
873+ match * self {
874+ Self :: Physical ( insets) => insets. to_logical ( scale_factor) ,
875+ Self :: Logical ( insets) => insets. cast ( ) ,
876+ }
877+ }
878+
879+ pub fn to_physical < P : Pixel > ( & self , scale_factor : f64 ) -> PhysicalInsets < P > {
880+ match * self {
881+ Self :: Physical ( insets) => insets. cast ( ) ,
882+ Self :: Logical ( insets) => insets. to_physical ( scale_factor) ,
883+ }
884+ }
885+ }
886+
887+ impl < P : Pixel > From < PhysicalInsets < P > > for Insets {
888+ #[ inline]
889+ fn from ( insets : PhysicalInsets < P > ) -> Self {
890+ Self :: Physical ( insets. cast ( ) )
891+ }
892+ }
893+
894+ impl < P : Pixel > From < LogicalInsets < P > > for Insets {
895+ #[ inline]
896+ fn from ( insets : LogicalInsets < P > ) -> Self {
897+ Self :: Logical ( insets. cast ( ) )
898+ }
899+ }
900+
762901#[ cfg( test) ]
763902mod tests {
764903 use std:: collections:: HashSet ;
0 commit comments