From ae2bb93ea73fd100ca7e059b293dcfa9dc84337d Mon Sep 17 00:00:00 2001 From: yggverse Date: Sat, 28 Sep 2024 03:54:14 +0300 Subject: [PATCH] use action object --- src/browser/main/tab/page.rs | 2 +- src/browser/main/tab/page/content.rs | 21 ++++++++++++------- src/browser/main/tab/page/content/text.rs | 7 +++++-- .../main/tab/page/content/text/gemini.rs | 7 +++++-- .../tab/page/content/text/gemini/reader.rs | 21 ++++++++++++------- 5 files changed, 38 insertions(+), 20 deletions(-) diff --git a/src/browser/main/tab/page.rs b/src/browser/main/tab/page.rs index eed9fd0f..c4773f64 100644 --- a/src/browser/main/tab/page.rs +++ b/src/browser/main/tab/page.rs @@ -52,7 +52,7 @@ impl Page { action_group.add_action(action_open.as_ref()); // Init components - let content = Arc::new(Content::new()); + let content = Arc::new(Content::new(action_open.clone())); let navigation = Arc::new(Navigation::new( navigation_request_text, action_tab_page_reload.clone(), diff --git a/src/browser/main/tab/page/content.rs b/src/browser/main/tab/page/content.rs index f305623d..805a5e0b 100644 --- a/src/browser/main/tab/page/content.rs +++ b/src/browser/main/tab/page/content.rs @@ -4,14 +4,17 @@ mod text; use text::Text; use gtk::{ + gio::SimpleAction, glib::{GString, Uri}, prelude::{BoxExt, WidgetExt}, Box, Orientation, }; +use std::sync::Arc; + pub enum Mime { TextGemini, - TextPlain, + // TextPlain, } pub struct ResetResult { @@ -19,14 +22,18 @@ pub struct ResetResult { } pub struct Content { + // GTK widget: Box, + // Actions + action_open: Arc, } impl Content { // Construct - pub fn new() -> Self { + pub fn new(action_open: Arc) -> Self { Self { widget: Box::builder().orientation(Orientation::Vertical).build(), + action_open, } } @@ -40,17 +47,17 @@ impl Content { // Re-compose match mime { Mime::TextGemini => { - let child = Text::gemini(data, base); + let child = Text::gemini(data, base, self.action_open.clone()); self.widget.append(child.widget()); ResetResult { title: child.meta_title().clone(), } - } - Mime::TextPlain => { - todo!() - } + } /* @TODO + Mime::TextPlain => { + todo!() + } */ } } diff --git a/src/browser/main/tab/page/content/text.rs b/src/browser/main/tab/page/content/text.rs index f4308d13..0970619b 100644 --- a/src/browser/main/tab/page/content/text.rs +++ b/src/browser/main/tab/page/content/text.rs @@ -3,10 +3,13 @@ mod gemini; use gemini::Gemini; use gtk::{ + gio::SimpleAction, glib::{GString, Uri}, ScrolledWindow, }; +use std::sync::Arc; + pub struct Meta { title: Option, } @@ -18,9 +21,9 @@ pub struct Text { impl Text { // Construct - pub fn gemini(gemtext: &str, base: &Uri) -> Self { + pub fn gemini(gemtext: &str, base: &Uri, action_open: Arc) -> Self { // Init components - let gemini = Gemini::new(gemtext, base); + let gemini = Gemini::new(gemtext, base, action_open); // Init meta let meta = Meta { diff --git a/src/browser/main/tab/page/content/text/gemini.rs b/src/browser/main/tab/page/content/text/gemini.rs index 04985c2b..dc7bca70 100644 --- a/src/browser/main/tab/page/content/text/gemini.rs +++ b/src/browser/main/tab/page/content/text/gemini.rs @@ -3,10 +3,13 @@ mod reader; use reader::Reader; use gtk::{ + gio::SimpleAction, glib::{GString, Uri}, Viewport, }; +use std::sync::Arc; + pub struct Gemini { reader: Reader, widget: Viewport, @@ -14,9 +17,9 @@ pub struct Gemini { impl Gemini { // Construct - pub fn new(gemtext: &str, base: &Uri) -> Self { + pub fn new(gemtext: &str, base: &Uri, action_open: Arc) -> Self { // Init components - let reader = Reader::new(gemtext, base); + let reader = Reader::new(gemtext, base, action_open); // Init widget let widget = Viewport::builder().scroll_to_focus(false).build(); diff --git a/src/browser/main/tab/page/content/text/gemini/reader.rs b/src/browser/main/tab/page/content/text/gemini/reader.rs index 37eefd20..62337942 100644 --- a/src/browser/main/tab/page/content/text/gemini/reader.rs +++ b/src/browser/main/tab/page/content/text/gemini/reader.rs @@ -5,20 +5,23 @@ use parser::link::Link; use parser::plain::Plain; use gtk::{ + gio::SimpleAction, glib::{GString, Propagation, Uri, UriFlags}, - prelude::{StyleContextExt, ToVariant, WidgetExt}, + prelude::{ActionExt, StyleContextExt, ToVariant, WidgetExt}, Align, CssProvider, Label, STYLE_PROVIDER_PRIORITY_APPLICATION, }; +use std::sync::Arc; + pub struct Reader { title: Option, - css: CssProvider, + // css: CssProvider, widget: Label, } impl Reader { // Construct - pub fn new(gemtext: &str, base: &Uri) -> Self { + pub fn new(gemtext: &str, base: &Uri, action_open: Arc) -> Self { // Init title let mut title = None; @@ -82,15 +85,13 @@ impl Reader { .add_provider(&css, STYLE_PROVIDER_PRIORITY_APPLICATION); // Connect actions - widget.connect_activate_link(|label, href| { + widget.connect_activate_link(move |_, href| { // Detect requested protocol if let Ok(uri) = Uri::parse(&href, UriFlags::NONE) { return match uri.scheme().as_str() { "gemini" => { // Open new page - label - .activate_action("page.open", Some(&uri.to_str().to_variant())) - .expect("Action `page.open` not found"); + action_open.activate(Some(&uri.to_str().to_variant())); // Prevent link open in external application Propagation::Stop @@ -105,7 +106,11 @@ impl Reader { }); // Result - Self { title, css, widget } + Self { + title, + // css, + widget, + } } // Getters