void ClearRenderers(IVisualElementRenderer renderer) { if (renderer == null) { return; } var subviews = renderer.GetNativeElement().Subviews; for (var i = 0; i < subviews.Count; i++) { if (subviews[i] is IVisualElementRenderer childRenderer) { PushRenderer(childRenderer); // The ListView CalculateHeightForCell method can create renderers and dispose its child renderers before this is called. // Thus, it is possible that this work is already completed. if (childRenderer.Element != null && ReferenceEquals(childRenderer, Platform.GetRenderer(childRenderer.Element))) { childRenderer.Element.ClearValue(Platform.RendererProperty); } } renderer.GetNativeElement().Remove(subviews[i]); } }
// If the control size changes, then re-layout the header and footer. private void Grid_SizeChanged(object sender, SizeChangedEventArgs e) { if (_headerRenderer is object) { _headerRenderer.GetNativeElement().Width = e.NewSize.Width; _headerRenderer.GetNativeElement().UpdateLayout(); var header = Element.HeaderElement; var headerView = (VisualElement)header; SizeRequest request = headerView.Measure(4096, 4096); Layout.LayoutChildIntoBoundingRegion(headerView, new Rectangle(0, 0, e.NewSize.Width, request.Request.Height)); } if (_footerRenderer is object) { _footerRenderer.GetNativeElement().Width = e.NewSize.Width; _footerRenderer.GetNativeElement().UpdateLayout(); var footer = Element.FooterElement; var footerView = (VisualElement)footer; SizeRequest request = footerView.Measure(4096, 4096); Layout.LayoutChildIntoBoundingRegion(footerView, new Rectangle(0, 0, e.NewSize.Width, request.Request.Height)); } }
void EnsureZIndex() { if (ElementController.LogicalChildren.Count == 0) { return; } for (var z = 0; z < ElementController.LogicalChildren.Count; z++) { var child = ElementController.LogicalChildren[z] as VisualElement; if (child == null) { continue; } IVisualElementRenderer childRenderer = Platform.GetRenderer(child); if (childRenderer == null) { continue; } if (CanvasExtensions.GetZIndex(childRenderer.GetNativeElement()) != (z + 1)) { if (!_isZChanged) { _isZChanged = true; } CanvasExtensions.SetZIndex(childRenderer.GetNativeElement(), z + 1); } } }
void FillChildrenWithRenderers(VisualElement element) { foreach (var logicalChild in ((IElementController)element).LogicalChildren) { if (logicalChild is VisualElement child) { var renderer = GetFreeRenderer(child) ?? Platform.CreateRenderer(child); Platform.SetRenderer(child, renderer); _parent.GetNativeElement().Add(renderer.GetNativeElement()); } } }
protected override Avalonia.Size ArrangeOverride(Avalonia.Size finalSize) { if (Element == null) { return(finalSize); } Element.IsInNativeLayout = true; for (var i = 0; i < ElementController.LogicalChildren.Count; i++) { var child = ElementController.LogicalChildren[i] as VisualElement; if (child == null) { continue; } IVisualElementRenderer renderer = Platform.GetRenderer(child); if (renderer == null) { continue; } Rectangle bounds = child.Bounds; Control control = renderer.GetNativeElement(); Rect childFinal = new Rect(bounds.X, bounds.Y, Math.Max(0, bounds.Width), Math.Max(0, bounds.Height)); control.Arrange(childFinal); } Element.IsInNativeLayout = false; return(finalSize); }
protected override System.Windows.Size MeasureOverride(System.Windows.Size availableSize) { if (Element == null || availableSize.Width * availableSize.Height == 0) { return(new System.Windows.Size(0, 0)); } Element.IsInNativeLayout = true; for (var i = 0; i < ElementController.LogicalChildren.Count; i++) { var child = ElementController.LogicalChildren[i] as VisualElement; if (child == null) { continue; } IVisualElementRenderer renderer = Platform.GetRenderer(child); if (renderer == null) { continue; } FrameworkElement control = renderer.GetNativeElement(); if (control.ActualWidth != child.Width || control.ActualHeight != child.Height) { double width = child.Width <= -1 ? ActualWidth : child.Width; double height = child.Height <= -1 ? ActualHeight : child.Height; control.Measure(new System.Windows.Size(width, height)); } } System.Windows.Size result; if (double.IsInfinity(availableSize.Width) || double.IsPositiveInfinity(availableSize.Height)) { Size request = Element.Measure(availableSize.Width, availableSize.Height, MeasureFlags.IncludeMargins).Request; result = new System.Windows.Size(request.Width, request.Height); } else { result = availableSize; } Element.IsInNativeLayout = false; if (Double.IsPositiveInfinity(result.Height)) { result.Height = 0.0; } if (Double.IsPositiveInfinity(result.Width)) { result.Width = 0.0; } return(result); }
void UpdateFooter() { var footer = Element.FooterElement; var footerView = (VisualElement)footer; if (_footerRenderer is object) { Control.Children.Remove(_footerRenderer.GetNativeElement()); _footerRenderer.Dispose(); } if (footerView is null) { return; } _footerRenderer = Platform.CreateRenderer(footerView); Platform.SetRenderer(footerView, _footerRenderer); WGrid.SetRow(_footerRenderer.GetNativeElement(), 2); Control.Children.Add(_footerRenderer.GetNativeElement()); }
void UpdateHeader() { var header = Element.HeaderElement; var headerView = (VisualElement)header; if (_headerRenderer is object) { Control.Children.Remove(_headerRenderer.GetNativeElement()); _headerRenderer.Dispose(); } if (headerView is null) { return; } _headerRenderer = Platform.CreateRenderer(headerView); Platform.SetRenderer(headerView, _headerRenderer); WGrid.SetRow(_headerRenderer.GetNativeElement(), 0); Control.Children.Add(_headerRenderer.GetNativeElement()); }
public void ArrangeSubviews() { Layout.IsInNativeLayout = true; for (var i = 0; i < ElementController.LogicalChildren.Count; i++) { if (!(ElementController.LogicalChildren[i] is VisualElement child)) { continue; } IVisualElementRenderer renderer = Platform.GetRenderer(child); if (renderer == null) { continue; } Rectangle bounds = child.Bounds; View nativeView = renderer.GetNativeElement(); // Set child size var height = Convert.ToInt32(Math.Max(0, bounds.Height / SizeConverter)); if (height <= 0) { height = 1; } nativeView.Height = Dim.Sized(height); var width = Convert.ToInt32(Math.Max(0, bounds.Width / SizeConverter)); if (width <= 0) { width = 1; } nativeView.Width = Dim.Sized(width); // Set child position int x = Convert.ToInt32(bounds.X / SizeConverter); int y = Convert.ToInt32(bounds.Y / SizeConverter); nativeView.X = Pos.At(x); nativeView.Y = Pos.At(y); } Layout.IsInNativeLayout = false; }
void OnDisplayPageModalRequested(object sender, ModalHostPage.DisplayPageModalRequestedEventArgs e) { e.PageToDisplay.Parent = this.Element; IVisualElementRenderer renderer = XFPlatform.GetRenderer(e.PageToDisplay); if (renderer == null) { renderer = XFPlatform.CreateRenderer(e.PageToDisplay); XFPlatform.SetRenderer(e.PageToDisplay, renderer); } // TODO: Now display our modal page. var modalElement = renderer.GetNativeElement(); (Control as FormsLightNavigationPage)?.PushModal(modalElement, true); }
private void LoadContent() { var currentView = Element.Content; IVisualElementRenderer renderer = null; if (currentView != null) { renderer = currentView.GetOrCreateRenderer(); } if (renderer != null) { var content = renderer.GetNativeElement(); Control.Add(content); } }
void HandleChildAdded(object sender, ElementEventArgs e) { UiHelper.ExecuteInUiThread(() => { var view = e.Element as VisualElement; if (view == null) { return; } IVisualElementRenderer renderer = Platform.GetOrCreateRenderer(view); Control.Children.Add(renderer.GetNativeElement()); if (_isZChanged) { EnsureZIndex(); } }); }
protected override System.Windows.Size ArrangeOverride(System.Windows.Size finalSize) { if (Element == null) { return(finalSize); } Element.IsInNativeLayout = true; var presentationSource = PresentationSource.FromVisual(this); var stepX = presentationSource.CompositionTarget.TransformFromDevice.M11; var stepY = presentationSource.CompositionTarget.TransformFromDevice.M22; for (var i = 0; i < ElementController.LogicalChildren.Count; i++) { var child = ElementController.LogicalChildren[i] as VisualElement; if (child == null) { continue; } IVisualElementRenderer renderer = Platform.GetRenderer(child); if (renderer == null) { continue; } Rectangle bounds = child.Bounds; var control = renderer.GetNativeElement(); var width = Math.Max(0, bounds.Width); var height = Math.Max(0, bounds.Height); if (stepX != 1 && stepY != 1 && stepX != 0 && stepY != 0) { control.Width = width = Math.Ceiling(width / stepX) * stepX; control.Height = height = Math.Ceiling(height / stepY) * stepY; } control.Arrange(new Rect(bounds.X, bounds.Y, width, height)); } Element.IsInNativeLayout = false; return(finalSize); }
protected override global::Avalonia.Size ArrangeOverride(global::Avalonia.Size finalSize) { if (Element == null) { return(finalSize); } Element.IsInNativeLayout = true; var stepX = 1; var stepY = 1; for (var i = 0; i < ElementController.LogicalChildren.Count; i++) { var child = ElementController.LogicalChildren[i] as VisualElement; if (child == null) { continue; } IVisualElementRenderer renderer = Platform.GetRenderer(child); if (renderer == null) { continue; } Rectangle bounds = child.Bounds; var control = renderer.GetNativeElement(); var width = Math.Max(bounds.Width, control.DesiredSize.Width); var height = Math.Max(bounds.Height, control.DesiredSize.Height); if (stepX != 1 && stepY != 1 && stepX != 0 && stepY != 0) { control.Width = width = Math.Ceiling(width / stepX) * stepX; control.Height = height = Math.Ceiling(height / stepY) * stepY; } var childRect = new global::Avalonia.Rect(bounds.X, bounds.Y, width, height); control.Arrange(childRect); } Element.IsInNativeLayout = false; return(finalSize); }
public static Control ToControl(this Page view) { if (!Forms.IsInitialized) { throw new InvalidOperationException("Call Forms.Init() before this"); } if (!(view.RealParent is Application)) { Application app = new DefaultApplication { MainPage = view }; var formsApplicationPage = new FormsApplicationPage(); formsApplicationPage.LoadApplication(app); var platform = new Platform(formsApplicationPage); platform.SetPage(view); } IVisualElementRenderer renderer = Platform.GetOrCreateRenderer(view); if (renderer == null) { throw new InvalidOperationException($"Could not find or create a renderer for {view}"); } var Control = renderer.GetNativeElement(); Control.AttachedToLogicalTree += (sender, e) => { view.Layout(new Rectangle(0, 0, Control.Bounds.Width, Control.Bounds.Height)); }; return(Control); }
public static FrameworkElement ToFrameworkElement(this Page view) { if (!System.Maui.Maui.IsInitialized) { throw new InvalidOperationException("Call System.Maui.Maui.Init() before this"); } if (!(view.RealParent is Application)) { Application app = new DefaultApplication { MainPage = view }; var formsApplicationPage = new FormsApplicationPage(); formsApplicationPage.LoadApplication(app); var platform = new Platform(formsApplicationPage); platform.SetPage(view); } IVisualElementRenderer renderer = Platform.GetOrCreateRenderer(view); if (renderer == null) { throw new InvalidOperationException($"Could not find or create a renderer for {view}"); } var frameworkElement = renderer.GetNativeElement(); frameworkElement.Loaded += (sender, args) => { view.Layout(new Rectangle(0, 0, frameworkElement.ActualWidth, frameworkElement.ActualHeight)); }; return(frameworkElement); }
protected override global::Avalonia.Size MeasureOverride(global::Avalonia.Size availableSize) { var baseDesiredSize = base.MeasureOverride(availableSize); bool hasVisibleChild = false; if (Element == null || availableSize.Width * availableSize.Height == 0) { return(new global::Avalonia.Size(0, 0)); } Element.IsInNativeLayout = true; for (var i = 0; i < ElementController.LogicalChildren.Count; i++) { var child = ElementController.LogicalChildren[i] as VisualElement; if (child == null) { continue; } IVisualElementRenderer renderer = Platform.GetRenderer(child); if (renderer == null) { continue; } Control control = renderer.GetNativeElement(); bool isVisible = control.IsVisible; if (isVisible && !hasVisibleChild) { hasVisibleChild = true; } var controlBounds = control.Bounds; if (controlBounds.Width != child.Width || controlBounds.Height != child.Height) { double width = child.Width <= -1 ? availableSize.Width : child.Width; double height = child.Height <= -1 ? availableSize.Height : child.Height; control.Measure(new global::Avalonia.Size(width, height)); var childDesiredSize = control.DesiredSize; } } global::Avalonia.Size result; if (double.IsInfinity(availableSize.Width) || double.IsPositiveInfinity(availableSize.Height)) { Size request = Element.Measure(availableSize.Width, availableSize.Height, MeasureFlags.IncludeMargins).Request; result = new global::Avalonia.Size(request.Width, request.Height); } else { result = availableSize; } Element.IsInNativeLayout = false; if (Double.IsPositiveInfinity(result.Height)) { result = result.WithHeight(0.0); } if (Double.IsPositiveInfinity(result.Width)) { result = result.WithWidth(0.0); } var finalDesiredSize = new global::Avalonia.Size(Math.Max(baseDesiredSize.Width, result.Width), Math.Max(baseDesiredSize.Height, result.Height)); return(result); }