Skip to content

Commit

Permalink
Close to tray instead of quitting
Browse files Browse the repository at this point in the history
  • Loading branch information
wsipak committed Feb 19, 2022
1 parent 17b2cb2 commit d66dbf9
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 3 deletions.
5 changes: 5 additions & 0 deletions lib/include/lib/settings/general.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ namespace lib
* Show a notification on track change
*/
bool notify_track_change = false;

/**
* Close to system tray instead of quitting when the close button is pressed
*/
bool close_to_tray = false;
};

void to_json(nlohmann::json &j, const general &g);
Expand Down
2 changes: 2 additions & 0 deletions lib/src/settings/general.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
void lib::setting::to_json(nlohmann::json &j, const general &g)
{
j = nlohmann::json{
{"close_to_tray", g.close_to_tray},
{"custom_playlist_order", g.custom_playlist_order},
{"fallback_icons", g.fallback_icons},
{"fixed_width_time", g.fixed_width_time},
Expand Down Expand Up @@ -35,6 +36,7 @@ void lib::setting::from_json(const nlohmann::json &j, general &g)
return;
}

lib::json::get(j, "close_to_tray", g.close_to_tray);
lib::json::get(j, "custom_playlist_order", g.custom_playlist_order);
lib::json::get(j, "fallback_icons", g.fallback_icons);
lib::json::get(j, "fixed_width_time", g.fixed_width_time);
Expand Down
12 changes: 10 additions & 2 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,16 @@ MainWindow::MainWindow(lib::settings &settings, lib::paths &paths)

void MainWindow::closeEvent(QCloseEvent *event)
{
delete trayIcon;
event->accept();
if (settings.general.close_to_tray && trayIcon != nullptr)
{
event->ignore();
this->setVisible(false);
}
else
{
delete trayIcon;
event->accept();
}
}

void MainWindow::initClient()
Expand Down
10 changes: 10 additions & 0 deletions src/settingspage/interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ auto SettingsPage::Interface::trayIcon() -> QWidget *
notifyTrackChange->setChecked(settings.general.notify_track_change);
trayOptions->addWidget(notifyTrackChange);

closeToTray = new QCheckBox("Close to system tray instead of quitting", this);
closeToTray->setToolTip("The app will remain active with the icon in system tray after the close button is pressed");
closeToTray->setChecked(settings.general.close_to_tray);
trayOptions->addWidget(closeToTray);

return Widget::layoutToWidget(content, this);
}

Expand Down Expand Up @@ -310,6 +315,11 @@ void SettingsPage::Interface::saveTrayIcon()
settings.general.notify_track_change = notifyTrackChange->isChecked();
}

if (closeToTray != nullptr)
{
settings.general.close_to_tray = closeToTray->isChecked();
}

// Reload if needed
if (reloadTray)
{
Expand Down
1 change: 1 addition & 0 deletions src/settingspage/interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ namespace SettingsPage
QCheckBox *albumInTray = nullptr;
QCheckBox *invertTrayIcon = nullptr;
QCheckBox *notifyTrackChange = nullptr;
QCheckBox *closeToTray = nullptr;

// Title bar
QGroupBox *appTitleBar = nullptr;
Expand Down
15 changes: 14 additions & 1 deletion src/view/maintoolbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ MainToolBar::MainToolBar(lib::spt::api &spotify, lib::settings &settings,
close = new QAction(Icon::get("window-close"),
QStringLiteral("Close"), this);

QAction::connect(close, &QAction::triggered, &QCoreApplication::quit);
QAction::connect(close, &QAction::triggered, this, &MainToolBar::onClose);

if (settings.qt().mirror_title_bar)
{
Expand Down Expand Up @@ -360,3 +360,16 @@ void MainToolBar::onMinimize(bool /*checked*/)
auto *mainWindow = MainWindow::find(parentWidget());
mainWindow->minimize();
}

void MainToolBar::onClose(bool /*checked*/)
{
auto *mainWindow = MainWindow::find(parentWidget());
if (settings.general.close_to_tray && mainWindow->getTrayIcon() != nullptr)
{
mainWindow->setVisible(false);
}
else
{
QCoreApplication::quit();
}
}
1 change: 1 addition & 0 deletions src/view/maintoolbar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Q_OBJECT
void onShuffle(bool checked);
void onRepeat(bool checked);
void onMinimize(bool checked);
void onClose(bool checked);

lib::repeat_state repeatState = lib::repeat_state::off;

Expand Down

0 comments on commit d66dbf9

Please sign in to comment.