Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added set_minimized and set_position to Window #1292

Merged
merged 4 commits into from
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions crates/bevy_window/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,11 @@ pub enum FileDragAndDrop {

HoveredFileCancelled { id: WindowId },
}

/// An event that is sent when a window is repositioned in physical pixels.
#[derive(Debug, Clone)]
pub struct WindowMoved {
pub id: WindowId,
pub x: i32,
pub y: i32,
}
3 changes: 2 additions & 1 deletion crates/bevy_window/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub use windows::*;
pub mod prelude {
pub use crate::{
CursorEntered, CursorLeft, CursorMoved, FileDragAndDrop, ReceivedCharacter, Window,
WindowDescriptor, Windows,
WindowDescriptor, WindowMoved, Windows,
};
}

Expand Down Expand Up @@ -47,6 +47,7 @@ impl Plugin for WindowPlugin {
.add_event::<WindowScaleFactorChanged>()
.add_event::<WindowBackendScaleFactorChanged>()
.add_event::<FileDragAndDrop>()
.add_event::<WindowMoved>()
.init_resource::<Windows>();

if self.add_primary_window {
Expand Down
61 changes: 61 additions & 0 deletions crates/bevy_window/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ pub struct Window {
requested_height: f32,
physical_width: u32,
physical_height: u32,
x: i32,
y: i32,
scale_factor_override: Option<f64>,
backend_scale_factor: f64,
title: String,
Expand Down Expand Up @@ -106,6 +108,13 @@ pub enum WindowCommand {
SetMaximized {
maximized: bool,
},
SetMinimized {
minimized: bool,
},
SetPosition {
x: i32,
y: i32,
},
}

/// Defines the way a window is displayed
Expand All @@ -132,6 +141,8 @@ impl Window {
id,
requested_width: window_descriptor.width,
requested_height: window_descriptor.height,
x: window_descriptor.x,
y: window_descriptor.y,
physical_width,
physical_height,
scale_factor_override: window_descriptor.scale_factor_override,
Expand Down Expand Up @@ -199,12 +210,51 @@ impl Window {
self.physical_height
}

/// The window's client position on the x axis in physical pixels.
#[inline]
pub fn x(&self) -> i32 {
Toniman575 marked this conversation as resolved.
Show resolved Hide resolved
self.x
}

/// The window's client position on the y axis in physical pixels.
#[inline]
pub fn y(&self) -> i32 {
self.y
}

#[inline]
pub fn set_maximized(&mut self, maximized: bool) {
self.command_queue
.push(WindowCommand::SetMaximized { maximized });
}

/// Sets the window to minimized or back.
///
/// # Platform-specific
/// - iOS / Android / Web: Unsupported.
/// - Wayland: Un-minimize is unsupported.
#[inline]
pub fn set_minimized(&mut self, minimized: bool) {
self.command_queue
.push(WindowCommand::SetMinimized { minimized });
}

/// Modifies the position of the window in physical pixels.
///
/// Note that the top-left hand corner of the desktop is not necessarily the same as the screen. If the user uses a desktop with multiple monitors,
/// the top-left hand corner of the desktop is the top-left hand corner of the monitor at the top-left of the desktop. This automatically un-maximizes
/// the window if it's maximized.
///
/// # Platform-specific
///
/// - iOS: Can only be called on the main thread. Sets the top left coordinates of the window in the screen space coordinate system.
/// - Web: Sets the top-left coordinates relative to the viewport.
/// - Android / Wayland: Unsupported.
#[inline]
pub fn set_position(&mut self, x: i32, y: i32) {
self.command_queue.push(WindowCommand::SetPosition { x, y })
}

/// Request the OS to resize the window such the the client area matches the
/// specified width and height.
#[allow(clippy::float_cmp)]
Expand Down Expand Up @@ -250,6 +300,13 @@ impl Window {
self.physical_height = physical_height;
}

#[allow(missing_docs)]
#[inline]
pub fn update_actual_position_from_backend(&mut self, x: i32, y: i32) {
self.x = x;
self.y = y;
}

/// The ratio of physical pixels to logical pixels
///
/// `physical_pixels = logical_pixels * scale_factor`
Expand Down Expand Up @@ -373,6 +430,8 @@ impl Window {

#[derive(Debug, Clone)]
pub struct WindowDescriptor {
pub x: i32,
pub y: i32,
pub width: f32,
pub height: f32,
pub scale_factor_override: Option<f64>,
Expand All @@ -391,6 +450,8 @@ impl Default for WindowDescriptor {
fn default() -> Self {
WindowDescriptor {
title: "bevy".to_string(),
x: 0,
y: 0,
width: 1280.,
height: 720.,
scale_factor_override: None,
Expand Down
20 changes: 19 additions & 1 deletion crates/bevy_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ use bevy_utils::tracing::{error, trace, warn};
use bevy_window::{
CreateWindow, CursorEntered, CursorLeft, CursorMoved, FileDragAndDrop, ReceivedCharacter,
WindowBackendScaleFactorChanged, WindowCloseRequested, WindowCreated, WindowFocused,
WindowResized, WindowScaleFactorChanged, Windows,
WindowMoved, WindowResized, WindowScaleFactorChanged, Windows,
};
use winit::{
dpi::PhysicalPosition,
event::{self, DeviceEvent, Event, WindowEvent},
event_loop::{ControlFlow, EventLoop, EventLoopWindowTarget},
};
Expand Down Expand Up @@ -127,6 +128,14 @@ fn change_window(_: &mut World, resources: &mut Resources) {
let window = winit_windows.get_window(id).unwrap();
window.set_maximized(maximized)
}
bevy_window::WindowCommand::SetMinimized { minimized } => {
let window = winit_windows.get_window(id).unwrap();
window.set_minimized(minimized)
}
bevy_window::WindowCommand::SetPosition { x, y } => {
let window = winit_windows.get_window(id).unwrap();
window.set_outer_position(PhysicalPosition { x, y });
}
}
}
}
Expand Down Expand Up @@ -423,6 +432,15 @@ pub fn winit_runner_with(mut app: App, mut event_loop: EventLoop<()>) {
app.resources.get_mut::<Events<FileDragAndDrop>>().unwrap();
events.send(FileDragAndDrop::HoveredFileCancelled { id: window_id });
}
WindowEvent::Moved(position) => {
window.update_actual_position_from_backend(position.x, position.y);
let mut events = app.resources.get_mut::<Events<WindowMoved>>().unwrap();
events.send(WindowMoved {
id: window_id,
x: position.x,
y: position.y,
});
}
_ => {}
}
}
Expand Down