From bfdc9a6d664702cf95f4a2ab60b6cb46ef18f762 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Wed, 19 Oct 2022 14:05:14 -0600 Subject: [PATCH] Show filename in title --- examples/editor-libcosmic/src/main.rs | 53 +++++++++++++++------------ 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/examples/editor-libcosmic/src/main.rs b/examples/editor-libcosmic/src/main.rs index 4ad829e..22f390f 100644 --- a/examples/editor-libcosmic/src/main.rs +++ b/examples/editor-libcosmic/src/main.rs @@ -29,6 +29,7 @@ use cosmic_text::{ use std::{ env, fs, + path::PathBuf, sync::Mutex, }; @@ -93,6 +94,7 @@ fn main() -> cosmic::iced::Result { pub struct Window { theme: Theme, + path_opt: Option, buffer: Mutex>, } @@ -104,6 +106,23 @@ pub enum Message { ThemeChanged(&'static str), } +impl Window { + pub fn open(&mut self, path: PathBuf) { + let mut buffer = self.buffer.lock().unwrap(); + match fs::read_to_string(&path) { + Ok(text) => { + buffer.set_text(&text); + self.path_opt = Some(path); + }, + Err(err) => { + log::error!("failed to open '{}': {}", path.display(), err); + buffer.set_text(""); + self.path_opt = None; + } + } + } +} + impl Application for Window { type Executor = iced::executor::Default; type Flags = (); @@ -117,21 +136,14 @@ impl Application for Window { FONT_SIZES[font_size_i], ); - if let Some(arg) = env::args().nth(1) { - match fs::read_to_string(&arg) { - Ok(text) => { - buffer.set_text(&text); - }, - Err(err) => { - log::error!("failed to open '{}': {}", arg, err); - } - } - } - - let window = Window { + let mut window = Window { theme: Theme::Dark, + path_opt: None, buffer: Mutex::new(buffer), }; + if let Some(arg) = env::args().nth(1) { + window.open(PathBuf::from(arg)); + } (window, Command::none()) } @@ -140,23 +152,18 @@ impl Application for Window { } fn title(&self) -> String { - format!("COSMIC Text - iced - {}", FONT_SYSTEM.locale) + if let Some(path) = &self.path_opt { + format!("COSMIC Text - {} - {}", FONT_SYSTEM.locale, path.display()) + } else { + format!("COSMIC Text - {}", FONT_SYSTEM.locale) + } } fn update(&mut self, message: Message) -> iced::Command { match message { Message::Open => { if let Some(path) = rfd::FileDialog::new().pick_file() { - let mut buffer = self.buffer.lock().unwrap(); - match fs::read_to_string(&path) { - Ok(text) => { - buffer.set_text(&text); - }, - Err(err) => { - buffer.set_text(""); - log::error!("failed to open '{}': {}", path.display(), err); - } - } + self.open(path); } }, Message::MetricsChanged(metrics) => {