mirror of
https://github.com/loimu/zeit.git
synced 2025-09-15 12:58:44 +08:00
zeit: add a possibility to optionally activate a tray icon at the application startup with the CLI -t or --tray-icon parameters
This commit is contained in:
parent
81e6603d37
commit
65a6e05fc2
@ -29,6 +29,13 @@ int main(int argc, char *argv[])
|
||||
{
|
||||
if(getuid() == 0)
|
||||
qFatal("User should not be root");
|
||||
bool showTrayIcon = false;
|
||||
if(argc > 1) {
|
||||
QByteArray arg = argv[1];
|
||||
if(arg == QByteArray("-t") || arg == QByteArray("--tray-icon")) {
|
||||
showTrayIcon = true;
|
||||
}
|
||||
}
|
||||
QApplication a(argc, argv);
|
||||
QApplication::setApplicationName(QStringLiteral("Zeit"));
|
||||
QApplication::setOrganizationName(QStringLiteral("zeit"));
|
||||
@ -47,5 +54,7 @@ int main(int argc, char *argv[])
|
||||
a.installTranslator(&libTranslator);
|
||||
MainWindow w;
|
||||
w.show();
|
||||
if(showTrayIcon)
|
||||
w.showTrayIcon();
|
||||
return a.exec();
|
||||
}
|
||||
|
||||
@ -23,6 +23,7 @@
|
||||
#include <QKeyEvent>
|
||||
#include <QProcess>
|
||||
#include <QSettings>
|
||||
#include <QSystemTrayIcon>
|
||||
|
||||
#ifdef BUILD_HELPER
|
||||
#define ROOT_ACTIONS cron->isSystemCron()
|
||||
@ -164,6 +165,24 @@ void MainWindow::show() {
|
||||
refreshActions(false);
|
||||
}
|
||||
|
||||
void MainWindow::showTrayIcon() {
|
||||
trayIcon = new QSystemTrayIcon(this);
|
||||
connect(trayIcon, &QSystemTrayIcon::activated, this, [this] {
|
||||
if(isHidden())
|
||||
show();
|
||||
else
|
||||
hide();
|
||||
});
|
||||
auto trayIconMenu = new QMenu(this);
|
||||
trayIconMenu->addAction(ui->actionAlarm);
|
||||
trayIconMenu->addAction(ui->actionTimer);
|
||||
trayIconMenu->addSeparator();
|
||||
trayIconMenu->addAction(ui->actionQuit);
|
||||
trayIcon->setContextMenu(trayIconMenu);
|
||||
trayIcon->setIcon(QIcon::fromTheme(QSL("chronometer")));
|
||||
trayIcon->show();
|
||||
}
|
||||
|
||||
void MainWindow::keyPressEvent(QKeyEvent* e) {
|
||||
if(e->key() == Qt::Key_Escape && ui->filterEdit->isVisible())
|
||||
ui->actionShowFilter->trigger();
|
||||
@ -181,6 +200,7 @@ void MainWindow::closeEvent(QCloseEvent* e) {
|
||||
settings.setValue(QStringLiteral("windowState"), saveState());
|
||||
settings.setValue(QStringLiteral("General/shortenText"),
|
||||
ui->actionShortenText->isChecked());
|
||||
trayIcon->hide();
|
||||
QMainWindow::closeEvent(e);
|
||||
}
|
||||
|
||||
|
||||
@ -22,6 +22,7 @@
|
||||
|
||||
#include <QMainWindow>
|
||||
|
||||
|
||||
namespace Ui {
|
||||
class MainWindow;
|
||||
}
|
||||
@ -33,6 +34,7 @@ class CTVariable;
|
||||
class QListWidgetItem;
|
||||
class Commands;
|
||||
class BaseDelegate;
|
||||
class QSystemTrayIcon;
|
||||
|
||||
class MainWindow : public QMainWindow
|
||||
{
|
||||
@ -43,6 +45,7 @@ class MainWindow : public QMainWindow
|
||||
Ui::MainWindow* ui;
|
||||
Commands* commands;
|
||||
BaseDelegate* list = nullptr;
|
||||
QSystemTrayIcon* trayIcon = nullptr;
|
||||
void keyPressEvent(QKeyEvent*) override;
|
||||
void resizeEvent(QResizeEvent*) override;
|
||||
void closeEvent(QCloseEvent*) override;
|
||||
@ -59,6 +62,7 @@ public:
|
||||
explicit MainWindow(QWidget* parent = nullptr);
|
||||
~MainWindow();
|
||||
void show();
|
||||
void showTrayIcon();
|
||||
};
|
||||
|
||||
#endif // MAINWINDOW_H
|
||||
|
||||
Loading…
Reference in New Issue
Block a user