Skip to content

Commit

Permalink
Merge pull request #745 from marhkb/perf/signal_list_item_factory/use…
Browse files Browse the repository at this point in the history
…-setup

perf(signal-list-item-factory): Use setup signal
  • Loading branch information
marhkb authored Nov 27, 2023
2 parents 5410ee2 + a3306fb commit 5017869
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 105 deletions.
1 change: 1 addition & 0 deletions data/com.github.marhkb.Pods.metainfo.xml.in.in
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<ul>
<li>Build about dialog from appdata. (#743)</li>
<li>Close all remaining processes after container terminal exits. (#744)</li>
<li>Improve performance by utilizing the <em>SignalListItemFactory::setup</em> signal. (#745)</li>
</ul>
</description>
</release>
Expand Down
6 changes: 6 additions & 0 deletions src/view/image_search_response_row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ glib::wrapper! {
@implements gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget;
}

impl Default for ImageSearchResponseRow {
fn default() -> Self {
glib::Object::builder().build()
}
}

impl From<&model::ImageSearchResponse> for ImageSearchResponseRow {
fn from(response: &model::ImageSearchResponse) -> Self {
glib::Object::builder()
Expand Down
25 changes: 11 additions & 14 deletions src/view/image_search_widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ mod imp {
#[template_child]
pub(super) no_results_status_page: TemplateChild<adw::StatusPage>,
#[template_child]
pub(super) signal_list_item_factory: TemplateChild<gtk::SignalListItemFactory>,
#[template_child]
pub(super) selection: TemplateChild<gtk::SingleSelection>,
#[template_child]
pub(super) tag_entry_row: TemplateChild<adw::EntryRow>,
Expand Down Expand Up @@ -175,23 +173,22 @@ mod imp {
}

#[template_callback]
fn on_signal_list_item_factory_bind(&self, list_item: &glib::Object) {
let list_item = list_item.downcast_ref::<gtk::ListItem>().unwrap();

if let Some(item) = list_item.item() {
let response = item.downcast::<model::ImageSearchResponse>().unwrap();
let row = view::ImageSearchResponseRow::from(&response);

list_item.set_child(Some(&row));
}
fn on_signal_list_item_factory_setup(&self, list_item: &gtk::ListItem) {
list_item.set_child(Some(&view::ImageSearchResponseRow::default()));
}

#[template_callback]
fn on_signal_list_item_factory_unbind(&self, list_item: &glib::Object) {
fn on_signal_list_item_factory_bind(&self, list_item: &gtk::ListItem) {
let response = list_item
.item()
.and_downcast::<model::ImageSearchResponse>()
.unwrap();

list_item
.downcast_ref::<gtk::ListItem>()
.child()
.and_downcast::<view::ImageSearchResponseRow>()
.unwrap()
.set_child(gtk::Widget::NONE);
.set_image_search_response(Some(response));
}

#[template_callback]
Expand Down
4 changes: 2 additions & 2 deletions src/view/image_search_widget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ You cannot search for images without registries. Please follow the instructions
<object class="GtkListView">
<property name="show-separators">True</property>
<property name="factory">
<object class="GtkSignalListItemFactory" id="signal_list_item_factory">
<object class="GtkSignalListItemFactory">
<signal name="setup" handler="on_signal_list_item_factory_setup" swapped="true"/>
<signal name="bind" handler="on_signal_list_item_factory_bind" swapped="true"/>
<signal name="unbind" handler="on_signal_list_item_factory_unbind" swapped="true"/>
</object>
</property>
<property name="model">
Expand Down
68 changes: 32 additions & 36 deletions src/view/image_selection_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ mod imp {
#[template_child]
pub(super) select_button: TemplateChild<gtk::Button>,
#[template_child]
pub(super) signal_list_item_factory: TemplateChild<gtk::SignalListItemFactory>,
#[template_child]
pub(super) selection: TemplateChild<gtk::SingleSelection>,
}

Expand Down Expand Up @@ -169,46 +167,44 @@ mod imp {
}

#[template_callback]
fn on_signal_list_item_factory_bind(&self, list_item: &glib::Object) {
let list_item = list_item.downcast_ref::<gtk::ListItem>().unwrap();

if let Some(item) = list_item.item() {
let image = item.downcast::<model::Image>().unwrap();
let repo_tag = image.repo_tags().get(0);

let label = gtk::Label::builder()
.label(
repo_tag
.as_ref()
.map(|repo_tag| repo_tag.full())
.unwrap_or_else(|| image.id()),
)
.margin_top(9)
.margin_end(12)
.margin_bottom(9)
.margin_start(12)
.xalign(0.0)
.wrap(true)
.wrap_mode(pango::WrapMode::WordChar)
.build();

if repo_tag.is_none() {
fn on_signal_list_item_factory_setup(&self, list_item: &gtk::ListItem) {
let label = gtk::Label::builder()
.margin_top(9)
.margin_end(12)
.margin_bottom(9)
.margin_start(12)
.xalign(0.0)
.wrap(true)
.wrap_mode(pango::WrapMode::WordChar)
.build();

list_item.set_child(Some(&label));
}

#[template_callback]
fn on_signal_list_item_factory_bind(&self, list_item: &gtk::ListItem) {
let image = list_item.item().and_downcast::<model::Image>().unwrap();
let repo_tag = image.repo_tags().get(0);

let label = list_item.child().and_downcast::<gtk::Label>().unwrap();
label.set_label(
&repo_tag
.as_ref()
.map(|repo_tag| repo_tag.full())
.unwrap_or_else(|| image.id()),
);
match image.repo_tags().get(0) {
Some(_) => {
label.remove_css_class("dim-label");
label.remove_css_class("numeric");
}
None => {
label.add_css_class("dim-label");
label.add_css_class("numeric");
}

list_item.set_child(Some(&label));
}
}

#[template_callback]
fn on_signal_list_item_factory_unbind(&self, list_item: &glib::Object) {
list_item
.downcast_ref::<gtk::ListItem>()
.unwrap()
.set_child(gtk::Widget::NONE);
}

#[template_callback]
fn on_image_selected(&self) {
self.obj()
Expand Down
4 changes: 2 additions & 2 deletions src/view/image_selection_page.ui
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@
<object class="GtkListView">
<property name="show-separators">True</property>
<property name="factory">
<object class="GtkSignalListItemFactory" id="signal_list_item_factory">
<object class="GtkSignalListItemFactory">
<signal name="setup" handler="on_signal_list_item_factory_setup" swapped="true"/>
<signal name="bind" handler="on_signal_list_item_factory_bind" swapped="true"/>
<signal name="unbind" handler="on_signal_list_item_factory_unbind" swapped="true"/>
</object>
</property>
<property name="model">
Expand Down
42 changes: 18 additions & 24 deletions src/view/pod_selection_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ mod imp {
#[template_child]
pub(super) select_button: TemplateChild<gtk::Button>,
#[template_child]
pub(super) signal_list_item_factory: TemplateChild<gtk::SignalListItemFactory>,
#[template_child]
pub(super) selection: TemplateChild<gtk::SingleSelection>,
}

Expand Down Expand Up @@ -164,33 +162,29 @@ mod imp {
}

#[template_callback]
fn on_signal_list_item_factory_bind(&self, list_item: &glib::Object) {
let list_item = list_item.downcast_ref::<gtk::ListItem>().unwrap();

if let Some(item) = list_item.item() {
let pod = item.downcast::<model::Pod>().unwrap();

let label = gtk::Label::builder()
.label(pod.name())
.margin_top(9)
.margin_end(12)
.margin_bottom(9)
.margin_start(12)
.xalign(0.0)
.wrap(true)
.wrap_mode(pango::WrapMode::WordChar)
.build();

list_item.set_child(Some(&label));
}
fn on_signal_list_item_factory_setup(&self, list_item: &gtk::ListItem) {
let label = gtk::Label::builder()
.margin_top(9)
.margin_end(12)
.margin_bottom(9)
.margin_start(12)
.xalign(0.0)
.wrap(true)
.wrap_mode(pango::WrapMode::WordChar)
.build();

list_item.set_child(Some(&label));
}

#[template_callback]
fn on_signal_list_item_factory_unbind(&self, list_item: &glib::Object) {
fn on_signal_list_item_factory_bind(&self, list_item: &gtk::ListItem) {
let pod = list_item.item().and_downcast::<model::Pod>().unwrap();

list_item
.downcast_ref::<gtk::ListItem>()
.child()
.and_downcast::<gtk::Label>()
.unwrap()
.set_child(gtk::Widget::NONE);
.set_label(&pod.name());
}

#[template_callback]
Expand Down
4 changes: 2 additions & 2 deletions src/view/pod_selection_page.ui
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@
<object class="GtkListView">
<property name="show-separators">True</property>
<property name="factory">
<object class="GtkSignalListItemFactory" id="signal_list_item_factory">
<object class="GtkSignalListItemFactory">
<signal name="setup" handler="on_signal_list_item_factory_setup" swapped="true"/>
<signal name="bind" handler="on_signal_list_item_factory_bind" swapped="true"/>
<signal name="unbind" handler="on_signal_list_item_factory_unbind" swapped="true"/>
</object>
</property>
<property name="model">
Expand Down
42 changes: 18 additions & 24 deletions src/view/volume_selection_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ mod imp {
#[template_child]
pub(super) select_button: TemplateChild<gtk::Button>,
#[template_child]
pub(super) signal_list_item_factory: TemplateChild<gtk::SignalListItemFactory>,
#[template_child]
pub(super) selection: TemplateChild<gtk::SingleSelection>,
}

Expand Down Expand Up @@ -164,33 +162,29 @@ mod imp {
}

#[template_callback]
fn on_signal_list_item_factory_bind(&self, list_item: &glib::Object) {
let list_item = list_item.downcast_ref::<gtk::ListItem>().unwrap();

if let Some(item) = list_item.item() {
let volume = item.downcast::<model::Volume>().unwrap();

let label = gtk::Label::builder()
.label(utils::format_volume_name(&volume.inner().name))
.margin_top(9)
.margin_end(12)
.margin_bottom(9)
.margin_start(12)
.xalign(0.0)
.wrap(true)
.wrap_mode(pango::WrapMode::WordChar)
.build();

list_item.set_child(Some(&label));
}
fn on_signal_list_item_factory_setup(&self, list_item: &gtk::ListItem) {
let label = gtk::Label::builder()
.margin_top(9)
.margin_end(12)
.margin_bottom(9)
.margin_start(12)
.xalign(0.0)
.wrap(true)
.wrap_mode(pango::WrapMode::WordChar)
.build();

list_item.set_child(Some(&label));
}

#[template_callback]
fn on_signal_list_item_factory_unbind(&self, list_item: &glib::Object) {
fn on_signal_list_item_factory_bind(&self, list_item: &gtk::ListItem) {
let volume = list_item.item().and_downcast::<model::Volume>().unwrap();

list_item
.downcast_ref::<gtk::ListItem>()
.child()
.and_downcast::<gtk::Label>()
.unwrap()
.set_child(gtk::Widget::NONE);
.set_label(&utils::format_volume_name(&volume.inner().name));
}

#[template_callback]
Expand Down
2 changes: 1 addition & 1 deletion src/view/volume_selection_page.ui
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@
<property name="show-separators">True</property>
<property name="factory">
<object class="GtkSignalListItemFactory" id="signal_list_item_factory">
<signal name="setup" handler="on_signal_list_item_factory_setup" swapped="true"/>
<signal name="bind" handler="on_signal_list_item_factory_bind" swapped="true"/>
<signal name="unbind" handler="on_signal_list_item_factory_unbind" swapped="true"/>
</object>
</property>
<property name="model">
Expand Down

0 comments on commit 5017869

Please sign in to comment.