Skip to content

Commit

Permalink
add system tray support
Browse files Browse the repository at this point in the history
closes #15
  • Loading branch information
keshavbhatt committed May 27, 2023
1 parent 4718299 commit d42abea
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 20 deletions.
2 changes: 2 additions & 0 deletions src/glate.pro
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ SOURCES += \
settings.cpp \
share.cpp \
slider.cpp \
systemtraymanager.cpp \
translationdownloader.cpp \
utils.cpp \
waitingspinnerwidget.cpp
Expand All @@ -55,6 +56,7 @@ HEADERS += \
settings.h \
share.h \
slider.h \
systemtraymanager.h \
translationdownloader.h \
utils.h \
waitingspinnerwidget.h
Expand Down
29 changes: 9 additions & 20 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QMimeData>
#include <QTimer>

#include <QDesktopWidget>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QMediaPlaylist>
#include <QScreen>
#include <QScrollBar>
#include <QVariant>

#include <QKeyEvent>
#include <QKeySequence>
#include <QShortcut>

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);

SystemTrayManager *trayManager = new SystemTrayManager(this);

connect(trayManager, &SystemTrayManager::showWindow, this, &MainWindow::show);
connect(trayManager, &SystemTrayManager::hideWindow, this, &MainWindow::hide);

this->setWindowTitle(QApplication::applicationName() + " v" +
QApplication::applicationVersion());
this->setWindowIcon(QIcon(":/icons/app/icon-64.png"));
Expand Down Expand Up @@ -430,12 +420,11 @@ void MainWindow::translate_clicked() {
qDebug() << urlStr;

if (urlStr.isEmpty()) {
showError("Invalid Input.");
showError("Invalid Input.");
} else {
_request->get(QUrl(urlStr));
translationId = utils::generateRandomId(20);
_request->get(QUrl(urlStr));
translationId = utils::generateRandomId(20);
}

}
}

Expand Down
15 changes: 15 additions & 0 deletions src/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,27 @@

#include <QClipboard>
#include <QDebug>
#include <QDesktopWidget>
#include <QFile>
#include <QHotkey>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QKeyEvent>
#include <QKeySequence>
#include <QMainWindow>
#include <QMediaPlayer>
#include <QMediaPlaylist>
#include <QMessageBox>
#include <QMimeData>
#include <QPushButton>
#include <QScreen>
#include <QScrollBar>
#include <QSettings>
#include <QShortcut>
#include <QTextEdit>
#include <QTimer>
#include <QVariant>

#include "controlbutton.h"
#include "error.h"
Expand All @@ -22,6 +36,7 @@
#include "ui_textoptionform.h"
#include "utils.h"
#include "waitingspinnerwidget.h"
#include "systemtraymanager.h"

namespace Ui {
class MainWindow;
Expand Down
53 changes: 53 additions & 0 deletions src/systemtraymanager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include "systemtraymanager.h"

SystemTrayManager::SystemTrayManager(QObject *parent) : QObject{parent} {
if (QSystemTrayIcon::isSystemTrayAvailable()) {
trayIcon =
new QSystemTrayIcon(QIcon(":/icons/app/icon-64.png"), this->parent());

trayMenu = new QMenu(0);
showAction = new QAction("Show", this);
hideAction = new QAction("Hide", this);
quitAction = new QAction("Quit", this);

showAction->setDisabled(true);

trayMenu->addAction(showAction);
trayMenu->addAction(hideAction);
trayMenu->addSeparator();
trayMenu->addAction(quitAction);

trayIcon->setContextMenu(trayMenu);

connect(showAction, &QAction::triggered, this,
&SystemTrayManager::showMainWindow);
connect(hideAction, &QAction::triggered, this,
&SystemTrayManager::hideMainWindow);
connect(quitAction, &QAction::triggered, this,
&SystemTrayManager::quitApplication);

trayIcon->show();
} else {
QMessageBox::information(nullptr, "Information",
"Unable to find System tray.");
}
}

SystemTrayManager::~SystemTrayManager() {
if (trayIcon)
delete trayIcon;
}

void SystemTrayManager::showMainWindow() {
emit showWindow();
showAction->setDisabled(true);
hideAction->setDisabled(false);
}

void SystemTrayManager::hideMainWindow() {
emit hideWindow();
hideAction->setDisabled(true);
showAction->setDisabled(false);
}

void SystemTrayManager::quitApplication() { qApp->quit(); }
37 changes: 37 additions & 0 deletions src/systemtraymanager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#ifndef SYSTEMTRAYMANAGER_H
#define SYSTEMTRAYMANAGER_H

#include <QAction>
#include <QApplication>
#include <QMenu>
#include <QMessageBox>
#include <QObject>
#include <QSystemTrayIcon>

class SystemTrayManager : public QObject {
Q_OBJECT
public:
explicit SystemTrayManager(QObject *parent = nullptr);

~SystemTrayManager();

signals:
void showWindow();
void hideWindow();

private:
QSystemTrayIcon *trayIcon = nullptr;
QMenu *trayMenu = nullptr;
QAction *showAction = nullptr;
QAction *hideAction = nullptr;
QAction *quitAction = nullptr;

private slots:
void showMainWindow();

void hideMainWindow();

void quitApplication();
};

#endif // SYSTEMTRAYMANAGER_H

0 comments on commit d42abea

Please sign in to comment.