h-gtk: Smooth out some stack transitions.

This commit is contained in:
Jordan Petridis 2018-05-16 19:40:36 +03:00
parent ccd3e3ab2c
commit 7d598bb1d0
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
2 changed files with 19 additions and 9 deletions

View File

@ -1,5 +1,6 @@
use gtk; use gtk;
use gtk::prelude::*; use gtk::prelude::*;
use gtk::StackTransitionType;
use failure::Error; use failure::Error;
use hammond_data::dbqueries::is_episodes_populated; use hammond_data::dbqueries::is_episodes_populated;
@ -60,6 +61,7 @@ impl HomeStack {
.ok(); .ok();
self.replace_view()?; self.replace_view()?;
// Determine the actuall state.
self.determine_state().map_err(From::from) self.determine_state().map_err(From::from)
} }
@ -69,8 +71,15 @@ impl HomeStack {
let eps = HomeView::new(self.sender.clone())?; let eps = HomeView::new(self.sender.clone())?;
// Remove the old widget and add the new one // Remove the old widget and add the new one
// during this the previous view is removed,
// and the visibile child fallsback to empty view.
self.stack.remove(old); self.stack.remove(old);
self.stack.add_named(&eps.container, "home"); self.stack.add_named(&eps.container, "home");
// Keep the previous state.
let s = self.state;
// Set the visible child back to the previous one to avoid
// the stack transition animation to show the empty view
self.switch_visible(s, StackTransitionType::None);
// replace view in the struct too // replace view in the struct too
self.episodes = eps; self.episodes = eps;
@ -81,16 +90,16 @@ impl HomeStack {
Ok(()) Ok(())
} }
fn switch_visible(&mut self, s: State) { fn switch_visible(&mut self, s: State, animation: StackTransitionType) {
use self::State::*; use self::State::*;
match s { match s {
Home => { Home => {
self.stack.set_visible_child_name("home"); self.stack.set_visible_child_full("home", animation);
self.state = Home; self.state = Home;
} }
Empty => { Empty => {
self.stack.set_visible_child_name("empty"); self.stack.set_visible_child_full("empty", animation);
self.state = Empty; self.state = Empty;
} }
} }
@ -98,9 +107,9 @@ impl HomeStack {
fn determine_state(&mut self) -> Result<(), DataError> { fn determine_state(&mut self) -> Result<(), DataError> {
if is_episodes_populated()? { if is_episodes_populated()? {
self.switch_visible(State::Home); self.switch_visible(State::Home, StackTransitionType::Crossfade);
} else { } else {
self.switch_visible(State::Empty); self.switch_visible(State::Empty, StackTransitionType::Crossfade);
}; };
Ok(()) Ok(())

View File

@ -1,5 +1,6 @@
use gtk; use gtk;
use gtk::prelude::*; use gtk::prelude::*;
use gtk::StackTransitionType;
use failure::Error; use failure::Error;
@ -65,7 +66,7 @@ impl PopulatedStack {
// to make sure it will stay the same. // to make sure it will stay the same.
let s = self.state; let s = self.state;
self.replace_shows()?; self.replace_shows()?;
self.switch_visible(s, gtk::StackTransitionType::None); self.switch_visible(s, StackTransitionType::Crossfade);
Ok(()) Ok(())
} }
@ -105,7 +106,7 @@ impl PopulatedStack {
// removal and insertion in the gtk::Stack, so we have // removal and insertion in the gtk::Stack, so we have
// to make sure it will stay the same. // to make sure it will stay the same.
let s = self.state; let s = self.state;
self.switch_visible(s, gtk::StackTransitionType::None); self.switch_visible(s, StackTransitionType::None);
Ok(()) Ok(())
} }
@ -124,7 +125,7 @@ impl PopulatedStack {
// removal and insertion in the gtk::Stack, so we have // removal and insertion in the gtk::Stack, so we have
// to make sure it will stay the same. // to make sure it will stay the same.
let s = self.state; let s = self.state;
self.switch_visible(s, gtk::StackTransitionType::None); self.switch_visible(s, StackTransitionType::Crossfade);
old.destroy(); old.destroy();
Ok(()) Ok(())
@ -144,7 +145,7 @@ impl PopulatedStack {
self.container.clone() self.container.clone()
} }
pub fn switch_visible(&mut self, state: PopulatedState, animation: gtk::StackTransitionType) { pub fn switch_visible(&mut self, state: PopulatedState, animation: StackTransitionType) {
use self::PopulatedState::*; use self::PopulatedState::*;
match state { match state {