sites_manager_child.py 19.7 KB
Newer Older
Cédric Bellegarde's avatar
Cédric Bellegarde committed
1
# Copyright (c) 2017-2019 Cedric Bellegarde <cedric.bellegarde@adishatz.org>
2 3 4 5 6 7 8 9 10 11 12
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

13
from gi.repository import Gtk, Gdk, GLib, Pango, GObject, WebKit2
14

Cédric Bellegarde's avatar
Cédric Bellegarde committed
15
from eolie.label_indicator import LabelIndicator
16
from eolie.define import App, ArtSize
17
from eolie.utils import resize_favicon
18
from eolie.logger import Logger
19 20


21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
class PageChildRow(Gtk.ListBoxRow):
    """
        Label for a view
    """

    def __init__(self, view, window):
        """
            Init widget
            @param view as View
            @param window as Window
        """
        Gtk.ListBoxRow.__init__(self)
        eventbox = Gtk.EventBox()
        eventbox.show()
        self.get_style_context().add_class("page-child-row")
        self.__view = view
        self.__window = window
Cédric Bellegarde's avatar
Cédric Bellegarde committed
38
        self.__label = Gtk.Label.new(view.webview.title)
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
        self.__label.set_property("valign", Gtk.Align.CENTER)
        self.__label.set_ellipsize(Pango.EllipsizeMode.END)
        self.__label.set_hexpand(True)
        self.__label.show()
        button = Gtk.Button.new_from_icon_name("window-close-symbolic",
                                               Gtk.IconSize.MENU)
        button.set_relief(Gtk.ReliefStyle.NONE)
        button.get_style_context().add_class("no-padding")
        button.set_property("valign", Gtk.Align.CENTER)
        button.set_property("halign", Gtk.Align.END)
        button.connect("clicked", self.__on_button_clicked)
        button.show()
        grid = Gtk.Grid()
        grid.add(self.__label)
        grid.add(button)
        grid.show()
        eventbox.add(grid)
        self.add(eventbox)
        self.connect("destroy", self.__on_destroy)
        self.set_property("has-tooltip", True)
        self.connect("query-tooltip", self.__on_query_tooltip)
60
        view.webview.connect("load-changed", self.__on_webview_load_changed)
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
        view.webview.connect("title-changed", self.__on_webview_title_changed)
        eventbox.connect("button-press-event", self.__on_button_press_event)

    @property
    def view(self):
        """
            Get associated view
        """
        return self.__view

#######################
# PRIVATE             #
#######################
    def __on_button_clicked(self, button):
        """
            Close view
            @param button as Gtk.Button
        """
        self.__window.container.try_close_view(self.__view)

    def __on_query_tooltip(self, widget, x, y, keyboard, tooltip):
        """
            Show tooltip if needed
            @param widget as Gtk.Widget
            @param x as int
            @param y as int
            @param keyboard as bool
            @param tooltip as Gtk.Tooltip
        """
        title = self.__view.webview.get_title()
        if title is not None:
            tooltip = GLib.markup_escape_text(title)
            widget.set_tooltip_markup(tooltip)

    def __on_button_press_event(self, widget, event):
        """
            Switch to view
            @param widget as Gtk.Widget
            @param event as Gdk.Event
        """
        self.__window.container.set_current(self.__view, True)
        self.__window.container.set_expose(False)
        return True

    def __on_destroy(self, widget):
        """
            Disconnect signals
            @param widget as Gtk.Widget
        """
110
        self.__view.webview.disconnect_by_func(self.__on_webview_load_changed)
111 112
        self.__view.webview.disconnect_by_func(self.__on_webview_title_changed)

113
    def __on_webview_load_changed(self, webview, event):
114
        """
115
            Update widget content
116
            @param webview as WebView
117
            @param event as WebKit2.LoadEvent
118
        """
119 120
        if event in [WebKit2.LoadEvent.STARTED,
                     WebKit2.LoadEvent.COMMITTED]:
121 122 123
            uri = webview.get_uri()
            if uri is not None:
                self.__label.set_text(uri)
124 125 126 127 128 129 130 131 132 133

    def __on_webview_title_changed(self, webview, title):
        """
            Update title
            @param webview as WebView
            @param title as str
        """
        self.__label.set_text(title)


134 135 136 137 138
class SitesManagerChild(Gtk.ListBoxRow):
    """
        Child showing snapshot, title and favicon
    """

139 140 141 142
    __gsignals__ = {
        'moved': (GObject.SignalFlags.RUN_FIRST, None, (str, bool))
    }

143
    def __init__(self, netloc, window, ephemeral):
144 145 146 147
        """
            Init child
            @param netloc as str
            @param window as Window
148
            @param ephemeral as bool
149 150 151 152
        """
        Gtk.ListBoxRow.__init__(self)
        self.__window = window
        self.__netloc = netloc
