Remove a bit of code duplication in both shells

This commit is contained in:
Héctor Ramón Jiménez 2020-11-05 04:50:57 +01:00
parent d5a15419e9
commit e966cd5b59
2 changed files with 79 additions and 99 deletions

View File

@ -1,5 +1,5 @@
//! 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};
@ -7,7 +7,7 @@ use iced_graphics::window;
use iced_winit::conversion;
use iced_winit::futures;
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 std::mem::ManuallyDrop;
@ -137,18 +137,18 @@ async fn run_instance<A, E, C>(
use glutin::event;
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 mut user_interface = ManuallyDrop::new(build_user_interface(
&mut application,
Cache::default(),
&mut renderer,
state.logical_size(),
&mut debug,
));
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());
@ -178,47 +178,36 @@ async fn run_instance<A, E, C>(
events.clear();
debug.event_processing_finished();
if messages.is_empty() {
debug.draw_started();
primitive = user_interface
.draw(&mut renderer, state.cursor_position());
debug.draw_finished();
} else {
if !messages.is_empty() {
let cache =
ManuallyDrop::into_inner(user_interface).into_cache();
for message in messages.drain(..) {
debug.log_message(&message);
debug.update_started();
let command =
runtime.enter(|| application.update(message));
debug.update_finished();
runtime.spawn(command);
}
// Update subscriptions
let subscription = application.subscription();
runtime.track(subscription);
// Update application
application::update(
&mut application,
&mut runtime,
&mut debug,
messages,
);
// Update window
state.synchronize(&application, context.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();
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) => {
@ -304,21 +293,3 @@ async fn run_instance<A, E, C>(
// Manually drop the 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
}

View File

@ -214,20 +214,21 @@ async fn run_instance<A, E, C>(
use iced_futures::futures::stream::StreamExt;
use winit::event;
let mut state = State::new(&application, &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 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(),
@ -264,29 +265,17 @@ async fn run_instance<A, E, C>(
events.clear();
debug.event_processing_finished();
if messages.is_empty() {
debug.draw_started();
primitive = user_interface
.draw(&mut renderer, state.cursor_position());
debug.draw_finished();
} else {
if !messages.is_empty() {
let cache =
ManuallyDrop::into_inner(user_interface).into_cache();
for message in messages.drain(..) {
debug.log_message(&message);
debug.update_started();
let command =
runtime.enter(|| application.update(message));
debug.update_finished();
runtime.spawn(command);
}
// Update subscriptions
let subscription = application.subscription();
runtime.track(subscription);
// Update application
update(
&mut application,
&mut runtime,
&mut debug,
messages,
);
// Update window
state.synchronize(&application, &window);
@ -298,13 +287,13 @@ async fn run_instance<A, E, C>(
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();
window.request_redraw();
}
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,
cache: Cache,
renderer: &mut A::Renderer,
@ -429,3 +418,23 @@ fn build_user_interface<'a, A: Application>(
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);
}