Skip to content

Commit ba79b4d

Browse files
committed
refactor(tray): cleanup and fixes
1 parent bd62c4c commit ba79b4d

File tree

14 files changed

+209
-243
lines changed

14 files changed

+209
-243
lines changed

include/ALabel.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace waybar {
77

88
class ALabel : public IModule {
99
public:
10-
ALabel(const Json::Value&, const std::string format);
10+
ALabel(const Json::Value&, const std::string& format);
1111
virtual ~ALabel() = default;
1212
virtual auto update() -> void;
1313
virtual std::string getIcon(uint16_t, const std::string& alt = "");
@@ -24,7 +24,7 @@ class ALabel : public IModule {
2424
bool handleToggle(GdkEventButton* const& ev);
2525
bool handleScroll(GdkEventScroll*);
2626
bool alt = false;
27-
const std::string default_format_;
27+
const std::string& default_format_;
2828
};
2929

3030
} // namespace waybar

include/modules/battery.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#pragma once
22

33
#ifdef FILESYSTEM_EXPERIMENTAL
4-
#include <experimental/filesystem>
4+
#include <experimental/filesystem>
55
#else
6-
#include <filesystem>
6+
#include <filesystem>
77
#endif
88
#include <fstream>
99
#include <iostream>

include/modules/sni/host.hpp

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#pragma once
2+
3+
#include <gtkmm.h>
4+
#include <json/json.h>
5+
#include <tuple>
6+
#include <dbus-status-notifier-watcher.h>
7+
#include "modules/sni/item.hpp"
8+
9+
namespace waybar::modules::SNI {
10+
11+
class Host {
12+
public:
13+
Host(const Json::Value&, const std::function<void(std::unique_ptr<Item>&)>&,
14+
const std::function<void(std::unique_ptr<Item>&)>&);
15+
~Host();
16+
private:
17+
void busAcquired(const Glib::RefPtr<Gio::DBus::Connection>&, Glib::ustring);
18+
void nameAppeared(const Glib::RefPtr<Gio::DBus::Connection>&, Glib::ustring, const Glib::ustring&);
19+
void nameVanished(const Glib::RefPtr<Gio::DBus::Connection>&, Glib::ustring);
20+
static void proxyReady(GObject*, GAsyncResult*, gpointer);
21+
static void registerHost(GObject*, GAsyncResult*, gpointer);
22+
static void itemRegistered(SnWatcher*, const gchar*, gpointer);
23+
static void itemUnregistered(SnWatcher*, const gchar*, gpointer);
24+
25+
std::tuple<std::string, std::string> getBusNameAndObjectPath(const std::string);
26+
void addRegisteredItem(std::string service);
27+
28+
std::vector<std::unique_ptr<Item>> items_;
29+
const std::string bus_name_;
30+
const std::string object_path_;
31+
std::size_t bus_name_id_;
32+
std::size_t watcher_id_;
33+
GCancellable* cancellable_ = nullptr;
34+
SnWatcher* watcher_ = nullptr;
35+
const Json::Value &config_;
36+
std::function<void(std::unique_ptr<Item>&)> on_add_;
37+
std::function<void(std::unique_ptr<Item>&)> on_remove_;
38+
};
39+
40+
}

include/modules/sni/sni.hpp renamed to include/modules/sni/item.hpp

+11-11
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,25 @@
55
#include <json/json.h>
66
#include <libdbusmenu-gtk/dbusmenu-gtk.h>
77
#ifdef FILESYSTEM_EXPERIMENTAL
8-
#include <experimental/filesystem>
8+
#include <experimental/filesystem>
99
#else
10-
#include <filesystem>
10+
#include <filesystem>
1111
#endif
1212

1313
namespace waybar::modules::SNI {
1414

1515
class Item {
1616
public:
17-
Item(std::string, std::string, Glib::Dispatcher*, Json::Value);
17+
Item(std::string, std::string, const Json::Value&);
18+
~Item() = default;
1819

1920
std::string bus_name;
2021
std::string object_path;
21-
Gtk::EventBox *event_box;
2222

2323
int icon_size;
2424
int effective_icon_size;
25-
Gtk::Image *image;
25+
Gtk::Image image;
26+
Gtk::EventBox event_box;
2627
std::string category;
2728
std::string id;
2829
std::string status;
@@ -35,26 +36,25 @@ class Item {
3536
std::string attention_icon_name;
3637
std::string attention_movie_name;
3738
std::string icon_theme_path;
38-
DbusmenuGtkMenu *menu = nullptr;
39+
std::string menu;
40+
DbusmenuGtkMenu *dbus_menu = nullptr;
3941
Gtk::Menu *gtk_menu = nullptr;
4042
bool item_is_menu;
4143

4244
private:
4345
static void proxyReady(GObject *obj, GAsyncResult *res, gpointer data);
4446
static void getAll(GObject *obj, GAsyncResult *res, gpointer data);
45-
static void handleActivate(GObject *, GAsyncResult *, gpointer);
46-
static void handleSecondaryActivate(GObject *, GAsyncResult *, gpointer);
4747

4848
void updateImage();
49-
bool showMenu(GdkEventButton *const &ev);
5049
Glib::RefPtr<Gdk::Pixbuf> extractPixBuf(GVariant *variant);
5150
Glib::RefPtr<Gdk::Pixbuf> getIconByName(std::string name, int size);
51+
static void onMenuDestroyed(Item *self);
52+
bool makeMenu(GdkEventButton *const &ev);
5253
bool handleClick(GdkEventButton *const & /*ev*/);
5354

54-
Glib::Dispatcher *dp_;
55+
Glib::RefPtr<Gio::DBus::Connection> conn_;
5556
GCancellable *cancellable_ = nullptr;
5657
SnItem *proxy_ = nullptr;
57-
Json::Value config_;
5858
};
5959

6060
} // namespace waybar::modules::SNI

include/modules/sni/snh.hpp

-38
This file was deleted.

include/modules/sni/tray.hpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
#include <thread>
55
#include "util/json.hpp"
66
#include "IModule.hpp"
7-
#include "modules/sni/snw.hpp"
8-
#include "modules/sni/snh.hpp"
7+
#include "modules/sni/watcher.hpp"
8+
#include "modules/sni/host.hpp"
99

1010
namespace waybar::modules::SNI {
1111

@@ -15,6 +15,9 @@ class Tray : public IModule {
1515
auto update() -> void;
1616
operator Gtk::Widget &();
1717
private:
18+
void onAdd(std::unique_ptr<Item>& item);
19+
void onRemove(std::unique_ptr<Item>& item);
20+
1821
std::thread thread_;
1922
const Json::Value& config_;
2023
Gtk::Box box_;
File renamed without changes.

meson.build

+3-3
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ if dbusmenu_gtk.found()
8383
add_project_arguments('-DHAVE_DBUSMENU', language: 'cpp')
8484
src_files += files(
8585
'src/modules/sni/tray.cpp',
86-
'src/modules/sni/snw.cpp',
87-
'src/modules/sni/snh.cpp',
88-
'src/modules/sni/sni.cpp'
86+
'src/modules/sni/watcher.cpp',
87+
'src/modules/sni/host.cpp',
88+
'src/modules/sni/item.cpp'
8989
)
9090
endif
9191

+38-69
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,47 @@
11
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
22
"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
33
<node>
4-
<interface name="org.kde.StatusNotifierItem">
4+
<interface name='org.kde.StatusNotifierItem'>
55
<annotation name="org.gtk.GDBus.C.Name" value="Item" />
6-
<property name="Category" type="s" access="read"/>
7-
<property name="Id" type="s" access="read"/>
8-
<property name="Title" type="s" access="read"/>
9-
<property name="Status" type="s" access="read"/>
10-
<property name="WindowId" type="i" access="read"/>
11-
<property name="Menu" type="o" access="read" />
12-
13-
<!-- main icon -->
14-
<!-- names are preferred over pixmaps -->
15-
<property name="IconName" type="s" access="read" />
16-
<property name="IconThemePath" type="s" access="read" />
17-
18-
<!-- struct containing width, height and image data-->
19-
<!-- implementation has been dropped as of now -->
20-
<property name="IconPixmap" type="a(iiay)" access="read" />
21-
22-
<!-- not used in ayatana code, no test case so far -->
23-
<property name="OverlayIconName" type="s" access="read"/>
24-
<property name="OverlayIconPixmap" type="a(iiay)" access="read" />
25-
26-
<!-- Requesting attention icon -->
27-
<property name="AttentionIconName" type="s" access="read"/>
28-
29-
<!--same definition as image-->
30-
<property name="AttentionIconPixmap" type="a(iiay)" access="read" />
31-
32-
<!-- tooltip data -->
33-
<!-- unimplemented as of now -->
34-
<!--(iiay) is an image-->
35-
<property name="ToolTip" type="(sa(iiay)ss)" access="read" />
36-
37-
38-
<!-- interaction: actually, we do not use them. -->
39-
<method name="Activate">
40-
<arg name="x" type="i" direction="in"/>
41-
<arg name="y" type="i" direction="in"/>
6+
<method name='ContextMenu'>
7+
<arg type='i' direction='in' name='x'/>
8+
<arg type='i' direction='in' name='y'/>
429
</method>
43-
<method name="SecondaryActivate">
44-
<arg name="x" type="i" direction="in"/>
45-
<arg name="y" type="i" direction="in"/>
10+
<method name='Activate'>
11+
<arg type='i' direction='in' name='x'/>
12+
<arg type='i' direction='in' name='y'/>
4613
</method>
47-
<method name="Scroll">
48-
<arg name="delta" type="i" direction="in"/>
49-
<arg name="dir" type="s" direction="in"/>
14+
<method name='SecondaryActivate'>
15+
<arg type='i' direction='in' name='x'/>
16+
<arg type='i' direction='in' name='y'/>
5017
</method>
51-
52-
<!-- Signals: the client wants to change something in the status-->
53-
<signal name="NewTitle"></signal>
54-
<signal name="NewIcon"></signal>
55-
<signal name="NewIconThemePath">
56-
<arg type="s" name="icon_theme_path" direction="out" />
57-
</signal>
58-
<signal name="NewAttentionIcon"></signal>
59-
<signal name="NewOverlayIcon"></signal>
60-
<signal name="NewToolTip"></signal>
61-
<signal name="NewStatus">
62-
<arg name="status" type="s" />
63-
</signal>
64-
65-
<!-- ayatana labels -->
66-
<!-- These are commented out because GDBusProxy would otherwise require them,
67-
but they are not available for KDE indicators
68-
-->
69-
<!--<signal name="XAyatanaNewLabel">
70-
<arg type="s" name="label" direction="out" />
71-
<arg type="s" name="guide" direction="out" />
18+
<method name='Scroll'>
19+
<arg type='i' direction='in' name='delta'/>
20+
<arg type='s' direction='in' name='orientation'/>
21+
</method>
22+
<signal name='NewTitle'/>
23+
<signal name='NewIcon'/>
24+
<signal name='NewAttentionIcon'/>
25+
<signal name='NewOverlayIcon'/>
26+
<signal name='NewToolTip'/>
27+
<signal name='NewStatus'>
28+
<arg type='s' name='status'/>
7229
</signal>
73-
<property name="XAyatanaLabel" type="s" access="read" />
74-
<property name="XAyatanaLabelGuide" type="s" access="read" />-->
75-
76-
77-
</interface>
30+
<property name='Category' type='s' access='read'/>
31+
<property name='Id' type='s' access='read'/>
32+
<property name='Title' type='s' access='read'/>
33+
<property name='Status' type='s' access='read'/>
34+
<property name='WindowId' type='u' access='read'/>
35+
<property name='IconThemePath' type='s' access='read'/>
36+
<property name='IconName' type='s' access='read'/>
37+
<property name='IconPixmap' type='a(iiay)' access='read'/>
38+
<property name='OverlayIconName' type='s' access='read'/>
39+
<property name='OverlayIconPixmap' type='a(iiay)' access='read'/>
40+
<property name='AttentionIconName' type='s' access='read'/>
41+
<property name='AttentionIconPixmap' type='a(iiay)' access='read'/>
42+
<property name='AttentionMovieName' type='s' access='read'/>
43+
<property name='ToolTip' type='(sa(iiay)ss)' access='read'/>
44+
<property name='Menu' type='o' access='read'/>
45+
<property name='ItemIsMenu' type='b' access='read'/>
46+
</interface>
7847
</node>

src/ALabel.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#include <iostream>
55

6-
waybar::ALabel::ALabel(const Json::Value& config, const std::string format)
6+
waybar::ALabel::ALabel(const Json::Value& config, const std::string& format)
77
: config_(config),
88
format_(config_["format"].isString() ? config_["format"].asString() : format),
99
default_format_(format_)

0 commit comments

Comments
 (0)