media_viewer.rs 7.01 KB
Newer Older
1
extern crate gtk;
2 3
extern crate gdk;

4 5

use self::gtk::prelude::*;
6
use self::gdk::WindowExt;
7 8
use self::gdk::DisplayExt;
use self::gdk::SeatExt;
9

10 11 12
use glib;
use std::sync::{Arc, Mutex};

13 14 15 16 17
use app::App;

impl App {
    pub fn connect_media_viewer_headerbar(&self) {
        let op = self.op.clone();
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
        let zoom_entry = self.ui.builder
            .get_object::<gtk::Entry>("zoom_entry")
            .expect("Cant find zoom_entry in ui file.");
        zoom_entry.connect_activate(move |_| {
            op.lock().unwrap().change_zoom_level();
        });

        let op = self.op.clone();
        let zoom_out_button = self.ui.builder
            .get_object::<gtk::Button>("zoom_out_button")
            .expect("Cant find zoom_out_button in ui file.");
        zoom_out_button.connect_clicked(move |_| {
            op.lock().unwrap().zoom_out();
        });

        let op = self.op.clone();
        let zoom_in_button = self.ui.builder
            .get_object::<gtk::Button>("zoom_in_button")
            .expect("Cant find zoom_in_button in ui file.");
        zoom_in_button.connect_clicked(move |_| {
            op.lock().unwrap().zoom_in();
        });

        let op = self.op.clone();
42 43 44 45 46 47 48 49
        let full_screen_button = self.ui.builder
            .get_object::<gtk::Button>("full_screen_button")
            .expect("Cant find full_screen_button in ui file.");
        full_screen_button.connect_clicked(move |_| {
            op.lock().unwrap().enter_full_screen();
        });

        let op = self.op.clone();
50
        let back_btn = self.ui.builder
51 52
            .get_object::<gtk::Button>("media_viewer_back_button")
            .expect("Cant find media_viewer_back_button in ui file.");
53
        back_btn.connect_clicked(move |_| {
54
            op.lock().unwrap().hide_media_viewer();
55 56 57 58
        });
    }

    pub fn connect_media_viewer_box(&self) {
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
        let ui = self.ui.clone();
        let hovered: Arc<Mutex<bool>> = Arc::new(Mutex::new(false));

        let previous_media_button = ui.builder
            .get_object::<gtk::Button>("previous_media_button")
            .expect("Cant find previous_media_button in ui file.");

        previous_media_button.connect_enter_notify_event(clone!(hovered => move |_, _| {
            *(hovered.lock().unwrap()) = true;

            Inhibit(false)
        }));
        previous_media_button.connect_leave_notify_event(clone!(hovered => move |_, _| {
            *(hovered.lock().unwrap()) = false;

            Inhibit(false)
        }));

        let next_media_button = ui.builder
            .get_object::<gtk::Button>("next_media_button")
            .expect("Cant find next_media_button in ui file.");

        next_media_button.connect_enter_notify_event(clone!(hovered => move |_, _| {
            *(hovered.lock().unwrap()) = true;

            Inhibit(false)
        }));
        next_media_button.connect_leave_notify_event(clone!(hovered => move |_, _| {
            *(hovered.lock().unwrap()) = false;

            Inhibit(false)
        }));

        let media_viewer_box = ui.builder
            .get_object::<gtk::Box>("media_viewer_box")
            .expect("Cant find media_viewer_box in ui file.");

        let source_id: Arc<Mutex<Option<glib::source::SourceId>>> = Arc::new(Mutex::new(None));
        media_viewer_box.connect_motion_notify_event(move |_, _| {
            {
                let mut id = source_id.lock().unwrap();
                if let Some(sid) = id.take() {
                    glib::source::source_remove(sid);
                }
            }

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
            let main_window = ui.builder
                .get_object::<gtk::Window>("main_window")
                .expect("Cant find main_window in ui file.");
            if let Some(win) = main_window.get_window() {
                if let Some(disp) = gdk::Display::get_default() {
                    if let Some(seat) = disp.get_default_seat() {
                        if let Some(ptr) = seat.get_pointer() {
                            let (_, _, y, _) = win.get_device_position(&ptr);
                            if y <= 6 && win.get_state().contains(gdk::WindowState::FULLSCREEN) {
                                headerbar_revealer.set_reveal_child(true);
                            }
                        }
                    }
                }
            }

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
            let previous_media_revealer = ui.builder
                .get_object::<gtk::Revealer>("previous_media_revealer")
                .expect("Cant find previous_media_revealer in ui file.");
            previous_media_revealer.set_reveal_child(true);

            let next_media_revealer = ui.builder
                .get_object::<gtk::Revealer>("next_media_revealer")
                .expect("Cant find next_media_revealer in ui file.");
            next_media_revealer.set_reveal_child(true);

            let sid = gtk::timeout_add(1000, clone!(ui, hovered, source_id => move || {
                if !*hovered.lock().unwrap() {
                    let previous_media_revealer = ui.builder
                        .get_object::<gtk::Revealer>("previous_media_revealer")
                        .expect("Cant find previous_media_revealer in ui file.");
                    previous_media_revealer.set_reveal_child(false);

                    let next_media_revealer = ui.builder
                        .get_object::<gtk::Revealer>("next_media_revealer")
                        .expect("Cant find next_media_revealer in ui file.");
                    next_media_revealer.set_reveal_child(false);
                }

                *(source_id.lock().unwrap()) = None;
                gtk::Continue(false)
            }));

            *(source_id.lock().unwrap()) = Some(sid);
            Inhibit(false)
        });

152 153 154 155 156 157 158
        let op = self.op.clone();
        let previous_media_button = self.ui.builder
            .get_object::<gtk::Button>("previous_media_button")
            .expect("Cant find previous_media_button in ui file.");
        previous_media_button.connect_clicked(move |_| {
            op.lock().unwrap().previous_media();
        });
159

160 161 162 163 164 165 166
        let op = self.op.clone();
        let next_media_button = self.ui.builder
            .get_object::<gtk::Button>("next_media_button")
            .expect("Cant find next_media_button in ui file.");
        next_media_button.connect_clicked(move |_| {
            op.lock().unwrap().next_media();
        });
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186

        // Connecting escape key to leave fullscreen mode
        let main_window = self.ui.builder
            .get_object::<gtk::ApplicationWindow>("main_window")
            .expect("Cant find main_window in ui file.");
        let op = self.op.clone();
        main_window.connect_key_release_event(move |w, k| {
            // leave full screen only if we're currently in fullscreen
            if let Some(win) = w.get_window() {
                if !win.get_state().contains(gdk::WindowState::FULLSCREEN) {
                    return Inhibit(false);
                }
            }

            if k.get_keyval() == gdk::enums::key::Escape {
                op.lock().unwrap().leave_full_screen();
            }

            Inhibit(false)
        });
187 188
    }
}