From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [IPv6:2a0f:8001:1:32::40]) by lore.proxmox.com (Postfix) with ESMTPS id 7699D1FF0B7 for ; Mon, 30 Nov 2026 13:53:31 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 8F22621704; Mon, 30 Nov 2026 13:53:30 +0100 (CET) From: Dominik Csapak To: pdm-devel@lists.proxmox.com Subject: [PATCH yew-widget-toolkit v3 1/8] state: event: add helper to dispatch custom DOM events Date: Mon, 30 Nov 2026 13:52:35 +0100 Message-ID: <20261130125325.2392967-2-d.csapak@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261130125325.2392967-1-d.csapak@proxmox.com> References: <20261130125325.2392967-1-d.csapak@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.199 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) PROLO_LEO1 0.1 Meta Catches all Leo drug variations so far RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: PJFYU5IA4SPLULE5K3G3KJGNJOS2NKA2 X-Message-ID-Hash: PJFYU5IA4SPLULE5K3G3KJGNJOS2NKA2 X-MailFrom: d.csapak@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox Datacenter Manager development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Dispatching a custom event is currently open coded for the `pwt-theme-changed` event. Provide a small wrapper, so that further users do not have to repeat the window/document lookup and the JS error conversion, and use it for the theme event too. Signed-off-by: Dominik Csapak --- src/state/event.rs | 21 +++++++++++++++++++++ src/state/mod.rs | 3 +++ src/state/theme.rs | 7 ++----- 3 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 src/state/event.rs diff --git a/src/state/event.rs b/src/state/event.rs new file mode 100644 index 0000000..39f4444 --- /dev/null +++ b/src/state/event.rs @@ -0,0 +1,21 @@ +//! Helper to dispatch custom DOM events. + +use anyhow::{Error, format_err}; +use web_sys::Event; + +/// Dispatch a custom event with the given name on the document. +/// +/// The event is only delivered to listeners in the current document, so listeners in other +/// browser tabs or windows are not notified. +pub fn dispatch_custom_event(name: &str) -> Result<(), Error> { + let document = web_sys::window() + .and_then(|window| window.document()) + .ok_or_else(|| format_err!("no document available"))?; + + let event = Event::new(name).map_err(crate::convert_js_error)?; + + document + .dispatch_event(&event) + .map(|_| ()) + .map_err(crate::convert_js_error) +} diff --git a/src/state/mod.rs b/src/state/mod.rs index d98d4db..335d7de 100644 --- a/src/state/mod.rs +++ b/src/state/mod.rs @@ -7,6 +7,9 @@ use serde::{Serialize, de::DeserializeOwned}; mod data_store; pub use data_store::{DataNode, DataNodeDerefGuard, DataStore}; +mod event; +pub use event::dispatch_custom_event; + mod loader; pub use loader::{Loader, LoaderState}; diff --git a/src/state/theme.rs b/src/state/theme.rs index 485c88e..a87679f 100644 --- a/src/state/theme.rs +++ b/src/state/theme.rs @@ -155,11 +155,8 @@ impl Default for Theme { } fn emit_theme_changed_event() { - if let Some(window) = web_sys::window() { - if let Some(document) = window.document() { - let event = web_sys::Event::new("pwt-theme-changed").unwrap(); - let _ = document.dispatch_event(&event); - } + if let Err(err) = super::dispatch_custom_event("pwt-theme-changed") { + log::error!("failed to send theme changed event: {err}"); } } -- 2.47.3