refactor(ags systray): use subclass

This commit is contained in:
matt1432 2023-11-13 16:33:19 -05:00
parent 5a66324c35
commit 43cf2ec543

View file

@ -1,8 +1,6 @@
import SystemTray from 'resource:///com/github/Aylur/ags/service/systemtray.js';
import { timeout } from 'resource:///com/github/Aylur/ags/utils.js';
import { Box, Icon, MenuItem, Revealer } from 'resource:///com/github/Aylur/ags/widget.js';
import Gtk from 'gi://Gtk';
import { Box, Icon, MenuItem, MenuBar, Revealer } from 'resource:///com/github/Aylur/ags/widget.js';
import Separator from '../misc/separator.js';
@ -16,25 +14,21 @@ const SysTrayItem = item => {
transition: 'slide_right',
child: Icon({
size: 24,
binds: [['icon', item, 'icon']],
}),
}),
submenu: item.menu,
connections: [[item, btn => {
btn.child.child.icon = item.icon;
btn.tooltipMarkup = item.tooltipMarkup;
}]],
binds: [['tooltipMarkup', item, 'tooltip-markup']],
});
};
const SysTray = () => {
const widget = Gtk.MenuBar.new();
const SysTray = () => MenuBar({
setup: self => {
self.items = new Map();
// Properties
widget._items = new Map();
widget._onAdded = id => {
self.onAdded = id => {
const item = SystemTray.getItem(id);
if (widget._items.has(id) || !item)
if (self.items.has(id) || !item)
return;
const w = SysTrayItem(item);
@ -42,29 +36,28 @@ const SysTray = () => {
if (!w)
return;
widget._items.set(id, w);
widget.add(w);
widget.show_all();
self.items.set(id, w);
self.add(w);
self.show_all();
w.child.revealChild = true;
};
widget._onRemoved = id => {
if (!widget._items.has(id))
self.onRemoved = id => {
if (!self.items.has(id))
return;
widget._items.get(id).child.revealChild = false;
self.items.get(id).child.revealChild = false;
timeout(400, () => {
widget._items.get(id).destroy();
widget._items.delete(id);
self.items.get(id).destroy();
self.items.delete(id);
});
};
// Connections
SystemTray.connect('added', (_, id) => widget._onAdded(id));
SystemTray.connect('removed', (_, id) => widget._onRemoved(id));
return widget;
};
},
connections: [
[SystemTray, (self, id) => self.onAdded(id), 'added'],
[SystemTray, (self, id) => self.onRemoved(id), 'removed'],
],
});
export default () => Revealer({
transition: 'slide_right',