瀏覽代碼

Merge pull request #1949 from jb-alvarado/master

UI: Add ability to disable hotkeys when not in focus
Jim 6 年之前
父節點
當前提交
b59742d49a
共有 5 個文件被更改,包括 103 次插入41 次删除
  1. 3 0
      UI/data/locale/en-US.ini
  2. 20 15
      UI/forms/OBSBasicSettings.ui
  3. 50 14
      UI/obs-app.cpp
  4. 8 1
      UI/obs-app.hpp
  5. 22 11
      UI/window-basic-settings.cpp

+ 3 - 0
UI/data/locale/en-US.ini

@@ -844,7 +844,10 @@ Basic.Settings.Advanced.Network="Network"
 Basic.Settings.Advanced.Network.BindToIP="Bind to IP"
 Basic.Settings.Advanced.Network.EnableNewSocketLoop="Enable new networking code"
 Basic.Settings.Advanced.Network.EnableLowLatencyMode="Low latency mode"
+Basic.Settings.Advanced.Hotkeys.HotkeyFocusBehavior="Hotkey Focus Behavior"
+Basic.Settings.Advanced.Hotkeys.NeverDisableHotkeys="Never disable hotkeys"
 Basic.Settings.Advanced.Hotkeys.DisableHotkeysInFocus="Disable hotkeys when main window is in focus"
+Basic.Settings.Advanced.Hotkeys.DisableHotkeysOutOfFocus="Disable hotkeys when main window is not in focus"
 Basic.Settings.Advanced.AutoRemux="Automatically remux to mp4"
 Basic.Settings.Advanced.AutoRemux.MP4="(record as mkv)"
 

+ 20 - 15
UI/forms/OBSBasicSettings.ui

@@ -151,8 +151,8 @@
              <rect>
               <x>0</x>
               <y>0</y>
-              <width>806</width>
-              <height>1225</height>
+              <width>803</width>
+              <height>954</height>
              </rect>
             </property>
             <layout class="QVBoxLayout" name="verticalLayout_19">
@@ -1180,8 +1180,8 @@
              <rect>
               <x>0</x>
               <y>0</y>
-              <width>813</width>
-              <height>761</height>
+              <width>601</width>
+              <height>631</height>
              </rect>
             </property>
             <layout class="QVBoxLayout" name="verticalLayout_21">
@@ -3628,8 +3628,8 @@
              <rect>
               <x>0</x>
               <y>0</y>
-              <width>767</width>
-              <height>582</height>
+              <width>555</width>
+              <height>469</height>
              </rect>
             </property>
             <layout class="QVBoxLayout" name="verticalLayout_50">
@@ -4443,9 +4443,9 @@
             <property name="geometry">
              <rect>
               <x>0</x>
-              <y>0</y>
-              <width>806</width>
-              <height>933</height>
+              <y>-114</y>
+              <width>803</width>
+              <height>761</height>
              </rect>
             </property>
             <layout class="QVBoxLayout" name="verticalLayout_23">
@@ -5143,17 +5143,23 @@
                    <string>Basic.Settings.Hotkeys</string>
                   </property>
                   <layout class="QFormLayout" name="formLayout_33">
+                   <property name="labelAlignment">
+                    <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
+                   </property>
                    <property name="topMargin">
                     <number>2</number>
                    </property>
-                   <item row="0" column="1">
-                    <widget class="QCheckBox" name="disableFocusHotkeys">
+                   <item row="0" column="0">
+                    <widget class="QLabel" name="label_21">
                      <property name="text">
-                      <string>Basic.Settings.Advanced.Hotkeys.DisableHotkeysInFocus</string>
+                      <string>Basic.Settings.Advanced.Hotkeys.HotkeyFocusBehavior</string>
                      </property>
                     </widget>
                    </item>
-                   <item row="0" column="0">
+                   <item row="0" column="1">
+                    <widget class="QComboBox" name="hotkeyFocusType"/>
+                   </item>
+                   <item row="1" column="0">
                     <spacer name="horizontalSpacer_14">
                      <property name="orientation">
                       <enum>Qt::Horizontal</enum>
@@ -5161,7 +5167,7 @@
                      <property name="sizeHint" stdset="0">
                       <size>
                        <width>170</width>
-                       <height>20</height>
+                       <height>0</height>
                       </size>
                      </property>
                     </spacer>
@@ -5407,7 +5413,6 @@
   <tabstop>enableNewSocketLoop</tabstop>
   <tabstop>enableLowLatencyMode</tabstop>
   <tabstop>browserHWAccel</tabstop>
-  <tabstop>disableFocusHotkeys</tabstop>
  </tabstops>
  <resources>
   <include location="obs.qrc"/>

+ 50 - 14
UI/obs-app.cpp

@@ -93,6 +93,9 @@ QObject *CreateShortcutFilter()
 {
 	return new OBSEventFilter([](QObject *obj, QEvent *event) {
 		auto mouse_event = [](QMouseEvent &event) {
+			if (!App()->HotkeysEnabledInFocus())
+				return true;
+
 			obs_key_combination_t hotkey = {0, OBS_KEY_NONE};
 			bool pressed = event.type() == QEvent::MouseButtonPress;
 
@@ -147,6 +150,9 @@ QObject *CreateShortcutFilter()
 		};
 
 		auto key_event = [&](QKeyEvent *event) {
+			if (!App()->HotkeysEnabledInFocus())
+				return true;
+
 			QDialog *dialog = qobject_cast<QDialog *>(obj);
 
 			obs_key_combination_t hotkey = {0, OBS_KEY_NONE};
@@ -426,6 +432,9 @@ bool OBSApp::InitGlobalConfigDefaults()
 					  "CurrentTheme", DEFAULT_THEME);
 	}
 
+	config_set_default_string(globalConfig, "General", "HotkeyFocusType",
+				  "NeverDisableHotkeys");
+
 	config_set_default_bool(globalConfig, "BasicWindow",
 				"VerticalVolControl", false);
 
@@ -693,9 +702,10 @@ bool OBSApp::InitGlobalConfig()
 		}
 	}
 
+	uint32_t lastVersion =
+		config_get_int(globalConfig, "General", "LastVersion");
+
 	if (!config_has_user_value(globalConfig, "General", "Pre19Defaults")) {
-		uint32_t lastVersion =
-			config_get_int(globalConfig, "General", "LastVersion");
 		bool useOldDefaults = lastVersion &&
 				      lastVersion <
 					      MAKE_SEMANTIC_VERSION(19, 0, 0);
@@ -706,8 +716,6 @@ bool OBSApp::InitGlobalConfig()
 	}
 
 	if (!config_has_user_value(globalConfig, "General", "Pre21Defaults")) {
-		uint32_t lastVersion =
-			config_get_int(globalConfig, "General", "LastVersion");
 		bool useOldDefaults = lastVersion &&
 				      lastVersion <
 					      MAKE_SEMANTIC_VERSION(21, 0, 0);
@@ -718,8 +726,6 @@ bool OBSApp::InitGlobalConfig()
 	}
 
 	if (!config_has_user_value(globalConfig, "General", "Pre23Defaults")) {
-		uint32_t lastVersion =
-			config_get_int(globalConfig, "General", "LastVersion");
 		bool useOldDefaults = lastVersion &&
 				      lastVersion <
 					      MAKE_SEMANTIC_VERSION(23, 0, 0);
@@ -736,6 +742,16 @@ bool OBSApp::InitGlobalConfig()
 		changed |= UpdatePre22MultiviewLayout(layout);
 	}
 
+	if (lastVersion && lastVersion < MAKE_SEMANTIC_VERSION(24, 0, 0)) {
+		bool disableHotkeysInFocus = config_get_bool(
+			globalConfig, "General", "DisableHotkeysInFocus");
+		if (disableHotkeysInFocus)
+			config_set_string(globalConfig, "General",
+					  "HotkeyFocusType",
+					  "DisableHotkeysInFocus");
+		changed = true;
+	}
+
 	if (changed)
 		config_save_safe(globalConfig, "tmp", nullptr);
 
