begin Label with TextView replacement

This commit is contained in:
yggverse 2024-10-12 06:31:05 +03:00
parent 5798b990ad
commit 46fa31ab16
5 changed files with 74 additions and 51 deletions

View File

@ -33,7 +33,7 @@ impl Text {
// Init widget // Init widget
let widget = ScrolledWindow::builder().build(); let widget = ScrolledWindow::builder().build();
widget.set_child(Some(gemini.widget())); widget.set_child(Some(gemini.gobject()));
// Result // Result
Self { meta, widget } Self { meta, widget }

View File

@ -1,30 +1,30 @@
mod reader; mod reader;
mod widget;
use reader::Reader; use reader::Reader;
use widget::Widget;
use gtk::{ use gtk::{
gio::SimpleAction, gio::SimpleAction,
glib::{GString, Uri}, glib::{GString, Uri},
Viewport,
}; };
use adw::ClampScrollable;
use std::sync::Arc; use std::sync::Arc;
pub struct Gemini { pub struct Gemini {
reader: Reader, reader: Arc<Reader>,
widget: Viewport, widget: Arc<Widget>,
} }
impl Gemini { impl Gemini {
// Construct // Construct
pub fn new(gemtext: &str, base: &Uri, action_page_open: Arc<SimpleAction>) -> Self { pub fn new(gemtext: &str, base: &Uri, action_page_open: Arc<SimpleAction>) -> Self {
// Init components // 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 = Widget::new_arc(&reader.gobject());
let widget = Viewport::builder().scroll_to_focus(false).build();
widget.set_child(Some(reader.widget()));
// Result // Result
Self { reader, widget } Self { reader, widget }
@ -35,7 +35,7 @@ impl Gemini {
&self.reader.title() &self.reader.title()
} }
pub fn widget(&self) -> &Viewport { pub fn gobject(&self) -> &ClampScrollable {
&self.widget &self.widget.gobject()
} }
} }

View File

@ -1,14 +1,16 @@
mod parser; mod parser;
mod widget;
use parser::header::Header; use parser::header::Header;
use parser::link::Link; use parser::link::Link;
use parser::plain::Plain; use parser::plain::Plain;
use widget::Widget;
use gtk::{ use gtk::{
gio::SimpleAction, gio::SimpleAction,
glib::{GString, Propagation, Uri, UriFlags}, glib::{GString, Uri},
prelude::{ActionExt, StyleContextExt, ToVariant, WidgetExt}, prelude::TextBufferExt,
Align, CssProvider, Label, STYLE_PROVIDER_PRIORITY_APPLICATION, TextBuffer, TextView,
}; };
use std::sync::Arc; use std::sync::Arc;
@ -16,12 +18,12 @@ use std::sync::Arc;
pub struct Reader { pub struct Reader {
title: Option<GString>, title: Option<GString>,
// css: CssProvider, // css: CssProvider,
widget: Label, widget: Arc<Widget>,
} }
impl Reader { impl Reader {
// Construct // Construct
pub fn new(gemtext: &str, base: &Uri, action_page_open: Arc<SimpleAction>) -> Self { pub fn new_arc(gemtext: &str, base: &Uri, action_page_open: Arc<SimpleAction>) -> Arc<Self> {
// Init title // Init title
let mut title = None; let mut title = None;
@ -54,37 +56,15 @@ impl Reader {
markup.push_str(Plain::from(line).markup()) markup.push_str(Plain::from(line).markup())
} }
// Init CSS // Init buffer @TODO
let css = CssProvider::new(); let buffer = TextBuffer::new(None);
buffer.set_text(&markup);
/* @TODO Theme parser error: <broken file>
css.load_from_path(
"src/browser/main/tab/page/content/text/gemini/reader/default.css"
); */
css.load_from_data("label{caret-color: transparent;}");
// Init widget // Init widget
let widget = Label::builder() let widget = Widget::new_arc(&buffer);
.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);
// Connect actions // Connect actions
/* @TODO
widget.connect_activate_link(move |_, 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) {
@ -103,14 +83,10 @@ impl Reader {
// Delegate unparsable // Delegate unparsable
Propagation::Proceed Propagation::Proceed
}); }); */
// Result // Result
Self { Arc::new(Self { title, widget })
title,
// css,
widget,
}
} }
// Getters // Getters
@ -118,7 +94,7 @@ impl Reader {
&self.title &self.title
} }
pub fn widget(&self) -> &Label { pub fn gobject(&self) -> &TextView {
&self.widget &self.widget.gobject()
} }
} }

View File

@ -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<Self> {
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
}
}

View File

@ -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<Self> {
Arc::new(Self {
gobject: ClampScrollable::builder().child(child).build(),
})
}
// Getters
pub fn gobject(&self) -> &ClampScrollable {
&self.gobject
}
}