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
14
15
16class Stack(Gtk.Overlay):
17    """
18        Compatible with Gtk.Stack API
19        Used to get snapshot from WebKit by unmapping webview only when needed
20    """
21
22    def __init__(self):
23        """
24            Init overlay
25        """
26        Gtk.Overlay.__init__(self)
27        self.__visible_child = None
28
29    def add(self, webview):
30        """
31            Add widget to stack
32            @param webview as WebView
33        """
34        webview.connect("snapshot-changed", self.__on_webview_snapshot_changed)
35        webview.connect("destroy", self.__on_webview_destroy)
36        self.add_overlay(webview)
37        self.reorder_overlay(webview, 0)
38        if self.__visible_child is None:
39            self.__visible_child = webview
40
41    def set_visible_child(self, webview):
42        """
43            Set visible child
44            @param webview as WebView
45        """
46        if self.__visible_child is not None and\
47                self.__visible_child.is_snapshot_valid:
48            self.__visible_child.hide()
49        webview.show()
50        self.__visible_child = webview
51        self.reorder_overlay(webview, -1)
52
53    def get_visible_child(self):
54        """
55            Get visible child
56            @return Gtk.Widget
57        """
58        return self.__visible_child
59
60#######################
61# PRIVATE             #
62#######################
63    def __on_webview_destroy(self, webview):
64        """
65            Reset visible child
66            @param webview as WebView
67        """
68        if self.__visible_child == webview:
69            self.__visible_child = None
70
71    def __on_webview_snapshot_changed(self, webview, surface):
72        """
73            We can now hide this view if not visible one
74            @param webview as WebView
75            @param surface as cairo.Surface
76        """
77        if webview != self.__visible_child:
78            webview.hide()
79