Mountain/ApplicationState/DTO/WindowStateDTO.rs
1//! # WindowStateDTO
2//!
3//! # RESPONSIBILITY
4//! - Data transfer object for main window state
5//! - Serializable format for gRPC/IPC transmission
6//! - Used by Mountain to track window presentation state
7//!
8//! # FIELDS
9//! - IsFocused: Window focus state
10//! - IsFullScreen: Fullscreen mode state
11//! - ZoomLevel: Window zoom level
12use serde::{Deserialize, Serialize};
13
14/// Minimum allowed zoom level
15const MIN_ZOOM_LEVEL:f64 = -20.0;
16
17/// Maximum allowed zoom level
18const MAX_ZOOM_LEVEL:f64 = 20.0;
19
20/// Default zoom level
21const DEFAULT_ZOOM_LEVEL:f64 = 0.0;
22
23/// Holds information about the state of the main application window, such as
24/// whether it is focused or fullscreen, and its current zoom level.
25#[derive(Serialize, Deserialize, Debug, Clone, Default)]
26#[serde(rename_all = "camelCase")]
27pub struct WindowStateDTO {
28 /// Whether the window currently has input focus
29 #[serde(default)]
30 pub IsFocused:bool,
31
32 /// Whether the window is in fullscreen mode
33 #[serde(default)]
34 pub IsFullScreen:bool,
35
36 /// Zoom level for content scaling (typically -10 to 10)
37 #[serde(default = "DefaultZoomLevel")]
38 pub ZoomLevel:f64,
39}
40
41impl WindowStateDTO {
42 /// Creates a new WindowStateDTO with validation.
43 ///
44 /// # Arguments
45 /// * `IsFocused` - Focus state
46 /// * `IsFullScreen` - Fullscreen state
47 /// * `ZoomLevel` - Zoom level
48 ///
49 /// # Returns
50 /// Result containing the DTO or validation error
51 pub fn New(IsFocused:bool, IsFullScreen:bool, ZoomLevel:f64) -> Result<Self, String> {
52 // Validate zoom level range
53 if ZoomLevel < MIN_ZOOM_LEVEL || ZoomLevel > MAX_ZOOM_LEVEL {
54 return Err(format!(
55 "Zoom level must be between {} and {}, got {}",
56 MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL, ZoomLevel
57 ));
58 }
59
60 Ok(Self { IsFocused, IsFullScreen, ZoomLevel })
61 }
62
63 /// Sets the zoom level with validation.
64 ///
65 /// # Arguments
66 /// * `ZoomLevel` - New zoom level
67 ///
68 /// # Returns
69 /// Result indicating success or error if out of range
70 pub fn SetZoomLevel(&mut self, ZoomLevel:f64) -> Result<(), String> {
71 if ZoomLevel < MIN_ZOOM_LEVEL || ZoomLevel > MAX_ZOOM_LEVEL {
72 return Err(format!(
73 "Zoom level must be between {} and {}, got {}",
74 MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL, ZoomLevel
75 ));
76 }
77
78 self.ZoomLevel = ZoomLevel;
79
80 Ok(())
81 }
82
83 /// Increases the zoom level by a step.
84 ///
85 /// # Arguments
86 /// * `Step` - Zoom increment amount
87 ///
88 /// # Returns
89 /// Result indicating success or error if would exceed range
90 pub fn ZoomIn(&mut self, Step:f64) -> Result<(), String> {
91 let NewZoom = self.ZoomLevel + Step;
92
93 self.SetZoomLevel(NewZoom)
94 }
95
96 /// Decreases the zoom level by a step.
97 ///
98 /// # Arguments
99 /// * `Step` - Zoom decrement amount
100 ///
101 /// # Returns
102 /// Result indicating success or error if would exceed range
103 pub fn ZoomOut(&mut self, Step:f64) -> Result<(), String> {
104 let NewZoom = self.ZoomLevel - Step;
105
106 self.SetZoomLevel(NewZoom)
107 }
108
109 /// Resets the zoom level to default.
110 pub fn ResetZoom(&mut self) { self.ZoomLevel = DEFAULT_ZOOM_LEVEL; }
111
112 /// Gets the current zoom level as a percentage.
113 /// A zoom level of 0 corresponds to 100%.
114 pub fn GetZoomPercent(&self) -> f64 { 100.0 + (self.ZoomLevel * 10.0) }
115}
116
117fn DefaultZoomLevel() -> f64 { DEFAULT_ZOOM_LEVEL }