@@ -1221,8 +1237,7 @@ void OBSApp::AppInit()
 		EnableOSXVSync(false);
 #endif
 
-	enableHotkeysInFocus = !config_get_bool(globalConfig, "General",
-						"DisableHotkeysInFocus");
+	UpdateHotkeyFocusSetting(false);
 
 	move_basic_to_profiles();
 	move_basic_to_scene_collections();
@@ -1251,13 +1266,34 @@ static bool StartupOBS(const char *locale, profiler_name_store_t *store)
 
 inline void OBSApp::ResetHotkeyState(bool inFocus)
 {
-	obs_hotkey_enable_background_press(inFocus || enableHotkeysInFocus);
+	obs_hotkey_enable_background_press(
+		(inFocus && enableHotkeysInFocus) ||
+		(!inFocus && enableHotkeysOutOfFocus));
+}
+
+void OBSApp::UpdateHotkeyFocusSetting(bool resetState)
+{
+	enableHotkeysInFocus = true;
+	enableHotkeysOutOfFocus = true;
+
+	const char *hotkeyFocusType =
+		config_get_string(globalConfig, "General", "HotkeyFocusType");
+
+	if (astrcmpi(hotkeyFocusType, "DisableHotkeysInFocus") == 0) {
+		enableHotkeysInFocus = false;
+	} else if (astrcmpi(hotkeyFocusType, "DisableHotkeysOutOfFocus") == 0) {
+		enableHotkeysOutOfFocus = false;
+	}
+
+	if (resetState)
+		ResetHotkeyState(applicationState() == Qt::ApplicationActive);
 }
 
-void OBSApp::EnableInFocusHotkeys(bool enable)
+void OBSApp::DisableHotkeys()
 {
-	enableHotkeysInFocus = enable;
-	ResetHotkeyState(applicationState() != Qt::ApplicationActive);
+	enableHotkeysInFocus = false;
+	enableHotkeysOutOfFocus = false;
+	ResetHotkeyState(applicationState() == Qt::ApplicationActive);
 }
 
 Q_DECLARE_METATYPE(VoidFunc)
@@ -1307,9 +1343,9 @@ bool OBSApp::OBSInit()
 
 	connect(this, &QGuiApplication::applicationStateChanged,
 		[this](Qt::ApplicationState state) {
-			ResetHotkeyState(state != Qt::ApplicationActive);
+			ResetHotkeyState(state == Qt::ApplicationActive);
 		});
-	ResetHotkeyState(applicationState() != Qt::ApplicationActive);
+	ResetHotkeyState(applicationState() == Qt::ApplicationActive);
 	return true;
 }
 

+ 8 - 1
UI/obs-app.hpp

@@ -80,6 +80,7 @@ private:
 	int sleepInhibitRefs = 0;
 
 	bool enableHotkeysInFocus = true;
+	bool enableHotkeysOutOfFocus = true;
 
 	std::deque<obs_frontend_translate_ui_cb> translatorHooks;
 
@@ -105,7 +106,13 @@ public:
 	void AppInit();
 	bool OBSInit();
 
-	void EnableInFocusHotkeys(bool enable);
+	void UpdateHotkeyFocusSetting(bool reset = true);
+	void DisableHotkeys();
+
+	inline bool HotkeysEnabledInFocus() const
+	{
+		return enableHotkeysInFocus;
+	}
 
 	inline QMainWindow *GetMainWindow() const { return mainWindow.data(); }
 

+ 22 - 11
UI/window-basic-settings.cpp

@@ -464,10 +464,20 @@ OBSBasicSettings::OBSBasicSettings(QWidget *parent)
 	HookWidget(ui->bindToIP,             COMBO_CHANGED,  ADV_CHANGED);
 	HookWidget(ui->enableNewSocketLoop,  CHECK_CHANGED,  ADV_CHANGED);
 	HookWidget(ui->enableLowLatencyMode, CHECK_CHANGED,  ADV_CHANGED);
