kyy
2025-07-02 07558e32634314eec359ec8437d97bdc5def64f9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright © 2021 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
 
namespace CefSharp.WinForms.Handler
{
    /// <summary>
    /// Fluent <see cref="CefSharp.WinForms.Handler.LifeSpanHandler"/> Builder
    /// </summary>
    public class LifeSpanHandlerBuilder
    {
        private readonly LifeSpanHandler handler;
 
        /// <summary>
        /// LifeSpanHandlerBuilder
        /// </summary>
        /// <param name="chromiumHostControlCreatedDelegate">
        /// When specified the delegate will be used to create the <see cref="Host.ChromiumHostControl"/>
        /// instance. Allowing users to create their own custom instance that extends <see cref="Host.ChromiumHostControl"/>
        /// </param>
        public LifeSpanHandlerBuilder(CreatePopupChromiumHostControl chromiumHostControlCreatedDelegate)
        {
            handler = new LifeSpanHandler(chromiumHostControlCreatedDelegate);
        }
 
        /// <summary>
        /// The <see cref="OnBeforePopupCreatedDelegate"/> will be called <b>before</b> the popup has been created and
        /// can be used to cancel popup creation if required, modify <see cref="IBrowserSettings"/> and disable javascript.
        /// </summary>
        /// <param name="onBeforePopupCreated">Action to be invoked before popup is created.</param>
        /// <returns><see cref="LifeSpanHandlerBuilder"/> instance allowing you to chain method calls together</returns>
        public LifeSpanHandlerBuilder OnBeforePopupCreated(OnBeforePopupCreatedDelegate onBeforePopupCreated)
        {
            handler.OnBeforePopupCreated(onBeforePopupCreated);
 
            return this;
        }
 
        /// <summary>
        /// The <see cref="OnPopupCreatedDelegate"/> will be called when the<see cref="Host.ChromiumHostControl"/> has been
        /// created. When the <see cref="OnPopupCreatedDelegate"/> is called you must add the control to it's intended parent
        /// so the <see cref="System.Windows.Forms.Control.ClientRectangle"/> can be calculated to set the initial
        /// size correctly.
        /// </summary>
        /// <param name="onPopupCreated">Action to be invoked when the Popup is to be destroyed.</param>
        /// <returns><see cref="LifeSpanHandlerBuilder"/> instance allowing you to chain method calls together</returns>
        public LifeSpanHandlerBuilder OnPopupCreated(OnPopupCreatedDelegate onPopupCreated)
        {
            handler.OnPopupCreated(onPopupCreated);
 
            return this;
        }
 
        /// <summary>
        /// The <see cref="OnPopupBrowserCreatedDelegate"/> will be called when the<see cref="IBrowser"/> has been
        /// created. The <see cref="IBrowser"/> instance is valid until <see cref="OnPopupDestroyed(OnPopupDestroyedDelegate)"/>
        /// is called. <see cref="IBrowser"/> provides low level access to the CEF Browser, you can access frames, view source,
        /// perform navigation (via frame) etc.
        /// </summary>
        /// <param name="onPopupBrowserCreated">Action to be invoked when the <see cref="IBrowser"/> has been created.</param>
        /// <returns><see cref="LifeSpanHandlerBuilder"/> instance allowing you to chain method calls together</returns>
        public LifeSpanHandlerBuilder OnPopupBrowserCreated(OnPopupBrowserCreatedDelegate onPopupBrowserCreated)
        {
            handler.OnPopupBrowserCreated(onPopupBrowserCreated);
 
            return this;
        }
 
        /// <summary>
        /// The <see cref="OnPopupDestroyedDelegate"/> will be called when the <see cref="Host.ChromiumHostControl"/> is to be
        /// removed from it's parent.
        /// When the <see cref="OnPopupDestroyedDelegate"/> is called you must remove/dispose of the <see cref="Host.ChromiumHostControl"/>.
        /// </summary>
        /// <param name="onPopupDestroyed">Action to be invoked when the Popup is to be destroyed.</param>
        /// <returns><see cref="LifeSpanHandlerBuilder"/> instance allowing you to chain method calls together</returns>
        public LifeSpanHandlerBuilder OnPopupDestroyed(OnPopupDestroyedDelegate onPopupDestroyed)
        {
            handler.OnPopupDestroyed(onPopupDestroyed);
 
            return this;
        }
 
        /// <summary>
        /// Creates an <see cref="ILifeSpanHandler"/> implementation
        /// that can be used to host popups as tabs/controls. The resulting
        /// <see cref="ILifeSpanHandler"/> returns true in <see cref="ILifeSpanHandler.DoClose(IWebBrowser, IBrowser)"/>
        /// so no WM_CLOSE message is sent, this differs from the default CEF behaviour.
        /// </summary>
        /// <returns>a <see cref="ILifeSpanHandler"/> instance</returns>
        public ILifeSpanHandler Build()
        {
            return handler;
        }
    }
}