diff options
39 files changed, 3904 insertions, 251 deletions
@@ -46,6 +46,8 @@ chrome-trace = [ "iced_wgpu?/tracing", "iced_glow?/tracing", ] +# Enables experimental multi-window support +multi_window = ["iced_winit/multi_window", "iced_glutin/multi_window"] [badges] maintenance = { status = "actively-developed" } diff --git a/examples/events/src/main.rs b/examples/events/src/main.rs index 1b97018e..b57010c7 100644 --- a/examples/events/src/main.rs +++ b/examples/events/src/main.rs @@ -25,7 +25,7 @@ struct Events { enum Message { EventOccurred(iced_native::Event), Toggled(bool), - Exit, + Exit(window::Id), } impl Application for Events { @@ -54,8 +54,8 @@ impl Application for Events { Command::none() } Message::EventOccurred(event) => { - if let Event::Window(window::Event::CloseRequested) = event { - window::close() + if let Event::Window(id, window::Event::CloseRequested) = event { + window::close(id) } else { Command::none() } @@ -65,7 +65,7 @@ impl Application for Events { Command::none() } - Message::Exit => window::close(), + Message::Exit(id) => window::close(id), } } diff --git a/examples/integration_opengl/src/main.rs b/examples/integration_opengl/src/main.rs index f161c8a0..fdbd7369 100644 --- a/examples/integration_opengl/src/main.rs +++ b/examples/integration_opengl/src/main.rs @@ -13,6 +13,7 @@ use iced_glow::{Backend, Renderer, Settings, Viewport}; use iced_glutin::conversion; use iced_glutin::glutin; use iced_glutin::renderer; +use iced_glutin::window; use iced_glutin::{program, Clipboard, Color, Debug, Size}; pub fn main() { @@ -30,7 +31,8 @@ pub fn main() { .unwrap(); unsafe { - let windowed_context = windowed_context.make_current().unwrap(); + let windowed_context = + windowed_context.make_current(todo!("derezzedex")).unwrap(); let gl = glow::Context::from_loader_function(|s| { windowed_context.get_proc_address(s) as *const _ @@ -107,6 +109,7 @@ pub fn main() { // Map window event to iced event if let Some(event) = iced_winit::conversion::window_event( + window::Id::MAIN, &event, windowed_context.window().scale_factor(), modifiers, @@ -179,7 +182,7 @@ pub fn main() { ), ); - windowed_context.swap_buffers().unwrap(); + windowed_context.swap_buffers(todo!("derezzedex")).unwrap(); } _ => (), } diff --git a/examples/integration_wgpu/src/main.rs b/examples/integration_wgpu/src/main.rs index 2a56b6fa..1f42013b 100644 --- a/examples/integration_wgpu/src/main.rs +++ b/examples/integration_wgpu/src/main.rs @@ -6,8 +6,8 @@ use scene::Scene; use iced_wgpu::{wgpu, Backend, Renderer, Settings, Viewport}; use iced_winit::{ - conversion, futures, program, renderer, winit, Clipboard, Color, Debug, - Size, + conversion, futures, program, renderer, window, winit, Clipboard, Color, + Debug, Size, }; use winit::{ @@ -169,6 +169,7 @@ pub fn main() { // Map window event to iced event if let Some(event) = iced_winit::conversion::window_event( + window::Id::MAIN, &event, window.scale_factor(), modifiers, diff --git a/examples/multi_window/Cargo.toml b/examples/multi_window/Cargo.toml new file mode 100644 index 00000000..62198595 --- /dev/null +++ b/examples/multi_window/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "multi_window" +version = "0.1.0" +authors = ["Richard Custodio <richardsoncusto@gmail.com>"] +edition = "2021" +publish = false +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +iced = { path = "../..", features = ["debug", "multi_window"] } +env_logger = "0.10.0" +iced_native = { path = "../../native" } +iced_lazy = { path = "../../lazy" } diff --git a/examples/multi_window/src/main.rs b/examples/multi_window/src/main.rs new file mode 100644 index 00000000..c2687ee6 --- /dev/null +++ b/examples/multi_window/src/main.rs @@ -0,0 +1,608 @@ +use iced::alignment::{self, Alignment}; +use iced::executor; +use iced::keyboard; +use iced::multi_window::Application; +use iced::theme::{self, Theme}; +use iced::widget::pane_grid::{self, PaneGrid}; +use iced::widget::{ + button, column, container, pick_list, row, scrollable, text, text_input, +}; +use iced::window; +use iced::{Color, Command, Element, Length, Settings, Size, Subscription}; +use iced_lazy::responsive; +use iced_native::{event, subscription, Event}; + +use iced_native::widget::scrollable::{Properties, RelativeOffset}; +use iced_native::window::Id; +use std::collections::HashMap; + +pub fn main() -> iced::Result { + env_logger::init(); + + Example::run(Settings::default()) +} + +struct Example { + windows: HashMap<window::Id, Window>, + panes_created: usize, + _focused: window::Id, +} + +#[derive(Debug)] +struct Window { + title: String, + scale: f64, + panes: pane_grid::State<Pane>, + focus: Option<pane_grid::Pane>, +} + +#[derive(Debug, Clone)] +enum Message { + Window(window::Id, WindowMessage), +} + +#[derive(Debug, Clone)] +enum WindowMessage { + Split(pane_grid::Axis, pane_grid::Pane), + SplitFocused(pane_grid::Axis), + FocusAdjacent(pane_grid::Direction), + Clicked(pane_grid::Pane), + Dragged(pane_grid::DragEvent), + PopOut(pane_grid::Pane), + Resized(pane_grid::ResizeEvent), + TitleChanged(String), + ToggleMoving(pane_grid::Pane), + TogglePin(pane_grid::Pane), + Close(pane_grid::Pane), + CloseFocused, + SelectedWindow(pane_grid::Pane, SelectableWindow), + CloseWindow, + SnapToggle, +} + +impl Application for Example { + type Executor = executor::Default; + type Message = Message; + type Theme = Theme; + type Flags = (); + + fn new(_flags: ()) -> (Self, Command<Message>) { + let (panes, _) = + pane_grid::State::new(Pane::new(0, pane_grid::Axis::Horizontal)); + let window = Window { + panes, + focus: None, + title: String::from("Default window"), + scale: 1.0, + }; + + ( + Example { + windows: HashMap::from([(window::Id::MAIN, window)]), + panes_created: 1, + _focused: window::Id::MAIN, + }, + Command::none(), + ) + } + + fn title(&self, window: window::Id) -> String { + self.windows + .get(&window) + .map(|w| w.title.clone()) + .unwrap_or(String::from("New Window")) + } + + fn update(&mut self, message: Message) -> Command<Message> { + let Message::Window(id, message) = message; + match message { + WindowMessage::SnapToggle => { + let window = self.windows.get_mut(&id).unwrap(); + + if let Some(focused) = &window.focus { + let pane = window.panes.get_mut(focused).unwrap(); + + let cmd = scrollable::snap_to( + pane.scrollable_id.clone(), + if pane.snapped { + RelativeOffset::START + } else { + RelativeOffset::END + }, + ); + + pane.snapped = !pane.snapped; + return cmd; + } + } + WindowMessage::Split(axis, pane) => { + let window = self.windows.get_mut(&id).unwrap(); + let result = window.panes.split( + axis, + &pane, + Pane::new(self.panes_created, axis), + ); + + if let Some((pane, _)) = result { + window.focus = Some(pane); + } + + self.panes_created += 1; + } + WindowMessage::SplitFocused(axis) => { + let window = self.windows.get_mut(&id).unwrap(); + if let Some(pane) = window.focus { + let result = window.panes.split( + axis, + &pane, + Pane::new(self.panes_created, axis), + ); + + if let Some((pane, _)) = result { + window.focus = Some(pane); + } + + self.panes_created += 1; + } + } + WindowMessage::FocusAdjacent(direction) => { + let window = self.windows.get_mut(&id).unwrap(); + if let Some(pane) = window.focus { + if let Some(adjacent) = + window.panes.adjacent(&pane, direction) + { + window.focus = Some(adjacent); + } + } + } + WindowMessage::Clicked(pane) => { + let window = self.windows.get_mut(&id).unwrap(); + window.focus = Some(pane); + } + WindowMessage::CloseWindow => { + let _ = self.windows.remove(&id); + return window::close(id); + } + WindowMessage::Resized(pane_grid::ResizeEvent { split, ratio }) => { + let window = self.windows.get_mut(&id).unwrap(); + window.panes.resize(&split, ratio); + } + WindowMessage::SelectedWindow(pane, selected) => { + let window = self.windows.get_mut(&id).unwrap(); + let (mut pane, _) = window.panes.close(&pane).unwrap(); + pane.is_moving = false; + + if let Some(window) = self.windows.get_mut(&selected.0) { + let (&first_pane, _) = window.panes.iter().next().unwrap(); + let result = + window.panes.split(pane.axis, &first_pane, pane); + + if let Some((pane, _)) = result { + window.focus = Some(pane); + } + } + } + WindowMessage::ToggleMoving(pane) => { + let window = self.windows.get_mut(&id).unwrap(); + if let Some(pane) = window.panes.get_mut(&pane) { + pane.is_moving = !pane.is_moving; + } + } + WindowMessage::TitleChanged(title) => { + let window = self.windows.get_mut(&id).unwrap(); + window.title = title; + } + WindowMessage::PopOut(pane) => { + let window = self.windows.get_mut(&id).unwrap(); + if let Some((popped, sibling)) = window.panes.close(&pane) { + window.focus = Some(sibling); + + let (panes, _) = pane_grid::State::new(popped); + let window = Window { + panes, + focus: None, + title: format!("New window ({})", self.windows.len()), + scale: 1.0 + (self.windows.len() as f64 / 10.0), + }; + + let window_id = window::Id::new(self.windows.len()); + self.windows.insert(window_id, window); + return window::spawn(window_id, Default::default()); + } + } + WindowMessage::Dragged(pane_grid::DragEvent::Dropped { + pane, + target, + }) => { + let window = self.windows.get_mut(&id).unwrap(); + window.panes.swap(&pane, &target); + } + // WindowMessage::Dragged(pane_grid::DragEvent::Picked { pane }) => { + // println!("Picked {pane:?}"); + // } + WindowMessage::Dragged(_) => {} + WindowMessage::TogglePin(pane) => { + let window = self.windows.get_mut(&id).unwrap(); + if let Some(Pane { is_pinned, .. }) = + window.panes.get_mut(&pane) + { + *is_pinned = !*is_pinned; + } + } + WindowMessage::Close(pane) => { + let window = self.windows.get_mut(&id).unwrap(); + if let Some((_, sibling)) = window.panes.close(&pane) { + window.focus = Some(sibling); + } + } + WindowMessage::CloseFocused => { + let window = self.windows.get_mut(&id).unwrap(); + if let Some(pane) = window.focus { + if let Some(Pane { is_pinned, .. }) = + window.panes.get(&pane) + { + if !is_pinned { + if let Some((_, sibling)) = + window.panes.close(&pane) + { + window.focus = Some(sibling); + } + } + } + } + } + } + + Command::none() + } + + fn subscription(&self) -> Subscription<Message> { + subscription::events_with(|event, status| { + if let event::Status::Captured = status { + return None; + } + + match event { + Event::Keyboard(keyboard::Event::KeyPressed { + modifiers, + key_code, + }) if modifiers.command() => { + handle_hotkey(key_code).map(|message| { + Message::Window(window::Id::new(0usize), message) + }) + } // TODO(derezzedex) + _ => None, + } + }) + } + + fn view(&self, window_id: window::Id) -> Element<Message> { + if let Some(window) = self.windows.get(&window_id) { + let focus = window.focus; + let total_panes = window.panes.len(); + + let window_controls = row![ + text_input( + "Window title", + &window.title, + WindowMessage::TitleChanged, + ), + button(text("Close")) + .on_press(WindowMessage::CloseWindow) + .style(theme::Button::Destructive), + ] + .spacing(5) + .align_items(Alignment::Center); + + let pane_grid = PaneGrid::new(&window.panes, |id, pane, _| { + let is_focused = focus == Some(id); + + let pin_button = button( + text(if pane.is_pinned { "Unpin" } else { "Pin" }).size(14), + ) + .on_press(WindowMessage::TogglePin(id)) + .padding(3); + + let title = row![ + pin_button, + "Pane", + text(pane.id.to_string()).style(if is_focused { + PANE_ID_COLOR_FOCUSED + } else { + PANE_ID_COLOR_UNFOCUSED + }), + ] + .spacing(5); + + let title_bar = pane_grid::TitleBar::new(title) + .controls(view_controls( + id, + total_panes, + pane.is_pinned, + pane.is_moving, + &window.title, + window_id, + &self.windows, + )) + .padding(10) + .style(if is_focused { + style::title_bar_focused + } else { + style::title_bar_active + }); + + pane_grid::Content::new(responsive(move |size| { + view_content( + id, + pane.scrollable_id.clone(), + total_panes, + pane.is_pinned, + size, + ) + })) + .title_bar(title_bar) + .style(if is_focused { + style::pane_focused + } else { + style::pane_active + }) + }) + .width(Length::Fill) + .height(Length::Fill) + .spacing(10) + .on_click(WindowMessage::Clicked) + .on_drag(WindowMessage::Dragged) + .on_resize(10, WindowMessage::Resized); + + let content: Element<_> = column![window_controls, pane_grid] + .width(Length::Fill) + .height(Length::Fill) + .padding(10) + .into(); + + return content + .map(move |message| Message::Window(window_id, message)); + } + + container(text("This shouldn't be possible!").size(20)) + .center_x() + .center_y() + .into() + } + + fn close_requested(&self, window: window::Id) -> Self::Message { + Message::Window(window, WindowMessage::CloseWindow) + } + + fn scale_factor(&self, window: Id) -> f64 { + self.windows.get(&window).map(|w| w.scale).unwrap_or(1.0) + } +} + +const PANE_ID_COLOR_UNFOCUSED: Color = Color::from_rgb( + 0xFF as f32 / 255.0, + 0xC7 as f32 / 255.0, + 0xC7 as f32 / 255.0, +); +const PANE_ID_COLOR_FOCUSED: Color = Color::from_rgb( + 0xFF as f32 / 255.0, + 0x47 as f32 / 255.0, + 0x47 as f32 / 255.0, +); + +fn handle_hotkey(key_code: keyboard::KeyCode) -> Option<WindowMessage> { + use keyboard::KeyCode; + use pane_grid::{Axis, Direction}; + + let direction = match key_code { + KeyCode::Up => Some(Direction::Up), + KeyCode::Down => Some(Direction::Down), + KeyCode::Left => Some(Direction::Left), + KeyCode::Right => Some(Direction::Right), + _ => None, + }; + + match key_code { + KeyCode::V => Some(WindowMessage::SplitFocused(Axis::Vertical)), + KeyCode::H => Some(WindowMessage::SplitFocused(Axis::Horizontal)), + KeyCode::W => Some(WindowMessage::CloseFocused), + _ => direction.map(WindowMessage::FocusAdjacent), + } +} + +#[derive(Debug, Clone)] +struct SelectableWindow(window::Id, String); + +impl PartialEq for SelectableWindow { + fn eq(&self, other: &Self) -> bool { + self.0 == other.0 + } +} + +impl Eq for SelectableWindow {} + +impl std::fmt::Display for SelectableWindow { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + self.1.fmt(f) + } +} + +#[derive(Debug)] +struct Pane { + id: usize, + pub scrollable_id: scrollable::Id, + pub axis: pane_grid::Axis, + pub is_pinned: bool, + pub is_moving: bool, + pub snapped: bool, +} + +impl Pane { + fn new(id: usize, axis: pane_grid::Axis) -> Self { + Self { + id, + scrollable_id: scrollable::Id::unique(), + axis, + is_pinned: false, + is_moving: false, + snapped: false, + } + } +} + +fn view_content<'a>( + pane: pane_grid::Pane, + scrollable_id: scrollable::Id, + total_panes: usize, + is_pinned: bool, + size: Size, +) -> Element<'a, WindowMessage> { + let button = |label, message| { + button( + text(label) + .width(Length::Fill) + .horizontal_alignment(alignment::Horizontal::Center) + .size(16), + ) + .width(Length::Fill) + .padding(8) + .on_press(message) + }; + + let mut controls = column![ + button( + "Split horizontally", + WindowMessage::Split(pane_grid::Axis::Horizontal, pane), + ), + button( + "Split vertically", + WindowMessage::Split(pane_grid::Axis::Vertical, pane), + ), + button("Snap", WindowMessage::SnapToggle,) + ] + .spacing(5) + .max_width(150); + + if total_panes > 1 && !is_pinned { + controls = controls.push( + button("Close", WindowMessage::Close(pane)) + .style(theme::Button::Destructive), + ); + } + + let content = column![ + text(format!("{}x{}", size.width, size.height)).size(24), + controls, + ] + .width(Length::Fill) + .height(800) + .spacing(10) + .align_items(Alignment::Center); + + container( + scrollable(content) + .height(Length::Fill) + .vertical_scroll(Properties::new()) + .id(scrollable_id), + ) + .width(Length::Fill) + .height(Length::Fill) + .padding(5) + .center_y() + .into() +} + +fn view_controls<'a>( + pane: pane_grid::Pane, + total_panes: usize, + is_pinned: bool, + is_moving: bool, + window_title: &'a str, + window_id: window::Id, + windows: &HashMap<window::Id, Window>, +) -> Element<'a, WindowMessage> { + let window_selector = { + let options: Vec<_> = windows + .iter() + .map(|(id, window)| SelectableWindow(*id, window.title.clone())) + .collect(); + pick_list( + options, + Some(SelectableWindow(window_id, window_title.to_string())), + move |window| WindowMessage::SelectedWindow(pane, window), + ) + }; + + let mut move_to = button(text("Move to").size(14)).padding(3); + + let mut pop_out = button(text("Pop Out").size(14)).padding(3); + + let mut close = button(text("Close").size(14)) + .style(theme::Button::Destructive) + .padding(3); + + if total_panes > 1 && !is_pinned { + close = close.on_press(WindowMessage::Close(pane)); + pop_out = pop_out.on_press(WindowMessage::PopOut(pane)); + } + + if windows.len() > 1 && total_panes > 1 && !is_pinned { + move_to = move_to.on_press(WindowMessage::ToggleMoving(pane)); + } + + let mut content = row![].spacing(10); + if is_moving { + content = content.push(pop_out).push(window_selector).push(close); + } else { + content = content.push(pop_out).push(move_to).push(close); + } + + content.into() +} + +mod style { + use iced::widget::container; + use iced::Theme; + + pub fn title_bar_active(theme: &Theme) -> container::Appearance { + let palette = theme.extended_palette(); + + container::Appearance { + text_color: Some(palette.background.strong.text), + background: Some(palette.background.strong.color.into()), + ..Default::default() + } + } + + pub fn title_bar_focused(theme: &Theme) -> container::Appearance { + let palette = theme.extended_palette(); + + container::Appearance { + text_color: Some(palette.primary.strong.text), + background: Some(palette.primary.strong.color.into()), + ..Default::default() + } + } + + pub fn pane_active(theme: &Theme) -> container::Appearance { + let palette = theme.extended_palette(); + + container::Appearance { + background: Some(palette.background.weak.color.into()), + border_width: 2.0, + border_color: palette.background.strong.color, + ..Default::default() + } + } + + pub fn pane_focused(theme: &Theme) -> container::Appearance { + let palette = theme.extended_palette(); + + container::Appearance { + background: Some(palette.background.weak.color.into()), + border_width: 2.0, + border_color: palette.primary.strong.color, + ..Default::default() + } + } +} diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index 9a4ee754..eb461bb0 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -89,7 +89,7 @@ impl Application for SolarSystem { } fn subscription(&self) -> Subscription<Message> { - window::frames().map(Message::Tick) + window::frames().map(|frame| Message::Tick(frame.at)) } } diff --git a/glutin/Cargo.toml b/glutin/Cargo.toml index 10d3778b..01dd3748 100644 --- a/glutin/Cargo.toml +++ b/glutin/Cargo.toml @@ -11,17 +11,19 @@ keywords = ["gui", "ui", "graphics", "interface", "widgets"] categories = ["gui"] [features] -trace = ["iced_winit/trace"] +trace = ["iced_winit/trace", "tracing"] debug = ["iced_winit/debug"] system = ["iced_winit/system"] +multi_window = ["iced_winit/multi_window"] + +[dependencies.raw-window-handle] +version = "0.5.0" [dependencies] log = "0.4" [dependencies.glutin] -version = "0.29" -git = "https://github.com/iced-rs/glutin" -rev = "da8d291486b4c9bec12487a46c119c4b1d386abf" +version = "0.30" [dependencies.iced_native] version = "0.9" diff --git a/glutin/src/application.rs b/glutin/src/application.rs index b7bf21c3..71f44dac 100644 --- a/glutin/src/application.rs +++ b/glutin/src/application.rs @@ -13,14 +13,33 @@ use iced_winit::futures::channel::mpsc; use iced_winit::renderer; use iced_winit::time::Instant; use iced_winit::user_interface; +use iced_winit::winit; use iced_winit::{Clipboard, Command, Debug, Event, Proxy, Settings}; -use glutin::window::Window; +use glutin::config::{ + Config, ConfigSurfaceTypes, ConfigTemplateBuilder, GlConfig, +}; +use glutin::context::{ + ContextApi, ContextAttributesBuilder, NotCurrentContext, + NotCurrentGlContextSurfaceAccessor, + PossiblyCurrentContextGlSurfaceAccessor, PossiblyCurrentGlContext, +}; +use glutin::display::{Display, DisplayApiPreference, GlDisplay}; +use glutin::surface::{ + GlSurface, Surface, SurfaceAttributesBuilder, WindowSurface, +}; +use raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle}; + +use std::ffi::CString; use std::mem::ManuallyDrop; +use std::num::NonZeroU32; -#[cfg(feature = "tracing")] +#[cfg(feature = "trace")] use tracing::{info_span, instrument::Instrument}; +#[allow(unsafe_code)] +const ONE: NonZeroU32 = unsafe { NonZeroU32::new_unchecked(1) }; + /// Runs an [`Application`] with an executor, compositor, and the provided /// settings. pub fn run<A, E, C>( @@ -35,9 +54,8 @@ where { use futures::task; use futures::Future; - use glutin::event_loop::EventLoopBuilder; - use glutin::platform::run_return::EventLoopExtRunReturn; - use glutin::ContextBuilder; + use winit::event_loop::EventLoopBuilder; + use winit::platform::run_return::EventLoopExtRunReturn; #[cfg(feature = "trace")] let _guard = iced_winit::Profiler::init(); @@ -45,7 +63,7 @@ where let mut debug = Debug::new(); debug.startup_started(); - #[cfg(feature = "tracing")] + #[cfg(feature = "trace")] let _ = info_span!("Application::Glutin", "RUN").entered(); let mut event_loop = EventLoopBuilder::with_user_event().build(); @@ -64,74 +82,205 @@ where runtime.enter(|| A::new(flags)) }; - let context = { - let builder = settings.window.into_builder( - &application.title(), - event_loop.primary_monitor(), - settings.id, - ); + let builder = settings.window.into_builder( + &application.title(), + event_loop.primary_monitor(), + settings.id, + ); - log::info!("Window builder: {:#?}", builder); + log::info!("Window builder: {:#?}", builder); - let opengl_builder = ContextBuilder::new() - .with_vsync(true) - .with_multisampling(C::sample_count(&compositor_settings) as u16); + #[allow(unsafe_code)] + let (display, window, surface, context) = unsafe { + struct Configuration(Config); + use std::fmt; + impl fmt::Debug for Configuration { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let config = &self.0; + + f.debug_struct("Configuration") + .field("raw", &config) + .field("samples", &config.num_samples()) + .field("buffer_type", &config.color_buffer_type()) + .field("surface_type", &config.config_surface_types()) + .field("depth", &config.depth_size()) + .field("alpha", &config.alpha_size()) + .field("stencil", &config.stencil_size()) + .field("float_pixels", &config.float_pixels()) + .field("srgb", &config.srgb_capable()) + .field("api", &config.api()) + .finish() + } + } - let opengles_builder = opengl_builder.clone().with_gl( - glutin::GlRequest::Specific(glutin::Api::OpenGlEs, (2, 0)), - ); + impl AsRef<Config> for Configuration { + fn as_ref(&self) -> &Config { + &self.0 + } + } + + let display_handle = event_loop.raw_display_handle(); + + #[cfg(all( + any(windows, target_os = "macos"), + not(target_arch = "wasm32") + ))] + let (window, window_handle) = { + let window = builder + .build(&event_loop) + .map_err(Error::WindowCreationFailed)?; - let (first_builder, second_builder) = if settings.try_opengles_first { - (opengles_builder, opengl_builder) - } else { - (opengl_builder, opengles_builder) + let handle = window.raw_window_handle(); + + (window, handle) + }; + + #[cfg(target_arch = "wasm32")] + let preference = Err(Error::GraphicsCreationFailed( + iced_graphics::Error::BackendError(format!( + "target not supported by backend" + )), + ))?; + + #[cfg(all(windows, not(target_arch = "wasm32")))] + let preference = DisplayApiPreference::WglThenEgl(Some(window_handle)); + + #[cfg(all(target_os = "macos", not(target_arch = "wasm32")))] + let preference = DisplayApiPreference::Cgl; + + #[cfg(all( + unix, + not(target_os = "macos"), + not(target_arch = "wasm32") + ))] + let preference = DisplayApiPreference::GlxThenEgl(Box::new( + winit::platform::unix::register_xlib_error_hook, + )); + + let display = + Display::new(display_handle, preference).map_err(|error| { + Error::GraphicsCreationFailed( + iced_graphics::Error::BackendError(format!( + "failed to create display: {error}" + )), + ) + })?; + + log::debug!("Display: {}", display.version_string()); + + let samples = C::sample_count(&compositor_settings) as u8; + let mut template = ConfigTemplateBuilder::new() + .with_surface_type(ConfigSurfaceTypes::WINDOW); + + if samples != 0 { + template = template.with_multisampling(samples); + } + + #[cfg(all(windows, not(target_arch = "wasm32")))] + let template = template.compatible_with_native_window(window_handle); + + log::debug!("Searching for display configurations"); + let configuration = display + .find_configs(template.build()) + .map_err(|_| { + Error::GraphicsCreationFailed( + iced_graphics::Error::NoAvailablePixelFormat, + ) + })? + .map(Configuration) + .inspect(|config| { + log::trace!("{config:#?}"); + }) + .min_by_key(|config| { + config.as_ref().num_samples().saturating_sub(samples) + }) + .ok_or(Error::GraphicsCreationFailed( + iced_graphics::Error::NoAvailablePixelFormat, + ))?; + + log::debug!("Selected: {configuration:#?}"); + + #[cfg(all( + unix, + not(target_os = "macos"), + not(target_arch = "wasm32") + ))] + let (window, window_handle) = { + use glutin::platform::x11::X11GlConfigExt; + let builder = + if let Some(visual) = configuration.as_ref().x11_visual() { + use winit::platform::unix::WindowBuilderExtUnix; + builder.with_x11_visual(visual.into_raw()) + } else { + builder + }; + + let window = builder + .build(&event_loop) + .map_err(Error::WindowCreationFailed)?; + + let handle = window.raw_window_handle(); + + (window, handle) }; - log::info!("Trying first builder: {:#?}", first_builder); + let attributes = + ContextAttributesBuilder::new().build(Some(window_handle)); + let fallback_attributes = ContextAttributesBuilder::new() + .with_context_api(ContextApi::Gles(None)) + .build(Some(window_handle)); - let context = first_builder - .build_windowed(builder.clone(), &event_loop) + let context = display + .create_context(configuration.as_ref(), &attributes) .or_else(|_| { - log::info!("Trying second builder: {:#?}", second_builder); - second_builder.build_windowed(builder, &event_loop) + display.create_context( + configuration.as_ref(), + &fallback_attributes, + ) }) .map_err(|error| { - use glutin::CreationError; - use iced_graphics::Error as ContextError; + Error::GraphicsCreationFailed( + iced_graphics::Error::BackendError(format!( + "failed to create context: {error}" + )), + ) + })?; - match error { - CreationError::Window(error) => { - Error::WindowCreationFailed(error) - } - CreationError::OpenGlVersionNotSupported => { - Error::GraphicsCreationFailed( - ContextError::VersionNotSupported, - ) - } - CreationError::NoAvailablePixelFormat => { - Error::GraphicsCreationFailed( - ContextError::NoAvailablePixelFormat, - ) - } - error => Error::GraphicsCreationFailed( - ContextError::BackendError(error.to_string()), - ), - } + let surface = gl_surface(&display, configuration.as_ref(), &window) + .map_err(|error| { + Error::GraphicsCreationFailed( + iced_graphics::Error::BackendError(format!( + "failed to create surface: {error}" + )), + ) })?; - #[allow(unsafe_code)] - unsafe { - context.make_current().expect("Make OpenGL context current") + let context = { + context + .make_current(&surface) + .expect("make context current") + }; + + if let Err(error) = surface.set_swap_interval( + &context, + glutin::surface::SwapInterval::Wait(ONE), + ) { + log::error!("set swap interval failed: {}", error); } + + (display, window, surface, context) }; #[allow(unsafe_code)] let (compositor, renderer) = unsafe { C::new(compositor_settings, |address| { - context.get_proc_address(address) + let address = CString::new(address).expect("address error"); + display.get_proc_address(address.as_c_str()) })? }; + let context = { context.make_not_current().expect("make context current") }; + let (mut event_sender, event_receiver) = mpsc::unbounded(); let (control_sender, mut control_receiver) = mpsc::unbounded(); @@ -145,12 +294,14 @@ where debug, event_receiver, control_sender, + window, + surface, context, init_command, settings.exit_on_close_request, ); - #[cfg(feature = "tracing")] + #[cfg(feature = "trace")] let run_instance = run_instance.instrument(info_span!("Application", "LOOP")); @@ -160,22 +311,22 @@ where let mut context = task::Context::from_waker(task::noop_waker_ref()); let _ = event_loop.run_return(move |event, _, control_flow| { - use glutin::event_loop::ControlFlow; + use winit::event_loop::ControlFlow; if let ControlFlow::ExitWithCode(_) = control_flow { return; } let event = match event { - glutin::event::Event::WindowEvent { + winit::event::Event::WindowEvent { event: - glutin::event::WindowEvent::ScaleFactorChanged { + winit::event::WindowEvent::ScaleFactorChanged { new_inner_size, .. }, window_id, - } => Some(glutin::event::Event::WindowEvent { - event: glutin::event::WindowEvent::Resized(*new_inner_size), + } => Some(winit::event::Event::WindowEvent { + event: winit::event::WindowEvent::Resized(*new_inner_size), window_id, }), _ => event.to_static(), @@ -207,13 +358,13 @@ async fn run_instance<A, E, C>( mut compositor: C, mut renderer: A::Renderer, mut runtime: Runtime<E, Proxy<A::Message>, A::Message>, - mut proxy: glutin::event_loop::EventLoopProxy<A::Message>, + mut proxy: winit::event_loop::EventLoopProxy<A::Message>, mut debug: Debug, - mut event_receiver: mpsc::UnboundedReceiver< - glutin::event::Event<'_, A::Message>, - >, - mut control_sender: mpsc::UnboundedSender<glutin::event_loop::ControlFlow>, - mut context: glutin::ContextWrapper<glutin::PossiblyCurrent, Window>, + mut event_receiver: mpsc::UnboundedReceiver<winit::event::Event<'_, A::Message>, >, + mut control_sender: mpsc::UnboundedSender<winit::event_loop::ControlFlow>, + window: winit::window::Window, + surface: Surface<WindowSurface>, + context: NotCurrentContext, init_command: Command<A::Message>, exit_on_close_request: bool, ) where @@ -222,13 +373,19 @@ async fn run_instance<A, E, C>( C: window::GLCompositor<Renderer = A::Renderer> + 'static, <A::Renderer as iced_native::Renderer>::Theme: StyleSheet, { - use glutin::event; - use glutin::event_loop::ControlFlow; use iced_winit::futures::stream::StreamExt; + use winit::event_loop::ControlFlow; + use winit::event; - let mut clipboard = Clipboard::connect(context.window()); + let context = { + context + .make_current(&surface) + .expect("make context current") + }; + + let mut clipboard = Clipboard::connect(&window); let mut cache = user_interface::Cache::default(); - let mut state = application::State::new(&application, context.window()); + let mut state = application::State::new(&application, &window); let mut viewport_version = state.viewport_version(); let mut should_exit = false; @@ -243,7 +400,7 @@ async fn run_instance<A, E, C>( &mut should_exit, &mut proxy, &mut debug, - context.window(), + &window, || compositor.fetch_information(), ); runtime.track(application.subscription()); @@ -316,12 +473,12 @@ async fn run_instance<A, E, C>( &mut proxy, &mut debug, &mut messages, - context.window(), + &window, || compositor.fetch_information(), ); // Update window - state.synchronize(&application, context.window()); + state.synchronize(&application, &window); user_interface = ManuallyDrop::new(application::build_user_interface( @@ -343,6 +500,7 @@ async fn run_instance<A, E, C>( // Then, we can use the `interface_state` here to decide if a redraw // is needed right away, or simply wait until a specific time. let redraw_event = Event::Window( + crate::window::Id::MAIN, crate::window::Event::RedrawRequested(Instant::now()), ); @@ -366,16 +524,15 @@ async fn run_instance<A, E, C>( debug.draw_finished(); if new_mouse_interaction != mouse_interaction { - context.window().set_cursor_icon( - conversion::mouse_interaction(new_mouse_interaction), - ); + window.set_cursor_icon(conversion::mouse_interaction( + new_mouse_interaction, + )); mouse_interaction = new_mouse_interaction; } - context.window().request_redraw(); - runtime - .broadcast((redraw_event, crate::event::Status::Ignored)); + window.request_redraw(); + runtime.broadcast((redraw_event, crate::event::Status::Ignored)); let _ = control_sender.start_send(match interface_state { user_interface::State::Updated { @@ -407,18 +564,15 @@ async fn run_instance<A, E, C>( messages.push(message); } event::Event::RedrawRequested(_) => { - #[cfg(feature = "tracing")] + #[cfg(feature = "trace")] let _ = info_span!("Application", "FRAME").entered(); debug.render_started(); - #[allow(unsafe_code)] - unsafe { - if !context.is_current() { - context = context - .make_current() - .expect("Make OpenGL context current"); - } + if !context.is_current() { + context + .make_current(&surface) + .expect("Make OpenGL context current"); } let current_viewport_version = state.viewport_version(); @@ -446,19 +600,18 @@ async fn run_instance<A, E, C>( debug.draw_finished(); if new_mouse_interaction != mouse_interaction { - context.window().set_cursor_icon( - conversion::mouse_interaction( - new_mouse_interaction, - ), - ); + window.set_cursor_icon(conversion::mouse_interaction( + new_mouse_interaction, + )); mouse_interaction = new_mouse_interaction; } - context.resize(glutin::dpi::PhysicalSize::new( - physical_size.width, - physical_size.height, - )); + surface.resize( + &context, + NonZeroU32::new(physical_size.width).unwrap_or(ONE), + NonZeroU32::new(physical_size.height).unwrap_or(ONE), + ); compositor.resize_viewport(physical_size); @@ -472,7 +625,7 @@ async fn run_instance<A, E, C>( &debug.overlay(), ); - context.swap_buffers().expect("Swap buffers"); + surface.swap_buffers(&context).expect("Swap buffers"); debug.render_finished(); @@ -489,9 +642,10 @@ async fn run_instance<A, E, C>( break; } - state.update(context.window(), &window_event, &mut debug); + state.update(&window, &window_event, &mut debug); if let Some(event) = conversion::window_event( + crate::window::Id::MAIN, &window_event, state.scale_factor(), state.modifiers(), @@ -506,3 +660,23 @@ async fn run_instance<A, E, C>( // Manually drop the user interface drop(ManuallyDrop::into_inner(user_interface)); } + +#[allow(unsafe_code)] +/// Creates a new [`glutin::Surface<WindowSurface>`]. +pub fn gl_surface( + display: &Display, + gl_config: &Config, + window: &winit::window::Window, +) -> Result<Surface<WindowSurface>, glutin::error::Error> { + let (width, height) = window.inner_size().into(); + + let surface_attributes = SurfaceAttributesBuilder::<WindowSurface>::new() + .with_srgb(Some(true)) + .build( + window.raw_window_handle(), + NonZeroU32::new(width).unwrap_or(ONE), + NonZeroU32::new(height).unwrap_or(ONE), + ); + + unsafe { display.create_window_surface(gl_config, &surface_attributes) } +} diff --git a/glutin/src/lib.rs b/glutin/src/lib.rs index 33afd664..45d6cb5b 100644 --- a/glutin/src/lib.rs +++ b/glutin/src/lib.rs @@ -29,5 +29,8 @@ pub use iced_winit::*; pub mod application; +#[cfg(feature = "multi_window")] +pub mod multi_window; + #[doc(no_inline)] pub use application::Application; diff --git a/glutin/src/multi_window.rs b/glutin/src/multi_window.rs new file mode 100644 index 00000000..620d01d8 --- /dev/null +++ b/glutin/src/multi_window.rs @@ -0,0 +1,1060 @@ +//! Create interactive, native cross-platform applications. +use crate::mouse; +use crate::{Error, Executor, Runtime}; + +pub use iced_winit::multi_window::{self, Application, StyleSheet}; + +use iced_winit::conversion; +use iced_winit::futures; +use iced_winit::futures::channel::mpsc; +use iced_winit::renderer; +use iced_winit::user_interface; +use iced_winit::window; +use iced_winit::winit; +use iced_winit::{Clipboard, Command, Debug, Proxy, Settings}; + +use glutin::config::{ + Config, ConfigSurfaceTypes, ConfigTemplateBuilder, GlConfig, +}; +use glutin::context::{ + ContextApi, ContextAttributesBuilder, NotCurrentContext, + NotCurrentGlContextSurfaceAccessor, PossiblyCurrentGlContext, +}; +use glutin::display::{Display, DisplayApiPreference, GlDisplay}; +use glutin::surface::{GlSurface, SwapInterval}; +use raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle}; + +use crate::application::gl_surface; +use iced_winit::multi_window::Event; +use std::collections::HashMap; +use std::ffi::CString; +use std::mem::ManuallyDrop; +use std::num::NonZeroU32; + +use iced_native::widget::operation; +#[cfg(feature = "tracing")] +use tracing::{info_span, instrument::Instrument}; + +#[allow(unsafe_code)] +const ONE: NonZeroU32 = unsafe { NonZeroU32::new_unchecked(1) }; + +/// Runs an [`Application`] with an executor, compositor, and the provided +/// settings. +pub fn run<A, E, C>( + settings: Settings<A::Flags>, + compositor_settings: C::Settings, +) -> Result<(), Error> +where + A: Application + 'static, + E: Executor + 'static, + C: iced_graphics::window::GLCompositor<Renderer = A::Renderer> + 'static, + <A::Renderer as iced_native::Renderer>::Theme: StyleSheet, +{ + use futures::task; + use futures::Future; + use winit::event_loop::EventLoopBuilder; + use winit::platform::run_return::EventLoopExtRunReturn; + + #[cfg(feature = "trace")] + let _guard = iced_winit::Profiler::init(); + + let mut debug = Debug::new(); + debug.startup_started(); + + #[cfg(feature = "tracing")] + let _ = info_span!("Application::Glutin", "RUN").entered(); + + let mut event_loop = EventLoopBuilder::with_user_event().build(); + let proxy = event_loop.create_proxy(); + + let runtime = { + let executor = E::new().map_err(Error::ExecutorCreationFailed)?; + let proxy = Proxy::new(event_loop.create_proxy()); + + Runtime::new(executor, proxy) + }; + + let (application, init_command) = { + let flags = settings.flags; + + runtime.enter(|| A::new(flags)) + }; + + let builder = settings.window.into_builder( + &application.title(window::Id::MAIN), + event_loop.primary_monitor(), + settings.id, + ); + + log::info!("Window builder: {:#?}", builder); + + #[allow(unsafe_code)] + let (display, window, configuration, surface, context) = unsafe { + struct Configuration(Config); + use std::fmt; + impl fmt::Debug for Configuration { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let config = &self.0; + + f.debug_struct("Configuration") + .field("raw", &config) + .field("samples", &config.num_samples()) + .field("buffer_type", &config.color_buffer_type()) + .field("surface_type", &config.config_surface_types()) + .field("depth", &config.depth_size()) + .field("alpha", &config.alpha_size()) + .field("stencil", &config.stencil_size()) + .field("float_pixels", &config.float_pixels()) + .field("srgb", &config.srgb_capable()) + .field("api", &config.api()) + .finish() + } + } + + impl AsRef<Config> for Configuration { + fn as_ref(&self) -> &Config { + &self.0 + } + } + + let display_handle = event_loop.raw_display_handle(); + + #[cfg(all( + any(windows, target_os = "macos"), + not(target_arch = "wasm32") + ))] + let (window, window_handle) = { + let window = builder + .build(&event_loop) + .map_err(Error::WindowCreationFailed)?; + + let handle = window.raw_window_handle(); + + (window, handle) + }; + + #[cfg(target_arch = "wasm32")] + let preference = Err(Error::GraphicsCreationFailed( + iced_graphics::Error::BackendError(format!( + "target not supported by backend" + )), + ))?; + + #[cfg(all(windows, not(target_arch = "wasm32")))] + let preference = DisplayApiPreference::WglThenEgl(Some(window_handle)); + + #[cfg(all(target_os = "macos", not(target_arch = "wasm32")))] + let preference = DisplayApiPreference::Cgl; + + #[cfg(all( + unix, + not(target_os = "macos"), + not(target_arch = "wasm32") + ))] + let preference = DisplayApiPreference::GlxThenEgl(Box::new( + winit::platform::unix::register_xlib_error_hook, + )); + + let display = + Display::new(display_handle, preference).map_err(|error| { + Error::GraphicsCreationFailed( + iced_graphics::Error::BackendError(format!( + "failed to create display: {error}" + )), + ) + })?; + + log::debug!("Display: {}", display.version_string()); + + let samples = C::sample_count(&compositor_settings) as u8; + let mut template = ConfigTemplateBuilder::new() + .with_surface_type(ConfigSurfaceTypes::WINDOW); + + if samples != 0 { + template = template.with_multisampling(samples); + } + + #[cfg(all(windows, not(target_arch = "wasm32")))] + let template = template.compatible_with_native_window(window_handle); + + log::debug!("Searching for display configurations"); + let configuration = display + .find_configs(template.build()) + .map_err(|_| { + Error::GraphicsCreationFailed( + iced_graphics::Error::NoAvailablePixelFormat, + ) + })? + .map(Configuration) + .inspect(|config| { + log::trace!("{config:#?}"); + }) + .min_by_key(|config| { + config.as_ref().num_samples().saturating_sub(samples) + }) + .ok_or(Error::GraphicsCreationFailed( + iced_graphics::Error::NoAvailablePixelFormat, + ))?; + + log::debug!("Selected: {configuration:#?}"); + + #[cfg(all( + unix, + not(target_os = "macos"), + not(target_arch = "wasm32") + ))] + let (window, window_handle) = { + use glutin::platform::x11::X11GlConfigExt; + let builder = + if let Some(visual) = configuration.as_ref().x11_visual() { + use winit::platform::unix::WindowBuilderExtUnix; + builder.with_x11_visual(visual.into_raw()) + } else { + builder + }; + + let window = builder + .build(&event_loop) + .map_err(Error::WindowCreationFailed)?; + + let handle = window.raw_window_handle(); + + (window, handle) + }; + + let attributes = + ContextAttributesBuilder::new().build(Some(window_handle)); + let fallback_attributes = ContextAttributesBuilder::new() + .with_context_api(ContextApi::Gles(None)) + .build(Some(window_handle)); + + let context = display + .create_context(configuration.as_ref(), &attributes) + .or_else(|_| { + display.create_context( + configuration.as_ref(), + &fallback_attributes, + ) + }) + .map_err(|error| { + Error::GraphicsCreationFailed( + iced_graphics::Error::BackendError(format!( + "failed to create context: {error}" + )), + ) + })?; + + let surface = gl_surface(&display, configuration.as_ref(), &window) + .map_err(|error| { + Error::GraphicsCreationFailed( + iced_graphics::Error::BackendError(format!( + "failed to create surface: {error}" + )), + ) + })?; + + (display, window, configuration.0, surface, context) + }; + + let windows: HashMap<window::Id, winit::window::Window> = + HashMap::from([(window::Id::MAIN, window)]); + + // need to make context current before trying to load GL functions + let context = context + .make_current(&surface) + .expect("Make context current."); + + #[allow(unsafe_code)] + let (compositor, renderer) = unsafe { + C::new(compositor_settings, |address| { + let address = CString::new(address).expect("address error"); + display.get_proc_address(address.as_c_str()) + })? + }; + + let context = context.make_not_current().expect("Make not current."); + + let (mut sender, receiver) = mpsc::unbounded(); + + let mut instance = Box::pin({ + let run_instance = run_instance::<A, E, C>( + application, + compositor, + renderer, + runtime, + proxy, + debug, + receiver, + display, + windows, + configuration, + context, + init_command, + settings.exit_on_close_request, + ); + + #[cfg(feature = "tracing")] + let run_instance = + run_instance.instrument(info_span!("Application", "LOOP")); + + run_instance + }); + + let mut context = task::Context::from_waker(task::noop_waker_ref()); + + let _ = event_loop.run_return(move |event, event_loop, control_flow| { + use winit::event_loop::ControlFlow; + + if let ControlFlow::ExitWithCode(_) = control_flow { + return; + } + + let event = match event { + winit::event::Event::WindowEvent { + event: + winit::event::WindowEvent::ScaleFactorChanged { + new_inner_size, + .. + }, + window_id, + } => Some(winit::event::Event::WindowEvent { + event: winit::event::WindowEvent::Resized(*new_inner_size), + window_id, + }), + winit::event::Event::UserEvent(Event::NewWindow { + id, + settings, + title, + }) => { + let window = settings + .into_builder(&title, event_loop.primary_monitor(), None) + .build(event_loop) + .expect("Failed to build window"); + + Some(winit::event::Event::UserEvent(Event::WindowCreated( + id, window, + ))) + } + _ => event.to_static(), + }; + + if let Some(event) = event { + sender.start_send(event).expect("Send event"); + + let poll = instance.as_mut().poll(&mut context); + + *control_flow = match poll { + task::Poll::Pending => ControlFlow::Wait, + task::Poll::Ready(_) => ControlFlow::Exit, + }; + } + }); + + Ok(()) +} + +async fn run_instance<A, E, C>( + mut application: A, + mut compositor: C, + mut renderer: A::Renderer, + mut runtime: Runtime<E, Proxy<Event<A::Message>>, Event<A::Message>>, + mut proxy: winit::event_loop::EventLoopProxy<Event<A::Message>>, + mut debug: Debug, + mut receiver: mpsc::UnboundedReceiver< + winit::event::Event<'_, Event<A::Message>>, + >, + display: Display, + mut windows: HashMap<window::Id, winit::window::Window>, + configuration: Config, + mut context: NotCurrentContext, + init_command: Command<A::Message>, + _exit_on_close_request: bool, +) where + A: Application + 'static, + E: Executor + 'static, + C: iced_graphics::window::GLCompositor<Renderer = A::Renderer> + 'static, + <A::Renderer as iced_native::Renderer>::Theme: StyleSheet, +{ + use iced_winit::futures::stream::StreamExt; + use winit::event; + + let mut clipboard = + Clipboard::connect(windows.values().next().expect("No window found")); + let mut caches = HashMap::new(); + let mut current_context_window = None; + let mut window_ids: HashMap<_, _> = windows + .iter() + .map(|(&id, window)| (window.id(), id)) + .collect(); + let mut states = HashMap::new(); + let mut surfaces = HashMap::new(); + let mut interfaces = ManuallyDrop::new(HashMap::new()); + + for (&id, window) in windows.keys().zip(windows.values()) { + let surface = gl_surface(&display, &configuration, &window) + .expect("Create surface."); + let current_context = + context.make_current(&surface).expect("Make current."); + let state = multi_window::State::new(&application, id, &window); + let physical_size = state.physical_size(); + + surface.resize( + ¤t_context, + NonZeroU32::new(physical_size.width).unwrap_or(ONE), + NonZeroU32::new(physical_size.height).unwrap_or(ONE), + ); + + let user_interface = multi_window::build_user_interface( + &application, + user_interface::Cache::default(), + &mut renderer, + state.logical_size(), + &mut debug, + id, + ); + + context = current_context + .make_not_current() + .expect("Make not current."); + + let _ = states.insert(id, state); + let _ = surfaces.insert(id, surface); + let _ = interfaces.insert(id, user_interface); + } + + run_command( + &application, + &mut caches, + &states, + &mut renderer, + init_command, + &mut runtime, + &mut clipboard, + &mut proxy, + &mut debug, + &windows, + || compositor.fetch_information(), + ); + + runtime.track(application.subscription().map(Event::Application)); + + let mut mouse_interaction = mouse::Interaction::default(); + let mut events = Vec::new(); + let mut messages = Vec::new(); + + debug.startup_finished(); + + 'main: while let Some(event) = receiver.next().await { + match event { + event::Event::MainEventsCleared => { + for id in windows.keys().copied() { + let (filtered, remaining): (Vec<_>, Vec<_>) = + events.iter().cloned().partition( + |(window_id, _event): &( + Option<window::Id>, + iced_native::event::Event, + )| { + *window_id == Some(id) || *window_id == None + }, + ); + + events.retain(|el| remaining.contains(el)); + let filtered: Vec<_> = filtered + .into_iter() + .map(|(_id, event)| event) + .collect(); + + let cursor_position = + states.get(&id).unwrap().cursor_position(); + let window = windows.get(&id).unwrap(); + + if filtered.is_empty() && messages.is_empty() { + continue; + } + + debug.event_processing_started(); + + let (interface_state, statuses) = { + let user_interface = interfaces.get_mut(&id).unwrap(); + user_interface.update( + &filtered, + cursor_position, + &mut renderer, + &mut clipboard, + &mut messages, + ) + }; + + debug.event_processing_finished(); + + for event in filtered.into_iter().zip(statuses.into_iter()) + { + runtime.broadcast(event); + } + + if !messages.is_empty() + || matches!( + interface_state, + user_interface::State::Outdated + ) + { + let user_interfaces: HashMap<_, _> = + ManuallyDrop::into_inner(interfaces) + .drain() + .map(|(id, interface)| { + (id, interface.into_cache()) + }) + .collect(); + + // Update application + update( + &mut application, + &mut caches, + &states, + &mut renderer, + &mut runtime, + &mut clipboard, + &mut proxy, + &mut debug, + &mut messages, + &windows, + || compositor.fetch_information(), + ); + + // Update window + states.get_mut(&id).unwrap().synchronize( + &application, + id, + windows.get(&id).expect("No window found with ID."), + ); + + let should_exit = application.should_exit(); + + interfaces = ManuallyDrop::new(build_user_interfaces( + &application, + &mut renderer, + &mut debug, + &states, + user_interfaces, + )); + + if should_exit { + break 'main; + } + } + + debug.draw_started(); + let new_mouse_interaction = { + let user_interface = interfaces.get_mut(&id).unwrap(); + let state = states.get(&id).unwrap(); + + user_interface.draw( + &mut renderer, + state.theme(), + &renderer::Style { + text_color: state.text_color(), + }, + state.cursor_position(), + ) + }; + debug.draw_finished(); + + if new_mouse_interaction != mouse_interaction { + window.set_cursor_icon(conversion::mouse_interaction( + new_mouse_interaction, + )); + + mouse_interaction = new_mouse_interaction; + } + + window.request_redraw(); + } + } + event::Event::PlatformSpecific(event::PlatformSpecific::MacOS( + event::MacOS::ReceivedUrl(url), + )) => { + use iced_native::event; + events.push(( + None, + iced_native::Event::PlatformSpecific( + event::PlatformSpecific::MacOS( + event::MacOS::ReceivedUrl(url), + ), + ), + )); + } + event::Event::UserEvent(event) => match event { + Event::Application(message) => messages.push(message), + Event::WindowCreated(id, window) => { + let state = + multi_window::State::new(&application, id, &window); + let user_interface = multi_window::build_user_interface( + &application, + user_interface::Cache::default(), + &mut renderer, + state.logical_size(), + &mut debug, + id, + ); + + let surface = gl_surface(&display, &configuration, &window) + .expect("Create surface."); + + let _ = states.insert(id, state); + let _ = interfaces.insert(id, user_interface); + let _ = window_ids.insert(window.id(), id); + let _ = windows.insert(id, window); + let _ = surfaces.insert(id, surface); + } + Event::CloseWindow(id) => { + // TODO(derezzedex): log errors + if let Some(window) = windows.get(&id) { + if window_ids.remove(&window.id()).is_none() { + println!("Failed to remove from `window_ids`!"); + } + } + if states.remove(&id).is_none() { + println!("Failed to remove from `states`!") + } + if interfaces.remove(&id).is_none() { + println!("Failed to remove from `interfaces`!"); + } + if surfaces.remove(&id).is_none() { + println!("Failed to remove from `surfaces`!") + } + if windows.remove(&id).is_none() { + println!("Failed to remove from `windows`!") + } + + if windows.is_empty() { + break 'main; + } + } + Event::NewWindow { .. } => unreachable!(), + }, + event::Event::RedrawRequested(id) => { + #[cfg(feature = "tracing")] + let _ = info_span!("Application", "FRAME").entered(); + + let state = window_ids + .get(&id) + .and_then(|id| states.get_mut(id)) + .unwrap(); + let window = + window_ids.get(&id).and_then(|id| windows.get(id)).unwrap(); + + let surface = window_ids + .get(&id) + .and_then(|id| surfaces.get(id)) + .unwrap(); + + debug.render_started(); + + let current_context = + context.make_current(&surface).expect("Make current."); + + if current_context_window != Some(id) { + current_context_window = Some(id); + } + + if state.viewport_changed() { + let physical_size = state.physical_size(); + let logical_size = state.logical_size(); + + let mut user_interface = window_ids + .get(&id) + .and_then(|id| interfaces.remove(id)) + .unwrap(); + + debug.layout_started(); + user_interface = + user_interface.relayout(logical_size, &mut renderer); + debug.layout_finished(); + + debug.draw_started(); + let new_mouse_interaction = user_interface.draw( + &mut renderer, + state.theme(), + &renderer::Style { + text_color: state.text_color(), + }, + state.cursor_position(), + ); + debug.draw_finished(); + + if new_mouse_interaction != mouse_interaction { + window.set_cursor_icon(conversion::mouse_interaction( + new_mouse_interaction, + )); + + mouse_interaction = new_mouse_interaction; + } + + surface.resize( + ¤t_context, + NonZeroU32::new(physical_size.width).unwrap_or(ONE), + NonZeroU32::new(physical_size.height).unwrap_or(ONE), + ); + + if let Err(_) = surface.set_swap_interval( + ¤t_context, + SwapInterval::Wait(ONE), + ) { + log::error!("Could not set swap interval for surface attached to window id: {:?}", id); + } + + compositor.resize_viewport(physical_size); + + let _ = interfaces + .insert(*window_ids.get(&id).unwrap(), user_interface); + } + + compositor.present( + &mut renderer, + state.viewport(), + state.background_color(), + &debug.overlay(), + ); + + surface + .swap_buffers(¤t_context) + .expect("Swap buffers"); + + context = current_context + .make_not_current() + .expect("Make not current."); + debug.render_finished(); + // TODO: Handle animations! + // Maybe we can use `ControlFlow::WaitUntil` for this. + } + event::Event::WindowEvent { + event: window_event, + window_id, + } => { + // dbg!(window_id); + if let Some(window) = + window_ids.get(&window_id).and_then(|id| windows.get(id)) + { + if let Some(state) = window_ids + .get(&window_id) + .and_then(|id| states.get_mut(id)) + { + if multi_window::requests_exit( + &window_event, + state.modifiers(), + ) { + if let Some(id) = + window_ids.get(&window_id).cloned() + { + let message = application.close_requested(id); + messages.push(message); + } + } + + state.update(window, &window_event, &mut debug); + + if let Some(event) = conversion::window_event( + *window_ids.get(&window_id).unwrap(), + &window_event, + state.scale_factor(), + state.modifiers(), + ) { + events.push(( + window_ids.get(&window_id).cloned(), + event, + )); + } + } else { + log::error!( + "Window state not found for id: {:?}", + window_id + ); + } + } else { + log::error!("Window not found for id: {:?}", window_id); + } + } + _ => {} + } + } + + // Manually drop the user interface + // drop(ManuallyDrop::into_inner(user_interface)); +} + +/// Updates an [`Application`] by feeding it the provided messages, spawning any +/// resulting [`Command`], and tracking its [`Subscription`]. +pub fn update<A: Application, E: Executor>( + application: &mut A, + caches: &mut HashMap<window::Id, user_interface::Cache>, + states: &HashMap<window::Id, multi_window::State<A>>, + renderer: &mut A::Renderer, + runtime: &mut Runtime<E, Proxy<Event<A::Message>>, Event<A::Message>>, + clipboard: &mut Clipboard, + proxy: &mut winit::event_loop::EventLoopProxy<Event<A::Message>>, + debug: &mut Debug, + messages: &mut Vec<A::Message>, + windows: &HashMap<window::Id, winit::window::Window>, + graphics_info: impl FnOnce() -> iced_graphics::compositor::Information + Copy, +) where + A: Application + 'static, + <A::Renderer as crate::Renderer>::Theme: StyleSheet, +{ + for message in messages.drain(..) { + debug.log_message(&message); + + debug.update_started(); + let command = runtime.enter(|| application.update(message)); + debug.update_finished(); + + run_command( + application, + caches, + &states, + renderer, + command, + runtime, + clipboard, + proxy, + debug, + windows, + graphics_info, + ); + } + + let subscription = application.subscription().map(Event::Application); + runtime.track(subscription); +} + +/// Runs the actions of a [`Command`]. +pub fn run_command<A, E>( + application: &A, + caches: &mut HashMap<window::Id, user_interface::Cache>, + states: &HashMap<window::Id, multi_window::State<A>>, + renderer: &mut A::Renderer, + command: Command<A::Message>, + runtime: &mut Runtime<E, Proxy<Event<A::Message>>, Event<A::Message>>, + clipboard: &mut Clipboard, + proxy: &mut winit::event_loop::EventLoopProxy<Event<A::Message>>, + debug: &mut Debug, + windows: &HashMap<window::Id, winit::window::Window>, + _graphics_info: impl FnOnce() -> iced_graphics::compositor::Information + Copy, +) where + A: Application + 'static, + E: Executor, + <A::Renderer as crate::Renderer>::Theme: StyleSheet, +{ + use iced_native::command; + use iced_native::system; + use iced_native::window; + use iced_winit::clipboard; + use iced_winit::futures::FutureExt; + + for action in command.actions() { + match action { + command::Action::Future(future) => { + runtime.spawn(Box::pin(future.map(Event::Application))); + } + command::Action::Clipboard(action) => match action { + clipboard::Action::Read(tag) => { + let message = tag(clipboard.read()); + + proxy + .send_event(Event::Application(message)) + .expect("Send message to event loop"); + } + clipboard::Action::Write(contents) => { + clipboard.write(contents); + } + }, + command::Action::Window(id, action) => match action { + window::Action::Spawn { settings } => { + proxy + .send_event(Event::NewWindow { + id, + settings: settings.into(), + title: application.title(id), + }) + .expect("Send message to event loop"); + } + window::Action::Close => { + proxy + .send_event(Event::CloseWindow(id)) + .expect("Send message to event loop"); + } + window::Action::Drag => { + let window = windows.get(&id).expect("No window found"); + let _res = window.drag_window(); + } + window::Action::Resize { width, height } => { + let window = windows.get(&id).expect("No window found"); + window.set_inner_size(winit::dpi::LogicalSize { + width, + height, + }); + } + window::Action::Move { x, y } => { + let window = windows.get(&id).expect("No window found"); + window.set_outer_position(winit::dpi::LogicalPosition { + x, + y, + }); + } + window::Action::ChangeMode(mode) => { + let window = windows.get(&id).expect("No window found"); + window.set_visible(conversion::visible(mode)); + window.set_fullscreen(conversion::fullscreen( + window.primary_monitor(), + mode, + )); + } + window::Action::FetchMode(tag) => { + let window = windows.get(&id).expect("No window found"); + let mode = if window.is_visible().unwrap_or(true) { + conversion::mode(window.fullscreen()) + } else { + window::Mode::Hidden + }; + + proxy + .send_event(Event::Application(tag(mode))) + .expect("Send message to event loop"); + } + window::Action::Maximize(value) => { + let window = windows.get(&id).expect("No window found!"); + window.set_maximized(value); + } + window::Action::Minimize(value) => { + let window = windows.get(&id).expect("No window found!"); + window.set_minimized(value); + } + window::Action::ToggleMaximize => { + let window = windows.get(&id).expect("No window found!"); + window.set_maximized(!window.is_maximized()); + } + window::Action::ToggleDecorations => { + let window = windows.get(&id).expect("No window found!"); + window.set_decorations(!window.is_decorated()); + } + window::Action::RequestUserAttention(attention_type) => { + let window = windows.get(&id).expect("No window found!"); + window.request_user_attention( + attention_type.map(conversion::user_attention), + ); + } + window::Action::GainFocus => { + let window = windows.get(&id).expect("No window found!"); + window.focus_window(); + } + window::Action::ChangeAlwaysOnTop(on_top) => { + let window = windows.get(&id).expect("No window found!"); + window.set_always_on_top(on_top); + } + window::Action::FetchId(tag) => { + let window = windows.get(&id).expect("No window found!"); + + proxy + .send_event(Event::Application(tag(window.id().into()))) + .expect("Send message to event loop.") + } + }, + command::Action::System(action) => match action { + system::Action::QueryInformation(_tag) => { + #[cfg(feature = "iced_winit/system")] + { + let graphics_info = _graphics_info(); + let proxy = proxy.clone(); + + let _ = std::thread::spawn(move || { + let information = + crate::system::information(graphics_info); + + let message = _tag(information); + + proxy + .send_event(Event::Application(message)) + .expect("Send message to event loop") + }); + } + } + }, + command::Action::Widget(action) => { + let mut current_caches = std::mem::take(caches); + let mut current_operation = Some(action.into_operation()); + + let mut user_interfaces = multi_window::build_user_interfaces( + application, + renderer, + debug, + states, + current_caches, + ); + + while let Some(mut operation) = current_operation.take() { + for user_interface in user_interfaces.values_mut() { + user_interface.operate(renderer, operation.as_mut()); + + match operation.finish() { + operation::Outcome::None => {} + operation::Outcome::Some(message) => { + proxy + .send_event(Event::Application(message)) + .expect("Send message to event loop"); + } + operation::Outcome::Chain(next) => { + current_operation = Some(next); + } + } + } + } + + let user_interfaces: HashMap<_, _> = user_interfaces + .drain() + .map(|(id, interface)| (id, interface.into_cache())) + .collect(); + + current_caches = user_interfaces; + *caches = current_caches; + } + } + } +} + +/// TODO(derezzedex) +pub fn build_user_interfaces<'a, A>( + application: &'a A, + renderer: &mut A::Renderer, + debug: &mut Debug, + states: &HashMap<window::Id, multi_window::State<A>>, + mut user_interfaces: HashMap<window::Id, user_interface::Cache>, +) -> HashMap< + window::Id, + iced_winit::UserInterface< + 'a, + <A as Application>::Message, + <A as Application>::Renderer, + >, +> +where + A: Application + 'static, + <A::Renderer as crate::Renderer>::Theme: StyleSheet, +{ + let mut interfaces = HashMap::new(); + + for (id, pure_state) in user_interfaces.drain() { + let state = &states.get(&id).unwrap(); + + let user_interface = multi_window::build_user_interface( + application, + pure_state, + renderer, + state.logical_size(), + debug, + id, + ); + + let _ = interfaces.insert(id, user_interface); + } + + interfaces +} diff --git a/graphics/src/window/gl_compositor.rs b/graphics/src/window/gl_compositor.rs index a45a7ca1..e6ae2364 100644 --- a/graphics/src/window/gl_compositor.rs +++ b/graphics/src/window/gl_compositor.rs @@ -30,7 +30,7 @@ pub trait GLCompositor: Sized { /// The settings of the [`GLCompositor`]. /// /// It's up to you to decide the configuration supported by your renderer! - type Settings: Default; + type Settings: Default + Clone; /// Creates a new [`GLCompositor`] and [`Renderer`] with the given /// [`Settings`] and an OpenGL address loader function. diff --git a/native/src/command/action.rs b/native/src/command/action.rs index a51b8c21..dcb79672 100644 --- a/native/src/command/action.rs +++ b/native/src/command/action.rs @@ -20,7 +20,7 @@ pub enum Action<T> { Clipboard(clipboard::Action<T>), /// Run a window action. - Window(window::Action<T>), + Window(window::Id, window::Action<T>), /// Run a system action. System(system::Action<T>), @@ -46,7 +46,7 @@ impl<T> Action<T> { match self { Self::Future(future) => Action::Future(Box::pin(future.map(f))), Self::Clipboard(action) => Action::Clipboard(action.map(f)), - Self::Window(window) => Action::Window(window.map(f)), + Self::Window(id, window) => Action::Window(id, window.map(f)), Self::System(system) => Action::System(system.map(f)), Self::Widget(widget) => Action::Widget(widget.map(f)), } @@ -60,7 +60,9 @@ impl<T> fmt::Debug for Action<T> { Self::Clipboard(action) => { write!(f, "Action::Clipboard({action:?})") } - Self::Window(action) => write!(f, "Action::Window({action:?})"), + Self::Window(id, action) => { + write!(f, "Action::Window({id:?}, {action:?})") + } Self::System(action) => write!(f, "Action::System({action:?})"), Self::Widget(_action) => write!(f, "Action::Widget"), } diff --git a/native/src/event.rs b/native/src/event.rs index bcfaf891..eb826399 100644 --- a/native/src/event.rs +++ b/native/src/event.rs @@ -19,7 +19,7 @@ pub enum Event { Mouse(mouse::Event), /// A window event - Window(window::Event), + Window(window::Id, window::Event), /// A touch event Touch(touch::Event), diff --git a/native/src/subscription.rs b/native/src/subscription.rs index b505f3cc..859abe8e 100644 --- a/native/src/subscription.rs +++ b/native/src/subscription.rs @@ -70,7 +70,7 @@ where events.filter_map(move |(event, status)| { future::ready(match event { - Event::Window(window::Event::RedrawRequested(_)) => None, + Event::Window(_, window::Event::RedrawRequested(_)) => None, _ => f(event, status), }) }) diff --git a/native/src/widget/text_input.rs b/native/src/widget/text_input.rs index ee0473ea..00e871e7 100644 --- a/native/src/widget/text_input.rs +++ b/native/src/widget/text_input.rs @@ -778,7 +778,7 @@ where state.keyboard_modifiers = modifiers; } - Event::Window(window::Event::RedrawRequested(now)) => { + Event::Window(_, window::Event::RedrawRequested(now)) => { let state = state(); if let Some(focus) = &mut state.is_focused { diff --git a/native/src/widget/tree.rs b/native/src/widget/tree.rs index 0af40c33..da269632 100644 --- a/native/src/widget/tree.rs +++ b/native/src/widget/tree.rs @@ -67,7 +67,7 @@ impl Tree { } } - /// Reconciliates the children of the tree with the provided list of widgets. + /// Reconciles the children of the tree with the provided list of widgets. pub fn diff_children<'a, Message, Renderer>( &mut self, new_children: &[impl Borrow<dyn Widget<Message, Renderer> + 'a>], diff --git a/native/src/window.rs b/native/src/window.rs index a5cdc8ce..a8f8b10f 100644 --- a/native/src/window.rs +++ b/native/src/window.rs @@ -1,14 +1,22 @@ //! Build window-based GUI applications. mod action; mod event; +mod icon; +mod id; mod mode; +mod position; mod redraw_request; +mod settings; mod user_attention; pub use action::Action; pub use event::Event; +pub use icon::Icon; +pub use id::Id; pub use mode::Mode; +pub use position::Position; pub use redraw_request::RedrawRequest; +pub use settings::Settings; pub use user_attention::UserAttention; use crate::subscription::{self, Subscription}; @@ -22,9 +30,20 @@ use crate::time::Instant; /// /// In any case, this [`Subscription`] is useful to smoothly draw application-driven /// animations without missing any frames. -pub fn frames() -> Subscription<Instant> { +pub fn frames() -> Subscription<Frame> { subscription::raw_events(|event, _status| match event { - crate::Event::Window(Event::RedrawRequested(at)) => Some(at), + crate::Event::Window(id, Event::RedrawRequested(at)) => { + Some(Frame { id, at }) + } _ => None, }) } + +/// The returned `Frame` for a framerate subscription. +#[derive(Debug)] +pub struct Frame { + /// The `window::Id` that the `Frame` was produced in. + pub id: Id, + /// The `Instant` at which the frame was produced. + pub at: Instant, +} diff --git a/native/src/window/action.rs b/native/src/window/action.rs index ce36d129..5751bf97 100644 --- a/native/src/window/action.rs +++ b/native/src/window/action.rs @@ -1,4 +1,4 @@ -use crate::window::{Mode, UserAttention}; +use crate::window::{Mode, UserAttention, Settings}; use iced_futures::MaybeSend; use std::fmt; @@ -13,6 +13,11 @@ pub enum Action<T> { /// There’s no guarantee that this will work unless the left mouse /// button was pressed immediately before this function is called. Drag, + /// Spawns a new window with the provided [`window::Settings`]. + Spawn { + /// The settings of the [`Window`]. + settings: Settings, + }, /// Resize the window. Resize { /// The new logical width of the window @@ -90,6 +95,7 @@ impl<T> Action<T> { T: 'static, { match self { + Self::Spawn { settings } => Action::Spawn { settings }, Self::Close => Action::Close, Self::Drag => Action::Drag, Self::Resize { width, height } => Action::Resize { width, height }, @@ -117,6 +123,9 @@ impl<T> fmt::Debug for Action<T> { match self { Self::Close => write!(f, "Action::Close"), Self::Drag => write!(f, "Action::Drag"), + Self::Spawn { settings } => { + write!(f, "Action::Spawn {{ settings: {:?} }}", settings) + } Self::Resize { width, height } => write!( f, "Action::Resize {{ widget: {width}, height: {height} }}" diff --git a/native/src/window/icon.rs b/native/src/window/icon.rs new file mode 100644 index 00000000..08a6acfd --- /dev/null +++ b/native/src/window/icon.rs @@ -0,0 +1,12 @@ +//! Attach an icon to the window of your application. + +/// The icon of a window. +#[derive(Debug, Clone)] +pub struct Icon { + /// The __rgba__ color data of the window [`Icon`]. + pub rgba: Vec<u8>, + /// The width of the window [`Icon`]. + pub width: u32, + /// The height of the window [`Icon`]. + pub height: u32, +} diff --git a/native/src/window/id.rs b/native/src/window/id.rs new file mode 100644 index 00000000..fa9761f5 --- /dev/null +++ b/native/src/window/id.rs @@ -0,0 +1,28 @@ +use std::collections::hash_map::DefaultHasher; +use std::fmt::{Display, Formatter}; +use std::hash::{Hash, Hasher}; + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] +/// The ID of the window. +/// +/// This is not necessarily the same as the window ID fetched from `winit::window::Window`. +pub struct Id(u64); + +impl Id { + /// TODO(derezzedex): maybe change `u64` to an enum `Type::{Single, Multi(u64)}` + pub const MAIN: Self = Id(0); + + /// Creates a new unique window ID. + pub fn new(id: impl Hash) -> Id { + let mut hasher = DefaultHasher::new(); + id.hash(&mut hasher); + + Id(hasher.finish()) + } +} + +impl Display for Id { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!(f, "Id({})", self.0) + } +} diff --git a/winit/src/position.rs b/native/src/window/position.rs index c260c29e..c260c29e 100644 --- a/winit/src/position.rs +++ b/native/src/window/position.rs diff --git a/src/window/settings.rs b/native/src/window/settings.rs index 24d0f4f9..67798fbe 100644 --- a/src/window/settings.rs +++ b/native/src/window/settings.rs @@ -50,21 +50,3 @@ impl Default for Settings { } } } - -impl From<Settings> for iced_winit::settings::Window { - fn from(settings: Settings) -> Self { - Self { - size: settings.size, - position: iced_winit::Position::from(settings.position), - min_size: settings.min_size, - max_size: settings.max_size, - visible: settings.visible, - resizable: settings.resizable, - decorations: settings.decorations, - transparent: settings.transparent, - always_on_top: settings.always_on_top, - icon: settings.icon.map(Icon::into), - platform_specific: Default::default(), - } - } -} @@ -182,6 +182,9 @@ pub mod touch; pub mod widget; pub mod window; +#[cfg(feature = "multi_window")] +pub mod multi_window; + #[cfg(all(not(feature = "glow"), feature = "wgpu"))] use iced_winit as runtime; diff --git a/src/multi_window.rs b/src/multi_window.rs new file mode 100644 index 00000000..5b7a00b4 --- /dev/null +++ b/src/multi_window.rs @@ -0,0 +1,4 @@ +//! Leverage multi-window support in your application. +mod application; + +pub use application::Application; diff --git a/src/multi_window/application.rs b/src/multi_window/application.rs new file mode 100644 index 00000000..d0b515ab --- /dev/null +++ b/src/multi_window/application.rs @@ -0,0 +1,254 @@ +use crate::window; +use crate::{Command, Element, Executor, Settings, Subscription}; + +pub use iced_native::application::{Appearance, StyleSheet}; + +/// An interactive cross-platform multi-window application. +/// +/// This trait is the main entrypoint of Iced. Once implemented, you can run +/// your GUI application by simply calling [`run`](#method.run). +/// +/// An [`Application`] can execute asynchronous actions by returning a +/// [`Command`] in some of its methods. For example, to spawn a new window, you +/// can use the `iced_winit::window::spawn()` [`Command`]. +/// +/// When using an [`Application`] with the `debug` feature enabled, a debug view +/// can be toggled by pressing `F12`. +/// +/// ## A simple "Hello, world!" +/// +/// If you just want to get started, here is a simple [`Application`] that +/// says "Hello, world!": +/// +/// ```no_run +/// use iced::executor; +/// use iced::multi_window::Application; +/// use iced::window; +/// use iced::{Command, Element, Settings, Theme}; +/// +/// pub fn main() -> iced::Result { +/// Hello::run(Settings::default()) +/// } +/// +/// struct Hello; +/// +/// impl Application for Hello { +/// type Executor = executor::Default; +/// type Message = (); +/// type Theme = Theme; +/// type Flags = (); +/// +/// fn new(_flags: ()) -> (Hello, Command<Self::Message>) { +/// (Hello, Command::none()) +/// } +/// +/// fn title(&self, window: window::Id) -> String { +/// String::from("A cool application") +/// } +/// +/// fn update(&mut self, _message: Self::Message) -> Command<Self::Message> { +/// Command::none() +/// } +/// +/// fn view(&self, window: window::Id) -> Element<Self::Message> { +/// "Hello, world!".into() +/// } +/// +/// fn close_requested(&self, window: window::Id) -> Self::Message { +/// () +/// } +/// } +/// ``` +pub trait Application: Sized { + /// The [`Executor`] that will run commands and subscriptions. + /// + /// The [default executor] can be a good starting point! + /// + /// [`Executor`]: Self::Executor + /// [default executor]: crate::executor::Default + type Executor: Executor; + + /// The type of __messages__ your [`Application`] will produce. + type Message: std::fmt::Debug + Send; + + /// The theme of your [`Application`]. + type Theme: Default + StyleSheet; + + /// The data needed to initialize your [`Application`]. + type Flags; + + /// Initializes the [`Application`] with the flags provided to + /// [`run`] as part of the [`Settings`]. + /// + /// Here is where you should return the initial state of your app. + /// + /// Additionally, you can return a [`Command`] if you need to perform some + /// async action in the background on startup. This is useful if you want to + /// load state from a file, perform an initial HTTP request, etc. + /// + /// [`run`]: Self::run + fn new(flags: Self::Flags) -> (Self, Command<Self::Message>); + + /// Returns the current title of the [`Application`]. + /// + /// This title can be dynamic! The runtime will automatically update the + /// title of your application when necessary. + fn title(&self, window: window::Id) -> String; + + /// Handles a __message__ and updates the state of the [`Application`]. + /// + /// This is where you define your __update logic__. All the __messages__, + /// produced by either user interactions or commands, will be handled by + /// this method. + /// + /// Any [`Command`] returned will be executed immediately in the background. + fn update(&mut self, message: Self::Message) -> Command<Self::Message>; + + /// Returns the current [`Theme`] of the [`Application`]. + /// + /// [`Theme`]: Self::Theme + fn theme(&self) -> Self::Theme { + Self::Theme::default() + } + + /// Returns the current [`Style`] of the [`Theme`]. + /// + /// [`Style`]: <Self::Theme as StyleSheet>::Style + /// [`Theme`]: Self::Theme + fn style(&self) -> <Self::Theme as StyleSheet>::Style { + <Self::Theme as StyleSheet>::Style::default() + } + + /// Returns the event [`Subscription`] for the current state of the + /// application. + /// + /// A [`Subscription`] will be kept alive as long as you keep returning it, + /// and the __messages__ produced will be handled by + /// [`update`](#tymethod.update). + /// + /// By default, this method returns an empty [`Subscription`]. + fn subscription(&self) -> Subscription<Self::Message> { + Subscription::none() + } + + /// Returns the widgets to display in the [`Application`]. + /// + /// These widgets can produce __messages__ based on user interaction. + fn view( + &self, + window: window::Id, + ) -> Element<'_, Self::Message, crate::Renderer<Self::Theme>>; + + /// Returns the scale factor of the [`Application`]. + /// + /// It can be used to dynamically control the size of the UI at runtime + /// (i.e. zooming). + /// + /// For instance, a scale factor of `2.0` will make widgets twice as big, + /// while a scale factor of `0.5` will shrink them to half their size. + /// + /// By default, it returns `1.0`. + #[allow(unused_variables)] + fn scale_factor(&self, window: window::Id) -> f64 { + 1.0 + } + + /// Returns whether the [`Application`] should be terminated. + /// + /// By default, it returns `false`. + fn should_exit(&self) -> bool { + false + } + + /// Requests that the [`window`] be closed. + fn close_requested(&self, window: window::Id) -> Self::Message; + + /// Runs the [`Application`]. + /// + /// On native platforms, this method will take control of the current thread + /// until the [`Application`] exits. + /// + /// On the web platform, this method __will NOT return__ unless there is an + /// [`Error`] during startup. + /// + /// [`Error`]: crate::Error + fn run(settings: Settings<Self::Flags>) -> crate::Result + where + Self: 'static, + { + #[allow(clippy::needless_update)] + let renderer_settings = crate::renderer::Settings { + default_font: settings.default_font, + default_text_size: settings.default_text_size, + text_multithreading: settings.text_multithreading, + antialiasing: if settings.antialiasing { + Some(crate::renderer::settings::Antialiasing::MSAAx4) + } else { + None + }, + ..crate::renderer::Settings::from_env() + }; + + Ok(crate::runtime::multi_window::run::< + Instance<Self>, + Self::Executor, + crate::renderer::window::Compositor<Self::Theme>, + >(settings.into(), renderer_settings)?) + } +} + +struct Instance<A: Application>(A); + +impl<A> crate::runtime::multi_window::Application for Instance<A> +where + A: Application, +{ + type Flags = A::Flags; + type Renderer = crate::Renderer<A::Theme>; + type Message = A::Message; + + fn update(&mut self, message: Self::Message) -> Command<Self::Message> { + self.0.update(message) + } + + fn view( + &self, + window: window::Id, + ) -> Element<'_, Self::Message, Self::Renderer> { + self.0.view(window) + } + + fn new(flags: Self::Flags) -> (Self, Command<A::Message>) { + let (app, command) = A::new(flags); + + (Instance(app), command) + } + + fn title(&self, window: window::Id) -> String { + self.0.title(window) + } + + fn theme(&self) -> A::Theme { + self.0.theme() + } + + fn style(&self) -> <A::Theme as StyleSheet>::Style { + self.0.style() + } + + fn subscription(&self) -> Subscription<Self::Message> { + self.0.subscription() + } + + fn scale_factor(&self, window: window::Id) -> f64 { + self.0.scale_factor(window) + } + + fn should_exit(&self) -> bool { + self.0.should_exit() + } + + fn close_requested(&self, window: window::Id) -> Self::Message { + self.0.close_requested(window) + } +} diff --git a/src/window.rs b/src/window.rs index 2018053f..73e90243 100644 --- a/src/window.rs +++ b/src/window.rs @@ -1,12 +1,7 @@ //! Configure the window of your application in native platforms. -mod position; -mod settings; - -pub mod icon; - -pub use icon::Icon; -pub use position::Position; -pub use settings::Settings; +pub use iced_native::window::Icon; +pub use iced_native::window::Position; +pub use iced_native::window::Settings; #[cfg(not(target_arch = "wasm32"))] pub use crate::runtime::window::*; diff --git a/src/window/position.rs b/src/window/position.rs deleted file mode 100644 index 6b9fac41..00000000 --- a/src/window/position.rs +++ /dev/null @@ -1,32 +0,0 @@ -/// The position of a window in a given screen. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum Position { - /// The platform-specific default position for a new window. - Default, - /// The window is completely centered on the screen. - Centered, - /// The window is positioned with specific coordinates: `(X, Y)`. - /// - /// When the decorations of the window are enabled, Windows 10 will add some - /// invisible padding to the window. This padding gets included in the - /// position. So if you have decorations enabled and want the window to be - /// at (0, 0) you would have to set the position to - /// `(PADDING_X, PADDING_Y)`. - Specific(i32, i32), -} - -impl Default for Position { - fn default() -> Self { - Self::Default - } -} - -impl From<Position> for iced_winit::Position { - fn from(position: Position) -> Self { - match position { - Position::Default => Self::Default, - Position::Centered => Self::Centered, - Position::Specific(x, y) => Self::Specific(x, y), - } - } -} diff --git a/winit/Cargo.toml b/winit/Cargo.toml index 60e464c6..443d89be 100644 --- a/winit/Cargo.toml +++ b/winit/Cargo.toml @@ -16,6 +16,7 @@ chrome-trace = ["trace", "tracing-chrome"] debug = ["iced_native/debug"] system = ["sysinfo"] application = [] +multi_window = [] [dependencies] window_clipboard = "0.2" diff --git a/winit/src/application.rs b/winit/src/application.rs index 3fdec658..3123a318 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -1,6 +1,4 @@ //! Create interactive, native cross-platform applications. -#[cfg(feature = "trace")] -mod profiler; mod state; pub use state::State; @@ -28,7 +26,7 @@ pub use iced_native::application::{Appearance, StyleSheet}; use std::mem::ManuallyDrop; #[cfg(feature = "trace")] -pub use profiler::Profiler; +pub use crate::Profiler; #[cfg(feature = "trace")] use tracing::{info_span, instrument::Instrument}; @@ -414,6 +412,7 @@ async fn run_instance<A, E, C>( // Then, we can use the `interface_state` here to decide if a redraw // is needed right away, or simply wait until a specific time. let redraw_event = Event::Window( + crate::window::Id::MAIN, crate::window::Event::RedrawRequested(Instant::now()), ); @@ -569,6 +568,7 @@ async fn run_instance<A, E, C>( state.update(&window, &window_event, &mut debug); if let Some(event) = conversion::window_event( + crate::window::Id::MAIN, &window_event, state.scale_factor(), state.modifiers(), @@ -734,13 +734,18 @@ pub fn run_command<A, E>( clipboard.write(contents); } }, - command::Action::Window(action) => match action { + command::Action::Window(_id, action) => match action { window::Action::Close => { *should_exit = true; } window::Action::Drag => { let _res = window.drag_window(); } + window::Action::Spawn { .. } => { + log::info!( + "This is only available on `multi_window::Application`" + ) + } window::Action::Resize { width, height } => { window.set_inner_size(winit::dpi::LogicalSize { width, diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index 1b2ead36..5c86b30a 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -10,6 +10,7 @@ use crate::{Event, Point, Position}; /// Converts a winit window event into an iced event. pub fn window_event( + id: window::Id, event: &winit::event::WindowEvent<'_>, scale_factor: f64, modifiers: winit::event::ModifiersState, @@ -20,21 +21,27 @@ pub fn window_event( WindowEvent::Resized(new_size) => { let logical_size = new_size.to_logical(scale_factor); - Some(Event::Window(window::Event::Resized { - width: logical_size.width, - height: logical_size.height, - })) + Some(Event::Window( + id, + window::Event::Resized { + width: logical_size.width, + height: logical_size.height, + }, + )) } WindowEvent::ScaleFactorChanged { new_inner_size, .. } => { let logical_size = new_inner_size.to_logical(scale_factor); - Some(Event::Window(window::Event::Resized { - width: logical_size.width, - height: logical_size.height, - })) + Some(Event::Window( + id, + window::Event::Resized { + width: logical_size.width, + height: logical_size.height, + }, + )) } WindowEvent::CloseRequested => { - Some(Event::Window(window::Event::CloseRequested)) + Some(Event::Window(id, window::Event::CloseRequested)) } WindowEvent::CursorMoved { position, .. } => { let position = position.to_logical::<f64>(scale_factor); @@ -112,19 +119,22 @@ pub fn window_event( WindowEvent::ModifiersChanged(new_modifiers) => Some(Event::Keyboard( keyboard::Event::ModifiersChanged(self::modifiers(*new_modifiers)), )), - WindowEvent::Focused(focused) => Some(Event::Window(if *focused { - window::Event::Focused - } else { - window::Event::Unfocused - })), + WindowEvent::Focused(focused) => Some(Event::Window( + id, + if *focused { + window::Event::Focused + } else { + window::Event::Unfocused + }, + )), WindowEvent::HoveredFile(path) => { - Some(Event::Window(window::Event::FileHovered(path.clone()))) + Some(Event::Window(id, window::Event::FileHovered(path.clone()))) } WindowEvent::DroppedFile(path) => { - Some(Event::Window(window::Event::FileDropped(path.clone()))) + Some(Event::Window(id, window::Event::FileDropped(path.clone()))) } WindowEvent::HoveredFileCancelled => { - Some(Event::Window(window::Event::FilesHoveredLeft)) + Some(Event::Window(id, window::Event::FilesHoveredLeft)) } WindowEvent::Touch(touch) => { Some(Event::Touch(touch_event(*touch, scale_factor))) @@ -133,7 +143,7 @@ pub fn window_event( let winit::dpi::LogicalPosition { x, y } = position.to_logical(scale_factor); - Some(Event::Window(window::Event::Moved { x, y })) + Some(Event::Window(id, window::Event::Moved { x, y })) } _ => None, } diff --git a/src/window/icon.rs b/winit/src/icon.rs index d57eb79c..73b4e037 100644 --- a/src/window/icon.rs +++ b/winit/src/icon.rs @@ -7,7 +7,7 @@ use std::path::Path; /// The icon of a window. #[derive(Debug, Clone)] -pub struct Icon(iced_winit::winit::window::Icon); +pub struct Icon(winit::window::Icon); impl Icon { /// Creates an icon from 32bpp RGBA data. @@ -16,8 +16,7 @@ impl Icon { width: u32, height: u32, ) -> Result<Self, Error> { - let raw = - iced_winit::winit::window::Icon::from_rgba(rgba, width, height)?; + let raw = winit::window::Icon::from_rgba(rgba, width, height)?; Ok(Icon(raw)) } @@ -91,9 +90,9 @@ impl From<std::io::Error> for Error { } } -impl From<iced_winit::winit::window::BadIcon> for Error { - fn from(error: iced_winit::winit::window::BadIcon) -> Self { - use iced_winit::winit::window::BadIcon; +impl From<winit::window::BadIcon> for Error { + fn from(error: winit::window::BadIcon) -> Self { + use winit::window::BadIcon; match error { BadIcon::ByteCountNotDivisibleBy4 { byte_count } => { @@ -114,7 +113,7 @@ impl From<iced_winit::winit::window::BadIcon> for Error { } } -impl From<Icon> for iced_winit::winit::window::Icon { +impl From<Icon> for winit::window::Icon { fn from(icon: Icon) -> Self { icon.0 } @@ -167,3 +166,11 @@ impl std::error::Error for Error { Some(self) } } + +impl TryFrom<iced_native::window::Icon> for Icon { + type Error = Error; + + fn try_from(icon: iced_native::window::Icon) -> Result<Self, Self::Error> { + Icon::from_rgba(icon.rgba, icon.width, icon.height) + } +} diff --git a/winit/src/lib.rs b/winit/src/lib.rs index 3a33e174..fe5768df 100644 --- a/winit/src/lib.rs +++ b/winit/src/lib.rs @@ -35,6 +35,9 @@ pub use iced_native::*; pub use winit; +#[cfg(feature = "multi_window")] +pub mod multi_window; + #[cfg(feature = "application")] pub mod application; pub mod clipboard; @@ -46,17 +49,20 @@ pub mod window; pub mod system; mod error; -mod position; +mod icon; mod proxy; +#[cfg(feature = "trace")] +mod profiler; #[cfg(feature = "application")] pub use application::Application; #[cfg(feature = "trace")] -pub use application::Profiler; +pub use profiler::Profiler; pub use clipboard::Clipboard; pub use error::Error; -pub use position::Position; +pub use icon::Icon; pub use proxy::Proxy; pub use settings::Settings; pub use iced_graphics::Viewport; +pub use iced_native::window::Position; diff --git a/winit/src/multi_window.rs b/winit/src/multi_window.rs new file mode 100644 index 00000000..fc91f41b --- /dev/null +++ b/winit/src/multi_window.rs @@ -0,0 +1,1215 @@ +//! Create interactive, native cross-platform applications. +mod state; + +pub use state::State; + +use crate::clipboard::{self, Clipboard}; +use crate::conversion; +use crate::mouse; +use crate::renderer; +use crate::settings; +use crate::widget::operation; +use crate::window; +use crate::{ + Command, Debug, Element, Error, Executor, Proxy, Renderer, Runtime, + Settings, Size, Subscription, +}; + +use iced_futures::futures::channel::mpsc; +use iced_futures::futures::{self, FutureExt}; +use iced_graphics::compositor; +use iced_native::user_interface::{self, UserInterface}; + +pub use iced_native::application::{Appearance, StyleSheet}; + +use std::collections::HashMap; +use std::mem::ManuallyDrop; +use std::time::Instant; + +#[cfg(feature = "trace")] +pub use crate::Profiler; +#[cfg(feature = "trace")] +use tracing::{info_span, instrument::Instrument}; + +/// TODO(derezzedex) +// This is the an wrapper around the `Application::Message` associate type +// to allows the `shell` to create internal messages, while still having +// the current user specified custom messages. +#[derive(Debug)] +pub enum Event<Message> { + /// An [`Application`] generated message + Application(Message), + /// TODO(derezzedex) + // Create a wrapper variant of `window::Event` type instead + // (maybe we should also allow users to listen/react to those internal messages?) + NewWindow { + /// The [window::Id] of the newly spawned [`Window`]. + id: window::Id, + /// The [settings::Window] of the newly spawned [`Window`]. + settings: settings::Window, + /// The title of the newly spawned [`Window`]. + title: String, + }, + /// TODO(derezzedex) + CloseWindow(window::Id), + /// TODO(derezzedex) + WindowCreated(window::Id, winit::window::Window), +} + +/// An interactive, native cross-platform application. +/// +/// This trait is the main entrypoint of Iced. Once implemented, you can run +/// your GUI application by simply calling [`run`]. It will run in +/// its own window. +/// +/// An [`Application`] can execute asynchronous actions by returning a +/// [`Command`] in some of its methods. +/// +/// When using an [`Application`] with the `debug` feature enabled, a debug view +/// can be toggled by pressing `F12`. +pub trait Application: Sized +where + <Self::Renderer as crate::Renderer>::Theme: StyleSheet, +{ + /// The data needed to initialize your [`Application`]. + type Flags; + + /// The graphics backend to use to draw the [`Program`]. + type Renderer: Renderer; + + /// The type of __messages__ your [`Program`] will produce. + type Message: std::fmt::Debug + Send; + + /// Handles a __message__ and updates the state of the [`Program`]. + /// + /// This is where you define your __update logic__. All the __messages__, + /// produced by either user interactions or commands, will be handled by + /// this method. + /// + /// Any [`Command`] returned will be executed immediately in the + /// background by shells. + fn update(&mut self, message: Self::Message) -> Command<Self::Message>; + + /// Returns the widgets to display in the [`Program`]. + /// + /// These widgets can produce __messages__ based on user interaction. + fn view( + &self, + window: window::Id, + ) -> Element<'_, Self::Message, Self::Renderer>; + + /// Initializes the [`Application`] with the flags provided to + /// [`run`] as part of the [`Settings`]. + /// + /// Here is where you should return the initial state of your app. + /// + /// Additionally, you can return a [`Command`] if you need to perform some + /// async action in the background on startup. This is useful if you want to + /// load state from a file, perform an initial HTTP request, etc. + fn new(flags: Self::Flags) -> (Self, Command<Self::Message>); + + /// Returns the current title of the current [`Application`] window. + /// + /// This title can be dynamic! The runtime will automatically update the + /// title of your application when necessary. + fn title(&self, window_id: window::Id) -> String; + + /// Returns the current [`Theme`] of the [`Application`]. + fn theme(&self) -> <Self::Renderer as crate::Renderer>::Theme; + + /// Returns the [`Style`] variation of the [`Theme`]. + fn style( + &self, + ) -> <<Self::Renderer as crate::Renderer>::Theme as StyleSheet>::Style { + Default::default() + } + + /// Returns the event `Subscription` for the current state of the + /// application. + /// + /// The messages produced by the `Subscription` will be handled by + /// [`update`](#tymethod.update). + /// + /// A `Subscription` will be kept alive as long as you keep returning it! + /// + /// By default, it returns an empty subscription. + fn subscription(&self) -> Subscription<Self::Message> { + Subscription::none() + } + + /// Returns the scale factor of the [`Application`]. + /// + /// It can be used to dynamically control the size of the UI at runtime + /// (i.e. zooming). + /// + /// For instance, a scale factor of `2.0` will make widgets twice as big, + /// while a scale factor of `0.5` will shrink them to half their size. + /// + /// By default, it returns `1.0`. + #[allow(unused_variables)] + fn scale_factor(&self, window: window::Id) -> f64 { + 1.0 + } + + /// Returns whether the [`Application`] should be terminated. + /// + /// By default, it returns `false`. + fn should_exit(&self) -> bool { + false + } + + /// Requests that the [`window`] be closed. + fn close_requested(&self, window: window::Id) -> Self::Message; +} + +/// Runs an [`Application`] with an executor, compositor, and the provided +/// settings. +pub fn run<A, E, C>( + settings: Settings<A::Flags>, + compositor_settings: C::Settings, +) -> Result<(), Error> +where + A: Application + 'static, + E: Executor + 'static, + C: iced_graphics::window::Compositor<Renderer = A::Renderer> + 'static, + <A::Renderer as crate::Renderer>::Theme: StyleSheet, +{ + use futures::task; + use futures::Future; + use winit::event_loop::EventLoopBuilder; + + #[cfg(feature = "trace")] + let _guard = Profiler::init(); + + let mut debug = Debug::new(); + debug.startup_started(); + + #[cfg(feature = "trace")] + let _ = info_span!("Application", "RUN").entered(); + + let event_loop = EventLoopBuilder::with_user_event().build(); + let proxy = event_loop.create_proxy(); + + let runtime = { + let proxy = Proxy::new(event_loop.create_proxy()); + let executor = E::new().map_err(Error::ExecutorCreationFailed)?; + + Runtime::new(executor, proxy) + }; + + let (application, init_command) = { + let flags = settings.flags; + + runtime.enter(|| A::new(flags)) + }; + + let builder = settings.window.into_builder( + &application.title(window::Id::MAIN), + event_loop.primary_monitor(), + settings.id, + ); + + log::info!("Window builder: {:#?}", builder); + + let window = builder + .build(&event_loop) + .map_err(Error::WindowCreationFailed)?; + + let windows: HashMap<window::Id, winit::window::Window> = + HashMap::from([(window::Id::MAIN, window)]); + + let window = windows.values().next().expect("No window found"); + + #[cfg(target_arch = "wasm32")] + { + use winit::platform::web::WindowExtWebSys; + + let canvas = window.canvas(); + + let window = web_sys::window().unwrap(); + let document = window.document().unwrap(); + let body = document.body().unwrap(); + + let _ = body + .append_child(&canvas) + .expect("Append canvas to HTML body"); + } + + let (compositor, renderer) = C::new(compositor_settings, Some(&window))?; + + let (mut event_sender, event_receiver) = mpsc::unbounded(); + let (control_sender, mut control_receiver) = mpsc::unbounded(); + + let mut instance = Box::pin({ + let run_instance = run_instance::<A, E, C>( + application, + compositor, + renderer, + runtime, + proxy, + debug, + event_receiver, + control_sender, + init_command, + windows, + settings.exit_on_close_request, + ); + + #[cfg(feature = "trace")] + let run_instance = + run_instance.instrument(info_span!("Application", "LOOP")); + + run_instance + }); + + let mut context = task::Context::from_waker(task::noop_waker_ref()); + + platform::run(event_loop, move |event, event_loop, control_flow| { + use winit::event_loop::ControlFlow; + + if let ControlFlow::ExitWithCode(_) = control_flow { + return; + } + + let event = match event { + winit::event::Event::WindowEvent { + event: + winit::event::WindowEvent::ScaleFactorChanged { + new_inner_size, + .. + }, + window_id, + } => Some(winit::event::Event::WindowEvent { + event: winit::event::WindowEvent::Resized(*new_inner_size), + window_id, + }), + winit::event::Event::UserEvent(Event::NewWindow { + id, + settings, + title, + }) => { + let window = settings + .into_builder(&title, event_loop.primary_monitor(), None) + .build(event_loop) + .expect("Failed to build window"); + + Some(winit::event::Event::UserEvent(Event::WindowCreated( + id, window, + ))) + } + _ => event.to_static(), + }; + + if let Some(event) = event { + event_sender.start_send(event).expect("Send event"); + + let poll = instance.as_mut().poll(&mut context); + + match poll { + task::Poll::Pending => { + if let Ok(Some(flow)) = control_receiver.try_next() { + *control_flow = flow; + } + } + task::Poll::Ready(_) => { + *control_flow = ControlFlow::Exit; + } + }; + } + }) +} + +async fn run_instance<A, E, C>( + mut application: A, + mut compositor: C, + mut renderer: A::Renderer, + mut runtime: Runtime<E, Proxy<Event<A::Message>>, Event<A::Message>>, + mut proxy: winit::event_loop::EventLoopProxy<Event<A::Message>>, + mut debug: Debug, + mut event_receiver: mpsc::UnboundedReceiver< + winit::event::Event<'_, Event<A::Message>>, + >, + mut control_sender: mpsc::UnboundedSender<winit::event_loop::ControlFlow>, + init_command: Command<A::Message>, + mut windows: HashMap<window::Id, winit::window::Window>, + _exit_on_close_request: bool, +) where + A: Application + 'static, + E: Executor + 'static, + C: iced_graphics::window::Compositor<Renderer = A::Renderer> + 'static, + <A::Renderer as crate::Renderer>::Theme: StyleSheet, +{ + use iced_futures::futures::stream::StreamExt; + use winit::event; + use winit::event_loop::ControlFlow; + + let mut clipboard = + Clipboard::connect(windows.values().next().expect("No window found")); + let mut caches = HashMap::new(); + let mut window_ids: HashMap<_, _> = windows + .iter() + .map(|(&id, window)| (window.id(), id)) + .collect(); + + let mut states = HashMap::new(); + let mut surfaces = HashMap::new(); + let mut interfaces = ManuallyDrop::new(HashMap::new()); + + for (&id, window) in windows.keys().zip(windows.values()) { + let mut surface = compositor.create_surface(window); + let state = State::new(&application, id, window); + let physical_size = state.physical_size(); + + compositor.configure_surface( + &mut surface, + physical_size.width, + physical_size.height, + ); + + let user_interface = build_user_interface( + &application, + user_interface::Cache::default(), + &mut renderer, + state.logical_size(), + &mut debug, + id, + ); + + let _ = states.insert(id, state); + let _ = surfaces.insert(id, surface); + let _ = interfaces.insert(id, user_interface); + let _ = caches.insert(id, user_interface::Cache::default()); + } + + run_command( + &application, + &mut caches, + &states, + &mut renderer, + init_command, + &mut runtime, + &mut clipboard, + &mut proxy, + &mut debug, + &windows, + || compositor.fetch_information(), + ); + + runtime.track(application.subscription().map(Event::Application)); + + let mut mouse_interaction = mouse::Interaction::default(); + let mut events = Vec::new(); + let mut messages = Vec::new(); + let mut redraw_pending = false; + + debug.startup_finished(); + + 'main: while let Some(event) = event_receiver.next().await { + match event { + event::Event::NewEvents(start_cause) => { + redraw_pending = matches!( + start_cause, + event::StartCause::Init + | event::StartCause::Poll + | event::StartCause::ResumeTimeReached { .. } + ); + } + event::Event::MainEventsCleared => { + for id in states.keys().copied().collect::<Vec<_>>() { + let (filtered, remaining): (Vec<_>, Vec<_>) = + events.iter().cloned().partition( + |(window_id, _event): &( + Option<window::Id>, + iced_native::event::Event, + )| { + *window_id == Some(id) || *window_id == None + }, + ); + + events.retain(|el| remaining.contains(el)); + let window_events: Vec<_> = filtered + .into_iter() + .map(|(_id, event)| event) + .collect(); + + if !redraw_pending + && window_events.is_empty() + && messages.is_empty() + { + continue; + } + + debug.event_processing_started(); + + let cursor_position = + states.get(&id).unwrap().cursor_position(); + + let (interface_state, statuses) = { + let user_interface = interfaces.get_mut(&id).unwrap(); + user_interface.update( + &window_events, + cursor_position, + &mut renderer, + &mut clipboard, + &mut messages, + ) + }; + + debug.event_processing_finished(); + + for event in + window_events.into_iter().zip(statuses.into_iter()) + { + runtime.broadcast(event); + } + + // TODO(derezzedex): Should we redraw every window? We can't know what changed. + if !messages.is_empty() + || matches!( + interface_state, + user_interface::State::Outdated, + ) + { + let mut cached_interfaces: HashMap<_, _> = + ManuallyDrop::into_inner(interfaces) + .drain() + .map( + |(id, interface): ( + window::Id, + UserInterface<'_, _, _>, + )| { + (id, interface.into_cache()) + }, + ) + .collect(); + + // Update application + update( + &mut application, + &mut cached_interfaces, + &states, + &mut renderer, + &mut runtime, + &mut clipboard, + &mut proxy, + &mut debug, + &mut messages, + &windows, + || compositor.fetch_information(), + ); + + // Update window + states.get_mut(&id).unwrap().synchronize( + &application, + id, + windows.get(&id).expect("No window found with ID."), + ); + + interfaces = ManuallyDrop::new(build_user_interfaces( + &application, + &mut renderer, + &mut debug, + &states, + cached_interfaces, + )); + + if application.should_exit() { + break 'main; + } + } + + // TODO: Avoid redrawing all the time by forcing widgets to + // request redraws on state changes + // + // Then, we can use the `interface_state` here to decide if a redraw + // is needed right away, or simply wait until a specific time. + let redraw_event = iced_native::Event::Window( + id, + window::Event::RedrawRequested(Instant::now()), + ); + + let (interface_state, _) = + interfaces.get_mut(&id).unwrap().update( + &[redraw_event.clone()], + cursor_position, + &mut renderer, + &mut clipboard, + &mut messages, + ); + + debug.draw_started(); + let new_mouse_interaction = { + let state = states.get(&id).unwrap(); + + interfaces.get_mut(&id).unwrap().draw( + &mut renderer, + state.theme(), + &renderer::Style { + text_color: state.text_color(), + }, + state.cursor_position(), + ) + }; + debug.draw_finished(); + + let window = windows.get(&id).unwrap(); + + if new_mouse_interaction != mouse_interaction { + window.set_cursor_icon(conversion::mouse_interaction( + new_mouse_interaction, + )); + + mouse_interaction = new_mouse_interaction; + } + + for window in windows.values() { + window.request_redraw(); + + runtime.broadcast(( + redraw_event.clone(), + crate::event::Status::Ignored, + )); + + let _ = + control_sender.start_send(match interface_state { + user_interface::State::Updated { + redraw_request: Some(redraw_request), + } => match redraw_request { + window::RedrawRequest::NextFrame => { + ControlFlow::Poll + } + window::RedrawRequest::At(at) => { + ControlFlow::WaitUntil(at) + } + }, + _ => ControlFlow::Wait, + }); + + redraw_pending = false; + } + } + } + event::Event::PlatformSpecific(event::PlatformSpecific::MacOS( + event::MacOS::ReceivedUrl(url), + )) => { + use iced_native::event; + events.push(( + None, + iced_native::Event::PlatformSpecific( + event::PlatformSpecific::MacOS( + event::MacOS::ReceivedUrl(url), + ), + ), + )); + } + event::Event::UserEvent(event) => match event { + Event::Application(message) => { + messages.push(message); + } + Event::WindowCreated(id, window) => { + let mut surface = compositor.create_surface(&window); + + let state = State::new(&application, id, &window); + + let physical_size = state.physical_size(); + + compositor.configure_surface( + &mut surface, + physical_size.width, + physical_size.height, + ); + + let user_interface = build_user_interface( + &application, + user_interface::Cache::default(), + &mut renderer, + state.logical_size(), + &mut debug, + id, + ); + + let _ = states.insert(id, state); + let _ = surfaces.insert(id, surface); + let _ = interfaces.insert(id, user_interface); + let _ = window_ids.insert(window.id(), id); + let _ = windows.insert(id, window); + let _ = caches.insert(id, user_interface::Cache::default()); + } + Event::CloseWindow(id) => { + if let Some(window) = windows.get(&id) { + if window_ids.remove(&window.id()).is_none() { + log::error!("Failed to remove window with id {:?} from window_ids.", window.id()); + } + } else { + log::error!( + "Could not find window with id {:?} in windows.", + id + ); + } + if states.remove(&id).is_none() { + log::error!( + "Failed to remove window {:?} from states.", + id + ); + } + if interfaces.remove(&id).is_none() { + log::error!( + "Failed to remove window {:?} from interfaces.", + id + ); + } + if windows.remove(&id).is_none() { + log::error!( + "Failed to remove window {:?} from windows.", + id + ); + } + if surfaces.remove(&id).is_none() { + log::error!( + "Failed to remove window {:?} from surfaces.", + id + ); + } + + if windows.is_empty() { + log::info!( + "All windows are closed. Terminating program." + ); + break 'main; + } else { + log::info!("Remaining windows: {:?}", windows.len()); + } + } + Event::NewWindow { .. } => unreachable!(), + }, + event::Event::RedrawRequested(id) => { + #[cfg(feature = "trace")] + let _ = info_span!("Application", "FRAME").entered(); + + let state = window_ids + .get(&id) + .and_then(|id| states.get_mut(id)) + .unwrap(); + let surface = window_ids + .get(&id) + .and_then(|id| surfaces.get_mut(id)) + .unwrap(); + let physical_size = state.physical_size(); + + if physical_size.width == 0 || physical_size.height == 0 { + continue; + } + + debug.render_started(); + + if state.viewport_changed() { + let mut user_interface = window_ids + .get(&id) + .and_then(|id| interfaces.remove(id)) + .unwrap(); + + let logical_size = state.logical_size(); + + debug.layout_started(); + user_interface = + user_interface.relayout(logical_size, &mut renderer); + debug.layout_finished(); + + debug.draw_started(); + let new_mouse_interaction = { + let state = &state; + + user_interface.draw( + &mut renderer, + state.theme(), + &renderer::Style { + text_color: state.text_color(), + }, + state.cursor_position(), + ) + }; + + let window = window_ids + .get(&id) + .and_then(|id| windows.get(id)) + .unwrap(); + if new_mouse_interaction != mouse_interaction { + window.set_cursor_icon(conversion::mouse_interaction( + new_mouse_interaction, + )); + + mouse_interaction = new_mouse_interaction; + } + debug.draw_finished(); + + let _ = interfaces + .insert(*window_ids.get(&id).unwrap(), user_interface); + + compositor.configure_surface( + surface, + physical_size.width, + physical_size.height, + ); + } + + match compositor.present( + &mut renderer, + surface, + state.viewport(), + state.background_color(), + &debug.overlay(), + ) { + Ok(()) => { + debug.render_finished(); + + // TODO: Handle animations! + // Maybe we can use `ControlFlow::WaitUntil` for this. + } + Err(error) => match error { + // This is an unrecoverable error. + compositor::SurfaceError::OutOfMemory => { + panic!("{:?}", error); + } + _ => { + debug.render_finished(); + + // Try rendering again next frame. + // TODO(derezzedex) + windows + .values() + .next() + .expect("No window found") + .request_redraw(); + } + }, + } + } + event::Event::WindowEvent { + event: window_event, + window_id, + } => { + // dbg!(window_id); + if let Some(window) = + window_ids.get(&window_id).and_then(|id| windows.get(id)) + { + if let Some(state) = window_ids + .get(&window_id) + .and_then(|id| states.get_mut(id)) + { + if requests_exit(&window_event, state.modifiers()) { + if let Some(id) = + window_ids.get(&window_id).cloned() + { + let message = application.close_requested(id); + messages.push(message); + } + } + + state.update(window, &window_event, &mut debug); + + if let Some(event) = conversion::window_event( + *window_ids.get(&window_id).unwrap(), + &window_event, + state.scale_factor(), + state.modifiers(), + ) { + events.push(( + window_ids.get(&window_id).cloned(), + event, + )); + } + } else { + log::error!( + "No window state found for id: {:?}", + window_id + ); + } + } else { + log::error!("No window found with id: {:?}", window_id); + } + } + _ => {} + } + } + + // Manually drop the user interface + // drop(ManuallyDrop::into_inner(user_interface)); +} + +/// Returns true if the provided event should cause an [`Application`] to +/// exit. +pub fn requests_exit( + event: &winit::event::WindowEvent<'_>, + _modifiers: winit::event::ModifiersState, +) -> bool { + use winit::event::WindowEvent; + + match event { + WindowEvent::CloseRequested => true, + #[cfg(target_os = "macos")] + WindowEvent::KeyboardInput { + input: + winit::event::KeyboardInput { + virtual_keycode: Some(winit::event::VirtualKeyCode::Q), + state: winit::event::ElementState::Pressed, + .. + }, + .. + } if _modifiers.logo() => true, + _ => false, + } +} + +/// Builds a [`UserInterface`] for the provided [`Application`], logging +/// [`struct@Debug`] information accordingly. +pub fn build_user_interface<'a, A: Application>( + application: &'a A, + cache: user_interface::Cache, + renderer: &mut A::Renderer, + size: Size, + debug: &mut Debug, + id: window::Id, +) -> UserInterface<'a, A::Message, A::Renderer> +where + <A::Renderer as crate::Renderer>::Theme: StyleSheet, +{ + #[cfg(feature = "trace")] + let view_span = info_span!("Application", "VIEW").entered(); + + debug.view_started(); + let view = application.view(id); + + #[cfg(feature = "trace")] + let _ = view_span.exit(); + debug.view_finished(); + + #[cfg(feature = "trace")] + let layout_span = info_span!("Application", "LAYOUT").entered(); + debug.layout_started(); + let user_interface = UserInterface::build(view, size, cache, renderer); + #[cfg(feature = "trace")] + let _ = layout_span.exit(); + debug.layout_finished(); + + user_interface +} + +/// Updates an [`Application`] by feeding it the provided messages, spawning any +/// resulting [`Command`], and tracking its [`Subscription`]. +pub fn update<A: Application, E: Executor>( + application: &mut A, + caches: &mut HashMap<window::Id, user_interface::Cache>, + states: &HashMap<window::Id, State<A>>, + renderer: &mut A::Renderer, + runtime: &mut Runtime<E, Proxy<Event<A::Message>>, Event<A::Message>>, + clipboard: &mut Clipboard, + proxy: &mut winit::event_loop::EventLoopProxy<Event<A::Message>>, + debug: &mut Debug, + messages: &mut Vec<A::Message>, + windows: &HashMap<window::Id, winit::window::Window>, + graphics_info: impl FnOnce() -> compositor::Information + Copy, +) where + A: Application + 'static, + <A::Renderer as crate::Renderer>::Theme: StyleSheet, +{ + for message in messages.drain(..) { + #[cfg(feature = "trace")] + let update_span = info_span!("Application", "UPDATE").entered(); + + debug.log_message(&message); + + debug.update_started(); + let command = runtime.enter(|| application.update(message)); + #[cfg(feature = "trace")] + let _ = update_span.exit(); + debug.update_finished(); + + run_command( + application, + caches, + states, + renderer, + command, + runtime, + clipboard, + proxy, + debug, + windows, + graphics_info, + ); + } + + let subscription = application.subscription().map(Event::Application); + runtime.track(subscription); +} + +/// Runs the actions of a [`Command`]. +pub fn run_command<A, E>( + application: &A, + caches: &mut HashMap<window::Id, user_interface::Cache>, + states: &HashMap<window::Id, State<A>>, + renderer: &mut A::Renderer, + command: Command<A::Message>, + runtime: &mut Runtime<E, Proxy<Event<A::Message>>, Event<A::Message>>, + clipboard: &mut Clipboard, + proxy: &mut winit::event_loop::EventLoopProxy<Event<A::Message>>, + debug: &mut Debug, + windows: &HashMap<window::Id, winit::window::Window>, + _graphics_info: impl FnOnce() -> compositor::Information + Copy, +) where + A: Application + 'static, + E: Executor, + <A::Renderer as crate::Renderer>::Theme: StyleSheet, +{ + use iced_native::command; + use iced_native::system; + use iced_native::window; + + for action in command.actions() { + match action { + command::Action::Future(future) => { + runtime.spawn(Box::pin(future.map(Event::Application))); + } + command::Action::Clipboard(action) => match action { + clipboard::Action::Read(tag) => { + let message = tag(clipboard.read()); + + proxy + .send_event(Event::Application(message)) + .expect("Send message to event loop"); + } + clipboard::Action::Write(contents) => { + clipboard.write(contents); + } + }, + command::Action::Window(id, action) => match action { + window::Action::Spawn { settings } => { + proxy + .send_event(Event::NewWindow { + id, + settings: settings.into(), + title: application.title(id), + }) + .expect("Send message to event loop"); + } + window::Action::Close => { + proxy + .send_event(Event::CloseWindow(id)) + .expect("Send message to event loop"); + } + window::Action::Drag => { + let window = windows.get(&id).expect("No window found"); + let _res = window.drag_window(); + } + window::Action::Resize { width, height } => { + let window = windows.get(&id).expect("No window found"); + window.set_inner_size(winit::dpi::LogicalSize { + width, + height, + }); + } + window::Action::Move { x, y } => { + let window = windows.get(&id).expect("No window found"); + window.set_outer_position(winit::dpi::LogicalPosition { + x, + y, + }); + } + window::Action::ChangeMode(mode) => { + let window = windows.get(&id).expect("No window found"); + window.set_visible(conversion::visible(mode)); + window.set_fullscreen(conversion::fullscreen( + window.primary_monitor(), + mode, + )); + } + window::Action::FetchMode(tag) => { + let window = windows.get(&id).expect("No window found"); + let mode = if window.is_visible().unwrap_or(true) { + conversion::mode(window.fullscreen()) + } else { + window::Mode::Hidden + }; + + proxy + .send_event(Event::Application(tag(mode))) + .expect("Send message to event loop"); + } + window::Action::Maximize(value) => { + let window = windows.get(&id).expect("No window found!"); + window.set_maximized(value); + } + window::Action::Minimize(value) => { + let window = windows.get(&id).expect("No window found!"); + window.set_minimized(value); + } + window::Action::ToggleMaximize => { + let window = windows.get(&id).expect("No window found!"); + window.set_maximized(!window.is_maximized()); + } + window::Action::ToggleDecorations => { + let window = windows.get(&id).expect("No window found!"); + window.set_decorations(!window.is_decorated()); + } + window::Action::RequestUserAttention(attention_type) => { + let window = windows.get(&id).expect("No window found!"); + window.request_user_attention( + attention_type.map(conversion::user_attention), + ); + } + window::Action::GainFocus => { + let window = windows.get(&id).expect("No window found!"); + window.focus_window(); + } + window::Action::ChangeAlwaysOnTop(on_top) => { + let window = windows.get(&id).expect("No window found!"); + window.set_always_on_top(on_top); + } + window::Action::FetchId(tag) => { + let window = windows.get(&id).expect("No window found!"); + + proxy + .send_event(Event::Application(tag(window.id().into()))) + .expect("Send message to event loop.") + } + }, + command::Action::System(action) => match action { + system::Action::QueryInformation(_tag) => { + #[cfg(feature = "system")] + { + let graphics_info = _graphics_info(); + let proxy = proxy.clone(); + + let _ = std::thread::spawn(move || { + let information = + crate::system::information(graphics_info); + + let message = _tag(information); + + proxy + .send_event(Event::Application(message)) + .expect("Send message to event loop") + }); + } + } + }, + command::Action::Widget(action) => { + let mut current_caches = std::mem::take(caches); + let mut current_operation = Some(action.into_operation()); + + let mut user_interfaces = build_user_interfaces( + application, + renderer, + debug, + states, + current_caches, + ); + + while let Some(mut operation) = current_operation.take() { + for user_interface in user_interfaces.values_mut() { + user_interface.operate(renderer, operation.as_mut()); + + match operation.finish() { + operation::Outcome::None => {} + operation::Outcome::Some(message) => { + proxy + .send_event(Event::Application(message)) + .expect("Send message to event loop"); + } + operation::Outcome::Chain(next) => { + current_operation = Some(next); + } + } + } + } + + let user_interfaces: HashMap<_, _> = user_interfaces + .drain() + .map(|(id, interface)| (id, interface.into_cache())) + .collect(); + + current_caches = user_interfaces; + *caches = current_caches; + } + } + } +} + +/// TODO(derezzedex) +pub fn build_user_interfaces<'a, A>( + application: &'a A, + renderer: &mut A::Renderer, + debug: &mut Debug, + states: &HashMap<window::Id, State<A>>, + mut cached_user_interfaces: HashMap<window::Id, user_interface::Cache>, +) -> HashMap< + window::Id, + UserInterface< + 'a, + <A as Application>::Message, + <A as Application>::Renderer, + >, +> +where + A: Application + 'static, + <A::Renderer as crate::Renderer>::Theme: StyleSheet, +{ + let mut interfaces = HashMap::new(); + + for (id, cache) in cached_user_interfaces.drain() { + let state = &states.get(&id).unwrap(); + + let user_interface = build_user_interface( + application, + cache, + renderer, + state.logical_size(), + debug, + id, + ); + + let _ = interfaces.insert(id, user_interface); + } + + interfaces +} + +#[cfg(not(target_arch = "wasm32"))] +mod platform { + pub fn run<T, F>( + mut event_loop: winit::event_loop::EventLoop<T>, + event_handler: F, + ) -> Result<(), super::Error> + where + F: 'static + + FnMut( + winit::event::Event<'_, T>, + &winit::event_loop::EventLoopWindowTarget<T>, + &mut winit::event_loop::ControlFlow, + ), + { + use winit::platform::run_return::EventLoopExtRunReturn; + + let _ = event_loop.run_return(event_handler); + + Ok(()) + } +} + +#[cfg(target_arch = "wasm32")] +mod platform { + pub fn run<T, F>( + event_loop: winit::event_loop::EventLoop<T>, + event_handler: F, + ) -> ! + where + F: 'static + + FnMut( + winit::event::Event<'_, T>, + &winit::event_loop::EventLoopWindowTarget<T>, + &mut winit::event_loop::ControlFlow, + ), + { + event_loop.run(event_handler) + } +} diff --git a/winit/src/multi_window/state.rs b/winit/src/multi_window/state.rs new file mode 100644 index 00000000..35c69924 --- /dev/null +++ b/winit/src/multi_window/state.rs @@ -0,0 +1,219 @@ +use crate::application::{self, StyleSheet as _}; +use crate::conversion; +use crate::multi_window::Application; +use crate::window; +use crate::{Color, Debug, Point, Size, Viewport}; + +use std::marker::PhantomData; +use winit::event::{Touch, WindowEvent}; +use winit::window::Window; + +/// The state of a windowed [`Application`]. +#[allow(missing_debug_implementations)] +pub struct State<A: Application> +where + <A::Renderer as crate::Renderer>::Theme: application::StyleSheet, +{ + title: String, + scale_factor: f64, + viewport: Viewport, + viewport_changed: bool, + cursor_position: winit::dpi::PhysicalPosition<f64>, + modifiers: winit::event::ModifiersState, + theme: <A::Renderer as crate::Renderer>::Theme, + appearance: application::Appearance, + application: PhantomData<A>, +} + +impl<A: Application> State<A> +where + <A::Renderer as crate::Renderer>::Theme: application::StyleSheet, +{ + /// Creates a new [`State`] for the provided [`Application`]'s window. + pub fn new( + application: &A, + window_id: window::Id, + window: &Window, + ) -> Self { + let title = application.title(window_id); + let scale_factor = application.scale_factor(window_id); + let theme = application.theme(); + let appearance = theme.appearance(&application.style()); + + let viewport = { + let physical_size = window.inner_size(); + + Viewport::with_physical_size( + Size::new(physical_size.width, physical_size.height), + window.scale_factor() * scale_factor, + ) + }; + + Self { + title, + scale_factor, + viewport, + viewport_changed: false, + // TODO: Encode cursor availability in the type-system + cursor_position: winit::dpi::PhysicalPosition::new(-1.0, -1.0), + modifiers: winit::event::ModifiersState::default(), + theme, + appearance, + application: PhantomData, + } + } + + /// Returns the current [`Viewport`] of the [`State`]. + pub fn viewport(&self) -> &Viewport { + &self.viewport + } + + /// Returns whether or not the viewport changed. + pub fn viewport_changed(&self) -> bool { + self.viewport_changed + } + + /// Returns the physical [`Size`] of the [`Viewport`] of the [`State`]. + pub fn physical_size(&self) -> Size<u32> { + self.viewport.physical_size() + } + + /// Returns the logical [`Size`] of the [`Viewport`] of the [`State`]. + pub fn logical_size(&self) -> Size<f32> { + self.viewport.logical_size() + } + + /// Returns the current scale factor of the [`Viewport`] of the [`State`]. + pub fn scale_factor(&self) -> f64 { + self.viewport.scale_factor() + } + + /// Returns the current cursor position of the [`State`]. + pub fn cursor_position(&self) -> Point { + conversion::cursor_position( + self.cursor_position, + self.viewport.scale_factor(), + ) + } + + /// Returns the current keyboard modifiers of the [`State`]. + pub fn modifiers(&self) -> winit::event::ModifiersState { + self.modifiers + } + + /// Returns the current theme of the [`State`]. + pub fn theme(&self) -> &<A::Renderer as crate::Renderer>::Theme { + &self.theme + } + + /// Returns the current background [`Color`] of the [`State`]. + pub fn background_color(&self) -> Color { + self.appearance.background_color + } + + /// Returns the current text [`Color`] of the [`State`]. + pub fn text_color(&self) -> Color { + self.appearance.text_color + } + + /// Processes the provided window event and updates the [`State`] + /// accordingly. + pub fn update( + &mut self, + window: &Window, + event: &WindowEvent<'_>, + _debug: &mut Debug, + ) { + match event { + WindowEvent::Resized(new_size) => { + let size = Size::new(new_size.width, new_size.height); + + self.viewport = Viewport::with_physical_size( + size, + window.scale_factor() * self.scale_factor, + ); + + self.viewport_changed = true; + } + WindowEvent::ScaleFactorChanged { + scale_factor: new_scale_factor, + new_inner_size, + } => { + let size = + Size::new(new_inner_size.width, new_inner_size.height); + + self.viewport = Viewport::with_physical_size( + size, + new_scale_factor * self.scale_factor, + ); + + self.viewport_changed = true; + } + WindowEvent::CursorMoved { position, .. } + | WindowEvent::Touch(Touch { + location: position, .. + }) => { + self.cursor_position = *position; + } + WindowEvent::CursorLeft { .. } => { + // TODO: Encode cursor availability in the type-system + self.cursor_position = + winit::dpi::PhysicalPosition::new(-1.0, -1.0); + } + WindowEvent::ModifiersChanged(new_modifiers) => { + self.modifiers = *new_modifiers; + } + #[cfg(feature = "debug")] + WindowEvent::KeyboardInput { + input: + winit::event::KeyboardInput { + virtual_keycode: Some(winit::event::VirtualKeyCode::F12), + state: winit::event::ElementState::Pressed, + .. + }, + .. + } => _debug.toggle(), + _ => {} + } + } + + /// Synchronizes the [`State`] with its [`Application`] and its respective + /// window. + /// + /// Normally an [`Application`] should be synchronized with its [`State`] + /// and window after calling [`Application::update`]. + /// + /// [`Application::update`]: crate::Program::update + pub fn synchronize( + &mut self, + application: &A, + window_id: window::Id, + window: &Window, + ) { + // Update window title + let new_title = application.title(window_id); + + if self.title != new_title { + window.set_title(&new_title); + self.title = new_title; + } + + // Update scale factor + let new_scale_factor = application.scale_factor(window_id); + + if self.scale_factor != new_scale_factor { + let size = window.inner_size(); + + self.viewport = Viewport::with_physical_size( + Size::new(size.width, size.height), + window.scale_factor() * new_scale_factor, + ); + + self.scale_factor = new_scale_factor; + } + + // Update theme and appearance + self.theme = application.theme(); + self.appearance = self.theme.appearance(&application.style()); + } +} diff --git a/winit/src/application/profiler.rs b/winit/src/profiler.rs index 7031507a..ff9bbdc0 100644 --- a/winit/src/application/profiler.rs +++ b/winit/src/profiler.rs @@ -21,6 +21,7 @@ pub struct Profiler { impl Profiler { /// Initializes the [`Profiler`]. pub fn init() -> Self { + log::info!("Capturing trace.."); // Registry stores the spans & generates unique span IDs let subscriber = Registry::default(); diff --git a/winit/src/settings.rs b/winit/src/settings.rs index 45f38833..2f73aff6 100644 --- a/winit/src/settings.rs +++ b/winit/src/settings.rs @@ -22,6 +22,7 @@ mod platform; pub use platform::PlatformSpecific; use crate::conversion; +use crate::Icon; use crate::Position; use winit::monitor::MonitorHandle; use winit::window::WindowBuilder; @@ -46,6 +47,10 @@ pub struct Settings<Flags> { /// Whether the [`Application`] should exit when the user requests the /// window to close (e.g. the user presses the close button). /// + /// NOTE: This is not used for `multi-window`, instead check [`Application::close_requested`]. + /// + /// [`close_requested`]: crate::multi_window::Application::close_requested + /// /// [`Application`]: crate::Application pub exit_on_close_request: bool, @@ -153,9 +158,9 @@ impl Window { { use winit::platform::windows::WindowBuilderExtWindows; - if let Some(parent) = self.platform_specific.parent { - window_builder = window_builder.with_parent_window(parent); - } + // if let Some(parent) = self.platform_specific.parent { + // window_builder = window_builder.with_parent_window(parent); + // } window_builder = window_builder .with_drag_and_drop(self.platform_specific.drag_and_drop); @@ -196,3 +201,23 @@ impl Default for Window { } } } + +impl From<iced_native::window::Settings> for Window { + fn from(settings: iced_native::window::Settings) -> Self { + Self { + size: settings.size, + position: Position::from(settings.position), + min_size: settings.min_size, + max_size: settings.max_size, + visible: settings.visible, + resizable: settings.resizable, + decorations: settings.decorations, + transparent: settings.transparent, + always_on_top: settings.always_on_top, + icon: settings.icon.and_then(|icon| { + Icon::try_from(icon).map(winit::window::Icon::from).ok() + }), + platform_specific: Default::default(), + } + } +} diff --git a/winit/src/settings/windows.rs b/winit/src/settings/windows.rs index ff03a9c5..0891ec2c 100644 --- a/winit/src/settings/windows.rs +++ b/winit/src/settings/windows.rs @@ -4,7 +4,7 @@ #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct PlatformSpecific { /// Parent window - pub parent: Option<winit::platform::windows::HWND>, + // pub parent: Option<winit::platform::windows::HWND>, /// Drag and drop support pub drag_and_drop: bool, @@ -13,7 +13,7 @@ pub struct PlatformSpecific { impl Default for PlatformSpecific { fn default() -> Self { Self { - parent: None, + // parent: None, drag_and_drop: true, } } diff --git a/winit/src/window.rs b/winit/src/window.rs index 961562bd..8fd415ef 100644 --- a/winit/src/window.rs +++ b/winit/src/window.rs @@ -2,67 +2,86 @@ use crate::command::{self, Command}; use iced_native::window; -pub use window::{frames, Event, Mode, RedrawRequest, UserAttention}; +pub use window::{Event, Id, Mode, RedrawRequest, frames, UserAttention}; -/// Closes the current window and exits the application. -pub fn close<Message>() -> Command<Message> { - Command::single(command::Action::Window(window::Action::Close)) +/// Closes the window. +pub fn close<Message>(id: window::Id) -> Command<Message> { + Command::single(command::Action::Window(id, window::Action::Close)) } /// Begins dragging the window while the left mouse button is held. -pub fn drag<Message>() -> Command<Message> { - Command::single(command::Action::Window(window::Action::Drag)) +pub fn drag<Message>(id: window::Id) -> Command<Message> { + Command::single(command::Action::Window(id, window::Action::Drag)) +} + +/// Spawns a new window. +pub fn spawn<Message>( + id: window::Id, + settings: window::Settings, +) -> Command<Message> { + Command::single(command::Action::Window( + id, + window::Action::Spawn { settings }, + )) } /// Resizes the window to the given logical dimensions. -pub fn resize<Message>(width: u32, height: u32) -> Command<Message> { - Command::single(command::Action::Window(window::Action::Resize { - width, - height, - })) +pub fn resize<Message>( + id: window::Id, + width: u32, + height: u32, +) -> Command<Message> { + Command::single(command::Action::Window( + id, + window::Action::Resize { width, height }, + )) } /// Maximizes the window. -pub fn maximize<Message>(maximized: bool) -> Command<Message> { - Command::single(command::Action::Window(window::Action::Maximize( - maximized, - ))) +pub fn maximize<Message>(id: window::Id, maximized: bool) -> Command<Message> { + Command::single(command::Action::Window( + id, + window::Action::Maximize(maximized), + )) } /// Minimes the window. -pub fn minimize<Message>(minimized: bool) -> Command<Message> { - Command::single(command::Action::Window(window::Action::Minimize( - minimized, - ))) +pub fn minimize<Message>(id: window::Id, minimized: bool) -> Command<Message> { + Command::single(command::Action::Window( + id, + window::Action::Minimize(minimized), + )) } /// Moves a window to the given logical coordinates. -pub fn move_to<Message>(x: i32, y: i32) -> Command<Message> { - Command::single(command::Action::Window(window::Action::Move { x, y })) +pub fn move_to<Message>(id: window::Id, x: i32, y: i32) -> Command<Message> { + Command::single(command::Action::Window(id, window::Action::Move { x, y })) } -/// Sets the [`Mode`] of the window. -pub fn change_mode<Message>(mode: Mode) -> Command<Message> { - Command::single(command::Action::Window(window::Action::ChangeMode(mode))) +/// Changes the [`Mode`] of the window. +pub fn change_mode<Message>(id: window::Id, mode: Mode) -> Command<Message> { + Command::single(command::Action::Window(id, window::Action::ChangeMode(mode))) } /// Fetches the current [`Mode`] of the window. pub fn fetch_mode<Message>( + id: window::Id, f: impl FnOnce(Mode) -> Message + 'static, ) -> Command<Message> { - Command::single(command::Action::Window(window::Action::FetchMode( - Box::new(f), - ))) + Command::single(command::Action::Window( + id, + window::Action::FetchMode(Box::new(f)), + )) } /// Toggles the window to maximized or back. -pub fn toggle_maximize<Message>() -> Command<Message> { - Command::single(command::Action::Window(window::Action::ToggleMaximize)) +pub fn toggle_maximize<Message>(id: window::Id) -> Command<Message> { + Command::single(command::Action::Window(id, window::Action::ToggleMaximize)) } /// Toggles the window decorations. -pub fn toggle_decorations<Message>() -> Command<Message> { - Command::single(command::Action::Window(window::Action::ToggleDecorations)) +pub fn toggle_decorations<Message>(id: window::Id) -> Command<Message> { + Command::single(command::Action::Window(id, window::Action::ToggleDecorations)) } /// Request user attention to the window, this has no effect if the application @@ -72,9 +91,11 @@ pub fn toggle_decorations<Message>() -> Command<Message> { /// Providing `None` will unset the request for user attention. Unsetting the request for /// user attention might not be done automatically by the WM when the window receives input. pub fn request_user_attention<Message>( + id: window::Id, user_attention: Option<UserAttention>, ) -> Command<Message> { Command::single(command::Action::Window( + id, window::Action::RequestUserAttention(user_attention), )) } @@ -85,22 +106,23 @@ pub fn request_user_attention<Message>( /// This [`Command`] steals input focus from other applications. Do not use this method unless /// you are certain that's what the user wants. Focus stealing can cause an extremely disruptive /// user experience. -pub fn gain_focus<Message>() -> Command<Message> { - Command::single(command::Action::Window(window::Action::GainFocus)) +pub fn gain_focus<Message>(id: window::Id) -> Command<Message> { + Command::single(command::Action::Window(id, window::Action::GainFocus)) } /// Changes whether or not the window will always be on top of other windows. -pub fn change_always_on_top<Message>(on_top: bool) -> Command<Message> { - Command::single(command::Action::Window(window::Action::ChangeAlwaysOnTop( +pub fn change_always_on_top<Message>(id: window::Id, on_top: bool) -> Command<Message> { + Command::single(command::Action::Window(id, window::Action::ChangeAlwaysOnTop( on_top, ))) } /// Fetches an identifier unique to the window. pub fn fetch_id<Message>( + id: window::Id, f: impl FnOnce(u64) -> Message + 'static, ) -> Command<Message> { - Command::single(command::Action::Window(window::Action::FetchId(Box::new( + Command::single(command::Action::Window(id, window::Action::FetchId(Box::new( f, )))) } |