From 0efb81bdaa1d15f0996a9eaa9065aa8665a61559 Mon Sep 17 00:00:00 2001 From: Bilal Elmoussaoui Date: Thu, 13 Apr 2023 00:39:51 +0200 Subject: [PATCH] widgets/providers: Split dialog row into a separate file Makes the code slightly bit simpler to navigate --- src/widgets/providers/dialog.rs | 89 +---------------------------- src/widgets/providers/dialog_row.rs | 74 ++++++++++++++++++++++++ src/widgets/providers/mod.rs | 2 + 3 files changed, 79 insertions(+), 86 deletions(-) create mode 100644 src/widgets/providers/dialog_row.rs diff --git a/src/widgets/providers/dialog.rs b/src/widgets/providers/dialog.rs index 49b6c56..1e0f402 100644 --- a/src/widgets/providers/dialog.rs +++ b/src/widgets/providers/dialog.rs @@ -1,12 +1,8 @@ use adw::{prelude::*, subclass::prelude::*}; use gettextrs::gettext; -use gtk::{ - glib::{self, clone}, - pango, -}; -use row::ProviderActionRow; +use gtk::glib::{self, clone}; -use super::ProviderPage; +use super::{dialog_row::ProviderActionRow, ProviderPage}; use crate::models::{Provider, ProviderSorter, ProvidersModel}; enum View { @@ -18,9 +14,8 @@ enum View { mod imp { use std::cell::Cell; - use adw::subclass::window::AdwWindowImpl; use glib::subclass::Signal; - use once_cell::sync::OnceCell; + use once_cell::sync::{Lazy, OnceCell}; use super::*; use crate::config; @@ -90,7 +85,6 @@ mod imp { impl ObjectImpl for ProvidersDialog { fn signals() -> &'static [Signal] { - use once_cell::sync::Lazy; static SIGNALS: Lazy> = Lazy::new(|| { vec![Signal::builder("changed") .param_types([Provider::static_type()]) @@ -306,80 +300,3 @@ impl ProvidersDialog { .add_toast(adw::Toast::new(&gettext("Provider removed successfully"))); } } - -mod row { - use super::*; - mod imp { - use once_cell::sync::OnceCell; - - use super::*; - - #[derive(Default, glib::Properties)] - #[properties(wrapper_type = super::ProviderActionRow)] - pub struct ProviderActionRow { - #[property(get, set = Self::set_provider, construct_only)] - pub provider: OnceCell, - pub title_label: gtk::Label, - } - - #[glib::object_subclass] - impl ObjectSubclass for ProviderActionRow { - const NAME: &'static str = "ProviderActionRow"; - type Type = super::ProviderActionRow; - type ParentType = gtk::ListBoxRow; - } - - impl ObjectImpl for ProviderActionRow { - fn properties() -> &'static [glib::ParamSpec] { - Self::derived_properties() - } - - fn set_property(&self, id: usize, value: &glib::Value, pspec: &glib::ParamSpec) { - self.derived_set_property(id, value, pspec) - } - - fn property(&self, id: usize, pspec: &glib::ParamSpec) -> glib::Value { - self.derived_property(id, pspec) - } - - fn constructed(&self) { - self.parent_constructed(); - let hbox = gtk::Box::builder() - .orientation(gtk::Orientation::Horizontal) - .margin_bottom(12) - .margin_end(6) - .margin_top(12) - .margin_start(6) - .build(); - self.title_label.set_valign(gtk::Align::Center); - self.title_label.set_halign(gtk::Align::Start); - self.title_label.set_wrap(true); - self.title_label.set_ellipsize(pango::EllipsizeMode::End); - hbox.append(&self.title_label); - self.obj().set_child(Some(&hbox)); - } - } - impl WidgetImpl for ProviderActionRow {} - impl ListBoxRowImpl for ProviderActionRow {} - - impl ProviderActionRow { - pub fn set_provider(&self, provider: Provider) { - self.title_label.set_text(&provider.name()); - self.provider.set(provider).unwrap(); - } - } - } - - glib::wrapper! { - pub struct ProviderActionRow(ObjectSubclass) - @extends gtk::Widget, gtk::ListBoxRow; - } - - impl ProviderActionRow { - pub fn new(provider: &Provider) -> Self { - glib::Object::builder() - .property("provider", provider) - .build() - } - } -} diff --git a/src/widgets/providers/dialog_row.rs b/src/widgets/providers/dialog_row.rs new file mode 100644 index 0000000..eaa81ba --- /dev/null +++ b/src/widgets/providers/dialog_row.rs @@ -0,0 +1,74 @@ +use gtk::{glib, prelude::*, subclass::prelude::*}; + +use crate::models::Provider; + +mod imp { + use gtk::pango; + use once_cell::sync::OnceCell; + + use super::*; + + #[derive(Default, glib::Properties)] + #[properties(wrapper_type = super::ProviderActionRow)] + pub struct ProviderActionRow { + #[property(get, set, construct_only)] + pub provider: OnceCell, + pub title_label: gtk::Label, + } + + #[glib::object_subclass] + impl ObjectSubclass for ProviderActionRow { + const NAME: &'static str = "ProviderActionRow"; + type Type = super::ProviderActionRow; + type ParentType = gtk::ListBoxRow; + } + + impl ObjectImpl for ProviderActionRow { + fn properties() -> &'static [glib::ParamSpec] { + Self::derived_properties() + } + + fn set_property(&self, id: usize, value: &glib::Value, pspec: &glib::ParamSpec) { + self.derived_set_property(id, value, pspec) + } + + fn property(&self, id: usize, pspec: &glib::ParamSpec) -> glib::Value { + self.derived_property(id, pspec) + } + + fn constructed(&self) { + self.parent_constructed(); + self.title_label.set_margin_bottom(12); + self.title_label.set_margin_end(6); + self.title_label.set_margin_top(12); + self.title_label.set_margin_start(6); + self.title_label.set_valign(gtk::Align::Center); + self.title_label.set_halign(gtk::Align::Start); + self.title_label.set_wrap(true); + self.title_label.set_ellipsize(pango::EllipsizeMode::End); + + self.obj() + .provider() + .bind_property("name", &self.title_label, "label") + .sync_create() + .build(); + + self.obj().set_child(Some(&self.title_label)); + } + } + impl WidgetImpl for ProviderActionRow {} + impl ListBoxRowImpl for ProviderActionRow {} +} + +glib::wrapper! { +pub struct ProviderActionRow(ObjectSubclass) + @extends gtk::Widget, gtk::ListBoxRow; +} + +impl ProviderActionRow { + pub fn new(provider: &Provider) -> Self { + glib::Object::builder() + .property("provider", provider) + .build() + } +} diff --git a/src/widgets/providers/mod.rs b/src/widgets/providers/mod.rs index 54c32cf..22d61c3 100644 --- a/src/widgets/providers/mod.rs +++ b/src/widgets/providers/mod.rs @@ -1,9 +1,11 @@ mod dialog; +mod dialog_row; mod entry_row; mod image; mod list; mod page; mod row; + pub use self::{ dialog::ProvidersDialog, entry_row::ProviderEntryRow,