From 841c4a4a99e00df54fb71a86bed1ff5370d32760 Mon Sep 17 00:00:00 2001 From: yggverse Date: Fri, 27 Sep 2024 21:20:23 +0300 Subject: [PATCH] use parsed header as title on available --- src/browser/main/tab/page/content/mod.rs | 18 +++++++++--- src/browser/main/tab/page/content/text/mod.rs | 21 ++++++++++++-- src/browser/main/tab/page/mod.rs | 28 +++++++++++-------- 3 files changed, 50 insertions(+), 17 deletions(-) diff --git a/src/browser/main/tab/page/content/mod.rs b/src/browser/main/tab/page/content/mod.rs index 36de0093..f305623d 100644 --- a/src/browser/main/tab/page/content/mod.rs +++ b/src/browser/main/tab/page/content/mod.rs @@ -4,7 +4,7 @@ mod text; use text::Text; use gtk::{ - glib::Uri, + glib::{GString, Uri}, prelude::{BoxExt, WidgetExt}, Box, Orientation, }; @@ -14,6 +14,10 @@ pub enum Mime { TextPlain, } +pub struct ResetResult { + pub title: Option, +} + pub struct Content { widget: Box, } @@ -27,16 +31,22 @@ impl Content { } // Actions - pub fn reset(&self, mime: Mime, base: &Uri, data: &str) { + pub fn reset(&self, mime: Mime, base: &Uri, data: &str) -> ResetResult { // Cleanup while let Some(child) = self.widget.last_child() { self.widget.remove(&child) } - // Compose + // Re-compose match mime { Mime::TextGemini => { - self.widget.append(Text::gemini(data, base).widget()); + let child = Text::gemini(data, base); + + self.widget.append(child.widget()); + + ResetResult { + title: child.meta_title().clone(), + } } Mime::TextPlain => { todo!() diff --git a/src/browser/main/tab/page/content/text/mod.rs b/src/browser/main/tab/page/content/text/mod.rs index 12d4ba39..f4308d13 100644 --- a/src/browser/main/tab/page/content/text/mod.rs +++ b/src/browser/main/tab/page/content/text/mod.rs @@ -2,9 +2,17 @@ mod gemini; use gemini::Gemini; -use gtk::{glib::Uri, ScrolledWindow}; +use gtk::{ + glib::{GString, Uri}, + ScrolledWindow, +}; + +pub struct Meta { + title: Option, +} pub struct Text { + meta: Meta, widget: ScrolledWindow, } @@ -14,16 +22,25 @@ impl Text { // Init components let gemini = Gemini::new(gemtext, base); + // Init meta + let meta = Meta { + title: gemini.reader_title().clone(), + }; + // Init widget let widget = ScrolledWindow::builder().build(); widget.set_child(Some(gemini.widget())); // Result - Self { widget } + Self { meta, widget } } // Getters + pub fn meta_title(&self) -> &Option { + &self.meta.title + } + pub fn widget(&self) -> &ScrolledWindow { &self.widget } diff --git a/src/browser/main/tab/page/mod.rs b/src/browser/main/tab/page/mod.rs index 9415629d..e1defe39 100644 --- a/src/browser/main/tab/page/mod.rs +++ b/src/browser/main/tab/page/mod.rs @@ -203,7 +203,13 @@ impl Page { meta.borrow_mut().mime = Some(Mime::TextGemini); // Select widget match parts.get(4) { - Some(source) => content.reset(content::Mime::TextGemini, &uri, &source), + Some(source) => { + // Update component + let result = content.reset(content::Mime::TextGemini, &uri, &source); + + // This content type may return parsed title + meta.borrow_mut().title = result.title.clone(); + }, None => todo!(), } }, @@ -229,20 +235,20 @@ impl Page { }; // Update - let _ = widget.activate_action( + widget.activate_action( "win.update", None, - ); + ).expect("Action `win.update` not found"); } Err(e) => { meta.borrow_mut().title = Some(gformat!("Oops")); meta.borrow_mut().description = Some(gformat!("Failed to read buffer data: {e}")); meta.borrow_mut().progress_fraction = 1.0; - let _ = widget.activate_action( + widget.activate_action( "win.update", None, - ); + ).expect("Action `win.update` not found"); } } @@ -257,10 +263,10 @@ impl Page { meta.borrow_mut().description = Some(gformat!("Failed to read response: {:?}", e)); meta.borrow_mut().progress_fraction = 1.0; - let _ = widget.activate_action( + widget.activate_action( "win.update", None, - ); + ).expect("Action `win.update` not found"); // Close connection if let Err(e) = connection.close(Some(&cancellable)) { @@ -276,10 +282,10 @@ impl Page { meta.borrow_mut().description = Some(gformat!("Failed to read request: {:?}", e)); meta.borrow_mut().progress_fraction = 1.0; - let _ = widget.activate_action( + widget.activate_action( "win.update", None, - ); + ).expect("Action `win.update` not found"); // Close connection if let Err(e) = connection.close(Some(&cancellable)) { @@ -295,10 +301,10 @@ impl Page { meta.borrow_mut().description = Some(gformat!("Failed to connect: {:?}", e)); meta.borrow_mut().progress_fraction = 1.0; - let _ = widget.activate_action( + widget.activate_action( "win.update", None, - ); + ).expect("Action `win.update` not found"); } }, );