use bevy_math::{DVec2, IVec2, UVec2, Vec2};
use bevy_reflect::{FromReflect, Reflect};
use bevy_utils::{tracing::warn, Uuid};
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Reflect, FromReflect)]
#[reflect_value(PartialEq, Hash)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct WindowId(Uuid);
#[repr(C)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[doc(alias = "vsync")]
pub enum PresentMode {
AutoVsync = 0,
AutoNoVsync = 1,
Immediate = 2,
Mailbox = 3,
Fifo = 4, }
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub enum CompositeAlphaMode {
Auto = 0,
Opaque = 1,
PreMultiplied = 2,
PostMultiplied = 3,
Inherit = 4,
}
impl WindowId {
pub fn new() -> Self {
WindowId(Uuid::new_v4())
}
pub fn primary() -> Self {
WindowId(Uuid::from_u128(0))
}
pub fn is_primary(&self) -> bool {
*self == WindowId::primary()
}
}
use crate::CursorIcon;
use std::fmt;
use crate::raw_handle::RawHandleWrapper;
impl fmt::Display for WindowId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.as_simple().fmt(f)
}
}
impl Default for WindowId {
fn default() -> Self {
WindowId::primary()
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct WindowResizeConstraints {
pub min_width: f32,
pub min_height: f32,
pub max_width: f32,
pub max_height: f32,
}
impl Default for WindowResizeConstraints {
fn default() -> Self {
Self {
min_width: 180.,
min_height: 120.,
max_width: f32::INFINITY,
max_height: f32::INFINITY,
}
}
}
impl WindowResizeConstraints {
#[must_use]
pub fn check_constraints(&self) -> Self {
let WindowResizeConstraints {
mut min_width,
mut min_height,
mut max_width,
mut max_height,
} = self;
min_width = min_width.max(1.);
min_height = min_height.max(1.);
if max_width < min_width {
warn!(
"The given maximum width {} is smaller than the minimum width {}",
max_width, min_width
);
max_width = min_width;
}
if max_height < min_height {
warn!(
"The given maximum height {} is smaller than the minimum height {}",
max_height, min_height
);
max_height = min_height;
}
WindowResizeConstraints {
min_width,
min_height,
max_width,
max_height,
}
}
}
#[derive(Debug)]
pub struct Window {
id: WindowId,
requested_width: f32,
requested_height: f32,
physical_width: u32,
physical_height: u32,
resize_constraints: WindowResizeConstraints,
position: Option<IVec2>,
scale_factor_override: Option<f64>,
backend_scale_factor: f64,
title: String,
present_mode: PresentMode,
resizable: bool,
decorations: bool,
cursor_icon: CursorIcon,
cursor_visible: bool,
cursor_grab_mode: CursorGrabMode,
physical_cursor_position: Option<DVec2>,
raw_handle: Option<RawHandleWrapper>,
focused: bool,
mode: WindowMode,
canvas: Option<String>,
fit_canvas_to_parent: bool,
command_queue: Vec<WindowCommand>,
alpha_mode: CompositeAlphaMode,
}
#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub enum WindowCommand {
SetWindowMode {
mode: WindowMode,
resolution: UVec2,
},
SetTitle {
title: String,
},
SetScaleFactor {
scale_factor: f64,
},
SetResolution {
logical_resolution: Vec2,
scale_factor: f64,
},
SetPresentMode {
present_mode: PresentMode,
},
SetResizable {
resizable: bool,
},
SetDecorations {
decorations: bool,
},
SetCursorGrabMode {
grab_mode: CursorGrabMode,
},
SetCursorIcon {
icon: CursorIcon,
},
SetCursorVisibility {
visible: bool,
},
SetCursorPosition {
position: Vec2,
},
SetMaximized {
maximized: bool,
},
SetMinimized {
minimized: bool,
},
SetPosition {
monitor_selection: MonitorSelection,
position: IVec2,
},
Center(MonitorSelection),
SetResizeConstraints {
resize_constraints: WindowResizeConstraints,
},
Close,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub enum CursorGrabMode {
None,
Confined,
Locked,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub enum WindowMode {
Windowed,
BorderlessFullscreen,
SizedFullscreen,
Fullscreen,
}
impl Window {
pub fn new(
id: WindowId,
window_descriptor: &WindowDescriptor,
physical_width: u32,
physical_height: u32,
scale_factor: f64,
position: Option<IVec2>,
raw_handle: Option<RawHandleWrapper>,
) -> Self {
Window {
id,
requested_width: window_descriptor.width,
requested_height: window_descriptor.height,
position,
physical_width,
physical_height,
resize_constraints: window_descriptor.resize_constraints,
scale_factor_override: window_descriptor.scale_factor_override,
backend_scale_factor: scale_factor,
title: window_descriptor.title.clone(),
present_mode: window_descriptor.present_mode,
resizable: window_descriptor.resizable,
decorations: window_descriptor.decorations,
cursor_visible: window_descriptor.cursor_visible,
cursor_grab_mode: window_descriptor.cursor_grab_mode,
cursor_icon: CursorIcon::Default,
physical_cursor_position: None,
raw_handle,
focused: true,
mode: window_descriptor.mode,
canvas: window_descriptor.canvas.clone(),
fit_canvas_to_parent: window_descriptor.fit_canvas_to_parent,
command_queue: Vec::new(),
alpha_mode: window_descriptor.alpha_mode,
}
}
#[inline]
pub fn id(&self) -> WindowId {
self.id
}
#[inline]
pub fn width(&self) -> f32 {
(self.physical_width as f64 / self.scale_factor()) as f32
}
#[inline]
pub fn height(&self) -> f32 {
(self.physical_height as f64 / self.scale_factor()) as f32
}
#[inline]
pub fn requested_width(&self) -> f32 {
self.requested_width
}
#[inline]
pub fn requested_height(&self) -> f32 {
self.requested_height
}
#[inline]
pub fn physical_width(&self) -> u32 {
self.physical_width
}
#[inline]
pub fn physical_height(&self) -> u32 {
self.physical_height
}
#[inline]
pub fn resize_constraints(&self) -> WindowResizeConstraints {
self.resize_constraints
}
#[inline]
pub fn position(&self) -> Option<IVec2> {
self.position
}
#[inline]
pub fn set_maximized(&mut self, maximized: bool) {
self.command_queue
.push(WindowCommand::SetMaximized { maximized });
}
#[inline]
pub fn set_minimized(&mut self, minimized: bool) {
self.command_queue
.push(WindowCommand::SetMinimized { minimized });
}
#[inline]
pub fn set_position(&mut self, monitor: MonitorSelection, position: IVec2) {
self.command_queue.push(WindowCommand::SetPosition {
monitor_selection: monitor,
position,
});
}
#[inline]
pub fn center_window(&mut self, monitor_selection: MonitorSelection) {
self.command_queue
.push(WindowCommand::Center(monitor_selection));
}
#[inline]
pub fn set_resize_constraints(&mut self, resize_constraints: WindowResizeConstraints) {
self.command_queue
.push(WindowCommand::SetResizeConstraints { resize_constraints });
}
#[allow(clippy::float_cmp)]
pub fn set_resolution(&mut self, width: f32, height: f32) {
if self.requested_width == width && self.requested_height == height {
return;
}
self.requested_width = width;
self.requested_height = height;
self.command_queue.push(WindowCommand::SetResolution {
logical_resolution: Vec2::new(self.requested_width, self.requested_height),
scale_factor: self.scale_factor(),
});
}
#[allow(clippy::float_cmp)]
pub fn set_scale_factor_override(&mut self, scale_factor: Option<f64>) {
if self.scale_factor_override == scale_factor {
return;
}
self.scale_factor_override = scale_factor;
self.command_queue.push(WindowCommand::SetScaleFactor {
scale_factor: self.scale_factor(),
});
self.command_queue.push(WindowCommand::SetResolution {
logical_resolution: Vec2::new(self.requested_width, self.requested_height),
scale_factor: self.scale_factor(),
});
}
#[allow(missing_docs)]
#[inline]
pub fn update_scale_factor_from_backend(&mut self, scale_factor: f64) {
self.backend_scale_factor = scale_factor;
}
#[allow(missing_docs)]
#[inline]
pub fn update_actual_size_from_backend(&mut self, physical_width: u32, physical_height: u32) {
self.physical_width = physical_width;
self.physical_height = physical_height;
}
#[allow(missing_docs)]
#[inline]
pub fn update_actual_position_from_backend(&mut self, position: IVec2) {
self.position = Some(position);
}
pub fn scale_factor(&self) -> f64 {
self.scale_factor_override
.unwrap_or(self.backend_scale_factor)
}
#[inline]
pub fn backend_scale_factor(&self) -> f64 {
self.backend_scale_factor
}
#[inline]
pub fn scale_factor_override(&self) -> Option<f64> {
self.scale_factor_override
}
#[inline]
pub fn title(&self) -> &str {
&self.title
}
pub fn set_title(&mut self, title: String) {
self.title = title.to_string();
self.command_queue.push(WindowCommand::SetTitle { title });
}
#[inline]
#[doc(alias = "vsync")]
pub fn present_mode(&self) -> PresentMode {
self.present_mode
}
#[inline]
pub fn alpha_mode(&self) -> CompositeAlphaMode {
self.alpha_mode
}
#[inline]
#[doc(alias = "set_vsync")]
pub fn set_present_mode(&mut self, present_mode: PresentMode) {
self.present_mode = present_mode;
self.command_queue
.push(WindowCommand::SetPresentMode { present_mode });
}
#[inline]
pub fn resizable(&self) -> bool {
self.resizable
}
pub fn set_resizable(&mut self, resizable: bool) {
self.resizable = resizable;
self.command_queue
.push(WindowCommand::SetResizable { resizable });
}
#[inline]
pub fn decorations(&self) -> bool {
self.decorations
}
pub fn set_decorations(&mut self, decorations: bool) {
self.decorations = decorations;
self.command_queue
.push(WindowCommand::SetDecorations { decorations });
}
#[inline]
pub fn cursor_grab_mode(&self) -> CursorGrabMode {
self.cursor_grab_mode
}
pub fn set_cursor_grab_mode(&mut self, grab_mode: CursorGrabMode) {
self.cursor_grab_mode = grab_mode;
self.command_queue
.push(WindowCommand::SetCursorGrabMode { grab_mode });
}
#[inline]
pub fn cursor_visible(&self) -> bool {
self.cursor_visible
}
pub fn set_cursor_visibility(&mut self, visible_mode: bool) {
self.cursor_visible = visible_mode;
self.command_queue.push(WindowCommand::SetCursorVisibility {
visible: visible_mode,
});
}
#[inline]
pub fn cursor_icon(&self) -> CursorIcon {
self.cursor_icon
}
pub fn set_cursor_icon(&mut self, icon: CursorIcon) {
self.command_queue
.push(WindowCommand::SetCursorIcon { icon });
}
#[inline]
pub fn physical_cursor_position(&self) -> Option<DVec2> {
self.physical_cursor_position
}
#[inline]
#[doc(alias = "mouse position")]
pub fn cursor_position(&self) -> Option<Vec2> {
self.physical_cursor_position
.map(|p| (p / self.scale_factor()).as_vec2())
}
pub fn set_cursor_position(&mut self, position: Vec2) {
self.command_queue
.push(WindowCommand::SetCursorPosition { position });
}
#[allow(missing_docs)]
#[inline]
pub fn update_focused_status_from_backend(&mut self, focused: bool) {
self.focused = focused;
}
#[allow(missing_docs)]
#[inline]
pub fn update_cursor_physical_position_from_backend(&mut self, cursor_position: Option<DVec2>) {
self.physical_cursor_position = cursor_position;
}
#[inline]
pub fn mode(&self) -> WindowMode {
self.mode
}
pub fn set_mode(&mut self, mode: WindowMode) {
self.mode = mode;
self.command_queue.push(WindowCommand::SetWindowMode {
mode,
resolution: UVec2::new(self.physical_width, self.physical_height),
});
}
pub fn close(&mut self) {
self.command_queue.push(WindowCommand::Close);
}
#[inline]
pub fn drain_commands(&mut self) -> impl Iterator<Item = WindowCommand> + '_ {
self.command_queue.drain(..)
}
#[inline]
pub fn is_focused(&self) -> bool {
self.focused
}
pub fn raw_handle(&self) -> Option<RawHandleWrapper> {
self.raw_handle.as_ref().cloned()
}
#[inline]
pub fn canvas(&self) -> Option<&str> {
self.canvas.as_deref()
}
#[inline]
pub fn fit_canvas_to_parent(&self) -> bool {
self.fit_canvas_to_parent
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub enum WindowPosition {
Automatic,
Centered,
At(Vec2),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub enum MonitorSelection {
Current,
Primary,
Index(usize),
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct WindowDescriptor {
pub width: f32,
pub height: f32,
pub position: WindowPosition,
pub monitor: MonitorSelection,
pub resize_constraints: WindowResizeConstraints,
pub scale_factor_override: Option<f64>,
pub title: String,
#[doc(alias = "vsync")]
pub present_mode: PresentMode,
pub resizable: bool,
pub decorations: bool,
pub cursor_visible: bool,
pub cursor_grab_mode: CursorGrabMode,
pub mode: WindowMode,
pub transparent: bool,
pub canvas: Option<String>,
pub fit_canvas_to_parent: bool,
pub alpha_mode: CompositeAlphaMode,
}
impl Default for WindowDescriptor {
fn default() -> Self {
WindowDescriptor {
title: "app".to_string(),
width: 1280.,
height: 720.,
position: WindowPosition::Automatic,
monitor: MonitorSelection::Current,
resize_constraints: WindowResizeConstraints::default(),
scale_factor_override: None,
present_mode: PresentMode::Fifo,
resizable: true,
decorations: true,
cursor_grab_mode: CursorGrabMode::None,
cursor_visible: true,
mode: WindowMode::Windowed,
transparent: false,
canvas: None,
fit_canvas_to_parent: false,
alpha_mode: CompositeAlphaMode::Auto,
}
}
}