File: getfocus.py

package info (click to toggle)
liferea 1.15.8-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 7,652 kB
  • sloc: ansic: 27,425; javascript: 1,646; python: 1,123; makefile: 657; sh: 115; xml: 13
file content (131 lines) | stat: -rw-r--r-- 4,737 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
"""
GetFocus! Liferea plugin

Copyright (C) 2021-2022 Paweł Marciniak <sunwire+liferea@gmail.com

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser 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 Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.
"""
import os
from pathlib import Path
import gettext
from gi.repository import GObject, Gtk, Liferea, PeasGtk

# Initialize translations for tooltips
_ = lambda x: x
try:
    t = gettext.translation("liferea")
except FileNotFoundError:
    pass
else:
    _ = t.gettext

FILE_CONFIG = 'opacity.conf'


def get_path():
    config_path = "liferea/plugins/getfocus"
    config_home = os.getenv('XDG_CONFIG_HOME',Path.joinpath(Path.home(), ".config"))
    return Path.joinpath(Path(config_home), config_path)


class GetFocusPlugin(GObject.Object, Liferea.ShellActivatable):
    __gtype_name__ = 'GetFocusPlugin'

    shell = GObject.property(type=Liferea.Shell)
    feedlist = None
    opacity = 0.3
    enter_event = None
    leave_event = None

    def do_activate(self):
        self.feedlist = self.shell.lookup('feedlist')
        self.read_opacity_from_file()
        self.set_opacity_leave(self.feedlist, None)
        self.leave_event = self.feedlist.connect('leave-notify-event',
                                                 self.set_opacity_leave)
        self.enter_event = self.feedlist.connect('enter-notify-event',
                                                 self.set_opacity_enter)

    def do_deactivate(self):
        self.feedlist.disconnect(self.enter_event)
        self.feedlist.disconnect(self.leave_event)
        self.set_opacity_enter(self.feedlist, None)

    def set_opacity_enter(self, widget, event):
        self.opacity = widget.get_property('opacity')
        widget.set_property('opacity', 1)

    def set_opacity_leave(self, widget, event):
        widget.set_property('opacity', self.opacity)

    def read_opacity_from_file(self):
        path = get_path()
        file_path = path / FILE_CONFIG
        if file_path.exists():
            self.opacity = float(file_path.read_text())


class GetFocusConfigure(GObject.Object, PeasGtk.Configurable):
    __gtype_name__ = 'GetFocusConfigure'

    opacity = None
    feedlist = None
    opacity_scale = None

    def do_create_configure_widget(self):
        """ Setup configuration widget """
        margin = 6

        shell = Liferea.Shell
        self.feedlist = shell.lookup('feedlist')
        self.opacity = self.feedlist.get_property('opacity')

        grid = Gtk.Grid(column_spacing=10)
        label = Gtk.Label(_("Opacity:"))
        label.props.tooltip_text = _("Opacity")
        label.props.xalign = 0
        label.props.margin = margin
        label.props.expand = False
        grid.attach(label, 0, 0, 1, 1)

        adj = Gtk.Adjustment(self.opacity, 0, 1.0, 0.1, 0.2, 0)
        self.opacity_scale = Gtk.Scale(orientation=Gtk.Orientation.HORIZONTAL,
                                       adjustment=adj)
        self.opacity_scale.props.margin = 10
        self.opacity_scale.add_mark(0, Gtk.PositionType.BOTTOM, _('Min'))
        self.opacity_scale.add_mark(0.2, Gtk.PositionType.BOTTOM, None)
        self.opacity_scale.add_mark(0.4, Gtk.PositionType.BOTTOM, None)
        self.opacity_scale.add_mark(0.6, Gtk.PositionType.BOTTOM, None)
        self.opacity_scale.add_mark(0.8, Gtk.PositionType.BOTTOM, None)
        self.opacity_scale.add_mark(1.0, Gtk.PositionType.BOTTOM, _('Max'))
        self.opacity_scale.set_hexpand(True)
        self.opacity_scale.set_size_request(300, 10)  #width, height
        self.opacity_scale.connect("value-changed", self.scale_moved)
        grid.attach(self.opacity_scale, 1, 0, 1, 1)

        save_button = Gtk.Button(_('Save'))
        save_button.set_valign(Gtk.Align.CENTER)
        save_button.connect("clicked", self.save_opacity_to_file)
        grid.attach(save_button, 2, 0, 1, 1)
        return grid

    def scale_moved(self, event):
        self.opacity = self.opacity_scale.get_value()
        self.feedlist.set_property('opacity', self.opacity)

    def save_opacity_to_file(self, widget):
        path = get_path()
        path.mkdir(0o700, True, True)
        file_path = path / FILE_CONFIG
        file_path.write_text(str(self.opacity))