From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9]) by lore.proxmox.com (Postfix) with ESMTPS id 35F031FF16B for ; Fri, 12 Sep 2025 10:33:02 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 70B2227359; Fri, 12 Sep 2025 10:32:58 +0200 (CEST) From: Dominik Csapak To: yew-devel@lists.proxmox.com Date: Fri, 12 Sep 2025 10:32:14 +0200 Message-ID: <20250912083225.942200-3-d.csapak@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20250912083225.942200-1-d.csapak@proxmox.com> References: <20250912083225.942200-1-d.csapak@proxmox.com> MIME-Version: 1.0 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.026 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RCVD_IN_VALIDITY_CERTIFIED_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. RCVD_IN_VALIDITY_SAFE_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [yew-devel] [PATCH yew-widget-toolkit 2/3] widget: rtl switcher: refactor setting rtl direction X-BeenThere: yew-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Yew framework devel list at Proxmox List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Yew framework devel list at Proxmox Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: yew-devel-bounces@lists.proxmox.com Sender: "yew-devel" we want to use that from the language selector too, so refactor this. Signed-off-by: Dominik Csapak --- 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, 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 { @@ -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