strawberry/app/tray.cpp
trvon 2e5637fe6c
Some checks failed
Build and Test / build (debug, clang) (push) Has been cancelled
Build and Test / build (debug, gcc) (push) Has been cancelled
Build and Test / build (release, clang) (push) Has been cancelled
Build and Test / build (release, gcc) (push) Has been cancelled
Build and Test / docker (push) Has been cancelled
Add "Start at login" to the tray app
A checkable tray menu item and a Diagnostics-tab checkbox write or
remove ~/.config/autostart/strawberry-tray.desktop (XDG autostart,
atomic write), pointing at the installed strawberry-tray when there is
one. Sessions that do not run autostart entries on their own (sway, i3,
Hyprland, niri without uwsm/dex) get a note with the exact line to add
to the compositor config instead of a silent no-op; GNOME, KDE, XFCE and
the like, or an active xdg-desktop-autostart.target, need nothing more.

The logic is Qt-free (app/autostart.cpp) with a unit test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 20:59:52 -06:00

125 lines
4.3 KiB
C++

#include "tray.hpp"
#include <QAction>
#include <QMenu>
#include <QSignalBlocker>
#include <QPainter>
#include <QPainterPath>
#include <QPixmap>
#include <QSystemTrayIcon>
namespace strawberry::app {
namespace {
QColor level_color(TrayLevel level) {
switch (level) {
case TrayLevel::ok: return QColor(0x2e, 0xa0, 0x43);
case TrayLevel::warning: return QColor(0xe0, 0xa1, 0x00);
case TrayLevel::alert: return QColor(0xd7, 0x26, 0x38);
case TrayLevel::unknown: break;
}
return QColor(0x8a, 0x8f, 0x98);
}
// A small strawberry: state-coloured body, dark leaves, light seeds.
QPixmap paint_icon(TrayLevel level, int size) {
QPixmap pixmap(size, size);
pixmap.fill(Qt::transparent);
QPainter p(&pixmap);
p.setRenderHint(QPainter::Antialiasing);
const qreal s = size;
QPainterPath body;
body.moveTo(0.50 * s, 0.96 * s);
body.cubicTo(0.12 * s, 0.72 * s, 0.04 * s, 0.42 * s, 0.20 * s, 0.30 * s);
body.cubicTo(0.34 * s, 0.20 * s, 0.66 * s, 0.20 * s, 0.80 * s, 0.30 * s);
body.cubicTo(0.96 * s, 0.42 * s, 0.88 * s, 0.72 * s, 0.50 * s, 0.96 * s);
p.setPen(QPen(QColor(0, 0, 0, 120), s * 0.04));
p.setBrush(level_color(level));
p.drawPath(body);
p.setPen(Qt::NoPen);
p.setBrush(QColor(0x1f, 0x5f, 0x2c));
QPainterPath leaves;
leaves.moveTo(0.50 * s, 0.30 * s);
leaves.lineTo(0.24 * s, 0.14 * s);
leaves.lineTo(0.40 * s, 0.24 * s);
leaves.lineTo(0.50 * s, 0.04 * s);
leaves.lineTo(0.60 * s, 0.24 * s);
leaves.lineTo(0.76 * s, 0.14 * s);
leaves.closeSubpath();
p.drawPath(leaves);
p.setBrush(QColor(255, 255, 255, 200));
const qreal r = s * 0.035;
for (const auto& [x, y] : {std::pair{0.36, 0.45}, {0.64, 0.45}, {0.50, 0.55}, {0.40, 0.68}, {0.60, 0.68}, {0.50, 0.80}}) {
p.drawEllipse(QPointF(x * s, y * s), r, r * 1.4);
}
return pixmap;
}
} // namespace
QIcon Tray::icon_for(TrayLevel level) {
QIcon icon;
for (const int size : {16, 22, 24, 32, 48, 64}) {
icon.addPixmap(paint_icon(level, size));
}
return icon;
}
Tray::Tray(QObject* parent) : QObject(parent) {
icon_ = new QSystemTrayIcon(icon_for(TrayLevel::unknown), this);
icon_->setToolTip(QStringLiteral("Strawberry: connecting…"));
auto* menu = new QMenu();
connect(this, &QObject::destroyed, menu, &QObject::deleteLater);
connect(menu->addAction(QStringLiteral("Open Strawberry")), &QAction::triggered,
this, &Tray::open_requested);
menu->addSeparator();
reload_action_ = menu->addAction(QStringLiteral("Reload rules"));
connect(reload_action_, &QAction::triggered, this, [this]() { emit command_requested(QStringLiteral("reload-rules")); });
rescan_action_ = menu->addAction(QStringLiteral("Rescan running processes"));
connect(rescan_action_, &QAction::triggered, this, [this]() { emit command_requested(QStringLiteral("rescan")); });
connect(menu->addAction(QStringLiteral("Mark all findings seen")), &QAction::triggered,
this, &Tray::mark_all_seen_requested);
menu->addSeparator();
autostart_action_ = menu->addAction(QStringLiteral("Start at login"));
autostart_action_->setCheckable(true);
connect(autostart_action_, &QAction::triggered, this, [this](bool checked) { emit autostart_toggled(checked); });
connect(menu->addAction(QStringLiteral("Quit")), &QAction::triggered, this, &Tray::quit_requested);
icon_->setContextMenu(menu);
connect(icon_, &QSystemTrayIcon::activated, this, [this](QSystemTrayIcon::ActivationReason reason) {
if (reason == QSystemTrayIcon::Trigger || reason == QSystemTrayIcon::DoubleClick) {
emit open_requested();
}
});
}
void Tray::show() {
icon_->show();
}
void Tray::apply(const TrayState& state) {
if (!level_applied_ || state.level != level_) {
icon_->setIcon(icon_for(state.level));
level_ = state.level;
level_applied_ = true;
}
icon_->setToolTip(QString::fromStdString(state.tooltip));
}
void Tray::set_autostart_checked(bool enabled) {
const QSignalBlocker block(autostart_action_);
autostart_action_->setChecked(enabled);
}
void Tray::set_controls_enabled(bool enabled) {
reload_action_->setEnabled(enabled);
rescan_action_->setEnabled(enabled);
}
} // namespace strawberry::app