base_view.rs 2.81 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// base_view.rs
//
// Copyright 2017 Jordan Petridis <jpetridis@gnome.org>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: GPL-3.0-or-later

20
use crate::utils::smooth_scroll_to;
21
use gtk::{self, prelude::*, Adjustment, Orientation, PolicyType};
22 23 24 25 26 27 28 29 30 31

#[derive(Debug, Clone)]
pub(crate) struct BaseView {
    container: gtk::Box,
    scrolled_window: gtk::ScrolledWindow,
}

impl Default for BaseView {
    fn default() -> Self {
        let container = gtk::Box::new(Orientation::Horizontal, 0);
32 33 34 35 36 37 38 39 40 41 42 43
        // TODO: Remember to file an issue about this API
        // error[E0283]: type annotations required: cannot resolve `_: gdk_pixbuf::IsA<gtk::Adjustment>`
        // --> ../podcasts-gtk/src/widgets/base_view.rs:32:31
        //    |
        // 32 |         let scrolled_window = gtk::ScrolledWindow::new(None, None);
        //    |                               ^^^^^^^^^^^^^^^^^^^^^^^^
        //    |
        //    = note: required by `gtk::ScrolledWindow::new`
        //
        // error: aborting due to previous error
        let foo: Option<&Adjustment> = None;
        let scrolled_window = gtk::ScrolledWindow::new(foo.clone(), foo.clone());
44 45

        scrolled_window.set_policy(PolicyType::Never, PolicyType::Automatic);
46
        container.set_size_request(360, -1);
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
        container.add(&scrolled_window);
        container.show_all();

        BaseView {
            container,
            scrolled_window,
        }
    }
}

impl BaseView {
    pub(crate) fn container(&self) -> &gtk::Box {
        &self.container
    }

    pub(crate) fn scrolled_window(&self) -> &gtk::ScrolledWindow {
        &self.scrolled_window
    }

    pub(crate) fn add<T: IsA<gtk::Widget>>(&self, widget: &T) {
        self.scrolled_window.add(widget);
    }
69

70
    pub(crate) fn set_adjutments<'a, 'b>(
71 72 73 74 75 76 77 78 79 80 81 82
        &self,
        hadjustment: Option<&'a Adjustment>,
        vadjustment: Option<&'b Adjustment>,
    ) {
        if let Some(h) = hadjustment {
            smooth_scroll_to(&self.scrolled_window, h);
        }

        if let Some(v) = vadjustment {
            smooth_scroll_to(&self.scrolled_window, v);
        }
    }
83 84 85 86

    pub(crate) fn get_vadjustment(&self) -> Option<Adjustment> {
        self.scrolled_window().get_vadjustment()
    }
87
}