dpi.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright 2019-2024 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. /**
  5. * A size represented in logical pixels.
  6. *
  7. * @since 2.0.0
  8. */
  9. class LogicalSize {
  10. type = 'Logical'
  11. width: number
  12. height: number
  13. constructor(width: number, height: number) {
  14. this.width = width
  15. this.height = height
  16. }
  17. /**
  18. * Converts the logical size to a physical one.
  19. * @example
  20. * ```typescript
  21. * import { getCurrentWindow } from '@tauri-apps/api/window';
  22. * const appWindow = getCurrentWindow();
  23. * const factor = await appWindow.scaleFactor();
  24. * const size = new LogicalSize(400, 500);
  25. * const physical = size.toPhysical(factor);
  26. * ```
  27. *
  28. * @since 2.0.0
  29. */
  30. toPhysical(scaleFactor: number): PhysicalSize {
  31. return new PhysicalSize(this.width * scaleFactor, this.height * scaleFactor)
  32. }
  33. }
  34. /**
  35. * A size represented in physical pixels.
  36. *
  37. * @since 2.0.0
  38. */
  39. class PhysicalSize {
  40. type = 'Physical'
  41. width: number
  42. height: number
  43. constructor(width: number, height: number) {
  44. this.width = width
  45. this.height = height
  46. }
  47. /**
  48. * Converts the physical size to a logical one.
  49. * @example
  50. * ```typescript
  51. * import { getCurrentWindow } from '@tauri-apps/api/window';
  52. * const appWindow = getCurrentWindow();
  53. * const factor = await appWindow.scaleFactor();
  54. * const size = await appWindow.innerSize();
  55. * const logical = size.toLogical(factor);
  56. * ```
  57. */
  58. toLogical(scaleFactor: number): LogicalSize {
  59. return new LogicalSize(this.width / scaleFactor, this.height / scaleFactor)
  60. }
  61. }
  62. /**
  63. * A position represented in logical pixels.
  64. *
  65. * @since 2.0.0
  66. */
  67. class LogicalPosition {
  68. type = 'Logical'
  69. x: number
  70. y: number
  71. constructor(x: number, y: number) {
  72. this.x = x
  73. this.y = y
  74. }
  75. /**
  76. * Converts the logical position to a physical one.
  77. * @example
  78. * ```typescript
  79. * import { getCurrentWindow } from '@tauri-apps/api/window';
  80. * const appWindow = getCurrentWindow();
  81. * const factor = await appWindow.scaleFactor();
  82. * const position = new LogicalPosition(400, 500);
  83. * const physical = position.toPhysical(factor);
  84. * ```
  85. *
  86. * @since 2.0.0
  87. */
  88. toPhysical(scaleFactor: number): PhysicalPosition {
  89. return new PhysicalPosition(this.x * scaleFactor, this.x * scaleFactor)
  90. }
  91. }
  92. /**
  93. * A position represented in physical pixels.
  94. *
  95. * @since 2.0.0
  96. */
  97. class PhysicalPosition {
  98. type = 'Physical'
  99. x: number
  100. y: number
  101. constructor(x: number, y: number) {
  102. this.x = x
  103. this.y = y
  104. }
  105. /**
  106. * Converts the physical position to a logical one.
  107. * @example
  108. * ```typescript
  109. * import { getCurrentWindow } from '@tauri-apps/api/window';
  110. * const appWindow = getCurrentWindow();
  111. * const factor = await appWindow.scaleFactor();
  112. * const position = await appWindow.innerPosition();
  113. * const logical = position.toLogical(factor);
  114. * ```
  115. */
  116. toLogical(scaleFactor: number): LogicalPosition {
  117. return new LogicalPosition(this.x / scaleFactor, this.y / scaleFactor)
  118. }
  119. }
  120. export { LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize }