StratoSDK is a framework with a declarative approach similar to Flutter/React, written and designed entirely for Rust.
| 1 | use crate::platform::WindowManager as _; |
| 2 | use crate::windowing::winit::window::WindowManager; |
| 3 | use crate::{DisplayId, DisplayIdx}; |
| 4 | use anyhow::Result; |
| 5 | use itertools::Itertools as _; |
| 6 | use pathfinder_geometry::rect::RectF; |
| 7 | use std::sync::Arc; |
| 8 | use winit::monitor::MonitorHandle; |
| 9 | use winit::platform::windows::MonitorHandleExtWindows; |
| 10 | use winit::window::Window as WinitWindow; |
| 11 | |
| 12 | use super::get_monitor_logical_bounds; |
| 13 | |
| 14 | impl WindowManager { |
| 15 | fn get_active_window_handle(&self) -> Result<Arc<WinitWindow>> { |
| 16 | let window_id = &self |
| 17 | .active_window_id() |
| 18 | .ok_or(anyhow::anyhow!("No active window ID"))?; |
| 19 | let ui_window = self |
| 20 | .windows |
| 21 | .get(window_id) |
| 22 | .ok_or(anyhow::anyhow!("Window not found"))?; |
| 23 | let winit_window_borrow = ui_window.inner.try_borrow()?; |
| 24 | let winit_window_ref = winit_window_borrow |
| 25 | .as_ref() |
| 26 | .ok_or(anyhow::anyhow!("Unable to read Window information"))?; |
| 27 | Ok(winit_window_ref.window.clone()) |
| 28 | } |
| 29 | |
| 30 | fn get_current_monitor_handle(&self) -> Result<MonitorHandle> { |
| 31 | let winit_window_ref = self.get_active_window_handle()?; |
| 32 | winit_window_ref |
| 33 | .current_monitor() |
| 34 | .ok_or(anyhow::anyhow!("Unable to get current monitor")) |
| 35 | } |
| 36 | |
| 37 | pub(super) fn get_monitor_bounds_for_display_idx(&self, idx: DisplayIdx) -> Result<RectF> { |
| 38 | let primary_monitor = self.get_primary_monitor_handle()?; |
| 39 | let monitor = match idx { |
| 40 | DisplayIdx::Primary => primary_monitor, |
| 41 | DisplayIdx::External(numerical_index) => { |
| 42 | let monitors = self.get_available_monitors()?; |
| 43 | monitors |
| 44 | .iter() |
| 45 | .filter(|monitor| { |
| 46 | // Filter out the primary monitor. |
| 47 | monitor.hmonitor() != primary_monitor.hmonitor() |
| 48 | }) |
| 49 | .nth(numerical_index) |
| 50 | .ok_or(anyhow::anyhow!( |
| 51 | "Could not find monitor handle for {numerical_index:?}" |
| 52 | ))? |
| 53 | .to_owned() |
| 54 | } |
| 55 | }; |
| 56 | Ok(get_monitor_logical_bounds(&monitor)) |
| 57 | } |
| 58 | |
| 59 | fn get_primary_monitor_handle(&self) -> Result<MonitorHandle> { |
| 60 | let winit_window_ref = self.get_active_window_handle()?; |
| 61 | winit_window_ref |
| 62 | .primary_monitor() |
| 63 | .ok_or(anyhow::anyhow!("No primary monitor found")) |
| 64 | } |
| 65 | |
| 66 | pub(super) fn get_current_monitor_id(&self) -> Result<DisplayId> { |
| 67 | let active_monitor = self.get_current_monitor_handle()?; |
| 68 | let active_monitor_id = active_monitor.hmonitor(); |
| 69 | Ok(DisplayId::from(active_monitor_id as usize)) |
| 70 | } |
| 71 | |
| 72 | fn get_available_monitors(&self) -> Result<Vec<MonitorHandle>> { |
| 73 | let winit_window_ref = self.get_active_window_handle()?; |
| 74 | Ok(winit_window_ref.available_monitors().collect_vec()) |
| 75 | } |
| 76 | |
| 77 | pub(super) fn get_available_monitor_count(&self) -> Result<usize> { |
| 78 | let winit_window_ref = self.get_active_window_handle()?; |
| 79 | Ok(winit_window_ref.available_monitors().count()) |
| 80 | } |
| 81 | |
| 82 | pub(super) fn get_active_monitor_logical_bounds(&self) -> Result<RectF> { |
| 83 | let active_monitor = self.get_current_monitor_handle()?; |
| 84 | Ok(get_monitor_logical_bounds(&active_monitor)) |
| 85 | } |
| 86 | } |
| 87 |