From 46fa31ab1645e65acf93964995e8ac4c78781070 Mon Sep 17 00:00:00 2001 From: yggverse Date: Sat, 12 Oct 2024 06:31:05 +0300 Subject: [PATCH] begin Label with TextView replacement --- .../window/tab/item/page/content/text.rs | 2 +- .../tab/item/page/content/text/gemini.rs | 20 +++---- .../item/page/content/text/gemini/reader.rs | 56 ++++++------------- .../page/content/text/gemini/reader/widget.rs | 26 +++++++++ .../item/page/content/text/gemini/widget.rs | 21 +++++++ 5 files changed, 74 insertions(+), 51 deletions(-) create mode 100644 src/app/browser/window/tab/item/page/content/text/gemini/reader/widget.rs create mode 100644 src/app/browser/window/tab/item/page/content/text/gemini/widget.rs diff --git a/src/app/browser/window/tab/item/page/content/text.rs b/src/app/browser/window/tab/item/page/content/text.rs index 2fd92e9f..222b61ec 100644 --- a/src/app/browser/window/tab/item/page/content/text.rs +++ b/src/app/browser/window/tab/item/page/content/text.rs @@ -33,7 +33,7 @@ impl Text { // Init widget let widget = ScrolledWindow::builder().build(); - widget.set_child(Some(gemini.widget())); + widget.set_child(Some(gemini.gobject())); // Result Self { meta, widget } diff --git a/src/app/browser/window/tab/item/page/content/text/gemini.rs b/src/app/browser/window/tab/item/page/content/text/gemini.rs index 00f2930c..a72d5505 100644 --- a/src/app/browser/window/tab/item/page/content/text/gemini.rs +++ b/src/app/browser/window/tab/item/page/content/text/gemini.rs @@ -1,30 +1,30 @@ mod reader; +mod widget; use reader::Reader; +use widget::Widget; use gtk::{ gio::SimpleAction, glib::{GString, Uri}, - Viewport, }; +use adw::ClampScrollable; + use std::sync::Arc; pub struct Gemini { - reader: Reader, - widget: Viewport, + reader: Arc, + widget: Arc, } impl Gemini { // Construct pub fn new(gemtext: &str, base: &Uri, action_page_open: Arc) -> Self { // Init components - let reader = Reader::new(gemtext, base, action_page_open); + let reader = Reader::new_arc(gemtext, base, action_page_open); - // Init widget - let widget = Viewport::builder().scroll_to_focus(false).build(); - - widget.set_child(Some(reader.widget())); + let widget = Widget::new_arc(&reader.gobject()); // Result Self { reader, widget } @@ -35,7 +35,7 @@ impl Gemini { &self.reader.title() } - pub fn widget(&self) -> &Viewport { - &self.widget + pub fn gobject(&self) -> &ClampScrollable { + &self.widget.gobject() } } diff --git a/src/app/browser/window/tab/item/page/content/text/gemini/reader.rs b/src/app/browser/window/tab/item/page/content/text/gemini/reader.rs index 45cdf24d..ff20d6e3 100644 --- a/src/app/browser/window/tab/item/page/content/text/gemini/reader.rs +++ b/src/app/browser/window/tab/item/page/content/text/gemini/reader.rs @@ -1,14 +1,16 @@ mod parser; +mod widget; use parser::header::Header; use parser::link::Link; use parser::plain::Plain; +use widget::Widget; use gtk::{ gio::SimpleAction, - glib::{GString, Propagation, Uri, UriFlags}, - prelude::{ActionExt, StyleContextExt, ToVariant, WidgetExt}, - Align, CssProvider, Label, STYLE_PROVIDER_PRIORITY_APPLICATION, + glib::{GString, Uri}, + prelude::TextBufferExt, + TextBuffer, TextView, }; use std::sync::Arc; @@ -16,12 +18,12 @@ use std::sync::Arc; pub struct Reader { title: Option, // css: CssProvider, - widget: Label, + widget: Arc, } impl Reader { // Construct - pub fn new(gemtext: &str, base: &Uri, action_page_open: Arc) -> Self { + pub fn new_arc(gemtext: &str, base: &Uri, action_page_open: Arc) -> Arc { // Init title let mut title = None; @@ -54,37 +56,15 @@ impl Reader { markup.push_str(Plain::from(line).markup()) } - // Init CSS - let css = CssProvider::new(); - - /* @TODO Theme parser error: - css.load_from_path( - "src/browser/main/tab/page/content/text/gemini/reader/default.css" - ); */ - - css.load_from_data("label{caret-color: transparent;}"); + // Init buffer @TODO + let buffer = TextBuffer::new(None); + buffer.set_text(&markup); // Init widget - let widget = Label::builder() - .halign(Align::Fill) - .valign(Align::Fill) - .hexpand(true) - .vexpand(true) - .xalign(0.0) - .yalign(0.0) - .margin_start(8) - .margin_end(8) - .wrap(true) - .selectable(true) - .use_markup(true) - .label(markup) - .build(); - - widget - .style_context() - .add_provider(&css, STYLE_PROVIDER_PRIORITY_APPLICATION); + let widget = Widget::new_arc(&buffer); // Connect actions + /* @TODO widget.connect_activate_link(move |_, href| { // Detect requested protocol if let Ok(uri) = Uri::parse(&href, UriFlags::NONE) { @@ -103,14 +83,10 @@ impl Reader { // Delegate unparsable Propagation::Proceed - }); + }); */ // Result - Self { - title, - // css, - widget, - } + Arc::new(Self { title, widget }) } // Getters @@ -118,7 +94,7 @@ impl Reader { &self.title } - pub fn widget(&self) -> &Label { - &self.widget + pub fn gobject(&self) -> &TextView { + &self.widget.gobject() } } diff --git a/src/app/browser/window/tab/item/page/content/text/gemini/reader/widget.rs b/src/app/browser/window/tab/item/page/content/text/gemini/reader/widget.rs new file mode 100644 index 00000000..5f535872 --- /dev/null +++ b/src/app/browser/window/tab/item/page/content/text/gemini/reader/widget.rs @@ -0,0 +1,26 @@ +use gtk::{TextBuffer, TextView, WrapMode}; +use std::sync::Arc; + +pub struct Widget { + gobject: TextView, +} + +impl Widget { + // Construct + pub fn new_arc(buffer: &TextBuffer) -> Arc { + Arc::new(Self { + gobject: TextView::builder() + .editable(false) + .cursor_visible(false) + .wrap_mode(WrapMode::Word) + .vexpand(true) + .buffer(buffer) + .build(), + }) + } + + // Getters + pub fn gobject(&self) -> &TextView { + &self.gobject + } +} diff --git a/src/app/browser/window/tab/item/page/content/text/gemini/widget.rs b/src/app/browser/window/tab/item/page/content/text/gemini/widget.rs new file mode 100644 index 00000000..896e0b7f --- /dev/null +++ b/src/app/browser/window/tab/item/page/content/text/gemini/widget.rs @@ -0,0 +1,21 @@ +use adw::ClampScrollable; +use gtk::TextView; +use std::sync::Arc; + +pub struct Widget { + gobject: ClampScrollable, +} + +impl Widget { + // Construct + pub fn new_arc(child: &TextView) -> Arc { + Arc::new(Self { + gobject: ClampScrollable::builder().child(child).build(), + }) + } + + // Getters + pub fn gobject(&self) -> &ClampScrollable { + &self.gobject + } +}