public inbox for yew-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Dominik Csapak <d.csapak@proxmox.com>
To: yew-devel@lists.proxmox.com
Subject: [yew-devel] [PATCH yew-widget-toolkit v2 01/12] widget: implement Image widget
Date: Mon, 30 Jun 2025 10:24:58 +0200	[thread overview]
Message-ID: <20250630082509.1202308-9-d.csapak@proxmox.com> (raw)
In-Reply-To: <20250630082509.1202308-1-d.csapak@proxmox.com>

It's a thin wrapper around the <img> tag, but provide convenience
properties for switching the image in dark mode, like we want to use for
our logo for example.

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 src/widget/image.rs | 76 +++++++++++++++++++++++++++++++++++++++++++++
 src/widget/mod.rs   |  3 ++
 2 files changed, 79 insertions(+)
 create mode 100644 src/widget/image.rs

diff --git a/src/widget/image.rs b/src/widget/image.rs
new file mode 100644
index 0000000..002e879
--- /dev/null
+++ b/src/widget/image.rs
@@ -0,0 +1,76 @@
+use yew::html::IntoPropValue;
+use yew::prelude::*;
+use yew::AttrValue;
+
+use crate::props::{EventSubscriber, WidgetBuilder};
+use crate::state::Theme;
+use crate::state::ThemeObserver;
+use crate::widget::Container;
+
+use pwt_macros::{builder, widget};
+
+/// Image
+///
+/// An image element. Has convenience options to change the image in dark mode.
+#[widget(pwt=crate, comp=PwtImage, @element)]
+#[derive(Properties, PartialEq, Clone)]
+#[builder]
+pub struct Image {
+    /// The source url for the image.
+    pub src: AttrValue,
+
+    /// An alternative source url for the image used in dark mode.
+    #[prop_or_default]
+    #[builder(IntoPropValue, into_prop_value)]
+    pub dark_mode_src: Option<AttrValue>,
+}
+
+impl Image {
+    /// Create a new [Image] with a src image url.
+    pub fn new(src: impl Into<AttrValue>) -> Self {
+        yew::props!(Self { src: src.into() })
+    }
+}
+
+pub enum Msg {
+    ThemeChanged((Theme, bool)),
+}
+
+#[doc(hidden)]
+pub struct PwtImage {
+    dark_mode: bool,
+    _theme_observer: ThemeObserver,
+}
+
+impl Component for PwtImage {
+    type Message = Msg;
+    type Properties = Image;
+
+    fn create(_ctx: &Context<Self>) -> Self {
+        let _theme_observer = ThemeObserver::new(_ctx.link().callback(Msg::ThemeChanged));
+        Self {
+            dark_mode: _theme_observer.dark_mode(),
+            _theme_observer,
+        }
+    }
+
+    fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
+        match msg {
+            Msg::ThemeChanged((_theme, dark_mode)) => self.dark_mode = dark_mode,
+        }
+        true
+    }
+
+    fn view(&self, ctx: &Context<Self>) -> Html {
+        let props = ctx.props();
+        let src = match (self.dark_mode, &props.dark_mode_src) {
+            (true, Some(src)) => src.clone(),
+            _ => props.src.clone(),
+        };
+        Container::from_tag("img")
+            .with_std_props(&props.std_props)
+            .listeners(&props.listeners)
+            .attribute("src", src)
+            .into()
+    }
+}
diff --git a/src/widget/mod.rs b/src/widget/mod.rs
index 18c52ad..2385bb7 100644
--- a/src/widget/mod.rs
+++ b/src/widget/mod.rs
@@ -65,6 +65,9 @@ pub use field_label::FieldLabel;
 #[doc(hidden)]
 pub use field_label::PwtFieldLabel;
 
+mod image;
+pub use image::Image;
+
 mod input;
 pub use input::Input;
 
-- 
2.39.5



_______________________________________________
yew-devel mailing list
yew-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/yew-devel


  parent reply	other threads:[~2025-06-30  8:24 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-30  8:24 [yew-devel] [PATCH yew-widget-toolkit/yew-widget-toolkit-assets v2 00/19] various touch improvements Dominik Csapak
2025-06-30  8:24 ` [yew-devel] [PATCH yew-widget-toolkit-assets v2 1/7] reset: remove the tap highlight color for chrome Dominik Csapak
2025-06-30  8:24 ` [yew-devel] [PATCH yew-widget-toolkit-assets v2 2/7] page: add more page animation styles Dominik Csapak
2025-06-30  8:24 ` [yew-devel] [PATCH yew-widget-toolkit-assets v2 3/7] page: animations: reverse the direction on X axis for RTL mode Dominik Csapak
2025-06-30  8:24 ` [yew-devel] [PATCH yew-widget-toolkit-assets v2 4/7] application bar: reduce horizontal padding Dominik Csapak
2025-06-30  8:24 ` [yew-devel] [PATCH yew-widget-toolkit-assets v2 5/7] application bar: reverse back arrow for rtl Dominik Csapak
2025-06-30  8:24 ` [yew-devel] [PATCH yew-widget-toolkit-assets v2 6/7] buttons: rework fab menu Dominik Csapak
2025-06-30  8:24 ` [yew-devel] [PATCH yew-widget-toolkit-assets v2 7/7] don't use 'html[dir="rtl"]' for RTL behavior Dominik Csapak
2025-06-30  8:24 ` Dominik Csapak [this message]
2025-06-30  8:24 ` [yew-devel] [PATCH yew-widget-toolkit v2 02/12] widget: button: don't hardcode icons font size Dominik Csapak
2025-06-30  8:25 ` [yew-devel] [PATCH yew-widget-toolkit v2 03/12] touch: page stack: add more animations Dominik Csapak
2025-06-30  8:25 ` [yew-devel] [PATCH yew-widget-toolkit v2 04/12] touch: application bar: set custom class on back button Dominik Csapak
2025-06-30  8:25 ` [yew-devel] [PATCH yew-widget-toolkit v2 05/12] touch: page stack: don't use animation by default Dominik Csapak
2025-06-30  8:25 ` [yew-devel] [PATCH yew-widget-toolkit v2 06/12] touch: material app: allow pass through of page animation style Dominik Csapak
2025-06-30  8:25 ` [yew-devel] [PATCH yew-widget-toolkit v2 07/12] touch: scaffold: fix overflow setting for body Dominik Csapak
2025-06-30  8:25 ` [yew-devel] [PATCH yew-widget-toolkit v2 08/12] touch: scaffold: use direction independent positioning for the FAB Dominik Csapak
2025-06-30  8:25 ` [yew-devel] [PATCH yew-widget-toolkit v2 09/12] touch: fab: add size option Dominik Csapak
2025-06-30  8:25 ` [yew-devel] [PATCH yew-widget-toolkit v2 10/12] touch: fab: convert to widget macro Dominik Csapak
2025-06-30  8:25 ` [yew-devel] [PATCH yew-widget-toolkit v2 11/12] touch: fab menu: " Dominik Csapak
2025-06-30  8:25 ` [yew-devel] [PATCH yew-widget-toolkit v2 12/12] touch: fab menu: rework fab menu Dominik Csapak
2025-06-30 10:56 ` [yew-devel] applied: [PATCH yew-widget-toolkit/yew-widget-toolkit-assets v2 00/19] various touch improvements Dietmar Maurer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250630082509.1202308-9-d.csapak@proxmox.com \
    --to=d.csapak@proxmox.com \
    --cc=yew-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal