Skip to content

Linux + iPhone Continuity / iMessage / SMS

feat: tray status icon

Zack Bartelf7e730ef4ef8
diff --git a/docs/man/man1/tether-gtk.1.in b/docs/man/man1/tether-gtk.1.in
index 4987524..486afe1 100644
--- a/docs/man/man1/tether-gtk.1.in
+++ b/docs/man/man1/tether-gtk.1.in
@@ -3,6 +3,7 @@
tether-gtk \- Graphical interface for Tether
.SH SYNOPSIS
.B tether-gtk
+[\fB\-\-tray\fR]
.SH DESCRIPTION
.B tether-gtk
is a GTK-based graphical application for managing Tether connections and devices.
@@ -18,6 +19,11 @@ View clipboard sync history and status.
.PP
If the \fBtetherd\fR(8) daemon is not running, \fBtether-gtk\fR will attempt to
start it automatically.
+.SH OPTIONS
+.TP
+.B \-\-tray
+Start without showing the window. The application is reachable only from its
+tray icon until \fBOpen Tether\fR is chosen.
.SH SEE ALSO
.BR tether (1),
.BR tetherd (8)
diff --git a/scripts/generate_icons.sh b/scripts/generate_icons.sh
index 49c4c9b..08f59bb 100755
--- a/scripts/generate_icons.sh
+++ b/scripts/generate_icons.sh
@@ -132,6 +132,18 @@ mkdir -p "$GTK_ICON_DIR"
for size in 16 32 48 64 128 256 512 1024; do
mkdir -p "$GTK_ICON_DIR/${size}x${size}"
convert "$SOURCE" -resize "${size}x${size}" "$GTK_ICON_DIR/${size}x${size}/tether.png"
+ # Tray icon for "nothing is connected". Colour is the only thing readable at
+ # panel size, so the offline state is the same mark drained of it.
+ convert "$SOURCE" -resize "${size}x${size}" -colorspace Gray -brightness-contrast -10x-25 \
+ "$GTK_ICON_DIR/${size}x${size}/tether-offline.png"
+ # Tray icon for unread messages. A dot, not a count: digits are illegible at
+ # the 22px most panels render.
+ dot_x=$(awk "BEGIN{printf \"%.0f\", $size*0.74}")
+ dot_y=$(awk "BEGIN{printf \"%.0f\", $size*0.26}")
+ dot_edge=$(awk "BEGIN{printf \"%.0f\", $size*0.74+$size*0.16}")
+ convert "$GTK_ICON_DIR/${size}x${size}/tether.png" \
+ -fill "#e01b24" -stroke none -draw "circle $dot_x,$dot_y $dot_edge,$dot_y" \
+ "$GTK_ICON_DIR/${size}x${size}/tether-unread.png"
done
echo "All icons generated successfully."
diff --git a/src/gtk/CMakeLists.txt b/src/gtk/CMakeLists.txt
index 23a5ba2..03bfcaa 100644
--- a/src/gtk/CMakeLists.txt
+++ b/src/gtk/CMakeLists.txt
@@ -8,6 +8,7 @@ add_executable(tether-gtk
devices_view.cpp
messages_view.cpp
notifications_view.cpp
+ tray.cpp
)
target_include_directories(tether-gtk PRIVATE ${GTK3_INCLUDE_DIRS})
@@ -29,5 +30,7 @@ install(FILES ${CMAKE_CURRENT_BINARY_DIR}/tether-gtk.desktop
foreach(SIZE 16 32 48 64 128 256 512 1024)
install(FILES icons/${SIZE}x${SIZE}/tether.png
+ icons/${SIZE}x${SIZE}/tether-offline.png
+ icons/${SIZE}x${SIZE}/tether-unread.png
DESTINATION ${CMAKE_INSTALL_DATADIR}/icons/hicolor/${SIZE}x${SIZE}/apps)
endforeach()
diff --git a/src/gtk/daemon_client.cpp b/src/gtk/daemon_client.cpp
index c599cd2..2aa2a75 100644
--- a/src/gtk/daemon_client.cpp
+++ b/src/gtk/daemon_client.cpp
@@ -1,4 +1,5 @@
#include "daemon_client.hpp"
+#include "tray.hpp"
#include "ui_util.hpp"
#include <cerrno>
@@ -183,7 +184,10 @@ namespace tether::ui {
daemon_send({{"command", "bt_connection"}});
daemon_send({{"command", "bt_status"}});
daemon_send({{"command", "bt_list_devices"}});
+ // primes the tray unread count
+ daemon_send({{"command", "bt_list_threads"}});
set_status_main("Daemon Online");
+ tray_refresh();
return G_SOURCE_REMOVE;
}
diff --git a/src/gtk/icons/1024x1024/tether-offline.png b/src/gtk/icons/1024x1024/tether-offline.png
new file mode 100644
index 0000000..d070b4a
Binary files /dev/null and b/src/gtk/icons/1024x1024/tether-offline.png differ
diff --git a/src/gtk/icons/1024x1024/tether-unread.png b/src/gtk/icons/1024x1024/tether-unread.png
new file mode 100644
index 0000000..5db949b
Binary files /dev/null and b/src/gtk/icons/1024x1024/tether-unread.png differ
diff --git a/src/gtk/icons/128x128/tether-offline.png b/src/gtk/icons/128x128/tether-offline.png
new file mode 100644
index 0000000..8c792a9
Binary files /dev/null and b/src/gtk/icons/128x128/tether-offline.png differ
diff --git a/src/gtk/icons/128x128/tether-unread.png b/src/gtk/icons/128x128/tether-unread.png
new file mode 100644
index 0000000..7b1e462
Binary files /dev/null and b/src/gtk/icons/128x128/tether-unread.png differ
diff --git a/src/gtk/icons/16x16/tether-offline.png b/src/gtk/icons/16x16/tether-offline.png
new file mode 100644
index 0000000..1f23aab
Binary files /dev/null and b/src/gtk/icons/16x16/tether-offline.png differ
diff --git a/src/gtk/icons/16x16/tether-unread.png b/src/gtk/icons/16x16/tether-unread.png
new file mode 100644
index 0000000..6e4bcf3
Binary files /dev/null and b/src/gtk/icons/16x16/tether-unread.png differ
diff --git a/src/gtk/icons/256x256/tether-offline.png b/src/gtk/icons/256x256/tether-offline.png
new file mode 100644
index 0000000..eaabfe9
Binary files /dev/null and b/src/gtk/icons/256x256/tether-offline.png differ
diff --git a/src/gtk/icons/256x256/tether-unread.png b/src/gtk/icons/256x256/tether-unread.png
new file mode 100644
index 0000000..5ba4fc0
Binary files /dev/null and b/src/gtk/icons/256x256/tether-unread.png differ
diff --git a/src/gtk/icons/32x32/tether-offline.png b/src/gtk/icons/32x32/tether-offline.png
new file mode 100644
index 0000000..beb6ef1
Binary files /dev/null and b/src/gtk/icons/32x32/tether-offline.png differ
diff --git a/src/gtk/icons/32x32/tether-unread.png b/src/gtk/icons/32x32/tether-unread.png
new file mode 100644
index 0000000..e6d2801
Binary files /dev/null and b/src/gtk/icons/32x32/tether-unread.png differ
diff --git a/src/gtk/icons/48x48/tether-offline.png b/src/gtk/icons/48x48/tether-offline.png
new file mode 100644
index 0000000..a9759c8
Binary files /dev/null and b/src/gtk/icons/48x48/tether-offline.png differ
diff --git a/src/gtk/icons/48x48/tether-unread.png b/src/gtk/icons/48x48/tether-unread.png
new file mode 100644
index 0000000..f5ac3d7
Binary files /dev/null and b/src/gtk/icons/48x48/tether-unread.png differ
diff --git a/src/gtk/icons/512x512/tether-offline.png b/src/gtk/icons/512x512/tether-offline.png
new file mode 100644
index 0000000..cd4f971
Binary files /dev/null and b/src/gtk/icons/512x512/tether-offline.png differ
diff --git a/src/gtk/icons/512x512/tether-unread.png b/src/gtk/icons/512x512/tether-unread.png
new file mode 100644
index 0000000..502c13b
Binary files /dev/null and b/src/gtk/icons/512x512/tether-unread.png differ
diff --git a/src/gtk/icons/64x64/tether-offline.png b/src/gtk/icons/64x64/tether-offline.png
new file mode 100644
index 0000000..295d3b4
Binary files /dev/null and b/src/gtk/icons/64x64/tether-offline.png differ
diff --git a/src/gtk/icons/64x64/tether-unread.png b/src/gtk/icons/64x64/tether-unread.png
new file mode 100644
index 0000000..d368637
Binary files /dev/null and b/src/gtk/icons/64x64/tether-unread.png differ
diff --git a/src/gtk/main.cpp b/src/gtk/main.cpp
index 4e2da90..3ef99ae 100644
--- a/src/gtk/main.cpp
+++ b/src/gtk/main.cpp
@@ -2,6 +2,7 @@
#include "devices_view.hpp"
#include "messages_view.hpp"
#include "notifications_view.hpp"
+#include "tray.hpp"
#include "ui_util.hpp"
#include <gtk/gtk.h>
@@ -12,6 +13,7 @@ namespace {
using namespace tether::ui;
GtkWidget* g_refresh_button = nullptr;
+ gboolean g_start_hidden = FALSE;
void on_visible_view_changed(GObject* stack, GParamSpec*, gpointer) {
const gchar* name = gtk_stack_get_visible_child_name(GTK_STACK(stack));
@@ -26,7 +28,52 @@ namespace {
notifications_view_set_visible(view == "notifications");
}
+ // The tray controls live here
+ GtkWidget* create_app_menu_button() {
+ GtkWidget* menu = gtk_menu_new();
+
+ GtkWidget* close_to_tray = gtk_check_menu_item_new_with_label("Keep running in tray when closed");
+ gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(close_to_tray), tray_close_to_tray());
+ g_signal_connect(close_to_tray,
+ "toggled",
+ G_CALLBACK(+[](GtkCheckMenuItem* item, gpointer) {
+ tray_set_close_to_tray(gtk_check_menu_item_get_active(item));
+ }),
+ nullptr);
+ gtk_menu_shell_append(GTK_MENU_SHELL(menu), close_to_tray);
+
+ GtkWidget* quit = gtk_menu_item_new_with_label("Quit");
+ g_signal_connect(quit,
+ "activate",
+ G_CALLBACK(+[](GtkMenuItem*, gpointer) {
+ if (GApplication* app = g_application_get_default())
+ g_application_quit(app);
+ }),
+ nullptr);
+ gtk_menu_shell_append(GTK_MENU_SHELL(menu), quit);
+ gtk_widget_show_all(menu);
+
+ GtkWidget* button = gtk_menu_button_new();
+ gtk_button_set_image(GTK_BUTTON(button),
+ gtk_image_new_from_icon_name("open-menu-symbolic", GTK_ICON_SIZE_BUTTON));
+ gtk_menu_button_set_popup(GTK_MENU_BUTTON(button), menu);
+ return button;
+ }
+
+ // Hiding leaves the window alive but unmapped, and a second launch re-activates this instance
+ gboolean on_window_delete(GtkWidget* window, GdkEvent*, gpointer) {
+ if (!tray_close_to_tray())
+ return FALSE;
+ gtk_widget_hide(window);
+ return TRUE;
+ }
+
void activate(GtkApplication* app, gpointer) {
+ if (GtkWidget* existing = main_window()) {
+ gtk_window_present(GTK_WINDOW(existing));
+ return;
+ }
+
tether::Crypto::instance().init();
install_style();
@@ -34,6 +81,8 @@ namespace {
gtk_window_set_title(GTK_WINDOW(window), "Tether");
gtk_window_set_default_size(GTK_WINDOW(window), 820, 560);
set_main_window(window);
+ g_signal_connect(window, "delete-event", G_CALLBACK(on_window_delete), nullptr);
+ tray_init();
GtkWidget* header_bar = gtk_header_bar_new();
gtk_header_bar_set_show_close_button(GTK_HEADER_BAR(header_bar), TRUE);
@@ -41,6 +90,8 @@ namespace {
gtk_window_set_titlebar(GTK_WINDOW(window), header_bar);
set_header_bar(header_bar);
+ gtk_header_bar_pack_end(GTK_HEADER_BAR(header_bar), create_app_menu_button());
+
g_refresh_button = gtk_button_new_from_icon_name("view-refresh-symbolic", GTK_ICON_SIZE_BUTTON);
g_signal_connect(g_refresh_button,
"clicked",
@@ -83,7 +134,8 @@ namespace {
devices_view_trigger_discovery();
devices_view_refresh();
- gtk_widget_show_all(window);
+ if (!g_start_hidden)
+ gtk_widget_show_all(window);
gtk_stack_set_visible_child_name(GTK_STACK(stack), "devices");
}
@@ -91,6 +143,21 @@ namespace {
int main(int argc, char** argv) {
GtkApplication* app = gtk_application_new("com.tether.desktop", G_APPLICATION_DEFAULT_FLAGS);
+ g_application_add_main_option(G_APPLICATION(app),
+ "tray",
+ 0,
+ G_OPTION_FLAG_NONE,
+ G_OPTION_ARG_NONE,
+ "Start hidden in the system tray",
+ nullptr);
+ g_signal_connect(app,
+ "handle-local-options",
+ G_CALLBACK(+[](GApplication*, GVariantDict* options, gpointer) -> gint {
+ if (g_variant_dict_contains(options, "tray"))
+ g_start_hidden = TRUE;
+ return -1;
+ }),
+ nullptr);
g_signal_connect(app, "activate", G_CALLBACK(activate), nullptr);
int status = g_application_run(G_APPLICATION(app), argc, argv);
tether::ui::daemon_client_stop();
diff --git a/src/gtk/messages_view.cpp b/src/gtk/messages_view.cpp
index f9da57e..7831ca0 100644
--- a/src/gtk/messages_view.cpp
+++ b/src/gtk/messages_view.cpp
@@ -1,5 +1,7 @@
#include "messages_view.hpp"
+
#include "daemon_client.hpp"
+#include "tray.hpp"
#include "ui_util.hpp"
#include <cstring>
@@ -736,6 +738,11 @@ namespace tether::ui {
bool messages_view_handle_event(const nlohmann::json& event) {
const std::string command = event.value("command", "");
if (command == "bt_threads") {
+ int unread = 0;
+ if (event.contains("threads") && event["threads"].is_array())
+ for (const auto& thread : event["threads"])
+ unread += thread.value("unread", 0);
+ tray_set_unread(unread);
show_threads(event);
return true;
}
@@ -748,13 +755,10 @@ namespace tether::ui {
return false;
}
if (command == "bt_message") {
- // A new message changes both the thread list and, when it lands in
- // the open conversation, the conversation itself. Refreshing while
- // the view is hidden would just be traffic nobody sees; switching
- // back re-reads everything anyway.
+ // thread list is re-read even while hidden for tray unread count
+ request_threads();
if (!g_messages.visible)
return true;
- request_threads();
if (event.value("thread", "") == g_messages.selected_thread)
request_messages(g_messages.selected_thread);
return true;
diff --git a/src/gtk/tray.cpp b/src/gtk/tray.cpp
new file mode 100644
index 0000000..d313402
--- /dev/null
+++ b/src/gtk/tray.cpp
@@ -0,0 +1,298 @@
+#include "tray.hpp"
+#include "daemon_client.hpp"
+
+#include <filesystem>
+#include <fstream>
+#include <gio/gio.h>
+#include <nlohmann/json.hpp>
+#include <tether/log.hpp>
+#include <unistd.h>
+
+namespace tether::ui {
+
+ namespace {
+
+ // StatusNotifierItem: the tray protocol, spoken directly over D-Bus.
+ constexpr const char* SNI_INTERFACE = "org.kde.StatusNotifierItem";
+ constexpr const char* SNI_PATH = "/StatusNotifierItem";
+ constexpr const char* WATCHER_NAME = "org.kde.StatusNotifierWatcher";
+ constexpr const char* WATCHER_PATH = "/StatusNotifierWatcher";
+
+ constexpr const char* INTROSPECTION = R"XML(
+<node>
+ <interface name="org.kde.StatusNotifierItem">
+ <property name="Category" type="s" access="read"/>
+ <property name="Id" type="s" access="read"/>
+ <property name="Title" type="s" access="read"/>
+ <property name="Status" type="s" access="read"/>
+ <property name="IconName" type="s" access="read"/>
+ <property name="ItemIsMenu" type="b" access="read"/>
+ <property name="ToolTip" type="(sa(iiay)ss)" access="read"/>
+ <method name="Activate">
+ <arg name="x" type="i" direction="in"/>
+ <arg name="y" type="i" direction="in"/>
+ </method>
+ <method name="SecondaryActivate">
+ <arg name="x" type="i" direction="in"/>
+ <arg name="y" type="i" direction="in"/>
+ </method>
+ <method name="ContextMenu">
+ <arg name="x" type="i" direction="in"/>
+ <arg name="y" type="i" direction="in"/>
+ </method>
+ <method name="Scroll">
+ <arg name="delta" type="i" direction="in"/>
+ <arg name="orientation" type="s" direction="in"/>
+ </method>
+ <signal name="NewIcon"/>
+ <signal name="NewToolTip"/>
+ <signal name="NewStatus"><arg name="status" type="s"/></signal>
+ </interface>
+</node>
+)XML";
+
+ struct RouteState {
+ bool ok = false;
+ std::string detail;
+ };
+
+ GDBusConnection* g_bus = nullptr;
+ GDBusNodeInfo* g_node = nullptr;
+ std::string g_bus_name;
+ guint g_own_id = 0;
+ guint g_watch_id = 0;
+ bool g_exported = false;
+
+ RouteState g_state[2];
+ std::string g_icon = "tether-offline";
+ int g_unread = 0;
+ std::string g_tooltip;
+ bool g_close_to_tray = false;
+
+ RouteState& state(Route route) { return g_state[route == Route::WiFi ? 0 : 1]; }
+ const char* route_name(Route route) { return route == Route::WiFi ? "Wi-Fi" : "Bluetooth"; }
+
+ std::string prefs_path() {
+ const char* home = g_get_home_dir();
+ if (!home || !*home)
+ return {};
+ return (std::filesystem::path(home) / ".config" / "tether" / "gtk.json").string();
+ }
+
+ void load_prefs() {
+ const std::string path = prefs_path();
+ if (path.empty())
+ return;
+ try {
+ std::ifstream in(path);
+ if (!in)
+ return;
+ g_close_to_tray = nlohmann::json::parse(in).value("close_to_tray", false);
+ } catch (const std::exception& e) {
+ // unreadable file means defaults, not a dead tray.
+ debug::log(WARN, "tray: ignoring {} ({})", path, e.what());
+ }
+ }
+
+ void save_prefs() {
+ const std::string path = prefs_path();
+ if (path.empty())
+ return;
+ try {
+ std::error_code ec;
+ std::filesystem::create_directories(std::filesystem::path(path).parent_path(), ec);
+ std::ofstream out(path);
+ out << nlohmann::json{{"close_to_tray", g_close_to_tray}}.dump(2) << "\n";
+ } catch (const std::exception& e) {
+ debug::log(ERR, "tray: could not write {} ({})", path, e.what());
+ }
+ }
+
+ void present_window() {
+ if (GtkWidget* window = main_window())
+ gtk_window_present(GTK_WINDOW(window));
+ }
+
+ // The only hide affordance on a desktop with no window buttons. Keyed on
+ // visibility, not focus: clicking the icon takes focus off the window.
+ void toggle_window() {
+ GtkWidget* window = main_window();
+ if (!window)
+ return;
+ if (gtk_widget_get_visible(window))
+ gtk_widget_hide(window);
+ else
+ gtk_window_present(GTK_WINDOW(window));
+ }
+
+ void emit(const char* signal) {
+ if (!g_exported)
+ return;
+ g_dbus_connection_emit_signal(g_bus, nullptr, SNI_PATH, SNI_INTERFACE, signal, nullptr, nullptr);
+ }
+
+ GVariant* build_tooltip() {
+ GVariantBuilder pixmaps;
+ g_variant_builder_init(&pixmaps, G_VARIANT_TYPE("a(iiay)"));
+ return g_variant_new("(sa(iiay)ss)", "", &pixmaps, "Tether", g_tooltip.c_str());
+ }
+
+ GVariant* get_property(
+ GDBusConnection*, const gchar*, const gchar*, const gchar*, const gchar* property, GError**, gpointer) {
+ const std::string name = property;
+ if (name == "Category")
+ return g_variant_new_string("ApplicationStatus");
+ if (name == "Id")
+ return g_variant_new_string("tether");
+ if (name == "Title")
+ return g_variant_new_string("Tether");
+ if (name == "Status")
+ return g_variant_new_string("Active");
+ if (name == "IconName")
+ return g_variant_new_string(g_icon.c_str());
+ if (name == "ItemIsMenu")
+ return g_variant_new_boolean(FALSE);
+ if (name == "ToolTip")
+ return build_tooltip();
+ return nullptr;
+ }
+
+ void on_method(GDBusConnection*,
+ const gchar*,
+ const gchar*,
+ const gchar*,
+ const gchar* method,
+ GVariant*,
+ GDBusMethodInvocation* invocation,
+ gpointer) {
+ const std::string name = method;
+ if (name == "Activate" || name == "SecondaryActivate")
+ toggle_window();
+ else if (name == "ContextMenu")
+ present_window();
+ g_dbus_method_invocation_return_value(invocation, nullptr);
+ }
+
+ constexpr GDBusInterfaceVTable VTABLE = {on_method, get_property, nullptr, {nullptr}};
+
+ void register_with_watcher() {
+ if (!g_exported)
+ return;
+ g_dbus_connection_call(g_bus,
+ WATCHER_NAME,
+ WATCHER_PATH,
+ WATCHER_NAME,
+ "RegisterStatusNotifierItem",
+ g_variant_new("(s)", g_bus_name.c_str()),
+ nullptr,
+ G_DBUS_CALL_FLAGS_NONE,
+ -1,
+ nullptr,
+ nullptr,
+ nullptr);
+ }
+
+ void on_name_acquired(GDBusConnection* connection, const gchar*, gpointer) {
+ if (g_exported)
+ return;
+ GError* error = nullptr;
+ if (g_dbus_connection_register_object(
+ connection, SNI_PATH, g_node->interfaces[0], &VTABLE, nullptr, nullptr, &error) == 0) {
+ debug::log(WARN, "tray: could not export the item ({})", error ? error->message : "unknown");
+ g_clear_error(&error);
+ return;
+ }
+ g_exported = true;
+ register_with_watcher();
+ }
+
+ } // namespace
+
+ void tray_init() {
+ if (g_own_id != 0)
+ return;
+
+ load_prefs();
+
+ GError* error = nullptr;
+ g_bus = g_bus_get_sync(G_BUS_TYPE_SESSION, nullptr, &error);
+ if (!g_bus) {
+ debug::log(WARN, "tray: no session bus ({})", error ? error->message : "unknown");
+ g_clear_error(&error);
+ return;
+ }
+
+ g_node = g_dbus_node_info_new_for_xml(INTROSPECTION, &error);
+ if (!g_node) {
+ debug::log(ERR, "tray: bad introspection data ({})", error ? error->message : "unknown");
+ g_clear_error(&error);
+ return;
+ }
+
+ // Keyed on the process, per spec, so two instances cannot collide.
+ g_bus_name = "org.kde.StatusNotifierItem-" + std::to_string(getpid()) + "-1";
+ g_own_id = g_bus_own_name_on_connection(
+ g_bus, g_bus_name.c_str(), G_BUS_NAME_OWNER_FLAGS_NONE, on_name_acquired, nullptr, nullptr, nullptr);
+
+ // Re-registering on reappearance is what survives a panel restart.
+ g_watch_id = g_bus_watch_name_on_connection(
+ g_bus,
+ WATCHER_NAME,
+ G_BUS_NAME_WATCHER_FLAGS_NONE,
+ +[](GDBusConnection*, const gchar*, const gchar*, gpointer) { register_with_watcher(); },
+ nullptr,
+ nullptr,
+ nullptr);
+
+ tray_refresh();
+ }
+
+ void tray_set_route(Route route, bool ok, const std::string& detail) {
+ state(route) = {ok, detail};
+ tray_refresh();
+ }
+
+ void tray_refresh() {
+ std::string tooltip;
+ for (Route route : {Route::WiFi, Route::Bluetooth}) {
+ const RouteState& r = state(route);
+ tooltip += std::string(route_name(route)) + ": " +
+ (r.ok ? "connected" : (r.detail.empty() ? "not connected" : r.detail)) + "\n";
+ }
+ if (!daemon_connected())
+ tooltip += "Daemon: not running\n";
+ if (g_unread > 0)
+ tooltip += std::to_string(g_unread) + (g_unread == 1 ? " unread message\n" : " unread messages\n");
+ if (!tooltip.empty())
+ tooltip.pop_back();
+
+ const bool any_up = g_state[0].ok || g_state[1].ok;
+ const std::string icon = !any_up ? "tether-offline" : (g_unread > 0 ? "tether-unread" : "tether");
+
+ if (icon != g_icon) {
+ g_icon = icon;
+ emit("NewIcon");
+ }
+ if (tooltip != g_tooltip) {
+ g_tooltip = tooltip;
+ emit("NewToolTip");
+ }
+ }
+
+ void tray_set_unread(int count) {
+ if (count == g_unread)
+ return;
+ g_unread = count;
+ tray_refresh();
+ }
+
+ bool tray_close_to_tray() { return g_close_to_tray; }
+
+ void tray_set_close_to_tray(bool enabled) {
+ if (g_close_to_tray == enabled)
+ return;
+ g_close_to_tray = enabled;
+ save_prefs();
+ }
+
+} // namespace tether::ui
diff --git a/src/gtk/tray.hpp b/src/gtk/tray.hpp
new file mode 100644
index 0000000..d534a1c
--- /dev/null
+++ b/src/gtk/tray.hpp
@@ -0,0 +1,25 @@
+#pragma once
+
+#include "ui_util.hpp"
+
+#include <string>
+
+namespace tether::ui {
+
+ // Publishes a StatusNotifierItem. A desktop with no tray never claims it.
+ void tray_init();
+
+ // Mirrors set_route_status; the tooltip is rebuilt from both routes at once.
+ void tray_set_route(Route route, bool ok, const std::string& detail);
+
+ // Total unread across threads. Drives the badged icon and a tooltip line.
+ void tray_set_unread(int count);
+
+ // Repaints from cached state, for when the daemon returns before any route event.
+ void tray_refresh();
+
+ // False means the close button quits, as it always has.
+ bool tray_close_to_tray();
+ void tray_set_close_to_tray(bool enabled);
+
+} // namespace tether::ui
diff --git a/src/gtk/ui_util.cpp b/src/gtk/ui_util.cpp
index 31b8b85..06e05f9 100644
--- a/src/gtk/ui_util.cpp
+++ b/src/gtk/ui_util.cpp
@@ -1,5 +1,7 @@
#include "ui_util.hpp"
+#include "tray.hpp"
+
namespace tether::ui {
namespace {
@@ -183,6 +185,8 @@ namespace tether::ui {
// off the strip, so it lives in the tooltip. The Devices page shows it
// in full.
gtk_widget_set_tooltip_text(r.box, detail.empty() ? state.c_str() : (state + "\n" + detail).c_str());
+
+ tray_set_route(route, ok, detail);
}
} // namespace tether::ui