diff options
author | 2024-03-24 11:13:45 +0900 | |
---|---|---|
committer | 2024-03-24 11:13:45 +0900 | |
commit | 4334e63ba1dd88b367f3b7f2790b7869d11d12c0 (patch) | |
tree | 7a02d84426a54a5a5283f5ad101cc803442998fd | |
parent | f3a1c785b2743e9c48c3d28df0c6772ce579d7c8 (diff) | |
parent | 3013463baa71504488a20436beb3db87ecb66df0 (diff) | |
download | iced-4334e63ba1dd88b367f3b7f2790b7869d11d12c0.tar.gz iced-4334e63ba1dd88b367f3b7f2790b7869d11d12c0.tar.bz2 iced-4334e63ba1dd88b367f3b7f2790b7869d11d12c0.zip |
Merge branch 'iced-rs:master' into viewer_content_fit
-rw-r--r-- | Cargo.toml | 8 | ||||
-rw-r--r-- | core/Cargo.toml | 6 | ||||
-rw-r--r-- | core/src/theme.rs | 25 | ||||
-rw-r--r-- | core/src/window/settings/wasm.rs | 12 | ||||
-rw-r--r-- | wgpu/src/image.rs | 12 | ||||
-rw-r--r-- | wgpu/src/window/compositor.rs | 4 | ||||
-rw-r--r-- | widget/src/lazy/component.rs | 2 | ||||
-rw-r--r-- | widget/src/lazy/responsive.rs | 45 |
8 files changed, 96 insertions, 18 deletions
@@ -18,7 +18,7 @@ all-features = true maintenance = { status = "actively-developed" } [features] -default = ["wgpu", "fira-sans"] +default = ["wgpu", "fira-sans", "auto-detect-theme"] # Enable the `wgpu` GPU-accelerated renderer backend wgpu = ["iced_renderer/wgpu", "iced_widget/wgpu"] # Enables the `Image` widget @@ -53,8 +53,11 @@ multi-window = ["iced_winit/multi-window"] advanced = [] # Enables embedding Fira Sans as the default font on Wasm builds fira-sans = ["iced_renderer/fira-sans"] +# Enables auto-detecting light/dark mode for the built-in theme +auto-detect-theme = ["iced_core/auto-detect-theme"] [dependencies] +iced_core.workspace = true iced_futures.workspace = true iced_renderer.workspace = true iced_widget.workspace = true @@ -121,6 +124,7 @@ async-std = "1.0" bitflags = "2.0" bytemuck = { version = "1.0", features = ["derive"] } cosmic-text = "0.10" +dark-light = "1.0" futures = "0.3" glam = "0.25" glyphon = "0.5" @@ -158,4 +162,4 @@ web-time = "0.2" wgpu = "0.19" winapi = "0.3" window_clipboard = "0.4.1" -winit = { git = "https://github.com/iced-rs/winit.git", rev = "b91e39ece2c0d378c3b80da7f3ab50e17bb798a5" } +winit = { git = "https://github.com/iced-rs/winit.git", rev = "592bd152f6d5786fae7d918532d7db752c0d164f" } diff --git a/core/Cargo.toml b/core/Cargo.toml index c273fcb4..29a95ad7 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -10,6 +10,9 @@ homepage.workspace = true categories.workspace = true keywords.workspace = true +[features] +auto-detect-theme = ["dep:dark-light"] + [dependencies] bitflags.workspace = true glam.workspace = true @@ -22,6 +25,9 @@ thiserror.workspace = true web-time.workspace = true xxhash-rust.workspace = true +dark-light.workspace = true +dark-light.optional = true + [target.'cfg(windows)'.dependencies] raw-window-handle.workspace = true diff --git a/core/src/theme.rs b/core/src/theme.rs index 948aaf83..6b2c04da 100644 --- a/core/src/theme.rs +++ b/core/src/theme.rs @@ -7,10 +7,9 @@ use std::fmt; use std::sync::Arc; /// A built-in theme. -#[derive(Debug, Clone, PartialEq, Default)] +#[derive(Debug, Clone, PartialEq)] pub enum Theme { /// The built-in light variant. - #[default] Light, /// The built-in dark variant. Dark, @@ -161,6 +160,28 @@ impl Theme { } } +impl Default for Theme { + fn default() -> Self { + #[cfg(feature = "auto-detect-theme")] + { + use once_cell::sync::Lazy; + + static DEFAULT: Lazy<Theme> = + Lazy::new(|| match dark_light::detect() { + dark_light::Mode::Dark => Theme::Dark, + dark_light::Mode::Light | dark_light::Mode::Default => { + Theme::Light + } + }); + + DEFAULT.clone() + } + + #[cfg(not(feature = "auto-detect-theme"))] + Theme::Light + } +} + impl fmt::Display for Theme { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { diff --git a/core/src/window/settings/wasm.rs b/core/src/window/settings/wasm.rs index 8e0f1bbc..30e60b6a 100644 --- a/core/src/window/settings/wasm.rs +++ b/core/src/window/settings/wasm.rs @@ -1,11 +1,21 @@ //! Platform specific settings for WebAssembly. /// The platform specific window settings of an application. -#[derive(Debug, Clone, PartialEq, Eq, Default)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct PlatformSpecific { /// The identifier of a DOM element that will be replaced with the /// application. /// /// If set to `None`, the application will be appended to the HTML body. + /// + /// By default, it is set to `"iced"`. pub target: Option<String>, } + +impl Default for PlatformSpecific { + fn default() -> Self { + Self { + target: Some(String::from("iced")), + } + } +} diff --git a/wgpu/src/image.rs b/wgpu/src/image.rs index c8e4a4c2..067b77ab 100644 --- a/wgpu/src/image.rs +++ b/wgpu/src/image.rs @@ -161,13 +161,21 @@ impl Data { queue: &wgpu::Queue, instances: &[Instance], ) { + self.instance_count = instances.len(); + + if self.instance_count == 0 { + return; + } + let _ = self.instances.resize(device, instances.len()); let _ = self.instances.write(queue, 0, instances); - - self.instance_count = instances.len(); } fn render<'a>(&'a self, render_pass: &mut wgpu::RenderPass<'a>) { + if self.instance_count == 0 { + return; + } + render_pass.set_bind_group(0, &self.constants, &[]); render_pass.set_vertex_buffer(0, self.instances.slice(..)); diff --git a/wgpu/src/window/compositor.rs b/wgpu/src/window/compositor.rs index 328ad781..fa6b9373 100644 --- a/wgpu/src/window/compositor.rs +++ b/wgpu/src/window/compositor.rs @@ -92,6 +92,10 @@ impl Compositor { .contains(&wgpu::CompositeAlphaMode::PostMultiplied) { wgpu::CompositeAlphaMode::PostMultiplied + } else if alpha_modes + .contains(&wgpu::CompositeAlphaMode::PreMultiplied) + { + wgpu::CompositeAlphaMode::PreMultiplied } else { wgpu::CompositeAlphaMode::Auto }; diff --git a/widget/src/lazy/component.rs b/widget/src/lazy/component.rs index a512e0de..7ba71a02 100644 --- a/widget/src/lazy/component.rs +++ b/widget/src/lazy/component.rs @@ -478,12 +478,14 @@ where translation: Vector, ) -> Option<overlay::Element<'b, Message, Theme, Renderer>> { self.rebuild_element_if_necessary(); + let tree = tree .state .downcast_mut::<Rc<RefCell<Option<Tree>>>>() .borrow_mut() .take() .unwrap(); + let overlay = Overlay(Some( InnerBuilder { instance: self, diff --git a/widget/src/lazy/responsive.rs b/widget/src/lazy/responsive.rs index 313e1edb..f612102e 100644 --- a/widget/src/lazy/responsive.rs +++ b/widget/src/lazy/responsive.rs @@ -308,10 +308,13 @@ where content_layout_node.as_ref().unwrap(), ); - element - .as_widget_mut() - .overlay(tree, content_layout, renderer, translation) - .map(|overlay| RefCell::new(Nested::new(overlay))) + ( + element + .as_widget_mut() + .overlay(tree, content_layout, renderer, translation) + .map(|overlay| RefCell::new(Nested::new(overlay))), + content_layout_node, + ) }, } .build(); @@ -341,7 +344,10 @@ struct Overlay<'a, 'b, Message, Theme, Renderer> { #[borrows(mut content, mut tree)] #[not_covariant] - overlay: Option<RefCell<Nested<'this, Message, Theme, Renderer>>>, + overlay: ( + Option<RefCell<Nested<'this, Message, Theme, Renderer>>>, + &'this mut Option<layout::Node>, + ), } impl<'a, 'b, Message, Theme, Renderer> @@ -351,7 +357,7 @@ impl<'a, 'b, Message, Theme, Renderer> &self, f: impl FnOnce(&mut Nested<'_, Message, Theme, Renderer>) -> T, ) -> Option<T> { - self.with_overlay(|overlay| { + self.with_overlay(|(overlay, _layout)| { overlay.as_ref().map(|nested| (f)(&mut nested.borrow_mut())) }) } @@ -360,7 +366,7 @@ impl<'a, 'b, Message, Theme, Renderer> &mut self, f: impl FnOnce(&mut Nested<'_, Message, Theme, Renderer>) -> T, ) -> Option<T> { - self.with_overlay_mut(|overlay| { + self.with_overlay_mut(|(overlay, _layout)| { overlay.as_mut().map(|nested| (f)(nested.get_mut())) }) } @@ -412,10 +418,27 @@ where clipboard: &mut dyn Clipboard, shell: &mut Shell<'_, Message>, ) -> event::Status { - self.with_overlay_mut_maybe(|overlay| { - overlay.on_event(event, layout, cursor, renderer, clipboard, shell) - }) - .unwrap_or(event::Status::Ignored) + let mut is_layout_invalid = false; + + let event_status = self + .with_overlay_mut_maybe(|overlay| { + let event_status = overlay.on_event( + event, layout, cursor, renderer, clipboard, shell, + ); + + is_layout_invalid = shell.is_layout_invalid(); + + event_status + }) + .unwrap_or(event::Status::Ignored); + + if is_layout_invalid { + self.with_overlay_mut(|(_overlay, layout)| { + **layout = None; + }); + } + + event_status } fn is_over( |