return components constructed, add documentation comments

This commit is contained in:
yggverse 2024-10-30 20:21:36 +02:00
parent 56744fd695
commit 54f09e80fb

View file

@ -9,7 +9,7 @@ use text::Text;
use gtk::{ use gtk::{
gdk_pixbuf::Pixbuf, gdk_pixbuf::Pixbuf,
gio::SimpleAction, gio::SimpleAction,
glib::{GString, Uri}, glib::Uri,
prelude::{BoxExt, WidgetExt}, prelude::{BoxExt, WidgetExt},
Box, Orientation, Box, Orientation,
}; };
@ -25,6 +25,8 @@ pub struct Content {
impl Content { impl Content {
// Construct // Construct
/// Create new container for different components
pub fn new(action_tab_open: SimpleAction, action_page_open: SimpleAction) -> Self { pub fn new(action_tab_open: SimpleAction, action_page_open: SimpleAction) -> Self {
Self { Self {
gobject: Box::builder().orientation(Orientation::Vertical).build(), gobject: Box::builder().orientation(Orientation::Vertical).build(),
@ -34,12 +36,20 @@ impl Content {
} }
// Actions // Actions
pub fn set_image(&self, buffer: &Pixbuf) {
/// Set new `content::Image` component for `Self`
///
/// * action removes previous children component from `Self`
pub fn set_image(&self, buffer: &Pixbuf) -> Image {
self.clean(); self.clean();
let image = Image::new_from_pixbuf(buffer); let image = Image::new_from_pixbuf(buffer);
self.gobject.append(image.gobject()); self.gobject.append(image.gobject());
image
} }
/// Set new `content::Status` component for `Self` with new `status::Failure` preset
///
/// * action removes previous children component from `Self`
pub fn set_status_failure(&self, title: Option<&str>, description: Option<&str>) -> Status { pub fn set_status_failure(&self, title: Option<&str>, description: Option<&str>) -> Status {
self.clean(); self.clean();
let status = Status::new_failure(title, description); let status = Status::new_failure(title, description);
@ -47,7 +57,9 @@ impl Content {
status status
} }
/// Loading placeholder /// Set new `content::Status` component for `Self` with new `status::Loading` preset
///
/// * action removes previous children component from `Self`
pub fn set_status_loading( pub fn set_status_loading(
&self, &self,
title: Option<&str>, title: Option<&str>,
@ -60,8 +72,11 @@ impl Content {
status status
} }
/// Default reading widget for [Gemtext](https://geminiprotocol.net/docs/gemtext.gmi), /// Set new `content::Text` component for `Self` with new `text::Gemini` preset
/// removes previous children component. ///
/// Useful as reader for [Gemtext](https://geminiprotocol.net/docs/gemtext.gmi)
///
/// * action removes previous children component from `Self`
/// ///
/// **Arguments** /// **Arguments**
/// ///
@ -70,23 +85,21 @@ impl Content {
/// ///
/// **Return** /// **Return**
/// ///
/// `GString` copy of any header found in `data` parsed, `None` otherwise /// `Text` component with it public API
/// * header useful as window, tab title or any other UI related to content loaded /// * could be useful to extract document title parsed from Gemtext
pub fn set_text_gemini(&self, base: &Uri, data: &str) -> Option<GString> { pub fn set_text_gemini(&self, base: &Uri, data: &str) -> Text {
self.clean(); self.clean();
let text = Text::gemini(
let text_gemini = Text::gemini(
data, data,
base, base,
self.action_tab_open.clone(), self.action_tab_open.clone(),
self.action_page_open.clone(), self.action_page_open.clone(),
); );
self.gobject.append(text.gobject());
self.gobject.append(text_gemini.gobject()); text
text_gemini.meta_title().clone()
} }
/// Remove all children components from `Self`
pub fn clean(&self) { pub fn clean(&self) {
while let Some(child) = self.gobject.last_child() { while let Some(child) = self.gobject.last_child() {
self.gobject.remove(&child); self.gobject.remove(&child);
@ -94,6 +107,8 @@ impl Content {
} }
// Getters // Getters
/// Get reference to `Self` gobject
pub fn gobject(&self) -> &Box { pub fn gobject(&self) -> &Box {
&self.gobject &self.gobject
} }