all lists on 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 2/3] widget: rtl switcher: refactor setting rtl direction
Date: Fri, 12 Sep 2025 10:47:21 +0200	[thread overview]
Message-ID: <20250912084732.1053980-3-d.csapak@proxmox.com> (raw)
In-Reply-To: <20250912084732.1053980-1-d.csapak@proxmox.com>

we want to use that from the language selector too, so refactor this.

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 src/widget/rtl_switcher.rs | 55 +++++++++++++++++++++++++++-----------
 1 file changed, 39 insertions(+), 16 deletions(-)

diff --git a/src/widget/rtl_switcher.rs b/src/widget/rtl_switcher.rs
index 2400dd7..8c18bbd 100644
--- a/src/widget/rtl_switcher.rs
+++ b/src/widget/rtl_switcher.rs
@@ -1,8 +1,13 @@
+use anyhow::format_err;
+
 use pwt_macros::widget;
 use yew::{Classes, Component, Properties};
 
 use super::form::Checkbox;
-use crate::props::{FieldBuilder, WidgetBuilder};
+use crate::{
+    props::{FieldBuilder, WidgetBuilder},
+    state::TextDirection,
+};
 
 /// A checkbox to switch between Left-to-Right and Right-to-Left layouts
 #[widget(pwt=crate, comp=PwtRtlSwitcher, @input, @element)]
@@ -51,25 +56,23 @@ impl Component for PwtRtlSwitcher {
     fn update(&mut self, _ctx: &yew::Context<Self>, msg: Self::Message) -> bool {
         match msg {
             Msg::ToggleRtl => {
-                let elements = gloo_utils::document().get_elements_by_tag_name("html");
-                if let Some(html) = elements.get_with_index(0) {
-                    if self.rtl {
-                        if let Err(err) = html.remove_attribute("dir") {
-                            log::error!("could not remove dir attribute: {:?}", err);
-                            return false;
-                        }
-                        self.rtl = false;
-                    } else {
-                        if let Err(err) = html.set_attribute("dir", "rtl") {
-                            log::error!("could not set dir attribute: {:?}", err);
-                            return false;
-                        }
-                        self.rtl = true;
+                let direction = if self.rtl {
+                    TextDirection::Ltr
+                } else {
+                    TextDirection::Rtl
+                };
+                match set_text_direction(direction) {
+                    Err(err) => {
+                        log::error!("{err}");
+                        false
+                    }
+                    Ok(()) => {
+                        self.rtl = !self.rtl;
+                        true
                     }
                 }
             }
         }
-        true
     }
 
     fn create(_ctx: &yew::Context<Self>) -> Self {
@@ -94,3 +97,23 @@ impl Component for PwtRtlSwitcher {
             .into()
     }
 }
+
+/// Sets the global text direction on the root HTML element (if possible).
+/// Otherwise returns an error.
+pub fn set_text_direction(direction: TextDirection) -> Result<(), anyhow::Error> {
+    let elements = gloo_utils::document().get_elements_by_tag_name("html");
+    let html = elements
+        .get_with_index(0)
+        .ok_or_else(|| format_err!("no html element found"))?;
+    match direction {
+        TextDirection::Ltr => {
+            html.remove_attribute("dir")
+                .map_err(|err| format_err!("could not remove dir attribute: {err:?}"))?;
+        }
+        TextDirection::Rtl => {
+            html.set_attribute("dir", "rtl")
+                .map_err(|err| format_err!("could not set dir attribute: {err:?}"))?;
+        }
+    }
+    Ok(())
+}
-- 
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-09-12  8:47 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-12  8:47 [yew-devel] [PATCH yew-comp/yew-widget-toolkit v2 0/4] Enable RTL text direction for relevant languages Dominik Csapak
2025-09-12  8:47 ` [yew-devel] [PATCH yew-widget-toolkit v2 1/3] state: language: add text direction setting Dominik Csapak
2025-09-12  8:47 ` Dominik Csapak [this message]
2025-09-12  8:47 ` [yew-devel] [PATCH yew-widget-toolkit v2 3/3] widget: language selector: automatically set text direction from language Dominik Csapak
2025-09-12  8:47 ` [yew-devel] [PATCH yew-comp v2 1/1] languages: add text direction information where necessary Dominik Csapak

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=20250912084732.1053980-3-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal