Skip to content

Commit

Permalink
Improve and optimize the active item handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
Wenveo committed Jan 12, 2023
1 parent b63b966 commit fa58dfe
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 174 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -435,14 +435,14 @@ private void OnExecuteCloseWindowCommand(object parameter)

#region ActiveItem

private void ActiveItemOfSinglePane(bool isActive)
internal void ActiveItemOfSinglePane(bool isActive)
{
var layoutAnchorablePane = _model.Descendents().OfType<LayoutAnchorablePane>()
var pane = _model.Descendents().OfType<LayoutAnchorablePane>()
.FirstOrDefault(p => p.ChildrenCount > 0 && p.SelectedContent != null);

if (layoutAnchorablePane != null)
if (pane != null)
{
layoutAnchorablePane.SelectedContent.IsActive = isActive;
pane.SelectedContent.IsActive = isActive;
}
// When the floating tool window is mixed with the floating document window
// and the document pane in the floating document window is dragged out.
Expand All @@ -457,90 +457,22 @@ private void ActiveItemOfSinglePane(bool isActive)
}
}

private LayoutAnchorablePaneControl FindPaneControlByMousePoint()
{
var mousePosition = Win32Helper.GetMousePosition();
var rootVisual = ((FloatingWindowContentHost)Content).RootVisual;
var areaHosts = rootVisual.FindVisualChildren<LayoutAnchorablePaneControl>();

foreach (var areaHost in areaHosts)
{
var rect = areaHost.GetScreenArea();
var pos = areaHost.TransformFromDeviceDPI(mousePosition);
var b = rect.Contains(pos);

if (b)
{
return areaHost;
}
}

return null;
}

private static void ActiveTheLastActivedItemOfPane(LayoutAnchorablePane pane)
{
if (pane.Children.Count > 0)
{
var index = 0;
if (pane.Children.Count > 1)
{
var tmTimeStamp = pane.Children[0].LastActivationTimeStamp;
for (var i = 1; i < pane.Children.Count; i++)
{
var item = pane.Children[i];
if (item.LastActivationTimeStamp > tmTimeStamp)
{
tmTimeStamp = item.LastActivationTimeStamp;
index = i;
}
}
}

pane.SelectedContentIndex = index;
}
}

private void ActiveTheLastActivedItemOfItems(bool isActive)
{
var items = _model.Descendents().OfType<LayoutContent>().ToList();
if (items.Count > 0)
{
var index = 0;
if (items.Count > 1)
{
var tmpTimeStamp2 = items[0].LastActivationTimeStamp;
for (var i = 1; i < items.Count; i++)
{
var item = items[i];
if (item.LastActivationTimeStamp > tmpTimeStamp2)
{
tmpTimeStamp2 = item.LastActivationTimeStamp;
index = i;
}
}
}

items[index].IsActive = isActive;
}
}

