use bevy_ecs::prelude::*;
use std::sync::{Arc, Mutex};
use windows::Win32::Foundation::{HWND, POINT};
use super::DragConstraint;
#[derive(Debug, Clone)]
pub struct WindowDragContext {
pub hwnd: Option<HWND>,
pub initial_window_pos: Option<POINT>,
pub move_window: bool,
pub constraint: Option<DragConstraint>,
}
unsafe impl Send for WindowDragContext {}
unsafe impl Sync for WindowDragContext {}
impl Default for WindowDragContext {
fn default() -> Self {
Self {
hwnd: None,
initial_window_pos: None,
move_window: false,
constraint: None,
}
}
}
impl WindowDragContext {
pub fn clear(&mut self) {
self.hwnd = None;
self.initial_window_pos = None;
self.move_window = false;
self.constraint = None;
}
}
#[derive(Resource, Clone)]
pub struct WindowDragContextResource {
inner: Arc<Mutex<WindowDragContext>>,
}
impl WindowDragContextResource {
pub fn new() -> Self {
Self {
inner: Arc::new(Mutex::new(WindowDragContext::default())),
}
}
pub fn set(&self, context: WindowDragContext) {
if let Ok(mut inner) = self.inner.lock() {
*inner = context;
}
}
pub fn get(&self) -> Option<WindowDragContext> {
self.inner.lock().ok().map(|inner| inner.clone())
}
pub fn clear(&self) {
if let Ok(mut inner) = self.inner.lock() {
inner.clear();
}
}
}
impl Default for WindowDragContextResource {
fn default() -> Self {
Self::new()
}
}