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

ChromelyEventedApp missing in 5.1 #259

Closed MichelJansson closed 3 years ago

MichelJansson commented 3 years ago

First - thanks for this project!

I just updated to 5.1 and it seems to have some breaking changes. I've used ChromelyEventedApp to override some events. This class is now gone, is there any upgrade paths to take?

Did have a look at #225 but could not find anything there. Thanks

MichelJansson commented 3 years ago

So I eventually figured out that the events of ChromelyEventedApp is now accessible/customizable by creating a custom window class inheriting from Chromely.Window, and then regisitering it in the builder with .UseWindow<MyWindow>().

Full code in the demo-projects: DemoWindow.cs Program.cs (.UseWindow)


Although I now got the event I'm looking for (BeforeClose) - what I actually used the event for no longer works here though, namely accessing the CefCookieManager, which now returns null... Ideas?

private void MyWindow_BeforeClose(object sender, BeforeCloseEventArgs e)
{
    // Flush cache
    // This is needed since there is some delay from when a cookie
    // has been set/deleted before it is actually persisted to disk.
    // This ensures all cookies is written to disk before exit.
    var cookieManager = CefCookieManager.GetGlobal(null);
    cookieManager.FlushStore(null);
}
mattkol commented 3 years ago

@MichelJansson

If this was working before, then it will be likely because it was a hack and was fixed - https://github.com/chromelyapps/Chromely/commit/fcb6e8eb1670e6788d521ef3bb80dbf4027b1078

But having said that I think we should be able to do stuff before actually closing/disposing CEF. The events handler need to move to the Window class itself rather than the Browser. Good catch! 👍

Meanwhile I think you can do this:

public class DemoWindow : Window
{
        public DemoWindow(IChromelyNativeHost nativeHost,
                      IChromelyConfiguration config,
                      ChromelyHandlersResolver handlersResolver)
            : base(nativeHost, config, handlersResolver)
        {
            nativeHost.HostClose += NativeHost_HostClose;

        }

        private void NativeHost_HostClose(object sender, CloseEventArgs e)
        {
            // Flush cache
            // This is needed since there is some delay from when a cookie
            // has been set/deleted before it is actually persisted to disk.
            // This ensures all cookies is written to disk before exit.
            var cookieManager = CefCookieManager.GetGlobal(null);
            cookieManager.FlushStore(null);
        }
}
MichelJansson commented 3 years ago

@mattkol, that works!

I agree that it's probably a good idea to allow that. On the other hand, why I require it here is not that I particularly want to, more that I just have to, to workaround the issue with cookies not being saved. So if that was handled somewhere else....

But that's for an other day! Closing this now - thanks for your prompt and accurate support!

mattkol commented 3 years ago

@MichelJansson definitely, not a showstopper, but it is still an issue and needs to be fixed. Easy to forget if closed, reopening. Thanks.

mattkol commented 3 years ago

@MichelJansson after looking at this closely, the code is doing the right thing. CefShutdown actually triggers the BeforeClose event, so the right place to do what you are doing is NativeHost HostClose event. Which is what you have now. Thanks.