draft gemtext composer

This commit is contained in:
yggverse 2024-09-26 14:30:54 +03:00
parent 70f3b51591
commit 1cc3ddaf2e
5 changed files with 89 additions and 8 deletions

View File

@ -1,6 +1,18 @@
use gtk::{Box, Orientation}; // @TODO mod image;
mod text;
use text::Text;
use gtk::{prelude::BoxExt, Box, Orientation};
pub enum Mime {
Undefined,
TextGemini,
TextPlain,
}
pub struct Content { pub struct Content {
mime: Mime,
widget: Box, widget: Box,
} }
@ -8,10 +20,28 @@ impl Content {
// Construct // Construct
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
mime: Mime::Undefined,
widget: Box::builder().orientation(Orientation::Vertical).build(), widget: Box::builder().orientation(Orientation::Vertical).build(),
} }
} }
// Actions
pub fn reset(&self, mime: Mime, data: &str) {
//self.widget.remove(self.child.widget());
match mime {
Mime::TextGemini => {
let child = Text::gemini(data);
self.widget.append(child.widget());
}
Mime::TextPlain => {
todo!()
}
Mime::Undefined => {
todo!()
}
}
}
// Getters // Getters
pub fn widget(&self) -> &Box { pub fn widget(&self) -> &Box {
&self.widget &self.widget

View File

@ -0,0 +1,30 @@
mod reader;
use reader::Reader;
use gtk::Viewport;
pub struct Gemini {
widget: Viewport,
}
impl Gemini {
// Construct
pub fn new(gemtext: &str) -> Self {
// Init components
let reader = Reader::new(gemtext);
// Init widget
let widget = Viewport::builder().scroll_to_focus(false).build();
widget.set_child(Some(reader.widget()));
// Result
Self { widget }
}
// Getters
pub fn widget(&self) -> &Viewport {
&self.widget
}
}

View File

@ -6,16 +6,18 @@ pub struct Reader {
impl Reader { impl Reader {
// Construct // Construct
pub fn new() -> Self { pub fn new(gemtext: &str) -> Self {
Self { Self {
widget: Label::builder() widget: Label::builder()
.halign(Align::Start) .halign(Align::Start)
.valign(Align::Start) .valign(Align::Start)
.vexpand(true)
.margin_start(8) .margin_start(8)
.margin_end(8) .margin_end(8)
.wrap(true) .wrap(true)
.selectable(true) .selectable(true)
.use_markup(true) .use_markup(true)
.label(gemtext) // @TODO
.build(), .build(),
} }
} }

View File

@ -1,3 +1,7 @@
mod gemini;
use gemini::Gemini;
use gtk::ScrolledWindow; use gtk::ScrolledWindow;
pub struct Text { pub struct Text {
@ -6,10 +10,17 @@ pub struct Text {
impl Text { impl Text {
// Construct // Construct
pub fn new() -> Self { pub fn gemini(gemtext: &str) -> Self {
Self { // Init components
widget: ScrolledWindow::builder().build(), let gemini = Gemini::new(gemtext);
}
// Init widget
let widget = ScrolledWindow::builder().build();
widget.set_child(Some(gemini.widget()));
// Result
Self { widget }
} }
// Getters // Getters

View File

@ -61,6 +61,7 @@ impl Page {
let request_text = self.navigation.request_text(); let request_text = self.navigation.request_text();
// Init shared objects for async access // Init shared objects for async access
let content = self.content.clone();
let meta = self.meta.clone(); let meta = self.meta.clone();
let widget = self.widget.clone(); let widget = self.widget.clone();
@ -148,6 +149,8 @@ impl Page {
RegexMatchFlags::DEFAULT, RegexMatchFlags::DEFAULT,
); );
println!("{:?}",parts);
// https://geminiprotocol.net/docs/protocol-specification.gmi#status-codes // https://geminiprotocol.net/docs/protocol-specification.gmi#status-codes
match parts.get(1) { match parts.get(1) {
Some(code) => match code.as_str() { Some(code) => match code.as_str() {
@ -155,12 +158,17 @@ impl Page {
match parts.get(2) { match parts.get(2) {
Some(mime) => match mime.as_str() { Some(mime) => match mime.as_str() {
"text/gemini" => { "text/gemini" => {
// Update meta
meta.borrow_mut().mime = Mime::TextGemini; meta.borrow_mut().mime = Mime::TextGemini;
// @TODO // Select widget
match parts.get(4) {
Some(source) => content.reset(content::Mime::TextGemini, source),
None => todo!(),
}
}, },
"text/plain" => { "text/plain" => {
meta.borrow_mut().mime = Mime::TextPlain; meta.borrow_mut().mime = Mime::TextPlain;
// @TODO todo!()
}, },
_ => { _ => {
meta.borrow_mut().title = GString::from("Oops"); meta.borrow_mut().title = GString::from("Oops");