Skip to content

Commit

Permalink
modify code
Browse files Browse the repository at this point in the history
  • Loading branch information
LyonJack committed Jul 3, 2020
1 parent 981370f commit 5032524
Show file tree
Hide file tree
Showing 15 changed files with 57 additions and 64 deletions.
6 changes: 4 additions & 2 deletions source/AvalonDocPanelMemoryLeaks/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ private void Button_Click(object sender, RoutedEventArgs e)
UserControl content = new UserControl();
HeavyLoad = new HeavyLoad();
content.DataContext = HeavyLoad;
LayoutDocument docDocument = new LayoutDocument();
docDocument.Content = content;
LayoutDocument docDocument = new LayoutDocument
{
Content = content
};
docGrup.Children.Add(docDocument);
docDocument.Closed += DocClosed;
}
Expand Down
8 changes: 4 additions & 4 deletions source/Components/AvalonDock/Controls/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public static IEnumerable<T> FindVisualChildren<T>(this DependencyObject depObj)
for (var i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
var child = VisualTreeHelper.GetChild(depObj, i);
if (child is T)
yield return (T) child;
if (child is T t)
yield return t;
foreach (var childOfChild in FindVisualChildren<T>(child))
yield return childOfChild;
}
Expand All @@ -45,8 +45,8 @@ public static IEnumerable<T> FindLogicalChildren<T>(this DependencyObject depObj
if (depObj == null) yield break;
foreach (var child in LogicalTreeHelper.GetChildren(depObj).OfType<DependencyObject>())
{
if (child is T)
yield return (T) child;
if (child is T t)
yield return t;
foreach (var childOfChild in FindLogicalChildren<T>(child))
yield return childOfChild;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace AvalonDock.Controls
public class LayoutAnchorableFloatingWindowControl : LayoutFloatingWindowControl, IOverlayWindowHost
{
#region fields
private LayoutAnchorableFloatingWindow _model;
private readonly LayoutAnchorableFloatingWindow _model;
private OverlayWindow _overlayWindow = null;
private List<IDropArea> _dropAreas = null;
#endregion fields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace AvalonDock.Controls
public class LayoutAnchorablePaneControl : TabControlEx, ILayoutControl//, ILogicalChildrenContainer
{
#region fields
private LayoutAnchorablePane _model;
private readonly LayoutAnchorablePane _model;
#endregion fields

#region Constructors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class LayoutAutoHideWindowControl : HwndHost, ILayoutControl
private LayoutAnchorable _model;
private HwndSource _internalHwndSource = null;
private IntPtr parentWindowHandle;
private ContentPresenter _internalHostPresenter = new ContentPresenter();
private readonly ContentPresenter _internalHostPresenter = new ContentPresenter();
private Grid _internalGrid = null;
private AnchorSide _side;
private LayoutGridResizerControl _resizer = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace AvalonDock.Controls
public class LayoutDocumentPaneControl : TabControlEx, ILayoutControl//, ILogicalChildrenContainer
{
#region fields
private LayoutDocumentPane _model;
private readonly LayoutDocumentPane _model;
#endregion fields

#region Constructors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace AvalonDock.Controls
public class LayoutDocumentPaneGroupControl : LayoutGridControl<ILayoutDocumentPane>, ILayoutControl
{
#region fields
private LayoutDocumentPaneGroup _model;
private readonly LayoutDocumentPaneGroup _model;
#endregion fields

#region Constructors
Expand Down
6 changes: 3 additions & 3 deletions source/Components/AvalonDock/Controls/LayoutGridControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ namespace AvalonDock.Controls
public abstract class LayoutGridControl<T> : Grid, ILayoutControl, IAdjustableSizeLayout where T : class, ILayoutPanelElement
{
#region fields
private LayoutPositionableGroup<T> _model;
private Orientation _orientation;
private readonly LayoutPositionableGroup<T> _model;
private readonly Orientation _orientation;
private bool _initialized;
private ChildrenTreeChange? _asyncRefreshCalled;
private ReentrantFlag _fixingChildrenDockLengths = new ReentrantFlag();
private readonly ReentrantFlag _fixingChildrenDockLengths = new ReentrantFlag();
private Border _resizerGhost = null;
private Window _resizerWindowHost = null;
private Vector _initialStartPoint;
Expand Down
4 changes: 2 additions & 2 deletions source/Components/AvalonDock/Controls/LayoutItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public abstract class LayoutItem : FrameworkElement
private ICommand _defaultMoveToNextTabGroupCommand;
private ICommand _defaultMoveToPreviousTabGroupCommand;
private ContentPresenter _view = null;
private ReentrantFlag _isSelectedReentrantFlag = new ReentrantFlag();
private ReentrantFlag _isActiveReentrantFlag = new ReentrantFlag();
private readonly ReentrantFlag _isSelectedReentrantFlag = new ReentrantFlag();
private readonly ReentrantFlag _isActiveReentrantFlag = new ReentrantFlag();
#endregion fields

#region Constructors
Expand Down
43 changes: 17 additions & 26 deletions source/Components/AvalonDock/Controls/LayoutPanelControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace AvalonDock.Controls
public class LayoutPanelControl : LayoutGridControl<ILayoutPanelElement>, ILayoutControl
{
#region fields
private LayoutPanel _model;
private readonly LayoutPanel _model;
#endregion fields

#region Constructors
Expand All @@ -41,34 +41,29 @@ protected override void OnFixChildrenDockLengths()
{
if (ActualWidth == 0.0 || ActualHeight == 0.0) return;

var modelAsPositionableElement = _model as ILayoutPositionableElementWithActualSize;
#region Setup DockWidth/Height for children
if (_model.Orientation == Orientation.Horizontal)
#region Setup DockWidth/Height for children
if (_model.Orientation == Orientation.Horizontal)
{
if (_model.ContainsChildOfType<LayoutDocumentPane, LayoutDocumentPaneGroup>())
{
for (var i = 0; i < _model.Children.Count; i++)
{
var childContainerModel = _model.Children[i] as ILayoutContainer;
var childPositionableModel = _model.Children[i] as ILayoutPositionableElement;

if (childContainerModel != null &&
(childContainerModel.IsOfType<LayoutDocumentPane, LayoutDocumentPaneGroup>() ||
childContainerModel.ContainsChildOfType<LayoutDocumentPane, LayoutDocumentPaneGroup>()))
if (_model.Children[i] as ILayoutContainer != null &&
((_model.Children[i] as ILayoutContainer).IsOfType<LayoutDocumentPane, LayoutDocumentPaneGroup>() ||
(_model.Children[i] as ILayoutContainer).ContainsChildOfType<LayoutDocumentPane, LayoutDocumentPaneGroup>()))
{
// Keep set values (from XML for instance)
if(!childPositionableModel.DockWidth.IsStar) childPositionableModel.DockWidth = new GridLength(1.0, GridUnitType.Star);
if(!(_model.Children[i] as ILayoutPositionableElement).DockWidth.IsStar) (_model.Children[i] as ILayoutPositionableElement).DockWidth = new GridLength(1.0, GridUnitType.Star);
}
else if (childPositionableModel != null && childPositionableModel.DockWidth.IsStar)
else if (_model.Children[i] as ILayoutPositionableElement != null && (_model.Children[i] as ILayoutPositionableElement).DockWidth.IsStar)
{
var childPositionableModelWidthActualSize = childPositionableModel as ILayoutPositionableElementWithActualSize;
var childDockMinWidth = childPositionableModel.CalculatedDockMinWidth();
var childPositionableModelWidthActualSize = _model.Children[i] as ILayoutPositionableElement as ILayoutPositionableElementWithActualSize;
var childDockMinWidth = (_model.Children[i] as ILayoutPositionableElement).CalculatedDockMinWidth();
var widthToSet = Math.Max(childPositionableModelWidthActualSize.ActualWidth, childDockMinWidth);

widthToSet = Math.Min(widthToSet, ActualWidth / 2.0);
widthToSet = Math.Max(widthToSet, childDockMinWidth);

childPositionableModel.DockWidth = new GridLength(widthToSet, GridUnitType.Pixel);
(_model.Children[i] as ILayoutPositionableElement).DockWidth = new GridLength(widthToSet, GridUnitType.Pixel);
}
}
}
Expand All @@ -91,25 +86,21 @@ protected override void OnFixChildrenDockLengths()
{
for (var i = 0; i < _model.Children.Count; i++)
{
var childContainerModel = _model.Children[i] as ILayoutContainer;
var childPositionableModel = _model.Children[i] as ILayoutPositionableElement;

if (childContainerModel != null &&
if (_model.Children[i] is ILayoutContainer childContainerModel &&
(childContainerModel.IsOfType<LayoutDocumentPane, LayoutDocumentPaneGroup>() ||
childContainerModel.ContainsChildOfType<LayoutDocumentPane, LayoutDocumentPaneGroup>()))
{
// Keep set values (from XML for instance)
if(!childPositionableModel.DockHeight.IsStar) childPositionableModel.DockHeight = new GridLength(1.0, GridUnitType.Star);
if(!(_model.Children[i] as ILayoutPositionableElement).DockHeight.IsStar) (_model.Children[i] as ILayoutPositionableElement).DockHeight = new GridLength(1.0, GridUnitType.Star);
}
else if (childPositionableModel != null && childPositionableModel.DockHeight.IsStar)
else if (_model.Children[i] as ILayoutPositionableElement != null && (_model.Children[i] as ILayoutPositionableElement).DockHeight.IsStar)
{
var childPositionableModelWidthActualSize = childPositionableModel as ILayoutPositionableElementWithActualSize;
var childDockMinHeight = childPositionableModel.CalculatedDockMinHeight();
var childPositionableModelWidthActualSize = _model.Children[i] as ILayoutPositionableElement as ILayoutPositionableElementWithActualSize;
var childDockMinHeight = (_model.Children[i] as ILayoutPositionableElement).CalculatedDockMinHeight();
var heightToSet = Math.Max(childPositionableModelWidthActualSize.ActualHeight, childDockMinHeight);
heightToSet = Math.Min(heightToSet, ActualHeight / 2.0);
heightToSet = Math.Max(heightToSet, childDockMinHeight);

childPositionableModel.DockHeight = new GridLength(heightToSet, GridUnitType.Pixel);
(_model.Children[i] as ILayoutPositionableElement).DockHeight = new GridLength(heightToSet, GridUnitType.Pixel);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1907,9 +1907,11 @@ public static NONCLIENTMETRICS VistaMetricsStruct
{
get
{
var ncm = new NONCLIENTMETRICS();
ncm.cbSize = Marshal.SizeOf(typeof(NONCLIENTMETRICS));
return ncm;
var ncm = new NONCLIENTMETRICS
{
cbSize = Marshal.SizeOf(typeof(NONCLIENTMETRICS))
};
return ncm;
}
}

Expand All @@ -1918,10 +1920,12 @@ public static NONCLIENTMETRICS XPMetricsStruct
{
get
{
var ncm = new NONCLIENTMETRICS();
// Account for the missing iPaddedBorderWidth
ncm.cbSize = Marshal.SizeOf(typeof(NONCLIENTMETRICS)) - sizeof(int);
return ncm;
var ncm = new NONCLIENTMETRICS
{
// Account for the missing iPaddedBorderWidth
cbSize = Marshal.SizeOf(typeof(NONCLIENTMETRICS)) - sizeof(int)
};
return ncm;
}
}
}
Expand Down
9 changes: 4 additions & 5 deletions source/Components/AvalonDock/Layout/LayoutDocumentPane.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class LayoutDocumentPane : LayoutPositionableGroup<LayoutContent>, ILayou
string _id;

[XmlIgnore]
bool _autoFixSelectedContent = true;
readonly bool _autoFixSelectedContent = true;
#endregion fields

#region Constructors
Expand Down Expand Up @@ -111,7 +111,7 @@ public IEnumerable<LayoutContent> ChildrenSorted
protected override bool GetVisibility()
{
if (Parent is LayoutDocumentPaneGroup)
return ChildrenCount > 0 && Children.Any(c => c is LayoutDocument && ((LayoutDocument)c).IsVisible || c is LayoutAnchorable);
return ChildrenCount > 0 && Children.Any(c => (c is LayoutDocument document && document.IsVisible) || c is LayoutAnchorable);
return true;
}

Expand Down Expand Up @@ -200,9 +200,8 @@ public override void ConsoleDump(int tab)
/// <returns></returns>
public int IndexOf(LayoutContent content)
{
var documentChild = content as LayoutDocument;
return documentChild == null ? -1 : Children.IndexOf(documentChild);
}
return !(content is LayoutDocument documentChild) ? -1 : Children.IndexOf(documentChild);
}

/// <summary>Invalidates the current <see cref="SelectedContentIndex"/> and sets the index for the next avialable child with IsEnabled == true.</summary>
internal void SetNextSelectedIndex()
Expand Down
2 changes: 1 addition & 1 deletion source/Components/AvalonDock/Layout/LayoutGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace AvalonDock.Layout
public abstract class LayoutGroup<T> : LayoutGroupBase, ILayoutGroup, IXmlSerializable where T : class, ILayoutElement
{
#region fields
private ObservableCollection<T> _children = new ObservableCollection<T>();
private readonly ObservableCollection<T> _children = new ObservableCollection<T>();
private bool _isVisible = true;
#endregion fields

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public string DisplayName
internal class WorkSpaceViewModel : MLibTest.ViewModels.Base.ViewModelBase, IWorkSpaceViewModel
{
#region fields
private ObservableCollection<DocumentViewModel> _files = new ObservableCollection<DocumentViewModel>();
private readonly ObservableCollection<DocumentViewModel> _files = new ObservableCollection<DocumentViewModel>();
private ToolViewModel[] _tools = null;

private ICommand _openCommand = null;
Expand Down Expand Up @@ -138,9 +138,8 @@ public DocumentViewModel ActiveDocument
_activeDocument = value;
RaisePropertyChanged(nameof(ActiveDocument));

if (ActiveDocumentChanged != null)
ActiveDocumentChanged(this, EventArgs.Empty);
}
ActiveDocumentChanged?.Invoke(this, EventArgs.Empty);
}
}
}

Expand Down
8 changes: 3 additions & 5 deletions source/MLibTest/MLibTest/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,10 @@ internal void OnLayoutLoaded_Event(object sender, LayoutLoadedEventArgs layoutLo

internal async void OnLoadLayoutAsync(object parameter = null)
{
AppViewModel wspace = this.DataContext as AppViewModel;

if (wspace != null)
wspace.CloseAllDocuments();
if (this.DataContext is AppViewModel wspace)
wspace.CloseAllDocuments();

App myApp = (App)Application.Current;
App myApp = (App)Application.Current;

LayoutLoaderResult LoaderResult = await myApp.LayoutLoaded.GetLayoutString(OnLayoutLoaded_Event);

Expand Down

0 comments on commit 5032524

Please sign in to comment.