dpi.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2019-2023 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. /**
  19. * A size represented in physical pixels.
  20. *
  21. * @since 2.0.0
  22. */
  23. class PhysicalSize {
  24. type = 'Physical'
  25. width: number
  26. height: number
  27. constructor(width: number, height: number) {
  28. this.width = width
  29. this.height = height
  30. }
  31. /**
  32. * Converts the physical size to a logical one.
  33. * @example
  34. * ```typescript
  35. * import { getCurrent } from '@tauri-apps/api/window';
  36. * const appWindow = getCurrent();
  37. * const factor = await appWindow.scaleFactor();
  38. * const size = await appWindow.innerSize();
  39. * const logical = size.toLogical(factor);
  40. * ```
  41. * */
  42. toLogical(scaleFactor: number): LogicalSize {
  43. return new LogicalSize(this.width / scaleFactor, this.height / scaleFactor)
  44. }
  45. }
  46. /**
  47. * A position represented in logical pixels.
  48. *
  49. * @since 2.0.0
  50. */
  51. class LogicalPosition {
  52. type = 'Logical'
  53. x: number
  54. y: number
  55. constructor(x: number, y: number) {
  56. this.x = x
  57. this.y = y
  58. }
  59. }
  60. /**
  61. * A position represented in physical pixels.
  62. *
  63. * @since 2.0.0
  64. */
  65. class PhysicalPosition {
  66. type = 'Physical'
  67. x: number
  68. y: number
  69. constructor(x: number, y: number) {
  70. this.x = x
  71. this.y = y
  72. }
  73. /**
  74. * Converts the physical position to a logical one.
  75. * @example
  76. * ```typescript
  77. * import { getCurrent } from '@tauri-apps/api/window';
  78. * const appWindow = getCurrent();
  79. * const factor = await appWindow.scaleFactor();
  80. * const position = await appWindow.innerPosition();
  81. * const logical = position.toLogical(factor);
  82. * ```
  83. * */
  84. toLogical(scaleFactor: number): LogicalPosition {
  85. return new LogicalPosition(this.x / scaleFactor, this.y / scaleFactor)
  86. }
  87. }
  88. export { LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize }