Browse Source

UI: Add window projector option "fit to content"

Adds an option to the context menu of a windowed projector to allow
resizing the aspect ratio of the client area of the window to the
contents of what's being projected (so that there's no extra space).
Scratch 5 years ago
parent
commit
5421851526
3 changed files with 35 additions and 1 deletions
  1. 1 0
      UI/data/locale/en-US.ini
  2. 33 1
      UI/window-projector.cpp
  3. 1 0
      UI/window-projector.hpp

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

@@ -42,6 +42,7 @@ SceneWindow="Windowed Projector (Scene)"
 SourceWindow="Windowed Projector (Source)"
 MultiviewProjector="Multiview (Fullscreen)"
 MultiviewWindowed="Multiview (Windowed)"
+ResizeProjectorWindowToContent="Fit window to content"
 Clear="Clear"
 Revert="Revert"
 Show="Show"

+ 33 - 1
UI/window-projector.cpp

@@ -828,10 +828,15 @@ void OBSProjector::mousePressEvent(QMouseEvent *event)
 					       SLOT(OpenFullScreenProjector()));
 		popup.addMenu(projectorMenu);
 
-		if (GetMonitor() > -1)
+		if (GetMonitor() > -1) {
 			popup.addAction(QTStr("Windowed"), this,
 					SLOT(OpenWindowedProjector()));
 
+		} else if (!this->isMaximized()) {
+			popup.addAction(QTStr("ResizeProjectorWindowToContent"),
+					this, SLOT(ResizeToContent()));
+		}
+
 		popup.addAction(QTStr("Close"), this, SLOT(EscapeTriggered()));
 		popup.exec(QCursor::pos());
 	}
@@ -1055,6 +1060,33 @@ void OBSProjector::OpenWindowedProjector()
 	UpdateProjectorTitle(QT_UTF8(obs_source_get_name(source)));
 }
 
+void OBSProjector::ResizeToContent()
+{
+	OBSSource source = GetSource();
+	uint32_t targetCX;
+	uint32_t targetCY;
+	int x, y, newX, newY;
+	float scale;
+
+	if (source) {
+		targetCX = std::max(obs_source_get_width(source), 1u);
+		targetCY = std::max(obs_source_get_height(source), 1u);
+	} else {
+		struct obs_video_info ovi;
+		obs_get_video_info(&ovi);
+		targetCX = ovi.base_width;
+		targetCY = ovi.base_height;
+	}
+
+	QSize size = this->size();
+	GetScaleAndCenterPos(targetCX, targetCY, size.width(), size.height(), x,
+			     y, scale);
+
+	newX = size.width() - (x * 2);
+	newY = size.height() - (y * 2);
+	resize(newX, newY);
+}
+
 void OBSProjector::closeEvent(QCloseEvent *event)
 {
 	EscapeTriggered();

+ 1 - 0
UI/window-projector.hpp

@@ -78,6 +78,7 @@ private:
 private slots:
 	void EscapeTriggered();
 	void OpenFullScreenProjector();
+	void ResizeToContent();
 	void OpenWindowedProjector();
 
 public: