From 1d13384f6c21e0cac59784217a8b67a6bfc7773a Mon Sep 17 00:00:00 2001 From: FeuRenard Date: Sat, 17 Aug 2019 16:47:38 +0200 Subject: [PATCH] headerbar: Refactor 'add' styling after url parsing The current code includes many duplications. I extract a single parameterized style function accompanied by two delegate functions. https://gitlab.gnome.org/World/podcasts/issues/45 --- podcasts-gtk/src/headerbar.rs | 77 +++++++++++++++++++---------------- 1 file changed, 41 insertions(+), 36 deletions(-) diff --git a/podcasts-gtk/src/headerbar.rs b/podcasts-gtk/src/headerbar.rs index 71967136..cc4dbe3a 100644 --- a/podcasts-gtk/src/headerbar.rs +++ b/podcasts-gtk/src/headerbar.rs @@ -104,58 +104,63 @@ impl AddPopover { }; debug!("Url: {}", url); - // TODO: refactor to avoid duplication match Url::parse(&url) { Ok(u) => { if !dbqueries::source_exists(u.as_str())? { - self.entry - .get_style_context() - .remove_class(>k::STYLE_CLASS_ERROR); - self.entry - .set_icon_from_icon_name(gtk::EntryIconPosition::Secondary, None); - self.add.set_sensitive(true); + self.style_neutral(true); } else { - self.entry - .get_style_context() - .add_class(>k::STYLE_CLASS_ERROR); - self.entry.set_icon_from_icon_name( - gtk::EntryIconPosition::Secondary, - Some("dialog-error-symbolic"), - ); - self.entry.set_icon_tooltip_text( - gtk::EntryIconPosition::Secondary, - Some(i18n("You are already subscribed to this show").as_str()), - ); - self.add.set_sensitive(false); + self.style_error("You are already subscribed to this show"); } Ok(()) } Err(err) => { - self.add.set_sensitive(false); if !is_input_url_empty { - self.entry - .get_style_context() - .add_class(>k::STYLE_CLASS_ERROR); - self.entry.set_icon_from_icon_name( - gtk::EntryIconPosition::Secondary, - Some("dialog-error-symbolic"), - ); - self.entry.set_icon_tooltip_text( - gtk::EntryIconPosition::Secondary, - Some(i18n("Invalid URL").as_str()), - ); + self.style_error("Invalid URL"); error!("Error: {}", err); } else { - self.entry - .get_style_context() - .remove_class(>k::STYLE_CLASS_ERROR); - self.entry - .set_icon_from_icon_name(gtk::EntryIconPosition::Secondary, None); + self.style_neutral(false); } Ok(()) } } } + + fn style_error(&self, icon_tooltip: &str) { + self.style( + true, + false, + Some("dialog-error-symbolic"), + Some(icon_tooltip), + ); + } + + fn style_neutral(&self, sensitive: bool) { + self.style(false, sensitive, None, None); + } + + fn style( + &self, + error: bool, + sensitive: bool, + icon_name: Option<&str>, + icon_tooltip: Option<&str>, + ) { + let entry = &self.entry; + let icon_position = gtk::EntryIconPosition::Secondary; + entry.set_icon_from_icon_name(icon_position, icon_name); + if let Some(icon_tooltip_text) = icon_tooltip { + entry.set_icon_tooltip_text(icon_position, Some(i18n(icon_tooltip_text).as_str())); + } + self.add.set_sensitive(sensitive); + + let error_style_class = >k::STYLE_CLASS_ERROR; + let style_context = entry.get_style_context(); + if error { + style_context.add_class(error_style_class); + } else { + style_context.remove_class(error_style_class); + } + } } impl Default for Header { -- GitLab