From b3c754571790f1aa6a2368c0b0ad0cff7b8ddb63 Mon Sep 17 00:00:00 2001 From: yggverse Date: Fri, 27 Sep 2024 20:38:55 +0300 Subject: [PATCH] use optionals, remove undefined enums --- src/browser/main/tab/label/mod.rs | 8 +++- src/browser/main/tab/label/title/mod.rs | 7 ++- src/browser/main/tab/mod.rs | 10 +++-- src/browser/main/tab/page/content/mod.rs | 6 --- src/browser/main/tab/page/meta.rs | 13 +++--- src/browser/main/tab/page/mod.rs | 55 ++++++++++++------------ 6 files changed, 52 insertions(+), 47 deletions(-) diff --git a/src/browser/main/tab/label/mod.rs b/src/browser/main/tab/label/mod.rs index 85dbae50..92356dc8 100644 --- a/src/browser/main/tab/label/mod.rs +++ b/src/browser/main/tab/label/mod.rs @@ -42,8 +42,12 @@ impl Label { } // Actions - pub fn update(&self, title: &GString) { - self.widget.set_tooltip_text(Some(title)); + pub fn update(&self, title: Option<&GString>) { + match title { + Some(tooltip_text) => self.widget.set_tooltip_text(Some(tooltip_text)), + None => self.widget.set_tooltip_text(None), + } + self.title.update(title); } diff --git a/src/browser/main/tab/label/title/mod.rs b/src/browser/main/tab/label/title/mod.rs index 5578c8d9..5095045e 100644 --- a/src/browser/main/tab/label/title/mod.rs +++ b/src/browser/main/tab/label/title/mod.rs @@ -18,8 +18,11 @@ impl Title { } // Actions - pub fn update(&self, title: &GString) { - self.widget.set_text(title); + pub fn update(&self, title: Option<&GString>) { + match title { + Some(title) => self.widget.set_text(title), + None => self.widget.set_text(""), // @TODO None/false option + } } // Getters diff --git a/src/browser/main/tab/mod.rs b/src/browser/main/tab/mod.rs index 61aee474..48fa732a 100644 --- a/src/browser/main/tab/mod.rs +++ b/src/browser/main/tab/mod.rs @@ -142,7 +142,11 @@ impl Tab { // Get label by widget ID if let Some(label) = self.labels.borrow().get(id) { - label.update(&page.title()); + if let Some(title) = page.title() { + label.update(Some(&title)); + } else { + label.update(None); + } } } } @@ -159,7 +163,7 @@ impl Tab { let id = &widget.widget_name(); // Get page by widget ID if let Some(page) = self.pages.borrow().get(id) { - return Some(page.title()); + return page.title(); } } } @@ -176,7 +180,7 @@ impl Tab { let id = &widget.widget_name(); // Get page by widget ID if let Some(page) = self.pages.borrow().get(id) { - return Some(page.description()); + return page.description(); } } } diff --git a/src/browser/main/tab/page/content/mod.rs b/src/browser/main/tab/page/content/mod.rs index 625bfa7b..36de0093 100644 --- a/src/browser/main/tab/page/content/mod.rs +++ b/src/browser/main/tab/page/content/mod.rs @@ -10,13 +10,11 @@ use gtk::{ }; pub enum Mime { - Undefined, TextGemini, TextPlain, } pub struct Content { - mime: Mime, widget: Box, } @@ -24,7 +22,6 @@ impl Content { // Construct pub fn new() -> Self { Self { - mime: Mime::Undefined, widget: Box::builder().orientation(Orientation::Vertical).build(), } } @@ -44,9 +41,6 @@ impl Content { Mime::TextPlain => { todo!() } - Mime::Undefined => { - todo!() - } } } diff --git a/src/browser/main/tab/page/meta.rs b/src/browser/main/tab/page/meta.rs index 0095a014..3b70aebb 100644 --- a/src/browser/main/tab/page/meta.rs +++ b/src/browser/main/tab/page/meta.rs @@ -1,24 +1,23 @@ use gtk::glib::GString; pub enum Mime { - Undefined, TextGemini, TextPlain, } pub struct Meta { - pub title: GString, - pub description: GString, - pub mime: Mime, + pub title: Option, + pub description: Option, + pub mime: Option, pub progress_fraction: f32, } impl Meta { pub fn new() -> Self { Self { - title: GString::new(), - description: GString::new(), - mime: Mime::Undefined, + title: None, + description: None, + mime: None, progress_fraction: 0.0, } } diff --git a/src/browser/main/tab/page/mod.rs b/src/browser/main/tab/page/mod.rs index 1df563a9..9415629d 100644 --- a/src/browser/main/tab/page/mod.rs +++ b/src/browser/main/tab/page/mod.rs @@ -93,9 +93,9 @@ impl Page { let widget = self.widget.clone(); // Update - meta.borrow_mut().mime = Mime::Undefined; - meta.borrow_mut().title = gformat!("Loading.."); - meta.borrow_mut().description = gformat!("Loading.."); + meta.borrow_mut().mime = None; + meta.borrow_mut().title = Some(gformat!("Loading..")); + meta.borrow_mut().description = None; meta.borrow_mut().progress_fraction = 0.0; widget @@ -119,7 +119,7 @@ impl Page { // Update meta.borrow_mut().progress_fraction = 0.25; - meta.borrow_mut().description = gformat!("Connect {host}.."); + meta.borrow_mut().description = Some(gformat!("Connect {host}..")); widget .activate_action("win.update", None) @@ -142,7 +142,7 @@ impl Page { Ok(connection) => { // Update meta.borrow_mut().progress_fraction = 0.50; - meta.borrow_mut().description = gformat!("Connected to {host}.."); + meta.borrow_mut().description = Some(gformat!("Connected to {host}..")); widget.activate_action("win.update", None).expect("Action `win.update` not found"); @@ -155,7 +155,7 @@ impl Page { Ok(_) => { // Update meta.borrow_mut().progress_fraction = 0.75; - meta.borrow_mut().description = gformat!("Request data from {host}.."); + meta.borrow_mut().description = Some(gformat!("Request data from {host}..")); widget.activate_action("win.update", None).expect("Action `win.update` not found"); @@ -172,15 +172,15 @@ impl Page { Ok(data) => { // Format response meta.borrow_mut().progress_fraction = 1.0; - meta.borrow_mut().description = host; - meta.borrow_mut().title = uri.path(); + meta.borrow_mut().description = Some(host); + meta.borrow_mut().title = Some(uri.path()); // Try create short base for title let path = uri.path(); let path = Path::new(&path); if let Some(base) = path.file_name() { if let Some(base_str) = base.to_str() { - meta.borrow_mut().title = GString::from(base_str); + meta.borrow_mut().title = Some(GString::from(base_str)); } } @@ -200,7 +200,7 @@ impl Page { Some(mime) => match mime.as_str() { "text/gemini" => { // Update meta - meta.borrow_mut().mime = Mime::TextGemini; + meta.borrow_mut().mime = Some(Mime::TextGemini); // Select widget match parts.get(4) { Some(source) => content.reset(content::Mime::TextGemini, &uri, &source), @@ -208,12 +208,12 @@ impl Page { } }, "text/plain" => { - meta.borrow_mut().mime = Mime::TextPlain; + meta.borrow_mut().mime = Some(Mime::TextPlain); todo!() }, _ => { - meta.borrow_mut().title = GString::from("Oops"); - meta.borrow_mut().description = gformat!("Content {mime} not supported"); + meta.borrow_mut().title = Some(gformat!("Oops")); + meta.borrow_mut().description = Some(gformat!("Content {mime} not supported")); }, } None => todo!(), @@ -221,8 +221,8 @@ impl Page { // @TODO }, _ => { - meta.borrow_mut().title = GString::from("Oops"); - meta.borrow_mut().description = gformat!("Status {code} not supported"); + meta.borrow_mut().title = Some(gformat!("Oops")); + meta.borrow_mut().description = Some(gformat!("Status {code} not supported")); }, } None => todo!(), @@ -235,8 +235,8 @@ impl Page { ); } Err(e) => { - meta.borrow_mut().title = GString::from("Oops"); - meta.borrow_mut().description = gformat!("Failed to read buffer data: {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( @@ -253,8 +253,8 @@ impl Page { } Err(e) => { // Update - meta.borrow_mut().title = GString::from("Oops"); - meta.borrow_mut().description = gformat!("Failed to read response: {:?}", e); + meta.borrow_mut().title = Some(gformat!("Oops")); + meta.borrow_mut().description = Some(gformat!("Failed to read response: {:?}", e)); meta.borrow_mut().progress_fraction = 1.0; let _ = widget.activate_action( @@ -272,8 +272,8 @@ impl Page { } Err(e) => { // Update - meta.borrow_mut().title = GString::from("Oops"); - meta.borrow_mut().description = gformat!("Failed to read request: {:?}", e); + meta.borrow_mut().title = Some(gformat!("Oops")); + meta.borrow_mut().description = Some(gformat!("Failed to read request: {:?}", e)); meta.borrow_mut().progress_fraction = 1.0; let _ = widget.activate_action( @@ -291,8 +291,8 @@ impl Page { } Err(e) => { // Update - meta.borrow_mut().title = GString::from("Oops"); - meta.borrow_mut().description = gformat!("Failed to connect: {:?}", e); + meta.borrow_mut().title = Some(gformat!("Oops")); + meta.borrow_mut().description = Some(gformat!("Failed to connect: {:?}", e)); meta.borrow_mut().progress_fraction = 1.0; let _ = widget.activate_action( @@ -308,8 +308,9 @@ impl Page { */ scheme => { // Update - meta.borrow_mut().title = GString::from("Oops"); - meta.borrow_mut().description = gformat!("Protocol {scheme} not supported"); + meta.borrow_mut().title = Some(gformat!("Oops")); + meta.borrow_mut().description = + Some(gformat!("Protocol {scheme} not supported")); meta.borrow_mut().progress_fraction = 1.0; widget @@ -360,11 +361,11 @@ impl Page { } // Getters - pub fn title(&self) -> GString { + pub fn title(&self) -> Option { self.meta.borrow().title.clone() } - pub fn description(&self) -> GString { + pub fn description(&self) -> Option { self.meta.borrow().description.clone() }