Skip to content

book: Second libadwaita section #1112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions book/listings/todo/7/resources/window.ui
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@
<binding name="show-end-title-buttons">
<lookup name="folded">leaflet</lookup>
</binding>
<child type="start">
<object class="GtkToggleButton">
<property name="icon-name">list-add-symbolic</property>
<property name="tooltip-text" translatable="yes">New Collection</property>
<property name="action-name">win.new-collection</property>
</object>
</child>
</object>
</child>
<child>
Expand Down Expand Up @@ -74,9 +81,6 @@
<property name="hexpand">True</property>
<child>
<object class="AdwHeaderBar">
<binding name="show-start-title-buttons">
<lookup name="folded">leaflet</lookup>
</binding>
<property name="title-widget">
<object class="AdwWindowTitle" />
</property>
Expand All @@ -100,8 +104,6 @@
</child>
<child>
<object class="GtkScrolledWindow">
<property name="hscrollbar-policy">never</property>
<property name="min-content-height">420</property>
<property name="vexpand">True</property>
<property name="child">
<object class="AdwClamp">
Expand All @@ -121,7 +123,6 @@
</child>
<child>
<object class="GtkListBox" id="tasks_list">
<property name="valign">start</property>
<property name="visible">False</property>
<property name="selection-mode">none</property>
<style>
Expand Down
7 changes: 5 additions & 2 deletions book/listings/todo/7/window/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use once_cell::sync::OnceCell;
use crate::task_object::{TaskData, TaskObject};
use crate::utils::data_path;

// ANCHOR: window
// Object holding the state
#[derive(CompositeTemplate, Default)]
#[template(resource = "/org/gtk_rs/Todo7/window.ui")]
Expand All @@ -27,12 +26,14 @@ pub struct Window {
pub settings: OnceCell<Settings>,
}

