Browse Source

use action object

master
yggverse 2 months ago
parent
commit
ae2bb93ea7
  1. 2
      src/browser/main/tab/page.rs
  2. 21
      src/browser/main/tab/page/content.rs
  3. 7
      src/browser/main/tab/page/content/text.rs
  4. 7
      src/browser/main/tab/page/content/text/gemini.rs
  5. 21
      src/browser/main/tab/page/content/text/gemini/reader.rs

2
src/browser/main/tab/page.rs

@ -52,7 +52,7 @@ impl Page {
action_group.add_action(action_open.as_ref()); action_group.add_action(action_open.as_ref());
// Init components // Init components
let content = Arc::new(Content::new()); let content = Arc::new(Content::new(action_open.clone()));
let navigation = Arc::new(Navigation::new( let navigation = Arc::new(Navigation::new(
navigation_request_text, navigation_request_text,
action_tab_page_reload.clone(), action_tab_page_reload.clone(),

21
src/browser/main/tab/page/content.rs

@ -4,14 +4,17 @@ mod text;
use text::Text; use text::Text;
use gtk::{ use gtk::{
gio::SimpleAction,
glib::{GString, Uri}, glib::{GString, Uri},
prelude::{BoxExt, WidgetExt}, prelude::{BoxExt, WidgetExt},
Box, Orientation, Box, Orientation,
}; };
use std::sync::Arc;
pub enum Mime { pub enum Mime {
TextGemini, TextGemini,
TextPlain, // TextPlain,
} }
pub struct ResetResult { pub struct ResetResult {
@ -19,14 +22,18 @@ pub struct ResetResult {
} }
pub struct Content { pub struct Content {
// GTK
widget: Box, widget: Box,
// Actions
action_open: Arc<SimpleAction>,
} }
impl Content { impl Content {
// Construct // Construct
pub fn new() -> Self { pub fn new(action_open: Arc<SimpleAction>) -> Self {
Self { Self {
widget: Box::builder().orientation(Orientation::Vertical).build(), widget: Box::builder().orientation(Orientation::Vertical).build(),
action_open,
} }
} }
@ -40,17 +47,17 @@ impl Content {
// Re-compose // Re-compose
match mime { match mime {
Mime::TextGemini => { Mime::TextGemini => {
let child = Text::gemini(data, base); let child = Text::gemini(data, base, self.action_open.clone());
self.widget.append(child.widget()); self.widget.append(child.widget());
ResetResult { ResetResult {
title: child.meta_title().clone(), title: child.meta_title().clone(),
} }
} } /* @TODO
Mime::TextPlain => { Mime::TextPlain => {
todo!() todo!()
} } */
} }
} }

7
src/browser/main/tab/page/content/text.rs

@ -3,10 +3,13 @@ mod gemini;
use gemini::Gemini; use gemini::Gemini;
use gtk::{ use gtk::{
gio::SimpleAction,
glib::{GString, Uri}, glib::{GString, Uri},
ScrolledWindow, ScrolledWindow,
}; };
use std::sync::Arc;
pub struct Meta { pub struct Meta {
title: Option<GString>, title: Option<GString>,
} }
@ -18,9 +21,9 @@ pub struct Text {
impl Text { impl Text {
// Construct // Construct
pub fn gemini(gemtext: &str, base: &Uri) -> Self { pub fn gemini(gemtext: &str, base: &Uri, action_open: Arc<SimpleAction>) -> Self {
// Init components // Init components
let gemini = Gemini::new(gemtext, base); let gemini = Gemini::new(gemtext, base, action_open);
// Init meta // Init meta
let meta = Meta { let meta = Meta {

7
src/browser/main/tab/page/content/text/gemini.rs

@ -3,10 +3,13 @@ mod reader;
use reader::Reader; use reader::Reader;
use gtk::{ use gtk::{
gio::SimpleAction,
glib::{GString, Uri}, glib::{GString, Uri},
Viewport, Viewport,
}; };
use std::sync::Arc;
pub struct Gemini { pub struct Gemini {
reader: Reader, reader: Reader,
widget: Viewport, widget: Viewport,
@ -14,9 +17,9 @@ pub struct Gemini {
impl Gemini { impl Gemini {
// Construct // Construct
pub fn new(gemtext: &str, base: &Uri) -> Self { pub fn new(gemtext: &str, base: &Uri, action_open: Arc<SimpleAction>) -> Self {
// Init components // Init components
let reader = Reader::new(gemtext, base); let reader = Reader::new(gemtext, base, action_open);
// Init widget // Init widget
let widget = Viewport::builder().scroll_to_focus(false).build(); let widget = Viewport::builder().scroll_to_focus(false).build();

21
src/browser/main/tab/page/content/text/gemini/reader.rs

@ -5,20 +5,23 @@ use parser::link::Link;
use parser::plain::Plain; use parser::plain::Plain;
use gtk::{ use gtk::{
gio::SimpleAction,
glib::{GString, Propagation, Uri, UriFlags}, glib::{GString, Propagation, Uri, UriFlags},
prelude::{StyleContextExt, ToVariant, WidgetExt}, prelude::{ActionExt, StyleContextExt, ToVariant, WidgetExt},
Align, CssProvider, Label, STYLE_PROVIDER_PRIORITY_APPLICATION, Align, CssProvider, Label, STYLE_PROVIDER_PRIORITY_APPLICATION,
}; };
use std::sync::Arc;
pub struct Reader { pub struct Reader {
title: Option<GString>, title: Option<GString>,
css: CssProvider, // css: CssProvider,
widget: Label, widget: Label,
} }
impl Reader { impl Reader {
// Construct // Construct
pub fn new(gemtext: &str, base: &Uri) -> Self { pub fn new(gemtext: &str, base: &Uri, action_open: Arc<SimpleAction>) -> Self {
// Init title // Init title
let mut title = None; let mut title = None;
@ -82,15 +85,13 @@ impl Reader {
.add_provider(&css, STYLE_PROVIDER_PRIORITY_APPLICATION); .add_provider(&css, STYLE_PROVIDER_PRIORITY_APPLICATION);
// Connect actions // Connect actions
widget.connect_activate_link(|label, href| { widget.connect_activate_link(move |_, href| {
// Detect requested protocol // Detect requested protocol
if let Ok(uri) = Uri::parse(&href, UriFlags::NONE) { if let Ok(uri) = Uri::parse(&href, UriFlags::NONE) {
return match uri.scheme().as_str() { return match uri.scheme().as_str() {
"gemini" => { "gemini" => {
// Open new page // Open new page
label action_open.activate(Some(&uri.to_str().to_variant()));
.activate_action("page.open", Some(&uri.to_str().to_variant()))
.expect("Action `page.open` not found");
// Prevent link open in external application // Prevent link open in external application
Propagation::Stop Propagation::Stop
@ -105,7 +106,11 @@ impl Reader {
}); });
// Result // Result
Self { title, css, widget } Self {
title,
// css,
widget,
}
} }
// Getters // Getters

Loading…
Cancel
Save