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 {
} }
// Actions // Actions
pub fn update(&self, title: &GString) { pub fn update(&self, title: Option<&GString>) {
self.widget.set_tooltip_text(Some(title)); match title {
Some(tooltip_text) => self.widget.set_tooltip_text(Some(tooltip_text)),
None => self.widget.set_tooltip_text(None),
}
self.title.update(title); self.title.update(title);
} }

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

@ -18,8 +18,11 @@ impl Title {
} }
// Actions // Actions
pub fn update(&self, title: &GString) { pub fn update(&self, title: Option<&GString>) {
self.widget.set_text(title); match title {
Some(title) => self.widget.set_text(title),
None => self.widget.set_text(""), // @TODO None/false option
}
} }
// Getters // Getters

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

@ -142,7 +142,11 @@ impl Tab {
// Get label by widget ID // Get label by widget ID
if let Some(label) = self.labels.borrow().get(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(); let id = &widget.widget_name();
// Get page by widget ID // Get page by widget ID
if let Some(page) = self.pages.borrow().get(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(); let id = &widget.widget_name();
// Get page by widget ID // Get page by widget ID
if let Some(page) = self.pages.borrow().get(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::{
}; };
pub enum Mime { pub enum Mime {
Undefined,
TextGemini, TextGemini,
TextPlain, TextPlain,
} }
pub struct Content { pub struct Content {
mime: Mime,
widget: Box, widget: Box,
} }
@ -24,7 +22,6 @@ impl Content {
// Construct // Construct
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
mime: Mime::Undefined,
widget: Box::builder().orientation(Orientation::Vertical).build(), widget: Box::builder().orientation(Orientation::Vertical).build(),
} }
} }
@ -44,9 +41,6 @@ impl Content {
Mime::TextPlain => { Mime::TextPlain => {
todo!() todo!()
} }
Mime::Undefined => {
todo!()
}
} }
} }

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

@ -1,24 +1,23 @@
use gtk::glib::GString; use gtk::glib::GString;
pub enum Mime { pub enum Mime {
Undefined,
TextGemini, TextGemini,
TextPlain, TextPlain,
} }
pub struct Meta { pub struct Meta {
pub title: GString, pub title: Option<GString>,
pub description: GString, pub description: Option<GString>,
pub mime: Mime, pub mime: Option<Mime>,
pub progress_fraction: f32, pub progress_fraction: f32,
} }
impl Meta { impl Meta {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
title: GString::new(), title: None,
description: GString::new(), description: None,
mime: Mime::Undefined, mime: None,
progress_fraction: 0.0, progress_fraction: 0.0,
} }
} }

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

