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 3/3] props: make RenderFn's return type generic
Date: Wed, 10 Dec 2025 11:48:50 +0100	[thread overview]
Message-ID: <20251210104856.1698157-4-d.csapak@proxmox.com> (raw)
In-Reply-To: <20251210104856.1698157-1-d.csapak@proxmox.com>

and use that with the type `String` instead of our TextRenderFn, that is
now obsolete.

This not only gets rid of a trait, but also allows us to have more
generic render functions in use, e.g. if some component needs a
render function that returns e.g. a `Dialog` to do something with it,
which would now be possible.

By defining the generic type `OUT` as `Html` by default, we don't break
compatibility with existing users.

This is now very similar to yew's `Callback` type, but it just gives a
reference to the function instead of an owned type.

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 src/props/mod.rs             |  5 +--
 src/props/render_function.rs | 81 ++++++------------------------------
 src/touch/material_app.rs    | 10 ++---
 src/widget/catalog_loader.rs |  6 +--
 src/widget/desktop_app.rs    |  6 +--
 src/widget/meter.rs          |  6 +--
 src/widget/theme_loader.rs   |  6 +--
 7 files changed, 30 insertions(+), 90 deletions(-)

diff --git a/src/props/mod.rs b/src/props/mod.rs
index db5cc52..254cb4e 100644
--- a/src/props/mod.rs
+++ b/src/props/mod.rs
@@ -200,10 +200,7 @@ mod padding;
 pub use padding::CssPaddingBuilder;
 
 mod render_function;
-pub use render_function::{
-    BuilderFn, IntoOptionalBuilderFn, IntoOptionalRenderFn, IntoOptionalTextRenderFn, RenderFn,
-    TextRenderFn,
-};
+pub use render_function::{BuilderFn, IntoOptionalBuilderFn, IntoOptionalRenderFn, RenderFn};
 
 mod sorter_function;
 pub use sorter_function::{IntoSorterFn, SorterFn};
diff --git a/src/props/render_function.rs b/src/props/render_function.rs
index 8773c8b..dd5632d 100644
--- a/src/props/render_function.rs
+++ b/src/props/render_function.rs
@@ -1,4 +1,3 @@
-use std::fmt::Display;
 use std::rc::Rc;
 
 use derivative::Derivative;
@@ -10,100 +9,44 @@ use yew::Html;
 /// Wraps `Rc` around `Fn` so it can be passed as a prop.
 #[derive(Derivative)]
 #[derivative(Clone(bound = ""), PartialEq(bound = ""))]
