feat(egui): set title to filename of opened DSN file

This commit is contained in:
Mikolaj Wielgus 2024-11-20 00:57:03 +01:00
parent 20e682243d
commit fd22413057
1 changed files with 31 additions and 2 deletions

View File

@ -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);