Commit 1df508a5 authored by Bilal Elmoussaoui's avatar Bilal Elmoussaoui

Make use of composite template callbacks

parent f35f9618
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<interface> <interface>
<template class="PaginatorWidget" parent="GtkBox"> <template class="PaginatorWidget" parent="GtkBox">
<property name="orientation">vertical</property>
<child> <child>
<object class="GtkHeaderBar"> <object class="GtkHeaderBar">
<property name="show-title-buttons">True</property> <property name="show-title-buttons">True</property>
...@@ -61,9 +62,15 @@ ...@@ -61,9 +62,15 @@
<object class="AdwCarousel" id="carousel"> <object class="AdwCarousel" id="carousel">
<property name="hexpand">True</property> <property name="hexpand">True</property>
<property name="vexpand">True</property> <property name="vexpand">True</property>
<signal name="notify::position" handler="on_position_notify" swapped="true" />
</object> </object>
</child> </child>
</object> </object>
</child> </child>
<child>
<object class="GtkEventControllerKey">
<signal name="key-pressed" handler="on_key_pressed" swapped="true" />
</object>
</child>
</template> </template>
</interface> </interface>
use gtk::prelude::*; use gtk::{gdk, glib, prelude::*, subclass::prelude::*};
use gtk::{
gdk,
glib::{self, clone},
subclass::prelude::*,
};
mod imp { mod imp {
use super::*; use super::*;
...@@ -33,7 +28,8 @@ mod imp { ...@@ -33,7 +28,8 @@ mod imp {
type Interfaces = (gtk::Buildable,); type Interfaces = (gtk::Buildable,);
fn class_init(klass: &mut Self::Class) { fn class_init(klass: &mut Self::Class) {
Self::bind_template(klass); klass.bind_template();
klass.bind_template_instance_callbacks();
} }
fn instance_init(obj: &glib::subclass::InitializingObject<Self>) { fn instance_init(obj: &glib::subclass::InitializingObject<Self>) {
...@@ -44,30 +40,8 @@ mod imp { ...@@ -44,30 +40,8 @@ mod imp {
impl ObjectImpl for PaginatorWidget { impl ObjectImpl for PaginatorWidget {
fn constructed(&self) { fn constructed(&self) {
self.parent_constructed(); self.parent_constructed();
let obj = self.obj();
let layout_manager = obj
.layout_manager()
.and_downcast::<gtk::BoxLayout>()
.unwrap();
layout_manager.set_orientation(gtk::Orientation::Vertical);
self.carousel self.carousel
.set_scroll_params(&adw::SpringParams::new(1.0, 0.5, 300.0)); .set_scroll_params(&adw::SpringParams::new(1.0, 0.5, 300.0));
self.carousel
.connect_position_notify(clone!(@weak obj => move |_| {
obj.update_position();
}));
let controller = gtk::EventControllerKey::new();
controller.connect_key_pressed(clone!(@weak obj => @default-return gtk::Inhibit(true), move |_controller, keyval, _keycode, _state| {
if keyval == gdk::Key::Right {
obj.try_next();
} else if keyval == gdk::Key::Left {
obj.try_previous();
}
gtk::Inhibit(false)
}));
obj.add_controller(controller);
} }
} }
impl WidgetImpl for PaginatorWidget {} impl WidgetImpl for PaginatorWidget {}
...@@ -90,6 +64,7 @@ glib::wrapper! { ...@@ -90,6 +64,7 @@ glib::wrapper! {
@implements gtk::Buildable; @implements gtk::Buildable;
} }
#[gtk::template_callbacks]
impl PaginatorWidget { impl PaginatorWidget {
pub fn try_next(&self) -> Option<()> { pub fn try_next(&self) -> Option<()> {
let imp = self.imp(); let imp = self.imp();
...@@ -116,10 +91,21 @@ impl PaginatorWidget { ...@@ -116,10 +91,21 @@ impl PaginatorWidget {
imp.carousel.insert(&page, page_nr as i32); imp.carousel.insert(&page, page_nr as i32);
imp.pages.borrow_mut().push(page.upcast()); imp.pages.borrow_mut().push(page.upcast());
self.update_position(); self.on_position_notify();
}
#[template_callback]
fn on_key_pressed(&self, keyval: gdk::Key) -> gtk::Inhibit {
if keyval == gdk::Key::Right {
self.try_next();
} else if keyval == gdk::Key::Left {
self.try_previous();
}
gtk::Inhibit(false)
} }
fn update_position(&self) { #[template_callback]
fn on_position_notify(&self) {
let imp = self.imp(); let imp = self.imp();
let position = imp.carousel.position(); let position = imp.carousel.position();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment