By using this site, you agree to have cookies stored on your device, strictly for functional purposes, such as storing your session and preferences.

Dismiss

 __init__.py

View raw Download
text/plain • 3.59 kiB
Python script, ASCII text executable
        
            
1
"""
2
Launcher applets for the Panorama panel.
3
Copyright 2025, roundabout-host.com <vlad@roundabout-host.com>
4
5
This program is free software: you can redistribute it and/or modify
6
it under the terms of the GNU General Public Licence as published by
7
the Free Software Foundation, either version 3 of the Licence, or
8
(at your option) any later version.
9
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU General Public Licence for more details.
14
15
You should have received a copy of the GNU General Public Licence
16
along with this program. If not, see <https://www.gnu.org/licenses/>.
17
"""
18
19
import os
20
import subprocess
21
from pathlib import Path
22
import panorama_panel
23
24
import gi
25
gi.require_version("Gtk", "4.0")
26
27
from gi.repository import Gtk, GLib, Gio, GioUnix, Gdk
28
29
30
class CommandButton(panorama_panel.Applet):
31
name = "Command button"
32
description = "Run a command when clicked"
33
icon = Gio.ThemedIcon.new("utilities-terminal")
34
35
def __init__(self, orientation=Gtk.Orientation.HORIZONTAL, config=None, **kwargs):
36
super().__init__(orientation=orientation, config=config, **kwargs)
37
if config is None:
38
config = {}
39
self.button = Gtk.Button()
40
self.button.set_has_frame(False) # flat look
41
self.inner_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
42
self.button.set_child(self.inner_box)
43
self.icon_name = config.get("icon_name")
44
self.icon_size = config.get("icon_size", 24)
45
self.label = config.get("label")
46
self.command = config.get("command")
47
self.button.connect("clicked", self.execute)
48
if config.get("icon_name"):
49
self.inner_box.append(Gtk.Image(icon_name=config["icon_name"], pixel_size=config.get("icon_size", 24)))
50
if config.get("label"):
51
self.inner_box.append(Gtk.Label.new(config["label"]))
52
53
self.append(self.button)
54
55
def execute(self, button, *args):
56
subprocess.Popen(self.command, shell=True, start_new_session=True)
57
58
def get_config(self):
59
return {"label": self.label, "icon_name": self.icon_name, "icon_size": self.icon_size, "command": self.command}
60
61
62
class DesktopFileButton(panorama_panel.Applet):
63
name = "Desktop file button"
64
description = "Launch a desktop file when clicked"
65
icon = Gio.ThemedIcon.new("applications-other")
66
67
def __init__(self, orientation=Gtk.Orientation.HORIZONTAL, config=None, **kwargs):
68
super().__init__(orientation=orientation, config=config, **kwargs)
69
if config is None:
70
config = {}
71
self.button = Gtk.Button()
72
self.button.set_has_frame(False) # flat look
73
self.inner_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
74
self.button.set_child(self.inner_box)
75
self.desktop_file = config.get("desktop_file")
76
self.app_id = config.get("app_id")
77
if self.desktop_file:
78
self.app_info = GioUnix.DesktopAppInfo.new_from_filename(self.desktop_file)
79
elif self.app_id:
80
self.app_info = GioUnix.DesktopAppInfo.new(self.app_id)
81
self.button.connect("clicked", self.execute)
82
self.icon_size = config.get("icon_size", 24)
83
self.inner_box.append(Gtk.Image(gicon=self.app_info.get_icon(), pixel_size=self.icon_size))
84
85
self.append(self.button)
86
87
def execute(self, button, *args):
88
if self.app_info:
89
self.app_info.launch()
90
91
def get_config(self):
92
return {"desktop_file": self.desktop_file, "app_id": self.app_id, "icon_size": self.icon_size}
93