From 571da42bc0f8c213ddee454b656a6260d7bc975a Mon Sep 17 00:00:00 2001 From: Alyssa Ross Date: Sat, 20 Jan 2024 12:19:05 +0100 Subject: [PATCH] Add support for setting zoom for browser sources Previously, the only way to scale up a page was to use custom CSS, which may or may not work well depending on how the CSS rules of the page being displayed were written, or to transform the browser source, which would be blurry. To make it possible to do this properly, I've implemented support for setting the zoom level of the browser, equivalent to pressing Ctrl-+ / Ctrl-- in a browser. The range of [-6, 9] comes from kPresetZoomFactorsArray in Chromium. Zoom levels are indexes into this array, offset so that a 0 zoom level is 1.0. --- browser-client.cpp | 6 ++++++ browser-client.hpp | 4 ++++ obs-browser-plugin.cpp | 2 ++ obs-browser-source.cpp | 9 ++++++++- obs-browser-source.hpp | 1 + 5 files changed, 21 insertions(+), 1 deletion(-) diff --git a/browser-client.cpp b/browser-client.cpp index d91ca7024..4ca339c07 100644 --- a/browser-client.cpp +++ b/browser-client.cpp @@ -648,6 +648,12 @@ void BrowserClient::OnAudioStreamStopped(CefRefPtr browser, int id) } #endif +void BrowserClient::OnLoadStart(CefRefPtr browser, + CefRefPtr, TransitionType) +{ + browser->GetHost()->SetZoomLevel(bs->zoom); +} + void BrowserClient::OnLoadEnd(CefRefPtr, CefRefPtr frame, int) { diff --git a/browser-client.hpp b/browser-client.hpp index cbff1201e..1e3178712 100644 --- a/browser-client.hpp +++ b/browser-client.hpp @@ -179,6 +179,10 @@ class BrowserClient : public CefClient, int frames_per_buffer) override; #endif /* CefLoadHandler */ + virtual void OnLoadStart(CefRefPtr browser, + CefRefPtr frame, + TransitionType transition_type) override; + virtual void OnLoadEnd(CefRefPtr browser, CefRefPtr frame, int httpStatusCode) override; diff --git a/obs-browser-plugin.cpp b/obs-browser-plugin.cpp index 188958efe..53ffbfc29 100644 --- a/obs-browser-plugin.cpp +++ b/obs-browser-plugin.cpp @@ -187,6 +187,8 @@ static obs_properties_t *browser_source_get_properties(void *data) 8192, 1); obs_properties_add_int(props, "height", obs_module_text("Height"), 1, 8192, 1); + obs_properties_add_int(props, "zoom", obs_module_text("Zoom"), -6, 9, + 1); obs_properties_add_bool(props, "reroute_audio", obs_module_text("RerouteAudio")); diff --git a/obs-browser-source.cpp b/obs-browser-source.cpp index 09b301718..13f04b787 100644 --- a/obs-browser-source.cpp +++ b/obs-browser-source.cpp @@ -512,6 +512,7 @@ void BrowserSource::Update(obs_data_t *settings) bool n_is_local; int n_width; int n_height; + int n_zoom; bool n_fps_custom; int n_fps; bool n_shutdown; @@ -524,6 +525,7 @@ void BrowserSource::Update(obs_data_t *settings) n_is_local = obs_data_get_bool(settings, "is_local_file"); n_width = (int)obs_data_get_int(settings, "width"); n_height = (int)obs_data_get_int(settings, "height"); + n_zoom = (int)obs_data_get_int(settings, "zoom"); n_fps_custom = obs_data_get_bool(settings, "fps_custom"); n_fps = (int)obs_data_get_int(settings, "fps"); n_shutdown = obs_data_get_bool(settings, "shutdown"); @@ -582,11 +584,13 @@ void BrowserSource::Update(obs_data_t *settings) n_reroute == reroute_audio && n_webpage_control_level == webpage_control_level) { - if (n_width == width && n_height == height) + if (n_width == width && n_height == height && + n_zoom == zoom) return; width = n_width; height = n_height; + zoom = n_zoom; ExecuteOnBrowser( [=](CefRefPtr cefBrowser) { const CefSize cefSize(width, height); @@ -596,6 +600,8 @@ void BrowserSource::Update(obs_data_t *settings) ->OnAutoResize(cefBrowser, cefSize); cefBrowser->GetHost()->WasResized(); + cefBrowser->GetHost()->SetZoomLevel( + zoom); cefBrowser->GetHost()->Invalidate( PET_VIEW); }, @@ -606,6 +612,7 @@ void BrowserSource::Update(obs_data_t *settings) is_local = n_is_local; width = n_width; height = n_height; + zoom = n_zoom; fps = n_fps; fps_custom = n_fps_custom; shutdown_on_invisible = n_shutdown; diff --git a/obs-browser-source.hpp b/obs-browser-source.hpp index 7c77f2e6d..e3d9bb92e 100644 --- a/obs-browser-source.hpp +++ b/obs-browser-source.hpp @@ -81,6 +81,7 @@ struct BrowserSource { int width = 0; int height = 0; + int zoom = 0; bool fps_custom = false; int fps = 0; double canvas_fps = 0;