Skip to content

Commit 1126dae

Browse files
committed
Refactor widgets in their own module
1 parent 2243be4 commit 1126dae

File tree

6 files changed

+43
-22
lines changed

6 files changed

+43
-22
lines changed

src/main.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,11 @@ use std::rc::Rc;
1313
mod build_config;
1414
mod common;
1515
mod config;
16-
mod download_page;
1716
mod gemini;
1817
mod lossy_text_read;
1918
mod macros;
20-
mod tab;
2119
mod text_extensions;
22-
mod window;
20+
mod widgets;
2321

2422
use gtk::prelude::*;
2523
async fn read_config() -> anyhow::Result<config::Config> {
@@ -85,7 +83,7 @@ fn main() {
8583
let windows = Rc::new(RefCell::new(vec![]));
8684

8785
application.connect_activate(move |_| {
88-
let window = window::Window::new(&app_clone, config.clone());
86+
let window = widgets::Window::new(&app_clone, config.clone());
8987
window.present();
9088
windows.borrow_mut().push(window);
9189
});
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use glib::subclass::prelude::*;
21
use gtk::glib;
32
use gtk::prelude::*;
43
use gtk::subclass::prelude::*;
@@ -51,3 +50,8 @@ impl DownloadPage {
5150
glib::Object::new(&[]).unwrap()
5251
}
5352
}
53+
impl Default for DownloadPage {
54+
fn default() -> Self {
55+
Self::new()
56+
}
57+
}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use glib::subclass::prelude::*;
21
use gtk::glib;
32
use gtk::prelude::*;
43
use gtk::subclass::prelude::*;
@@ -47,3 +46,8 @@ impl InputPage {
4746
glib::Object::new(&[]).unwrap()
4847
}
4948
}
49+
impl Default for InputPage {
50+
fn default() -> Self {
51+
Self::new()
52+
}
53+
}

src/widgets/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
mod download_page;
2+
mod input_page;
3+
#[allow(clippy::await_holding_refcell_ref)]
4+
mod tab;
5+
mod window;
6+
7+
pub use download_page::DownloadPage;
8+
pub use input_page::InputPage;
9+
pub use tab::Tab;
10+
pub use window::Window;

src/tab.rs renamed to src/widgets/tab.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,19 @@ use std::rc::Rc;
2222
use url::Url;
2323

2424
use crate::common;
25-
use crate::common::{glibctx, HistoryItem, LossyTextRead};
25+
use crate::common::glibctx;
2626
use crate::gemini;
2727
use crate::gemini::PageElement;
28+
use crate::lossy_text_read::*;
2829
use crate::text_extensions::Gemini as GeminiTextExt;
30+
use crate::widgets;
31+
32+
#[derive(Debug, Clone, PartialEq)]
33+
pub struct HistoryItem {
34+
pub url: url::Url,
35+
pub cache: Rc<RefCell<Option<Vec<u8>>>>,
36+
pub scroll_progress: f64,
37+
}
2938