-	HookWidget(ui->disableFocusHotkeys,  CHECK_CHANGED,  ADV_CHANGED);
+	HookWidget(ui->hotkeyFocusType,      COMBO_CHANGED,  ADV_CHANGED);
 	HookWidget(ui->autoRemux,            CHECK_CHANGED,  ADV_CHANGED);
 	/* clang-format on */
 
+#define ADD_HOTKEY_FOCUS_TYPE(s)      \
+	ui->hotkeyFocusType->addItem( \
+		QTStr("Basic.Settings.Advanced.Hotkeys." s), s)
+
+	ADD_HOTKEY_FOCUS_TYPE("NeverDisableHotkeys");
+	ADD_HOTKEY_FOCUS_TYPE("DisableHotkeysInFocus");
+	ADD_HOTKEY_FOCUS_TYPE("DisableHotkeysOutOfFocus");
+
+#undef ADD_HOTKEY_FOCUS_TYPE
+
 	ui->simpleOutputVBitrate->setSingleStep(50);
 	ui->simpleOutputVBitrate->setSuffix(" Kbps");
 	ui->advOutFFVBitrate->setSingleStep(50);
@@ -742,16 +752,15 @@ OBSBasicSettings::OBSBasicSettings(QWidget *parent)
 
 	UpdateAutomaticReplayBufferCheckboxes();
 
-	App()->EnableInFocusHotkeys(false);
+	App()->DisableHotkeys();
 }
 
 OBSBasicSettings::~OBSBasicSettings()
 {
-	bool disableHotkeysInFocus = config_get_bool(
-		App()->GlobalConfig(), "General", "DisableHotkeysInFocus");
 	delete ui->filenameFormatting->completer();
 	main->EnableOutputs(true);
-	App()->EnableInFocusHotkeys(!disableHotkeysInFocus);
+
+	App()->UpdateHotkeyFocusSetting();
 
 	EnableThreadedMessageBoxes(false);
 }
@@ -2253,6 +2262,8 @@ void OBSBasicSettings::LoadAdvancedSettings()
 	int rbTime = config_get_int(main->Config(), "AdvOut", "RecRBTime");
 	int rbSize = config_get_int(main->Config(), "AdvOut", "RecRBSize");
 	bool autoRemux = config_get_bool(main->Config(), "Video", "AutoRemux");
+	const char *hotkeyFocusType = config_get_string(
+		App()->GlobalConfig(), "General", "HotkeyFocusType");
 
 	loading = true;
 
@@ -2325,9 +2336,7 @@ void OBSBasicSettings::LoadAdvancedSettings()
 	ui->browserHWAccel->setChecked(browserHWAccel);
 #endif
 
-	bool disableFocusHotkeys = config_get_bool(
-		App()->GlobalConfig(), "General", "DisableHotkeysInFocus");
-	ui->disableFocusHotkeys->setChecked(disableFocusHotkeys);
+	SetComboByValue(ui->hotkeyFocusType, hotkeyFocusType);
 
 	loading = false;
 }
@@ -2930,9 +2939,11 @@ void OBSBasicSettings::SaveAdvancedSettings()
 			browserHWAccel);
 #endif
 
-	bool disableFocusHotkeys = ui->disableFocusHotkeys->isChecked();
-	config_set_bool(App()->GlobalConfig(), "General",
-			"DisableHotkeysInFocus", disableFocusHotkeys);
+	if (WidgetChanged(ui->hotkeyFocusType)) {
+		QString str = GetComboData(ui->hotkeyFocusType);
+		config_set_string(App()->GlobalConfig(), "General",
+				  "HotkeyFocusType", QT_TO_UTF8(str));
+	}
 
 #ifdef __APPLE__
 	if (WidgetChanged(ui->disableOSXVSync)) {