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 "ui_squawk.h"
#include <QDebug> #include <QDebug>
#include <QIcon> #include <QIcon>
#include <QStyle>
Squawk::Squawk(Models::Roster& p_rosterModel, QWidget *parent) : Squawk::Squawk(Models::Roster& p_rosterModel, QWidget *parent) :
QMainWindow(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->addItem(Shared::availabilityIcon(av), Shared::Global::getName(av));
} }
m_ui->comboBox->setCurrentIndex(static_cast<int>(Shared::Availability::offline)); m_ui->comboBox->setCurrentIndex(static_cast<int>(Shared::Availability::offline));
createTrayIcon();
connect(m_ui->actionAccounts, &QAction::triggered, this, &Squawk::onAccounts); connect(m_ui->actionAccounts, &QAction::triggered, this, &Squawk::onAccounts);
connect(m_ui->actionPreferences, &QAction::triggered, this, &Squawk::onPreferences); connect(m_ui->actionPreferences, &QAction::triggered, this, &Squawk::onPreferences);
connect(m_ui->actionAddContact, &QAction::triggered, this, &Squawk::onNewContact); connect(m_ui->actionAddContact, &QAction::triggered, this, &Squawk::onNewContact);
connect(m_ui->actionAddConference, &QAction::triggered, this, &Squawk::onNewConference); 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->comboBox, qOverload<int>(&QComboBox::activated), this, &Squawk::onComboboxActivated);
//connect(m_ui->roster, &QTreeView::doubleClicked, this, &Squawk::onRosterItemDoubleClicked); //connect(m_ui->roster, &QTreeView::doubleClicked, this, &Squawk::onRosterItemDoubleClicked);
connect(m_ui->roster, &QTreeView::customContextMenuRequested, this, &Squawk::onRosterContextMenu); connect(m_ui->roster, &QTreeView::customContextMenuRequested, this, &Squawk::onRosterContextMenu);
@ -89,10 +91,35 @@ Squawk::Squawk(Models::Roster& p_rosterModel, QWidget *parent) :
settings.endGroup(); settings.endGroup();
} }
QSystemTrayIcon* Squawk::trayIcon;
Squawk::~Squawk() { Squawk::~Squawk() {
delete contextMenu; 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() void Squawk::onAccounts()
{ {
if (accounts == nullptr) { if (accounts == nullptr) {
@ -183,6 +210,12 @@ void Squawk::onJoinConferenceAccepted()
void Squawk::closeEvent(QCloseEvent* event) void Squawk::closeEvent(QCloseEvent* event)
{ {
QSettings settings;
if(this->isVisible() && settings.value("trayIconCheckbox").toBool()){
event->ignore();
this->hide();
} else {
if (accounts != nullptr) { if (accounts != nullptr) {
accounts->close(); accounts->close();
} }
@ -199,9 +232,10 @@ void Squawk::closeEvent(QCloseEvent* event)
} }
vCards.clear(); vCards.clear();
writeSettings(); writeSettings();
emit closing();; emit closing();
QMainWindow::closeEvent(event); QMainWindow::closeEvent(event);
}
} }
void Squawk::onAccountsClosed() { void Squawk::onAccountsClosed() {

View File

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

View File

@ -18,6 +18,7 @@
#include "pagegeneral.h" #include "pagegeneral.h"
#include "ui_pagegeneral.h" #include "ui_pagegeneral.h"
#include "ui/squawk.h"
PageGeneral::PageGeneral(QWidget* parent): PageGeneral::PageGeneral(QWidget* parent):
QWidget(parent), QWidget(parent),
@ -28,7 +29,10 @@ PageGeneral::PageGeneral(QWidget* parent):
QSettings settings; QSettings settings;
m_ui->downloadsPathInput->setText(settings.value("downloadsPath").toString()); 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->downloadsPathButton, &QPushButton::clicked, this, &PageGeneral::onBrowseButtonClicked);
connect(m_ui->trayIconCheckbox, &QCheckBox::stateChanged, this, &PageGeneral::onTrayIconCheckboxChecked);
} }
PageGeneral::~PageGeneral() PageGeneral::~PageGeneral()
@ -76,3 +80,9 @@ void PageGeneral::onDialogDestroyed()
{ {
dialog = nullptr; 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 onBrowseButtonClicked();
void onDialogAccepted(); void onDialogAccepted();
void onDialogDestroyed(); void onDialogDestroyed();
void onTrayIconCheckboxChecked();
private: private:
QScopedPointer<Ui::PageGeneral> m_ui; QScopedPointer<Ui::PageGeneral> m_ui;

View File

@ -39,6 +39,19 @@
</item> </item>
</layout> </layout>
</item> </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> </layout>
</widget> </widget>
<resources/> <resources/>