[gnome-tour] make the whole thing a coursel



commit 522f1c99027aad11ddeb636ded2caaa28844b040
Author: Bilal Elmoussaoui <bil elmoussaoui gmail com>
Date:   Tue Jul 14 01:55:36 2020 +0200

    make the whole thing a coursel

 src/application.rs           |  4 +--
 src/widgets/pages/image.rs   | 36 ++++++++----------------
 src/widgets/pages/page.rs    |  2 --
 src/widgets/pages/welcome.rs | 65 ++++++++++++++++++++++++++------------------
 src/widgets/paginator.rs     | 35 ++++++++++++++++--------
 src/widgets/window.rs        | 26 ++++--------------
 6 files changed, 81 insertions(+), 87 deletions(-)
---
diff --git a/src/application.rs b/src/application.rs
index f4b5c1b..8d8a003 100644
--- a/src/application.rs
+++ b/src/application.rs
@@ -70,7 +70,7 @@ impl Application {
             clone!(@strong application => move |_, _| {
                 if let Some(window) = &*application.window.borrow().clone() {
                     if window.paginator.borrow_mut().previous().is_err() {
-                        window.stop_tour();
+                        window.reset_tour();
                     }
                 }
             }),
@@ -88,7 +88,7 @@ impl Application {
            let window = Window::new(&gtk_app);
             gtk_app.add_window(&window.widget);
             window.widget.present();
-            window.widget.show_all();
+            window.widget.show();
             app.window.replace(Rc::new(Some(window)));
         }));
     }
