Implement backpressure mechanism in iced_winit::Proxy
This commit is contained in:
parent
e8ec6b94b6
commit
b6b51375cf
3 changed files with 152 additions and 112 deletions
|
|
@ -149,13 +149,14 @@ where
|
||||||
let event_loop = EventLoopBuilder::with_user_event()
|
let event_loop = EventLoopBuilder::with_user_event()
|
||||||
.build()
|
.build()
|
||||||
.expect("Create event loop");
|
.expect("Create event loop");
|
||||||
let proxy = event_loop.create_proxy();
|
|
||||||
|
let (proxy, worker) = Proxy::new(event_loop.create_proxy());
|
||||||
|
|
||||||
let runtime = {
|
let runtime = {
|
||||||
let proxy = Proxy::new(event_loop.create_proxy());
|
|
||||||
let executor = E::new().map_err(Error::ExecutorCreationFailed)?;
|
let executor = E::new().map_err(Error::ExecutorCreationFailed)?;
|
||||||
|
executor.spawn(worker);
|
||||||
|
|
||||||
Runtime::new(executor, proxy)
|
Runtime::new(executor, proxy.clone())
|
||||||
};
|
};
|
||||||
|
|
||||||
let (application, init_command) = {
|
let (application, init_command) = {
|
||||||
|
|
@ -305,7 +306,7 @@ async fn run_instance<A, E, C>(
|
||||||
mut compositor: C,
|
mut compositor: C,
|
||||||
mut renderer: A::Renderer,
|
mut renderer: A::Renderer,
|
||||||
mut runtime: Runtime<E, Proxy<A::Message>, A::Message>,
|
mut runtime: Runtime<E, Proxy<A::Message>, A::Message>,
|
||||||
mut proxy: winit::event_loop::EventLoopProxy<A::Message>,
|
mut proxy: Proxy<A::Message>,
|
||||||
mut debug: Debug,
|
mut debug: Debug,
|
||||||
mut event_receiver: mpsc::UnboundedReceiver<
|
mut event_receiver: mpsc::UnboundedReceiver<
|
||||||
winit::event::Event<A::Message>,
|
winit::event::Event<A::Message>,
|
||||||
|
|
@ -370,6 +371,7 @@ async fn run_instance<A, E, C>(
|
||||||
let mut mouse_interaction = mouse::Interaction::default();
|
let mut mouse_interaction = mouse::Interaction::default();
|
||||||
let mut events = Vec::new();
|
let mut events = Vec::new();
|
||||||
let mut messages = Vec::new();
|
let mut messages = Vec::new();
|
||||||
|
let mut user_events = 0;
|
||||||
let mut redraw_pending = false;
|
let mut redraw_pending = false;
|
||||||
|
|
||||||
debug.startup_finished();
|
debug.startup_finished();
|
||||||
|
|
@ -396,6 +398,7 @@ async fn run_instance<A, E, C>(
|
||||||
}
|
}
|
||||||
event::Event::UserEvent(message) => {
|
event::Event::UserEvent(message) => {
|
||||||
messages.push(message);
|
messages.push(message);
|
||||||
|
user_events += 1;
|
||||||
}
|
}
|
||||||
event::Event::WindowEvent {
|
event::Event::WindowEvent {
|
||||||
event: event::WindowEvent::RedrawRequested { .. },
|
event: event::WindowEvent::RedrawRequested { .. },
|
||||||
|
|
@ -593,6 +596,11 @@ async fn run_instance<A, E, C>(
|
||||||
if should_exit {
|
if should_exit {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if user_events > 0 {
|
||||||
|
proxy.free_slots(user_events);
|
||||||
|
user_events = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !redraw_pending {
|
if !redraw_pending {
|
||||||
|
|
@ -667,7 +675,7 @@ pub fn update<A: Application, C, E: Executor>(
|
||||||
runtime: &mut Runtime<E, Proxy<A::Message>, A::Message>,
|
runtime: &mut Runtime<E, Proxy<A::Message>, A::Message>,
|
||||||
clipboard: &mut Clipboard,
|
clipboard: &mut Clipboard,
|
||||||
should_exit: &mut bool,
|
should_exit: &mut bool,
|
||||||
proxy: &mut winit::event_loop::EventLoopProxy<A::Message>,
|
proxy: &mut Proxy<A::Message>,
|
||||||
debug: &mut Debug,
|
debug: &mut Debug,
|
||||||
messages: &mut Vec<A::Message>,
|
messages: &mut Vec<A::Message>,
|
||||||
window: &winit::window::Window,
|
window: &winit::window::Window,
|
||||||
|
|
@ -717,7 +725,7 @@ pub fn run_command<A, C, E>(
|
||||||
runtime: &mut Runtime<E, Proxy<A::Message>, A::Message>,
|
runtime: &mut Runtime<E, Proxy<A::Message>, A::Message>,
|
||||||
clipboard: &mut Clipboard,
|
clipboard: &mut Clipboard,
|
||||||
should_exit: &mut bool,
|
should_exit: &mut bool,
|
||||||
proxy: &mut winit::event_loop::EventLoopProxy<A::Message>,
|
proxy: &mut Proxy<A::Message>,
|
||||||
debug: &mut Debug,
|
debug: &mut Debug,
|
||||||
window: &winit::window::Window,
|
window: &winit::window::Window,
|
||||||
) where
|
) where
|
||||||
|
|
@ -742,9 +750,7 @@ pub fn run_command<A, C, E>(
|
||||||
clipboard::Action::Read(tag, kind) => {
|
clipboard::Action::Read(tag, kind) => {
|
||||||
let message = tag(clipboard.read(kind));
|
let message = tag(clipboard.read(kind));
|
||||||
|
|
||||||
proxy
|
proxy.send(message);
|
||||||
.send_event(message)
|
|
||||||
.expect("Send message to event loop");
|
|
||||||
}
|
}
|
||||||
clipboard::Action::Write(contents, kind) => {
|
clipboard::Action::Write(contents, kind) => {
|
||||||
clipboard.write(kind, contents);
|
clipboard.write(kind, contents);
|
||||||
|
|
@ -774,25 +780,16 @@ pub fn run_command<A, C, E>(
|
||||||
let size =
|
let size =
|
||||||
window.inner_size().to_logical(window.scale_factor());
|
window.inner_size().to_logical(window.scale_factor());
|
||||||
|
|
||||||
proxy
|
proxy.send(callback(Size::new(size.width, size.height)));
|
||||||
.send_event(callback(Size::new(
|
|
||||||
size.width,
|
|
||||||
size.height,
|
|
||||||
)))
|
|
||||||
.expect("Send message to event loop");
|
|
||||||
}
|
}
|
||||||
window::Action::FetchMaximized(_id, callback) => {
|
window::Action::FetchMaximized(_id, callback) => {
|
||||||
proxy
|
proxy.send(callback(window.is_maximized()));
|
||||||
.send_event(callback(window.is_maximized()))
|
|
||||||
.expect("Send message to event loop");
|
|
||||||
}
|
}
|
||||||
window::Action::Maximize(_id, maximized) => {
|
window::Action::Maximize(_id, maximized) => {
|
||||||
window.set_maximized(maximized);
|
window.set_maximized(maximized);
|
||||||
}
|
}
|
||||||
window::Action::FetchMinimized(_id, callback) => {
|
window::Action::FetchMinimized(_id, callback) => {
|
||||||
proxy
|
proxy.send(callback(window.is_minimized()));
|
||||||
.send_event(callback(window.is_minimized()))
|
|
||||||
.expect("Send message to event loop");
|
|
||||||
}
|
}
|
||||||
window::Action::Minimize(_id, minimized) => {
|
window::Action::Minimize(_id, minimized) => {
|
||||||
window.set_minimized(minimized);
|
window.set_minimized(minimized);
|
||||||
|
|
@ -808,9 +805,7 @@ pub fn run_command<A, C, E>(
|
||||||
})
|
})
|
||||||
.ok();
|
.ok();
|
||||||
|
|
||||||
proxy
|
proxy.send(callback(position));
|
||||||
.send_event(callback(position))
|
|
||||||
.expect("Send message to event loop");
|
|
||||||
}
|
}
|
||||||
window::Action::Move(_id, position) => {
|
window::Action::Move(_id, position) => {
|
||||||
window.set_outer_position(winit::dpi::LogicalPosition {
|
window.set_outer_position(winit::dpi::LogicalPosition {
|
||||||
|
|
@ -835,9 +830,7 @@ pub fn run_command<A, C, E>(
|
||||||
core::window::Mode::Hidden
|
core::window::Mode::Hidden
|
||||||
};
|
};
|
||||||
|
|
||||||
proxy
|
proxy.send(tag(mode));
|
||||||
.send_event(tag(mode))
|
|
||||||
.expect("Send message to event loop");
|
|
||||||
}
|
}
|
||||||
window::Action::ToggleMaximize(_id) => {
|
window::Action::ToggleMaximize(_id) => {
|
||||||
window.set_maximized(!window.is_maximized());
|
window.set_maximized(!window.is_maximized());
|
||||||
|
|
@ -865,17 +858,13 @@ pub fn run_command<A, C, E>(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
window::Action::FetchId(_id, tag) => {
|
window::Action::FetchId(_id, tag) => {
|
||||||
proxy
|
proxy.send(tag(window.id().into()));
|
||||||
.send_event(tag(window.id().into()))
|
|
||||||
.expect("Send message to event loop");
|
|
||||||
}
|
}
|
||||||
window::Action::RunWithHandle(_id, tag) => {
|
window::Action::RunWithHandle(_id, tag) => {
|
||||||
use window::raw_window_handle::HasWindowHandle;
|
use window::raw_window_handle::HasWindowHandle;
|
||||||
|
|
||||||
if let Ok(handle) = window.window_handle() {
|
if let Ok(handle) = window.window_handle() {
|
||||||
proxy
|
proxy.send(tag(&handle));
|
||||||
.send_event(tag(&handle))
|
|
||||||
.expect("Send message to event loop");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -888,12 +877,10 @@ pub fn run_command<A, C, E>(
|
||||||
&debug.overlay(),
|
&debug.overlay(),
|
||||||
);
|
);
|
||||||
|
|
||||||
proxy
|
proxy.send(tag(window::Screenshot::new(
|
||||||
.send_event(tag(window::Screenshot::new(
|
bytes,
|
||||||
bytes,
|
state.physical_size(),
|
||||||
state.physical_size(),
|
)));
|
||||||
)))
|
|
||||||
.expect("Send message to event loop.");
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
command::Action::System(action) => match action {
|
command::Action::System(action) => match action {
|
||||||
|
|
@ -901,7 +888,7 @@ pub fn run_command<A, C, E>(
|
||||||
#[cfg(feature = "system")]
|
#[cfg(feature = "system")]
|
||||||
{
|
{
|
||||||
let graphics_info = compositor.fetch_information();
|
let graphics_info = compositor.fetch_information();
|
||||||
let proxy = proxy.clone();
|
let mut proxy = proxy.clone();
|
||||||
|
|
||||||
let _ = std::thread::spawn(move || {
|
let _ = std::thread::spawn(move || {
|
||||||
let information =
|
let information =
|
||||||
|
|
@ -909,9 +896,7 @@ pub fn run_command<A, C, E>(
|
||||||
|
|
||||||
let message = _tag(information);
|
let message = _tag(information);
|
||||||
|
|
||||||
proxy
|
proxy.send(message);
|
||||||
.send_event(message)
|
|
||||||
.expect("Send message to event loop");
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -934,9 +919,7 @@ pub fn run_command<A, C, E>(
|
||||||
match operation.finish() {
|
match operation.finish() {
|
||||||
operation::Outcome::None => {}
|
operation::Outcome::None => {}
|
||||||
operation::Outcome::Some(message) => {
|
operation::Outcome::Some(message) => {
|
||||||
proxy
|
proxy.send(message);
|
||||||
.send_event(message)
|
|
||||||
.expect("Send message to event loop");
|
|
||||||
}
|
}
|
||||||
operation::Outcome::Chain(next) => {
|
operation::Outcome::Chain(next) => {
|
||||||
current_operation = Some(next);
|
current_operation = Some(next);
|
||||||
|
|
@ -951,9 +934,7 @@ pub fn run_command<A, C, E>(
|
||||||
// TODO: Error handling (?)
|
// TODO: Error handling (?)
|
||||||
compositor.load_font(bytes);
|
compositor.load_font(bytes);
|
||||||
|
|
||||||
proxy
|
proxy.send(tagger(Ok(())));
|
||||||
.send_event(tagger(Ok(())))
|
|
||||||
.expect("Send message to event loop");
|
|
||||||
}
|
}
|
||||||
command::Action::Custom(_) => {
|
command::Action::Custom(_) => {
|
||||||
log::warn!("Unsupported custom action in `iced_winit` shell");
|
log::warn!("Unsupported custom action in `iced_winit` shell");
|
||||||
|
|
|
||||||
|
|
@ -123,13 +123,13 @@ where
|
||||||
.build()
|
.build()
|
||||||
.expect("Create event loop");
|
.expect("Create event loop");
|
||||||
|
|
||||||
let proxy = event_loop.create_proxy();
|
let (proxy, worker) = Proxy::new(event_loop.create_proxy());
|
||||||
|
|
||||||
let runtime = {
|
let runtime = {
|
||||||
let proxy = Proxy::new(event_loop.create_proxy());
|
|
||||||
let executor = E::new().map_err(Error::ExecutorCreationFailed)?;
|
let executor = E::new().map_err(Error::ExecutorCreationFailed)?;
|
||||||
|
executor.spawn(worker);
|
||||||
|
|
||||||
Runtime::new(executor, proxy)
|
Runtime::new(executor, proxy.clone())
|
||||||
};
|
};
|
||||||
|
|
||||||
let (application, init_command) = {
|
let (application, init_command) = {
|
||||||
|
|
@ -343,7 +343,7 @@ async fn run_instance<A, E, C>(
|
||||||
mut application: A,
|
mut application: A,
|
||||||
mut compositor: C,
|
mut compositor: C,
|
||||||
mut runtime: Runtime<E, Proxy<A::Message>, A::Message>,
|
mut runtime: Runtime<E, Proxy<A::Message>, A::Message>,
|
||||||
mut proxy: winit::event_loop::EventLoopProxy<A::Message>,
|
mut proxy: Proxy<A::Message>,
|
||||||
mut debug: Debug,
|
mut debug: Debug,
|
||||||
mut event_receiver: mpsc::UnboundedReceiver<Event<A::Message>>,
|
mut event_receiver: mpsc::UnboundedReceiver<Event<A::Message>>,
|
||||||
mut control_sender: mpsc::UnboundedSender<Control>,
|
mut control_sender: mpsc::UnboundedSender<Control>,
|
||||||
|
|
@ -408,6 +408,7 @@ async fn run_instance<A, E, C>(
|
||||||
runtime.track(application.subscription().into_recipes());
|
runtime.track(application.subscription().into_recipes());
|
||||||
|
|
||||||
let mut messages = Vec::new();
|
let mut messages = Vec::new();
|
||||||
|
let mut user_events = 0;
|
||||||
|
|
||||||
debug.startup_finished();
|
debug.startup_finished();
|
||||||
|
|
||||||
|
|
@ -482,6 +483,7 @@ async fn run_instance<A, E, C>(
|
||||||
}
|
}
|
||||||
event::Event::UserEvent(message) => {
|
event::Event::UserEvent(message) => {
|
||||||
messages.push(message);
|
messages.push(message);
|
||||||
|
user_events += 1;
|
||||||
}
|
}
|
||||||
event::Event::WindowEvent {
|
event::Event::WindowEvent {
|
||||||
window_id: id,
|
window_id: id,
|
||||||
|
|
@ -803,6 +805,11 @@ async fn run_instance<A, E, C>(
|
||||||
&mut window_manager,
|
&mut window_manager,
|
||||||
cached_interfaces,
|
cached_interfaces,
|
||||||
));
|
));
|
||||||
|
|
||||||
|
if user_events > 0 {
|
||||||
|
proxy.free_slots(user_events);
|
||||||
|
user_events = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
|
|
@ -845,7 +852,7 @@ fn update<A: Application, C, E: Executor>(
|
||||||
runtime: &mut Runtime<E, Proxy<A::Message>, A::Message>,
|
runtime: &mut Runtime<E, Proxy<A::Message>, A::Message>,
|
||||||
clipboard: &mut Clipboard,
|
clipboard: &mut Clipboard,
|
||||||
control_sender: &mut mpsc::UnboundedSender<Control>,
|
control_sender: &mut mpsc::UnboundedSender<Control>,
|
||||||
proxy: &mut winit::event_loop::EventLoopProxy<A::Message>,
|
proxy: &mut Proxy<A::Message>,
|
||||||
debug: &mut Debug,
|
debug: &mut Debug,
|
||||||
messages: &mut Vec<A::Message>,
|
messages: &mut Vec<A::Message>,
|
||||||
window_manager: &mut WindowManager<A, C>,
|
window_manager: &mut WindowManager<A, C>,
|
||||||
|
|
@ -887,7 +894,7 @@ fn run_command<A, C, E>(
|
||||||
runtime: &mut Runtime<E, Proxy<A::Message>, A::Message>,
|
runtime: &mut Runtime<E, Proxy<A::Message>, A::Message>,
|
||||||
clipboard: &mut Clipboard,
|
clipboard: &mut Clipboard,
|
||||||
control_sender: &mut mpsc::UnboundedSender<Control>,
|
control_sender: &mut mpsc::UnboundedSender<Control>,
|
||||||
proxy: &mut winit::event_loop::EventLoopProxy<A::Message>,
|
proxy: &mut Proxy<A::Message>,
|
||||||
debug: &mut Debug,
|
debug: &mut Debug,
|
||||||
window_manager: &mut WindowManager<A, C>,
|
window_manager: &mut WindowManager<A, C>,
|
||||||
ui_caches: &mut FxHashMap<window::Id, user_interface::Cache>,
|
ui_caches: &mut FxHashMap<window::Id, user_interface::Cache>,
|
||||||
|
|
@ -913,9 +920,7 @@ fn run_command<A, C, E>(
|
||||||
clipboard::Action::Read(tag, kind) => {
|
clipboard::Action::Read(tag, kind) => {
|
||||||
let message = tag(clipboard.read(kind));
|
let message = tag(clipboard.read(kind));
|
||||||
|
|
||||||
proxy
|
proxy.send(message);
|
||||||
.send_event(message)
|
|
||||||
.expect("Send message to event loop");
|
|
||||||
}
|
}
|
||||||
clipboard::Action::Write(contents, kind) => {
|
clipboard::Action::Write(contents, kind) => {
|
||||||
clipboard.write(kind, contents);
|
clipboard.write(kind, contents);
|
||||||
|
|
@ -967,18 +972,12 @@ fn run_command<A, C, E>(
|
||||||
.to_logical(window.raw.scale_factor());
|
.to_logical(window.raw.scale_factor());
|
||||||
|
|
||||||
proxy
|
proxy
|
||||||
.send_event(callback(Size::new(
|
.send(callback(Size::new(size.width, size.height)));
|
||||||
size.width,
|
|
||||||
size.height,
|
|
||||||
)))
|
|
||||||
.expect("Send message to event loop");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
window::Action::FetchMaximized(id, callback) => {
|
window::Action::FetchMaximized(id, callback) => {
|
||||||
if let Some(window) = window_manager.get_mut(id) {
|
if let Some(window) = window_manager.get_mut(id) {
|
||||||
proxy
|
proxy.send(callback(window.raw.is_maximized()));
|
||||||
.send_event(callback(window.raw.is_maximized()))
|
|
||||||
.expect("Send message to event loop");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
window::Action::Maximize(id, maximized) => {
|
window::Action::Maximize(id, maximized) => {
|
||||||
|
|
@ -988,9 +987,7 @@ fn run_command<A, C, E>(
|
||||||
}
|
}
|
||||||
window::Action::FetchMinimized(id, callback) => {
|
window::Action::FetchMinimized(id, callback) => {
|
||||||
if let Some(window) = window_manager.get_mut(id) {
|
if let Some(window) = window_manager.get_mut(id) {
|
||||||
proxy
|
proxy.send(callback(window.raw.is_minimized()));
|
||||||
.send_event(callback(window.raw.is_minimized()))
|
|
||||||
.expect("Send message to event loop");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
window::Action::Minimize(id, minimized) => {
|
window::Action::Minimize(id, minimized) => {
|
||||||
|
|
@ -1012,9 +1009,7 @@ fn run_command<A, C, E>(
|
||||||
})
|
})
|
||||||
.ok();
|
.ok();
|
||||||
|
|
||||||
proxy
|
proxy.send(callback(position));
|
||||||
.send_event(callback(position))
|
|
||||||
.expect("Send message to event loop");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
window::Action::Move(id, position) => {
|
window::Action::Move(id, position) => {
|
||||||
|
|
@ -1049,9 +1044,7 @@ fn run_command<A, C, E>(
|
||||||
core::window::Mode::Hidden
|
core::window::Mode::Hidden
|
||||||
};
|
};
|
||||||
|
|
||||||
proxy
|
proxy.send(tag(mode));
|
||||||
.send_event(tag(mode))
|
|
||||||
.expect("Send message to event loop");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
window::Action::ToggleMaximize(id) => {
|
window::Action::ToggleMaximize(id) => {
|
||||||
|
|
@ -1099,9 +1092,7 @@ fn run_command<A, C, E>(
|
||||||
}
|
}
|
||||||
window::Action::FetchId(id, tag) => {
|
window::Action::FetchId(id, tag) => {
|
||||||
if let Some(window) = window_manager.get_mut(id) {
|
if let Some(window) = window_manager.get_mut(id) {
|
||||||
proxy
|
proxy.send(tag(window.raw.id().into()));
|
||||||
.send_event(tag(window.raw.id().into()))
|
|
||||||
.expect("Send message to event loop");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
window::Action::RunWithHandle(id, tag) => {
|
window::Action::RunWithHandle(id, tag) => {
|
||||||
|
|
@ -1111,9 +1102,7 @@ fn run_command<A, C, E>(
|
||||||
.get_mut(id)
|
.get_mut(id)
|
||||||
.and_then(|window| window.raw.window_handle().ok())
|
.and_then(|window| window.raw.window_handle().ok())
|
||||||
{
|
{
|
||||||
proxy
|
proxy.send(tag(&handle));
|
||||||
.send_event(tag(&handle))
|
|
||||||
.expect("Send message to event loop");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
window::Action::Screenshot(id, tag) => {
|
window::Action::Screenshot(id, tag) => {
|
||||||
|
|
@ -1126,12 +1115,10 @@ fn run_command<A, C, E>(
|
||||||
&debug.overlay(),
|
&debug.overlay(),
|
||||||
);
|
);
|
||||||
|
|
||||||
proxy
|
proxy.send(tag(window::Screenshot::new(
|
||||||
.send_event(tag(window::Screenshot::new(
|
bytes,
|
||||||
bytes,
|
window.state.physical_size(),
|
||||||
window.state.physical_size(),
|
)));
|
||||||
)))
|
|
||||||
.expect("Event loop doesn't exist.");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
@ -1140,7 +1127,7 @@ fn run_command<A, C, E>(
|
||||||
#[cfg(feature = "system")]
|
#[cfg(feature = "system")]
|
||||||
{
|
{
|
||||||
let graphics_info = compositor.fetch_information();
|
let graphics_info = compositor.fetch_information();
|
||||||
let proxy = proxy.clone();
|
let mut proxy = proxy.clone();
|
||||||
|
|
||||||
let _ = std::thread::spawn(move || {
|
let _ = std::thread::spawn(move || {
|
||||||
let information =
|
let information =
|
||||||
|
|
@ -1148,9 +1135,7 @@ fn run_command<A, C, E>(
|
||||||
|
|
||||||
let message = _tag(information);
|
let message = _tag(information);
|
||||||
|
|
||||||
proxy
|
proxy.send(message);
|
||||||
.send_event(message)
|
|
||||||
.expect("Event loop doesn't exist.");
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1175,9 +1160,7 @@ fn run_command<A, C, E>(
|
||||||
match operation.finish() {
|
match operation.finish() {
|
||||||
operation::Outcome::None => {}
|
operation::Outcome::None => {}
|
||||||
operation::Outcome::Some(message) => {
|
operation::Outcome::Some(message) => {
|
||||||
proxy
|
proxy.send(message);
|
||||||
.send_event(message)
|
|
||||||
.expect("Event loop doesn't exist.");
|
|
||||||
|
|
||||||
// operation completed, don't need to try to operate on rest of UIs
|
// operation completed, don't need to try to operate on rest of UIs
|
||||||
break 'operate;
|
break 'operate;
|
||||||
|
|
@ -1197,9 +1180,7 @@ fn run_command<A, C, E>(
|
||||||
// TODO: Error handling (?)
|
// TODO: Error handling (?)
|
||||||
compositor.load_font(bytes.clone());
|
compositor.load_font(bytes.clone());
|
||||||
|
|
||||||
proxy
|
proxy.send(tagger(Ok(())));
|
||||||
.send_event(tagger(Ok(())))
|
|
||||||
.expect("Send message to event loop");
|
|
||||||
}
|
}
|
||||||
command::Action::Custom(_) => {
|
command::Action::Custom(_) => {
|
||||||
log::warn!("Unsupported custom action in `iced_winit` shell");
|
log::warn!("Unsupported custom action in `iced_winit` shell");
|
||||||
|
|
|
||||||
|
|
@ -1,28 +1,101 @@
|
||||||
use crate::futures::futures::{
|
use crate::futures::futures::{
|
||||||
channel::mpsc,
|
channel::mpsc,
|
||||||
|
stream,
|
||||||
task::{Context, Poll},
|
task::{Context, Poll},
|
||||||
Sink,
|
Future, Sink, StreamExt,
|
||||||
};
|
};
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
|
|
||||||
/// An event loop proxy that implements `Sink`.
|
/// An event loop proxy with backpressure that implements `Sink`.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Proxy<Message: 'static> {
|
pub struct Proxy<Message: 'static> {
|
||||||
raw: winit::event_loop::EventLoopProxy<Message>,
|
raw: winit::event_loop::EventLoopProxy<Message>,
|
||||||
|
sender: mpsc::Sender<Message>,
|
||||||
|
notifier: mpsc::Sender<usize>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Message: 'static> Clone for Proxy<Message> {
|
impl<Message: 'static> Clone for Proxy<Message> {
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
Self {
|
Self {
|
||||||
raw: self.raw.clone(),
|
raw: self.raw.clone(),
|
||||||
|
sender: self.sender.clone(),
|
||||||
|
notifier: self.notifier.clone(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Message: 'static> Proxy<Message> {
|
impl<Message: 'static> Proxy<Message> {
|
||||||
|
const MAX_SIZE: usize = 100;
|
||||||
|
|
||||||
/// Creates a new [`Proxy`] from an `EventLoopProxy`.
|
/// Creates a new [`Proxy`] from an `EventLoopProxy`.
|
||||||
pub fn new(raw: winit::event_loop::EventLoopProxy<Message>) -> Self {
|
pub fn new(
|
||||||
Self { raw }
|
raw: winit::event_loop::EventLoopProxy<Message>,
|
||||||
|
) -> (Self, impl Future<Output = ()>) {
|
||||||
|
let (notifier, processed) = mpsc::channel(Self::MAX_SIZE);
|
||||||
|
let (sender, receiver) = mpsc::channel(Self::MAX_SIZE);
|
||||||
|
let proxy = raw.clone();
|
||||||
|
|
||||||
|
let worker = async move {
|
||||||
|
enum Item<T> {
|
||||||
|
MessageProduced(T),
|
||||||
|
BatchProcessed(usize),
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut receiver = receiver.map(Item::MessageProduced);
|
||||||
|
let mut processed = processed.map(Item::BatchProcessed);
|
||||||
|
|
||||||
|
let mut count = 0;
|
||||||
|
|
||||||
|
loop {
|
||||||
|
if count < Self::MAX_SIZE {
|
||||||
|
let mut stream =
|
||||||
|
stream::select(receiver.by_ref(), processed.by_ref());
|
||||||
|
|
||||||
|
match stream.select_next_some().await {
|
||||||
|
Item::MessageProduced(message) => {
|
||||||
|
let _ = proxy.send_event(message);
|
||||||
|
|
||||||
|
count += 1;
|
||||||
|
}
|
||||||
|
Item::BatchProcessed(amount) => {
|
||||||
|
count = count.saturating_sub(amount);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if let Item::BatchProcessed(amount) =
|
||||||
|
processed.select_next_some().await
|
||||||
|
{
|
||||||
|
count = count.saturating_sub(amount);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
(
|
||||||
|
Self {
|
||||||
|
raw,
|
||||||
|
sender,
|
||||||
|
notifier,
|
||||||
|
},
|
||||||
|
worker,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sends a `Message` to the event loop.
|
||||||
|
///
|
||||||
|
/// Note: This skips the backpressure mechanism with an unbounded
|
||||||
|
/// channel. Use sparingly!
|
||||||
|
pub fn send(&mut self, message: Message)
|
||||||
|
where
|
||||||
|
Message: std::fmt::Debug,
|
||||||
|
{
|
||||||
|
self.raw
|
||||||
|
.send_event(message)
|
||||||
|
.expect("Send message to event loop");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Frees an amount of slots for additional messages to be queued in
|
||||||
|
/// this [`Proxy`].
|
||||||
|
pub fn free_slots(&mut self, amount: usize) {
|
||||||
|
let _ = self.notifier.start_send(amount);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -30,32 +103,37 @@ impl<Message: 'static> Sink<Message> for Proxy<Message> {
|
||||||
type Error = mpsc::SendError;
|
type Error = mpsc::SendError;
|
||||||
|
|
||||||
fn poll_ready(
|
fn poll_ready(
|
||||||
self: Pin<&mut Self>,
|
mut self: Pin<&mut Self>,
|
||||||
_cx: &mut Context<'_>,
|
cx: &mut Context<'_>,
|
||||||
) -> Poll<Result<(), Self::Error>> {
|
) -> Poll<Result<(), Self::Error>> {
|
||||||
Poll::Ready(Ok(()))
|
self.sender.poll_ready(cx)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn start_send(
|
fn start_send(
|
||||||
self: Pin<&mut Self>,
|
mut self: Pin<&mut Self>,
|
||||||
message: Message,
|
message: Message,
|
||||||
) -> Result<(), Self::Error> {
|
) -> Result<(), Self::Error> {
|
||||||
let _ = self.raw.send_event(message);
|
self.sender.start_send(message)
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn poll_flush(
|
fn poll_flush(
|
||||||
self: Pin<&mut Self>,
|
mut self: Pin<&mut Self>,
|
||||||
_cx: &mut Context<'_>,
|
cx: &mut Context<'_>,
|
||||||
) -> Poll<Result<(), Self::Error>> {
|
) -> Poll<Result<(), Self::Error>> {
|
||||||
Poll::Ready(Ok(()))
|
match self.sender.poll_ready(cx) {
|
||||||
|
Poll::Ready(Err(ref e)) if e.is_disconnected() => {
|
||||||
|
// If the receiver disconnected, we consider the sink to be flushed.
|
||||||
|
Poll::Ready(Ok(()))
|
||||||
|
}
|
||||||
|
x => x,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn poll_close(
|
fn poll_close(
|
||||||
self: Pin<&mut Self>,
|
mut self: Pin<&mut Self>,
|
||||||
_cx: &mut Context<'_>,
|
_cx: &mut Context<'_>,
|
||||||
) -> Poll<Result<(), Self::Error>> {
|
) -> Poll<Result<(), Self::Error>> {
|
||||||
|
self.sender.disconnect();
|
||||||
Poll::Ready(Ok(()))
|
Poll::Ready(Ok(()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue