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:
parent
bc21f83f03
commit
17d36370b7
@ -34,7 +34,7 @@ impl PopulatedStack {
|
|||||||
pub(crate) fn new(sender: Sender<Action>) -> PopulatedStack {
|
pub(crate) fn new(sender: Sender<Action>) -> PopulatedStack {
|
||||||
let stack = gtk::Stack::new();
|
let stack = gtk::Stack::new();
|
||||||
let state = PopulatedState::View;
|
let state = PopulatedState::View;
|
||||||
let populated = ShowsView::new(sender.clone());
|
let populated = ShowsView::new(sender.clone(), None);
|
||||||
let show = Rc::new(ShowWidget::default());
|
let show = Rc::new(ShowWidget::default());
|
||||||
let container = gtk::Box::new(gtk::Orientation::Horizontal, 0);
|
let container = gtk::Box::new(gtk::Orientation::Horizontal, 0);
|
||||||
|
|
||||||
@ -73,12 +73,8 @@ impl PopulatedStack {
|
|||||||
let old = &self.populated.view.container().clone();
|
let old = &self.populated.view.container().clone();
|
||||||
debug!("Name: {:?}", WidgetExt::get_name(old));
|
debug!("Name: {:?}", WidgetExt::get_name(old));
|
||||||
|
|
||||||
self.populated
|
let vadj = self.populated.view.get_vadjustment();
|
||||||
.save_alignment()
|
let pop = ShowsView::new(self.sender.clone(), vadj);
|
||||||
.map_err(|err| error!("Failed to set episodes_view allignment: {}", err))
|
|
||||||
.ok();
|
|
||||||
|
|
||||||
let pop = ShowsView::new(self.sender.clone());
|
|
||||||
self.populated = pop;
|
self.populated = pop;
|
||||||
self.stack.remove(old);
|
self.stack.remove(old);
|
||||||
self.stack
|
self.stack
|
||||||
|
|||||||
@ -1,24 +1,18 @@
|
|||||||
use gtk::{self, prelude::*, Align, SelectionMode};
|
use gtk::{self, prelude::*, Adjustment, Align, SelectionMode};
|
||||||
|
|
||||||
use crossbeam_channel::Sender;
|
use crossbeam_channel::Sender;
|
||||||
use failure::Error;
|
use failure::Error;
|
||||||
use fragile::Fragile;
|
|
||||||
|
|
||||||
use podcasts_data::dbqueries;
|
use podcasts_data::dbqueries;
|
||||||
use podcasts_data::Show;
|
use podcasts_data::Show;
|
||||||
|
|
||||||
use app::Action;
|
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 widgets::BaseView;
|
||||||
|
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::sync::Arc;
|
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)]
|
#[derive(Debug, Clone)]
|
||||||
pub(crate) struct ShowsView {
|
pub(crate) struct ShowsView {
|
||||||
@ -50,11 +44,11 @@ impl Default for ShowsView {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl 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());
|
let pop = Rc::new(ShowsView::default());
|
||||||
pop.init(sender);
|
pop.init(sender);
|
||||||
// Populate the flowbox with the Shows.
|
// Populate the flowbox with the Shows.
|
||||||
let res = populate_flowbox(&pop);
|
let res = populate_flowbox(&pop, vadj);
|
||||||
debug_assert!(res.is_ok());
|
debug_assert!(res.is_ok());
|
||||||
pop
|
pop
|
||||||
}
|
}
|
||||||
@ -65,53 +59,18 @@ impl ShowsView {
|
|||||||
debug_assert!(res.is_ok());
|
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 ignore = get_ignored_shows()?;
|
||||||
let podcasts = dbqueries::get_podcasts_filter(&ignore)?;
|
let podcasts = dbqueries::get_podcasts_filter(&ignore)?;
|
||||||
|
|
||||||
let constructor = move |parent| ShowsChild::new(&parent).child;
|
let constructor = move |parent| ShowsChild::new(&parent).child;
|
||||||
// FIXME: We are, possibly,leaking the strong ref here
|
// FIXME: We are, possibly,leaking the strong ref here
|
||||||
let callback = clone!(shows => move || {
|
let callback = clone!(shows => move || {
|
||||||
shows.set_vadjustment()
|
if let Some(ref v) = vadj {
|
||||||
.map_err(|err| error!("Failed to set ShowsView Alignment: {}", err))
|
shows.view.set_adjutments(None, Some(v))
|
||||||
.ok();
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
let flowbox = shows.flowbox.clone();
|
let flowbox = shows.flowbox.clone();
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user