Skip to content

Commit

Permalink
Add support for setting zoom for browser sources
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
alyssais committed Jan 25, 2024
1 parent e873fb0 commit 571da42
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 1 deletion.
6 changes: 6 additions & 0 deletions browser-client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,12 @@ void BrowserClient::OnAudioStreamStopped(CefRefPtr<CefBrowser> browser, int id)
}
#endif

void BrowserClient::OnLoadStart(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame>, TransitionType)
{
browser->GetHost()->SetZoomLevel(bs->zoom);
}

void BrowserClient::OnLoadEnd(CefRefPtr<CefBrowser>, CefRefPtr<CefFrame> frame,
int)
{
Expand Down
4 changes: 4 additions & 0 deletions browser-client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ class BrowserClient : public CefClient,
int frames_per_buffer) override;
#endif
/* CefLoadHandler */
virtual void OnLoadStart(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
TransitionType transition_type) override;

virtual void OnLoadEnd(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
int httpStatusCode) override;
Expand Down
2 changes: 2 additions & 0 deletions obs-browser-plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down
9 changes: 8 additions & 1 deletion obs-browser-source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
Expand Down Expand Up @@ -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> cefBrowser) {
const CefSize cefSize(width, height);
Expand All @@ -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);
},
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions obs-browser-source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 571da42

Please sign in to comment.