// ANCHOR: object_subclass
// The central trait for subclassing a GObject
#[glib::object_subclass]
impl ObjectSubclass for Window {
// `NAME` needs to match `class` attribute of template
const NAME: &'static str = "TodoWindow";
type Type = super::Window;
// 👇 changed
type ParentType = adw::ApplicationWindow;

fn class_init(klass: &mut Self::Class) {
Expand All @@ -43,7 +44,7 @@ impl ObjectSubclass for Window {
obj.init_template();
}
}
// ANCHOR_END: window
// ANCHOR_END: object_subclass

// Trait shared by all GObjects
impl ObjectImpl for Window {
Expand Down Expand Up @@ -88,5 +89,7 @@ impl WindowImpl for Window {
// Trait shared by all application windows
impl ApplicationWindowImpl for Window {}

// ANCHOR: adw_application_window_impl
// Trait shared by all adwaita application windows
impl AdwApplicationWindowImpl for Window {}
// ANCHOR_END: adw_application_window_impl
3 changes: 3 additions & 0 deletions book/listings/todo/7/window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ use crate::task_object::{TaskData, TaskObject};
use crate::utils::data_path;
use crate::APP_ID;

// ANCHOR: glib_wrapper
glib::wrapper! {
pub struct Window(ObjectSubclass<imp::Window>)
// 👇 changed
@extends adw::ApplicationWindow, gtk::ApplicationWindow, gtk::Window, gtk::Widget,
@implements gio::ActionGroup, gio::ActionMap, gtk::Accessible, gtk::Buildable,
gtk::ConstraintTarget, gtk::Native, gtk::Root, gtk::ShortcutManager;
}
// ANCHOR_END: glib_wrapper

impl Window {
pub fn new(app: &adw::Application) -> Self {
Expand Down
2 changes: 2 additions & 0 deletions book/listings/todo/8/collection_object/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use gtk::glib::ParamSpecObject;
use gtk::{gio, glib};
use once_cell::sync::{Lazy, OnceCell};

// ANCHOR: collection_object
// Object holding the state
#[derive(Default)]
pub struct CollectionObject {
Expand All @@ -20,6 +21,7 @@ impl ObjectSubclass for CollectionObject {
const NAME: &'static str = "TodoCollectionObject";
type Type = super::CollectionObject;
}
// ANCHOR_END: collection_object

// Trait shared by all GObjects
impl ObjectImpl for CollectionObject {
Expand Down
4 changes: 4 additions & 0 deletions book/listings/todo/8/collection_object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ glib::wrapper! {
pub struct CollectionObject(ObjectSubclass<imp::CollectionObject>);
}

// ANCHOR: impl
impl CollectionObject {
pub fn new(title: String, tasks: gio::ListStore) -> Self {
Object::new(&[("title", &title), ("tasks", &tasks)])
Expand Down Expand Up @@ -53,9 +54,12 @@ impl CollectionObject {
Self::new(title, tasks)
}
}
// ANCHOR_END: impl

// ANCHOR: collection_data
#[derive(Default, Clone, Serialize, Deserialize)]
pub struct CollectionData {
pub title: String,
pub tasks_data: Vec<TaskData>,
}
// ANCHOR_END: collection_data
5 changes: 1 addition & 4 deletions book/listings/todo/8/resources/window.ui
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<property name="transition-type">crossfade</property>
<child>
<object class="GtkStackPage">
<property name="name">empty</property>
<property name="name">placeholder</property>
<property name="child">
<object class="GtkBox">
<property name="orientation">vertical</property>
Expand Down Expand Up @@ -128,9 +128,6 @@
<property name="hexpand">True</property>
<child>
<object class="AdwHeaderBar">
<binding name="show-start-title-buttons">
<lookup name="folded">leaflet</lookup>
</binding>
<property name="title-widget">
<object class="AdwWindowTitle" />
</property>
Expand Down
14 changes: 12 additions & 2 deletions book/listings/todo/8/window/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,25 @@ use gio::Settings;
use glib::signal::Inhibit;
use glib::subclass::InitializingObject;
use gtk::glib::SignalHandlerId;
use gtk::{gio, glib, Button, CompositeTemplate, Entry, ListBox, Stack};
use gtk::{
gio, glib, Button, CompositeTemplate, Entry, FilterListModel, ListBox, Stack,
};
use once_cell::sync::OnceCell;

use crate::collection_object::{CollectionData, CollectionObject};
use crate::utils::data_path;

// ANCHOR: struct
// Object holding the state
#[derive(CompositeTemplate, Default)]
#[template(resource = "/org/gtk_rs/Todo8/window.ui")]
pub struct Window {
pub settings: OnceCell<Settings>,
#[template_child]
pub entry: TemplateChild<Entry>,
#[template_child]
pub tasks_list: TemplateChild<ListBox>,
// 👇 all members below are new
#[template_child]
pub collections_list: TemplateChild<ListBox>,
#[template_child]
Expand All @@ -32,9 +37,10 @@ pub struct Window {
pub back_button: TemplateChild<Button>,
pub collections: OnceCell<gio::ListStore>,
pub current_collection: RefCell<Option<CollectionObject>>,
pub current_filter_model: RefCell<Option<FilterListModel>>,
pub tasks_changed_handler_id: RefCell<Option<SignalHandlerId>>,
pub settings: OnceCell<Settings>,
}
// ANCHOR_END: struct

// The central trait for subclassing a GObject
#[glib::object_subclass]
Expand All @@ -53,6 +59,7 @@ impl ObjectSubclass for Window {
}
}

// ANCHOR: object_impl
// Trait shared by all GObjects
impl ObjectImpl for Window {
fn constructed(&self, obj: &Self::Type) {
Expand All @@ -67,10 +74,12 @@ impl ObjectImpl for Window {
obj.setup_actions();
}
}
// ANCHOR_END: object_impl

// Trait shared by all widgets
impl WidgetImpl for Window {}

// ANCHOR: window_impl
// Trait shared by all windows
impl WindowImpl for Window {
fn close_request(&self, window: &Self::Type) -> Inhibit {
Expand All @@ -92,6 +101,7 @@ impl WindowImpl for Window {
self.parent_close_request(window)
}
}
// ANCHOR_END: window_impl

// Trait shared by all application windows
impl ApplicationWindowImpl for Window {}
Expand Down
Loading