Files
tauri/tooling/api/src/dpi.ts
Amr Bashir c9a9246c37 feat: move window plugin back to core (#8007)
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
2023-10-17 08:33:23 -03:00

100 lines
2.1 KiB
TypeScript

// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
/**
* A size represented in logical pixels.
*
* @since 2.0.0
*/
class LogicalSize {
type = 'Logical'
width: number
height: number
constructor(width: number, height: number) {
this.width = width
this.height = height
}
}
/**
* A size represented in physical pixels.
*
* @since 2.0.0
*/
class PhysicalSize {
type = 'Physical'
width: number
height: number
constructor(width: number, height: number) {
this.width = width
this.height = height
}
/**
* Converts the physical size to a logical one.
* @example
* ```typescript
* import { getCurrent } from '@tauri-apps/api/window';
* const appWindow = getCurrent();
* const factor = await appWindow.scaleFactor();
* const size = await appWindow.innerSize();
* const logical = size.toLogical(factor);
* ```
* */
toLogical(scaleFactor: number): LogicalSize {
return new LogicalSize(this.width / scaleFactor, this.height / scaleFactor)
}
}
/**
* A position represented in logical pixels.
*
* @since 2.0.0
*/
class LogicalPosition {
type = 'Logical'
x: number
y: number
constructor(x: number, y: number) {
this.x = x
this.y = y
}
}
/**
* A position represented in physical pixels.
*
* @since 2.0.0
*/
class PhysicalPosition {
type = 'Physical'
x: number
y: number
constructor(x: number, y: number) {
this.x = x
this.y = y
}
/**
* Converts the physical position to a logical one.
* @example
* ```typescript
* import { getCurrent } from '@tauri-apps/api/window';
* const appWindow = getCurrent();
* const factor = await appWindow.scaleFactor();
* const position = await appWindow.innerPosition();
* const logical = position.toLogical(factor);
* ```
* */
toLogical(scaleFactor: number): LogicalPosition {
return new LogicalPosition(this.x / scaleFactor, this.y / scaleFactor)
}
}
export { LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize }