1# Copyright (c) 2017-2021 Cedric Bellegarde <cedric.bellegarde@adishatz.org>
2# This program is free software: you can redistribute it and/or modify
3# it under the terms of the GNU General Public License as published by
4# the Free Software Foundation, either version 3 of the License, or
5# (at your option) any later version.
6# This program is distributed in the hope that it will be useful,
7# but WITHOUT ANY WARRANTY; without even the implied warranty of
8# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9# GNU General Public License for more details.
10# You should have received a copy of the GNU General Public License
11# along with this program. If not, see <http://www.gnu.org/licenses/>.
12
13from gi.repository import Gtk, GLib
14
15from eolie.pages_manager_box import PagesManagerBox
16
17
18class ExposeContainer:
19    """
20        Expose management for container
21    """
22
23    def __init__(self):
24        """
25            Init container
26        """
27        self.__next_timeout_id = None
28        self.__previous_timeout_id = None
29        self.__expose_stack = Gtk.Stack()
30        self.__expose_stack.set_hexpand(True)
31        self.__expose_stack.set_vexpand(True)
32        self.__expose_stack.set_transition_type(
33            Gtk.StackTransitionType.CROSSFADE)
34        self.__expose_stack.set_transition_duration(150)
35        self.__expose_stack.show()
36        self.__pages_manager = PagesManagerBox(self._window)
37        self.__pages_manager.show()
38        self.overlay.add(self.__expose_stack)
39        self.__expose_stack.add_named(self._stack, "stack")
40        self.__expose_stack.add_named(self.__pages_manager, "expose")
41
42    def set_expose(self, expose):
43        """
44            Show current views
45            @param expose as bool
46        """
47        if expose:
48            self.__pages_manager.update_sort(self.sites_manager.sort)
49            self.__pages_manager.set_filtered(True)
50        else:
51            self._window.toolbar.actions.view_button.set_active(False)
52            self._window.container.pages_manager.set_filter("")
53            self.__pages_manager.set_filtered(False)
54        self.__set_expose(expose)
55
56    def next(self):
57        """
58            Show next view
59        """
60        if self.__next_timeout_id is None and\
61                self.__next_timeout_id != -1:
62            self.__next_timeout_id = GLib.timeout_add(
63                100,
64                self.__on_prev_next_timeout,
65                self.__pages_manager.next)
66        else:
67            self.__pages_manager.next()
68
69    def previous(self):
70        """
71            Show next view
72        """
73        if self.__previous_timeout_id is None and\
74                self.__previous_timeout_id != -1:
75            self.__previous_timeout_id = GLib.timeout_add(
76                100,
77                self.__on_prev_next_timeout,
78                self.__pages_manager.previous)
79        else:
80            self.__pages_manager.previous()
81
82    def ctrl_released(self):
83        """
84            Disable any pending expose
85        """
86        if self.__next_timeout_id is not None:
87            if self.__next_timeout_id != -1:
88                self.pages_manager.next()
89                GLib.source_remove(self.__next_timeout_id)
90        if self.__previous_timeout_id is not None:
91            if self.__previous_timeout_id != -1:
92                self.pages_manager.previous()
93                GLib.source_remove(self.__previous_timeout_id)
94
95        self.__next_timeout_id = None
96        self.__previous_timeout_id = None
97        self.set_expose(False)
98
99    @property
100    def in_expose(self):
101        """
102            True if in expose mode
103            @return bool
104        """
105        return self.__expose_stack.get_visible_child_name() == "expose"
106
107    @property
108    def pages_manager(self):
109        """
110            Get pages manager
111            @return PagesManager
112        """
113        return self.__pages_manager
114
115    @property
116    def webviews(self):
117        """
118            Get webviews
119            @return webviews as [WebView]
120        """
121        return self._stack.get_children()
122
123#######################
124# PRIVATE             #
125#######################
126    def __on_prev_next_timeout(self, callback):
127        """
128            Set expose on and call callback
129            @param callback as __next()/__previous()
130        """
131        self.__next_timeout_id = -1
132        self.__previous_timeout_id = -1
133        if not self.in_expose:
134            self.__set_expose(True)
135        callback()
136
137    def __set_expose(self, expose):
138        """
139            Show current views
140            @param expose as bool
141            @param search as bool
142        """
143        # Show expose mode
144        if expose:
145            self.__expose_stack.set_visible_child_name("expose")
146        else:
147            self.__expose_stack.set_visible_child_name("stack")
148            self.__pages_manager.update_visible_child()
149