use std::num::NonZero;
use magma_app::entities::Entity;
use magma_math::{IVec2, UVec2};
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub struct Window {
title: String,
name: Option<String>,
position: WindowPosition,
resolution: WindowResolution,
resizable: bool,
resize_limit: WindowResizeLimit,
mode: WindowMode,
cursor_mode: CursorMode,
cursor_visible: bool,
decorations: bool,
titlebar_buttons: TitlebarButtons,
present_mode: PresentMode, alpha_mode: AlphaMode, transparent: bool,
focused: bool,
default_event_handling: bool, window_theme: WindowTheme,
desired_maximum_frame_latency: Option<NonZero<u32>>,
pub has_window: bool,
pub changed_attr: bool,
}
impl Default for Window {
fn default() -> Self {
Self {
title: "Magma Window".to_owned(),
name: None,
position: Default::default(),
resolution: Default::default(),
resizable: true,
resize_limit: Default::default(),
mode: Default::default(),
cursor_mode: Default::default(),
cursor_visible: true,
decorations: true,
titlebar_buttons: Default::default(),
present_mode: Default::default(),
alpha_mode: Default::default(),
transparent: false,
focused: true,
default_event_handling: true,
window_theme: Default::default(),
desired_maximum_frame_latency: NonZero::new(2_u32),
has_window: false,
changed_attr: false,
}
}
}
impl Window {
pub fn new() -> Self {
Self::default()
}
pub fn with_title(mut self, title: &str) -> Self {
self.title = title.to_owned();
self
}
pub fn title(&self) -> String {
self.title.to_owned()
}
pub fn set_title(&mut self, title: &str) {
self.title = title.to_owned();
self.changed_attr = true;
}
pub fn with_name(mut self, name: &str) -> Self {
self.name = Some(name.to_owned());
self
}
pub fn name(&self) -> Option<String> {
self.name.to_owned()
}
pub fn set_name(&mut self, name: &str) {
self.name = Some(name.to_owned());
self.changed_attr = true;
}
pub fn with_position(mut self, position: WindowPosition) -> Self {
self.position = position;
self
}
pub fn position(&self) -> WindowPosition {
self.position
}
pub fn set_position(&mut self, position: WindowPosition) {
self.position = position;
self.changed_attr = true;
}
pub fn with_resolution(mut self, resolution: WindowResolution) -> Self {
self.resolution = resolution;
self
}
pub fn resolution(&self) -> WindowResolution {
self.resolution
}
pub fn set_resolution(&mut self, resolution: WindowResolution) {
self.resolution = resolution;
self.changed_attr = true;
}
pub fn with_resizable(mut self, resizable: bool) -> Self {
self.resizable = resizable;
self
}
pub fn resizable(&self) -> bool {
self.resizable
}
pub fn set_resizable(&mut self, resizable: bool) {
self.resizable = resizable;
self.changed_attr = true;
}
pub fn with_resize_limit(mut self, resize_limit: WindowResizeLimit) -> Self {
self.resize_limit = resize_limit;
self
}
pub fn resize_limit(&self) -> WindowResizeLimit {
self.resize_limit
}
pub fn set_resize_limit(&mut self, resize_limit: WindowResizeLimit) {
self.resize_limit = resize_limit;
self.changed_attr = true;
}
pub fn with_mode(mut self, mode: WindowMode) -> Self {
self.mode = mode;
self
}
pub fn mode(&self) -> WindowMode {
self.mode
}
pub fn set_mode(&mut self, mode: WindowMode) {
self.mode = mode;
self.changed_attr = true;
}
pub fn with_cursor_mode(mut self, cursor_mode: CursorMode) -> Self {
self.cursor_mode = cursor_mode;
self
}
pub fn cursor_mode(&self) -> CursorMode {
self.cursor_mode
}
pub fn set_cursor_mode(&mut self, cursor_mode: CursorMode) {
self.cursor_mode = cursor_mode;
self.changed_attr = true;
}
pub fn with_cursor_visible(mut self, cursor_visible: bool) -> Self {
self.cursor_visible = cursor_visible;
self
}
pub fn cursor_visible(&self) -> bool {
self.cursor_visible
}
pub fn set_cursor_visible(&mut self, cursor_visible: bool) {
self.cursor_visible = cursor_visible;
self.changed_attr = true;
}
pub fn with_decorations(mut self, decorations: bool) -> Self {
self.decorations = decorations;
self
}
pub fn decorations(&self) -> bool {
self.decorations
}
pub fn set_decorations(&mut self, decorations: bool) {
self.decorations = decorations;
self.changed_attr = true;
}
pub fn with_titlebar_buttons(mut self, titlebar_buttons: TitlebarButtons) -> Self {
self.titlebar_buttons = titlebar_buttons;
self
}
pub fn titlebar_buttons(&self) -> TitlebarButtons {
self.titlebar_buttons
}
pub fn set_titlebar_buttons(&mut self, titlebar_buttons: TitlebarButtons) {
self.titlebar_buttons = titlebar_buttons;
self.changed_attr = true;
}
pub fn with_present_mode(mut self, present_mode: PresentMode) -> Self {
self.present_mode = present_mode;
self
}
pub fn present_mode(&self) -> PresentMode {
self.present_mode
}
pub fn set_present_mode(&mut self, present_mode: PresentMode) {
self.present_mode = present_mode;
self.changed_attr = true;
}
pub fn with_alpha_mode(mut self, alpha_mode: AlphaMode) -> Self {
self.alpha_mode = alpha_mode;
self
}
pub fn alpha_mode(&self) -> AlphaMode {
self.alpha_mode
}
pub fn set_alpha_mode(&mut self, alpha_mode: AlphaMode) {
self.alpha_mode = alpha_mode;
self.changed_attr = true;
}
pub fn with_transparent(mut self, transparent: bool) -> Self {
self.transparent = transparent;
self
}
pub fn transparent(&self) -> bool {
self.transparent
}
pub fn set_transparent(&mut self, transparent: bool) {
self.transparent = transparent;
self.changed_attr = true;
}
pub fn with_focused(mut self, focused: bool) -> Self {
self.focused = focused;
self
}
pub fn focused(&self) -> bool {
self.focused
}
pub fn set_focused(&mut self, focused: bool) {
self.focused = focused;
self.changed_attr = true;
}
pub fn with_default_event_handling(mut self, default_event_handling: bool) -> Self {
self.default_event_handling = default_event_handling;
self
}
pub fn default_event_handling(&self) -> bool {
self.default_event_handling
}
pub fn set_default_event_handling(&mut self, default_event_handling: bool) {
self.default_event_handling = default_event_handling;
self.changed_attr = true;
}
pub fn with_window_theme(mut self, window_theme: WindowTheme) -> Self {
self.window_theme = window_theme;
self
}
pub fn window_theme(&self) -> WindowTheme {
self.window_theme
}
pub fn set_window_theme(&mut self, window_theme: WindowTheme) {
self.window_theme = window_theme;
self.changed_attr = true;
}
pub fn with_desired_maximum_frame_latency(
mut self,
desired_maximum_frame_latency: Option<NonZero<u32>>,
) -> Self {
self.desired_maximum_frame_latency = desired_maximum_frame_latency;
self
}
pub fn desired_maximum_frame_latency(&self) -> Option<NonZero<u32>> {
self.desired_maximum_frame_latency
}
pub fn set_desired_maximum_frame_latency(
&mut self,
desired_maximum_frame_latency: Option<NonZero<u32>>,
) {
self.desired_maximum_frame_latency = desired_maximum_frame_latency;
self.changed_attr = true;
}
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Default)]
pub struct ClosingWindow;
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default)]
pub enum WindowPosition {
#[default]
Auto,
Center,
Pos(IVec2),
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct WindowResolution {
width: u32,
height: u32,
}
impl Default for WindowResolution {
fn default() -> Self {
Self {
width: 1280,
height: 720,
}
}
}
impl WindowResolution {
pub const fn new(width: u32, height: u32) -> Self {
Self { width, height }
}
pub const fn width(&self) -> u32 {
self.width
}
pub const fn height(&self) -> u32 {
self.height
}
pub const fn size(&self) -> UVec2 {
UVec2::new(self.width, self.height)
}
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct WindowResizeLimit {
min_width: u32,
min_height: u32,
max_width: u32,
max_height: u32,
}
impl Default for WindowResizeLimit {
fn default() -> Self {
Self {
min_width: 144,
min_height: 256,
max_width: u32::MAX,
max_height: u32::MAX,
}
}
}
impl WindowResizeLimit {
pub const fn new(min_width: u32, min_height: u32, max_width: u32, max_height: u32) -> Self {
Self {
min_width,
min_height,
max_width,
max_height,
}
}
pub const fn min_width(&self) -> u32 {
self.min_width
}
pub const fn min_height(&self) -> u32 {
self.min_height
}
pub const fn max_width(&self) -> u32 {
self.max_width
}
pub const fn max_height(&self) -> u32 {
self.max_height
}
pub const fn min_size(&self) -> UVec2 {
UVec2::new(self.min_width, self.min_height)
}
pub const fn max_size(&self) -> UVec2 {
UVec2::new(self.max_width, self.max_height)
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash, Default)]
pub enum WindowMode {
#[default]
Windowed,
BorderlessFullscreen(MonitorSelection),
Fullscreen(MonitorSelection, VideoModeSelection),
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Default)]
pub enum WindowTheme {
#[default]
Auto,
Light,
Dark,
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Default)]
pub enum MonitorSelection {
#[default]
Current,
Primary,
Entity(Entity),
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default)]
pub enum VideoModeSelection {
#[default]
Current,
Specific {
size: UVec2,
bit_depth: u16,
refresh_rate_millihertz: u32,
},
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Default)]
pub enum CursorMode {
#[default]
Free,
Confined,
Locked,
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Default)]
pub enum PresentMode {
Vsync,
NoVsync,
#[default]
Fifo,
RelaxedFifo,
Mailbox,
Immediate,
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Default)]
pub enum AlphaMode {
#[default]
Auto,
Opaque,
PreMultiplied,
PostMultiplied,
Inherit,
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct TitlebarButtons {
minimize: bool,
maximize: bool,
close: bool,
}
impl Default for TitlebarButtons {
fn default() -> Self {
Self {
minimize: true,
maximize: true,
close: true,
}
}
}
impl TitlebarButtons {
pub const fn new(minimize: bool, maximize: bool, close: bool) -> Self {
Self {
minimize,
maximize,
close,
}
}
pub const fn all_enabled() -> Self {
Self {
minimize: true,
maximize: true,
close: true,
}
}
pub const fn minimize(&self) -> bool {
self.minimize
}
pub const fn maximize(&self) -> bool {
self.maximize
}
pub const fn close(&self) -> bool {
self.close
}
}