153
        self.__minimal = None
154
        self.__current_child = None
155
        self.__ephemeral = ephemeral
156
        self.__views = []
157 158
        self.__connected_ids = []
        self.__scroll_timeout_id = None
159
        self.__pages_listbox = None
160
        self.set_margin_top(1)
161 162 163 164 165 166
        self.set_property("has-tooltip", True)
        self.connect("query-tooltip", self.__on_query_tooltip)
        builder = Gtk.Builder()
        builder.add_from_resource("/org/gnome/Eolie/SitesManagerChild.ui")
        builder.connect_signals(self)
        widget = builder.get_object("widget")
167
        self.__indicator_label = LabelIndicator(True)
Cédric Bellegarde's avatar
Cédric Bellegarde committed
168
        self.__indicator_label.set_property("halign", Gtk.Align.CENTER)
169
        self.__indicator_label.show()
170
        self.__separator = builder.get_object("separator")
171
        self.__grid = builder.get_object("grid")
172
        self.__netloc_label = builder.get_object("netloc")
Cédric Bellegarde's avatar
Cédric Bellegarde committed
173
        self.__netloc_label.set_text(self.__netloc)
174 175 176
        self.__image = builder.get_object("image")
        self.__image.set_property("pixel-size", ArtSize.FAVICON)
        self.add(widget)
177 178 179 180 181 182 183 184 185 186 187
        self.drag_source_set(Gdk.ModifierType.BUTTON1_MASK, [],
                             Gdk.DragAction.MOVE)
        self.drag_source_add_text_targets()
        self.connect("drag-begin", self.__on_drag_begin)
        self.connect("drag-data-get", self.__on_drag_data_get)
        self.drag_dest_set(Gtk.DestDefaults.DROP | Gtk.DestDefaults.MOTION,
                           [], Gdk.DragAction.MOVE)
        self.drag_dest_add_text_targets()
        self.connect("drag-data-received", self.__on_drag_data_received)
        self.connect("drag-motion", self.__on_drag_motion)
        self.connect("drag-leave", self.__on_drag_leave)
188

189
    def add_view(self, view):
190
        """
191 192
            Add view
            @param view as View
193 194
            @param uri as str
        """
195 196
        if view not in self.__views:
            self.__views.append(view)
197
            view.webview.connect("shown", self.__on_webview_shown)
198 199
            view.webview.connect("load-changed",
                                 self.__on_webview_load_changed)
200 201
            view.webview.connect("notify::is-playing-audio",
                                 self.__on_webview_notify_is_playing_audio)
202
            view.webview.connect("notify::favicon",
203
                                 self.__on_webview_favicon_changed)
204 205
            self.update_label()
            self.__indicator_label.update_count(True)
206
            if view.webview.shown:
207
                self.__on_webview_favicon_changed(view.webview)
208
            else:
209
                self.__indicator_label.mark_unshown(view.webview)
210
            if self.__pages_listbox is not None:
211 212 213
                child = PageChildRow(view, self.__window)
                child.show()
                self.__pages_listbox.add(child)
214

215
    def remove_view(self, view):
216
        """
217 218
            Remove view and destroy self if no more view
            @param view as View
219
        """
220 221
        if view in self.__views:
            self.__views.remove(view)
222 223
            view.webview.disconnect_by_func(self.__on_webview_shown)
            view.webview.disconnect_by_func(self.__on_webview_favicon_changed)
224
            view.webview.disconnect_by_func(self.__on_webview_load_changed)
225 226
            view.webview.disconnect_by_func(
                                    self.__on_webview_notify_is_playing_audio)
227 228 229 230
            self.update_label()
            self.__indicator_label.update_count(False)
            if not view.webview.shown:
                self.__indicator_label.mark_shown(view.webview)
231 232 233
            if self.__pages_listbox is not None:
                for child in self.__pages_listbox.get_children():
                    if child.view == view:
234
                        child.destroy()
235
                        break
236 237
            if self.__views:
                self.__on_webview_favicon_changed(self.__views[0].webview)
238

239 240 241 242 243
    def set_minimal(self, minimal):
        """
            Make widget minimal
            @param minimal as bool
        """
244 245
        if self.__minimal == minimal:
            return
246
        if minimal:
247 248 249 250 251 252
            self.__grid.remove(self.__netloc_label)
            if self.__pages_listbox is not None:
                self.__grid.remove(self.__pages_listbox)
                self.__pages_listbox.destroy()
            self.__pages_listbox = None
            self.__grid.attach(self.__indicator_label, 1, 0, 1, 1)
253
            self.__image.set_property("halign", Gtk.Align.CENTER)
254
            self.__image.set_hexpand(True)
255
            self.__separator.hide()
256
        else:
257 258 259
            self.__grid.remove(self.__indicator_label)
            self.__grid.attach(self.__netloc_label, 1, 0, 1, 1)
            self.__pages_listbox = Gtk.ListBox.new()
260
            self.__pages_listbox.set_sort_func(self.__sort_func)
261
            self.__pages_listbox.show()
262
            self.__separator.show()
263
            self.__grid.attach(self.__pages_listbox, 0, 2, 2, 1)
264
            self.__image.set_hexpand(False)
265
            self.__image.set_property("halign", Gtk.Align.START)
266 267 268 269 270 271 272 273 274
            # Setup listbox
            current = self.__window.container.current
            for view in self.__views:
                child = PageChildRow(view, self.__window)
                child.show()
                self.__pages_listbox.add(child)
                if view == current:
                    self.__pages_listbox.select_row(child)
        self.__minimal = minimal
275

276 277 278 279 280 281 282 283 284
    def reset(self, netloc):
        """
            Reset widget to new netloc
            @param netloc as str
        """
        if netloc != self.__netloc:
            self.__netloc = netloc
            self.__netloc_label.set_text(self.__netloc)

285
    def update_label(self):
286 287 288 289
        """
            Update label: if one view, use title else use netloc
            @param view as View
        """
290 291 292 293 294 295 296 297 298
        self.__netloc_label.set_text(self.__netloc)

    def set_selected(self, selected):
        """
            Mark self as selected
            @param selected as bool
        """
        if selected:
            self.get_style_context().add_class("item-selected")
299
        else:
300 301 302 303 304 305 306
            self.get_style_context().remove_class("item-selected")
        if self.__pages_listbox is not None:
            if selected:
                current = self.__window.container.current
                for child in self.__pages_listbox.get_children():
                    if child.view == current:
                        self.__pages_listbox.select_row(child)
307
                        self.__current_child = child
308
                self.__pages_listbox.invalidate_sort()
309 310
            else:
                self.__pages_listbox.unselect_all()
311
                self.__current_child = None
312

313 314 315
    @property
    def empty(self):
        """
316
            True if no view associated
317 318
            @return bool
        """
319
        return len(self.__views) == 0
320

321
    @property
322
    def views(self):
323
        """
324 325
            Get views
            @return [view]
326
        """
327
        return self.__views
328

329 330 331 332 333 334 335 336
    @property
    def ephemeral(self):
        """
            Get ephemeral
            @return bool
        """
        return self.__ephemeral

337 338 339 340 341 342 343 344 345 346 347
    @property
    def netloc(self):
        """
            Get netloc
            @return str
        """
        return self.__netloc

#######################
# PROTECTED           #
#######################
348 349 350 351 352 353
    def _on_button_press_event(self, eventbox, event):
        """
            Hide popover or close view
            @param eventbox as Gtk.EventBox
            @param event as Gdk.Event
        """
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
        try:
            if event.button == 2:
                for view in self.__views:
                    self.__window.container.try_close_view(view)
                return True
            elif event.button == 3:
                from eolie.menu_sites import SitesMenu
                from eolie.menu_move_to import MoveToMenu
                sites_menu = SitesMenu(self.__views, self.__window)
                sites_menu.show()
                moveto_menu = MoveToMenu(self.__views, self.__window)
                moveto_menu.show()
                popover = Gtk.PopoverMenu.new()
                popover.add(sites_menu)
                popover.add(moveto_menu)
                popover.child_set_property(moveto_menu,
                                           "submenu", "moveto")
                popover.set_relative_to(eventbox)
                popover.set_position(Gtk.PositionType.RIGHT)
                popover.forall(self.__update_popover_internals)
                popover.show()
                return True
        except Exception as e:
377
            Logger.error("SitesManagerChild::_on_button_press_event:", e)
378 379 380 381

#######################
# PRIVATE             #
#######################
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
    def __sort_func(self, row1, row2):
        """
            Sort pages
            @param row1 as PageChildRow
            @param row2 as PageChildRow
        """
        # Always show current first
        if self.__current_child in [row1, row2]:
            return self.__current_child == row2
        # Unshown first
        elif not row2.view.webview.shown and row1.view.webview.shown:
            return True
        else:
            return row2.view.webview.atime > row1.view.webview.atime

397
    def __update_popover_internals(self, widget):
398
        """
399
            Little hack to manage Gtk.ModelButton text
400 401
            @param widget as Gtk.Widget
        """
402 403
        if isinstance(widget, Gtk.Label):
            widget.set_ellipsize(Pango.EllipsizeMode.END)
404
            widget.set_max_width_chars(30)
405
            widget.set_tooltip_text(widget.get_text())
406
        elif hasattr(widget, "forall"):
407
            GLib.idle_add(widget.forall, self.__update_popover_internals)
408

409 410 411 412 413 414 415 416
    def __on_webview_notify_is_playing_audio(self, webview, playing):
        """
            Update favicon
            @param webview as WebView
            @param playing as bool
        """
        self.__on_webview_favicon_changed(webview)

417
    def __on_webview_favicon_changed(self, webview, *ignore):
418
        """
419
            Set favicon
420
            @param webview as WebView
421
        """
422 423
        if webview.current_event == WebKit2.LoadEvent.STARTED:
            return
424

425 426 427 428
        if webview.is_playing_audio():
            self.__image.set_from_icon_name("audio-speakers-symbolic",
                                            Gtk.IconSize.INVALID)
            return
429

430
        artwork = App().art.get_icon_theme_artwork(webview.uri,
431
                                                   webview.ephemeral)
432
        if artwork is not None:
433
            self.__image.set_from_icon_name(artwork, Gtk.IconSize.INVALID)
434
        else:
435 436 437 438 439 440
            surface = webview.get_favicon()
            if surface is not None:
                self.__image.set_from_surface(resize_favicon(surface))
            else:
                self.__image.set_from_icon_name("applications-internet",
                                                Gtk.IconSize.INVALID)
441

442 443 444 445 446 447 448 449 450
    def __on_query_tooltip(self, widget, x, y, keyboard, tooltip):
        """
            Show tooltip if needed
            @param widget as Gtk.Widget
            @param x as int
            @param y as int
            @param keyboard as bool
            @param tooltip as Gtk.Tooltip
        """
451 452
        if self.__pages_listbox is not None:
            return
453
        tooltip = "<b>%s</b>" % GLib.markup_escape_text(self.__netloc)
454 455
        for view in self.__views:
            title = view.webview.get_title()
456 457
            if title is not None:
                tooltip += "\n%s" % GLib.markup_escape_text(title)
458
        widget.set_tooltip_markup(tooltip)
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499

    def __on_drag_begin(self, widget, context):
        """
            Set icon
            @param widget as Gtk.Widget
            @param context as Gdk.DragContext
        """
        surface = self.__image.get_property("surface")
        if surface is None:
            return
        pixbuf = Gdk.pixbuf_get_from_surface(surface,
                                             0, 0,
                                             surface.get_width(),
                                             surface.get_height())

        widget.drag_source_set_icon_pixbuf(pixbuf)
        del pixbuf

    def __on_drag_data_get(self, widget, context, data, info, time):
        """
            Send netloc
            @param widget as Gtk.Widget
            @param context as Gdk.DragContext
            @param data as Gtk.SelectionData
            @param info as int
            @param time as int
        """
        data.set_text(self.__netloc, len(self.__netloc))

    def __on_drag_data_received(self, widget, context, x, y, data, info, time):
        """
            Move track
            @param widget as Gtk.Widget
            @param context as Gdk.DragContext
            @param x as int
            @param y as int
            @param data as Gtk.SelectionData
            @param info as int
            @param time as int
        """
        height = self.get_allocated_height()
Cédric Bellegarde's avatar
Cédric Bellegarde committed
500
        if y > height / 2:
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
            up = False
        else:
            up = True
        try:
            netloc = data.get_text()
            self.emit("moved", netloc, up)
        except:
            pass

    def __on_drag_motion(self, widget, context, x, y, time):
        """
            Add style
            @param widget as Gtk.Widget
            @param context as Gdk.DragContext
            @param x as int
            @param y as int
            @param time as int
        """
        height = self.get_allocated_height()
Cédric Bellegarde's avatar
Cédric Bellegarde committed
520
        if y > height / 2:
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
            self.get_style_context().add_class("drag-up")
            self.get_style_context().remove_class("drag-down")
        else:
            self.get_style_context().remove_class("drag-up")
            self.get_style_context().add_class("drag-down")

    def __on_drag_leave(self, widget, context, time):
        """
            Remove style
            @param widget as Gtk.Widget
            @param context as Gdk.DragContext
            @param time as int
        """
        self.get_style_context().remove_class("drag-up")
        self.get_style_context().remove_class("drag-down")
536 537 538

    def __on_webview_shown(self, webview):
        """
539
            Update indicator
540
        """
541
        self.__indicator_label.mark_shown(webview)
542
        self.__on_webview_favicon_changed(webview)
543 544 545 546 547 548 549 550 551 552 553 554

    def __on_webview_load_changed(self, webview, event):
        """
            Update widget content
            @param webview as WebView
            @param event as WebKit2.LoadEvent
        """
        if event == WebKit2.LoadEvent.STARTED:
            self.__image.set_from_icon_name("content-loading-symbolic",
                                            Gtk.IconSize.INVALID)
        elif event == WebKit2.LoadEvent.FINISHED:
            self.__on_webview_favicon_changed(webview)