From fd22413057beb2ac63ee4598730b24c76917faa4 Mon Sep 17 00:00:00 2001 From: Mikolaj Wielgus Date: Wed, 20 Nov 2024 00:57:03 +0100 Subject: [PATCH] feat(egui): set title to filename of opened DSN file --- src/bin/topola-egui/app.rs | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/bin/topola-egui/app.rs b/src/bin/topola-egui/app.rs index 640b3c5..bfea6b6 100644 --- a/src/bin/topola-egui/app.rs +++ b/src/bin/topola-egui/app.rs @@ -3,6 +3,7 @@ use std::{ future::Future, io, ops::ControlFlow, + path::Path, sync::mpsc::{channel, Receiver, Sender}, }; use unic_langid::{langid, LanguageIdentifier}; @@ -126,6 +127,35 @@ impl App { ControlFlow::Break(()) } + #[cfg(not(target_arch = "wasm32"))] + fn update_title(&mut self, ctx: &egui::Context) { + if let Some(workspace) = &self.maybe_workspace { + if let Some(filename) = Path::new(workspace.design.get_name()) + .file_name() + .and_then(|n| n.to_str()) + { + ctx.send_viewport_cmd(egui::ViewportCommand::Title(filename.to_string())); + } + } + } + + #[cfg(target_arch = "wasm32")] + fn update_title(&mut self, ctx: &egui::Context) { + if let Some(workspace) = &self.maybe_workspace { + if let Some(filename) = Path::new(workspace.design.get_name()) + .file_name() + .and_then(|n| n.to_str()) + { + let document = eframe::web_sys::window() + .expect("No window") + .document() + .expect("No document"); + + document.set_title(filename); + } + } + } + #[cfg(not(target_arch = "wasm32"))] fn update_locale(&mut self) { // I don't know any equivalent of changing the lang property in desktop. @@ -133,8 +163,6 @@ impl App { #[cfg(target_arch = "wasm32")] fn update_locale(&mut self) { - use eframe::wasm_bindgen::JsCast; - let document_element = eframe::web_sys::window() .expect("No window") .document() @@ -192,6 +220,7 @@ impl eframe::App for App { .update(ctx, &self.menu_bar, self.maybe_workspace.as_mut()); self.update_locale(); + self.update_title(ctx); if ctx.input(|i| i.key_pressed(egui::Key::Escape)) { ctx.send_viewport_cmd(egui::ViewportCommand::Close);