chromelyapps / Chromely

Build Cross Platform HTML Desktop Apps on .NET using native GUI, HTML5, JavaScript, CSS, Owin, AspNetCore (MVC, RazorPages, Blazor)
MIT License
2.98k stars 279 forks source link

Feature Request: Provide a way of getting/setting the browser zoom level #270

Closed wwarby closed 3 years ago

wwarby commented 3 years ago

I believe the CEF project exposes APIs to get and set the browser's zoom level and I see a couple of references to zoom levels in the Chromely codebase, but no usable API to set it. It'd be really useful for the project I'm working on to be able call an API to change the zoom level and read the current zoom level.

mattkol commented 3 years ago

@wwarby sure that can be done. Holiday season... will see this weekend.

wwarby commented 3 years ago

Sweet - no rush obviously 👍

mattkol commented 3 years ago

@wwarby you can also actually do this without the fix.

SetZoomtDemo.zip


    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            AppBuilder
            .Create()
            .UseWindow<DemoWindow>()
            .UseApp<DemoApp>()
            .Build()
            .Run(args);
        }
    }

    public class DemoApp : ChromelyBasicApp
    {
    }

    public class DemoWindow : Window
    {
        public DemoWindow(IChromelyNativeHost nativeHost,
                      IChromelyConfiguration config,
                      ChromelyHandlersResolver handlersResolver)
            : base(nativeHost, config, handlersResolver)
        {
            FrameLoadStart += DemoWindow_FrameLoadStart;
        }

        public CefBrowserHost BrowserHost
        {
            get
            {
                return Browser?.GetHost();
            }
        }

        public double GetZoomLevel()
        {
           return BrowserHost == null ? 0 : BrowserHost.GetZoomLevel();
        }

        public void SetZoomLevel(double value)
        {
            BrowserHost?.SetZoomLevel(value);
        }

        private void DemoWindow_FrameLoadStart(object sender, FrameLoadStartEventArgs e)
        {
            SetZoomLevel(26.0);
        }
    }

Before zoom:

image

After zoom: image

wwarby commented 3 years ago

Thanks so much for this - I can confirm it works like an absolute charm. I have a slider in my app that changes the page zoom level and up until now I'd been using the CSS zoom property with a CSS variable, which kind of works but causes all sorts of issues with third party components like the ones from Angular Material. Setting the zoom level at the browser level completely resolves all those issue :)