Remove unsafe pointer shenanigans and give Lua instance ownership over `Context`. Event handlers now directly mutate Context instead of using a shared app state which is then hooked up to Context via unsafe pointers. The removal of a shared state means stages (previously called "modes") are now responsible for keeping their own internal state. Additionally, clean up draw request handling and module hierarchy.
89 lines
2.5 KiB
Rust
89 lines
2.5 KiB
Rust
use crate::{
|
|
clipboard::Clipboard,
|
|
dpi::{ConvertToLogical, LogicalSize, PhysicalSize},
|
|
};
|
|
use raw_window_handle::HasDisplayHandle;
|
|
use std::sync::Arc;
|
|
use winit::window::Window;
|
|
|
|
pub struct WindowState {
|
|
// NOTE: clipboard needs to be destroyed before window
|
|
clipboard: Option<Clipboard>,
|
|
pub window: Option<Arc<Window>>,
|
|
pub size: PhysicalSize<u32>,
|
|
scale_factor: f32,
|
|
pub scale_factor_override: Option<f32>,
|
|
pending_window_title: std::cell::Cell<Option<String>>,
|
|
pub is_hovered: bool,
|
|
pub is_focused: bool,
|
|
}
|
|
|
|
impl Default for WindowState {
|
|
fn default() -> Self {
|
|
Self {
|
|
window: None,
|
|
size: Default::default(),
|
|
scale_factor: 1.0,
|
|
scale_factor_override: None,
|
|
pending_window_title: std::cell::Cell::new(None),
|
|
clipboard: None,
|
|
is_hovered: true,
|
|
is_focused: true,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl WindowState {
|
|
pub fn set_window(&mut self, window: Arc<Window>) {
|
|
if let Some(title) = self.pending_window_title.take() {
|
|
window.set_title(&title);
|
|
}
|
|
|
|
let winit::dpi::PhysicalSize { width, height } = window.inner_size();
|
|
self.size = PhysicalSize::new(width, height);
|
|
self.scale_factor = window.scale_factor() as f32;
|
|
|
|
let raw_display_handle = window.display_handle().ok().map(|h| h.as_raw());
|
|
self.clipboard = Some(Clipboard::new(raw_display_handle));
|
|
self.window = Some(window);
|
|
}
|
|
|
|
pub fn set_window_title(&self, title: &str) {
|
|
if let Some(ref window) = self.window {
|
|
window.set_title(title);
|
|
} else {
|
|
self.pending_window_title.set(Some(title.to_string()));
|
|
}
|
|
}
|
|
|
|
pub fn logical_size(&self) -> LogicalSize<u32> {
|
|
self.size.to_logical(self.scale_factor())
|
|
}
|
|
|
|
pub fn scale_factor(&self) -> f32 {
|
|
self.scale_factor_override.unwrap_or(self.scale_factor)
|
|
}
|
|
|
|
pub fn set_scale_factor(&mut self, scale_factor: f32) {
|
|
self.scale_factor = scale_factor;
|
|
}
|
|
|
|
pub fn focus(&self) {
|
|
if let Some(ref window) = self.window {
|
|
window.focus_window();
|
|
}
|
|
}
|
|
|
|
pub fn set_clipboard_text(&mut self, text: String) {
|
|
if let Some(clipboard) = &mut self.clipboard {
|
|
clipboard.set_text(text);
|
|
}
|
|
}
|
|
|
|
pub fn get_clipboard_text(&mut self) -> Option<String> {
|
|
self.clipboard
|
|
.as_mut()
|
|
.and_then(|clipboard| clipboard.get_text())
|
|
}
|
|
}
|