private void ActiveItemOfMultiPane(bool isActive)
internal void ActiveItemOfMultiPane(bool isActive)
{
if (isActive)
{
var anchorablePane = FindPaneControlByMousePoint();
if (anchorablePane != null)
var paneControl = FindPaneControlByMousePoint<LayoutAnchorablePaneControl>();
if (paneControl != null)
{
var model = (LayoutAnchorablePane)anchorablePane.Model;
var model = (LayoutAnchorablePane)paneControl.Model;
if (model.SelectedContent != null)
{
model.SelectedContent.IsActive = true;
return;
}
else
{
ActiveTheLastActivedItemOfPane(model);
ActiveTheLastActivedItemOfPane<LayoutAnchorablePane, LayoutAnchorable>(model);
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -434,14 +434,14 @@ private void OnExecuteCloseWindowCommand(object parameter)

#region ActiveItem

private void ActiveItemOfSinglePane(bool isActive)
internal void ActiveItemOfSinglePane(bool isActive)
{
var layoutDocumentPane = _model.Descendents().OfType<LayoutDocumentPane>()
var pane = _model.Descendents().OfType<LayoutDocumentPane>()
.FirstOrDefault(p => p.ChildrenCount > 0 && p.SelectedContent != null);

if (layoutDocumentPane != null)
if (pane != null)
{
layoutDocumentPane.SelectedContent.IsActive = isActive;
pane.SelectedContent.IsActive = isActive;
}
// When the floating tool window is mixed with the floating document window
// and the document pane in the floating document window is dragged out.
Expand All @@ -456,98 +456,30 @@ private void ActiveItemOfSinglePane(bool isActive)
}
}

private LayoutDocumentPaneControl FindPaneControlByMousePoint()
{
var mousePosition = Win32Helper.GetMousePosition();
var rootVisual = ((FloatingWindowContentHost)Content).RootVisual;
var areaHosts = rootVisual.FindVisualChildren<LayoutDocumentPaneControl>();

foreach (var areaHost in areaHosts)
{
var rect = areaHost.GetScreenArea();
var pos = areaHost.TransformFromDeviceDPI(mousePosition);
var b = rect.Contains(pos);

if (b)
{
return areaHost;
}
}

return null;
}

private static void ActiveTheLastActivedItemOfPane(LayoutDocumentPane pane)
{
if (pane.Children.Count > 0)
{
var index = 0;
if (pane.Children.Count > 1)
{
var tmTimeStamp = pane.Children[0].LastActivationTimeStamp;
for (var i = 1; i < pane.Children.Count; i++)
{
var item = pane.Children[i];
if (item.LastActivationTimeStamp > tmTimeStamp)
{
tmTimeStamp = item.LastActivationTimeStamp;
index = i;
}
}
}

pane.SelectedContentIndex = index;
}
}

private void ActiveTheLastActivedItemOfItems(bool isActive)
{
var items = _model.Descendents().OfType<LayoutContent>().ToList();
if (items.Count > 0)
{
var index = 0;
if (items.Count > 1)
{
var tmpTimeStamp2 = items[0].LastActivationTimeStamp;
for (var i = 1; i < items.Count; i++)
{
var item = items[i];
if (item.LastActivationTimeStamp > tmpTimeStamp2)
{
tmpTimeStamp2 = item.LastActivationTimeStamp;
index = i;
}
}
}

items[index].IsActive = isActive;
}
}

private void ActiveItemOfMultiPane(bool isActive)
internal void ActiveItemOfMultiPane(bool isActive)
{
if (isActive)
{
var documentPane = FindPaneControlByMousePoint();
if (documentPane != null)
var paneControl = FindPaneControlByMousePoint<LayoutDocumentPaneControl>();
if (paneControl != null)
{
var model = (LayoutDocumentPane)documentPane.Model;
var model = (LayoutDocumentPane)paneControl.Model;
if (model.SelectedContent != null)
{
model.SelectedContent.IsActive = true;
return;
}
else
{
ActiveTheLastActivedItemOfPane(model);
ActiveTheLastActivedItemOfPane<LayoutDocumentPane, LayoutContent>(model);
return;
}
}
}
ActiveTheLastActivedItemOfItems(isActive);
}

#endregion
#endregion ActiveItem

#endregion Private Methods
}
Expand Down
108 changes: 89 additions & 19 deletions source/Components/AvalonDock/Controls/LayoutFloatingWindowControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ This program is provided to you under the terms of the Microsoft Public
License (Ms-PL) as published at https://opensource.org/licenses/MS-PL
************************************************************************/

using AvalonDock.Layout;
using AvalonDock.Themes;
using System;
using System.ComponentModel;
using System.Linq;
Expand All @@ -22,6 +20,9 @@ This program is provided to you under the terms of the Microsoft Public
using System.Windows.Interop;
using System.Windows.Media;

using AvalonDock.Layout;
using AvalonDock.Themes;

namespace AvalonDock.Controls
{
/// <inheritdoc cref="Window"/>
Expand Down Expand Up @@ -52,7 +53,7 @@ public abstract class LayoutFloatingWindowControl : Window, ILayoutControl
/// </summary>
/// <see cref="TotalMargin"/>
private bool _isTotalMarginSet = false;

#endregion fields

#region Constructors
Expand Down Expand Up @@ -491,6 +492,75 @@ internal void InternalClose(bool closeInitiatedByUser = false)
Close();
}

internal T FindPaneControlByMousePoint<T>() where T : FrameworkElement, ILayoutControl
{
var mousePosition = this.PointToScreenDPI(Mouse.GetPosition(this));
var rootVisual = ((FloatingWindowContentHost)Content).RootVisual;
var areaHosts = rootVisual.FindVisualChildren<T>();

foreach (var areaHost in areaHosts)
{
var rect = areaHost.GetScreenArea();
var b = rect.Contains(mousePosition);

if (b)
{
return areaHost;
}
}

return null;
}

internal void ActiveTheLastActivedItemOfItems(bool isActive)
{
var items = _model.Descendents().OfType<LayoutContent>().ToList();
if (items.Count > 0)
{
var index = 0;
if (items.Count > 1)
{
var tmpTimeStamp2 = items[0].LastActivationTimeStamp;
for (var i = 1; i < items.Count; i++)
{
var item = items[i];
if (item.LastActivationTimeStamp > tmpTimeStamp2)
{
tmpTimeStamp2 = item.LastActivationTimeStamp;
index = i;
}
}
}

items[index].IsActive = isActive;
}
}

internal static void ActiveTheLastActivedItemOfPane<TPane, TChild>(TPane pane)
where TPane : LayoutPositionableGroup<TChild>, ILayoutContentSelector
where TChild : LayoutContent
{
if (pane.Children.Count > 0)
{
var index = 0;
if (pane.Children.Count > 1)
{
var tmTimeStamp = pane.Children[0].LastActivationTimeStamp;
for (var i = 1; i < pane.Children.Count; i++)
{
var item = pane.Children[i];
if (item.LastActivationTimeStamp > tmTimeStamp)
{
tmTimeStamp = item.LastActivationTimeStamp;
index = i;
}
}
}

pane.SelectedContentIndex = index;
}
}

#endregion Internal Methods

#region Overrides
Expand Down Expand Up @@ -528,24 +598,24 @@ protected override void OnInitialized(EventArgs e)
}

protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(e);
AssureOwnerIsNotMinimized();
}
{
base.OnClosing(e);
AssureOwnerIsNotMinimized();
}

/// <summary>
/// <summary>
/// Prevents a known bug in WPF, which wronlgy minimizes the parent window, when closing this control
/// </summary>
private void AssureOwnerIsNotMinimized()
{
try
{
Owner?.Activate();
}
catch (Exception)
{
}
}
/// </summary>
private void AssureOwnerIsNotMinimized()
{
try
{
Owner?.Activate();
}
catch (Exception)
{
}
}

#endregion Overrides

Expand Down

0 comments on commit fa58dfe

Please sign in to comment.