Use actual floats for logical coordinates
This commit is contained in:
parent
9f29aec128
commit
67408311f4
12 changed files with 165 additions and 136 deletions
|
|
@ -1,26 +1,34 @@
|
||||||
use crate::Vector;
|
use crate::Vector;
|
||||||
|
|
||||||
|
use num_traits::{Float, Num};
|
||||||
|
use std::fmt;
|
||||||
|
|
||||||
/// A 2D point.
|
/// A 2D point.
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Default)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
||||||
pub struct Point {
|
pub struct Point<T = f32> {
|
||||||
/// The X coordinate.
|
/// The X coordinate.
|
||||||
pub x: f32,
|
pub x: T,
|
||||||
|
|
||||||
/// The Y coordinate.
|
/// The Y coordinate.
|
||||||
pub y: f32,
|
pub y: T,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Point {
|
impl Point {
|
||||||
/// The origin (i.e. a [`Point`] at (0, 0)).
|
/// The origin (i.e. a [`Point`] at (0, 0)).
|
||||||
pub const ORIGIN: Point = Point::new(0.0, 0.0);
|
pub const ORIGIN: Self = Self::new(0.0, 0.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Num> Point<T> {
|
||||||
/// Creates a new [`Point`] with the given coordinates.
|
/// Creates a new [`Point`] with the given coordinates.
|
||||||
pub const fn new(x: f32, y: f32) -> Self {
|
pub const fn new(x: T, y: T) -> Self {
|
||||||
Self { x, y }
|
Self { x, y }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Computes the distance to another [`Point`].
|
/// Computes the distance to another [`Point`].
|
||||||
pub fn distance(&self, to: Point) -> f32 {
|
pub fn distance(&self, to: Self) -> T
|
||||||
|
where
|
||||||
|
T: Float,
|
||||||
|
{
|
||||||
let a = self.x - to.x;
|
let a = self.x - to.x;
|
||||||
let b = self.y - to.y;
|
let b = self.y - to.y;
|
||||||
|
|
||||||
|
|
@ -34,9 +42,9 @@ impl From<[f32; 2]> for Point {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<[u16; 2]> for Point {
|
impl From<[u16; 2]> for Point<u16> {
|
||||||
fn from([x, y]: [u16; 2]) -> Self {
|
fn from([x, y]: [u16; 2]) -> Self {
|
||||||
Point::new(x.into(), y.into())
|
Point::new(x, y)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -46,10 +54,13 @@ impl From<Point> for [f32; 2] {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::ops::Add<Vector> for Point {
|
impl<T> std::ops::Add<Vector<T>> for Point<T>
|
||||||
|
where
|
||||||
|
T: std::ops::Add<Output = T>,
|
||||||
|
{
|
||||||
type Output = Self;
|
type Output = Self;
|
||||||
|
|
||||||
fn add(self, vector: Vector) -> Self {
|
fn add(self, vector: Vector<T>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
x: self.x + vector.x,
|
x: self.x + vector.x,
|
||||||
y: self.y + vector.y,
|
y: self.y + vector.y,
|
||||||
|
|
@ -57,10 +68,13 @@ impl std::ops::Add<Vector> for Point {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::ops::Sub<Vector> for Point {
|
impl<T> std::ops::Sub<Vector<T>> for Point<T>
|
||||||
|
where
|
||||||
|
T: std::ops::Sub<Output = T>,
|
||||||
|
{
|
||||||
type Output = Self;
|
type Output = Self;
|
||||||
|
|
||||||
fn sub(self, vector: Vector) -> Self {
|
fn sub(self, vector: Vector<T>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
x: self.x - vector.x,
|
x: self.x - vector.x,
|
||||||
y: self.y - vector.y,
|
y: self.y - vector.y,
|
||||||
|
|
@ -68,10 +82,22 @@ impl std::ops::Sub<Vector> for Point {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::ops::Sub<Point> for Point {
|
impl<T> std::ops::Sub<Point<T>> for Point<T>
|
||||||
type Output = Vector;
|
where
|
||||||
|
T: std::ops::Sub<Output = T>,
|
||||||
|
{
|
||||||
|
type Output = Vector<T>;
|
||||||
|
|
||||||
fn sub(self, point: Point) -> Vector {
|
fn sub(self, point: Self) -> Vector<T> {
|
||||||
Vector::new(self.x - point.x, self.y - point.y)
|
Vector::new(self.x - point.x, self.y - point.y)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T> fmt::Display for Point<T>
|
||||||
|
where
|
||||||
|
T: fmt::Display,
|
||||||
|
{
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
write!(f, "Point {{ x: {}, y: {} }}", self.x, self.y)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,10 @@
|
||||||
use crate::time::Instant;
|
use crate::time::Instant;
|
||||||
use crate::Size;
|
use crate::{Point, Size};
|
||||||
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
/// A window-related event.
|
/// A window-related event.
|
||||||
#[derive(PartialEq, Eq, Clone, Debug)]
|
#[derive(PartialEq, Clone, Debug)]
|
||||||
pub enum Event {
|
pub enum Event {
|
||||||
/// A window was moved.
|
/// A window was moved.
|
||||||
Moved {
|
Moved {
|
||||||
|
|
@ -30,22 +30,22 @@ pub enum Event {
|
||||||
/// The user has requested for the window to close.
|
/// The user has requested for the window to close.
|
||||||
CloseRequested,
|
CloseRequested,
|
||||||
|
|
||||||
/// A window was destroyed by the runtime.
|
|
||||||
Destroyed,
|
|
||||||
|
|
||||||
/// A window was created.
|
/// A window was created.
|
||||||
///
|
|
||||||
/// **Note:** this event is not supported on Wayland.
|
|
||||||
Created {
|
Created {
|
||||||
/// The position of the created window. This is relative to the top-left corner of the desktop
|
/// The position of the created window. This is relative to the top-left corner of the desktop
|
||||||
/// the window is on, including virtual desktops. Refers to window's "inner" position,
|
/// the window is on, including virtual desktops. Refers to window's "inner" position,
|
||||||
/// or the client area, in logical pixels.
|
/// or the client area, in logical pixels.
|
||||||
position: (i32, i32),
|
///
|
||||||
|
/// **Note**: Not available in Wayland.
|
||||||
|
position: Option<Point>,
|
||||||
/// The size of the created window. This is its "inner" size, or the size of the
|
/// The size of the created window. This is its "inner" size, or the size of the
|
||||||
/// client area, in logical pixels.
|
/// client area, in logical pixels.
|
||||||
size: Size<u32>,
|
size: Size,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/// A window was destroyed by the runtime.
|
||||||
|
Destroyed,
|
||||||
|
|
||||||
/// A window was focused.
|
/// A window was focused.
|
||||||
Focused,
|
Focused,
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
|
use crate::Point;
|
||||||
|
|
||||||
/// The position of a window in a given screen.
|
/// The position of a window in a given screen.
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
pub enum Position {
|
pub enum Position {
|
||||||
/// The platform-specific default position for a new window.
|
/// The platform-specific default position for a new window.
|
||||||
Default,
|
Default,
|
||||||
|
|
@ -12,7 +14,7 @@ pub enum Position {
|
||||||
/// position. So if you have decorations enabled and want the window to be
|
/// position. So if you have decorations enabled and want the window to be
|
||||||
/// at (0, 0) you would have to set the position to
|
/// at (0, 0) you would have to set the position to
|
||||||
/// `(PADDING_X, PADDING_Y)`.
|
/// `(PADDING_X, PADDING_Y)`.
|
||||||
Specific(i32, i32),
|
Specific(Point),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Position {
|
impl Default for Position {
|
||||||
|
|
|
||||||
|
|
@ -25,22 +25,23 @@ mod platform;
|
||||||
mod platform;
|
mod platform;
|
||||||
|
|
||||||
use crate::window::{Icon, Level, Position};
|
use crate::window::{Icon, Level, Position};
|
||||||
|
use crate::Size;
|
||||||
|
|
||||||
pub use platform::PlatformSpecific;
|
pub use platform::PlatformSpecific;
|
||||||
/// The window settings of an application.
|
/// The window settings of an application.
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Settings {
|
pub struct Settings {
|
||||||
/// The initial size of the window.
|
/// The initial logical dimensions of the window.
|
||||||
pub size: (u32, u32),
|
pub size: Size,
|
||||||
|
|
||||||
/// The initial position of the window.
|
/// The initial position of the window.
|
||||||
pub position: Position,
|
pub position: Position,
|
||||||
|
|
||||||
/// The minimum size of the window.
|
/// The minimum size of the window.
|
||||||
pub min_size: Option<(u32, u32)>,
|
pub min_size: Option<Size>,
|
||||||
|
|
||||||
/// The maximum size of the window.
|
/// The maximum size of the window.
|
||||||
pub max_size: Option<(u32, u32)>,
|
pub max_size: Option<Size>,
|
||||||
|
|
||||||
/// Whether the window should be visible or not.
|
/// Whether the window should be visible or not.
|
||||||
pub visible: bool,
|
pub visible: bool,
|
||||||
|
|
@ -77,7 +78,7 @@ pub struct Settings {
|
||||||
impl Default for Settings {
|
impl Default for Settings {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
size: (1024, 768),
|
size: Size::new(1024.0, 768.0),
|
||||||
position: Position::default(),
|
position: Position::default(),
|
||||||
min_size: None,
|
min_size: None,
|
||||||
max_size: None,
|
max_size: None,
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,10 @@ use iced::multi_window::{self, Application};
|
||||||
use iced::widget::{button, column, container, scrollable, text, text_input};
|
use iced::widget::{button, column, container, scrollable, text, text_input};
|
||||||
use iced::window;
|
use iced::window;
|
||||||
use iced::{
|
use iced::{
|
||||||
Alignment, Command, Element, Length, Settings, Subscription, Theme,
|
Alignment, Command, Element, Length, Point, Settings, Subscription, Theme,
|
||||||
|
Vector,
|
||||||
};
|
};
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
fn main() -> iced::Result {
|
fn main() -> iced::Result {
|
||||||
|
|
@ -33,8 +35,8 @@ enum Message {
|
||||||
ScaleChanged(window::Id, String),
|
ScaleChanged(window::Id, String),
|
||||||
TitleChanged(window::Id, String),
|
TitleChanged(window::Id, String),
|
||||||
CloseWindow(window::Id),
|
CloseWindow(window::Id),
|
||||||
|
WindowCreated(window::Id, Option<Point>),
|
||||||
WindowDestroyed(window::Id),
|
WindowDestroyed(window::Id),
|
||||||
WindowCreated(window::Id, (i32, i32)),
|
|
||||||
NewWindow,
|
NewWindow,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -90,10 +92,11 @@ impl multi_window::Application for Example {
|
||||||
self.windows.remove(&id);
|
self.windows.remove(&id);
|
||||||
}
|
}
|
||||||
Message::WindowCreated(id, position) => {
|
Message::WindowCreated(id, position) => {
|
||||||
self.next_window_pos = window::Position::Specific(
|
if let Some(position) = position {
|
||||||
position.0 + 20,
|
self.next_window_pos = window::Position::Specific(
|
||||||
position.1 + 20,
|
position + Vector::new(20.0, 20.0),
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(window) = self.windows.get(&id) {
|
if let Some(window) = self.windows.get(&id) {
|
||||||
return text_input::focus(window.input_id.clone());
|
return text_input::focus(window.input_id.clone());
|
||||||
|
|
|
||||||
|
|
@ -114,14 +114,14 @@ impl State {
|
||||||
|
|
||||||
pub fn new() -> State {
|
pub fn new() -> State {
|
||||||
let now = Instant::now();
|
let now = Instant::now();
|
||||||
let (width, height) = window::Settings::default().size;
|
let size = window::Settings::default().size;
|
||||||
|
|
||||||
State {
|
State {
|
||||||
space_cache: canvas::Cache::default(),
|
space_cache: canvas::Cache::default(),
|
||||||
system_cache: canvas::Cache::default(),
|
system_cache: canvas::Cache::default(),
|
||||||
start: now,
|
start: now,
|
||||||
now,
|
now,
|
||||||
stars: Self::generate_stars(width, height),
|
stars: Self::generate_stars(size.width, size.height),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -130,7 +130,7 @@ impl State {
|
||||||
self.system_cache.clear();
|
self.system_cache.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generate_stars(width: u32, height: u32) -> Vec<(Point, f32)> {
|
fn generate_stars(width: f32, height: f32) -> Vec<(Point, f32)> {
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
|
|
||||||
let mut rng = rand::thread_rng();
|
let mut rng = rand::thread_rng();
|
||||||
|
|
@ -139,12 +139,8 @@ impl State {
|
||||||
.map(|_| {
|
.map(|_| {
|
||||||
(
|
(
|
||||||
Point::new(
|
Point::new(
|
||||||
rng.gen_range(
|
rng.gen_range((-width / 2.0)..(width / 2.0)),
|
||||||
(-(width as f32) / 2.0)..(width as f32 / 2.0),
|
rng.gen_range((-height / 2.0)..(height / 2.0)),
|
||||||
),
|
|
||||||
rng.gen_range(
|
|
||||||
(-(height as f32) / 2.0)..(height as f32 / 2.0),
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
rng.gen_range(0.5..1.0),
|
rng.gen_range(0.5..1.0),
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ use iced::widget::{
|
||||||
};
|
};
|
||||||
use iced::window;
|
use iced::window;
|
||||||
use iced::{Application, Element};
|
use iced::{Application, Element};
|
||||||
use iced::{Color, Command, Length, Settings, Subscription};
|
use iced::{Color, Command, Length, Settings, Size, Subscription};
|
||||||
|
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
@ -22,7 +22,7 @@ pub fn main() -> iced::Result {
|
||||||
|
|
||||||
Todos::run(Settings {
|
Todos::run(Settings {
|
||||||
window: window::Settings {
|
window: window::Settings {
|
||||||
size: (500, 800),
|
size: Size::new(500.0, 800.0),
|
||||||
..window::Settings::default()
|
..window::Settings::default()
|
||||||
},
|
},
|
||||||
..Settings::default()
|
..Settings::default()
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ pub use screenshot::Screenshot;
|
||||||
use crate::command::{self, Command};
|
use crate::command::{self, Command};
|
||||||
use crate::core::time::Instant;
|
use crate::core::time::Instant;
|
||||||
use crate::core::window::{self, Event, Icon, Level, Mode, UserAttention};
|
use crate::core::window::{self, Event, Icon, Level, Mode, UserAttention};
|
||||||
use crate::core::Size;
|
use crate::core::{Point, Size};
|
||||||
use crate::futures::event;
|
use crate::futures::event;
|
||||||
use crate::futures::Subscription;
|
use crate::futures::Subscription;
|
||||||
|
|
||||||
|
|
@ -48,17 +48,14 @@ pub fn drag<Message>(id: window::Id) -> Command<Message> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Resizes the window to the given logical dimensions.
|
/// Resizes the window to the given logical dimensions.
|
||||||
pub fn resize<Message>(
|
pub fn resize<Message>(id: window::Id, new_size: Size) -> Command<Message> {
|
||||||
id: window::Id,
|
|
||||||
new_size: Size<u32>,
|
|
||||||
) -> Command<Message> {
|
|
||||||
Command::single(command::Action::Window(id, Action::Resize(new_size)))
|
Command::single(command::Action::Window(id, Action::Resize(new_size)))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Fetches the window's size in logical dimensions.
|
/// Fetches the window's size in logical dimensions.
|
||||||
pub fn fetch_size<Message>(
|
pub fn fetch_size<Message>(
|
||||||
id: window::Id,
|
id: window::Id,
|
||||||
f: impl FnOnce(Size<u32>) -> Message + 'static,
|
f: impl FnOnce(Size) -> Message + 'static,
|
||||||
) -> Command<Message> {
|
) -> Command<Message> {
|
||||||
Command::single(command::Action::Window(id, Action::FetchSize(Box::new(f))))
|
Command::single(command::Action::Window(id, Action::FetchSize(Box::new(f))))
|
||||||
}
|
}
|
||||||
|
|
@ -74,8 +71,8 @@ pub fn minimize<Message>(id: window::Id, minimized: bool) -> Command<Message> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Moves the window to the given logical coordinates.
|
/// Moves the window to the given logical coordinates.
|
||||||
pub fn move_to<Message>(id: window::Id, x: i32, y: i32) -> Command<Message> {
|
pub fn move_to<Message>(id: window::Id, position: Point) -> Command<Message> {
|
||||||
Command::single(command::Action::Window(id, Action::Move { x, y }))
|
Command::single(command::Action::Window(id, Action::Move(position)))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Changes the [`Mode`] of the window.
|
/// Changes the [`Mode`] of the window.
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
use crate::core::window::{Icon, Level, Mode, Settings, UserAttention};
|
use crate::core::window::{Icon, Level, Mode, Settings, UserAttention};
|
||||||
use crate::core::Size;
|
use crate::core::{Point, Size};
|
||||||
use crate::futures::MaybeSend;
|
use crate::futures::MaybeSend;
|
||||||
use crate::window::Screenshot;
|
use crate::window::Screenshot;
|
||||||
|
|
||||||
|
|
@ -20,23 +20,18 @@ pub enum Action<T> {
|
||||||
/// The settings of the window.
|
/// The settings of the window.
|
||||||
settings: Settings,
|
settings: Settings,
|
||||||
},
|
},
|
||||||
/// Resize the window.
|
/// Resize the window to the given logical dimensions.
|
||||||
Resize(Size<u32>),
|
Resize(Size),
|
||||||
/// Fetch the current size of the window.
|
/// Fetch the current logical dimensions of the window.
|
||||||
FetchSize(Box<dyn FnOnce(Size<u32>) -> T + 'static>),
|
FetchSize(Box<dyn FnOnce(Size) -> T + 'static>),
|
||||||
/// Set the window to maximized or back
|
/// Set the window to maximized or back
|
||||||
Maximize(bool),
|
Maximize(bool),
|
||||||
/// Set the window to minimized or back
|
/// Set the window to minimized or back
|
||||||
Minimize(bool),
|
Minimize(bool),
|
||||||
/// Move the window.
|
/// Move the window to the given logical coordinates.
|
||||||
///
|
///
|
||||||
/// Unsupported on Wayland.
|
/// Unsupported on Wayland.
|
||||||
Move {
|
Move(Point),
|
||||||
/// The new logical x location of the window
|
|
||||||
x: i32,
|
|
||||||
/// The new logical y location of the window
|
|
||||||
y: i32,
|
|
||||||
},
|
|
||||||
/// Change the [`Mode`] of the window.
|
/// Change the [`Mode`] of the window.
|
||||||
ChangeMode(Mode),
|
ChangeMode(Mode),
|
||||||
/// Fetch the current [`Mode`] of the window.
|
/// Fetch the current [`Mode`] of the window.
|
||||||
|
|
@ -114,7 +109,7 @@ impl<T> Action<T> {
|
||||||
Self::FetchSize(o) => Action::FetchSize(Box::new(move |s| f(o(s)))),
|
Self::FetchSize(o) => Action::FetchSize(Box::new(move |s| f(o(s)))),
|
||||||
Self::Maximize(maximized) => Action::Maximize(maximized),
|
Self::Maximize(maximized) => Action::Maximize(maximized),
|
||||||
Self::Minimize(minimized) => Action::Minimize(minimized),
|
Self::Minimize(minimized) => Action::Minimize(minimized),
|
||||||
Self::Move { x, y } => Action::Move { x, y },
|
Self::Move(position) => Action::Move(position),
|
||||||
Self::ChangeMode(mode) => Action::ChangeMode(mode),
|
Self::ChangeMode(mode) => Action::ChangeMode(mode),
|
||||||
Self::FetchMode(o) => Action::FetchMode(Box::new(move |s| f(o(s)))),
|
Self::FetchMode(o) => Action::FetchMode(Box::new(move |s| f(o(s)))),
|
||||||
Self::ToggleMaximize => Action::ToggleMaximize,
|
Self::ToggleMaximize => Action::ToggleMaximize,
|
||||||
|
|
@ -151,8 +146,8 @@ impl<T> fmt::Debug for Action<T> {
|
||||||
Self::Minimize(minimized) => {
|
Self::Minimize(minimized) => {
|
||||||
write!(f, "Action::Minimize({minimized}")
|
write!(f, "Action::Minimize({minimized}")
|
||||||
}
|
}
|
||||||
Self::Move { x, y } => {
|
Self::Move(position) => {
|
||||||
write!(f, "Action::Move {{ x: {x}, y: {y} }}")
|
write!(f, "Action::Move({position})")
|
||||||
}
|
}
|
||||||
Self::ChangeMode(mode) => write!(f, "Action::SetMode({mode:?})"),
|
Self::ChangeMode(mode) => write!(f, "Action::SetMode({mode:?})"),
|
||||||
Self::FetchMode(_) => write!(f, "Action::FetchMode"),
|
Self::FetchMode(_) => write!(f, "Action::FetchMode"),
|
||||||
|
|
|
||||||
|
|
@ -732,7 +732,8 @@ pub fn run_command<A, C, E>(
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
window::Action::FetchSize(callback) => {
|
window::Action::FetchSize(callback) => {
|
||||||
let size = window.inner_size();
|
let size =
|
||||||
|
window.inner_size().to_logical(window.scale_factor());
|
||||||
|
|
||||||
proxy
|
proxy
|
||||||
.send_event(callback(Size::new(
|
.send_event(callback(Size::new(
|
||||||
|
|
@ -747,10 +748,10 @@ pub fn run_command<A, C, E>(
|
||||||
window::Action::Minimize(minimized) => {
|
window::Action::Minimize(minimized) => {
|
||||||
window.set_minimized(minimized);
|
window.set_minimized(minimized);
|
||||||
}
|
}
|
||||||
window::Action::Move { x, y } => {
|
window::Action::Move(position) => {
|
||||||
window.set_outer_position(winit::dpi::LogicalPosition {
|
window.set_outer_position(winit::dpi::LogicalPosition {
|
||||||
x,
|
x: position.x,
|
||||||
y,
|
y: position.y,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
window::Action::ChangeMode(mode) => {
|
window::Action::ChangeMode(mode) => {
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ use crate::core::keyboard;
|
||||||
use crate::core::mouse;
|
use crate::core::mouse;
|
||||||
use crate::core::touch;
|
use crate::core::touch;
|
||||||
use crate::core::window;
|
use crate::core::window;
|
||||||
use crate::core::{Event, Point};
|
use crate::core::{Event, Point, Size};
|
||||||
|
|
||||||
/// Converts some [`window::Settings`] into a `WindowBuilder` from `winit`.
|
/// Converts some [`window::Settings`] into a `WindowBuilder` from `winit`.
|
||||||
pub fn window_settings(
|
pub fn window_settings(
|
||||||
|
|
@ -17,11 +17,12 @@ pub fn window_settings(
|
||||||
) -> winit::window::WindowBuilder {
|
) -> winit::window::WindowBuilder {
|
||||||
let mut window_builder = winit::window::WindowBuilder::new();
|
let mut window_builder = winit::window::WindowBuilder::new();
|
||||||
|
|
||||||
let (width, height) = settings.size;
|
|
||||||
|
|
||||||
window_builder = window_builder
|
window_builder = window_builder
|
||||||
.with_title(title)
|
.with_title(title)
|
||||||
.with_inner_size(winit::dpi::LogicalSize { width, height })
|
.with_inner_size(winit::dpi::LogicalSize {
|
||||||
|
width: settings.size.width,
|
||||||
|
height: settings.size.height,
|
||||||
|
})
|
||||||
.with_resizable(settings.resizable)
|
.with_resizable(settings.resizable)
|
||||||
.with_enabled_buttons(if settings.resizable {
|
.with_enabled_buttons(if settings.resizable {
|
||||||
winit::window::WindowButtons::all()
|
winit::window::WindowButtons::all()
|
||||||
|
|
@ -41,14 +42,20 @@ pub fn window_settings(
|
||||||
window_builder = window_builder.with_position(position);
|
window_builder = window_builder.with_position(position);
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some((width, height)) = settings.min_size {
|
if let Some(min_size) = settings.min_size {
|
||||||
window_builder = window_builder
|
window_builder =
|
||||||
.with_min_inner_size(winit::dpi::LogicalSize { width, height });
|
window_builder.with_min_inner_size(winit::dpi::LogicalSize {
|
||||||
|
width: min_size.width,
|
||||||
|
height: min_size.height,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some((width, height)) = settings.max_size {
|
if let Some(max_size) = settings.max_size {
|
||||||
window_builder = window_builder
|
window_builder =
|
||||||
.with_max_inner_size(winit::dpi::LogicalSize { width, height });
|
window_builder.with_max_inner_size(winit::dpi::LogicalSize {
|
||||||
|
width: max_size.width,
|
||||||
|
height: max_size.height,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(
|
#[cfg(any(
|
||||||
|
|
@ -277,15 +284,15 @@ pub fn window_level(level: window::Level) -> winit::window::WindowLevel {
|
||||||
/// [`winit`]: https://github.com/rust-windowing/winit
|
/// [`winit`]: https://github.com/rust-windowing/winit
|
||||||
pub fn position(
|
pub fn position(
|
||||||
monitor: Option<&winit::monitor::MonitorHandle>,
|
monitor: Option<&winit::monitor::MonitorHandle>,
|
||||||
(width, height): (u32, u32),
|
size: Size,
|
||||||
position: window::Position,
|
position: window::Position,
|
||||||
) -> Option<winit::dpi::Position> {
|
) -> Option<winit::dpi::Position> {
|
||||||
match position {
|
match position {
|
||||||
window::Position::Default => None,
|
window::Position::Default => None,
|
||||||
window::Position::Specific(x, y) => {
|
window::Position::Specific(position) => {
|
||||||
Some(winit::dpi::Position::Logical(winit::dpi::LogicalPosition {
|
Some(winit::dpi::Position::Logical(winit::dpi::LogicalPosition {
|
||||||
x: f64::from(x),
|
x: f64::from(position.x),
|
||||||
y: f64::from(y),
|
y: f64::from(position.y),
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
window::Position::Centered => {
|
window::Position::Centered => {
|
||||||
|
|
@ -297,8 +304,8 @@ pub fn position(
|
||||||
|
|
||||||
let centered: winit::dpi::PhysicalPosition<i32> =
|
let centered: winit::dpi::PhysicalPosition<i32> =
|
||||||
winit::dpi::LogicalPosition {
|
winit::dpi::LogicalPosition {
|
||||||
x: (resolution.width - f64::from(width)) / 2.0,
|
x: (resolution.width - f64::from(size.width)) / 2.0,
|
||||||
y: (resolution.height - f64::from(height)) / 2.0,
|
y: (resolution.height - f64::from(size.height)) / 2.0,
|
||||||
}
|
}
|
||||||
.to_physical(monitor.scale_factor());
|
.to_physical(monitor.scale_factor());
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,8 +5,12 @@ mod windows;
|
||||||
pub use state::State;
|
pub use state::State;
|
||||||
|
|
||||||
use crate::conversion;
|
use crate::conversion;
|
||||||
|
use crate::core;
|
||||||
|
use crate::core::mouse;
|
||||||
|
use crate::core::renderer;
|
||||||
use crate::core::widget::operation;
|
use crate::core::widget::operation;
|
||||||
use crate::core::{self, mouse, renderer, window, Size};
|
use crate::core::window;
|
||||||
|
use crate::core::{Point, Size};
|
||||||
use crate::futures::futures::channel::mpsc;
|
use crate::futures::futures::channel::mpsc;
|
||||||
use crate::futures::futures::{task, Future, StreamExt};
|
use crate::futures::futures::{task, Future, StreamExt};
|
||||||
use crate::futures::{Executor, Runtime, Subscription};
|
use crate::futures::{Executor, Runtime, Subscription};
|
||||||
|
|
@ -350,18 +354,18 @@ async fn run_instance<A, E, C>(
|
||||||
|
|
||||||
let mut mouse_interaction = mouse::Interaction::default();
|
let mut mouse_interaction = mouse::Interaction::default();
|
||||||
|
|
||||||
let mut events =
|
let mut events = {
|
||||||
if let Some((position, size)) = logical_bounds_of(windows.main()) {
|
let (position, size) = logical_bounds_of(windows.main());
|
||||||
vec![(
|
|
||||||
Some(window::Id::MAIN),
|
vec![(
|
||||||
core::Event::Window(
|
Some(window::Id::MAIN),
|
||||||
window::Id::MAIN,
|
core::Event::Window(
|
||||||
window::Event::Created { position, size },
|
window::Id::MAIN,
|
||||||
),
|
window::Event::Created { position, size },
|
||||||
)]
|
),
|
||||||
} else {
|
)]
|
||||||
Vec::new()
|
};
|
||||||
};
|
|
||||||
let mut messages = Vec::new();
|
let mut messages = Vec::new();
|
||||||
let mut redraw_pending = false;
|
let mut redraw_pending = false;
|
||||||
|
|
||||||
|
|
@ -374,7 +378,7 @@ async fn run_instance<A, E, C>(
|
||||||
window,
|
window,
|
||||||
exit_on_close_request,
|
exit_on_close_request,
|
||||||
} => {
|
} => {
|
||||||
let bounds = logical_bounds_of(&window);
|
let (position, size) = logical_bounds_of(&window);
|
||||||
|
|
||||||
let (inner_size, i) = windows.add(
|
let (inner_size, i) = windows.add(
|
||||||
&application,
|
&application,
|
||||||
|
|
@ -394,18 +398,13 @@ async fn run_instance<A, E, C>(
|
||||||
));
|
));
|
||||||
ui_caches.push(user_interface::Cache::default());
|
ui_caches.push(user_interface::Cache::default());
|
||||||
|
|
||||||
if let Some(bounds) = bounds {
|
events.push((
|
||||||
events.push((
|
Some(id),
|
||||||
Some(id),
|
core::Event::Window(
|
||||||
core::Event::Window(
|
id,
|
||||||
id,
|
window::Event::Created { position, size },
|
||||||
window::Event::Created {
|
),
|
||||||
position: bounds.0,
|
));
|
||||||
size: bounds.1,
|
|
||||||
},
|
|
||||||
),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Event::EventLoopAwakened(event) => {
|
Event::EventLoopAwakened(event) => {
|
||||||
match event {
|
match event {
|
||||||
|
|
@ -925,7 +924,8 @@ fn run_command<A, C, E>(
|
||||||
}
|
}
|
||||||
window::Action::FetchSize(callback) => {
|
window::Action::FetchSize(callback) => {
|
||||||
let window = windows.with_raw(id);
|
let window = windows.with_raw(id);
|
||||||
let size = window.inner_size();
|
let size =
|
||||||
|
window.inner_size().to_logical(window.scale_factor());
|
||||||
|
|
||||||
proxy
|
proxy
|
||||||
.send_event(callback(Size::new(
|
.send_event(callback(Size::new(
|
||||||
|
|
@ -940,9 +940,12 @@ fn run_command<A, C, E>(
|
||||||
window::Action::Minimize(minimized) => {
|
window::Action::Minimize(minimized) => {
|
||||||
windows.with_raw(id).set_minimized(minimized);
|
windows.with_raw(id).set_minimized(minimized);
|
||||||
}
|
}
|
||||||
window::Action::Move { x, y } => {
|
window::Action::Move(position) => {
|
||||||
windows.with_raw(id).set_outer_position(
|
windows.with_raw(id).set_outer_position(
|
||||||
winit::dpi::LogicalPosition { x, y },
|
winit::dpi::LogicalPosition {
|
||||||
|
x: position.x,
|
||||||
|
y: position.y,
|
||||||
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
window::Action::ChangeMode(mode) => {
|
window::Action::ChangeMode(mode) => {
|
||||||
|
|
@ -1145,25 +1148,23 @@ pub fn user_force_quit(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn logical_bounds_of(
|
fn logical_bounds_of(window: &winit::window::Window) -> (Option<Point>, Size) {
|
||||||
window: &winit::window::Window,
|
let position = window
|
||||||
) -> Option<((i32, i32), Size<u32>)> {
|
|
||||||
let scale = window.scale_factor();
|
|
||||||
let pos = window
|
|
||||||
.inner_position()
|
.inner_position()
|
||||||
.map(|pos| {
|
.ok()
|
||||||
((pos.x as f64 / scale) as i32, (pos.y as f64 / scale) as i32)
|
.map(|position| position.to_logical(window.scale_factor()))
|
||||||
})
|
.map(|position| Point {
|
||||||
.ok()?;
|
x: position.x,
|
||||||
|
y: position.y,
|
||||||
|
});
|
||||||
|
|
||||||
let size = {
|
let size = {
|
||||||
let size = window.inner_size();
|
let size = window.inner_size().to_logical(window.scale_factor());
|
||||||
Size::new(
|
|
||||||
(size.width as f64 / scale) as u32,
|
Size::new(size.width, size.height)
|
||||||
(size.height as f64 / scale) as u32,
|
|
||||||
)
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Some((pos, size))
|
(position, size)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue