Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto resizing floating window on startup #146

Merged
merged 8 commits into from
Apr 9, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ internal void AttachDrag(bool onActivated = true)
}
}

internal Thickness TotalMargin { get; private set; }
private bool _foundMargin = false;
protected virtual IntPtr FilterMessage(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
handled = false;
Expand All @@ -248,7 +250,9 @@ protected virtual IntPtr FilterMessage(IntPtr hwnd, int msg, IntPtr wParam, IntP
handled = true;
}
}
break;
UpdateWindowsSizeBasedOnMinSize();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the first location where the style was loaded and the grid and border size could be retreived


break;
case Win32Helper.WM_EXITSIZEMOVE:
UpdatePositionAndSizeOfPanes();

Expand Down Expand Up @@ -283,7 +287,54 @@ protected virtual IntPtr FilterMessage(IntPtr hwnd, int msg, IntPtr wParam, IntP
return IntPtr.Zero;
}

internal void InternalClose(bool closeInitiatedByUser = false)
private void UpdateMargins()
{
var grid = this.GetChildrenRecursive()
.OfType<Grid>()
.FirstOrDefault(g => g.RowDefinitions.Count > 0);
if (grid != null)
{
_foundMargin = true;
var margin = grid.Margin;
margin.Top = grid.RowDefinitions[0].MinHeight + grid.Margin.Top;
TotalMargin = margin;
}
}

private void UpdateWindowsSizeBasedOnMinSize()
{
if (!_foundMargin)
{
UpdateMargins();
if (_foundMargin)
{
ContentPresenter contentControl = this.GetChildrenRecursive()
.OfType<ContentPresenter>()
.FirstOrDefault(c => c.Content is LayoutContent);
if (contentControl == null)
return;
var layoutContent = (LayoutContent)contentControl.Content;
if (layoutContent.Content is FrameworkElement content)
{
var parent = VisualTreeHelper.GetParent(content) as FrameworkElement;
// StackPanels among others have an ActualHeight larger than visible, hence we check the parent control as well
if (content.ActualHeight < content.MinHeight ||
parent != null && parent.ActualHeight < content.MinHeight)
{
Height = content.MinHeight + TotalMargin.Top + TotalMargin.Bottom;
}

if (content.ActualWidth < content.MinWidth ||
parent != null && parent.ActualWidth < content.MinWidth)
{
Width = content.MinWidth + TotalMargin.Left + TotalMargin.Right;
}
}
}
}
}

internal void InternalClose(bool closeInitiatedByUser = false)
{
_internalCloseFlag = !closeInitiatedByUser;
if (_isClosing) return;
Expand Down
40 changes: 40 additions & 0 deletions source/Components/AvalonDock/VisualTreeHelperExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/************************************************************************
AvalonDock

Copyright (C) 2020 ????
eriove marked this conversation as resolved.
Show resolved Hide resolved

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 System.Collections.Generic;
using System.Windows;
using System.Windows.Media;

namespace AvalonDock
{
internal static class VisualTreeHelperExtensions
{
public static IEnumerable<DependencyObject> GetChildrenRecursive(this DependencyObject dependencyObject)
{
var children = dependencyObject.GetChildren();
foreach (var child in children)
{
yield return child;
foreach (var c in GetChildrenRecursive(child))
{
yield return c;
}
}
}

public static IEnumerable<DependencyObject> GetChildren(this DependencyObject dependencyObject)
{
int n = VisualTreeHelper.GetChildrenCount(dependencyObject);
for (int i = 0; i < n; i++)
{
yield return VisualTreeHelper.GetChild(dependencyObject, i);
}
}
}
}
3 changes: 2 additions & 1 deletion source/TestApp/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<MenuItem Header="Tools">
<MenuItem Click="OnShowWinformsWindow" Header="WinForms Window" />
<MenuItem Click="OnShowToolWindow1" Header="Tool Window1" />
<MenuItem Click="OnNewFloatingWindow" Header="New floating window"/>
</MenuItem>
</Menu>

Expand Down Expand Up @@ -108,7 +109,7 @@
Title="Tool Window 1"
ContentId="toolWindow1"
Hiding="OnToolWindow1Hiding">
<StackPanel>
<StackPanel MinHeight="1500">
<local:TestUserControl />
<TextBox Text="{Binding TestTimer, Mode=OneWay, StringFormat='Tool Window 1 Attached to Timer ->\{0\}'}" />
</StackPanel>
Expand Down
14 changes: 13 additions & 1 deletion source/TestApp/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,5 +236,17 @@ private void OnShowHeader(object sender, RoutedEventArgs e)
{
//// LayoutDocumentPane.ShowHeader = !LayoutDocumentPane.ShowHeader;
}
}

private void OnNewFloatingWindow(object sender, RoutedEventArgs e)
{
var view = new TestUserControl();
var anchorable = new LayoutAnchorable()
{
Title = "New floating window",
Content = view
};
anchorable.AddToLayout(dockManager,AnchorableShowStrategy.Most);
anchorable.Float();
}
}
}
1 change: 1 addition & 0 deletions source/TestApp/TestUserControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
MinHeight="300" MinWidth="300"
Height="300" Width="300">
<Grid>

Expand Down