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

@ -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);
}