-pub struct RenderFn<T>(
-    #[derivative(PartialEq(compare_with = "Rc::ptr_eq"))] Rc<dyn Fn(&T) -> Html>,
+pub struct RenderFn<IN, OUT = Html>(
+    #[derivative(PartialEq(compare_with = "Rc::ptr_eq"))] Rc<dyn Fn(&IN) -> OUT>,
 );
 
-impl<T> RenderFn<T> {
+impl<IN, OUT> RenderFn<IN, OUT> {
     /// Creates a new [`RenderFn`]
-    pub fn new(renderer: impl 'static + Fn(&T) -> Html) -> Self {
+    pub fn new(renderer: impl 'static + Fn(&IN) -> OUT) -> Self {
         Self(Rc::new(renderer))
     }
     /// Apply the render function
-    pub fn apply(&self, data: &T) -> Html {
+    pub fn apply(&self, data: &IN) -> OUT {
         (self.0)(data)
     }
 }
 
-impl<T, F: 'static + Fn(&T) -> Html> From<F> for RenderFn<T> {
+impl<IN, OUT, F: 'static + Fn(&IN) -> OUT> From<F> for RenderFn<IN, OUT> {
     fn from(f: F) -> Self {
         RenderFn::new(f)
     }
 }
 
 /// Helper trait to create an optional [RenderFn] property.
-pub trait IntoOptionalRenderFn<T> {
-    fn into_optional_render_fn(self) -> Option<RenderFn<T>>;
+pub trait IntoOptionalRenderFn<IN, OUT = Html> {
+    fn into_optional_render_fn(self) -> Option<RenderFn<IN, OUT>>;
 }
 
-impl<T, R: Into<RenderFn<T>>> IntoOptionalRenderFn<T> for R {
-    fn into_optional_render_fn(self) -> Option<RenderFn<T>> {
+impl<IN, OUT, R: Into<RenderFn<IN, OUT>>> IntoOptionalRenderFn<IN, OUT> for R {
+    fn into_optional_render_fn(self) -> Option<RenderFn<IN, OUT>> {
         Some(self.into())
     }
 }
 
-impl<T, R: Into<RenderFn<T>>> IntoOptionalRenderFn<T> for Option<R> {
-    fn into_optional_render_fn(self) -> Option<RenderFn<T>> {
+impl<IN, OUT, R: Into<RenderFn<IN, OUT>>> IntoOptionalRenderFn<IN, OUT> for Option<R> {
+    fn into_optional_render_fn(self) -> Option<RenderFn<IN, OUT>> {
         self.map(|me| me.into())
     }
 }
 
-/// A [TextRenderFn] function is a callback that transforms data into [String].
-///
-/// Wraps `Rc` around `Fn` so it can be passed as a prop.
-#[derive(Derivative)]
-#[derivative(Clone(bound = ""), PartialEq(bound = ""))]
-pub struct TextRenderFn<T>(
-    #[derivative(PartialEq(compare_with = "Rc::ptr_eq"))] Rc<dyn Fn(&T) -> String>,
-);
-
-impl<T> TextRenderFn<T> {
-    /// Creates a new [TextRenderFn]
-    pub fn new(renderer: impl 'static + Fn(&T) -> String) -> Self {
-        Self(Rc::new(renderer))
-    }
-    /// Apply the render function
-    pub fn apply(&self, data: &T) -> String {
-        (self.0)(data)
-    }
-}
-
-impl<T, F: 'static + Fn(&T) -> String> From<F> for TextRenderFn<T> {
-    fn from(f: F) -> Self {
-        TextRenderFn::new(f)
-    }
-}
-
-/// Helper trait to create an optional [TextRenderFn] property.
-///
-/// For types implementing [Display], you can pass 'true' to create
-/// a render function whichs uses `to_string()`.
-pub trait IntoOptionalTextRenderFn<T> {
-    fn into_optional_text_render_fn(self) -> Option<TextRenderFn<T>>;
-}
-
-impl<T, R: Into<TextRenderFn<T>>> IntoOptionalTextRenderFn<T> for R {
-    fn into_optional_text_render_fn(self) -> Option<TextRenderFn<T>> {
-        Some(self.into())
-    }
-}
-
-impl<T, R: Into<TextRenderFn<T>>> IntoOptionalTextRenderFn<T> for Option<R> {
-    fn into_optional_text_render_fn(self) -> Option<TextRenderFn<T>> {
-        self.map(|me| me.into())
-    }
-}
-
-impl<T: Display> IntoOptionalTextRenderFn<T> for bool {
-    fn into_optional_text_render_fn(self) -> Option<TextRenderFn<T>> {
-        if self {
-            Some(TextRenderFn::new(|t: &T| t.to_string()))
-        } else {
-            None
-        }
-    }
-}
-
 /// A [BuilderFn] function is a callback that returns a generic type.
 ///
 /// Wraps `Rc` around `Fn` so it can be passed as a prop.
diff --git a/src/touch/material_app.rs b/src/touch/material_app.rs
index 52e7042..26de2b3 100644
--- a/src/touch/material_app.rs
+++ b/src/touch/material_app.rs
@@ -12,7 +12,7 @@ use yew_router::{history::History, Router};
 use pwt_macros::builder;
 
 use crate::prelude::*;
-use crate::props::{IntoOptionalTextRenderFn, TextRenderFn};
+use crate::props::{IntoOptionalRenderFn, RenderFn};
 use crate::state::{NavigationContainer, SharedState, SharedStateObserver};
 use crate::touch::{PageAnimationStyle, SnackBarController, SnackBarManager};
 use crate::widget::{CatalogLoader, Container, ThemeLoader};
@@ -216,14 +216,14 @@ pub struct MaterialApp {
     pub page_animation: Option<PageAnimationStyle>,
 
     /// Returns the server side CSS URLs (see [ThemeLoader]).
-    #[builder_cb(IntoOptionalTextRenderFn, into_optional_text_render_fn, String)]
+    #[builder_cb(IntoOptionalRenderFn, into_optional_render_fn, String, String)]
     #[prop_or_default]
-    pub theme_url_builder: Option<TextRenderFn<String>>,
+    pub theme_url_builder: Option<RenderFn<String, String>>,
 
     /// Convert ISO 639-1 language code to server side catalog URLs (see [CatalogLoader]).
-    #[builder_cb(IntoOptionalTextRenderFn, into_optional_text_render_fn, String)]
+    #[builder_cb(IntoOptionalRenderFn, into_optional_render_fn, String, String)]
     #[prop_or_default]
-    pub catalog_url_builder: Option<TextRenderFn<String>>,
+    pub catalog_url_builder: Option<RenderFn<String, String>>,
 
     /// Default language (skip catalog loading for this language)
     #[builder(IntoPropValue, into_prop_value)]
diff --git a/src/widget/catalog_loader.rs b/src/widget/catalog_loader.rs
index ef53a05..e8afe90 100644
--- a/src/widget/catalog_loader.rs
+++ b/src/widget/catalog_loader.rs
@@ -4,7 +4,7 @@ use gettext::Catalog;
 use yew::html::IntoPropValue;
 use yew::virtual_dom::{VComp, VNode};
 
-use crate::props::{IntoOptionalTextRenderFn, TextRenderFn};
+use crate::props::{IntoOptionalRenderFn, RenderFn};
 use crate::state::{get_language_info, Language, LanguageObserver};
 use crate::widget::rtl_switcher::set_text_direction;
 use crate::{impl_to_html, prelude::*};
@@ -35,9 +35,9 @@ pub struct CatalogLoader {
     /// # fn test () -> Callback<String, String> {
     ///  Callback::from(|lang: String| format!("catalog-{}.mo", lang))
     /// # }
-    #[builder_cb(IntoOptionalTextRenderFn, into_optional_text_render_fn, String)]
+    #[builder_cb(IntoOptionalRenderFn, into_optional_render_fn, String, String)]
     #[prop_or_default]
-    pub url_builder: Option<TextRenderFn<String>>,
+    pub url_builder: Option<RenderFn<String, String>>,
 
     /// Default language (skip catalog loading for this language)
     #[builder(IntoPropValue, into_prop_value)]
diff --git a/src/widget/desktop_app.rs b/src/widget/desktop_app.rs
index 622b2ed..a7fe20c 100644
--- a/src/widget/desktop_app.rs
+++ b/src/widget/desktop_app.rs
@@ -7,7 +7,7 @@ use yew_router::Router;
 use gloo_history::{AnyHistory, HashHistory};
 
 use crate::prelude::*;
-use crate::props::{IntoOptionalTextRenderFn, TextRenderFn};
+use crate::props::{IntoOptionalRenderFn, RenderFn};
 use crate::state::NavigationContainer;
 use crate::widget::{CatalogLoader, ThemeLoader};
 
@@ -41,9 +41,9 @@ pub struct DesktopApp {
     pub history: Option<AnyHistory>,
 
     /// Convert ISO 639-1 language code to server side catalog URLs (see [CatalogLoader]).
-    #[builder_cb(IntoOptionalTextRenderFn, into_optional_text_render_fn, String)]
+    #[builder_cb(IntoOptionalRenderFn, into_optional_render_fn, String, String)]
     #[prop_or_default]
-    pub catalog_url_builder: Option<TextRenderFn<String>>,
+    pub catalog_url_builder: Option<RenderFn<String, String>>,
 
     /// Default language (skip catalog loading for this language)
     #[builder(IntoPropValue, into_prop_value)]
diff --git a/src/widget/meter.rs b/src/widget/meter.rs
index c547a87..cf552cb 100644
--- a/src/widget/meter.rs
+++ b/src/widget/meter.rs
@@ -7,7 +7,7 @@ use yew::virtual_dom::VTag;
 use pwt_macros::{builder, widget};
 
 use crate::props::{
-    ContainerBuilder, CssLength, IntoOptionalTextRenderFn, IntoVTag, TextRenderFn, WidgetBuilder,
+    ContainerBuilder, CssLength, IntoOptionalRenderFn, IntoVTag, RenderFn, WidgetBuilder,
     WidgetStyleBuilder,
 };
 use crate::widget::Container;
@@ -64,9 +64,9 @@ pub struct Meter {
     pub value: f32,
 
     /// Show value as text.
-    #[builder_cb(IntoOptionalTextRenderFn, into_optional_text_render_fn, f32)]
+    #[builder_cb(IntoOptionalRenderFn, into_optional_render_fn, f32, String)]
     #[prop_or_default]
-    pub render_text: Option<TextRenderFn<f32>>,
+    pub render_text: Option<RenderFn<f32, String>>,
 
     /// Determines if the meter value transitions are animated (via CSS) or not.
     /// It is equivalent to setting the class `pwt-animated`.
diff --git a/src/widget/theme_loader.rs b/src/widget/theme_loader.rs
index f7baa46..cb4831d 100644
--- a/src/widget/theme_loader.rs
+++ b/src/widget/theme_loader.rs
@@ -5,7 +5,7 @@ use yew::prelude::*;
 use yew::virtual_dom::{Key, VComp, VNode};
 
 use crate::impl_to_html;
-use crate::props::{IntoOptionalTextRenderFn, TextRenderFn};
+use crate::props::{IntoOptionalRenderFn, RenderFn};
 use crate::state::{Theme, ThemeDensity, ThemeObserver};
 
 /// Dynamically load selected theme
@@ -23,9 +23,9 @@ pub struct ThemeLoader {
     /// Returns the server side CSS URL (full path)
     ///
     /// Default is "{lc(theme_name)}-yew-style.css".
-    #[builder_cb(IntoOptionalTextRenderFn, into_optional_text_render_fn, String)]
+    #[builder_cb(IntoOptionalRenderFn, into_optional_render_fn, String, String)]
     #[prop_or_default]
-    pub theme_url_builder: Option<TextRenderFn<String>>,
+    pub theme_url_builder: Option<RenderFn<String, String>>,
 }
 
 impl ThemeLoader {
-- 
2.47.3



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


  parent reply	other threads:[~2025-12-10 10:48 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-12-10 10:48 [yew-devel] [RFC yew-comp/yew-widget-toolkit 0/4] " Dominik Csapak
2025-12-10 10:48 ` [yew-devel] [PATCH yew-widget-toolkit 1/3] pwt-macros: update tests to work for newer rustc Dominik Csapak
2025-12-10 10:48 ` [yew-devel] [PATCH yew-widget-toolkit 2/3] pwt-macros: builder: allow multiple generic types in callback variant Dominik Csapak
2025-12-10 10:48 ` Dominik Csapak [this message]
2025-12-10 10:48 ` [yew-devel] [PATCH yew-comp 1/1] rrd: replace TextRenderFn with new generic RenderFn Dominik Csapak
2025-12-11  9:35 ` [yew-devel] applied: [RFC yew-comp/yew-widget-toolkit 0/4] make RenderFn's return type generic 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=20251210104856.1698157-4-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