зеркало из https://github.com/jsuarezruiz/HotUI.git
Fixed layout issues on Mac.
This commit is contained in:
Родитель
321dd20d09
Коммит
d2e129b602
|
@ -15,12 +15,10 @@ namespace HotUI.Mac.Handlers
|
|||
|
||||
protected AbstractLayoutHandler(CGRect rect) : base(rect)
|
||||
{
|
||||
InitializeDefaults();
|
||||
}
|
||||
|
||||
protected AbstractLayoutHandler()
|
||||
{
|
||||
InitializeDefaults();
|
||||
}
|
||||
|
||||
public override bool IsFlipped => true;
|
||||
|
@ -44,7 +42,7 @@ namespace HotUI.Mac.Handlers
|
|||
|
||||
public void SetFrame(RectangleF frame)
|
||||
{
|
||||
View.Frame = frame.ToCGRect();
|
||||
Frame = frame.ToCGRect();
|
||||
}
|
||||
|
||||
public void SetView(View view)
|
||||
|
@ -52,6 +50,7 @@ namespace HotUI.Mac.Handlers
|
|||
_view = view as AbstractLayout;
|
||||
if (_view != null)
|
||||
{
|
||||
_view.NeedsLayout += HandleNeedsLayout;
|
||||
_view.ChildrenChanged += HandleChildrenChanged;
|
||||
_view.ChildrenAdded += HandleChildrenAdded;
|
||||
_view.ChildrenRemoved += ViewOnChildrenRemoved;
|
||||
|
@ -70,6 +69,11 @@ namespace HotUI.Mac.Handlers
|
|||
}
|
||||
}
|
||||
|
||||
private void HandleNeedsLayout(object sender, EventArgs e)
|
||||
{
|
||||
SetNeedsLayout();
|
||||
}
|
||||
|
||||
public void Remove(View view)
|
||||
{
|
||||
foreach (var subview in _view)
|
||||
|
@ -84,6 +88,7 @@ namespace HotUI.Mac.Handlers
|
|||
|
||||
if (view != null)
|
||||
{
|
||||
_view.NeedsLayout -= HandleNeedsLayout;
|
||||
_view.ChildrenChanged -= HandleChildrenChanged;
|
||||
_view.ChildrenAdded -= HandleChildrenAdded;
|
||||
_view.ChildrenRemoved -= ViewOnChildrenRemoved;
|
||||
|
@ -110,11 +115,6 @@ namespace HotUI.Mac.Handlers
|
|||
{
|
||||
}
|
||||
|
||||
private void InitializeDefaults()
|
||||
{
|
||||
TranslatesAutoresizingMaskIntoConstraints = false;
|
||||
}
|
||||
|
||||
private void HandleChildrenAdded(object sender, LayoutEventArgs e)
|
||||
{
|
||||
for (var i = 0; i < e.Count; i++)
|
||||
|
@ -199,7 +199,11 @@ namespace HotUI.Mac.Handlers
|
|||
|
||||
public void SizeToFit()
|
||||
{
|
||||
_measured = _view.Measure(Superview?.Bounds.Size.ToSizeF() ?? NSScreen.MainScreen.Frame.Size.ToSizeF());
|
||||
var size = Superview?.Bounds.Size;
|
||||
if (size == null || ((CGSize)size).IsEmpty)
|
||||
size = NSScreen.MainScreen.Frame.Size;
|
||||
|
||||
_measured = _view.Measure(((CGSize)size).ToSizeF());
|
||||
base.Frame = new CGRect(new CGPoint(0, 0), _measured.ToCGSize());
|
||||
}
|
||||
|
||||
|
@ -229,7 +233,10 @@ namespace HotUI.Mac.Handlers
|
|||
|
||||
public override void Layout()
|
||||
{
|
||||
if (Superview == null || Bounds.Size.IsEmpty)
|
||||
if (Superview == null)
|
||||
return;
|
||||
|
||||
if (Bounds.Size.IsEmpty)
|
||||
return;
|
||||
|
||||
_view.Frame = Frame.ToRectangleF();
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
using AppKit;
|
||||
using HotUI.Mac.Extensions;
|
||||
using HotUI.Mac.Handlers;
|
||||
using System;
|
||||
|
||||
namespace HotUI.Mac
|
||||
{
|
||||
|
@ -13,16 +14,11 @@ namespace HotUI.Mac
|
|||
|
||||
public HotUIView()
|
||||
{
|
||||
TranslatesAutoresizingMaskIntoConstraints = false;
|
||||
AutoresizesSubviews = true;
|
||||
AutoresizingMask = NSViewResizingMask.HeightSizable | NSViewResizingMask.WidthSizable;
|
||||
}
|
||||
|
||||
public HotUIView(CGRect rect) : base(rect)
|
||||
{
|
||||
TranslatesAutoresizingMaskIntoConstraints = false;
|
||||
AutoresizesSubviews = true;
|
||||
AutoresizingMask = NSViewResizingMask.HeightSizable | NSViewResizingMask.WidthSizable;
|
||||
|
||||
}
|
||||
|
||||
public View CurrentView
|
||||
|
@ -33,12 +29,52 @@ namespace HotUI.Mac
|
|||
if (value == _virtualView)
|
||||
return;
|
||||
|
||||
_virtualView = value;
|
||||
_handler = _virtualView.GetOrCreateViewHandler();
|
||||
if (_handler is ViewHandler viewHandler)
|
||||
viewHandler.NativeViewChanged += HandleNativeViewChanged;
|
||||
if (_virtualView != null)
|
||||
{
|
||||
_virtualView.ViewHandlerChanged -= HandleViewHandlerChanged;
|
||||
_virtualView.NeedsLayout -= HandleNeedsLayout;
|
||||
if (_handler is MacViewHandler viewHandler)
|
||||
viewHandler.NativeViewChanged -= HandleNativeViewChanged;
|
||||
}
|
||||
|
||||
HandleNativeViewChanged(this, null);
|
||||
_virtualView = value;
|
||||
|
||||
if (_virtualView != null)
|
||||
{
|
||||
_handler = _virtualView.GetOrCreateViewHandler();
|
||||
|
||||
_virtualView.ViewHandlerChanged += HandleViewHandlerChanged;
|
||||
_virtualView.NeedsLayout += HandleNeedsLayout;
|
||||
if (_handler is MacViewHandler viewHandler)
|
||||
viewHandler.NativeViewChanged += HandleNativeViewChanged;
|
||||
|
||||
HandleNativeViewChanged(this, new ViewChangedEventArgs(_virtualView, null, (NSView)_handler.NativeView));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleNeedsLayout(object sender, EventArgs e)
|
||||
{
|
||||
SetNeedsLayout();
|
||||
}
|
||||
|
||||
private void HandleViewHandlerChanged(object sender, ViewHandlerChangedEventArgs e)
|
||||
{
|
||||
Console.WriteLine($"[{GetType().Name}] HandleViewHandlerChanged: [{sender.GetType()}] From:[{e.OldViewHandler?.GetType()}] To:[{e.NewViewHandler?.GetType()}]");
|
||||
|
||||
if (e.OldViewHandler is MacViewHandler oldHandler)
|
||||
{
|
||||
oldHandler.NativeViewChanged -= HandleNativeViewChanged;
|
||||
_nativeView?.RemoveFromSuperview();
|
||||
_nativeView = null;
|
||||
}
|
||||
|
||||
if (e.NewViewHandler is MacViewHandler newHandler)
|
||||
{
|
||||
newHandler.NativeViewChanged += HandleNativeViewChanged;
|
||||
_nativeView = newHandler.View ?? new NSView();
|
||||
AddSubview(_nativeView);
|
||||
SetNeedsLayout();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -110,19 +146,13 @@ namespace HotUI.Mac
|
|||
bounds.Height -= padding.VerticalThickness;
|
||||
}
|
||||
|
||||
if (_nativeView is NSTableView || _nativeView is ListViewHandler)
|
||||
_nativeView.Frame = bounds;
|
||||
else
|
||||
{
|
||||
CGSize sizeThatFits;
|
||||
if (_nativeView is AbstractLayoutHandler layout)
|
||||
sizeThatFits = layout.SizeThatFits(bounds.Size);
|
||||
else
|
||||
sizeThatFits = bounds.Size;
|
||||
var x = ((bounds.Width - sizeThatFits.Width) / 2) + padding.Left;
|
||||
var y = ((bounds.Height - sizeThatFits.Height) / 2) + padding.Top;
|
||||
_nativeView.Frame = new CGRect(x, y, sizeThatFits.Width, sizeThatFits.Height);
|
||||
}
|
||||
var sizeThatFits = _virtualView.Measure(bounds.Size.ToSizeF());
|
||||
_virtualView.MeasuredSize = sizeThatFits;
|
||||
_virtualView.MeasurementValid = true;
|
||||
|
||||
var x = ((bounds.Width - sizeThatFits.Width) / 2) + padding.Left;
|
||||
var y = ((bounds.Height - sizeThatFits.Height) / 2) + padding.Top;
|
||||
_virtualView.Frame = new RectangleF((float)x, (float)y, sizeThatFits.Width, sizeThatFits.Height);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using CoreGraphics;
|
||||
using HotUI.iOS.Controls;
|
||||
using HotUI.Layout;
|
||||
using UIKit;
|
||||
|
||||
namespace HotUI.iOS.Handlers
|
||||
|
|
|
@ -13,7 +13,6 @@ namespace HotUI.iOS
|
|||
public HotUIView()
|
||||
{
|
||||
BackgroundColor = UIColor.White;
|
||||
AutosizesSubviews = false;
|
||||
}
|
||||
|
||||
public HotUIView(CGRect rect) : base(rect)
|
||||
|
|
Загрузка…
Ссылка в новой задаче