Skip to main content

tauri_runtime/
dpi.rs

1// Copyright 2019-2024 Tauri Programme within The Commons Conservancy
2// SPDX-License-Identifier: Apache-2.0
3// SPDX-License-Identifier: MIT
4
5pub use dpi::*;
6use serde::Serialize;
7
8/// A rectangular region.
9#[derive(Clone, Copy, Debug, Serialize)]
10pub struct Rect {
11  /// Rect position.
12  pub position: dpi::Position,
13  /// Rect size.
14  pub size: dpi::Size,
15}
16
17impl Rect {
18  pub fn to_physical<P: dpi::Pixel, S: dpi::Pixel>(self, scale: f64) -> PhysicalRect<P, S> {
19    PhysicalRect {
20      position: self.position.to_physical(scale),
21      size: self.size.to_physical(scale),
22    }
23  }
24
25  pub fn to_logical<P: dpi::Pixel, S: dpi::Pixel>(self, scale: f64) -> LogicalRect<P, S> {
26    LogicalRect {
27      position: self.position.to_logical(scale),
28      size: self.size.to_logical(scale),
29    }
30  }
31}
32
33impl Default for Rect {
34  fn default() -> Self {
35    Self {
36      position: Position::Logical((0, 0).into()),
37      size: Size::Logical((0, 0).into()),
38    }
39  }
40}
41
42/// A rectangular region in physical pixels.
43#[derive(Clone, Copy, Debug, Serialize)]
44pub struct PhysicalRect<P: dpi::Pixel, S: dpi::Pixel> {
45  /// Rect position.
46  pub position: dpi::PhysicalPosition<P>,
47  /// Rect size.
48  pub size: dpi::PhysicalSize<S>,
49}
50
51impl<P: dpi::Pixel, S: dpi::Pixel> Default for PhysicalRect<P, S> {
52  fn default() -> Self {
53    Self {
54      position: (0, 0).into(),
55      size: (0, 0).into(),
56    }
57  }
58}
59
60impl<P: dpi::Pixel, S: dpi::Pixel> PhysicalRect<P, S> {
61  pub fn to_logical(self, scale: f64) -> LogicalRect<P, S> {
62    LogicalRect {
63      position: self.position.to_logical(scale),
64      size: self.size.to_logical(scale),
65    }
66  }
67}
68
69/// A rectangular region in logical pixels.
70#[derive(Clone, Copy, Debug, Serialize)]
71pub struct LogicalRect<P: dpi::Pixel, S: dpi::Pixel> {
72  /// Rect position.
73  pub position: dpi::LogicalPosition<P>,
74  /// Rect size.
75  pub size: dpi::LogicalSize<S>,
76}
77
78impl<P: dpi::Pixel, S: dpi::Pixel> Default for LogicalRect<P, S> {
79  fn default() -> Self {
80    Self {
81      position: (0, 0).into(),
82      size: (0, 0).into(),
83    }
84  }
85}
86
87impl<P: dpi::Pixel, S: dpi::Pixel> LogicalRect<P, S> {
88  pub fn to_physical(self, scale: f64) -> PhysicalRect<P, S> {
89    PhysicalRect {
90      position: self.position.to_physical(scale),
91      size: self.size.to_physical(scale),
92    }
93  }
94}