feat: hide to tray on closing

This commit is contained in:
antonpavanvo 2022-05-31 22:04:36 +04:00
parent 645b92ba51
commit 93e6af3e20
5 changed files with 82 additions and 20 deletions

View File

@ -20,6 +20,7 @@
#include "ui_squawk.h"
#include <QDebug>
#include <QIcon>
#include <QStyle>
Squawk::Squawk(Models::Roster& p_rosterModel, QWidget *parent) :
QMainWindow(parent),
@ -51,12 +52,13 @@ Squawk::Squawk(Models::Roster& p_rosterModel, QWidget *parent) :
m_ui->comboBox->addItem(Shared::availabilityIcon(av), Shared::Global::getName(av));
}
m_ui->comboBox->setCurrentIndex(static_cast<int>(Shared::Availability::offline));
createTrayIcon();
connect(m_ui->actionAccounts, &QAction::triggered, this, &Squawk::onAccounts);
connect(m_ui->actionPreferences, &QAction::triggered, this, &Squawk::onPreferences);
connect(m_ui->actionAddContact, &QAction::triggered, this, &Squawk::onNewContact);
connect(m_ui->actionAddConference, &QAction::triggered, this, &Squawk::onNewConference);
connect(m_ui->actionQuit, &QAction::triggered, this, &Squawk::close);
connect(m_ui->actionQuit, &QAction::triggered, [this]() { hide(); close(); }); // Actually closing
connect(m_ui->comboBox, qOverload<int>(&QComboBox::activated), this, &Squawk::onComboboxActivated);
//connect(m_ui->roster, &QTreeView::doubleClicked, this, &Squawk::onRosterItemDoubleClicked);
connect(m_ui->roster, &QTreeView::customContextMenuRequested, this, &Squawk::onRosterContextMenu);
@ -89,10 +91,35 @@ Squawk::Squawk(Models::Roster& p_rosterModel, QWidget *parent) :
settings.endGroup();
}
QSystemTrayIcon* Squawk::trayIcon;
Squawk::~Squawk() {
delete contextMenu;
delete trayIcon;
}
void Squawk::createTrayIcon()
{
QSettings settings;
trayIcon = new QSystemTrayIcon(this);
trayIcon->setIcon(QApplication::windowIcon());
QMenu * menu = new QMenu(this);
QAction * viewWindow = new QAction("Open Main Window", this);//TODO add translations
QAction * quitAction = new QAction("Quit", this);
connect(viewWindow, SIGNAL(triggered()), this, SLOT(show()));
connect(quitAction, &QAction::triggered, [this]() { hide(); close(); }); // Actually closing
menu->addAction(viewWindow);
menu->addAction(quitAction);
trayIcon->setContextMenu(menu);
if(settings.value("trayIconCheckbox").toBool())
trayIcon->show();
}
void Squawk::onAccounts()
{
if (accounts == nullptr) {
@ -183,6 +210,12 @@ void Squawk::onJoinConferenceAccepted()
void Squawk::closeEvent(QCloseEvent* event)
{
QSettings settings;
if(this->isVisible() && settings.value("trayIconCheckbox").toBool()){
event->ignore();
this->hide();
} else {
if (accounts != nullptr) {
accounts->close();
}
@ -199,10 +232,11 @@ void Squawk::closeEvent(QCloseEvent* event)
}
vCards.clear();
writeSettings();
emit closing();;
emit closing();
QMainWindow::closeEvent(event);
}
}
void Squawk::onAccountsClosed() {
accounts = nullptr;}

View File

@ -24,6 +24,7 @@
#include <QCloseEvent>
#include <QSettings>
#include <QInputDialog>
#include <QSystemTrayIcon>
#include <deque>
#include <map>
@ -85,6 +86,7 @@ signals:
public:
Models::Roster::ElId currentConversationId() const;
void closeCurrentConversation();
static QSystemTrayIcon *trayIcon;
public slots:
void writeSettings();
@ -93,6 +95,8 @@ public slots:
void select(QModelIndex index);
private:
void createTrayIcon();
QScopedPointer<Ui::Squawk> m_ui;
Accounts* accounts;

View File

@ -18,6 +18,7 @@
#include "pagegeneral.h"
#include "ui_pagegeneral.h"
#include "ui/squawk.h"
PageGeneral::PageGeneral(QWidget* parent):
QWidget(parent),
@ -28,7 +29,10 @@ PageGeneral::PageGeneral(QWidget* parent):
QSettings settings;
m_ui->downloadsPathInput->setText(settings.value("downloadsPath").toString());
m_ui->trayIconCheckbox->setChecked(settings.value("trayIconCheckbox").toBool());
connect(m_ui->downloadsPathButton, &QPushButton::clicked, this, &PageGeneral::onBrowseButtonClicked);
connect(m_ui->trayIconCheckbox, &QCheckBox::stateChanged, this, &PageGeneral::onTrayIconCheckboxChecked);
}
PageGeneral::~PageGeneral()
@ -76,3 +80,9 @@ void PageGeneral::onDialogDestroyed()
{
dialog = nullptr;
}
void PageGeneral::onTrayIconCheckboxChecked(){
QSettings settings;
settings.setValue("trayIconCheckbox", m_ui->trayIconCheckbox->isChecked());
Squawk::trayIcon->setVisible(m_ui->trayIconCheckbox->isChecked());
}

View File

@ -47,6 +47,7 @@ private slots:
void onBrowseButtonClicked();
void onDialogAccepted();
void onDialogDestroyed();
void onTrayIconCheckboxChecked();
private:
QScopedPointer<Ui::PageGeneral> m_ui;

View File

@ -39,6 +39,19 @@
</item>
</layout>
</item>
<item row="2" column="0" colspan="2">
<widget class="QCheckBox" name="trayIconCheckbox">
<property name="layoutDirection">
<enum>Qt::LeftToRight</enum>
</property>
<property name="text">
<string>Hide Squawk to tray</string>
</property>
<property name="tristate">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>