Skip to content

Commit de35e78

Browse files
committed
feat: 添加开机自启动功能
1 parent 0cb38aa commit de35e78

10 files changed

+88
-3
lines changed

src/AlwaysEnglish_en_US.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,14 @@ Please go to settings to install English (US) language.</oldsource>
209209
<translation type="unfinished"></translation>
210210
</message>
211211
<message>
212-
<location filename="qml/page/settings.qml" line="51"/>
212+
<location filename="qml/page/settings.qml" line="76"/>
213213
<source>Whether turn on Cap Lock when AlwaysEnglish is on</source>
214214
<translation type="unfinished"></translation>
215215
</message>
216+
<message>
217+
<location filename="qml/page/settings.qml" line="53"/>
218+
<source>Running at start</source>
219+
<translation type="unfinished"></translation>
220+
</message>
216221
</context>
217222
</TS>

src/AlwaysEnglish_zh_CN.qm

63 Bytes
Binary file not shown.

src/AlwaysEnglish_zh_CN.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<message>
2323
<location filename="qml/window/MainWindow.qml" line="12"/>
2424
<source>AlwaysEnglish</source>
25-
<translation type="unfinished">英文输入法锁定软件 – V1.0</translation>
25+
<translation type="unfinished">英文输入法锁定软件 – V1.2</translation>
2626
</message>
2727
<message>
2828
<location filename="qml/window/MainWindow.qml" line="69"/>
@@ -215,12 +215,17 @@ Please go to settings to install English (US) language.</oldsource>
215215
<source>Dark</source>
216216
<translation type="unfinished">深色</translation>
217217
</message>
218+
<message>
219+
<location filename="qml/page/settings.qml" line="53"/>
220+
<source>Running at start</source>
221+
<translation type="unfinished">开机自启动</translation>
222+
</message>
218223
<message>
219224
<source>Sticky on Top</source>
220225
<translation type="obsolete">窗口置顶</translation>
221226
</message>
222227
<message>
223-
<location filename="qml/page/settings.qml" line="51"/>
228+
<location filename="qml/page/settings.qml" line="76"/>
224229
<source>Whether turn on Cap Lock when AlwaysEnglish is on</source>
225230
<translation type="unfinished">当一直开启英文输入法时,是否一直打开大小写键</translation>
226231
</message>

src/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ if (${QT_VERSION_MAJOR} GREATER_EQUAL 6)
5858
${ALWAYSENGLISH_VERSION_RC_PATH}
5959
main.cpp
6060
IconProvider.h IconProvider.cpp
61+
utils.hpp
6162
singleton.h
6263
LnkResolver.h LnkResolver.cpp
6364
ControlInputLayout.h ControlInputLayout.cpp

src/helper/SettingsHelper.h

+9
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,15 @@ SINGLETON(SettingsHelper)
8888
return get("openCount", QVariant("")).toInt();
8989
}
9090

91+
// 开机自启动
92+
Q_INVOKABLE void saveAutoStart(bool autoStart) {
93+
save("autoStart", autoStart);
94+
}
95+
96+
Q_INVOKABLE bool getAutoStart() {
97+
return get("autoStart", QVariant(0)).toBool();
98+
}
99+
91100
private:
92101
void save(const QString &key, QVariant val);
93102

src/main.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "ControlInputLayout.h"
1010
#include "GetActiveWindowPath.h"
1111
#include "helper/SettingsHelper.h"
12+
#include "utils.hpp"
1213

1314
int main(int argc, char *argv[]) {
1415
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
@@ -34,6 +35,7 @@ int main(int argc, char *argv[]) {
3435
engine.rootContext()->setContextProperty("ControlInputLayout", ControlInputLayout::getInstance());
3536
engine.rootContext()->setContextProperty("GetActiveWindowPath", GetActiveWindowPath::getInstance());
3637
engine.rootContext()->setContextProperty("SettingsHelper", SettingsHelper::getInstance());
38+
engine.rootContext()->setContextProperty("Utils", Utils::getInstance());
3739

3840
const QUrl url(QStringLiteral("qrc:/qml/App.qml"));
3941
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,

src/qml/App.qml

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ FluLauncher {
2727
FluTheme.darkMode = SettingsHelper.getDarkMode()
2828
GlobalModel.isAlwaysCapLock = SettingsHelper.getCapLock()
2929

30+
// 是否开机自启动
31+
GlobalModel.isAutoStart = SettingsHelper.getAutoStart();
32+
3033
var openCount = SettingsHelper.getOpenCount()
3134
openCount += 1
3235
SettingsHelper.saveOpenCount(openCount)

src/qml/global/GlobalModel.qml

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ QtObject {
77
property int testInt: 0
88
property bool isAlwaysCapLock: true
99

10+
// 开机自启动
11+
property bool isAutoStart: false
12+
1013
// 使用 Set 来存储表格的软件路径
1114
property var existingFilePath: new Set()
1215

src/qml/page/settings.qml

+25
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,31 @@ FluScrollablePage {
4141
}
4242
}
4343

44+
FluFrame {
45+
Layout.fillWidth: true
46+
Layout.topMargin: 20
47+
height: 50
48+
padding: 10
49+
50+
FluCheckBox {
51+
id: _autoStart
52+
53+
text: qsTr("Running at start")
54+
anchors.top: _isAlwaysCapLock.bottom
55+
anchors.verticalCenter: parent.verticalCenter
56+
57+
onClicked: {
58+
GlobalModel.isAutoStart = checked
59+
SettingsHelper.saveAutoStart(GlobalModel.isAutoStart)
60+
Utils.setAutoStart(checked)
61+
}
62+
63+
Component.onCompleted: {
64+
_autoStart.checked = GlobalModel.isAutoStart
65+
}
66+
}
67+
}
68+
4469
FluFrame {
4570
Layout.fillWidth: true
4671
Layout.topMargin: 20

src/utils.hpp

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#pragma once
2+
3+
#include <QCoreApplication>
4+
#include <QSettings>
5+
#include "singleton.h"
6+
7+
class Utils : public QObject {
8+
Q_OBJECT
9+
10+
public:
11+
SINGLETON(Utils)
12+
13+
explicit Utils(QObject *parent = nullptr);
14+
15+
Q_INVOKABLE void setAutoStart(bool enable) {
16+
auto appName = QCoreApplication::applicationName();
17+
auto appPath = QCoreApplication::applicationFilePath();
18+
19+
appPath = appPath.replace("/", "\\");
20+
21+
QSettings settings("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat);
22+
23+
if (enable) {
24+
settings.setValue(appName, appPath);
25+
}
26+
else {
27+
settings.remove(appName);
28+
}
29+
}
30+
};
31+
32+
inline Utils::Utils(QObject* parent) : QObject(parent) {}

0 commit comments

Comments
 (0)