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 Gio, Gtk, GLib 14 15from gettext import gettext as _ 16 17from eolie.define import App, LoadingType 18 19 20class MoveToMenu(Gtk.Grid): 21 """ 22 Menu allowing to move webviews to a window 23 """ 24 25 def __init__(self, webviews, window, back=True): 26 """ 27 Init menu 28 @param webviews as [WebView] 29 @param window as Window 30 @param back as bool 31 """ 32 self.__window = window 33 self.__actions = [] 34 self.__webviews = list(webviews) 35 Gtk.Menu.__init__(self) 36 self.set_margin_start(5) 37 self.set_margin_end(5) 38 self.set_margin_top(5) 39 self.set_margin_bottom(5) 40 self.set_orientation(Gtk.Orientation.VERTICAL) 41 42 # Back button 43 if back: 44 item = Gtk.ModelButton.new() 45 item.set_hexpand(True) 46 item.set_property("centered", True) 47 item.set_property("text", _("Move to")) 48 item.set_property("inverted", True) 49 item.set_property("menu-name", "main") 50 item.show() 51 self.add(item) 52 53 action = Gio.SimpleAction(name="switch_window") 54 action = Gio.SimpleAction.new("switch_window", 55 GLib.VariantType.new("s")) 56 action.connect('activate', 57 self.__on_action_activate) 58 window.add_action(action) 59 60 # New window button 61 item = Gtk.ModelButton.new() 62 item.set_hexpand(True) 63 item.set_property("text", _("New window")) 64 item.set_action_name("win.switch_window") 65 item.set_action_target_value(GLib.Variant("s", "new_window")) 66 item.show() 67 self.add(item) 68 if len(App().windows) > 1: 69 item = Gtk.Separator.new(Gtk.Orientation.HORIZONTAL) 70 item.show() 71 self.add(item) 72 73 for _window in App().windows: 74 if _window == window: 75 continue 76 item = Gtk.ModelButton.new() 77 item.set_hexpand(True) 78 item.set_property("text", _window.get_title()) 79 item.set_action_name("win.switch_window") 80 item.set_action_target_value(GLib.Variant("s", str(_window))) 81 item.show() 82 item.connect("enter-notify-event", 83 self.__on_enter_notify_event, 84 _window) 85 item.connect("leave-notify-event", 86 self.__on_leave_notify_event, 87 _window) 88 self.add(item) 89 90 def do_hide(self): 91 """ 92 Remove action on hide 93 """ 94 Gtk.Grid.do_hide(self) 95 self.__window.remove_action("switch_window") 96 97####################### 98# PRIVATE # 99####################### 100 def __on_enter_notify_event(self, widget, event, window): 101 """ 102 Mark window 103 @param widget as Gtk.Widget 104 @param event as Gdk.Event 105 @param window as Window 106 """ 107 window.mark(True) 108 109 def __on_leave_notify_event(self, widget, event, window): 110 """ 111 Unmark window 112 @param widget as Gtk.Widget 113 @param event as Gdk.Event 114 @param window as Window 115 """ 116 window.mark(False) 117 118 def __on_action_activate(self, action, variant): 119 """ 120 Moves webviews to window 121 @param action as Gio.SimpleAction 122 @param variant as GLib.Variant 123 @param window as Window 124 """ 125 window = None 126 window_str = variant.get_string() 127 128 # Get wanted window 129 if window_str == "new_window": 130 window = App().get_new_window() 131 else: 132 for window in App().windows: 133 if window_str == str(window): 134 break 135 if window is None: 136 return 137 138 # Move webviews to window 139 for webview in self.__webviews: 140 self.__window.container.remove_webview(webview) 141 window.container.add_webview(webview, LoadingType.FOREGROUND) 142 webview.set_window(window) 143