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.
This commit is contained in:
Jordan Petridis 2018-08-09 09:38:32 +03:00
parent bc21f83f03
commit 17d36370b7
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
2 changed files with 11 additions and 56 deletions

View File

@ -34,7 +34,7 @@ impl PopulatedStack {
pub(crate) fn new(sender: Sender<Action>) -> 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 allignment: {}", 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

View File

@ -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<Option<Fragile<gtk::Adjustment>>> = 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<Action>) -> Rc<Self> {
pub(crate) fn new(sender: Sender<Action>, vadj: Option<Adjustment>) -> Rc<Self> {
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<ShowsView>) -> Result<(), Error> {
fn populate_flowbox(shows: &Rc<ShowsView>, vadj: Option<Adjustment>) -> 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();