瀏覽代碼

UI: Allow the use of Esc key to quit settings window

Bennik2000 5 年之前
父節點
當前提交
f2f336229d
共有 3 個文件被更改,包括 54 次插入8 次删除
  1. 1 1
      UI/window-basic-interaction.hpp
  2. 50 7
      UI/window-basic-settings.cpp
  3. 3 0
      UI/window-basic-settings.hpp

+ 1 - 1
UI/window-basic-interaction.hpp

@@ -80,6 +80,6 @@ protected:
 		return filter(obj, event);
 	}
 
-private:
+public:
 	EventFilterFunc filter;
 };

+ 50 - 7
UI/window-basic-settings.cpp

@@ -55,6 +55,33 @@
 
 using namespace std;
 
+class SettingsEventFilter : public QObject {
+	QScopedPointer<OBSEventFilter> shortcutFilter;
+
+public:
+	inline SettingsEventFilter()
+		: shortcutFilter((OBSEventFilter *)CreateShortcutFilter())
+	{
+	}
+
+protected:
+	bool eventFilter(QObject *obj, QEvent *event) override
+	{
+		int key;
+
+		switch (event->type()) {
+		case QEvent::KeyPress:
+		case QEvent::KeyRelease:
+			key = static_cast<QKeyEvent *>(event)->key();
+			if (key == Qt::Key_Escape) {
+				return false;
+			}
+		}
+
+		return shortcutFilter->filter(obj, event);
+	}
+};
+
 // Used for QVariant in codec comboboxes
 namespace {
 static bool StringEquals(QString left, QString right)
@@ -656,7 +683,7 @@ OBSBasicSettings::OBSBasicSettings(QWidget *parent)
 	// Initialize libff library
 	ff_init();
 
-	installEventFilter(CreateShortcutFilter());
+	installEventFilter(new SettingsEventFilter());
 
 	LoadEncoderTypes();
 	LoadColorRanges();
@@ -3611,14 +3638,14 @@ bool OBSBasicSettings::QueryChanges()
 
 void OBSBasicSettings::closeEvent(QCloseEvent *event)
 {
-	if (Changed() && !QueryChanges())
+	if (!AskIfCanCloseSettings())
 		event->ignore();
+}
 
-	if (forceAuthReload) {
-		main->auth->Save();
-		main->auth->Load();
-		forceAuthReload = false;
-	}
+void OBSBasicSettings::reject()
+{
+	if (AskIfCanCloseSettings())
+		close();
 }
 
 void OBSBasicSettings::on_theme_activated(int idx)
@@ -3872,6 +3899,22 @@ void OBSBasicSettings::RecalcOutputResPixels(const char *resText)
 	}
 }
 
+bool OBSBasicSettings::AskIfCanCloseSettings()
+{
+	bool canCloseSettings = false;
+
+	if (!Changed() || QueryChanges())
+		canCloseSettings = true;
+
+	if (forceAuthReload) {
+		main->auth->Save();
+		main->auth->Load();
+		forceAuthReload = false;
+	}
+
+	return canCloseSettings;
+}
+
 void OBSBasicSettings::on_filenameFormatting_textEdited(const QString &text)
 {
 #ifdef __APPLE__

+ 3 - 0
UI/window-basic-settings.hpp

@@ -289,6 +289,8 @@ private:
 
 	void RecalcOutputResPixels(const char *resText);
 
+	bool AskIfCanCloseSettings();
+
 	QIcon generalIcon;
 	QIcon streamIcon;
 	QIcon outputIcon;
@@ -376,6 +378,7 @@ private slots:
 
 protected:
 	virtual void closeEvent(QCloseEvent *event);
+	void reject() override;
 
 public:
 	OBSBasicSettings(QWidget *parent);