Seregon/StratoSDK

StratoSDK is a framework with a declarative approach similar to Flutter/React, written and designed entirely for Rust.

Rust/27.3 KB/No license
crates/strato-ui-core/src/zoom.rs
1//! Module containing definitions related to UI magnification via a [`ZoomFactor`].
2 
3use pathfinder_geometry::vector::Vector2F;
4 
5/// The zoom factor of the application. All UI elements are magnified by this value.
6#[derive(Copy, Clone)]
7pub struct ZoomFactor(f32);
8 
9impl ZoomFactor {
10 pub fn new(zoom_level: f32) -> Self {
11 Self(zoom_level)
12 }
13}
14 
15impl Default for ZoomFactor {
16 fn default() -> Self {
17 Self(1.0)
18 }
19}
20 
21/// Helper trait that scales a value by the given [`ZoomFactor`].
22pub trait Scale: Sized {
23 /// Scales the current value up by the current [`ZoomFactor`].
24 fn scale_up(self, zoom_level: ZoomFactor) -> Self;
25 
26 /// Scales the current value down by the current [`ZoomFactor`].
27 fn scale_down(self, zoom_level: ZoomFactor) -> Self {
28 self.scale_up(ZoomFactor::new(1.0 / zoom_level.0))
29 }
30}
31 
32impl Scale for f32 {
33 fn scale_up(self, zoom_level: ZoomFactor) -> Self {
34 self * zoom_level.0
35 }
36}
37 
38impl Scale for Vector2F {
39 fn scale_up(self, zoom_level: ZoomFactor) -> Self {
40 Vector2F::new(self.x() * zoom_level.0, self.y() * zoom_level.0)
41 }
42}
43