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, GLib, Gtk 14 15from gettext import gettext as _ 16 17from eolie.define import App, LoadingType 18 19 20class ApplicationMenu(Gio.Menu): 21 """ 22 Show application menu 23 """ 24 25 def __init__(self, window): 26 """ 27 Init menu 28 @param window as Window 29 """ 30 Gio.Menu.__init__(self) 31 self.__window = window 32 # Main actions menu 33 action = Gio.SimpleAction(name="new-private") 34 App().add_action(action) 35 action.connect("activate", 36 self.__on_private_clicked) 37 self.append(_("New private page"), "app.new-private") 38 39 # Blockers submenu 40 blockers_menu = Gio.Menu() 41 for (label, blocker) in [ 42 (_("Block ads"), "block-ads"), 43 (_("Block popups"), "block-popups"), 44 (_("Block images"), "block-images"), 45 (_("Block multimedia"), "block-medias"), 46 (_("Block scripts"), "block-scripts")]: 47 value = App().settings.get_value(blocker) 48 action = Gio.SimpleAction.new_stateful( 49 blocker, 50 None, 51 GLib.Variant.new_boolean(value)) 52 App().add_action(action) 53 action.connect("change-state", 54 self.__on_blocker_change_state, 55 blocker) 56 blockers_menu.append(label, "app.%s" % blocker) 57 58 self.append_submenu(_("Blockers"), blockers_menu) 59 60 # Default application menu 61 section = Gio.Menu() 62 settings_action = Gio.SimpleAction.new("settings", None) 63 settings_action.connect("activate", self.__on_settings_activate) 64 App().add_action(settings_action) 65 section.append(_("_Preferences"), "app.settings") 66 67 show_sidebar = App().settings.get_value("show-sidebar") 68 sidebar_action = Gio.SimpleAction.new_stateful( 69 "sidebar", 70 None, 71 GLib.Variant.new_boolean(show_sidebar)) 72 sidebar_action.connect("change-state", self.__on_sidebar_change_state) 73 App().add_action(sidebar_action) 74 section.append(_("_Sidebar"), "app.sidebar") 75 76 value = App().settings.get_value("night-mode") 77 action = Gio.SimpleAction.new_stateful( 78 "night-mode", 79 None, 80 GLib.Variant.new_boolean(value)) 81 App().add_action(action) 82 action.connect("change-state", 83 self.__on_night_mode_change_state) 84 section.append(_("Night mode"), "app.night-mode") 85 86 shortcuts_action = Gio.SimpleAction.new("shortcuts", None) 87 shortcuts_action.connect("activate", self.__on_shortcuts_activate) 88 App().add_action(shortcuts_action) 89 section.append(_("_Keyboard Shortcuts"), "app.shortcuts") 90 self.append_section(None, section) 91 92 about_action = Gio.SimpleAction.new("about", None) 93 about_action.connect("activate", self.__on_about_activate) 94 App().add_action(about_action) 95 section.append(_("_About"), "app.about") 96 97####################### 98# PRIVATE # 99####################### 100 def __on_settings_activate(self, action, param): 101 """ 102 Show settings dialog 103 @param action as Gio.SimpleAction 104 @param param as GLib.Variant 105 """ 106 from eolie.dialog_settings import SettingsDialog 107 dialog = SettingsDialog(self.__window) 108 dialog.show() 109 110 def __on_about_activate(self, action, param): 111 """ 112 Setup about dialog 113 @param action as Gio.SimpleAction 114 @param param as GLib.Variant 115 """ 116 builder = Gtk.Builder() 117 builder.add_from_resource('/org/gnome/Eolie/AboutDialog.ui') 118 about = builder.get_object('about_dialog') 119 about.set_transient_for(self.__window) 120 about.connect("response", self.__on_about_activate_response) 121 about.show() 122 123 def __on_shortcuts_activate(self, action, param): 124 """ 125 Show shortcuts 126 @param action as Gio.SimpleAction 127 @param param as GLib.Variant 128 """ 129 builder = Gtk.Builder() 130 builder.add_from_resource("/org/gnome/Eolie/Shortcuts.ui") 131 shortcuts = builder.get_object("shortcuts") 132 shortcuts.set_transient_for(self.__window) 133 shortcuts.show() 134 135 def __on_about_activate_response(self, dialog, response_id): 136 """ 137 Destroy about dialog when closed 138 @param dialog as Gtk.Dialog 139 @param response ID as int 140 """ 141 dialog.destroy() 142 143 def __on_sidebar_change_state(self, action, value): 144 """ 145 Show/hide sidebar 146 @param action as Gio.SimpleAction 147 @param value as bool 148 """ 149 action.set_state(value) 150 App().settings.set_value("show-sidebar", GLib.Variant("b", value)) 151 152 def __on_blocker_change_state(self, action, value, blocker): 153 """ 154 Update blocker value 155 @param action as Gio.SimpleAction 156 @param value as bool 157 @param blocker as str 158 """ 159 action.set_state(value) 160 App().settings.set_value(blocker, GLib.Variant("b", value)) 161 self.__window.container.webview.reload() 162 163 def __on_night_mode_change_state(self, action, value): 164 """ 165 Update night mode value 166 @param action as Gio.SimpleAction 167 @param value as bool 168 """ 169 action.set_state(value) 170 App().settings.set_value("night-mode", GLib.Variant("b", value)) 171 settings = Gtk.Settings.get_default() 172 settings.set_property("gtk-application-prefer-dark-theme", value) 173 for window in App().windows: 174 GLib.idle_add(window.toolbar.title.entry.update_style) 175 for webview in window.container.webviews: 176 webview.night_mode() 177 178 def __on_private_clicked(self, action, variant): 179 """ 180 Add a new private view 181 @param Gio.SimpleAction 182 @param GVariant 183 """ 184 App().active_window.container.add_webview_for_uri( 185 App().start_page, 186 LoadingType.FOREGROUND, 187 True) 188