1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
mod image;
mod status;
mod text;
use image::Image;
use status::Status;
use text::Text;
use crate::app::browser::window::{tab::item::Action as TabAction, Action as WindowAction};
use gtk::{
gdk::Paintable,
gio::{Cancellable, File},
glib::Uri,
prelude::{BoxExt, IsA, WidgetExt},
Box, Orientation,
};
use std::{rc::Rc, time::Duration};
pub struct Content {
window_action: Rc<WindowAction>,
tab_action: Rc<TabAction>,
pub gobject: Box,
}
impl Content {
// Construct
/// Create new container for different components
pub fn new(action: (Rc<WindowAction>, Rc<TabAction>)) -> Self {
Self {
gobject: Box::builder().orientation(Orientation::Vertical).build(),
window_action: action.0,
tab_action: action.1,
}
}
// Actions
/// Set new `content::Image` component for `Self`
///
/// * action removes previous children component from `Self`
pub fn to_image(&self, paintable: &impl IsA<Paintable>) -> Image {
self.clean();
let image = Image::new_from_paintable(paintable);
self.gobject.append(image.gobject());
image
}
/// Set new `content::Status` component for `Self` with new `status::Download` preset
///
/// * action removes previous children component from `Self`
pub fn to_status_download(
&self,
initial_filename: &str,
cancellable: &Cancellable,
on_choose: impl Fn(File, Rc<status::download::Action>) + 'static,
) -> Status {
self.clean();
let status = Status::new_download(initial_filename, cancellable, on_choose);
self.gobject.append(status.gobject());
status
}
/// Set new `content::Status` component for `Self` with new `status::Failure` preset
///
/// * action removes previous children component from `Self`
pub fn to_status_failure(&self) -> Status {
self.clean();
let status = Status::new_failure();
self.gobject.append(status.gobject());
status
}
/// Set new `content::Status` component for `Self` with new `status::Identity` preset
///
/// * action removes previous children component from `Self`
pub fn to_status_identity(&self) -> Status {
self.clean();
let status = Status::new_identity(self.tab_action.clone());
self.gobject.append(status.gobject());
status
}
/// Set new `content::Status` component for `Self` with new `status::Loading` preset
///
/// * action removes previous children component from `Self`
pub fn to_status_loading(&self, show_with_delay: Option<Duration>) -> Status {
self.clean();
let status = Status::new_loading(show_with_delay);
self.gobject.append(status.gobject());
status
}
/// Set new `content::Text` component for `Self` with new `text::Gemini` preset
///
/// Useful as reader for [Gemtext](https://geminiprotocol.net/docs/gemtext.gmi)
///
/// * action removes previous children component from `Self`
///
/// **Arguments**
///
/// * `base` - [Uri](https://docs.gtk.org/glib/struct.Uri.html) to resolve relative links in Gemtext
/// * `data` - Gemtext source to be parsed
///
/// **Return**
///
/// `Text` component with it public API
/// * could be useful to extract document title parsed from Gemtext
pub fn to_text_gemini(&self, base: &Uri, data: &str) -> Text {
self.clean();
let text = Text::new_gemini(
data,
base,
(self.window_action.clone(), self.tab_action.clone()),
);
self.gobject.append(&text.scrolled_window);
text
}
pub fn to_text_source(&self, data: &str) -> Text {
self.clean();
let text = Text::new_source(data);
self.gobject.append(&text.scrolled_window);
text
}
/// Remove all children components from `Self`
pub fn clean(&self) {
while let Some(child) = self.gobject.last_child() {
self.gobject.remove(&child);
}
}
}