This repository was archived by the owner on Sep 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Services
Kevin edited this page Mar 8, 2024
·
6 revisions
A service is a class that inherits from GObject.Object
. It is a singleton instance that emits signals when an internal value changes or when it receives new values. By default, one can connect to the signals with:
Service().connect("signal-name", callback)
An example to test could be the BatteryService:
from PotatoWidgets import BatteryService
BatteryService().connect("percentage", lambda _, value: do_something_with_this_value(value))
Now, for example, to use it with widgets:
from PotatoWidgets import BatteryService, Widget
MyDynamicLabel = Widget.Label("")
BatteryService().connect("percentage", lambda _, value: MyDynamicLabel.set_text(value))
# or
OtherDynamicLabel = Widget.Label(
attributes=lambda self: BatteryService().connect(
"percentage", lambda _, x: self.set_text(x)
)
)
In the previous case, the instance BatteryService()
is called, and the connect
method is used. However, there is also another method for services, which is better to use when services return useful information that should not be modified directly. For example, in a progress bar:
from PotatoWidgets import BatteryService, Widget
Percentage = BatteryService().bind("percentage")
BatteryProgress = Widget.ProgressBar(value=Percentage)
In this example, the bind
method will return a variable that will be updated when the service emits the specified signal.
from PotatoWidgets import BatteryService, Widget
BatteryModule = Widget.Overlay(
[
Widget.ProgressBar(
value=BatteryService().bind("percentage"),
orientation="v",
halign="center",
classname="battery-progress",
inverted=True,
),
# Nerd Font as icon
Widget.Label(text="", css="color: #111;", classname="nf-icon"),
]
)
.nf-icon {
font-family: "Symbols Nerd Font";
font-size: 20px;
min-width: 1em;
min-height: 1em;
}
.battery-progress {
background: #232323;
&,
& > trough,
& > trough > progress {
min-width: 20px;
border-radius: 20px;
}
trough progress {
background-color: #43b365;
}
}
Preview

from PotatoWidgets import Applications, Variable, Widget, lookup_icon
def GenerateApp(entry):
_app = Widget.Revealer( valign="start", transition="slideup", duration=250, reveal=True,
attributes=lambda self: (
setattr(self, "app", entry),
setattr(self, "keywords", entry.keywords),
self.bind(
AppQuery, lambda query: self.set_revealed(query in self.keywords)
),
),
children=Widget.Button( classname="app", valign="start",
onclick=lambda: (
entry.launch(),
AppLauncher.close(),
AppQuery.set_value(""),
),
children=Widget.Box( spacing=10,
children=[
Widget.Image(lookup_icon(entry.icon_name), 35),
Widget.Box( orientation="v", vexpand=True,
children=[
Widget.Label( entry.name.title(), wrap=True, halign="start", classname="name"),
Widget.Label( entry.comment or entry.generic_name, wrap=True, classname="comment",
)])])))
return _app
AppQuery = Variable("")
AppsList = Widget.Scroll( hexpand=True, vexpand=True,
children=Widget.Box(
orientation="v",
children=[GenerateApp(app) for app in Applications().all()],
),
)
AppLauncher = Widget.Window( size=[500, 600], layer="dialog", namespace="AppLauncher",
children=Widget.Box( classname="launcher", orientation="v", spacing=20,
children=[
Widget.Entry( onchange=AppQuery.set_value,
onenter=lambda text: next(
app_revealer.app.launch()
for app_revealer in AppsList.get_children()[0]
.get_children()[0]
.get_children()
if text in app_revealer.keywords
),
),
AppsList,
],
),
)
.launcher {
padding: 40px;
border-radius: 20px;
background: #111;
.app {
border-radius: 20px;
padding: 10px 20px;
&:hover {
background: #232323;
}
.name {
color: #fff;
font-weight: 700;
}
.comment {
color: #525252;
font-weight: 500,
}
}
}
Preview
