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 3/3] widget: language selector: automatically set text direction from language
Date: Fri, 12 Sep 2025 10:47:22 +0200	[thread overview]
Message-ID: <20250912084732.1053980-4-d.csapak@proxmox.com> (raw)
In-Reply-To: <20250912084732.1053980-1-d.csapak@proxmox.com>

on first render and when changing the language.

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
changes from v1:
* remove use of 'get_current_language_info', was a leftover

 src/state/language.rs           |  7 +++++++
 src/state/mod.rs                |  4 ++--
 src/widget/language_selector.rs | 18 +++++++++++++++++-
 3 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/src/state/language.rs b/src/state/language.rs
index 3848731..5621a80 100644
--- a/src/state/language.rs
+++ b/src/state/language.rs
@@ -76,6 +76,13 @@ pub fn get_available_languages() -> Vec<LanguageInfo> {
         .collect()
 }
 
+/// Get [`LanguageInfo`] by its short name.
+pub fn get_language_info(lang: &str) -> Option<LanguageInfo> {
+    get_available_languages()
+        .into_iter()
+        .find(|info| info.lang == lang)
+}
+
 // this `thread_local!` definition should be fine as this crate is essentially WASM only where
 // besides web workers and similar ways to spawn futures, there is only one thread. if this
 // assumption changes, this will need to be adapted.
diff --git a/src/state/mod.rs b/src/state/mod.rs
index ff91faa..5a3ffc5 100644
--- a/src/state/mod.rs
+++ b/src/state/mod.rs
@@ -39,8 +39,8 @@ pub use theme::{
 
 mod language;
 pub use language::{
-    get_available_languages, set_available_languages, Language, LanguageInfo, LanguageObserver,
-    TextDirection,
+    get_available_languages, get_language_info, set_available_languages, Language, LanguageInfo,
+    LanguageObserver, TextDirection,
 };
 
 use crate::props::StorageLocation;
diff --git a/src/widget/language_selector.rs b/src/widget/language_selector.rs
index 588cfab..ff5d467 100644
--- a/src/widget/language_selector.rs
+++ b/src/widget/language_selector.rs
@@ -1,6 +1,7 @@
 use std::rc::Rc;
 
-use crate::state::{Selection, Store};
+use crate::state::{get_language_info, Selection, Store};
+use crate::widget::rtl_switcher::set_text_direction;
 use yew::html::IntoEventCallback;
 use yew::virtual_dom::{Key, VComp, VNode};
 
@@ -122,6 +123,11 @@ impl Component for PwtLanguageSelector {
                 } else {
                     Language::store(lang);
                 }
+                if let Some(info) = get_language_info(&self.lang) {
+                    if let Err(err) = set_text_direction(info.direction) {
+                        log::error!("could not set text direction: {err}");
+                    }
+                }
                 true
             }
         }
@@ -163,6 +169,16 @@ impl Component for PwtLanguageSelector {
             })
             .into()
     }
+
+    fn rendered(&mut self, _ctx: &Context<Self>, first_render: bool) {
+        if first_render {
+            if let Some(info) = get_language_info(&self.lang) {
+                if let Err(err) = set_text_direction(info.direction) {
+                    log::error!("could not set text direction: {err}");
+                }
+            }
+        }
+    }
 }
 
 impl From<LanguageSelector> for VNode {
-- 
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 ` [yew-devel] [PATCH yew-widget-toolkit v2 2/3] widget: rtl switcher: refactor setting rtl direction Dominik Csapak
2025-09-12  8:47 ` Dominik Csapak [this message]
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-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