Skip to content

Commit ab51db2

Browse files
committed
Allow to keep running Syncthing in background when closing app
1 parent 8c2ad19 commit ab51db2

File tree

4 files changed

+45
-4
lines changed

4 files changed

+45
-4
lines changed

tray/android/src/io/github/martchus/syncthingtray/Activity.java

+9
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ public boolean performHapticFeedback() {
3232
return res;
3333
}
3434

35+
public boolean minimize() {
36+
runOnUiThread(new Runnable() {
37+
public void run() {
38+
moveTaskToBack(true);
39+
}
40+
});
41+
return true;
42+
}
43+
3544
public boolean showToast(String message) {
3645
Activity activity = this;
3746
runOnUiThread(new Runnable() {

tray/gui/qml/Main.qml

+13-2
Original file line numberDiff line numberDiff line change
@@ -440,19 +440,30 @@ ApplicationWindow {
440440
}
441441
}
442442

443-
// avoid closing app (TODO: allow to keep Syncthing running in the background)
443+
// handle closing
444444
CustomDialog {
445445
id: closeDialog
446446
title: window.title
447447
contentItem: Label {
448448
Layout.fillWidth: true
449-
text: qsTr("Do you really want to close Syncthing?")
449+
text: qsTr("Do you really want to close Syncthing? You can also just quit the UI but keep running Syncthing in the backround.")
450450
wrapMode: Text.WordWrap
451451
}
452452
onAccepted: {
453453
window.forceClose = true;
454454
window.close();
455455
}
456+
footer: DialogButtonBox {
457+
Button {
458+
text: qsTr("Run in background")
459+
flat: true
460+
onClicked: {
461+
closeDialog.close();
462+
App.minimize();
463+
}
464+
DialogButtonBox.buttonRole: DialogButtonBox.InvalidRole
465+
}
466+
}
456467
}
457468
onClosing: (event) => {
458469
if (!window.forceClose && App.launcher.running) {

tray/gui/quick/app.cpp

+21-2
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ App::App(bool insecure, QObject *parent)
115115
, m_darkColorScheme(false)
116116
, m_darkPalette(QtUtilities::isPaletteDark())
117117
, m_isGuiLoaded(false)
118+
, m_alwaysUnloadGuiWhenHidden(false)
118119
, m_unloadGuiWhenHidden(false)
119120
{
120121
m_app->installEventFilter(this);
@@ -704,7 +705,8 @@ void App::handleStateChanged(Qt::ApplicationState state)
704705
for (auto *const uiObject : m_uiObjects) {
705706
uiObject->moveToThread(nullptr);
706707
}
707-
if (m_unloadGuiWhenHidden) {
708+
if (m_unloadGuiWhenHidden || m_alwaysUnloadGuiWhenHidden) {
709+
m_unloadGuiWhenHidden = false;
708710
unloadMain();
709711
}
710712
} else if (state & Qt::ApplicationActive) {
@@ -968,6 +970,23 @@ QVariantList App::computeDirsNeedingItems(const QModelIndex &devProxyModelIndex)
968970
return dirs;
969971
}
970972

973+
bool App::minimize()
974+
{
975+
#ifdef Q_OS_ANDROID
976+
if (!QJniObject(QNativeInterface::QAndroidApplication::context()).callMethod<jboolean>("minimize", "()Z")) {
977+
emit showError(tr("Unable to minimize app."));
978+
return false;
979+
}
980+
m_unloadGuiWhenHidden = true;
981+
#else
982+
const auto windows = QGuiApplication::topLevelWindows();
983+
for (auto *const window : windows) {
984+
window->setWindowState(Qt::WindowMinimized);
985+
}
986+
#endif
987+
return true;
988+
}
989+
971990
bool App::openSettings()
972991
{
973992
if (!m_settingsDir.has_value()) {
@@ -1060,7 +1079,7 @@ bool App::applySettings()
10601079
m_connection.reconnect();
10611080
}
10621081
auto tweaksSettings = m_settings.value(QLatin1String("tweaks")).toObject();
1063-
m_unloadGuiWhenHidden = tweaksSettings.value(QLatin1String("unloadGuiWhenHidden")).toBool(false);
1082+
m_alwaysUnloadGuiWhenHidden = tweaksSettings.value(QLatin1String("unloadGuiWhenHidden")).toBool(false);
10641083
applyLauncherSettings();
10651084
invalidateStatus();
10661085
return true;

tray/gui/quick/app.h

+2
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ class App : public QObject {
246246
Q_INVOKABLE QString deviceDisplayName(const QString &id) const;
247247
Q_INVOKABLE QString dirDisplayName(const QString &id) const;
248248
Q_INVOKABLE QVariantList computeDirsNeedingItems(const QModelIndex &devProxyModelIndex) const;
249+
Q_INVOKABLE bool minimize();
249250

250251
Q_SIGNALS:
251252
void darkmodeEnabledChanged(bool darkmodeEnabled);
@@ -340,6 +341,7 @@ private Q_SLOTS:
340341
bool m_darkColorScheme;
341342
bool m_darkPalette;
342343
bool m_isGuiLoaded;
344+
bool m_alwaysUnloadGuiWhenHidden;
343345
bool m_unloadGuiWhenHidden;
344346
};
345347

0 commit comments

Comments
 (0)