use std::hash::Hash;
use std::num::NonZeroU64;
use std::sync::atomic::{AtomicU64, Ordering};
use tracing::field::DisplayValue;
use std::fmt::Debug;
use std::sync::Arc;
use std::sync::mpsc::Sender;
use accesskit_winit::Adapter;
use masonry_core::app::{RenderRoot, RenderRootOptions, RenderRootSignal, WindowSizePolicy};
use masonry_core::core::{DefaultProperties, NewWidget, Widget};
use masonry_core::peniko::Color;
use ui_events_winit::WindowEventReducer;
use winit::dpi::PhysicalSize;
use winit::window::{Window as WindowHandle, WindowAttributes};
pub struct Window {
pub(crate) id: WindowId,
pub(crate) handle: Arc<WindowHandle>,
pub(crate) accesskit_adapter: Adapter,
pub(crate) event_reducer: WindowEventReducer,
pub(crate) render_root: RenderRoot,
pub(crate) base_color: Color,
}
#[derive(Debug)]
pub struct NewWindow {
pub id: WindowId,
pub attributes: WindowAttributes,
pub root_widget: NewWidget<dyn Widget>,
pub base_color: Color,
}
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub struct WindowId(pub(crate) NonZeroU64);
impl Window {
pub(crate) fn new(
window_id: WindowId,
handle: Arc<WindowHandle>,
accesskit_adapter: Adapter,
root_widget: NewWidget<dyn Widget>,
signal_sender: Sender<(WindowId, RenderRootSignal)>,
default_properties: Arc<DefaultProperties>,
base_color: Color,
size: PhysicalSize<u32>,
scale_factor: f64,
) -> Self {
Self {
id: window_id,
handle,
accesskit_adapter,
event_reducer: WindowEventReducer::default(),
render_root: RenderRoot::new(
root_widget,
move |signal| {
signal_sender.clone().send((window_id, signal)).unwrap();
},
RenderRootOptions {
default_properties,
use_system_fonts: true,
size_policy: WindowSizePolicy::User,
size,
scale_factor,
test_font: None,
},
),
base_color,
}
}
pub fn handle(&self) -> &winit::window::Window {
&self.handle
}
pub fn render_root(&mut self) -> &mut RenderRoot {
&mut self.render_root
}
pub fn base_color(&mut self) -> &mut Color {
&mut self.base_color
}
}
impl Debug for Window {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Window")
.field("id", &self.id)
.field("handle", &self.handle)
.field("event_reducer", &self.event_reducer)
.field("render_root", &self.render_root)
.field("base_color", &self.base_color)
.finish_non_exhaustive()
}
}
impl NewWindow {
pub fn new(attributes: WindowAttributes, root_widget: NewWidget<dyn Widget + 'static>) -> Self {
Self::new_with_id(WindowId::next(), attributes, root_widget)
}
pub fn new_with_id(
id: WindowId,
attributes: WindowAttributes,
root_widget: NewWidget<dyn Widget + 'static>,
) -> Self {
Self {
id,
attributes,
root_widget,
base_color: Color::BLACK,
}
}
pub fn with_base_color(mut self, base_color: Color) -> Self {
self.base_color = base_color;
self
}
}
impl WindowId {
pub fn next() -> Self {
static WINDOW_ID_COUNTER: AtomicU64 = AtomicU64::new(1);
let id = WINDOW_ID_COUNTER.fetch_add(1, Ordering::Relaxed);
Self(id.try_into().unwrap())
}
pub fn trace(self) -> DisplayValue<NonZeroU64> {
tracing::field::display(self.0)
}
}