summaryrefslogtreecommitdiff
path: root/engine/src/windowing
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2026-06-26 22:53:25 +0200
committerHampusM <hampus@hampusmat.com>2026-06-30 18:24:46 +0200
commit4a5e3e612e8b597dd1734afefd01324b74fd99ab (patch)
treebd875cc1ca639b2cc957fb9746e6a816fc2e4047 /engine/src/windowing
parent3cdd8c99bac3833bef0c4795aab1fde8d71e59df (diff)
refactor(engine): make window creation attrs struct not a wrapper
Diffstat (limited to 'engine/src/windowing')
-rw-r--r--engine/src/windowing/window.rs100
1 files changed, 42 insertions, 58 deletions
diff --git a/engine/src/windowing/window.rs b/engine/src/windowing/window.rs
index 5c5e149..7be2562 100644
--- a/engine/src/windowing/window.rs
+++ b/engine/src/windowing/window.rs
@@ -1,3 +1,5 @@
+use std::borrow::Cow;
+
use crate::data_types::dimens::Dimens;
use crate::ecs::Component;
use crate::reflection::Reflection;
@@ -18,18 +20,25 @@ impl Id
}
}
-macro_rules! impl_creation_attributes_field_fns {
- ($field: ident, ($($getter_ret_pre: tt)?), $getter_ret_type: ty, $field_type: ty) => {
- impl CreationAttributes
- {
- pub fn $field(&self) -> $getter_ret_type
- {
- $($getter_ret_pre)? self.attrs.$field
- }
+#[derive(Debug, Component, Clone)]
+#[non_exhaustive]
+pub struct CreationAttributes
+{
+ pub title: Cow<'static, str>,
+ pub transparent: bool,
+ pub maximized: bool,
+ pub fullscreen: Option<Fullscreen>,
+ pub x_visual_id: Option<XVisualID>,
+}
- paste::paste! {
- pub fn [<with_ $field>](mut self, $field: impl Into<$field_type>) -> Self {
- self.attrs.$field = $field.into();
+macro_rules! gen_creation_attrs_with_fn {
+ ($field: ident, $field_type: ty) => {
+ paste::paste! {
+ impl CreationAttributes
+ {
+ pub fn [<with_ $field>](mut self, new: impl Into<$field_type>) -> Self
+ {
+ self.$field = new.into();
self
}
}
@@ -37,54 +46,31 @@ macro_rules! impl_creation_attributes_field_fns {
};
}
-#[derive(Debug, Component, Clone)]
-#[non_exhaustive]
-pub struct CreationAttributes
-{
- attrs: winit::window::WindowAttributes,
-}
-
-impl_creation_attributes_field_fns!(title, (&), &str, String);
-impl_creation_attributes_field_fns!(transparent, (), bool, bool);
-impl_creation_attributes_field_fns!(maximized, (), bool, bool);
+gen_creation_attrs_with_fn!(title, Cow<'static, str>);
+gen_creation_attrs_with_fn!(transparent, bool);
+gen_creation_attrs_with_fn!(maximized, bool);
+gen_creation_attrs_with_fn!(fullscreen, Option<Fullscreen>);
+gen_creation_attrs_with_fn!(x_visual_id, Option<XVisualID>);
impl CreationAttributes
{
- pub fn fullscreen(&self) -> Option<Fullscreen>
+ pub(crate) fn into_window_attrs(self) -> winit::window::WindowAttributes
{
- match self.attrs.fullscreen.as_ref()? {
- winit::window::Fullscreen::Borderless(_) => Some(Fullscreen::Borderless),
- winit::window::Fullscreen::Exclusive(_) => unreachable!(),
+ let window_attrs = winit::window::WindowAttributes::default()
+ .with_title(self.title.into_owned())
+ .with_transparent(self.transparent)
+ .with_maximized(self.maximized)
+ .with_fullscreen(match self.fullscreen {
+ Some(Fullscreen::Borderless) => Some(winit::window::Fullscreen::Borderless(None)),
+ None => None
+ });
+
+ #[cfg(target_os = "linux")]
+ if let Some(visual_id) = self.x_visual_id {
+ return <winit::window::WindowAttributes as winit::platform::x11::WindowAttributesExtX11>::with_x11_visual(window_attrs, visual_id)
}
- }
- pub fn with_fullscreen(mut self, fullscreen: Option<Fullscreen>) -> Self
- {
- self.attrs.fullscreen = fullscreen.map(|fullscreen| match fullscreen {
- Fullscreen::Borderless => winit::window::Fullscreen::Borderless(None),
- });
- self
- }
-}
-
-impl CreationAttributes
-{
- #[cfg(target_os = "linux")]
- pub fn with_x11_visual(mut self, visual_id: XVisualID) -> Self
- {
- use winit::platform::x11::WindowAttributesExtX11;
-
- self.attrs = self.attrs.with_x11_visual(visual_id);
-
- self
- }
-}
-
-impl CreationAttributes
-{
- pub(crate) fn into_inner(self) -> winit::window::WindowAttributes
- {
- self.attrs
+ window_attrs
}
}
@@ -92,13 +78,11 @@ impl Default for CreationAttributes
{
fn default() -> Self
{
- CreationAttributes {
- attrs: winit::window::WindowAttributes::default().with_title("Application"),
- }
+ Self { title: "Unnamed window".into(), transparent: false, maximized: false, fullscreen: None, x_visual_id: None }
}
}
-#[derive(Debug)]
+#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum Fullscreen
{
@@ -138,7 +122,7 @@ impl Window
) -> Self
{
Self {
- title: creation_attrs.title().to_string(),
+ title: creation_attrs.title.clone().into_owned(),
cursor_visible: true,
cursor_grab_mode: CursorGrabMode::None,
wid: Id::from_inner(winit_window.id()),