// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using Microsoft.AspNetCore.Blazor.Browser.Interop; using Microsoft.AspNetCore.Blazor.Browser.Services; using Microsoft.AspNetCore.Blazor.Components; using Microsoft.AspNetCore.Blazor.Rendering; using Microsoft.AspNetCore.Blazor.RenderTree; using System; namespace Microsoft.AspNetCore.Blazor.Browser.Rendering { /// /// Provides mechanisms for rendering instances in a /// web browser, dispatching events to them, and refreshing the UI as required. /// public class BrowserRenderer : Renderer, IDisposable { private readonly int _browserRendererId; /// /// Constructs an instance of . /// public BrowserRenderer(): this(new DefaultBrowserServiceProvider()) { } /// /// Constructs an instance of . /// /// The to use when initializing components. public BrowserRenderer(IServiceProvider serviceProvider): base(serviceProvider) { _browserRendererId = BrowserRendererRegistry.Add(this); } internal void DispatchBrowserEvent(int componentId, int eventHandlerId, UIEventArgs eventArgs) => DispatchEvent(componentId, eventHandlerId, eventArgs); /// /// Attaches a new root component to the renderer, /// causing it to be displayed in the specified DOM element. /// /// The type of the component. /// A CSS selector that uniquely identifies a DOM element. public void AddComponent(string domElementSelector) where TComponent: IComponent { AddComponent(typeof(TComponent), domElementSelector); } /// /// Associates the with the , /// causing it to be displayed in the specified DOM element. /// /// The type of the component. /// A CSS selector that uniquely identifies a DOM element. public void AddComponent(Type componentType, string domElementSelector) { var component = InstantiateComponent(componentType); var componentId = AssignComponentId(component); RegisteredFunction.InvokeUnmarshalled( "attachComponentToElement", _browserRendererId, domElementSelector, componentId); component.SetParameters(ParameterCollection.Empty); } /// /// Disposes the instance. /// public void Dispose() { BrowserRendererRegistry.TryRemove(_browserRendererId); } /// protected override void UpdateDisplay(RenderBatch batch) { RegisteredFunction.InvokeUnmarshalled( "renderBatch", _browserRendererId, batch); } } }