@ -93,9 +93,9 @@ impl Page {
let widget = self.widget.clone(); let widget = self.widget.clone();
// Update // Update
meta.borrow_mut().mime = Mime::Undefined; meta.borrow_mut().mime = None;
meta.borrow_mut().title = gformat!("Loading.."); meta.borrow_mut().title = Some(gformat!("Loading.."));
meta.borrow_mut().description = gformat!("Loading.."); meta.borrow_mut().description = None;
meta.borrow_mut().progress_fraction = 0.0; meta.borrow_mut().progress_fraction = 0.0;
widget widget
@ -119,7 +119,7 @@ impl Page {
// Update // Update
meta.borrow_mut().progress_fraction = 0.25; meta.borrow_mut().progress_fraction = 0.25;
meta.borrow_mut().description = gformat!("Connect {host}.."); meta.borrow_mut().description = Some(gformat!("Connect {host}.."));
widget widget
.activate_action("win.update", None) .activate_action("win.update", None)
@ -142,7 +142,7 @@ impl Page {
Ok(connection) => { Ok(connection) => {
// Update // Update
meta.borrow_mut().progress_fraction = 0.50; 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"); widget.activate_action("win.update", None).expect("Action `win.update` not found");
@ -155,7 +155,7 @@ impl Page {
Ok(_) => { Ok(_) => {
// Update // Update
meta.borrow_mut().progress_fraction = 0.75; 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"); widget.activate_action("win.update", None).expect("Action `win.update` not found");
@ -172,15 +172,15 @@ impl Page {
Ok(data) => { Ok(data) => {
// Format response // Format response
meta.borrow_mut().progress_fraction = 1.0; meta.borrow_mut().progress_fraction = 1.0;
meta.borrow_mut().description = host; meta.borrow_mut().description = Some(host);
meta.borrow_mut().title = uri.path(); meta.borrow_mut().title = Some(uri.path());
// Try create short base for title // Try create short base for title
let path = uri.path(); let path = uri.path();
let path = Path::new(&path); let path = Path::new(&path);
if let Some(base) = path.file_name() { if let Some(base) = path.file_name() {
if let Some(base_str) = base.to_str() { 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() { Some(mime) => match mime.as_str() {
"text/gemini" => { "text/gemini" => {
// Update meta // Update meta
meta.borrow_mut().mime = Mime::TextGemini; meta.borrow_mut().mime = Some(Mime::TextGemini);
// Select widget // Select widget
match parts.get(4) { match parts.get(4) {
Some(source) => content.reset(content::Mime::TextGemini, &uri, &source), Some(source) => content.reset(content::Mime::TextGemini, &uri, &source),
@ -208,12 +208,12 @@ impl Page {
} }
}, },
"text/plain" => { "text/plain" => {
meta.borrow_mut().mime = Mime::TextPlain; meta.borrow_mut().mime = Some(Mime::TextPlain);
todo!() todo!()
}, },
_ => { _ => {
meta.borrow_mut().title = GString::from("Oops"); meta.borrow_mut().title = Some(gformat!("Oops"));
meta.borrow_mut().description = gformat!("Content {mime} not supported"); meta.borrow_mut().description = Some(gformat!("Content {mime} not supported"));
}, },
} }
None => todo!(), None => todo!(),
@ -221,8 +221,8 @@ impl Page {
// @TODO // @TODO
}, },
_ => { _ => {
meta.borrow_mut().title = GString::from("Oops"); meta.borrow_mut().title = Some(gformat!("Oops"));
meta.borrow_mut().description = gformat!("Status {code} not supported"); meta.borrow_mut().description = Some(gformat!("Status {code} not supported"));
}, },
} }
None => todo!(), None => todo!(),
@ -235,8 +235,8 @@ impl Page {
); );
} }
Err(e) => { Err(e) => {
meta.borrow_mut().title = GString::from("Oops"); meta.borrow_mut().title = Some(gformat!("Oops"));
meta.borrow_mut().description = gformat!("Failed to read buffer data: {e}"); meta.borrow_mut().description = Some(gformat!("Failed to read buffer data: {e}"));
meta.borrow_mut().progress_fraction = 1.0; meta.borrow_mut().progress_fraction = 1.0;
let _ = widget.activate_action( let _ = widget.activate_action(
@ -253,8 +253,8 @@ impl Page {
} }
Err(e) => { Err(e) => {
// Update // Update
meta.borrow_mut().title = GString::from("Oops"); meta.borrow_mut().title = Some(gformat!("Oops"));
meta.borrow_mut().description = gformat!("Failed to read response: {:?}", e); meta.borrow_mut().description = Some(gformat!("Failed to read response: {:?}", e));
meta.borrow_mut().progress_fraction = 1.0; meta.borrow_mut().progress_fraction = 1.0;
let _ = widget.activate_action( let _ = widget.activate_action(
@ -272,8 +272,8 @@ impl Page {
} }
Err(e) => { Err(e) => {
// Update // Update
meta.borrow_mut().title = GString::from("Oops"); meta.borrow_mut().title = Some(gformat!("Oops"));
meta.borrow_mut().description = gformat!("Failed to read request: {:?}", e); meta.borrow_mut().description = Some(gformat!("Failed to read request: {:?}", e));
meta.borrow_mut().progress_fraction = 1.0; meta.borrow_mut().progress_fraction = 1.0;
let _ = widget.activate_action( let _ = widget.activate_action(
@ -291,8 +291,8 @@ impl Page {
} }
Err(e) => { Err(e) => {
// Update // Update
meta.borrow_mut().title = GString::from("Oops"); meta.borrow_mut().title = Some(gformat!("Oops"));
meta.borrow_mut().description = gformat!("Failed to connect: {:?}", e); meta.borrow_mut().description = Some(gformat!("Failed to connect: {:?}", e));
meta.borrow_mut().progress_fraction = 1.0; meta.borrow_mut().progress_fraction = 1.0;
let _ = widget.activate_action( let _ = widget.activate_action(
@ -308,8 +308,9 @@ impl Page {
*/ */
scheme => { scheme => {
// Update // Update
meta.borrow_mut().title = GString::from("Oops"); meta.borrow_mut().title = Some(gformat!("Oops"));
meta.borrow_mut().description = gformat!("Protocol {scheme} not supported"); meta.borrow_mut().description =
Some(gformat!("Protocol {scheme} not supported"));
meta.borrow_mut().progress_fraction = 1.0; meta.borrow_mut().progress_fraction = 1.0;
widget widget
@ -360,11 +361,11 @@ impl Page {
} }
// Getters // Getters
pub fn title(&self) -> GString { pub fn title(&self) -> Option<GString> {
self.meta.borrow().title.clone() self.meta.borrow().title.clone()
} }
pub fn description(&self) -> GString { pub fn description(&self) -> Option<GString> {
self.meta.borrow().description.clone() self.meta.borrow().description.clone()
} }

Loading…
Cancel
Save