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 Gdk
14
15from eolie.popover_uri_input import Input
16from eolie.define import Type
17
18
19class UriPopoverEvents:
20    """
21        Events handler for UriPopover
22    """
23
24    def __init__(self):
25        """
26            Init handler
27        """
28        pass
29
30    def forward_event(self, keyval, state):
31        """
32            Forward event, smart navigation between boxes
33            @param keyval as bool
34            @param state as state as Gdk.ModifierType
35            @return True if event forwarded
36        """
37        if not self.is_visible():
38            return False
39        if keyval == Gdk.KEY_Up and self._input == Input.NONE:
40            return False
41        elif keyval == Gdk.KEY_Left and self._input == Input.BOOKMARKS:
42            self._input = Input.TAGS
43            self._tags_box.get_style_context().add_class("kbd-input")
44            self._bookmarks_box.get_style_context().remove_class("kbd-input")
45            self._window.toolbar.title.entry.set_text_entry("")
46            return True
47        elif keyval == Gdk.KEY_Right and self._input == Input.TAGS:
48            self._input = Input.BOOKMARKS
49            self._bookmarks_box.get_style_context().add_class("kbd-input")
50            self._tags_box.get_style_context().remove_class("kbd-input")
51            return True
52        elif keyval in [Gdk.KEY_Left, Gdk.KEY_Right] and\
53                self._input == Input.SEARCH:
54            return False
55        elif keyval in [Gdk.KEY_Left, Gdk.KEY_Right] and\
56                self._input != Input.NONE:
57            return True
58        elif keyval in [Gdk.KEY_Down, Gdk.KEY_Up]:
59            # If nothing selected, detect default widget
60            if self._input == Input.NONE:
61                if self._stack.get_visible_child_name() == "search":
62                    self._input = Input.SEARCH
63                elif self._stack.get_visible_child_name() == "bookmarks":
64                    self._tags_box.get_style_context().add_class("kbd-input")
65                    self._input = Input.TAGS
66                else:
67                    self._input = Input.NONE
68                box = self._get_current_input_box()
69                if box is not None:
70                    rows = box.get_children()
71                    if rows:
72                        box.select_row(None)
73                        box.select_row(rows[0])
74                return True
75            box = self._get_current_input_box()
76            rows = box.get_children()
77            if box is None or not rows:
78                self._input = Input.NONE
79                return False
80            selected = box.get_selected_row()
81            # If nothing selected, select first row
82            if selected is None:
83                box.select_row(rows[0])
84                if self._input == Input.TAGS:
85                    item_id = rows[0].item.get_property("id")
86                    self._set_bookmarks(item_id)
87                return True
88            else:
89                idx = -1 if keyval == Gdk.KEY_Up else 1
90                for row in rows:
91                    if row == selected:
92                        break
93                    idx += 1
94                box.select_row(None)
95                if idx >= len(rows):
96                    box.select_row(rows[0])
97                    if self._input == Input.TAGS:
98                        item_id = rows[0].item.get_property("id")
99                        self._set_bookmarks(item_id)
100                    return True
101                elif idx < 0:
102                    # Do not go to uribar for bookmarks & search
103                    if self._input in [Input.BOOKMARKS, Input.SEARCH]:
104                        box.select_row(rows[-1])
105                        return True
106                    else:
107                        box.select_row(None)
108                        self._input = Input.NONE
109                        box.get_style_context().remove_class("kbd-input")
110                else:
111                    box.select_row(rows[idx])
112                    if self._input == Input.TAGS:
113                        item_id = rows[idx].item.get_property("id")
114                        self._set_bookmarks(item_id)
115                    return True
116        elif keyval in [Gdk.KEY_Return, Gdk.KEY_KP_Enter]:
117            box = self._get_current_input_box()
118            if box is not None:
119                selected = box.get_selected_row()
120                if selected is not None:
121                    container = self._window.container
122                    item = selected.item
123                    # Switch to view
124                    if item.get_property("type") == Type.WEBVIEW:
125                        title = item.get_property("title")
126                        uri = item.get_property("uri")
127                        for webview in container.webviews:
128                            if webview.uri == uri and webview.title == title:
129                                container.set_visible_webview(webview)
130                                self._window.close_popovers()
131                                break
132                    # Load URI
133                    else:
134                        uri = selected.item.get_property("uri")
135                        if uri:
136                            self._window.close_popovers()
137                            container.webview.load_uri(uri)
138                            container.set_expose(False)
139                            return True
140            else:
141                self._input = Input.NONE
142        else:
143            self._input = Input.NONE
144        return False
145