Skip to content

Commit 2682317

Browse files
committed
Refactor pages in own folder, handle open during in flight req, color links
1 parent 57f7704 commit 2682317

File tree

9 files changed

+92
-142
lines changed

9 files changed

+92
-142
lines changed

data/resources/ui/download_page.blp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Gtk 4.0;
22
using Adw 1;
33

4-
template DownloadPage: Gtk.Box {
4+
template Download: Gtk.Box {
55
margin-top: 8;
66
margin-bottom: 8;
77
margin-start: 8;

data/resources/ui/input_page.blp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Gtk 4.0;
22
using Adw 1;
33

4-
template InputPage: Gtk.Box {
4+
template Input: Gtk.Box {
55
margin-top: 8;
66
margin-bottom: 8;
77
margin-start: 8;

src/widgets/mod.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
mod download_page;
2-
mod input_page;
3-
mod page;
1+
mod pages;
42
#[allow(clippy::await_holding_refcell_ref)]
53
mod tab;
64
mod window;
75

8-
pub use download_page::DownloadPage;
9-
pub use input_page::InputPage;
106
pub use tab::Tab;
117
pub use window::Window;
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ mod imp {
88
pub use super::*;
99
#[derive(CompositeTemplate, Default)]
1010
#[template(resource = "/com/ranfdev/Geopard/ui/download_page.ui")]
11-
pub struct DownloadPage {
11+
pub struct Download {
1212
#[template_child]
1313
pub label: TemplateChild<gtk::Label>,
1414
#[template_child]
@@ -20,10 +20,10 @@ mod imp {
2020
}
2121

2222
#[glib::object_subclass]
23-
impl ObjectSubclass for DownloadPage {
23+
impl ObjectSubclass for Download {
2424
// `NAME` needs to match `class` attribute of template
25-
const NAME: &'static str = "DownloadPage";
26-
type Type = super::DownloadPage;
25+
const NAME: &'static str = "Download";
26+
type Type = super::Download;
2727
type ParentType = gtk::Box;
2828

2929
fn class_init(klass: &mut Self::Class) {
@@ -35,22 +35,22 @@ mod imp {
3535
}
3636
}
3737

38-
impl ObjectImpl for DownloadPage {}
39-
impl WidgetImpl for DownloadPage {}
40-
impl BoxImpl for DownloadPage {}
38+
impl ObjectImpl for Download {}
39+
impl WidgetImpl for Download {}
40+
impl BoxImpl for Download {}
4141
}
4242

4343
glib::wrapper! {
44-
pub struct DownloadPage(ObjectSubclass<imp::DownloadPage>)
44+
pub struct Download(ObjectSubclass<imp::Download>)
4545
@extends gtk::Box, gtk::Widget;
4646
}
4747

48-
impl DownloadPage {
48+
impl Download {
4949
pub fn new() -> Self {
5050
glib::Object::new(&[]).unwrap()
5151
}
5252
}
53-
impl Default for DownloadPage {
53+
impl Default for Download {
5454
fn default() -> Self {
5555
Self::new()
5656
}
Lines changed: 20 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use glib::subclass::{Signal, SignalType};
66
use glib::Properties;
77
use gtk::glib;
88
use gtk::prelude::*;
9-
use gtk::prelude::*;
109
use gtk::subclass::prelude::*;
1110
use gtk::{gdk, gio};
1211
use once_cell::sync::Lazy;
@@ -112,7 +111,8 @@ impl Surface {
112111
.unwrap(),
113112
);
114113
tag_a.set_line_height(1.4);
115-
tag_a.set_foreground(Some("blue"));
114+
let accent_color = &self.text_view.style_context().lookup_color("accent_color");
115+
tag_a.set_foreground_rgba(accent_color.as_ref());
116116

117117
let tag_pre = Self::create_tag(
118118
"pre",
@@ -140,23 +140,15 @@ impl Surface {
140140
.name(name)
141141
.build()
142142
}
143-
pub fn set_link_color(&self, color: &gtk::gdk::RGBA) {
144-
self.text_view
145-
.buffer()
146-
.tag_table()
147-
.lookup("a")
148-
.unwrap()
149-
.set_foreground_rgba(Some(color));
150-
}
143+
151144
pub fn clear(&mut self) {
152145
let b = &self.text_view.buffer();
153146
b.delete(&mut b.start_iter(), &mut b.end_iter());
154147
}
155148
}
156149

157-
pub enum PageEvent {
150+
pub enum HypertextEvent {
158151
Title(String),
159-
Link(gtk::TextTag, String),
160152
}
161153

162154
pub enum Title {
@@ -168,8 +160,8 @@ pub mod imp {
168160
use super::*;
169161

170162
#[derive(Default, Properties)]
171-
#[properties(wrapper_type = super::Page)]
172-
pub struct Page {
163+
#[properties(wrapper_type = super::Hypertext)]
164+
pub struct Hypertext {
173165
pub(super) tag_stack: RefCell<Vec<gemini::Tag>>,
174166
pub(super) links: RefCell<HashMap<gtk::TextTag, String>>,
175167
pub(super) surface: RefCell<Option<Surface>>,
@@ -181,12 +173,12 @@ pub mod imp {
181173
}
182174

183175
#[glib::object_subclass]
184-
impl ObjectSubclass for Page {
185-
const NAME: &'static str = "GeopardPage";
186-
type Type = super::Page;
176+
impl ObjectSubclass for Hypertext {
177+
const NAME: &'static str = "GeopardHypertext";
178+
type Type = super::Hypertext;
187179
}
188180

189-
impl ObjectImpl for Page {
181+
impl ObjectImpl for Hypertext {
190182
fn signals() -> &'static [glib::subclass::Signal] {
191183
static SIGNALS: Lazy<Vec<glib::subclass::Signal>> = Lazy::new(|| {
192184
vec![
@@ -214,7 +206,7 @@ pub mod imp {
214206
}
215207
}
216208

217-
impl Page {
209+
impl Hypertext {
218210
pub fn title(&self) -> String {
219211
match &*self.title.borrow() {
220212
None => String::new(),
@@ -225,14 +217,14 @@ pub mod imp {
225217
}
226218
}
227219
glib::wrapper! {
228-
pub struct Page(ObjectSubclass<imp::Page>);
220+
pub struct Hypertext(ObjectSubclass<imp::Hypertext>);
229221
}
230-
impl Default for Page {
222+
impl Default for Hypertext {
231223
fn default() -> Self {
232224
glib::Object::new(&[]).unwrap()
233225
}
234226
}
235-
impl Page {
227+
impl Hypertext {
236228
pub fn new(url: String, surface: Surface) -> Self {
237229
let text_view = surface.text_view.clone();
238230

@@ -249,22 +241,22 @@ impl Page {
249241
text_view.add_controller(&motion_ctrl);
250242

251243
left_click_ctrl.connect_released(
252-
clone!(@weak this => @default-panic, move |ctrl, _n_press, x, y| {
244+
clone!(@strong this => @default-panic, move |ctrl, _n_press, x, y| {
253245
if let Err(e) = this.handle_click(ctrl, x, y) {
254246
log::info!("{}", e);
255247
};
256248
}),
257249
);
258250

259251
right_click_ctrl.connect_pressed(
260-
clone!(@weak this => @default-panic, move |_ctrl, _n_press, x, y| {
252+
clone!(@strong this => @default-panic, move |_ctrl, _n_press, x, y| {
261253
if let Err(e) = this.handle_right_click(x, y) {
262254
log::info!("{}", e);
263255
};
264256
}),
265257
);
266258

267-
motion_ctrl.connect_motion(clone!(@weak this => @default-panic,move |_ctrl, x, y| {
259+
motion_ctrl.connect_motion(clone!(@strong this => @default-panic,move |_ctrl, x, y| {
268260
let _ = this.handle_motion(x, y);
269261
}));
270262

@@ -273,9 +265,9 @@ impl Page {
273265
pub fn render<'e>(
274266
&self,
275267
tokens: impl Iterator<Item = gemini::Event<'e>>,
276-
page_events: &'e mut Vec<PageEvent>,
268+
out_events: &'e mut Vec<HypertextEvent>,
277269
) -> anyhow::Result<()> {
278-
page_events.clear();
270+
out_events.clear();
279271
let mut tag_stack = self.imp().tag_stack.borrow_mut();
280272
for ev in tokens {
281273
let parent_tag = tag_stack.last();
@@ -323,7 +315,6 @@ impl Page {
323315
.links
324316
.borrow_mut()
325317
.insert(tag.clone(), url.clone());
326-
page_events.push(PageEvent::Link(tag, url.clone()));
327318
}
328319
gemini::Tag::Heading(1) => {
329320
let mut title = self.imp().title.borrow_mut();
@@ -358,7 +349,7 @@ impl Page {
358349
buffer.insert(&mut buffer.end_iter(), "\n");
359350
if matches!(parent_tag, gemini::Tag::Heading(1)) {
360351
if let Some(Title::Incomplete(title)) = self.imp().title.take() {
361-
page_events.push(PageEvent::Title(title.clone()));
352+
out_events.push(HypertextEvent::Title(title.clone()));
362353
self.imp().title.replace(Some(Title::Complete(title)));
363354
}
364355
}
@@ -415,36 +406,11 @@ impl Page {
415406
}
416407
Ok(())
417408
}
418-
pub fn display_error(&self, error: anyhow::Error) {
419-
log::error!("{:?}", error);
420-
421-
let status_page = adw::StatusPage::new();
422-
status_page.set_title("Error");
423-
status_page.set_description(Some(&error.to_string()));
424-
status_page.set_icon_name(Some("dialog-error-symbolic"));
425-
426-
// TODO:
427-
/* self.stack.add_child(&status_page);
428-
self.stack.set_visible_child(&status_page); */
429-
}
430409
fn parse_link(&self, link: &str) -> Result<Url, url::ParseError> {
431410
let current_url = Url::parse(self.imp().url.borrow().as_str())?;
432411
let link_url = Url::options().base_url(Some(&current_url)).parse(link)?;
433412
Ok(link_url)
434413
}
435-
pub fn set_link_color(&self, color: &gtk::gdk::RGBA) {
436-
self.imp()
437-
.surface
438-
.borrow()
439-
.as_ref()
440-
.unwrap()
441-
.text_view
442-
.buffer()
443-
.tag_table()
444-
.lookup("a")
445-
.unwrap()
446-
.set_foreground_rgba(Some(color));
447-
}
448414
fn extract_linkhandler<'a>(
449415
m: &'a HashMap<gtk::TextTag, String>,
450416
text_view: &gtk::TextView,
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ mod imp {
88
pub use super::*;
99
#[derive(CompositeTemplate, Default)]
1010
#[template(resource = "/com/ranfdev/Geopard/ui/input_page.ui")]
11-
pub struct InputPage {
11+
pub struct Input {
1212
#[template_child]
1313
pub label: TemplateChild<gtk::Label>,
1414
#[template_child]
1515
pub entry: TemplateChild<gtk::Entry>,
1616
}
1717

1818
#[glib::object_subclass]
19-
impl ObjectSubclass for InputPage {
19+
impl ObjectSubclass for Input {
2020
// `NAME` needs to match `class` attribute of template
21-
const NAME: &'static str = "InputPage";
22-
type Type = super::InputPage;
21+
const NAME: &'static str = "Input";
22+
type Type = super::Input;
2323
type ParentType = gtk::Box;
2424

2525
fn class_init(klass: &mut Self::Class) {
@@ -31,22 +31,22 @@ mod imp {
3131
}
3232
}
3333

34-
impl ObjectImpl for InputPage {}
35-
impl WidgetImpl for InputPage {}
36-
impl BoxImpl for InputPage {}
34+
impl ObjectImpl for Input {}
35+
impl WidgetImpl for Input {}
36+
impl BoxImpl for Input {}
3737
}
3838

3939
glib::wrapper! {
40-
pub struct InputPage(ObjectSubclass<imp::InputPage>)
40+
pub struct Input(ObjectSubclass<imp::Input>)
4141
@extends gtk::Box, gtk::Widget;
4242
}
4343

44-
impl InputPage {
44+
impl Input {
4545
pub fn new() -> Self {
4646
glib::Object::new(&[]).unwrap()
4747
}
4848
}
49-
impl Default for InputPage {
49+
impl Default for Input {
5050
fn default() -> Self {
5151
Self::new()
5252
}

src/widgets/pages/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
mod download;
2+
pub mod hypertext;
3+
mod input;
4+
5+
pub use download::Download;
6+
pub use hypertext::Hypertext;
7+
pub use input::Input;

0 commit comments

Comments
 (0)