summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Bingus <shankern@protonmail.com>2023-03-13 14:16:45 -0700
committerLibravatar Bingus <shankern@protonmail.com>2023-03-13 14:16:45 -0700
commit8ba18430800142965549077373e2a45d0a3429a1 (patch)
tree0ba36d187e3c66e88476779dc40ba3cf0b28bff0
parentfa068b904a904c86195ebfaa4e953466426a27aa (diff)
downloadiced-8ba18430800142965549077373e2a45d0a3429a1.tar.gz
iced-8ba18430800142965549077373e2a45d0a3429a1.tar.bz2
iced-8ba18430800142965549077373e2a45d0a3429a1.zip
Code cleanup, clearer comments + removed some unnecessary dupe;
Removed `Frames` struct return for `window::frames()` since we are just redrawing every window anyways; Interface dropping;
-rw-r--r--Cargo.toml4
-rw-r--r--examples/multi_window/Cargo.toml2
-rw-r--r--examples/solar_system/src/main.rs2
-rw-r--r--native/src/window.rs15
-rw-r--r--src/lib.rs2
-rw-r--r--src/multi_window/application.rs5
-rw-r--r--winit/Cargo.toml2
-rw-r--r--winit/src/application.rs2
-rw-r--r--winit/src/lib.rs2
-rw-r--r--winit/src/multi_window.rs126
-rw-r--r--winit/src/multi_window/state.rs2
11 files changed, 60 insertions, 104 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 5276d921..7f89b05e 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -46,8 +46,8 @@ chrome-trace = [
"iced_wgpu?/tracing",
"iced_glow?/tracing",
]
-# Enables experimental multi-window support for iced_winit
-multi_window = ["iced_winit/multi_window"]
+# Enables experimental multi-window support for iced_winit + wgpu.
+multi-window = ["iced_winit/multi-window"]
[badges]
maintenance = { status = "actively-developed" }
diff --git a/examples/multi_window/Cargo.toml b/examples/multi_window/Cargo.toml
index 0bb83f37..a59a0e68 100644
--- a/examples/multi_window/Cargo.toml
+++ b/examples/multi_window/Cargo.toml
@@ -7,7 +7,7 @@ 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", "tokio"] }
+iced = { path = "../..", features = ["debug", "multi-window", "tokio"] }
env_logger = "0.10.0"
iced_native = { path = "../../native" }
iced_lazy = { path = "../../lazy" }
diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs
index eb461bb0..9a4ee754 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(|frame| Message::Tick(frame.at))
+ window::frames().map(Message::Tick)
}
}
diff --git a/native/src/window.rs b/native/src/window.rs
index a8f8b10f..660cd54f 100644
--- a/native/src/window.rs
+++ b/native/src/window.rs
@@ -30,20 +30,9 @@ 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<Frame> {
+pub fn frames() -> Subscription<Instant> {
subscription::raw_events(|event, _status| match event {
- crate::Event::Window(id, Event::RedrawRequested(at)) => {
- Some(Frame { id, at })
- }
+ crate::Event::Window(_, Event::RedrawRequested(at)) => Some(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/src/lib.rs b/src/lib.rs
index 65fe3b93..e7481c77 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -182,7 +182,7 @@ pub mod touch;
pub mod widget;
pub mod window;
-#[cfg(all(not(feature = "glow"), feature = "multi_window"))]
+#[cfg(all(not(feature = "glow"), feature = "multi-window"))]
pub mod multi_window;
#[cfg(all(not(feature = "glow"), feature = "wgpu"))]
diff --git a/src/multi_window/application.rs b/src/multi_window/application.rs
index d0b515ab..1fb4bcd4 100644
--- a/src/multi_window/application.rs
+++ b/src/multi_window/application.rs
@@ -139,7 +139,7 @@ pub trait Application: Sized {
window: window::Id,
) -> Element<'_, Self::Message, crate::Renderer<Self::Theme>>;
- /// Returns the scale factor of the [`Application`].
+ /// Returns the scale factor of the `window` of the [`Application`].
///
/// It can be used to dynamically control the size of the UI at runtime
/// (i.e. zooming).
@@ -160,7 +160,8 @@ pub trait Application: Sized {
false
}
- /// Requests that the [`window`] be closed.
+ /// Returns the `Self::Message` that should be processed when a `window` is requested to
+ /// be closed.
fn close_requested(&self, window: window::Id) -> Self::Message;
/// Runs the [`Application`].
diff --git a/winit/Cargo.toml b/winit/Cargo.toml
index 7de69528..9efd1890 100644
--- a/winit/Cargo.toml
+++ b/winit/Cargo.toml
@@ -16,7 +16,7 @@ chrome-trace = ["trace", "tracing-chrome"]
debug = ["iced_native/debug"]
system = ["sysinfo"]
application = []
-multi_window = []
+multi-window = []
[dependencies]
window_clipboard = "0.2"
diff --git a/winit/src/application.rs b/winit/src/application.rs
index 906da3c6..fe97486f 100644
--- a/winit/src/application.rs
+++ b/winit/src/application.rs
@@ -743,7 +743,7 @@ pub fn run_command<A, E>(
}
window::Action::Spawn { .. } => {
log::info!(
- "This is only available on `multi_window::Application`"
+ "Spawning a window is only available with `multi_window::Application`s."
)
}
window::Action::Resize { width, height } => {
diff --git a/winit/src/lib.rs b/winit/src/lib.rs
index fe5768df..54b9c31f 100644
--- a/winit/src/lib.rs
+++ b/winit/src/lib.rs
@@ -35,7 +35,7 @@
pub use iced_native::*;
pub use winit;
-#[cfg(feature = "multi_window")]
+#[cfg(feature = "multi-window")]
pub mod multi_window;
#[cfg(feature = "application")]
diff --git a/winit/src/multi_window.rs b/winit/src/multi_window.rs
index eac8b260..d5da406c 100644
--- a/winit/src/multi_window.rs
+++ b/winit/src/multi_window.rs
@@ -33,7 +33,7 @@ use tracing::{info_span, instrument::Instrument};
/// This is a wrapper around the `Application::Message` associate type
/// to allows the `shell` to create internal messages, while still having
-/// the current user specified custom messages.
+/// the current user-specified custom messages.
#[derive(Debug)]
pub enum Event<Message> {
/// An [`Application`] generated message
@@ -53,9 +53,9 @@ pub enum Event<Message> {
WindowCreated(window::Id, winit::window::Window),
}
-/// An interactive, native cross-platform application.
+/// An interactive, native, cross-platform, multi-windowed application.
///
-/// This trait is the main entrypoint of Iced. Once implemented, you can run
+/// This trait is the main entrypoint of multi-window Iced. Once implemented, you can run
/// your GUI application by simply calling [`run`]. It will run in
/// its own window.
///
@@ -105,7 +105,7 @@ where
/// 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 each [`Application`] window.
+ /// Returns the current title of each window of the [`Application`].
///
/// This title can be dynamic! The runtime will automatically update the
/// title of your application when necessary.
@@ -155,7 +155,8 @@ where
false
}
- /// Requests that the [`window`] be closed.
+ /// Returns the `Self::Message` that should be processed when a `window` is requested to
+ /// be closed.
fn close_requested(&self, window: window::Id) -> Self::Message;
}
@@ -462,9 +463,9 @@ async fn run_instance<A, E, C>(
}
debug.event_processing_finished();
- // Update application with app message(s)
- // Note: without tying an app message to a window ID, we must redraw all windows
- // as we cannot know what changed without some kind of damage tracking.
+ // Update application with app messages
+ // Unless we implement some kind of diffing, we must redraw all windows as we
+ // cannot know what changed.
if !messages.is_empty()
|| matches!(
interface_state,
@@ -612,9 +613,7 @@ async fn run_instance<A, E, C>(
}
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(
@@ -776,14 +775,12 @@ async fn run_instance<A, E, C>(
}
_ => {
debug.render_finished();
+ log::error!("Error {error:?} when presenting surface.");
- // Try rendering again next frame.
- // TODO(derezzedex)
- windows
- .values()
- .next()
- .expect("No window found")
- .request_redraw();
+ // Try rendering windows again next frame.
+ for window in windows.values() {
+ window.request_redraw();
+ }
}
},
}
@@ -792,80 +789,45 @@ async fn run_instance<A, E, C>(
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
+ if let (Some(window), Some(state)) = (
+ window_ids.get(&window_id).and_then(|id| windows.get(id)),
+ 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);
- }
+ .and_then(|id| states.get_mut(id)),
+ ) {
+ if crate::application::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
- );
+ 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 found with id: {:?}", window_id);
+ log::error!(
+ "Could not find window or state for 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,
- }
+ // Manually drop the user interfaces
+ drop(ManuallyDrop::into_inner(interfaces));
}
-/// Builds a [`UserInterface`] for the provided [`Application`], logging
-/// [`struct@Debug`] information accordingly.
+/// Builds a window's [`UserInterface`] for the [`Application`].
pub fn build_user_interface<'a, A: Application>(
application: &'a A,
cache: user_interface::Cache,
@@ -890,7 +852,9 @@ where
#[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();
@@ -898,7 +862,7 @@ where
user_interface
}
-/// Updates an [`Application`] by feeding it the provided messages, spawning any
+/// Updates an [`Application`] by feeding it messages, spawning any
/// resulting [`Command`], and tracking its [`Subscription`].
pub fn update<A: Application, E: Executor>(
application: &mut A,
@@ -923,7 +887,9 @@ pub fn update<A: Application, E: Executor>(
debug.log_message(&message);
debug.update_started();
+
let command = runtime.enter(|| application.update(message));
+
#[cfg(feature = "trace")]
let _ = update_span.exit();
debug.update_finished();
@@ -1023,7 +989,7 @@ pub fn run_command<A, E>(
let window = windows.get(&id).expect("No window found");
window.set_visible(conversion::visible(mode));
window.set_fullscreen(conversion::fullscreen(
- window.primary_monitor(),
+ window.current_monitor(),
mode,
));
}
diff --git a/winit/src/multi_window/state.rs b/winit/src/multi_window/state.rs
index a7e65de7..d0e442d0 100644
--- a/winit/src/multi_window/state.rs
+++ b/winit/src/multi_window/state.rs
@@ -179,7 +179,7 @@ where
/// Synchronizes the [`State`] with its [`Application`] and its respective
/// window.
///
- /// Normally an [`Application`] should be synchronized with its [`State`]
+ /// Normally, an [`Application`] should be synchronized with its [`State`]
/// and window after calling [`Application::update`].
///
/// [`Application::update`]: crate::Program::update