implement notice banner

This commit is contained in:
yggverse 2025-03-03 09:54:02 +02:00
parent a68033bc01
commit 12474b0f44
7 changed files with 71 additions and 16 deletions

View file

@ -12,6 +12,7 @@ use std::rc::Rc;
pub struct Meta {
pub title: Option<String>,
pub notice: Option<String>,
} // @TODO move to separated mod
pub struct Text {
@ -27,7 +28,15 @@ impl Text {
gemtext: &str,
) -> Self {
// Init gemtext reader
let gemini = Gemini::build(actions, base, gemtext).unwrap(); // @TODO handle
let (gemini, notice) = match Gemini::build(actions, base, gemtext) {
Ok(gemini) => (gemini, None),
Err(e) => {
let notice = e.message();
match e {
gemini::Error::Multiline(gemini) => (gemini, Some(notice)),
}
}
};
// Init container widget
let clamp_scrollable = ClampScrollable::builder()
@ -42,6 +51,7 @@ impl Text {
text_view: gemini.text_view,
meta: Meta {
title: gemini.title,
notice,
},
scrolled_window: ScrolledWindow::builder().child(&clamp_scrollable).build(),
}
@ -60,7 +70,10 @@ impl Text {
Self {
scrolled_window: ScrolledWindow::builder().child(&clamp_scrollable).build(),
text_view,
meta: Meta { title: None },
meta: Meta {
title: None,
notice: None,
},
}
}
@ -69,7 +82,10 @@ impl Text {
Self {
scrolled_window: ScrolledWindow::builder().child(&source).build(),
text_view: source.into_text_view(),
meta: Meta { title: None },
meta: Meta {
title: None,
notice: None,
},
}
}
}

View file

@ -234,7 +234,7 @@ impl Gemini {
// Skip other actions for this line
continue;
}
Err(e) => return Err(Error::Gemtext(e.to_string())),
Err(_) => todo!(),
}
}
}
@ -502,6 +502,10 @@ impl Gemini {
}); // @TODO may be expensive for CPU, add timeout?
// Result
Ok(Self { text_view, title })
if is_multiline_enabled {
Ok(Self { text_view, title })
} else {
Err(Error::Multiline(Self { text_view, title }))
}
}
}

View file

@ -1,15 +1,12 @@
use std::fmt::{Display, Formatter, Result};
#[derive(Debug)]
pub enum Error {
Gemtext(String),
Multiline(super::Gemini),
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter) -> Result {
impl Error {
pub fn message(&self) -> String {
match self {
Self::Gemtext(e) => {
write!(f, "Gemtext error: {e}")
Self::Multiline(_) => {
"Invalid multiline markup! Gemtext format partially ignored.".to_string()
}
}
}

View file

@ -0,0 +1,24 @@
use adw::Banner;
pub trait Notice {
fn notice() -> Self;
fn show(&self, title: &str);
}
impl Notice for Banner {
// Constructors
/// Create new `Self`
fn notice() -> Self {
let banner = Banner::builder().button_label("Ok").revealed(false).build();
banner.connect_button_clicked(|this| this.set_revealed(false));
banner
}
// Actions
fn show(&self, title: &str) {
self.set_title(title);
self.set_revealed(true);
}
}