Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide to system tray instead of quitting #151

Merged
merged 1 commit into from
Feb 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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*/)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only works with the application title bar right? Maybe some handling in the main window itself would be required to make it work with the system title bar?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is true.
In order to make it work the same way when it's closed by the system title bar, I've added some logic to MainWindow::closeEvent.
This function also catches every other method of closing like right-click->close or alt+f4, or any other thing available, and they're not distinguishable.
I thought this could be a problem; however, I've checked that other apps that use system tray work the same way, so I expect that It shouldn't be a problem. At least it's not uncommon.

Now, when you select the option to close to system tray, all these methods will result in spotify-qt staying active in tray. In order to quit, one shall right-click the tray icon and select Quit, or use Ctrl+Q inside the app, or use menu->Quit.

{
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