Browse Source

use optionals, remove undefined enums

master
yggverse 2 months ago
parent
commit
b3c7545717
  1. 8
      src/browser/main/tab/label/mod.rs
  2. 7
      src/browser/main/tab/label/title/mod.rs
  3. 10
      src/browser/main/tab/mod.rs
  4. 6
      src/browser/main/tab/page/content/mod.rs
  5. 13
      src/browser/main/tab/page/meta.rs
  6. 55
      src/browser/main/tab/page/mod.rs

8
src/browser/main/tab/label/mod.rs

@ -42,8 +42,12 @@ impl Label { @@ -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);
}

7
src/browser/main/tab/label/title/mod.rs

@ -18,8 +18,11 @@ impl Title { @@ -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

10
src/browser/main/tab/mod.rs

@ -142,7 +142,11 @@ impl Tab { @@ -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 { @@ -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 { @@ -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();
}
}
}

6
src/browser/main/tab/page/content/mod.rs

@ -10,13 +10,11 @@ use gtk::{ @@ -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 { @@ -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 { @@ -44,9 +41,6 @@ impl Content {
Mime::TextPlain => {
todo!()
}
Mime::Undefined => {
todo!()
}
}
}

13
src/browser/main/tab/page/meta.rs

@ -1,24 +1,23 @@ @@ -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<GString>,
pub description: Option<GString>,
pub mime: Option<Mime>,
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,
}
}

55
src/browser/main/tab/page/mod.rs

@ -93,9 +93,9 @@ impl Page { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -360,11 +361,11 @@ impl Page {
}
// Getters
pub fn title(&self) -> GString {
pub fn title(&self) -> Option<GString> {
self.meta.borrow().title.clone()
}
pub fn description(&self) -> GString {
pub fn description(&self) -> Option<GString> {
self.meta.borrow().description.clone()
}

Loading…
Cancel
Save