Merge pull request #597 from hecrj/improvement/reuse-view-in-event-loop
Rebuild widget tree only after an application update
This commit is contained in:
commit
da1a3eed1e
|
@ -1,13 +1,17 @@
|
||||||
//! Create interactive, native cross-platform applications.
|
//! Create interactive, native cross-platform applications.
|
||||||
use crate::{mouse, Error, Executor, Runtime, Size};
|
use crate::{mouse, Error, Executor, Runtime};
|
||||||
use iced_graphics::window;
|
|
||||||
use iced_graphics::Viewport;
|
|
||||||
use iced_winit::application;
|
|
||||||
use iced_winit::conversion;
|
|
||||||
use iced_winit::{Clipboard, Debug, Proxy, Settings};
|
|
||||||
|
|
||||||
pub use iced_winit::Application;
|
pub use iced_winit::Application;
|
||||||
pub use iced_winit::{program, Program};
|
|
||||||
|
use iced_graphics::window;
|
||||||
|
use iced_winit::application;
|
||||||
|
use iced_winit::conversion;
|
||||||
|
use iced_winit::futures;
|
||||||
|
use iced_winit::futures::channel::mpsc;
|
||||||
|
use iced_winit::{Cache, Clipboard, Debug, Proxy, Settings};
|
||||||
|
|
||||||
|
use glutin::window::Window;
|
||||||
|
use std::mem::ManuallyDrop;
|
||||||
|
|
||||||
/// Runs an [`Application`] with an executor, compositor, and the provided
|
/// Runs an [`Application`] with an executor, compositor, and the provided
|
||||||
/// settings.
|
/// settings.
|
||||||
|
@ -22,11 +26,10 @@ where
|
||||||
E: Executor + 'static,
|
E: Executor + 'static,
|
||||||
C: window::GLCompositor<Renderer = A::Renderer> + 'static,
|
C: window::GLCompositor<Renderer = A::Renderer> + 'static,
|
||||||
{
|
{
|
||||||
use glutin::{
|
use futures::task;
|
||||||
event,
|
use futures::Future;
|
||||||
event_loop::{ControlFlow, EventLoop},
|
use glutin::event_loop::EventLoop;
|
||||||
ContextBuilder,
|
use glutin::ContextBuilder;
|
||||||
};
|
|
||||||
|
|
||||||
let mut debug = Debug::new();
|
let mut debug = Debug::new();
|
||||||
debug.startup_started();
|
debug.startup_started();
|
||||||
|
@ -39,22 +42,21 @@ where
|
||||||
Runtime::new(executor, proxy)
|
Runtime::new(executor, proxy)
|
||||||
};
|
};
|
||||||
|
|
||||||
let flags = settings.flags;
|
let (application, init_command) = {
|
||||||
let (application, init_command) = runtime.enter(|| A::new(flags));
|
let flags = settings.flags;
|
||||||
runtime.spawn(init_command);
|
|
||||||
|
runtime.enter(|| A::new(flags))
|
||||||
|
};
|
||||||
|
|
||||||
let subscription = application.subscription();
|
let subscription = application.subscription();
|
||||||
runtime.track(subscription);
|
|
||||||
|
|
||||||
let mut title = application.title();
|
runtime.spawn(init_command);
|
||||||
let mut mode = application.mode();
|
runtime.track(subscription);
|
||||||
let mut background_color = application.background_color();
|
|
||||||
let mut scale_factor = application.scale_factor();
|
|
||||||
|
|
||||||
let context = {
|
let context = {
|
||||||
let builder = settings.window.into_builder(
|
let builder = settings.window.into_builder(
|
||||||
&title,
|
&application.title(),
|
||||||
mode,
|
application.mode(),
|
||||||
event_loop.primary_monitor(),
|
event_loop.primary_monitor(),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -79,189 +81,216 @@ where
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let clipboard = Clipboard::new(&context.window());
|
|
||||||
let mut cursor_position = glutin::dpi::PhysicalPosition::new(-1.0, -1.0);
|
|
||||||
let mut mouse_interaction = mouse::Interaction::default();
|
|
||||||
let mut modifiers = glutin::event::ModifiersState::default();
|
|
||||||
|
|
||||||
let physical_size = context.window().inner_size();
|
|
||||||
let mut viewport = Viewport::with_physical_size(
|
|
||||||
Size::new(physical_size.width, physical_size.height),
|
|
||||||
context.window().scale_factor() * scale_factor,
|
|
||||||
);
|
|
||||||
let mut resized = false;
|
|
||||||
|
|
||||||
#[allow(unsafe_code)]
|
#[allow(unsafe_code)]
|
||||||
let (mut compositor, mut renderer) = unsafe {
|
let (compositor, renderer) = unsafe {
|
||||||
C::new(compositor_settings, |address| {
|
C::new(compositor_settings, |address| {
|
||||||
context.get_proc_address(address)
|
context.get_proc_address(address)
|
||||||
})?
|
})?
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut state = program::State::new(
|
let (mut sender, receiver) = mpsc::unbounded();
|
||||||
|
|
||||||
|
let mut instance = Box::pin(run_instance::<A, E, C>(
|
||||||
application,
|
application,
|
||||||
viewport.logical_size(),
|
compositor,
|
||||||
conversion::cursor_position(cursor_position, viewport.scale_factor()),
|
renderer,
|
||||||
&mut renderer,
|
context,
|
||||||
&mut debug,
|
runtime,
|
||||||
);
|
debug,
|
||||||
|
receiver,
|
||||||
|
));
|
||||||
|
|
||||||
|
let mut context = task::Context::from_waker(task::noop_waker_ref());
|
||||||
|
|
||||||
|
event_loop.run(move |event, _, control_flow| {
|
||||||
|
use glutin::event_loop::ControlFlow;
|
||||||
|
|
||||||
|
if let ControlFlow::Exit = control_flow {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(event) = event.to_static() {
|
||||||
|
sender.start_send(event).expect("Send event");
|
||||||
|
|
||||||
|
let poll = instance.as_mut().poll(&mut context);
|
||||||
|
|
||||||
|
*control_flow = match poll {
|
||||||
|
task::Poll::Pending => ControlFlow::Wait,
|
||||||
|
task::Poll::Ready(_) => ControlFlow::Exit,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn run_instance<A, E, C>(
|
||||||
|
mut application: A,
|
||||||
|
mut compositor: C,
|
||||||
|
mut renderer: A::Renderer,
|
||||||
|
context: glutin::ContextWrapper<glutin::PossiblyCurrent, Window>,
|
||||||
|
mut runtime: Runtime<E, Proxy<A::Message>, A::Message>,
|
||||||
|
mut debug: Debug,
|
||||||
|
mut receiver: mpsc::UnboundedReceiver<glutin::event::Event<'_, A::Message>>,
|
||||||
|
) where
|
||||||
|
A: Application + 'static,
|
||||||
|
E: Executor + 'static,
|
||||||
|
C: window::GLCompositor<Renderer = A::Renderer> + 'static,
|
||||||
|
{
|
||||||
|
use glutin::event;
|
||||||
|
use iced_winit::futures::stream::StreamExt;
|
||||||
|
|
||||||
|
let clipboard = Clipboard::new(context.window());
|
||||||
|
|
||||||
|
let mut state = application::State::new(&application, context.window());
|
||||||
|
let mut viewport_version = state.viewport_version();
|
||||||
|
let mut user_interface =
|
||||||
|
ManuallyDrop::new(application::build_user_interface(
|
||||||
|
&mut application,
|
||||||
|
Cache::default(),
|
||||||
|
&mut renderer,
|
||||||
|
state.logical_size(),
|
||||||
|
&mut debug,
|
||||||
|
));
|
||||||
|
|
||||||
|
let mut primitive =
|
||||||
|
user_interface.draw(&mut renderer, state.cursor_position());
|
||||||
|
let mut mouse_interaction = mouse::Interaction::default();
|
||||||
|
|
||||||
|
let mut events = Vec::new();
|
||||||
|
let mut external_messages = Vec::new();
|
||||||
|
|
||||||
debug.startup_finished();
|
debug.startup_finished();
|
||||||
|
|
||||||
event_loop.run(move |event, _, control_flow| match event {
|
while let Some(event) = receiver.next().await {
|
||||||
event::Event::MainEventsCleared => {
|
match event {
|
||||||
if state.is_queue_empty() {
|
event::Event::MainEventsCleared => {
|
||||||
return;
|
if events.is_empty() && external_messages.is_empty() {
|
||||||
}
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
let command = runtime.enter(|| {
|
debug.event_processing_started();
|
||||||
state.update(
|
let mut messages = user_interface.update(
|
||||||
viewport.logical_size(),
|
&events,
|
||||||
conversion::cursor_position(
|
state.cursor_position(),
|
||||||
cursor_position,
|
|
||||||
viewport.scale_factor(),
|
|
||||||
),
|
|
||||||
clipboard.as_ref().map(|c| c as _),
|
clipboard.as_ref().map(|c| c as _),
|
||||||
&mut renderer,
|
&mut renderer,
|
||||||
&mut debug,
|
|
||||||
)
|
|
||||||
});
|
|
||||||
|
|
||||||
// If the application was updated
|
|
||||||
if let Some(command) = command {
|
|
||||||
runtime.spawn(command);
|
|
||||||
|
|
||||||
let program = state.program();
|
|
||||||
|
|
||||||
// Update subscriptions
|
|
||||||
let subscription = program.subscription();
|
|
||||||
runtime.track(subscription);
|
|
||||||
|
|
||||||
// Update window title
|
|
||||||
let new_title = program.title();
|
|
||||||
|
|
||||||
if title != new_title {
|
|
||||||
context.window().set_title(&new_title);
|
|
||||||
|
|
||||||
title = new_title;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update window mode
|
|
||||||
let new_mode = program.mode();
|
|
||||||
|
|
||||||
if mode != new_mode {
|
|
||||||
context.window().set_fullscreen(conversion::fullscreen(
|
|
||||||
context.window().current_monitor(),
|
|
||||||
new_mode,
|
|
||||||
));
|
|
||||||
|
|
||||||
mode = new_mode;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update background color
|
|
||||||
background_color = program.background_color();
|
|
||||||
|
|
||||||
// Update scale factor
|
|
||||||
let new_scale_factor = program.scale_factor();
|
|
||||||
|
|
||||||
if scale_factor != new_scale_factor {
|
|
||||||
let size = context.window().inner_size();
|
|
||||||
|
|
||||||
viewport = Viewport::with_physical_size(
|
|
||||||
Size::new(size.width, size.height),
|
|
||||||
context.window().scale_factor() * new_scale_factor,
|
|
||||||
);
|
|
||||||
|
|
||||||
// We relayout the UI with the new logical size.
|
|
||||||
// The queue is empty, therefore this will never produce
|
|
||||||
// a `Command`.
|
|
||||||
//
|
|
||||||
// TODO: Properly queue `WindowResized`
|
|
||||||
let _ = state.update(
|
|
||||||
viewport.logical_size(),
|
|
||||||
conversion::cursor_position(
|
|
||||||
cursor_position,
|
|
||||||
viewport.scale_factor(),
|
|
||||||
),
|
|
||||||
clipboard.as_ref().map(|c| c as _),
|
|
||||||
&mut renderer,
|
|
||||||
&mut debug,
|
|
||||||
);
|
|
||||||
|
|
||||||
scale_factor = new_scale_factor;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
context.window().request_redraw();
|
|
||||||
}
|
|
||||||
event::Event::UserEvent(message) => {
|
|
||||||
state.queue_message(message);
|
|
||||||
}
|
|
||||||
event::Event::RedrawRequested(_) => {
|
|
||||||
debug.render_started();
|
|
||||||
|
|
||||||
if resized {
|
|
||||||
let physical_size = viewport.physical_size();
|
|
||||||
|
|
||||||
context.resize(glutin::dpi::PhysicalSize::new(
|
|
||||||
physical_size.width,
|
|
||||||
physical_size.height,
|
|
||||||
));
|
|
||||||
|
|
||||||
compositor.resize_viewport(physical_size);
|
|
||||||
|
|
||||||
resized = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
let new_mouse_interaction = compositor.draw(
|
|
||||||
&mut renderer,
|
|
||||||
&viewport,
|
|
||||||
background_color,
|
|
||||||
state.primitive(),
|
|
||||||
&debug.overlay(),
|
|
||||||
);
|
|
||||||
|
|
||||||
context.swap_buffers().expect("Swap buffers");
|
|
||||||
|
|
||||||
debug.render_finished();
|
|
||||||
|
|
||||||
if new_mouse_interaction != mouse_interaction {
|
|
||||||
context.window().set_cursor_icon(
|
|
||||||
conversion::mouse_interaction(new_mouse_interaction),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
mouse_interaction = new_mouse_interaction;
|
messages.extend(external_messages.drain(..));
|
||||||
}
|
events.clear();
|
||||||
|
debug.event_processing_finished();
|
||||||
|
|
||||||
// TODO: Handle animations!
|
if !messages.is_empty() {
|
||||||
// Maybe we can use `ControlFlow::WaitUntil` for this.
|
let cache =
|
||||||
}
|
ManuallyDrop::into_inner(user_interface).into_cache();
|
||||||
event::Event::WindowEvent {
|
|
||||||
event: window_event,
|
|
||||||
..
|
|
||||||
} => {
|
|
||||||
application::handle_window_event(
|
|
||||||
&window_event,
|
|
||||||
context.window(),
|
|
||||||
scale_factor,
|
|
||||||
control_flow,
|
|
||||||
&mut cursor_position,
|
|
||||||
&mut modifiers,
|
|
||||||
&mut viewport,
|
|
||||||
&mut resized,
|
|
||||||
&mut debug,
|
|
||||||
);
|
|
||||||
|
|
||||||
if let Some(event) = conversion::window_event(
|
// Update application
|
||||||
&window_event,
|
application::update(
|
||||||
viewport.scale_factor(),
|
&mut application,
|
||||||
modifiers,
|
&mut runtime,
|
||||||
) {
|
&mut debug,
|
||||||
state.queue_event(event.clone());
|
messages,
|
||||||
runtime.broadcast(event);
|
);
|
||||||
|
|
||||||
|
// Update window
|
||||||
|
state.synchronize(&application, context.window());
|
||||||
|
|
||||||
|
user_interface =
|
||||||
|
ManuallyDrop::new(application::build_user_interface(
|
||||||
|
&mut application,
|
||||||
|
cache,
|
||||||
|
&mut renderer,
|
||||||
|
state.logical_size(),
|
||||||
|
&mut debug,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
debug.draw_started();
|
||||||
|
primitive =
|
||||||
|
user_interface.draw(&mut renderer, state.cursor_position());
|
||||||
|
debug.draw_finished();
|
||||||
|
|
||||||
|
context.window().request_redraw();
|
||||||
}
|
}
|
||||||
|
event::Event::UserEvent(message) => {
|
||||||
|
external_messages.push(message);
|
||||||
|
}
|
||||||
|
event::Event::RedrawRequested(_) => {
|
||||||
|
debug.render_started();
|
||||||
|
let current_viewport_version = state.viewport_version();
|
||||||
|
|
||||||
|
if viewport_version != current_viewport_version {
|
||||||
|
let physical_size = state.physical_size();
|
||||||
|
let logical_size = state.logical_size();
|
||||||
|
|
||||||
|
debug.layout_started();
|
||||||
|
user_interface = ManuallyDrop::new(
|
||||||
|
ManuallyDrop::into_inner(user_interface)
|
||||||
|
.relayout(logical_size, &mut renderer),
|
||||||
|
);
|
||||||
|
debug.layout_finished();
|
||||||
|
|
||||||
|
debug.draw_started();
|
||||||
|
primitive = user_interface
|
||||||
|
.draw(&mut renderer, state.cursor_position());
|
||||||
|
debug.draw_finished();
|
||||||
|
|
||||||
|
context.resize(glutin::dpi::PhysicalSize::new(
|
||||||
|
physical_size.width,
|
||||||
|
physical_size.height,
|
||||||
|
));
|
||||||
|
|
||||||
|
compositor.resize_viewport(physical_size);
|
||||||
|
|
||||||
|
viewport_version = current_viewport_version;
|
||||||
|
}
|
||||||
|
|
||||||
|
let new_mouse_interaction = compositor.draw(
|
||||||
|
&mut renderer,
|
||||||
|
state.viewport(),
|
||||||
|
state.background_color(),
|
||||||
|
&primitive,
|
||||||
|
&debug.overlay(),
|
||||||
|
);
|
||||||
|
|
||||||
|
context.swap_buffers().expect("Swap buffers");
|
||||||
|
|
||||||
|
debug.render_finished();
|
||||||
|
|
||||||
|
if new_mouse_interaction != mouse_interaction {
|
||||||
|
context.window().set_cursor_icon(
|
||||||
|
conversion::mouse_interaction(new_mouse_interaction),
|
||||||
|
);
|
||||||
|
|
||||||
|
mouse_interaction = new_mouse_interaction;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Handle animations!
|
||||||
|
// Maybe we can use `ControlFlow::WaitUntil` for this.
|
||||||
|
}
|
||||||
|
event::Event::WindowEvent {
|
||||||
|
event: window_event,
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
if application::requests_exit(&window_event, state.modifiers())
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
state.update(context.window(), &window_event, &mut debug);
|
||||||
|
|
||||||
|
if let Some(event) = conversion::window_event(
|
||||||
|
&window_event,
|
||||||
|
state.scale_factor(),
|
||||||
|
state.modifiers(),
|
||||||
|
) {
|
||||||
|
events.push(event.clone());
|
||||||
|
runtime.broadcast(event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
}
|
}
|
||||||
_ => {
|
}
|
||||||
*control_flow = ControlFlow::Wait;
|
|
||||||
}
|
// Manually drop the user interface
|
||||||
})
|
drop(ManuallyDrop::into_inner(user_interface));
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use crate::{Size, Transformation};
|
use crate::{Size, Transformation};
|
||||||
|
|
||||||
/// A viewing region for displaying computer graphics.
|
/// A viewing region for displaying computer graphics.
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Viewport {
|
pub struct Viewport {
|
||||||
physical_size: Size<u32>,
|
physical_size: Size<u32>,
|
||||||
logical_size: Size<f32>,
|
logical_size: Size<f32>,
|
||||||
|
|
|
@ -388,6 +388,23 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Relayouts and returns a new [`UserInterface`] using the provided
|
||||||
|
/// bounds.
|
||||||
|
///
|
||||||
|
/// [`UserInterface`]: struct.UserInterface.html
|
||||||
|
pub fn relayout(self, bounds: Size, renderer: &mut Renderer) -> Self {
|
||||||
|
Self::build(
|
||||||
|
self.root,
|
||||||
|
bounds,
|
||||||
|
Cache {
|
||||||
|
base: self.base,
|
||||||
|
overlay: self.overlay,
|
||||||
|
bounds: self.bounds,
|
||||||
|
},
|
||||||
|
renderer,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
/// Extract the [`Cache`] of the [`UserInterface`], consuming it in the
|
/// Extract the [`Cache`] of the [`UserInterface`], consuming it in the
|
||||||
/// process.
|
/// process.
|
||||||
///
|
///
|
||||||
|
|
|
@ -1,13 +1,22 @@
|
||||||
//! Create interactive, native cross-platform applications.
|
//! Create interactive, native cross-platform applications.
|
||||||
|
mod state;
|
||||||
|
|
||||||
|
pub use state::State;
|
||||||
|
|
||||||
use crate::conversion;
|
use crate::conversion;
|
||||||
use crate::mouse;
|
use crate::mouse;
|
||||||
use crate::{
|
use crate::{
|
||||||
Clipboard, Color, Command, Debug, Error, Executor, Mode, Proxy, Runtime,
|
Clipboard, Color, Command, Debug, Error, Executor, Mode, Proxy, Runtime,
|
||||||
Settings, Size, Subscription,
|
Settings, Size, Subscription,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use iced_futures::futures;
|
||||||
|
use iced_futures::futures::channel::mpsc;
|
||||||
use iced_graphics::window;
|
use iced_graphics::window;
|
||||||
use iced_graphics::Viewport;
|
use iced_native::program::Program;
|
||||||
use iced_native::program::{self, Program};
|
use iced_native::{Cache, UserInterface};
|
||||||
|
|
||||||
|
use std::mem::ManuallyDrop;
|
||||||
|
|
||||||
/// An interactive, native cross-platform application.
|
/// An interactive, native cross-platform application.
|
||||||
///
|
///
|
||||||
|
@ -116,279 +125,270 @@ where
|
||||||
E: Executor + 'static,
|
E: Executor + 'static,
|
||||||
C: window::Compositor<Renderer = A::Renderer> + 'static,
|
C: window::Compositor<Renderer = A::Renderer> + 'static,
|
||||||
{
|
{
|
||||||
use winit::{
|
use futures::task;
|
||||||
event,
|
use futures::Future;
|
||||||
event_loop::{ControlFlow, EventLoop},
|
use winit::event_loop::EventLoop;
|
||||||
};
|
|
||||||
|
|
||||||
let mut debug = Debug::new();
|
let mut debug = Debug::new();
|
||||||
debug.startup_started();
|
debug.startup_started();
|
||||||
|
|
||||||
|
let (compositor, renderer) = C::new(compositor_settings)?;
|
||||||
|
|
||||||
let event_loop = EventLoop::with_user_event();
|
let event_loop = EventLoop::with_user_event();
|
||||||
|
|
||||||
let mut runtime = {
|
let mut runtime = {
|
||||||
let executor = E::new().map_err(Error::ExecutorCreationFailed)?;
|
|
||||||
let proxy = Proxy::new(event_loop.create_proxy());
|
let proxy = Proxy::new(event_loop.create_proxy());
|
||||||
|
let executor = E::new().map_err(Error::ExecutorCreationFailed)?;
|
||||||
|
|
||||||
Runtime::new(executor, proxy)
|
Runtime::new(executor, proxy)
|
||||||
};
|
};
|
||||||
|
|
||||||
let flags = settings.flags;
|
let (application, init_command) = {
|
||||||
let (application, init_command) = runtime.enter(|| A::new(flags));
|
let flags = settings.flags;
|
||||||
runtime.spawn(init_command);
|
|
||||||
|
runtime.enter(|| A::new(flags))
|
||||||
|
};
|
||||||
|
|
||||||
let subscription = application.subscription();
|
let subscription = application.subscription();
|
||||||
runtime.track(subscription);
|
|
||||||
|
|
||||||
let mut title = application.title();
|
runtime.spawn(init_command);
|
||||||
let mut mode = application.mode();
|
runtime.track(subscription);
|
||||||
let mut background_color = application.background_color();
|
|
||||||
let mut scale_factor = application.scale_factor();
|
|
||||||
|
|
||||||
let window = settings
|
let window = settings
|
||||||
.window
|
.window
|
||||||
.into_builder(&title, mode, event_loop.primary_monitor())
|
.into_builder(
|
||||||
|
&application.title(),
|
||||||
|
application.mode(),
|
||||||
|
event_loop.primary_monitor(),
|
||||||
|
)
|
||||||
.build(&event_loop)
|
.build(&event_loop)
|
||||||
.map_err(Error::WindowCreationFailed)?;
|
.map_err(Error::WindowCreationFailed)?;
|
||||||
|
|
||||||
let clipboard = Clipboard::new(&window);
|
let (mut sender, receiver) = mpsc::unbounded();
|
||||||
// TODO: Encode cursor availability in the type-system
|
|
||||||
let mut cursor_position = winit::dpi::PhysicalPosition::new(-1.0, -1.0);
|
|
||||||
let mut mouse_interaction = mouse::Interaction::default();
|
|
||||||
let mut modifiers = winit::event::ModifiersState::default();
|
|
||||||
|
|
||||||
let physical_size = window.inner_size();
|
let mut instance = Box::pin(run_instance::<A, E, C>(
|
||||||
let mut viewport = Viewport::with_physical_size(
|
|
||||||
Size::new(physical_size.width, physical_size.height),
|
|
||||||
window.scale_factor() * scale_factor,
|
|
||||||
);
|
|
||||||
let mut resized = false;
|
|
||||||
|
|
||||||
let (mut compositor, mut renderer) = C::new(compositor_settings)?;
|
|
||||||
|
|
||||||
let surface = compositor.create_surface(&window);
|
|
||||||
|
|
||||||
let mut swap_chain = compositor.create_swap_chain(
|
|
||||||
&surface,
|
|
||||||
physical_size.width,
|
|
||||||
physical_size.height,
|
|
||||||
);
|
|
||||||
|
|
||||||
let mut state = program::State::new(
|
|
||||||
application,
|
application,
|
||||||
viewport.logical_size(),
|
compositor,
|
||||||
conversion::cursor_position(cursor_position, viewport.scale_factor()),
|
renderer,
|
||||||
&mut renderer,
|
window,
|
||||||
&mut debug,
|
runtime,
|
||||||
);
|
debug,
|
||||||
debug.startup_finished();
|
receiver,
|
||||||
|
));
|
||||||
|
|
||||||
event_loop.run(move |event, _, control_flow| match event {
|
let mut context = task::Context::from_waker(task::noop_waker_ref());
|
||||||
event::Event::MainEventsCleared => {
|
|
||||||
if state.is_queue_empty() {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let command = runtime.enter(|| {
|
event_loop.run(move |event, _, control_flow| {
|
||||||
state.update(
|
use winit::event_loop::ControlFlow;
|
||||||
viewport.logical_size(),
|
|
||||||
conversion::cursor_position(
|
|
||||||
cursor_position,
|
|
||||||
viewport.scale_factor(),
|
|
||||||
),
|
|
||||||
clipboard.as_ref().map(|c| c as _),
|
|
||||||
&mut renderer,
|
|
||||||
&mut debug,
|
|
||||||
)
|
|
||||||
});
|
|
||||||
|
|
||||||
// If the application was updated
|
if let ControlFlow::Exit = control_flow {
|
||||||
if let Some(command) = command {
|
return;
|
||||||
runtime.spawn(command);
|
|
||||||
|
|
||||||
let program = state.program();
|
|
||||||
|
|
||||||
// Update subscriptions
|
|
||||||
let subscription = program.subscription();
|
|
||||||
runtime.track(subscription);
|
|
||||||
|
|
||||||
// Update window title
|
|
||||||
let new_title = program.title();
|
|
||||||
|
|
||||||
if title != new_title {
|
|
||||||
window.set_title(&new_title);
|
|
||||||
|
|
||||||
title = new_title;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update window mode
|
|
||||||
let new_mode = program.mode();
|
|
||||||
|
|
||||||
if mode != new_mode {
|
|
||||||
window.set_fullscreen(conversion::fullscreen(
|
|
||||||
window.current_monitor(),
|
|
||||||
new_mode,
|
|
||||||
));
|
|
||||||
|
|
||||||
mode = new_mode;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update background color
|
|
||||||
background_color = program.background_color();
|
|
||||||
|
|
||||||
// Update scale factor
|
|
||||||
let new_scale_factor = program.scale_factor();
|
|
||||||
|
|
||||||
if scale_factor != new_scale_factor {
|
|
||||||
let size = window.inner_size();
|
|
||||||
|
|
||||||
viewport = Viewport::with_physical_size(
|
|
||||||
Size::new(size.width, size.height),
|
|
||||||
window.scale_factor() * new_scale_factor,
|
|
||||||
);
|
|
||||||
|
|
||||||
// We relayout the UI with the new logical size.
|
|
||||||
// The queue is empty, therefore this will never produce
|
|
||||||
// a `Command`.
|
|
||||||
//
|
|
||||||
// TODO: Properly queue `WindowResized`
|
|
||||||
let _ = state.update(
|
|
||||||
viewport.logical_size(),
|
|
||||||
conversion::cursor_position(
|
|
||||||
cursor_position,
|
|
||||||
viewport.scale_factor(),
|
|
||||||
),
|
|
||||||
clipboard.as_ref().map(|c| c as _),
|
|
||||||
&mut renderer,
|
|
||||||
&mut debug,
|
|
||||||
);
|
|
||||||
|
|
||||||
scale_factor = new_scale_factor;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
window.request_redraw();
|
|
||||||
}
|
}
|
||||||
event::Event::UserEvent(message) => {
|
|
||||||
state.queue_message(message);
|
if let Some(event) = event.to_static() {
|
||||||
|
sender.start_send(event).expect("Send event");
|
||||||
|
|
||||||
|
let poll = instance.as_mut().poll(&mut context);
|
||||||
|
|
||||||
|
*control_flow = match poll {
|
||||||
|
task::Poll::Pending => ControlFlow::Wait,
|
||||||
|
task::Poll::Ready(_) => ControlFlow::Exit,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
event::Event::RedrawRequested(_) => {
|
});
|
||||||
debug.render_started();
|
|
||||||
|
|
||||||
if resized {
|
|
||||||
let physical_size = viewport.physical_size();
|
|
||||||
|
|
||||||
swap_chain = compositor.create_swap_chain(
|
|
||||||
&surface,
|
|
||||||
physical_size.width,
|
|
||||||
physical_size.height,
|
|
||||||
);
|
|
||||||
|
|
||||||
resized = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
let new_mouse_interaction = compositor.draw(
|
|
||||||
&mut renderer,
|
|
||||||
&mut swap_chain,
|
|
||||||
&viewport,
|
|
||||||
background_color,
|
|
||||||
state.primitive(),
|
|
||||||
&debug.overlay(),
|
|
||||||
);
|
|
||||||
|
|
||||||
debug.render_finished();
|
|
||||||
|
|
||||||
if new_mouse_interaction != mouse_interaction {
|
|
||||||
window.set_cursor_icon(conversion::mouse_interaction(
|
|
||||||
new_mouse_interaction,
|
|
||||||
));
|
|
||||||
|
|
||||||
mouse_interaction = new_mouse_interaction;
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Handle animations!
|
|
||||||
// Maybe we can use `ControlFlow::WaitUntil` for this.
|
|
||||||
}
|
|
||||||
event::Event::WindowEvent {
|
|
||||||
event: window_event,
|
|
||||||
..
|
|
||||||
} => {
|
|
||||||
handle_window_event(
|
|
||||||
&window_event,
|
|
||||||
&window,
|
|
||||||
scale_factor,
|
|
||||||
control_flow,
|
|
||||||
&mut cursor_position,
|
|
||||||
&mut modifiers,
|
|
||||||
&mut viewport,
|
|
||||||
&mut resized,
|
|
||||||
&mut debug,
|
|
||||||
);
|
|
||||||
|
|
||||||
if let Some(event) = conversion::window_event(
|
|
||||||
&window_event,
|
|
||||||
viewport.scale_factor(),
|
|
||||||
modifiers,
|
|
||||||
) {
|
|
||||||
state.queue_event(event.clone());
|
|
||||||
runtime.broadcast(event);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
*control_flow = ControlFlow::Wait;
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Handles a `WindowEvent` and mutates the provided control flow, keyboard
|
async fn run_instance<A, E, C>(
|
||||||
/// modifiers, viewport, and resized flag accordingly.
|
mut application: A,
|
||||||
pub fn handle_window_event(
|
mut compositor: C,
|
||||||
|
mut renderer: A::Renderer,
|
||||||
|
window: winit::window::Window,
|
||||||
|
mut runtime: Runtime<E, Proxy<A::Message>, A::Message>,
|
||||||
|
mut debug: Debug,
|
||||||
|
mut receiver: mpsc::UnboundedReceiver<winit::event::Event<'_, A::Message>>,
|
||||||
|
) where
|
||||||
|
A: Application + 'static,
|
||||||
|
E: Executor + 'static,
|
||||||
|
C: window::Compositor<Renderer = A::Renderer> + 'static,
|
||||||
|
{
|
||||||
|
use iced_futures::futures::stream::StreamExt;
|
||||||
|
use winit::event;
|
||||||
|
|
||||||
|
let surface = compositor.create_surface(&window);
|
||||||
|
let clipboard = Clipboard::new(&window);
|
||||||
|
|
||||||
|
let mut state = State::new(&application, &window);
|
||||||
|
let mut viewport_version = state.viewport_version();
|
||||||
|
let mut swap_chain = {
|
||||||
|
let physical_size = state.physical_size();
|
||||||
|
|
||||||
|
compositor.create_swap_chain(
|
||||||
|
&surface,
|
||||||
|
physical_size.width,
|
||||||
|
physical_size.height,
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut user_interface = ManuallyDrop::new(build_user_interface(
|
||||||
|
&mut application,
|
||||||
|
Cache::default(),
|
||||||
|
&mut renderer,
|
||||||
|
state.logical_size(),
|
||||||
|
&mut debug,
|
||||||
|
));
|
||||||
|
|
||||||
|
let mut primitive =
|
||||||
|
user_interface.draw(&mut renderer, state.cursor_position());
|
||||||
|
let mut mouse_interaction = mouse::Interaction::default();
|
||||||
|
|
||||||
|
let mut events = Vec::new();
|
||||||
|
let mut external_messages = Vec::new();
|
||||||
|
|
||||||
|
debug.startup_finished();
|
||||||
|
|
||||||
|
while let Some(event) = receiver.next().await {
|
||||||
|
match event {
|
||||||
|
event::Event::MainEventsCleared => {
|
||||||
|
if events.is_empty() && external_messages.is_empty() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
debug.event_processing_started();
|
||||||
|
let mut messages = user_interface.update(
|
||||||
|
&events,
|
||||||
|
state.cursor_position(),
|
||||||
|
clipboard.as_ref().map(|c| c as _),
|
||||||
|
&mut renderer,
|
||||||
|
);
|
||||||
|
|
||||||
|
messages.extend(external_messages.drain(..));
|
||||||
|
events.clear();
|
||||||
|
debug.event_processing_finished();
|
||||||
|
|
||||||
|
if !messages.is_empty() {
|
||||||
|
let cache =
|
||||||
|
ManuallyDrop::into_inner(user_interface).into_cache();
|
||||||
|
|
||||||
|
// Update application
|
||||||
|
update(
|
||||||
|
&mut application,
|
||||||
|
&mut runtime,
|
||||||
|
&mut debug,
|
||||||
|
messages,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Update window
|
||||||
|
state.synchronize(&application, &window);
|
||||||
|
|
||||||
|
user_interface = ManuallyDrop::new(build_user_interface(
|
||||||
|
&mut application,
|
||||||
|
cache,
|
||||||
|
&mut renderer,
|
||||||
|
state.logical_size(),
|
||||||
|
&mut debug,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
debug.draw_started();
|
||||||
|
primitive =
|
||||||
|
user_interface.draw(&mut renderer, state.cursor_position());
|
||||||
|
debug.draw_finished();
|
||||||
|
|
||||||
|
window.request_redraw();
|
||||||
|
}
|
||||||
|
event::Event::UserEvent(message) => {
|
||||||
|
external_messages.push(message);
|
||||||
|
}
|
||||||
|
event::Event::RedrawRequested(_) => {
|
||||||
|
debug.render_started();
|
||||||
|
let current_viewport_version = state.viewport_version();
|
||||||
|
|
||||||
|
if viewport_version != current_viewport_version {
|
||||||
|
let physical_size = state.physical_size();
|
||||||
|
let logical_size = state.logical_size();
|
||||||
|
|
||||||
|
debug.layout_started();
|
||||||
|
user_interface = ManuallyDrop::new(
|
||||||
|
ManuallyDrop::into_inner(user_interface)
|
||||||
|
.relayout(logical_size, &mut renderer),
|
||||||
|
);
|
||||||
|
debug.layout_finished();
|
||||||
|
|
||||||
|
debug.draw_started();
|
||||||
|
primitive = user_interface
|
||||||
|
.draw(&mut renderer, state.cursor_position());
|
||||||
|
debug.draw_finished();
|
||||||
|
|
||||||
|
swap_chain = compositor.create_swap_chain(
|
||||||
|
&surface,
|
||||||
|
physical_size.width,
|
||||||
|
physical_size.height,
|
||||||
|
);
|
||||||
|
|
||||||
|
viewport_version = current_viewport_version;
|
||||||
|
}
|
||||||
|
|
||||||
|
let new_mouse_interaction = compositor.draw(
|
||||||
|
&mut renderer,
|
||||||
|
&mut swap_chain,
|
||||||
|
state.viewport(),
|
||||||
|
state.background_color(),
|
||||||
|
&primitive,
|
||||||
|
&debug.overlay(),
|
||||||
|
);
|
||||||
|
|
||||||
|
debug.render_finished();
|
||||||
|
|
||||||
|
if new_mouse_interaction != mouse_interaction {
|
||||||
|
window.set_cursor_icon(conversion::mouse_interaction(
|
||||||
|
new_mouse_interaction,
|
||||||
|
));
|
||||||
|
|
||||||
|
mouse_interaction = new_mouse_interaction;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Handle animations!
|
||||||
|
// Maybe we can use `ControlFlow::WaitUntil` for this.
|
||||||
|
}
|
||||||
|
event::Event::WindowEvent {
|
||||||
|
event: window_event,
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
if requests_exit(&window_event, state.modifiers()) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
state.update(&window, &window_event, &mut debug);
|
||||||
|
|
||||||
|
if let Some(event) = conversion::window_event(
|
||||||
|
&window_event,
|
||||||
|
state.scale_factor(),
|
||||||
|
state.modifiers(),
|
||||||
|
) {
|
||||||
|
events.push(event.clone());
|
||||||
|
runtime.broadcast(event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Manually drop the user interface
|
||||||
|
drop(ManuallyDrop::into_inner(user_interface));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns true if the provided event should cause an [`Application`] to
|
||||||
|
/// exit.
|
||||||
|
///
|
||||||
|
/// [`Application`]: trait.Application.html
|
||||||
|
pub fn requests_exit(
|
||||||
event: &winit::event::WindowEvent<'_>,
|
event: &winit::event::WindowEvent<'_>,
|
||||||
window: &winit::window::Window,
|
_modifiers: winit::event::ModifiersState,
|
||||||
scale_factor: f64,
|
) -> bool {
|
||||||
control_flow: &mut winit::event_loop::ControlFlow,
|
use winit::event::WindowEvent;
|
||||||
cursor_position: &mut winit::dpi::PhysicalPosition<f64>,
|
|
||||||
modifiers: &mut winit::event::ModifiersState,
|
|
||||||
viewport: &mut Viewport,
|
|
||||||
resized: &mut bool,
|
|
||||||
_debug: &mut Debug,
|
|
||||||
) {
|
|
||||||
use winit::{event::WindowEvent, event_loop::ControlFlow};
|
|
||||||
|
|
||||||
match event {
|
match event {
|
||||||
WindowEvent::Resized(new_size) => {
|
WindowEvent::CloseRequested => true,
|
||||||
let size = Size::new(new_size.width, new_size.height);
|
|
||||||
|
|
||||||
*viewport = Viewport::with_physical_size(
|
|
||||||
size,
|
|
||||||
window.scale_factor() * scale_factor,
|
|
||||||
);
|
|
||||||
*resized = true;
|
|
||||||
}
|
|
||||||
WindowEvent::ScaleFactorChanged {
|
|
||||||
scale_factor: new_scale_factor,
|
|
||||||
new_inner_size,
|
|
||||||
} => {
|
|
||||||
let size = Size::new(new_inner_size.width, new_inner_size.height);
|
|
||||||
|
|
||||||
*viewport = Viewport::with_physical_size(
|
|
||||||
size,
|
|
||||||
new_scale_factor * scale_factor,
|
|
||||||
);
|
|
||||||
*resized = true;
|
|
||||||
}
|
|
||||||
WindowEvent::CloseRequested => {
|
|
||||||
*control_flow = ControlFlow::Exit;
|
|
||||||
}
|
|
||||||
WindowEvent::CursorMoved { position, .. } => {
|
|
||||||
*cursor_position = *position;
|
|
||||||
}
|
|
||||||
WindowEvent::CursorLeft { .. } => {
|
|
||||||
// TODO: Encode cursor availability in the type-system
|
|
||||||
*cursor_position = winit::dpi::PhysicalPosition::new(-1.0, -1.0);
|
|
||||||
}
|
|
||||||
WindowEvent::ModifiersChanged(new_modifiers) => {
|
|
||||||
*modifiers = *new_modifiers;
|
|
||||||
}
|
|
||||||
#[cfg(target_os = "macos")]
|
#[cfg(target_os = "macos")]
|
||||||
WindowEvent::KeyboardInput {
|
WindowEvent::KeyboardInput {
|
||||||
input:
|
input:
|
||||||
|
@ -398,19 +398,57 @@ pub fn handle_window_event(
|
||||||
..
|
..
|
||||||
},
|
},
|
||||||
..
|
..
|
||||||
} if modifiers.logo() => {
|
} if _modifiers.logo() => true,
|
||||||
*control_flow = ControlFlow::Exit;
|
_ => false,
|
||||||
}
|
|
||||||
#[cfg(feature = "debug")]
|
|
||||||
WindowEvent::KeyboardInput {
|
|
||||||
input:
|
|
||||||
winit::event::KeyboardInput {
|
|
||||||
virtual_keycode: Some(winit::event::VirtualKeyCode::F12),
|
|
||||||
state: winit::event::ElementState::Pressed,
|
|
||||||
..
|
|
||||||
},
|
|
||||||
..
|
|
||||||
} => _debug.toggle(),
|
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Builds a [`UserInterface`] for the provided [`Application`], logging
|
||||||
|
/// [`Debug`] information accordingly.
|
||||||
|
///
|
||||||
|
/// [`UserInterface`]: struct.UserInterface.html
|
||||||
|
/// [`Application`]: trait.Application.html
|
||||||
|
/// [`Debug`]: struct.Debug.html
|
||||||
|
pub fn build_user_interface<'a, A: Application>(
|
||||||
|
application: &'a mut A,
|
||||||
|
cache: Cache,
|
||||||
|
renderer: &mut A::Renderer,
|
||||||
|
size: Size,
|
||||||
|
debug: &mut Debug,
|
||||||
|
) -> UserInterface<'a, A::Message, A::Renderer> {
|
||||||
|
debug.view_started();
|
||||||
|
let view = application.view();
|
||||||
|
debug.view_finished();
|
||||||
|
|
||||||
|
debug.layout_started();
|
||||||
|
let user_interface = UserInterface::build(view, size, cache, renderer);
|
||||||
|
debug.layout_finished();
|
||||||
|
|
||||||
|
user_interface
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Updates an [`Application`] by feeding it the provided messages, spawning any
|
||||||
|
/// resulting [`Command`], and tracking its [`Subscription`].
|
||||||
|
///
|
||||||
|
/// [`Application`]: trait.Application.html
|
||||||
|
/// [`Command`]: struct.Command.html
|
||||||
|
/// [`Subscription`]: struct.Subscription.html
|
||||||
|
pub fn update<A: Application, E: Executor>(
|
||||||
|
application: &mut A,
|
||||||
|
runtime: &mut Runtime<E, Proxy<A::Message>, A::Message>,
|
||||||
|
debug: &mut Debug,
|
||||||
|
messages: Vec<A::Message>,
|
||||||
|
) {
|
||||||
|
for message in messages {
|
||||||
|
debug.log_message(&message);
|
||||||
|
|
||||||
|
debug.update_started();
|
||||||
|
let command = runtime.enter(|| application.update(message));
|
||||||
|
debug.update_finished();
|
||||||
|
|
||||||
|
runtime.spawn(command);
|
||||||
|
}
|
||||||
|
|
||||||
|
let subscription = application.subscription();
|
||||||
|
runtime.track(subscription);
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,235 @@
|
||||||
|
use crate::conversion;
|
||||||
|
use crate::{Application, Color, Debug, Mode, Point, Size, Viewport};
|
||||||
|
|
||||||
|
use std::marker::PhantomData;
|
||||||
|
use winit::event::WindowEvent;
|
||||||
|
use winit::window::Window;
|
||||||
|
|
||||||
|
/// The state of a windowed [`Application`].
|
||||||
|
///
|
||||||
|
/// [`Application`]: ../trait.Application.html
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct State<A: Application> {
|
||||||
|
title: String,
|
||||||
|
mode: Mode,
|
||||||
|
background_color: Color,
|
||||||
|
scale_factor: f64,
|
||||||
|
viewport: Viewport,
|
||||||
|
viewport_version: usize,
|
||||||
|
cursor_position: winit::dpi::PhysicalPosition<f64>,
|
||||||
|
modifiers: winit::event::ModifiersState,
|
||||||
|
application: PhantomData<A>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<A: Application> State<A> {
|
||||||
|
/// Creates a new [`State`] for the provided [`Application`] and window.
|
||||||
|
///
|
||||||
|
/// [`State`]: struct.State.html
|
||||||
|
/// [`Application`]: ../trait.Application.html
|
||||||
|
pub fn new(application: &A, window: &Window) -> Self {
|
||||||
|
let title = application.title();
|
||||||
|
let mode = application.mode();
|
||||||
|
let background_color = application.background_color();
|
||||||
|
let scale_factor = application.scale_factor();
|
||||||
|
|
||||||
|
let viewport = {
|
||||||
|
let physical_size = window.inner_size();
|
||||||
|
|
||||||
|
Viewport::with_physical_size(
|
||||||
|
Size::new(physical_size.width, physical_size.height),
|
||||||
|
window.scale_factor() * scale_factor,
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
Self {
|
||||||
|
title,
|
||||||
|
mode,
|
||||||
|
background_color,
|
||||||
|
scale_factor,
|
||||||
|
viewport,
|
||||||
|
viewport_version: 0,
|
||||||
|
// TODO: Encode cursor availability in the type-system
|
||||||
|
cursor_position: winit::dpi::PhysicalPosition::new(-1.0, -1.0),
|
||||||
|
modifiers: winit::event::ModifiersState::default(),
|
||||||
|
application: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the current background [`Color`] of the [`State`].
|
||||||
|
///
|
||||||
|
/// [`Color`]: ../struct.Color.html
|
||||||
|
/// [`State`]: struct.State.html
|
||||||
|
pub fn background_color(&self) -> Color {
|
||||||
|
self.background_color
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the current [`Viewport`] of the [`State`].
|
||||||
|
///
|
||||||
|
/// [`Viewport`]: ../struct.Viewport.html
|
||||||
|
/// [`State`]: struct.State.html
|
||||||
|
pub fn viewport(&self) -> &Viewport {
|
||||||
|
&self.viewport
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the version of the [`Viewport`] of the [`State`].
|
||||||
|
///
|
||||||
|
/// The version is incremented every time the [`Viewport`] changes.
|
||||||
|
///
|
||||||
|
/// [`Viewport`]: ../struct.Viewport.html
|
||||||
|
/// [`State`]: struct.State.html
|
||||||
|
pub fn viewport_version(&self) -> usize {
|
||||||
|
self.viewport_version
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the physical [`Size`] of the [`Viewport`] of the [`State`].
|
||||||
|
///
|
||||||
|
/// [`Size`]: ../struct.Size.html
|
||||||
|
/// [`Viewport`]: ../struct.Viewport.html
|
||||||
|
/// [`State`]: struct.State.html
|
||||||
|
pub fn physical_size(&self) -> Size<u32> {
|
||||||
|
self.viewport.physical_size()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the logical [`Size`] of the [`Viewport`] of the [`State`].
|
||||||
|
///
|
||||||
|
/// [`Size`]: ../struct.Size.html
|
||||||
|
/// [`Viewport`]: ../struct.Viewport.html
|
||||||
|
/// [`State`]: struct.State.html
|
||||||
|
pub fn logical_size(&self) -> Size<f32> {
|
||||||
|
self.viewport.logical_size()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the current scale factor of the [`Viewport`] of the [`State`].
|
||||||
|
///
|
||||||
|
/// [`Viewport`]: ../struct.Viewport.html
|
||||||
|
/// [`State`]: struct.State.html
|
||||||
|
pub fn scale_factor(&self) -> f64 {
|
||||||
|
self.viewport.scale_factor()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the current cursor position of the [`State`].
|
||||||
|
///
|
||||||
|
/// [`State`]: struct.State.html
|
||||||
|
pub fn cursor_position(&self) -> Point {
|
||||||
|
conversion::cursor_position(
|
||||||
|
self.cursor_position,
|
||||||
|
self.viewport.scale_factor(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the current keyboard modifiers of the [`State`].
|
||||||
|
///
|
||||||
|
/// [`State`]: struct.State.html
|
||||||
|
pub fn modifiers(&self) -> winit::event::ModifiersState {
|
||||||
|
self.modifiers
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Processes the provided window event and updates the [`State`]
|
||||||
|
/// accordingly.
|
||||||
|
///
|
||||||
|
/// [`State`]: struct.State.html
|
||||||
|
pub fn update(
|
||||||
|
&mut self,
|
||||||
|
window: &Window,
|
||||||
|
event: &WindowEvent<'_>,
|
||||||
|
_debug: &mut Debug,
|
||||||
|
) {
|
||||||
|
match event {
|
||||||
|
WindowEvent::Resized(new_size) => {
|
||||||
|
let size = Size::new(new_size.width, new_size.height);
|
||||||
|
|
||||||
|
self.viewport = Viewport::with_physical_size(
|
||||||
|
size,
|
||||||
|
window.scale_factor() * self.scale_factor,
|
||||||
|
);
|
||||||
|
|
||||||
|
self.viewport_version = self.viewport_version.wrapping_add(1);
|
||||||
|
}
|
||||||
|
WindowEvent::ScaleFactorChanged {
|
||||||
|
scale_factor: new_scale_factor,
|
||||||
|
new_inner_size,
|
||||||
|
} => {
|
||||||
|
let size =
|
||||||
|
Size::new(new_inner_size.width, new_inner_size.height);
|
||||||
|
|
||||||
|
self.viewport = Viewport::with_physical_size(
|
||||||
|
size,
|
||||||
|
new_scale_factor * self.scale_factor,
|
||||||
|
);
|
||||||
|
|
||||||
|
self.viewport_version = self.viewport_version.wrapping_add(1);
|
||||||
|
}
|
||||||
|
WindowEvent::CursorMoved { position, .. } => {
|
||||||
|
self.cursor_position = *position;
|
||||||
|
}
|
||||||
|
WindowEvent::CursorLeft { .. } => {
|
||||||
|
// TODO: Encode cursor availability in the type-system
|
||||||
|
self.cursor_position =
|
||||||
|
winit::dpi::PhysicalPosition::new(-1.0, -1.0);
|
||||||
|
}
|
||||||
|
WindowEvent::ModifiersChanged(new_modifiers) => {
|
||||||
|
self.modifiers = *new_modifiers;
|
||||||
|
}
|
||||||
|
#[cfg(feature = "debug")]
|
||||||
|
WindowEvent::KeyboardInput {
|
||||||
|
input:
|
||||||
|
winit::event::KeyboardInput {
|
||||||
|
virtual_keycode: Some(winit::event::VirtualKeyCode::F12),
|
||||||
|
state: winit::event::ElementState::Pressed,
|
||||||
|
..
|
||||||
|
},
|
||||||
|
..
|
||||||
|
} => _debug.toggle(),
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Synchronizes the [`State`] with its [`Application`] and its respective
|
||||||
|
/// window.
|
||||||
|
///
|
||||||
|
/// Normally an [`Application`] should be synchronized with its [`State`]
|
||||||
|
/// and window after calling [`Application::update`].
|
||||||
|
///
|
||||||
|
/// [`State`]: struct.State.html
|
||||||
|
/// [`Application`]: ../trait.Application.html
|
||||||
|
/// [`Application::update`]: ../trait.Application.html#tymethod.update
|
||||||
|
pub fn synchronize(&mut self, application: &A, window: &Window) {
|
||||||
|
// Update window title
|
||||||
|
let new_title = application.title();
|
||||||
|
|
||||||
|
if self.title != new_title {
|
||||||
|
window.set_title(&new_title);
|
||||||
|
|
||||||
|
self.title = new_title;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update window mode
|
||||||
|
let new_mode = application.mode();
|
||||||
|
|
||||||
|
if self.mode != new_mode {
|
||||||
|
window.set_fullscreen(conversion::fullscreen(
|
||||||
|
window.current_monitor(),
|
||||||
|
new_mode,
|
||||||
|
));
|
||||||
|
|
||||||
|
self.mode = new_mode;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update background color
|
||||||
|
self.background_color = application.background_color();
|
||||||
|
|
||||||
|
// Update scale factor
|
||||||
|
let new_scale_factor = application.scale_factor();
|
||||||
|
|
||||||
|
if self.scale_factor != new_scale_factor {
|
||||||
|
let size = window.inner_size();
|
||||||
|
|
||||||
|
self.viewport = Viewport::with_physical_size(
|
||||||
|
Size::new(size.width, size.height),
|
||||||
|
window.scale_factor() * new_scale_factor,
|
||||||
|
);
|
||||||
|
|
||||||
|
self.scale_factor = new_scale_factor;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue