Skip to content

Commit 3ddb669

Browse files
committed
Add blueprint file for Tab, fix janky scrolling
1 parent 16cb0f1 commit 3ddb669

File tree

4 files changed

+52
-44
lines changed

4 files changed

+52
-44
lines changed

data/resources/meson.build

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ blueprints = custom_target('blueprints',
33
'ui/window.blp',
44
'ui/shortcuts.blp',
55
'ui/input_page.blp',
6-
'ui/download_page.blp'
6+
'ui/download_page.blp',
7+
'ui/tab.blp'
78
),
89
output: '.',
910
command: [find_program('blueprint-compiler'), 'batch-compile', '@OUTPUT@', '@CURRENT_SOURCE_DIR@', '@INPUT@'],

data/resources/resources.gresource.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
<file compressed="true" preprocess="xml-stripblanks">ui/window.ui</file>
77
<file compressed="true" preprocess="xml-stripblanks">ui/input_page.ui</file>
88
<file compressed="true" preprocess="xml-stripblanks">ui/download_page.ui</file>
9+
<file compressed="true" preprocess="xml-stripblanks">ui/tab.ui</file>
910
</gresource>
1011
</gresources>

data/resources/ui/tab.blp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Gtk 4.0;
2+
using Adw 1;
3+
4+
template Tab: Adw.Bin {
5+
Adw.Clamp clamp {
6+
halign: center;
7+
maximum-size: 768;
8+
tightening-threshold: 720;
9+
Gtk.Stack stack {
10+
Gtk.ScrolledWindow scroll_win {
11+
vexpand: true;
12+
Gtk.TextView text_view {
13+
top-margin: 40;
14+
bottom-margin: 80;
15+
left-margin: 20;
16+
right-margin: 20;
17+
indent: 2;
18+
editable: false;
19+
cursor-visible: false;
20+
wrap-mode: word_char;
21+
}
22+
}
23+
}
24+
}
25+
}

src/tab.rs

Lines changed: 24 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ use std::marker::PhantomData;
1717
use std::pin::Pin;
1818
use std::rc::Rc;
1919
use url::Url;
20+
use gtk::CompositeTemplate;
21+
use gtk::TemplateChild;
2022

2123
use crate::common;
2224
use crate::common::{glibctx, HistoryItem, LossyTextRead, PageElement, RequestCtx};
@@ -32,16 +34,22 @@ pub struct HistoryStatus {
3234
pub mod imp {
3335

3436
pub use super::*;
35-
#[derive(Debug, Default, Properties)]
37+
#[derive(Debug, Default, Properties, CompositeTemplate)]
38+
#[template(resource = "/com/ranfdev/Geopard/ui/tab.ui")]
3639
#[properties(wrapper_type = super::Tab)]
3740
pub struct Tab {
3841
pub(crate) gemini_client: RefCell<gemini::Client>,
3942
pub(crate) draw_ctx: RefCell<Option<DrawCtx>>,
4043
pub(crate) history: RefCell<Vec<HistoryItem>>,
4144
pub(crate) current_hi: RefCell<Option<usize>>,
42-
pub(crate) scroll_win: gtk::ScrolledWindow,
43-
pub(crate) stack: gtk::Stack,
44-
pub(crate) clamp: adw::Clamp,
45+
#[template_child]
46+
pub(crate) scroll_win: TemplateChild<gtk::ScrolledWindow>,
47+
#[template_child]
48+
pub(crate) text_view: TemplateChild<gtk::TextView>,
49+
#[template_child]
50+
pub(crate) stack: TemplateChild<gtk::Stack>,
51+
#[template_child]
52+
pub(crate) clamp: TemplateChild<adw::Clamp>,
4553
pub(crate) left_click_ctrl: RefCell<Option<gtk::GestureClick>>,
4654
pub(crate) right_click_ctrl: RefCell<Option<gtk::GestureClick>>,
4755
pub(crate) motion_ctrl: RefCell<Option<gtk::EventControllerMotion>>,
@@ -58,28 +66,22 @@ pub mod imp {
5866

5967
#[glib::object_subclass]
6068
impl ObjectSubclass for Tab {
61-
const NAME: &'static str = "GeopardTab";
69+
const NAME: &'static str = "Tab";
6270
type Type = super::Tab;
63-
type ParentType = gtk::Widget;
71+
type ParentType = adw::Bin;
6472

6573
fn class_init(klass: &mut Self::Class) {
66-
// The layout manager determines how child widgets are laid out.
67-
klass.set_layout_manager_type::<gtk::BinLayout>();
74+
Self::bind_template(klass);
75+
}
76+
fn instance_init(obj: &glib::subclass::InitializingObject<Self>) {
77+
obj.init_template();
6878
}
6979
}
7080

7181
impl ObjectImpl for Tab {
7282
fn constructed(&self, obj: &Self::Type) {
7383
self.parent_constructed(obj);
7484

75-
self.scroll_win.set_vexpand(true);
76-
obj.set_vexpand(true);
77-
78-
self.clamp.set_parent(obj);
79-
self.clamp.set_maximum_size(768);
80-
self.clamp.set_tightening_threshold(720);
81-
self.clamp.set_child(Some(&self.scroll_win));
82-
8385
self.left_click_ctrl
8486
.replace(Some(gtk::GestureClick::builder().button(1).build()));
8587
self.right_click_ctrl
@@ -90,10 +92,6 @@ pub mod imp {
9092
.replace(gemini::ClientBuilder::new().redirect(true).build());
9193
}
9294

93-
fn dispose(&self, _obj: &Self::Type) {
94-
self.clamp.unparent();
95-
}
96-
9795
fn signals() -> &'static [glib::subclass::Signal] {
9896
static SIGNALS: Lazy<Vec<glib::subclass::Signal>> = Lazy::new(|| {
9997
vec![glib::subclass::Signal::builder(
@@ -133,6 +131,7 @@ pub mod imp {
133131
}
134132
}
135133
impl WidgetImpl for Tab {}
134+
impl adw::subclass::bin::BinImpl for Tab {}
136135
}
137136
glib::wrapper! {
138137
pub struct Tab(ObjectSubclass<imp::Tab>)
@@ -143,33 +142,15 @@ impl Tab {
143142
pub fn new(config: crate::config::Config) -> Self {
144143
let this: Self = glib::Object::new(&[]).unwrap();
145144
let imp = this.imp();
146-
use common::MARGIN;
147-
let text_view = gtk::builders::TextViewBuilder::new()
148-
.top_margin(MARGIN * 2)
149-
.left_margin(MARGIN)
150-
.right_margin(MARGIN)
151-
.bottom_margin(MARGIN * 4)
152-
.indent(2)
153-
.editable(false)
154-
.cursor_visible(false)
155-
.wrap_mode(gtk::WrapMode::WordChar)
156-
.build();
157-
text_view.add_controller(imp.left_click_ctrl.borrow().as_ref().unwrap());
158-
text_view.add_controller(imp.right_click_ctrl.borrow().as_ref().unwrap());
159-
text_view.add_controller(imp.motion_ctrl.borrow().as_ref().unwrap());
160-
161-
imp.stack.add_child(&text_view);
162-
imp.scroll_win.set_child(Some(&imp.stack));
163-
164-
imp.draw_ctx.replace(Some(DrawCtx::new(text_view, config)));
145+
imp.text_view.add_controller(imp.left_click_ctrl.borrow().as_ref().unwrap());
146+
imp.text_view.add_controller(imp.right_click_ctrl.borrow().as_ref().unwrap());
147+
imp.text_view.add_controller(imp.motion_ctrl.borrow().as_ref().unwrap());
148+
149+
imp.draw_ctx.replace(Some(DrawCtx::new(imp.text_view.clone(), config)));
165150

166151
this.bind_signals();
167152
this
168153
}
169-
pub fn child(&self) -> &gtk::ScrolledWindow {
170-
let imp = self.imp();
171-
&imp.scroll_win
172-
}
173154
fn build_request_ctx(&self, url: Url) -> RequestCtx {
174155
let imp = self.imp();
175156
RequestCtx {

0 commit comments

Comments
 (0)