diff --git a/src/widgets/pages/image.rs b/src/widgets/pages/image.rs
index 92bae44..08dbb91 100644
--- a/src/widgets/pages/image.rs
+++ b/src/widgets/pages/image.rs
@@ -3,10 +3,7 @@ use gtk::prelude::*;
 
 pub struct ImagePageWidget {
     pub widget: gtk::Box,
-    resource_uri: String,
     pub title: String,
-    pub head: String,
-    pub body: String,
 }
 
 impl Pageable for ImagePageWidget {
@@ -17,33 +14,19 @@ impl Pageable for ImagePageWidget {
     fn get_title(&self) -> String {
         self.title.clone()
     }
-
-    fn get_head(&self) -> String {
-        self.head.clone()
-    }
-
-    fn get_body(&self) -> String {
-        self.body.clone()
-    }
 }
 
 impl ImagePageWidget {
     pub fn new(resource_uri: &str, title: String, head: String, body: String) -> Self {
         let widget = gtk::Box::new(gtk::Orientation::Vertical, 0);
 
-        let image_page = Self {
-            widget,
-            resource_uri: resource_uri.to_string(),
-            title,
-            head,
-            body,
-        };
+        let image_page = Self { widget, title };
 
-        image_page.init();
+        image_page.init(resource_uri, head, body);
         image_page
     }
 
-    fn init(&self) {
+    fn init(&self, resource_uri: &str, head: String, body: String) {
         self.widget.set_property_expand(true);
         self.widget.set_halign(gtk::Align::Fill);
         self.widget.set_valign(gtk::Align::Fill);
@@ -53,26 +36,31 @@ impl ImagePageWidget {
         container.set_valign(gtk::Align::Center);
         container.set_property_margin(48);
 
-        let image = gtk::Image::from_resource(&self.resource_uri);
+        let image = gtk::Image::from_resource(&resource_uri);
         image.set_valign(gtk::Align::Start);
+        image.show();
         container.add(&image);
 
-        let head_label = gtk::Label::new(Some(&self.get_head()));
+        let head_label = gtk::Label::new(Some(&head));
         head_label.set_justify(gtk::Justification::Center);
         head_label.set_valign(gtk::Align::Center);
         head_label.set_margin_top(36);
         head_label.get_style_context().add_class("page-title");
+        head_label.show();
         container.add(&head_label);
 
-        let body_label = gtk::Label::new(Some(&self.get_body()));
+        let body_label = gtk::Label::new(Some(&body));
         body_label.set_lines(2);
         body_label.set_property_wrap(true);
         body_label.set_justify(gtk::Justification::Center);
         body_label.set_valign(gtk::Align::Center);
-        body_label.get_style_context().add_class("page-body");
         body_label.set_margin_top(12);
+        body_label.get_style_context().add_class("page-body");
+        body_label.show();
         container.add(&body_label);
 
+        container.show();
         self.widget.add(&container);
+        self.widget.show();
     }
 }
diff --git a/src/widgets/pages/page.rs b/src/widgets/pages/page.rs
index 01f8b2c..609f366 100644
--- a/src/widgets/pages/page.rs
+++ b/src/widgets/pages/page.rs
@@ -1,6 +1,4 @@
 pub trait Pageable {
     fn get_widget(&self) -> gtk::Widget;
     fn get_title(&self) -> String;
-    fn get_head(&self) -> String;
-    fn get_body(&self) -> String;
 }
diff --git a/src/widgets/pages/welcome.rs b/src/widgets/pages/welcome.rs
index b3a2185..8510243 100644
--- a/src/widgets/pages/welcome.rs
+++ b/src/widgets/pages/welcome.rs
@@ -1,15 +1,29 @@
+use super::page::Pageable;
 use gettextrs::gettext;
 use gtk::prelude::*;
-use libhandy::prelude::HeaderBarExt;
 
 pub struct WelcomePageWidget {
     pub widget: gtk::Box,
+    pub title: String,
+}
+
+impl Pageable for WelcomePageWidget {
+    fn get_widget(&self) -> gtk::Widget {
+        self.widget.clone().upcast::<gtk::Widget>()
+    }
+
+    fn get_title(&self) -> String {
+        self.title.clone()
+    }
 }
 
 impl WelcomePageWidget {
     pub fn new() -> Self {
         let widget = gtk::Box::new(gtk::Orientation::Vertical, 0);
-        let welcome_page = Self { widget };
+        let welcome_page = Self {
+            widget,
+            title: gettext("Welcome Tour"),
+        };
 
         welcome_page.init();
         welcome_page
@@ -17,13 +31,10 @@ impl WelcomePageWidget {
 
     fn init(&self) {
         self.widget.set_property_expand(true);
-
-        let container = gtk::Box::new(gtk::Orientation::Vertical, 0);
-        container.set_property_expand(true);
-        container.set_valign(gtk::Align::Center);
-        container.set_halign(gtk::Align::Center);
-        container.set_margin_top(24);
-        container.set_margin_bottom(24);
+        self.widget.set_valign(gtk::Align::Center);
+        self.widget.set_halign(gtk::Align::Center);
+        self.widget.set_margin_top(24);
+        self.widget.set_margin_bottom(24);
 
         let name = glib::get_os_info("NAME").unwrap_or("GNOME".into());
         let version = glib::get_os_info("VERSION").unwrap_or("3.36".into());
@@ -31,46 +42,46 @@ impl WelcomePageWidget {
 
         let logo = gtk::Image::from_icon_name(Some(&icon), gtk::IconSize::Dialog);
         logo.set_pixel_size(196);
-        container.add(&logo);
+        logo.show();
+        self.widget.add(&logo);
 
         let title = gtk::Label::new(Some(&gettext(format!("Welcome to {} {}", name, version))));
         title.set_margin_top(36);
         title.get_style_context().add_class("large-title");
-        container.add(&title);
+        title.show();
+        self.widget.add(&title);
 
         let text = gtk::Label::new(Some(&gettext("Hi there! If you are new to GNOME, you can take the tour 
to learn some essential features.")));
         text.get_style_context().add_class("body");
         text.set_margin_top(12);
-        container.add(&text);
+        text.show();
+        self.widget.add(&text);
 
         let actions_container = gtk::Box::new(gtk::Orientation::Horizontal, 12);
         actions_container.set_halign(gtk::Align::Center);
         actions_container.set_margin_top(36);
 
-        let start_tour_btn = gtk::Button::with_label(&gettext("_Take the Tour"));
-        start_tour_btn.get_style_context().add_class("suggested-action");
-        start_tour_btn.set_property_height_request(40);
-        start_tour_btn.set_property_width_request(180);
-        start_tour_btn.set_use_underline(true);
-        start_tour_btn.set_action_name(Some("app.start-tour"));
-
         let skip_tour_btn = gtk::Button::with_label(&gettext("_No Thanks"));
         skip_tour_btn.set_property_height_request(40);
         skip_tour_btn.set_property_width_request(180);
         skip_tour_btn.set_use_underline(true);
         skip_tour_btn.set_action_name(Some("app.skip-tour"));
-
+        skip_tour_btn.show();
         actions_container.add(&skip_tour_btn);
+
+        let start_tour_btn = gtk::Button::with_label(&gettext("_Take the Tour"));
+        start_tour_btn.set_property_height_request(40);
+        start_tour_btn.set_property_width_request(180);
+        start_tour_btn.set_use_underline(true);
+        start_tour_btn.set_action_name(Some("app.start-tour"));
+        start_tour_btn.get_style_context().add_class("suggested-action");
+        start_tour_btn.show();
         actions_container.add(&start_tour_btn);
         actions_container.set_focus_child(Some(&start_tour_btn));
 
-        container.add(&actions_container);
-
-        let headerbar = libhandy::HeaderBar::new();
-        headerbar.set_show_close_button(true);
-        headerbar.set_title(Some(&gettext("Welcome Tour")));
+        actions_container.show();
 
-        self.widget.add(&headerbar);
-        self.widget.add(&container);
+        self.widget.add(&actions_container);
+        self.widget.show();
     }
 }
diff --git a/src/widgets/paginator.rs b/src/widgets/paginator.rs
index fc9571a..ccd34fd 100644
--- a/src/widgets/paginator.rs
+++ b/src/widgets/paginator.rs
@@ -15,6 +15,7 @@ pub struct PaginatorWidget {
     current_page: RefCell<u32>,
     next_btn: gtk::Button,
     close_btn: gtk::Button,
+    previous_btn: gtk::Button,
 }
 
 impl PaginatorWidget {
@@ -27,6 +28,7 @@ impl PaginatorWidget {
             headerbar: libhandy::HeaderBar::new(),
             next_btn: gtk::Button::with_label(&gettext("_Next")),
             close_btn: gtk::Button::with_label(&gettext("_Close")),
+            previous_btn: gtk::Button::with_label(&gettext("_Previous")),
             pages: RefCell::new(Vec::new()),
             current_page: RefCell::new(0),
         });
@@ -63,15 +65,23 @@ impl PaginatorWidget {
     fn update_position(&self) {
         let n_pages = self.carousel.get_n_pages() as f64;
         let position = self.carousel.get_position();
-        let opacity = (position - n_pages + 2_f64).max(0_f64);
+        let page_nr = position.round() as u32;
 
-        self.close_btn.set_opacity(opacity);
-        self.close_btn.set_visible(opacity > 0_f64);
+        let opacity_close = (position - n_pages + 2_f64).max(0_f64);
+        let opacity_previous = if position <= 1_f64 { position } else { 1_f64 };
+        let opacity_next = if position <= 1_f64 && position <= n_pages { position % n_pages } else { 1_f64 };
+
+        self.close_btn.set_opacity(opacity_close);
+        self.close_btn.set_visible(opacity_close > 0_f64);
+
+        self.previous_btn.set_opacity(opacity_previous);
+        self.previous_btn.set_visible(opacity_previous > 0_f64);
+
+        self.next_btn.set_opacity(opacity_next);
+        self.next_btn.set_visible(opacity_next > 0_f64);
 
-        let page_nr = position.round() as u32;
         let pages = &self.pages.borrow();
         let page = pages.get(page_nr as usize).unwrap();
-
         self.headerbar.set_title(Some(&page.get_title()));
         self.current_page.replace(page_nr);
     }
@@ -79,17 +89,14 @@ impl PaginatorWidget {
     fn init(&self, p: Rc<Self>) {
         self.carousel.set_property_expand(true);
         self.carousel.set_animation_duration(300);
+        self.carousel.show();
 
         self.carousel.connect_property_position_notify(clone!(@weak p => move |_| {
             p.update_position();
         }));
 
-        let previous_btn = gtk::Button::with_label(&gettext("_Previous"));
-        previous_btn.set_use_underline(true);
-        previous_btn.set_action_name(Some("app.previous-page"));
-
         let btn_size_group = gtk::SizeGroup::new(gtk::SizeGroupMode::Horizontal);
-        btn_size_group.add_widget(&previous_btn);
+        btn_size_group.add_widget(&self.previous_btn);
         btn_size_group.add_widget(&self.next_btn);
         btn_size_group.add_widget(&self.close_btn);
 
@@ -101,16 +108,22 @@ impl PaginatorWidget {
         self.close_btn.set_use_underline(true);
         self.close_btn.set_action_name(Some("app.next-page"));
 
+        self.previous_btn.set_use_underline(true);
+        self.previous_btn.set_action_name(Some("app.previous-page"));
+
         let next_overlay = gtk::Overlay::new();
         next_overlay.add(&self.next_btn);
         next_overlay.add_overlay(&self.close_btn);
+        next_overlay.show();
 
-        self.headerbar.pack_start(&previous_btn);
+        self.headerbar.pack_start(&self.previous_btn);
         self.headerbar.pack_end(&next_overlay);
         self.headerbar.set_show_close_button(false);
+        self.headerbar.show();
 
         self.widget.add(&self.headerbar);
         self.widget.add(&self.carousel);
+        self.widget.show();
     }
 
     pub fn set_page(&self, page_nr: u32) {
diff --git a/src/widgets/window.rs b/src/widgets/window.rs
index 31f0df3..c802304 100644
--- a/src/widgets/window.rs
+++ b/src/widgets/window.rs
@@ -6,13 +6,10 @@ use std::rc::Rc;
 use super::pages::{ImagePageWidget, WelcomePageWidget};
 use super::paginator::PaginatorWidget;
 use crate::config::PROFILE;
-use libhandy::prelude::DeckExt;
 
 pub struct Window {
     pub widget: libhandy::ApplicationWindow,
-    deck: libhandy::Deck,
     pub paginator: RefCell<Rc<PaginatorWidget>>,
-    welcome_page: WelcomePageWidget,
 }
 
 impl Window {
@@ -20,43 +17,31 @@ impl Window {
         let widget = libhandy::ApplicationWindow::new();
         widget.set_application(Some(app));
 
-        let deck = libhandy::Deck::new();
         let paginator = RefCell::new(PaginatorWidget::new());
 
-        let mut window_widget = Window {
-            widget,
-            deck,
-            welcome_page: WelcomePageWidget::new(),
-            paginator,
-        };
+        let mut window_widget = Window { widget, paginator };
 
         window_widget.init();
         window_widget
     }
 
     pub fn start_tour(&self) {
-        self.deck.set_visible_child(&self.paginator.borrow().widget);
-        self.paginator.borrow_mut().set_page(0);
+        self.paginator.borrow_mut().set_page(1);
     }
 
-    pub fn stop_tour(&self) {
+    pub fn reset_tour(&self) {
         self.paginator.borrow_mut().set_page(0);
-        self.deck.set_visible_child(&self.welcome_page.widget);
     }
 
     fn init(&mut self) {
         self.widget.set_default_size(920, 640);
-        self.deck.set_transition_type(libhandy::DeckTransitionType::Slide);
-        self.deck.set_transition_duration(300);
-        self.deck.set_can_swipe_back(true);
-        self.deck.set_can_swipe_forward(true);
 
         // Devel Profile
         if PROFILE == "Devel" {
             self.widget.get_style_context().add_class("devel");
         }
 
-        self.deck.add(&self.welcome_page.widget);
+        self.paginator.borrow_mut().add_page(Box::new(WelcomePageWidget::new()));
 
         self.paginator.borrow_mut().add_page(Box::new(ImagePageWidget::new(
             "/org/gnome/Tour/activities.svg",
@@ -101,7 +86,6 @@ impl Window {
         last_page.widget.get_style_context().add_class("last-page");
         self.paginator.borrow_mut().add_page(Box::new(last_page));
 
-        self.deck.add(&self.paginator.borrow().widget);
-        self.widget.add(&self.deck);
+        self.widget.add(&self.paginator.borrow().widget);
     }
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]