Remove a bit of code duplication in both shells
This commit is contained in:
parent
d5a15419e9
commit
e966cd5b59
2 changed files with 79 additions and 99 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
//! 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};
|
||||||
|
|
||||||
pub use iced_winit::application::{self, Application};
|
pub use iced_winit::application::{self, Application};
|
||||||
|
|
||||||
|
|
@ -7,7 +7,7 @@ use iced_graphics::window;
|
||||||
use iced_winit::conversion;
|
use iced_winit::conversion;
|
||||||
use iced_winit::futures;
|
use iced_winit::futures;
|
||||||
use iced_winit::futures::channel::mpsc;
|
use iced_winit::futures::channel::mpsc;
|
||||||
use iced_winit::{Cache, Clipboard, Debug, Proxy, Settings, UserInterface};
|
use iced_winit::{Cache, Clipboard, Debug, Proxy, Settings};
|
||||||
|
|
||||||
use glutin::window::Window;
|
use glutin::window::Window;
|
||||||
use std::mem::ManuallyDrop;
|
use std::mem::ManuallyDrop;
|
||||||
|
|
@ -137,18 +137,18 @@ async fn run_instance<A, E, C>(
|
||||||
use glutin::event;
|
use glutin::event;
|
||||||
use iced_winit::futures::stream::StreamExt;
|
use iced_winit::futures::stream::StreamExt;
|
||||||
|
|
||||||
let mut state = application::State::new(&application, context.window());
|
|
||||||
let mut viewport_version = state.viewport_version();
|
|
||||||
|
|
||||||
let clipboard = Clipboard::new(context.window());
|
let clipboard = Clipboard::new(context.window());
|
||||||
|
|
||||||
let mut user_interface = ManuallyDrop::new(build_user_interface(
|
let mut state = application::State::new(&application, context.window());
|
||||||
&mut application,
|
let mut viewport_version = state.viewport_version();
|
||||||
Cache::default(),
|
let mut user_interface =
|
||||||
&mut renderer,
|
ManuallyDrop::new(application::build_user_interface(
|
||||||
state.logical_size(),
|
&mut application,
|
||||||
&mut debug,
|
Cache::default(),
|
||||||
));
|
&mut renderer,
|
||||||
|
state.logical_size(),
|
||||||
|
&mut debug,
|
||||||
|
));
|
||||||
|
|
||||||
let mut primitive =
|
let mut primitive =
|
||||||
user_interface.draw(&mut renderer, state.cursor_position());
|
user_interface.draw(&mut renderer, state.cursor_position());
|
||||||
|
|
@ -178,47 +178,36 @@ async fn run_instance<A, E, C>(
|
||||||
events.clear();
|
events.clear();
|
||||||
debug.event_processing_finished();
|
debug.event_processing_finished();
|
||||||
|
|
||||||
if messages.is_empty() {
|
if !messages.is_empty() {
|
||||||
debug.draw_started();
|
|
||||||
primitive = user_interface
|
|
||||||
.draw(&mut renderer, state.cursor_position());
|
|
||||||
debug.draw_finished();
|
|
||||||
} else {
|
|
||||||
let cache =
|
let cache =
|
||||||
ManuallyDrop::into_inner(user_interface).into_cache();
|
ManuallyDrop::into_inner(user_interface).into_cache();
|
||||||
|
|
||||||
for message in messages.drain(..) {
|
// Update application
|
||||||
debug.log_message(&message);
|
application::update(
|
||||||
|
&mut application,
|
||||||
debug.update_started();
|
&mut runtime,
|
||||||
let command =
|
&mut debug,
|
||||||
runtime.enter(|| application.update(message));
|
messages,
|
||||||
debug.update_finished();
|
);
|
||||||
|
|
||||||
runtime.spawn(command);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update subscriptions
|
|
||||||
let subscription = application.subscription();
|
|
||||||
runtime.track(subscription);
|
|
||||||
|
|
||||||
// Update window
|
// Update window
|
||||||
state.synchronize(&application, context.window());
|
state.synchronize(&application, context.window());
|
||||||
|
|
||||||
user_interface = ManuallyDrop::new(build_user_interface(
|
user_interface =
|
||||||
&mut application,
|
ManuallyDrop::new(application::build_user_interface(
|
||||||
cache,
|
&mut application,
|
||||||
&mut renderer,
|
cache,
|
||||||
state.logical_size(),
|
&mut renderer,
|
||||||
&mut debug,
|
state.logical_size(),
|
||||||
));
|
&mut debug,
|
||||||
|
));
|
||||||
debug.draw_started();
|
|
||||||
primitive = user_interface
|
|
||||||
.draw(&mut renderer, state.cursor_position());
|
|
||||||
debug.draw_finished();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
debug.draw_started();
|
||||||
|
primitive =
|
||||||
|
user_interface.draw(&mut renderer, state.cursor_position());
|
||||||
|
debug.draw_finished();
|
||||||
|
|
||||||
context.window().request_redraw();
|
context.window().request_redraw();
|
||||||
}
|
}
|
||||||
event::Event::UserEvent(message) => {
|
event::Event::UserEvent(message) => {
|
||||||
|
|
@ -304,21 +293,3 @@ async fn run_instance<A, E, C>(
|
||||||
// Manually drop the user interface
|
// Manually drop the user interface
|
||||||
drop(ManuallyDrop::into_inner(user_interface));
|
drop(ManuallyDrop::into_inner(user_interface));
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -214,20 +214,21 @@ async fn run_instance<A, E, C>(
|
||||||
use iced_futures::futures::stream::StreamExt;
|
use iced_futures::futures::stream::StreamExt;
|
||||||
use winit::event;
|
use winit::event;
|
||||||
|
|
||||||
let mut state = State::new(&application, &window);
|
|
||||||
|
|
||||||
let surface = compositor.create_surface(&window);
|
let surface = compositor.create_surface(&window);
|
||||||
let physical_size = state.physical_size();
|
|
||||||
|
|
||||||
let mut viewport_version = state.viewport_version();
|
|
||||||
let mut swap_chain = compositor.create_swap_chain(
|
|
||||||
&surface,
|
|
||||||
physical_size.width,
|
|
||||||
physical_size.height,
|
|
||||||
);
|
|
||||||
|
|
||||||
let clipboard = Clipboard::new(&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(
|
let mut user_interface = ManuallyDrop::new(build_user_interface(
|
||||||
&mut application,
|
&mut application,
|
||||||
Cache::default(),
|
Cache::default(),
|
||||||
|
|
@ -264,29 +265,17 @@ async fn run_instance<A, E, C>(
|
||||||
events.clear();
|
events.clear();
|
||||||
debug.event_processing_finished();
|
debug.event_processing_finished();
|
||||||
|
|
||||||
if messages.is_empty() {
|
if !messages.is_empty() {
|
||||||
debug.draw_started();
|
|
||||||
primitive = user_interface
|
|
||||||
.draw(&mut renderer, state.cursor_position());
|
|
||||||
debug.draw_finished();
|
|
||||||
} else {
|
|
||||||
let cache =
|
let cache =
|
||||||
ManuallyDrop::into_inner(user_interface).into_cache();
|
ManuallyDrop::into_inner(user_interface).into_cache();
|
||||||
|
|
||||||
for message in messages.drain(..) {
|
// Update application
|
||||||
debug.log_message(&message);
|
update(
|
||||||
|
&mut application,
|
||||||
debug.update_started();
|
&mut runtime,
|
||||||
let command =
|
&mut debug,
|
||||||
runtime.enter(|| application.update(message));
|
messages,
|
||||||
debug.update_finished();
|
);
|
||||||
|
|
||||||
runtime.spawn(command);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update subscriptions
|
|
||||||
let subscription = application.subscription();
|
|
||||||
runtime.track(subscription);
|
|
||||||
|
|
||||||
// Update window
|
// Update window
|
||||||
state.synchronize(&application, &window);
|
state.synchronize(&application, &window);
|
||||||
|
|
@ -298,13 +287,13 @@ async fn run_instance<A, E, C>(
|
||||||
state.logical_size(),
|
state.logical_size(),
|
||||||
&mut debug,
|
&mut debug,
|
||||||
));
|
));
|
||||||
|
|
||||||
debug.draw_started();
|
|
||||||
primitive = user_interface
|
|
||||||
.draw(&mut renderer, state.cursor_position());
|
|
||||||
debug.draw_finished();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
debug.draw_started();
|
||||||
|
primitive =
|
||||||
|
user_interface.draw(&mut renderer, state.cursor_position());
|
||||||
|
debug.draw_finished();
|
||||||
|
|
||||||
window.request_redraw();
|
window.request_redraw();
|
||||||
}
|
}
|
||||||
event::Event::UserEvent(message) => {
|
event::Event::UserEvent(message) => {
|
||||||
|
|
@ -412,7 +401,7 @@ pub fn requests_exit(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_user_interface<'a, A: Application>(
|
pub fn build_user_interface<'a, A: Application>(
|
||||||
application: &'a mut A,
|
application: &'a mut A,
|
||||||
cache: Cache,
|
cache: Cache,
|
||||||
renderer: &mut A::Renderer,
|
renderer: &mut A::Renderer,
|
||||||
|
|
@ -429,3 +418,23 @@ fn build_user_interface<'a, A: Application>(
|
||||||
|
|
||||||
user_interface
|
user_interface
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue