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, Pango, GLib
14
15
16class UriLabelWidget(Gtk.EventBox):
17    """
18        Small label trying to not be under mouse pointer
19    """
20
21    def __init__(self):
22        """
23            Init label
24        """
25        Gtk.EventBox.__init__(self)
26        self.__label = Gtk.Label.new()
27        self.__label.set_ellipsize(Pango.EllipsizeMode.END)
28        self.__label.get_style_context().add_class("urilabel")
29        self.__label.show()
30        self.add(self.__label)
31        self.connect("enter-notify-event", self.__on_enter_notify)
32
33    def set_text(self, text):
34        """
35            Set label text
36            @param text as str
37        """
38        if text == self.__label.get_text():
39            return
40        self.set_property("halign", Gtk.Align.END)
41        self.set_property("valign", Gtk.Align.END)
42        self.__label.get_style_context().add_class("bottom-right")
43        self.__label.get_style_context().remove_class("bottom-left")
44        self.__label.get_style_context().remove_class("top-left")
45        self.__label.set_text(text)
46
47#######################
48# PRIVATE             #
49#######################
50    def __on_enter_notify(self, widget, event):
51        """
52            Try to go away from mouse cursor
53            @param widget as Gtk.Widget
54            @param event as Gdk.Event
55        """
56        GLib.idle_add(self.hide)
57        # Move label at the right
58        if self.get_property("halign") == Gtk.Align.END:
59            self.set_property("halign", Gtk.Align.START)
60            self.__label.get_style_context().add_class("bottom-left")
61            self.__label.get_style_context().remove_class("bottom-right")
62            self.__label.get_style_context().remove_class("top-left")
63        # Move label at top
64        else:
65            self.set_property("halign", Gtk.Align.END)
66            self.set_property("valign", Gtk.Align.END)
67            self.__label.get_style_context().add_class("top-left")
68            self.__label.get_style_context().remove_class("bottom-left")
69            self.__label.get_style_context().remove_class("bottom-right")
70        GLib.idle_add(self.show)
71