Refactor Windows abstraction into WindowManager

This commit is contained in:
Héctor Ramón Jiménez 2023-12-02 22:26:01 +01:00
parent 31cccd8f7b
commit 5c5e7653be
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
4 changed files with 464 additions and 505 deletions

View file

@ -1,21 +1,20 @@
//! Create interactive, native cross-platform applications for WGPU. //! Create interactive, native cross-platform applications for WGPU.
mod state; mod state;
mod windows; mod window_manager;
pub use state::State; pub use state::State;
use crate::conversion; use crate::conversion;
use crate::core; use crate::core;
use crate::core::mouse;
use crate::core::renderer; use crate::core::renderer;
use crate::core::widget::operation; use crate::core::widget::operation;
use crate::core::window; use crate::core::window;
use crate::core::{Point, Size}; use crate::core::Size;
use crate::futures::futures::channel::mpsc; use crate::futures::futures::channel::mpsc;
use crate::futures::futures::{task, Future, StreamExt}; use crate::futures::futures::{task, Future, StreamExt};
use crate::futures::{Executor, Runtime, Subscription}; use crate::futures::{Executor, Runtime, Subscription};
use crate::graphics::{compositor, Compositor}; use crate::graphics::{compositor, Compositor};
use crate::multi_window::windows::Windows; use crate::multi_window::window_manager::WindowManager;
use crate::runtime::command::{self, Command}; use crate::runtime::command::{self, Command};
use crate::runtime::multi_window::Program; use crate::runtime::multi_window::Program;
use crate::runtime::user_interface::{self, UserInterface}; use crate::runtime::user_interface::{self, UserInterface};
@ -23,6 +22,7 @@ use crate::runtime::Debug;
use crate::style::application::StyleSheet; use crate::style::application::StyleSheet;
use crate::{Clipboard, Error, Proxy, Settings}; use crate::{Clipboard, Error, Proxy, Settings};
use std::collections::HashMap;
use std::mem::ManuallyDrop; use std::mem::ManuallyDrop;
use std::time::Instant; use std::time::Instant;
@ -182,13 +182,13 @@ where
} }
let mut compositor = C::new(compositor_settings, Some(&main_window))?; let mut compositor = C::new(compositor_settings, Some(&main_window))?;
let renderer = compositor.create_renderer();
let windows = Windows::new( let mut window_manager = WindowManager::new();
let _ = window_manager.insert(
window::Id::MAIN,
main_window,
&application, &application,
&mut compositor, &mut compositor,
renderer,
main_window,
exit_on_close_request, exit_on_close_request,
); );
@ -204,7 +204,7 @@ where
event_receiver, event_receiver,
control_sender, control_sender,
init_command, init_command,
windows, window_manager,
should_main_be_visible, should_main_be_visible,
)); ));
@ -312,7 +312,7 @@ async fn run_instance<A, E, C>(
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>,
init_command: Command<A::Message>, init_command: Command<A::Message>,
mut windows: Windows<A, C>, mut window_manager: WindowManager<A, C>,
should_main_window_be_visible: bool, should_main_window_be_visible: bool,
) where ) where
A: Application + 'static, A: Application + 'static,
@ -323,20 +323,39 @@ async fn run_instance<A, E, C>(
use winit::event; use winit::event;
use winit::event_loop::ControlFlow; use winit::event_loop::ControlFlow;
let mut clipboard = Clipboard::connect(windows.main()); let main_window = window_manager
.get_mut(window::Id::MAIN)
.expect("Get main window");
let mut ui_caches = vec![user_interface::Cache::default()]; if should_main_window_be_visible {
main_window.raw.set_visible(true);
}
let mut clipboard = Clipboard::connect(&main_window.raw);
let mut events = {
vec![(
Some(window::Id::MAIN),
core::Event::Window(
window::Id::MAIN,
window::Event::Opened {
position: main_window.position(),
size: main_window.size(),
},
),
)]
};
let mut ui_caches = HashMap::new();
let mut user_interfaces = ManuallyDrop::new(build_user_interfaces( let mut user_interfaces = ManuallyDrop::new(build_user_interfaces(
&application, &application,
&mut debug, &mut debug,
&mut windows, &mut window_manager,
vec![user_interface::Cache::default()], HashMap::from_iter([(
window::Id::MAIN,
user_interface::Cache::default(),
)]),
)); ));
if should_main_window_be_visible {
windows.main().set_visible(true);
}
run_command( run_command(
&application, &application,
&mut compositor, &mut compositor,
@ -346,26 +365,12 @@ async fn run_instance<A, E, C>(
&mut control_sender, &mut control_sender,
&mut proxy, &mut proxy,
&mut debug, &mut debug,
&mut windows, &mut window_manager,
&mut ui_caches, &mut ui_caches,
); );
runtime.track(application.subscription().into_recipes()); runtime.track(application.subscription().into_recipes());
let mut mouse_interaction = mouse::Interaction::default();
let mut events = {
let (position, size) = logical_bounds_of(windows.main());
vec![(
Some(window::Id::MAIN),
core::Event::Window(
window::Id::MAIN,
window::Event::Opened { position, size },
),
)]
};
let mut messages = Vec::new(); let mut messages = Vec::new();
let mut redraw_pending = false; let mut redraw_pending = false;
@ -378,31 +383,37 @@ async fn run_instance<A, E, C>(
window, window,
exit_on_close_request, exit_on_close_request,
} => { } => {
let (position, size) = logical_bounds_of(&window); let window = window_manager.insert(
let (inner_size, i) = windows.add(
&application,
&mut compositor,
id, id,
window, window,
&application,
&mut compositor,
exit_on_close_request, exit_on_close_request,
); );
user_interfaces.push(build_user_interface( let logical_size = window.state.logical_size();
&application,
user_interface::Cache::default(), let _ = user_interfaces.insert(
&mut windows.renderers[i],
inner_size,
&mut debug,
id, id,
)); build_user_interface(
ui_caches.push(user_interface::Cache::default()); &application,
user_interface::Cache::default(),
&mut window.renderer,
logical_size,
&mut debug,
id,
),
);
let _ = ui_caches.insert(id, user_interface::Cache::default());
events.push(( events.push((
Some(id), Some(id),
core::Event::Window( core::Event::Window(
id, id,
window::Event::Opened { position, size }, window::Event::Opened {
position: window.position(),
size: window.size(),
},
), ),
)); ));
} }
@ -420,12 +431,11 @@ async fn run_instance<A, E, C>(
debug.event_processing_started(); debug.event_processing_started();
let mut uis_stale = false; let mut uis_stale = false;
for (i, id) in windows.ids.iter().enumerate() { for (id, window) in window_manager.iter_mut() {
let mut window_events = vec![]; let mut window_events = vec![];
events.retain(|(window_id, event)| { events.retain(|(window_id, event)| {
if *window_id == Some(*id) if *window_id == Some(id) || window_id.is_none()
|| window_id.is_none()
{ {
window_events.push(event.clone()); window_events.push(event.clone());
false false
@ -441,11 +451,13 @@ async fn run_instance<A, E, C>(
continue; continue;
} }
let (ui_state, statuses) = user_interfaces[i] let (ui_state, statuses) = user_interfaces
.get_mut(&id)
.expect("Get user interface")
.update( .update(
&window_events, &window_events,
windows.states[i].cursor(), window.state.cursor(),
&mut windows.renderers[i], &mut window.renderer,
&mut clipboard, &mut clipboard,
&mut messages, &mut messages,
); );
@ -469,11 +481,12 @@ async fn run_instance<A, E, C>(
// TODO mw application update returns which window IDs to update // TODO mw application update returns which window IDs to update
if !messages.is_empty() || uis_stale { if !messages.is_empty() || uis_stale {
let mut cached_interfaces: Vec< let mut cached_interfaces: HashMap<
window::Id,
user_interface::Cache, user_interface::Cache,
> = ManuallyDrop::into_inner(user_interfaces) > = ManuallyDrop::into_inner(user_interfaces)
.drain(..) .drain()
.map(UserInterface::into_cache) .map(|(id, ui)| (id, ui.into_cache()))
.collect(); .collect();
// Update application // Update application
@ -486,18 +499,18 @@ async fn run_instance<A, E, C>(
&mut proxy, &mut proxy,
&mut debug, &mut debug,
&mut messages, &mut messages,
&mut windows, &mut window_manager,
&mut cached_interfaces, &mut cached_interfaces,
); );
// we must synchronize all window states with application state after an // we must synchronize all window states with application state after an
// application update since we don't know what changed // application update since we don't know what changed
for (state, (id, window)) in windows for (id, window) in window_manager.iter_mut() {
.states window.state.synchronize(
.iter_mut() &application,
.zip(windows.ids.iter().zip(windows.raw.iter())) id,
{ &window.raw,
state.synchronize(&application, *id, window); );
} }
// rebuild UIs with the synchronized states // rebuild UIs with the synchronized states
@ -505,39 +518,43 @@ async fn run_instance<A, E, C>(
ManuallyDrop::new(build_user_interfaces( ManuallyDrop::new(build_user_interfaces(
&application, &application,
&mut debug, &mut debug,
&mut windows, &mut window_manager,
cached_interfaces, cached_interfaces,
)); ));
} }
debug.draw_started(); debug.draw_started();
for (i, id) in windows.ids.iter().enumerate() { for (id, window) in window_manager.iter_mut() {
// TODO: Avoid redrawing all the time by forcing widgets to // TODO: Avoid redrawing all the time by forcing widgets to
// request redraws on state changes // request redraws on state changes
// //
// Then, we can use the `interface_state` here to decide if a redraw // Then, we can use the `interface_state` here to decide if a redraw
// is needed right away, or simply wait until a specific time. // is needed right away, or simply wait until a specific time.
let redraw_event = core::Event::Window( let redraw_event = core::Event::Window(
*id, id,
window::Event::RedrawRequested(Instant::now()), window::Event::RedrawRequested(Instant::now()),
); );
let cursor = windows.states[i].cursor(); let cursor = window.state.cursor();
let (ui_state, _) = user_interfaces[i].update( let ui = user_interfaces
.get_mut(&id)
.expect("Get user interface");
let (ui_state, _) = ui.update(
&[redraw_event.clone()], &[redraw_event.clone()],
cursor, cursor,
&mut windows.renderers[i], &mut window.renderer,
&mut clipboard, &mut clipboard,
&mut messages, &mut messages,
); );
let new_mouse_interaction = { let new_mouse_interaction = {
let state = &windows.states[i]; let state = &window.state;
user_interfaces[i].draw( ui.draw(
&mut windows.renderers[i], &mut window.renderer,
state.theme(), state.theme(),
&renderer::Style { &renderer::Style {
text_color: state.text_color(), text_color: state.text_color(),
@ -546,19 +563,21 @@ async fn run_instance<A, E, C>(
) )
}; };
if new_mouse_interaction != mouse_interaction { if new_mouse_interaction != window.mouse_interaction
windows.raw[i].set_cursor_icon( {
window.raw.set_cursor_icon(
conversion::mouse_interaction( conversion::mouse_interaction(
new_mouse_interaction, new_mouse_interaction,
), ),
); );
mouse_interaction = new_mouse_interaction; window.mouse_interaction =
new_mouse_interaction;
} }
// TODO once widgets can request to be redrawn, we can avoid always requesting a // TODO once widgets can request to be redrawn, we can avoid always requesting a
// redraw // redraw
windows.raw[i].request_redraw(); window.raw.request_redraw();
runtime.broadcast( runtime.broadcast(
redraw_event.clone(), redraw_event.clone(),
@ -606,9 +625,13 @@ async fn run_instance<A, E, C>(
messages.push(message); messages.push(message);
} }
event::Event::RedrawRequested(id) => { event::Event::RedrawRequested(id) => {
let i = windows.index_from_raw(id); let Some((id, window)) =
let state = &windows.states[i]; window_manager.get_mut_alias(id)
let physical_size = state.physical_size(); else {
continue;
};
let physical_size = window.state.physical_size();
if physical_size.width == 0 || physical_size.height == 0 if physical_size.width == 0 || physical_size.height == 0
{ {
@ -616,60 +639,65 @@ async fn run_instance<A, E, C>(
} }
debug.render_started(); debug.render_started();
let current_viewport_version = state.viewport_version(); if window.viewport_version
let window_viewport_version = != window.state.viewport_version()
windows.viewport_versions[i]; {
let logical_size = window.state.logical_size();
if window_viewport_version != current_viewport_version {
let logical_size = state.logical_size();
debug.layout_started(); debug.layout_started();
let renderer = &mut windows.renderers[i]; let ui = user_interfaces
let ui = user_interfaces.remove(i); .remove(&id)
.expect("Remove user interface");
user_interfaces let _ = user_interfaces.insert(
.insert(i, ui.relayout(logical_size, renderer)); id,
ui.relayout(logical_size, &mut window.renderer),
);
debug.layout_finished(); debug.layout_finished();
debug.draw_started(); debug.draw_started();
let new_mouse_interaction = user_interfaces[i] let new_mouse_interaction = user_interfaces
.get_mut(&id)
.expect("Get user interface")
.draw( .draw(
renderer, &mut window.renderer,
state.theme(), window.state.theme(),
&renderer::Style { &renderer::Style {
text_color: state.text_color(), text_color: window.state.text_color(),
}, },
state.cursor(), window.state.cursor(),
); );
if new_mouse_interaction != mouse_interaction { if new_mouse_interaction != window.mouse_interaction
windows.raw[i].set_cursor_icon( {
window.raw.set_cursor_icon(
conversion::mouse_interaction( conversion::mouse_interaction(
new_mouse_interaction, new_mouse_interaction,
), ),
); );
mouse_interaction = new_mouse_interaction; window.mouse_interaction =
new_mouse_interaction;
} }
debug.draw_finished(); debug.draw_finished();
compositor.configure_surface( compositor.configure_surface(
&mut windows.surfaces[i], &mut window.surface,
physical_size.width, physical_size.width,
physical_size.height, physical_size.height,
); );
windows.viewport_versions[i] = window.viewport_version =
current_viewport_version; window.state.viewport_version();
} }
match compositor.present( match compositor.present(
&mut windows.renderers[i], &mut window.renderer,
&mut windows.surfaces[i], &mut window.surface,
state.viewport(), window.state.viewport(),
state.background_color(), window.state.background_color(),
&debug.overlay(), &debug.overlay(),
) { ) {
Ok(()) => { Ok(()) => {
@ -690,8 +718,10 @@ async fn run_instance<A, E, C>(
); );
// Try rendering all windows again next frame. // Try rendering all windows again next frame.
for window in &windows.raw { for (_id, window) in
window.request_redraw(); window_manager.iter_mut()
{
window.raw.request_redraw();
} }
} }
}, },
@ -701,70 +731,43 @@ async fn run_instance<A, E, C>(
event: window_event, event: window_event,
window_id, window_id,
} => { } => {
let window_index = windows let Some((id, window)) =
.raw window_manager.get_mut_alias(window_id)
.iter() else {
.position(|w| w.id() == window_id); continue;
};
match window_index { if matches!(
Some(i) => { window_event,
let id = windows.ids[i]; winit::event::WindowEvent::CloseRequested
let raw = &windows.raw[i]; ) && window.exit_on_close_request
let exit_on_close_request = {
windows.exit_on_close_requested[i]; let _ = window_manager.remove(id);
let _ = user_interfaces.remove(&id);
let _ = ui_caches.remove(&id);
if matches!( events.push((
window_event, None,
winit::event::WindowEvent::CloseRequested core::Event::Window(id, window::Event::Closed),
) && exit_on_close_request ));
{
let i = windows.delete(id);
let _ = user_interfaces.remove(i);
let _ = ui_caches.remove(i);
if windows.is_empty() { if window_manager.is_empty() {
break 'main; break 'main;
}
} else {
let state = &mut windows.states[i];
state.update(
raw,
&window_event,
&mut debug,
);
if let Some(event) =
conversion::window_event(
id,
&window_event,
state.scale_factor(),
state.modifiers(),
)
{
events.push((Some(id), event));
}
}
} }
None => { } else {
// This is the only special case, since in order to trigger the Destroyed event the window.state.update(
// window reference from winit must be dropped, but we still want to inform the &window.raw,
// user that the window was destroyed so they can clean up any specific window &window_event,
// code for this window &mut debug,
if matches!( );
window_event,
winit::event::WindowEvent::Destroyed
) {
let id =
windows.get_pending_destroy(window_id);
events.push(( if let Some(event) = conversion::window_event(
None, id,
core::Event::Window( &window_event,
id, window.state.scale_factor(),
window::Event::Closed, window.state.modifiers(),
), ) {
)); events.push((Some(id), event));
}
} }
} }
} }
@ -811,8 +814,8 @@ fn update<A: Application, C, E: Executor>(
proxy: &mut winit::event_loop::EventLoopProxy<A::Message>, proxy: &mut winit::event_loop::EventLoopProxy<A::Message>,
debug: &mut Debug, debug: &mut Debug,
messages: &mut Vec<A::Message>, messages: &mut Vec<A::Message>,
windows: &mut Windows<A, C>, window_manager: &mut WindowManager<A, C>,
ui_caches: &mut Vec<user_interface::Cache>, ui_caches: &mut HashMap<window::Id, user_interface::Cache>,
) where ) where
C: Compositor<Renderer = A::Renderer> + 'static, C: Compositor<Renderer = A::Renderer> + 'static,
<A::Renderer as core::Renderer>::Theme: StyleSheet, <A::Renderer as core::Renderer>::Theme: StyleSheet,
@ -833,7 +836,7 @@ fn update<A: Application, C, E: Executor>(
control_sender, control_sender,
proxy, proxy,
debug, debug,
windows, window_manager,
ui_caches, ui_caches,
); );
} }
@ -852,8 +855,8 @@ fn run_command<A, C, E>(
control_sender: &mut mpsc::UnboundedSender<Control>, control_sender: &mut mpsc::UnboundedSender<Control>,
proxy: &mut winit::event_loop::EventLoopProxy<A::Message>, proxy: &mut winit::event_loop::EventLoopProxy<A::Message>,
debug: &mut Debug, debug: &mut Debug,
windows: &mut Windows<A, C>, window_manager: &mut WindowManager<A, C>,
ui_caches: &mut Vec<user_interface::Cache>, ui_caches: &mut HashMap<window::Id, user_interface::Cache>,
) where ) where
A: Application, A: Application,
E: Executor, E: Executor,
@ -886,7 +889,7 @@ fn run_command<A, C, E>(
}, },
command::Action::Window(action) => match action { command::Action::Window(action) => match action {
window::Action::Spawn(id, settings) => { window::Action::Spawn(id, settings) => {
let monitor = windows.last_monitor(); let monitor = window_manager.last_monitor();
control_sender control_sender
.start_send(Control::CreateWindow { .start_send(Control::CreateWindow {
@ -900,10 +903,10 @@ fn run_command<A, C, E>(
window::Action::Close(id) => { window::Action::Close(id) => {
use winit::event_loop::ControlFlow; use winit::event_loop::ControlFlow;
let i = windows.delete(id); let _ = window_manager.remove(id);
let _ = ui_caches.remove(i); let _ = ui_caches.remove(&id);
if windows.is_empty() { if window_manager.is_empty() {
control_sender control_sender
.start_send(Control::ChangeFlow( .start_send(Control::ChangeFlow(
ControlFlow::ExitWithCode(0), ControlFlow::ExitWithCode(0),
@ -912,113 +915,133 @@ fn run_command<A, C, E>(
} }
} }
window::Action::Drag(id) => { window::Action::Drag(id) => {
let _ = windows.with_raw(id).drag_window(); if let Some(window) = window_manager.get_mut(id) {
let _ = window.raw.drag_window();
}
} }
window::Action::Resize(id, size) => { window::Action::Resize(id, size) => {
windows.with_raw(id).set_inner_size( if let Some(window) = window_manager.get_mut(id) {
winit::dpi::LogicalSize { window.raw.set_inner_size(winit::dpi::LogicalSize {
width: size.width, width: size.width,
height: size.height, height: size.height,
}, });
); }
} }
window::Action::FetchSize(id, callback) => { window::Action::FetchSize(id, callback) => {
let window = windows.with_raw(id); if let Some(window) = window_manager.get_mut(id) {
let size = let size = window
window.inner_size().to_logical(window.scale_factor()); .raw
.inner_size()
.to_logical(window.raw.scale_factor());
proxy proxy
.send_event(callback(Size::new( .send_event(callback(Size::new(
size.width, size.width,
size.height, size.height,
))) )))
.expect("Send message to event loop"); .expect("Send message to event loop");
}
} }
window::Action::Maximize(id, maximized) => { window::Action::Maximize(id, maximized) => {
windows.with_raw(id).set_maximized(maximized); if let Some(window) = window_manager.get_mut(id) {
window.raw.set_maximized(maximized);
}
} }
window::Action::Minimize(id, minimized) => { window::Action::Minimize(id, minimized) => {
windows.with_raw(id).set_minimized(minimized); if let Some(window) = window_manager.get_mut(id) {
window.raw.set_minimized(minimized);
}
} }
window::Action::Move(id, position) => { window::Action::Move(id, position) => {
windows.with_raw(id).set_outer_position( if let Some(window) = window_manager.get_mut(id) {
winit::dpi::LogicalPosition { window.raw.set_outer_position(
x: position.x, winit::dpi::LogicalPosition {
y: position.y, x: position.x,
}, y: position.y,
); },
);
}
} }
window::Action::ChangeMode(id, mode) => { window::Action::ChangeMode(id, mode) => {
let window = windows.with_raw(id); if let Some(window) = window_manager.get_mut(id) {
window.set_visible(conversion::visible(mode)); window.raw.set_visible(conversion::visible(mode));
window.set_fullscreen(conversion::fullscreen( window.raw.set_fullscreen(conversion::fullscreen(
window.current_monitor(), window.raw.current_monitor(),
mode, mode,
)); ));
}
} }
window::Action::ChangeIcon(id, icon) => { window::Action::ChangeIcon(id, icon) => {
windows if let Some(window) = window_manager.get_mut(id) {
.with_raw(id) window.raw.set_window_icon(conversion::icon(icon));
.set_window_icon(conversion::icon(icon)); }
} }
window::Action::FetchMode(id, tag) => { window::Action::FetchMode(id, tag) => {
let window = windows.with_raw(id); if let Some(window) = window_manager.get_mut(id) {
let mode = if window.is_visible().unwrap_or(true) { let mode = if window.raw.is_visible().unwrap_or(true) {
conversion::mode(window.fullscreen()) conversion::mode(window.raw.fullscreen())
} else { } else {
core::window::Mode::Hidden core::window::Mode::Hidden
}; };
proxy proxy
.send_event(tag(mode)) .send_event(tag(mode))
.expect("Event loop doesn't exist."); .expect("Event loop doesn't exist.");
}
} }
window::Action::ToggleMaximize(id) => { window::Action::ToggleMaximize(id) => {
let window = windows.with_raw(id); if let Some(window) = window_manager.get_mut(id) {
window.set_maximized(!window.is_maximized()); window.raw.set_maximized(!window.raw.is_maximized());
}
} }
window::Action::ToggleDecorations(id) => { window::Action::ToggleDecorations(id) => {
let window = windows.with_raw(id); if let Some(window) = window_manager.get_mut(id) {
window.set_decorations(!window.is_decorated()); window.raw.set_decorations(!window.raw.is_decorated());
}
} }
window::Action::RequestUserAttention(id, attention_type) => { window::Action::RequestUserAttention(id, attention_type) => {
windows.with_raw(id).request_user_attention( if let Some(window) = window_manager.get_mut(id) {
attention_type.map(conversion::user_attention), window.raw.request_user_attention(
); attention_type.map(conversion::user_attention),
);
}
} }
window::Action::GainFocus(id) => { window::Action::GainFocus(id) => {
windows.with_raw(id).focus_window(); if let Some(window) = window_manager.get_mut(id) {
window.raw.focus_window();
}
} }
window::Action::ChangeLevel(id, level) => { window::Action::ChangeLevel(id, level) => {
windows if let Some(window) = window_manager.get_mut(id) {
.with_raw(id) window
.set_window_level(conversion::window_level(level)); .raw
.set_window_level(conversion::window_level(level));
}
} }
window::Action::FetchId(id, tag) => { window::Action::FetchId(id, tag) => {
proxy if let Some(window) = window_manager.get_mut(id) {
.send_event(tag(windows.with_raw(id).id().into())) proxy
.expect("Event loop doesn't exist."); .send_event(tag(window.raw.id().into()))
.expect("Event loop doesn't exist.");
}
} }
window::Action::Screenshot(id, tag) => { window::Action::Screenshot(id, tag) => {
let i = windows.index_from_id(id); if let Some(window) = window_manager.get_mut(id) {
let state = &windows.states[i]; let bytes = compositor.screenshot(
let surface = &mut windows.surfaces[i]; &mut window.renderer,
let renderer = &mut windows.renderers[i]; &mut window.surface,
window.state.viewport(),
window.state.background_color(),
&debug.overlay(),
);
let bytes = compositor.screenshot( proxy
renderer, .send_event(tag(window::Screenshot::new(
surface, bytes,
state.viewport(), window.state.physical_size(),
state.background_color(), )))
&debug.overlay(), .expect("Event loop doesn't exist.");
); }
proxy
.send_event(tag(window::Screenshot::new(
bytes,
state.physical_size(),
)))
.expect("Event loop doesn't exist.");
} }
}, },
command::Action::System(action) => match action { command::Action::System(action) => match action {
@ -1047,43 +1070,45 @@ fn run_command<A, C, E>(
let mut uis = build_user_interfaces( let mut uis = build_user_interfaces(
application, application,
debug, debug,
windows, window_manager,
std::mem::take(ui_caches), std::mem::take(ui_caches),
); );
'operate: while let Some(mut operation) = 'operate: while let Some(mut operation) =
current_operation.take() current_operation.take()
{ {
for (i, ui) in uis.iter_mut().enumerate() { for (id, ui) in uis.iter_mut() {
ui.operate(&windows.renderers[i], operation.as_mut()); if let Some(window) = window_manager.get_mut(*id) {
ui.operate(&window.renderer, operation.as_mut());
match operation.finish() { match operation.finish() {
operation::Outcome::None => {} operation::Outcome::None => {}
operation::Outcome::Some(message) => { operation::Outcome::Some(message) => {
proxy proxy
.send_event(message) .send_event(message)
.expect("Event loop doesn't exist."); .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;
} }
operation::Outcome::Chain(next) => { operation::Outcome::Chain(next) => {
current_operation = Some(next); current_operation = Some(next);
}
} }
} }
} }
} }
*ui_caches = *ui_caches =
uis.drain(..).map(UserInterface::into_cache).collect(); uis.drain().map(|(id, ui)| (id, ui.into_cache())).collect();
} }
command::Action::LoadFont { bytes, tagger } => { command::Action::LoadFont { bytes, tagger } => {
use crate::core::text::Renderer; use crate::core::text::Renderer;
// TODO change this once we change each renderer to having a single backend reference.. :pain: // TODO change this once we change each renderer to having a single backend reference.. :pain:
// TODO: Error handling (?) // TODO: Error handling (?)
for renderer in &mut windows.renderers { for (_, window) in window_manager.iter_mut() {
renderer.load_font(bytes.clone()); window.renderer.load_font(bytes.clone());
} }
proxy proxy
@ -1098,33 +1123,31 @@ fn run_command<A, C, E>(
pub fn build_user_interfaces<'a, A: Application, C: Compositor>( pub fn build_user_interfaces<'a, A: Application, C: Compositor>(
application: &'a A, application: &'a A,
debug: &mut Debug, debug: &mut Debug,
windows: &mut Windows<A, C>, window_manager: &mut WindowManager<A, C>,
mut cached_user_interfaces: Vec<user_interface::Cache>, mut cached_user_interfaces: HashMap<window::Id, user_interface::Cache>,
) -> Vec<UserInterface<'a, A::Message, A::Renderer>> ) -> HashMap<window::Id, UserInterface<'a, A::Message, A::Renderer>>
where where
<A::Renderer as core::Renderer>::Theme: StyleSheet, <A::Renderer as core::Renderer>::Theme: StyleSheet,
C: Compositor<Renderer = A::Renderer>, C: Compositor<Renderer = A::Renderer>,
{ {
cached_user_interfaces cached_user_interfaces
.drain(..) .drain()
.zip( .filter_map(|(id, cache)| {
windows let window = window_manager.get_mut(id)?;
.ids
.iter()
.zip(windows.states.iter().zip(windows.renderers.iter_mut())),
)
.fold(vec![], |mut uis, (cache, (id, (state, renderer)))| {
uis.push(build_user_interface(
application,
cache,
renderer,
state.logical_size(),
debug,
*id,
));
uis Some((
id,
build_user_interface(
application,
cache,
&mut window.renderer,
window.state.logical_size(),
debug,
id,
),
))
}) })
.collect()
} }
/// Returns true if the provided event should cause an [`Application`] to /// Returns true if the provided event should cause an [`Application`] to
@ -1148,25 +1171,6 @@ pub fn user_force_quit(
} }
} }
fn logical_bounds_of(window: &winit::window::Window) -> (Option<Point>, Size) {
let position = window
.inner_position()
.ok()
.map(|position| position.to_logical(window.scale_factor()))
.map(|position| Point {
x: position.x,
y: position.y,
});
let size = {
let size = window.inner_size().to_logical(window.scale_factor());
Size::new(size.width, size.height)
};
(position, size)
}
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
mod platform { mod platform {
pub fn run<T, F>( pub fn run<T, F>(

View file

@ -19,7 +19,7 @@ where
title: String, title: String,
scale_factor: f64, scale_factor: f64,
viewport: Viewport, viewport: Viewport,
viewport_version: usize, viewport_version: u64,
cursor_position: Option<winit::dpi::PhysicalPosition<f64>>, cursor_position: Option<winit::dpi::PhysicalPosition<f64>>,
modifiers: winit::event::ModifiersState, modifiers: winit::event::ModifiersState,
theme: <A::Renderer as core::Renderer>::Theme, theme: <A::Renderer as core::Renderer>::Theme,
@ -86,7 +86,7 @@ where
/// Returns the version of the [`Viewport`] of the [`State`]. /// Returns the version of the [`Viewport`] of the [`State`].
/// ///
/// The version is incremented every time the [`Viewport`] changes. /// The version is incremented every time the [`Viewport`] changes.
pub fn viewport_version(&self) -> usize { pub fn viewport_version(&self) -> u64 {
self.viewport_version self.viewport_version
} }

View file

@ -0,0 +1,156 @@
use crate::core::mouse;
use crate::core::window::Id;
use crate::core::{Point, Size};
use crate::graphics::Compositor;
use crate::multi_window::{Application, State};
use crate::style::application::StyleSheet;
use std::collections::BTreeMap;
use winit::monitor::MonitorHandle;
#[allow(missing_debug_implementations)]
pub struct WindowManager<A: Application, C: Compositor>
where
<A::Renderer as crate::core::Renderer>::Theme: StyleSheet,
C: Compositor<Renderer = A::Renderer>,
{
aliases: BTreeMap<winit::window::WindowId, Id>,
entries: BTreeMap<Id, Window<A, C>>,
}
impl<A, C> WindowManager<A, C>
where
A: Application,
C: Compositor<Renderer = A::Renderer>,
<A::Renderer as crate::core::Renderer>::Theme: StyleSheet,
{
pub fn new() -> Self {
Self {
aliases: BTreeMap::new(),
entries: BTreeMap::new(),
}
}
pub fn insert(
&mut self,
id: Id,
window: winit::window::Window,
application: &A,
compositor: &mut C,
exit_on_close_request: bool,
) -> &mut Window<A, C> {
let state = State::new(application, id, &window);
let viewport_version = state.viewport_version();
let physical_size = state.physical_size();
let surface = compositor.create_surface(
&window,
physical_size.width,
physical_size.height,
);
let renderer = compositor.create_renderer();
let _ = self.aliases.insert(window.id(), id);
let _ = self.entries.insert(
id,
Window {
raw: window,
state,
viewport_version,
exit_on_close_request,
surface,
renderer,
mouse_interaction: mouse::Interaction::Idle,
},
);
self.entries
.get_mut(&id)
.expect("Get window that was just inserted")
}
pub fn is_empty(&self) -> bool {
self.entries.is_empty()
}
pub fn iter_mut(
&mut self,
) -> impl Iterator<Item = (Id, &mut Window<A, C>)> {
self.entries.iter_mut().map(|(k, v)| (*k, v))
}
pub fn get_mut(&mut self, id: Id) -> Option<&mut Window<A, C>> {
self.entries.get_mut(&id)
}
pub fn get_mut_alias(
&mut self,
id: winit::window::WindowId,
) -> Option<(Id, &mut Window<A, C>)> {
let id = self.aliases.get(&id).copied()?;
Some((id, self.get_mut(id)?))
}
pub fn last_monitor(&self) -> Option<MonitorHandle> {
self.entries.values().last()?.raw.current_monitor()
}
pub fn remove(&mut self, id: Id) -> Option<Window<A, C>> {
let window = self.entries.remove(&id)?;
let _ = self.aliases.remove(&window.raw.id());
Some(window)
}
}
impl<A, C> Default for WindowManager<A, C>
where
A: Application,
C: Compositor<Renderer = A::Renderer>,
<A::Renderer as crate::core::Renderer>::Theme: StyleSheet,
{
fn default() -> Self {
Self::new()
}
}
#[allow(missing_debug_implementations)]
pub struct Window<A, C>
where
A: Application,
C: Compositor<Renderer = A::Renderer>,
<A::Renderer as crate::core::Renderer>::Theme: StyleSheet,
{
pub raw: winit::window::Window,
pub state: State<A>,
pub viewport_version: u64,
pub exit_on_close_request: bool,
pub mouse_interaction: mouse::Interaction,
pub surface: C::Surface,
pub renderer: A::Renderer,
}
impl<A, C> Window<A, C>
where
A: Application,
C: Compositor<Renderer = A::Renderer>,
<A::Renderer as crate::core::Renderer>::Theme: StyleSheet,
{
pub fn position(&self) -> Option<Point> {
self.raw
.inner_position()
.ok()
.map(|position| position.to_logical(self.raw.scale_factor()))
.map(|position| Point {
x: position.x,
y: position.y,
})
}
pub fn size(&self) -> Size {
let size = self.raw.inner_size().to_logical(self.raw.scale_factor());
Size::new(size.width, size.height)
}
}

View file

@ -1,201 +0,0 @@
use crate::core::{window, Size};
use crate::graphics::Compositor;
use crate::multi_window::{Application, State};
use crate::style::application::StyleSheet;
use winit::monitor::MonitorHandle;
use std::fmt::{Debug, Formatter};
pub struct Windows<A: Application, C: Compositor>
where
<A::Renderer as crate::core::Renderer>::Theme: StyleSheet,
C: Compositor<Renderer = A::Renderer>,
{
pub ids: Vec<window::Id>,
pub raw: Vec<winit::window::Window>,
pub states: Vec<State<A>>,
pub viewport_versions: Vec<usize>,
pub exit_on_close_requested: Vec<bool>,
pub surfaces: Vec<C::Surface>,
pub renderers: Vec<A::Renderer>,
pub pending_destroy: Vec<(window::Id, winit::window::WindowId)>,
}
impl<A: Application, C: Compositor> Debug for Windows<A, C>
where
<A::Renderer as crate::core::Renderer>::Theme: StyleSheet,
C: Compositor<Renderer = A::Renderer>,
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Windows")
.field("ids", &self.ids)
.field(
"raw",
&self
.raw
.iter()
.map(winit::window::Window::id)
.collect::<Vec<winit::window::WindowId>>(),
)
.field("states", &self.states)
.field("viewport_versions", &self.viewport_versions)
.finish()
}
}
impl<A: Application, C: Compositor> Windows<A, C>
where
<A::Renderer as crate::core::Renderer>::Theme: StyleSheet,
C: Compositor<Renderer = A::Renderer>,
{
/// Creates a new [`Windows`] with a single `window::Id::MAIN` window.
pub fn new(
application: &A,
compositor: &mut C,
renderer: A::Renderer,
main: winit::window::Window,
exit_on_close_requested: bool,
) -> Self {
let state = State::new(application, window::Id::MAIN, &main);
let viewport_version = state.viewport_version();
let physical_size = state.physical_size();
let surface = compositor.create_surface(
&main,
physical_size.width,
physical_size.height,
);
Self {
ids: vec![window::Id::MAIN],
raw: vec![main],
states: vec![state],
viewport_versions: vec![viewport_version],
exit_on_close_requested: vec![exit_on_close_requested],
surfaces: vec![surface],
renderers: vec![renderer],
pending_destroy: vec![],
}
}
/// Adds a new window to [`Windows`]. Returns the size of the newly created window in logical
/// pixels & the index of the window within [`Windows`].
pub fn add(
&mut self,
application: &A,
compositor: &mut C,
id: window::Id,
window: winit::window::Window,
exit_on_close_requested: bool,
) -> (Size, usize) {
let state = State::new(application, id, &window);
let window_size = state.logical_size();
let viewport_version = state.viewport_version();
let physical_size = state.physical_size();
let surface = compositor.create_surface(
&window,
physical_size.width,
physical_size.height,
);
let renderer = compositor.create_renderer();
self.ids.push(id);
self.raw.push(window);
self.states.push(state);
self.exit_on_close_requested.push(exit_on_close_requested);
self.viewport_versions.push(viewport_version);
self.surfaces.push(surface);
self.renderers.push(renderer);
(window_size, self.ids.len() - 1)
}
pub fn is_empty(&self) -> bool {
self.ids.is_empty()
}
pub fn main(&self) -> &winit::window::Window {
&self.raw[0]
}
pub fn index_from_raw(&self, id: winit::window::WindowId) -> usize {
self.raw
.iter()
.position(|window| window.id() == id)
.expect("No raw window in multi_window::Windows")
}
pub fn index_from_id(&self, id: window::Id) -> usize {
self.ids
.iter()
.position(|window_id| *window_id == id)
.expect("No window in multi_window::Windows")
}
pub fn last_monitor(&self) -> Option<MonitorHandle> {
self.raw
.last()
.and_then(winit::window::Window::current_monitor)
}
pub fn last(&self) -> usize {
self.ids.len() - 1
}
pub fn with_raw(&self, id: window::Id) -> &winit::window::Window {
let i = self.index_from_id(id);
&self.raw[i]
}
/// Deletes the window with `id` from [`Windows`]. Returns the index that the window had.
pub fn delete(&mut self, id: window::Id) -> usize {
let i = self.index_from_id(id);
let id = self.ids.remove(i);
let window = self.raw.remove(i);
let _ = self.states.remove(i);
let _ = self.exit_on_close_requested.remove(i);
let _ = self.viewport_versions.remove(i);
let _ = self.surfaces.remove(i);
self.pending_destroy.push((id, window.id()));
i
}
/// Gets the winit `window` that is pending to be destroyed if it exists.
pub fn get_pending_destroy(
&mut self,
window: winit::window::WindowId,
) -> window::Id {
let i = self
.pending_destroy
.iter()
.position(|(_, window_id)| window == *window_id)
.unwrap();
let (id, _) = self.pending_destroy.remove(i);
id
}
/// Returns the windows that need to be requested to closed, and also the windows that can be
/// closed immediately.
pub fn partition_close_requests(
&self,
) -> (Vec<window::Id>, Vec<window::Id>) {
self.exit_on_close_requested.iter().enumerate().fold(
(vec![], vec![]),
|(mut close_immediately, mut needs_request_closed), (i, close)| {
let id = self.ids[i];
if *close {
close_immediately.push(id);
} else {
needs_request_closed.push(id);
}
(close_immediately, needs_request_closed)
},
)
}
}