Skip to content

Commit

Permalink
Merge pull request #68 from Aylur/feat/tray-gtk4
Browse files Browse the repository at this point in the history
tray: gtk4 compatibility
  • Loading branch information
Aylur authored Dec 19, 2024
2 parents 4f47b69 + c5664e2 commit c34349c
Show file tree
Hide file tree
Showing 20 changed files with 365 additions and 306 deletions.
50 changes: 32 additions & 18 deletions examples/js/simple-bar/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,45 @@ window.Bar {
font-size: 1.1em;
font-weight: bold;

button {
all: unset;
background-color: transparent;
label {
margin: 0 8px;
}

.Workspaces {
button {
all: unset;
background-color: transparent;

&:hover label {
background-color: color.adjust($fg, $alpha: -0.84);
border-color: color.adjust($accent, $alpha: -0.8);
&:hover label {
background-color: color.adjust($fg, $alpha: -0.84);
border-color: color.adjust($accent, $alpha: -0.8);
}

&:active label {
background-color: color.adjust($fg, $alpha: -0.8)
}
}

&:active label {
background-color: color.adjust($fg, $alpha: -0.8)
label {
transition: 200ms;
padding: 0 8px;
margin: 2px;
border-radius: $radius;
border: 1pt solid transparent;
}
}

label {
transition: 200ms;
padding: 0 8px;
margin: 2px;
border-radius: $radius;
border: 1pt solid transparent;
.focused label {
color: $accent;
border-color: $accent;
}
}

.Workspaces .focused label {
color: $accent;
border-color: $accent;
.SysTray {
margin-right: 8px;

button {
padding: 0 4px;
}
}

.FocusedClient {
Expand Down
22 changes: 8 additions & 14 deletions examples/js/simple-bar/widget/Bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,16 @@ import Tray from "gi://AstalTray"
function SysTray() {
const tray = Tray.get_default()

return <box>
{bind(tray, "items").as(items => items.map(item => {
if (item.iconThemePath)
App.add_icons(item.iconThemePath)

const menu = item.create_menu()

return <button
return <box className="SysTray">
{bind(tray, "items").as(items => items.map(item => (
<menubutton
tooltipMarkup={bind(item, "tooltipMarkup")}
onDestroy={() => menu?.destroy()}
onClickRelease={self => {
menu?.popup_at_widget(self, Gdk.Gravity.SOUTH, Gdk.Gravity.NORTH, null)
}}>
usePopover={false}
actionGroup={bind(item, "action-group").as(ag => ["dbusmenu", ag])}
menuModel={bind(item, "menu-model")}>
<icon gIcon={bind(item, "gicon")} />
</button>
}))}
</menubutton>
))}
</box>
}

Expand Down
50 changes: 32 additions & 18 deletions examples/lua/simple-bar/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,45 @@ window.Bar {
font-size: 1.1em;
font-weight: bold;

button {
all: unset;
background-color: transparent;
label {
margin: 0 8px;
}

.Workspaces {
button {
all: unset;
background-color: transparent;

&:hover label {
background-color: color.adjust($fg, $alpha: -0.84);
border-color: color.adjust($accent, $alpha: -0.8);
&:hover label {
background-color: color.adjust($fg, $alpha: -0.84);
border-color: color.adjust($accent, $alpha: -0.8);
}

&:active label {
background-color: color.adjust($fg, $alpha: -0.8)
}
}

&:active label {
background-color: color.adjust($fg, $alpha: -0.8)
label {
transition: 200ms;
padding: 0 8px;
margin: 2px;
border-radius: $radius;
border: 1pt solid transparent;
}
}

label {
transition: 200ms;
padding: 0 8px;
margin: 2px;
border-radius: $radius;
border: 1pt solid transparent;
.focused label {
color: $accent;
border-color: $accent;
}
}

.Workspaces .focused label {
color: $accent;
border-color: $accent;
.SysTray {
margin-right: 8px;

button {
padding: 0 4px;
}
}

.FocusedClient {
Expand Down
28 changes: 8 additions & 20 deletions examples/lua/simple-bar/widget/Bar.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
local astal = require("astal")
local App = require("astal.gtk3.app")
local Widget = require("astal.gtk3.widget")
local Variable = astal.Variable
local Gdk = astal.require("Gdk", "3.0")
local GLib = astal.require("GLib")
local bind = astal.bind
local Mpris = astal.require("AstalMpris")
Expand All @@ -17,26 +15,16 @@ local function SysTray()
local tray = Tray.get_default()

return Widget.Box({
class_name = "SysTray",
bind(tray, "items"):as(function(items)
return map(items, function(item)
if item.icon_theme_path ~= nil then
App:add_icons(item.icon_theme_path)
end

local menu = item:create_menu()

return Widget.Button({
return Widget.MenuButton({
tooltip_markup = bind(item, "tooltip_markup"),
on_destroy = function()
if menu ~= nil then
menu:destroy()
end
end,
on_click_release = function(self)
if menu ~= nil then
menu:popup_at_widget(self, Gdk.Gravity.SOUTH, Gdk.Gravity.NORTH, nil)
end
end,
use_popover = false,
menu_model = bind(item, "menu-model"),
action_group = bind(item, "action-group"):as(function(ag)
return { "dbusmenu", ag }
end),
Widget.Icon({
g_icon = bind(item, "gicon"),
}),
Expand Down Expand Up @@ -189,10 +177,10 @@ return function(gdkmonitor)
}),
Widget.Box({
halign = "END",
SysTray(),
Wifi(),
AudioSlider(),
BatteryLevel(),
SysTray(),
Time("%H:%M - %A %e."),
}),
}),
Expand Down
6 changes: 3 additions & 3 deletions examples/py/simple-bar/app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3
import sys
import versions
import subprocess
from gi.repository import AstalIO, Astal, Gio
from widget.Bar import Bar
from pathlib import Path
Expand All @@ -18,9 +19,8 @@ def do_astal_application_request(

def do_activate(self) -> None:
self.hold()
AstalIO.Process.execv(["sass", scss, css])
subprocess.run(["sass", scss, css])
self.apply_css(css, True)
print("hello")
for mon in self.get_monitors():
self.add_window(Bar(mon))

Expand All @@ -30,7 +30,7 @@ def do_activate(self) -> None:

if __name__ == "__main__":
try:
print(app.acquire_socket())
app.acquire_socket()
app.run(None)
except Exception as e:
print(AstalIO.send_message(instance_name, "".join(sys.argv[1:])))
50 changes: 32 additions & 18 deletions examples/py/simple-bar/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,45 @@ window.Bar {
font-size: 1.1em;
font-weight: bold;

button {
all: unset;
background-color: transparent;
label {
margin: 0 8px;
}

.Workspaces {
button {
all: unset;
background-color: transparent;

&:hover label {
background-color: color.adjust($fg, $alpha: -0.84);
border-color: color.adjust($accent, $alpha: -0.8);
&:hover label {
background-color: color.adjust($fg, $alpha: -0.84);
border-color: color.adjust($accent, $alpha: -0.8);
}

&:active label {
background-color: color.adjust($fg, $alpha: -0.8)
}
}

&:active label {
background-color: color.adjust($fg, $alpha: -0.8)
label {
transition: 200ms;
padding: 0 8px;
margin: 2px;
border-radius: $radius;
border: 1pt solid transparent;
}
}

label {
transition: 200ms;
padding: 0 8px;
margin: 2px;
border-radius: $radius;
border: 1pt solid transparent;
.focused label {
color: $accent;
border-color: $accent;
}
}

.Workspaces .focused label {
color: $accent;
border-color: $accent;
.SysTray {
margin-right: 8px;

button {
padding: 0 4px;
}
}

.FocusedClient {
Expand Down
30 changes: 10 additions & 20 deletions examples/py/simple-bar/widget/Bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ def on_cover_art(*_):
class SysTray(Gtk.Box):
def __init__(self) -> None:
super().__init__()
Astal.widget_set_class_names(self, ["SysTray"])
self.items = {}
tray = Tray.get_default()
tray.connect("item_added", self.add_item)
Expand All @@ -118,33 +119,22 @@ def add_item(self, _: Tray.Tray, id: str):
return

item = Tray.get_default().get_item(id)
theme = item.get_icon_theme_path()

if theme is not None:
from app import app

app.add_icons(theme)

menu = item.create_menu()
btn = Astal.Button(visible=True)
btn = Gtk.MenuButton(use_popover=False, visible=True)
icon = Astal.Icon(visible=True)

def on_clicked(btn):
if menu:
menu.popup_at_widget(btn, Gdk.Gravity.SOUTH, Gdk.Gravity.NORTH, None)
item.bind_property("tooltip-markup", btn, "tooltip-markup", SYNC)
item.bind_property("gicon", icon, "g-icon", SYNC)
item.bind_property("menu-model", btn, "menu-model", SYNC)
btn.insert_action_group("dbusmenu", item.get_action_group())

def on_destroy(btn):
if menu:
menu.destroy()
def on_action_group(*args):
btn.insert_action_group("dbusmenu", item.get_action_group())

btn.connect("clicked", on_clicked)
btn.connect("destroy", on_destroy)
item.connect("notify::action-group", on_action_group)

item.bind_property("tooltip-markup", btn, "tooltip-markup", SYNC)
item.bind_property("gicon", icon, "gicon", SYNC)
btn.add(icon)
self.add(btn)
self.items[id] = btn
self.show_all()

def remove_item(self, _: Tray.Tray, id: str):
if id in self.items:
Expand Down
19 changes: 6 additions & 13 deletions examples/vala/simple-bar/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,15 @@ pkgconfig_deps = [
# needed for GLib.Math
deps = pkgconfig_deps + meson.get_compiler('c').find_library('m')

custom_target(
'style.css',
command: [
find_program('sass'),
meson.project_source_root() / 'style.scss',
'@OUTPUT@',
],
output: 'style.css',
install: true,
install_dir: libdir,
)

main = configure_file(
input: 'app.in.vala',
output: 'app.vala',
configuration: {'STYLE': libdir / 'style.css'},
configuration: {
'STYLE': run_command(
find_program('sass'),
meson.project_source_root() / 'style.scss',
).stdout(),
},
)

sources = files(
Expand Down
Loading

0 comments on commit c34349c

Please sign in to comment.