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 = "PascalCase")]
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		Ok(())
80	}
81
82	/// Increases the zoom level by a step.
83	///
84	/// # Arguments
85	/// * `Step` - Zoom increment amount
86	///
87	/// # Returns
88	/// Result indicating success or error if would exceed range
89	pub fn ZoomIn(&mut self, Step:f64) -> Result<(), String> {
90		let NewZoom = self.ZoomLevel + Step;
91		self.SetZoomLevel(NewZoom)
92	}
93
94	/// Decreases the zoom level by a step.
95	///
96	/// # Arguments
97	/// * `Step` - Zoom decrement amount
98	///
99	/// # Returns
100	/// Result indicating success or error if would exceed range
101	pub fn ZoomOut(&mut self, Step:f64) -> Result<(), String> {
102		let NewZoom = self.ZoomLevel - Step;
103		self.SetZoomLevel(NewZoom)
104	}
105
106	/// Resets the zoom level to default.
107	pub fn ResetZoom(&mut self) { self.ZoomLevel = DEFAULT_ZOOM_LEVEL; }
108
109	/// Gets the current zoom level as a percentage.
110	/// A zoom level of 0 corresponds to 100%.
111	pub fn GetZoomPercent(&self) -> f64 { 100.0 + (self.ZoomLevel * 10.0) }
112}
113
114fn DefaultZoomLevel() -> f64 { DEFAULT_ZOOM_LEVEL }