add text/plain content type support

This commit is contained in:
yggverse 2025-02-04 15:15:12 +02:00
parent 63d2821c07
commit f2da250376
6 changed files with 75 additions and 64 deletions

View file

@ -1,10 +1,12 @@
mod gemini;
mod plain;
mod source;
use super::{ItemAction, WindowAction};
use adw::ClampScrollable;
use gemini::Gemini;
use gtk::{glib::Uri, prelude::Cast, ScrolledWindow, TextView};
use plain::Plain;
use source::Source;
use std::rc::Rc;
@ -34,19 +36,7 @@ impl Text {
.maximum_size(800)
.build();
// Grab focus into the `TextView` on click empty `ClampScrollable` area
{
use gtk::{prelude::WidgetExt, GestureClick};
let controller = GestureClick::new();
controller.connect_pressed({
let text_view = gemini.text_view.clone();
move |_, _, _, _| {
text_view.grab_focus();
}
});
clamp_scrollable.add_controller(controller);
}
grab_focus_patch(&clamp_scrollable, &gemini.text_view);
Self {
text_view: gemini.text_view,
@ -57,6 +47,22 @@ impl Text {
}
}
pub fn plain(data: &str) -> Self {
let text_view = TextView::plain(data);
let clamp_scrollable = ClampScrollable::builder()
.child(&text_view)
.css_classes(["view"])
.build();
grab_focus_patch(&clamp_scrollable, &text_view);
Self {
scrolled_window: ScrolledWindow::builder().child(&clamp_scrollable).build(),
text_view,
meta: Meta { title: None },
}
}
pub fn source(data: &str) -> Self {
let source = Source::new(data);
Self {
@ -66,3 +72,20 @@ impl Text {
}
}
}
// Tools
// Grab focus into the `TextView` on click empty `ClampScrollable` area
fn grab_focus_patch(clamp_scrollable: &ClampScrollable, text_view: &TextView) {
use gtk::{prelude::WidgetExt, GestureClick};
let controller = GestureClick::new();
controller.connect_pressed({
let text_view = text_view.clone();
move |_, _, _, _| {
text_view.grab_focus();
}
});
clamp_scrollable.add_controller(controller);
}

View file

@ -0,0 +1,22 @@
use gtk::{TextBuffer, TextView};
pub trait Plain {
fn plain(data: &str) -> Self;
}
impl Plain for TextView {
fn plain(data: &str) -> Self {
const MARGIN: i32 = 8;
TextView::builder()
.bottom_margin(MARGIN)
.cursor_visible(false)
.buffer(&TextBuffer::builder().text(data).build())
.editable(false)
.left_margin(MARGIN)
.monospace(true)
.right_margin(MARGIN)
.top_margin(MARGIN)
.vexpand(true)
.build()
}
}

View file

@ -1,33 +0,0 @@
use gtk::{Align, Label};
pub struct Reader {
widget: Label,
}
impl Default for Reader {
fn default() -> Self {
Self::new()
}
}
impl Reader {
// Construct
pub fn new() -> Self {
Self {
widget: Label::builder()
.halign(Align::Start)
.valign(Align::Start)
.margin_start(8)
.margin_end(8)
.wrap(true)
.selectable(true)
.use_markup(true)
.build(),
}
}
// Getters
pub fn widget(&self) -> &Label {
&self.widget
}
}