ShowsView: Retain vertical scrolling adjustment upon view refresh.

This commit is contained in:
Jordan Petridis 2018-01-05 00:09:09 +02:00
parent 8ad5bf6f09
commit 299d2e8db1
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
3 changed files with 39 additions and 10 deletions

View File

@ -3,16 +3,18 @@
<interface> <interface>
<requires lib="gtk+" version="3.20"/> <requires lib="gtk+" version="3.20"/>
<object class="GtkBox" id="fb_parent"> <object class="GtkBox" id="fb_parent">
<property name="name">fb_parent</property>
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="orientation">vertical</property> <property name="orientation">vertical</property>
<child> <child>
<object class="GtkScrolledWindow"> <object class="GtkScrolledWindow" id="scrolled_window">
<property name="name">scrolled_window</property>
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
<property name="shadow_type">in</property> <property name="shadow_type">in</property>
<child> <child>
<object class="GtkViewport" id="viewport"> <object class="GtkViewport">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<child> <child>

View File

@ -111,9 +111,31 @@ impl ShowStack {
pub fn update_podcasts(&self) { pub fn update_podcasts(&self) {
let vis = self.stack.get_visible_child_name().unwrap(); let vis = self.stack.get_visible_child_name().unwrap();
let old = self.stack.get_child_by_name("podcasts").unwrap();
let old = self.stack
.get_child_by_name("podcasts")
// This is guaranted to exists, based on `ShowStack::new()`.
.unwrap()
.downcast::<gtk::Box>()
// This is guaranted to be a Box based on the `ShowsPopulated` impl.
.unwrap();
debug!("Name: {:?}", WidgetExt::get_name(&old));
let scrolled_window = old.get_children()
.first()
// This is guaranted to exist based on the show_widget.ui file.
.unwrap()
.clone()
.downcast::<gtk::ScrolledWindow>()
// This is guaranted based on the show_widget.ui file.
.unwrap();
debug!("Name: {:?}", WidgetExt::get_name(&scrolled_window));
let pop = ShowsPopulated::new(Arc::new(self.clone()), self.sender.clone()); let pop = ShowsPopulated::new(Arc::new(self.clone()), self.sender.clone());
// Copy the vertical scrollbar adjustment from the old view into the new one.
scrolled_window
.get_vadjustment()
.map(|x| pop.set_vadjustment(&x));
self.stack.remove(&old); self.stack.remove(&old);
self.stack.add_named(&pop.container, "podcasts"); self.stack.add_named(&pop.container, "podcasts");
@ -132,10 +154,10 @@ impl ShowStack {
pub fn replace_widget(&self, pd: &Podcast) { pub fn replace_widget(&self, pd: &Podcast) {
let old = self.stack let old = self.stack
.get_child_by_name("widget") .get_child_by_name("widget")
// This is guaranted to exists, based on ShowStack::new(). // This is guaranted to exists, based on `ShowStack::new()`.
.unwrap() .unwrap()
.downcast::<gtk::Box>() .downcast::<gtk::Box>()
// This is guaranted to be a Box based on the ShowWidget impl. // This is guaranted to be a Box based on the `ShowWidget` impl.
.unwrap(); .unwrap();
debug!("Name: {:?}", WidgetExt::get_name(&old)); debug!("Name: {:?}", WidgetExt::get_name(&old));
@ -218,10 +240,10 @@ impl EpisodeStack {
pub fn update(&self) { pub fn update(&self) {
let old = self.stack let old = self.stack
.get_child_by_name("episodes") .get_child_by_name("episodes")
// This is guaranted to exists, based on EpisodeStack::new(). // This is guaranted to exists, based on `EpisodeStack::new()`.
.unwrap() .unwrap()
.downcast::<gtk::Box>() .downcast::<gtk::Box>()
// This is guaranted to be a Box based on the EpisodesView impl. // This is guaranted to be a Box based on the `EpisodesView` impl.
.unwrap(); .unwrap();
debug!("Name: {:?}", WidgetExt::get_name(&old)); debug!("Name: {:?}", WidgetExt::get_name(&old));

View File

@ -15,21 +15,21 @@ use std::sync::Arc;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct ShowsPopulated { pub struct ShowsPopulated {
pub container: gtk::Box, pub container: gtk::Box,
scrolled_window: gtk::ScrolledWindow,
flowbox: gtk::FlowBox, flowbox: gtk::FlowBox,
viewport: gtk::Viewport,
} }
impl Default for ShowsPopulated { impl Default for ShowsPopulated {
fn default() -> Self { fn default() -> Self {
let builder = gtk::Builder::new_from_resource("/org/gnome/hammond/gtk/shows_view.ui"); let builder = gtk::Builder::new_from_resource("/org/gnome/hammond/gtk/shows_view.ui");
let container: gtk::Box = builder.get_object("fb_parent").unwrap(); let container: gtk::Box = builder.get_object("fb_parent").unwrap();
let scrolled_window: gtk::ScrolledWindow = builder.get_object("scrolled_window").unwrap();
let flowbox: gtk::FlowBox = builder.get_object("flowbox").unwrap(); let flowbox: gtk::FlowBox = builder.get_object("flowbox").unwrap();
let viewport: gtk::Viewport = builder.get_object("viewport").unwrap();
ShowsPopulated { ShowsPopulated {
container, container,
scrolled_window,
flowbox, flowbox,
viewport,
} }
} }
} }
@ -74,6 +74,11 @@ impl ShowsPopulated {
pub fn is_empty(&self) -> bool { pub fn is_empty(&self) -> bool {
self.flowbox.get_children().is_empty() self.flowbox.get_children().is_empty()
} }
/// Set scrolled window vertical adjustment.
pub fn set_vadjustment(&self, vadjustment: &gtk::Adjustment) {
self.scrolled_window.set_vadjustment(vadjustment)
}
} }
#[derive(Debug)] #[derive(Debug)]