3039
#[derive(Clone, Debug, glib::Boxed, Default)]
3140
#[boxed_type(name = "GeopardHistoryStatus")]
@@ -413,7 +422,7 @@ impl Tab {
413422
.iter()
414423
.find_map(GeminiTextExt::linkhandler)
415424
.cloned()
416-
.ok_or(anyhow::Error::msg("Clicked text doesn't have a link tag"))
425+
.ok_or_else(|| anyhow::Error::msg("Clicked text doesn't have a link tag"))
417426
}
418427
async fn open_file_url(&self, url: Url) -> Result<()> {
419428
let path = url
@@ -459,7 +468,7 @@ impl Tab {
459468
}
460469
async fn open_gemini_url(&self, url: Url) -> anyhow::Result<Option<Vec<u8>>> {
461470
let imp = self.imp();
462-
let res: gemini::Response = imp.gemini_client.borrow().fetch(url.as_str()).await?;
471+
let res: gemini::Response = { imp.gemini_client.borrow().fetch(url.as_str()) }.await?;
463472

464473
use gemini::Status::*;
465474
let meta = res.meta().to_owned();
@@ -536,7 +545,7 @@ impl Tab {
536545
.context("Can't get last url segment")?;
537546
let d_path = Self::download_path(file_name)?;
538547

539-
let page = crate::download_page::DownloadPage::new();
548+
let page = widgets::DownloadPage::new();
540549
page.imp().label.set_label(file_name);
541550
imp.stack.add_child(&page);
542551
imp.stack.set_visible_child(&page);
@@ -547,7 +556,7 @@ impl Tab {
547556
gtk::show_uri(None::<&gtk::Window>, &downloaded_file_url, 0);
548557
});
549558

550-
let ext = file_name.split(".").last();
559+
let ext = file_name.split('.').last();
551560
if let Some(true) = ext.map(|ext| crate::common::STREAMABLE_EXTS.contains(&ext)) {
552561
page.imp().open_btn.set_opacity(1.0);
553562
}
@@ -611,7 +620,7 @@ impl Tab {
611620
fn display_input(&self, url: Url, msg: &str) {
612621
let imp = self.imp();
613622

614-
let text_input = crate::input_page::InputPage::new();
623+
let text_input = widgets::InputPage::new();
615624
imp.stack.add_child(&text_input);
616625
imp.stack.set_visible_child(&text_input);
617626
text_input.imp().label.set_label(msg);
@@ -699,7 +708,7 @@ impl Tab {
699708
if !title_updated {
700709
title_updated = true;
701710
imp.title
702-
.replace(line.trim_end().trim_start_matches("#").to_string());
711+
.replace(line.trim_end().trim_start_matches('#').to_string());
703712
self.notify("title");
704713
}
705714
}

src/window.rs renamed to src/widgets/window.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,19 @@ use glib::{clone, Properties};
66
use gtk::gdk;
77
use gtk::gio;
88
use gtk::glib;
9-
use gtk::prelude::*;
109
use gtk::subclass::prelude::*;
1110
use gtk::CompositeTemplate;
1211
use gtk::TemplateChild;
1312
use log::{error, info, warn};
14-
use std::cell::Cell;
1513
use std::cell::RefCell;
1614
use std::hash::{Hash, Hasher};
1715
use std::marker::PhantomData;
1816
use url::Url;
1917

2018
use crate::common::{bookmarks_url, glibctx, BOOKMARK_FILE_PATH};
2119
use crate::config;
22-
use crate::tab::HistoryStatus;
23-
use crate::tab::Tab;
24-
use crate::{self_action, view};
20+
use crate::self_action;
21+
use crate::widgets::tab::{HistoryStatus, Tab};
2522

2623
pub mod imp {
2724
use super::*;
@@ -360,15 +357,15 @@ impl Window {
360357
}
361358
fn open_omni(&self, v: &str) {
362359
let url = Url::parse(v).or_else(|_| {
363-
if v.contains(".") && v.split(".").all(|s| s.chars().all(char::is_alphanumeric)) {
360+
if v.contains('.') && v.split('.').all(|s| s.chars().all(char::is_alphanumeric)) {
364361
Url::parse(&format!("gemini://{}", v))
365362
} else {
366363
Url::parse(&format!("gemini://geminispace.info/search?{}", v))
367364
}
368365
});
369366
match url {
370367
Ok(url) => self.current_tab().spawn_open_url(url),
371-
Err(e) => error!("Failed to open from omni bar"),
368+
Err(e) => error!("Failed to open from omni bar: {}", e),
372369
}
373370
}
374371
fn open_url_str(&self, v: &str) {
@@ -446,8 +443,7 @@ impl Window {
446443
let imp = self.imp();
447444
imp.squeezer
448445
.visible_child()
449-
.map(|child| child.downcast().ok())
450-
.flatten()
446+
.and_then(|child| child.downcast().ok())
451447
.map(|w: gtk::WindowHandle| w == self.imp().header_small.get())
452448
.unwrap_or(false)
453449
}

0 commit comments

Comments
 (0)