From aa5195e5a9432d3922028acd2ee0dce9c5b18140 Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Thu, 9 Aug 2018 09:38:32 +0300 Subject: [PATCH] ShowsView: handle vadjustment with BaseView Instead of using lazy_static to save the adjustment, pass it to the widget upon creation. If its the first instance created, pass None instead. --- podcasts-gtk/src/stacks/populated.rs | 10 ++--- podcasts-gtk/src/widgets/shows_view.rs | 57 ++++---------------------- 2 files changed, 11 insertions(+), 56 deletions(-) diff --git a/podcasts-gtk/src/stacks/populated.rs b/podcasts-gtk/src/stacks/populated.rs index 957b494..f275699 100644 --- a/podcasts-gtk/src/stacks/populated.rs +++ b/podcasts-gtk/src/stacks/populated.rs @@ -34,7 +34,7 @@ impl PopulatedStack { pub(crate) fn new(sender: Sender) -> PopulatedStack { let stack = gtk::Stack::new(); let state = PopulatedState::View; - let populated = ShowsView::new(sender.clone()); + let populated = ShowsView::new(sender.clone(), None); let show = Rc::new(ShowWidget::default()); let container = gtk::Box::new(gtk::Orientation::Horizontal, 0); @@ -73,12 +73,8 @@ impl PopulatedStack { let old = &self.populated.view.container().clone(); debug!("Name: {:?}", WidgetExt::get_name(old)); - self.populated - .save_alignment() - .map_err(|err| error!("Failed to set episodes_view alignment: {}", err)) - .ok(); - - let pop = ShowsView::new(self.sender.clone()); + let vadj = self.populated.view.get_vadjustment(); + let pop = ShowsView::new(self.sender.clone(), vadj); self.populated = pop; self.stack.remove(old); self.stack diff --git a/podcasts-gtk/src/widgets/shows_view.rs b/podcasts-gtk/src/widgets/shows_view.rs index d4803e0..e4bace7 100644 --- a/podcasts-gtk/src/widgets/shows_view.rs +++ b/podcasts-gtk/src/widgets/shows_view.rs @@ -1,24 +1,18 @@ -use gtk::{self, prelude::*, Align, SelectionMode}; +use gtk::{self, prelude::*, Adjustment, Align, SelectionMode}; use crossbeam_channel::Sender; use failure::Error; -use fragile::Fragile; use podcasts_data::dbqueries; use podcasts_data::Show; use app::Action; -use utils::{self, get_ignored_shows, lazy_load, set_image_from_path}; +use utils::{get_ignored_shows, lazy_load, set_image_from_path}; use widgets::BaseView; use std::cell::Cell; use std::rc::Rc; use std::sync::Arc; -use std::sync::Mutex; - -lazy_static! { - static ref SHOWS_VIEW_VALIGNMENT: Mutex>> = Mutex::new(None); -} #[derive(Debug, Clone)] pub(crate) struct ShowsView { @@ -50,11 +44,11 @@ impl Default for ShowsView { } impl ShowsView { - pub(crate) fn new(sender: Sender) -> Rc { + pub(crate) fn new(sender: Sender, vadj: Option) -> Rc { let pop = Rc::new(ShowsView::default()); pop.init(sender); // Populate the flowbox with the Shows. - let res = populate_flowbox(&pop); + let res = populate_flowbox(&pop, vadj); debug_assert!(res.is_ok()); pop } @@ -65,53 +59,18 @@ impl ShowsView { debug_assert!(res.is_ok()); }); } - - /// Set scrolled window vertical adjustment. - fn set_vadjustment(&self) -> Result<(), Error> { - let guard = SHOWS_VIEW_VALIGNMENT - .lock() - .map_err(|err| format_err!("Failed to lock widget align mutex: {}", err))?; - - if let Some(ref fragile) = *guard { - // Copy the vertical scrollbar adjustment from the old view into the new one. - let res = fragile - .try_get() - .map(|x| utils::smooth_scroll_to(self.view.scrolled_window(), &x)) - .map_err(From::from); - - debug_assert!(res.is_ok()); - return res; - } - - Ok(()) - } - - /// Save the vertical scrollbar position. - pub(crate) fn save_alignment(&self) -> Result<(), Error> { - if let Ok(mut guard) = SHOWS_VIEW_VALIGNMENT.lock() { - let adj = self - .view - .scrolled_window() - .get_vadjustment() - .ok_or_else(|| format_err!("Could not get the adjustment"))?; - *guard = Some(Fragile::new(adj)); - info!("Saved episodes_view alignment."); - } - - Ok(()) - } } -fn populate_flowbox(shows: &Rc) -> Result<(), Error> { +fn populate_flowbox(shows: &Rc, vadj: Option) -> Result<(), Error> { let ignore = get_ignored_shows()?; let podcasts = dbqueries::get_podcasts_filter(&ignore)?; let constructor = move |parent| ShowsChild::new(&parent).child; // FIXME: We are, possibly,leaking the strong ref here let callback = clone!(shows => move || { - shows.set_vadjustment() - .map_err(|err| error!("Failed to set ShowsView Alignment: {}", err)) - .ok(); + if let Some(ref v) = vadj { + shows.view.set_adjutments(None, Some(v)) + }; }); let flowbox = shows.flowbox.clone();