Skip to content

Commit

Permalink
Merge pull request #3503 from MahApps/feature/TitleBar_Overflow
Browse files Browse the repository at this point in the history
Improvement for title bar and window commands
  • Loading branch information
punker76 authored May 24, 2019
2 parents 3f782d7 + 1a234ca commit 2dc993c
Show file tree
Hide file tree
Showing 4 changed files with 298 additions and 119 deletions.
9 changes: 6 additions & 3 deletions src/MahApps.Metro/Controls/MetroWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1030,7 +1030,8 @@ private void MetroWindow_SizeChanged(object sender, RoutedEventArgs e)
// Half of this MetroWindow
var halfDistance = this.ActualWidth / 2;
// Distance between center and left/right
var distanceToCenter = this.titleBar.DesiredSize.Width / 2;
var margin = (Thickness)this.titleBar.GetValue(MarginProperty);
var distanceToCenter = (this.titleBar.DesiredSize.Width - margin.Left - margin.Right) / 2;
// Distance between right edge from LeftWindowCommands to left window side
var distanceFromLeft = this.icon.ActualWidth + this.LeftWindowCommands.ActualWidth;
// Distance between left edge from RightWindowCommands to right window side
Expand All @@ -1042,12 +1043,14 @@ private void MetroWindow_SizeChanged(object sender, RoutedEventArgs e)
var dRight = distanceFromRight + distanceToCenter + horizontalMargin;
if ((dLeft < halfDistance) && (dRight < halfDistance))
{
this.titleBar.SetCurrentValue(MarginProperty, default(Thickness));
Grid.SetColumn(this.titleBar, 0);
Grid.SetColumnSpan(this.titleBar, 7);
Grid.SetColumnSpan(this.titleBar, 5);
}
else
{
Grid.SetColumn(this.titleBar, 3);
this.titleBar.SetCurrentValue(MarginProperty, new Thickness(this.LeftWindowCommands.ActualWidth, 0, this.RightWindowCommands.ActualWidth, 0));
Grid.SetColumn(this.titleBar, 2);
Grid.SetColumnSpan(this.titleBar, 1);
}
}
Expand Down
44 changes: 39 additions & 5 deletions src/MahApps.Metro/Controls/WindowCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace MahApps.Metro.Controls
{
[StyleTypedProperty(Property = "ItemContainerStyle", StyleTargetType = typeof(WindowCommands))]
public class WindowCommands : ItemsControl, INotifyPropertyChanged
public class WindowCommands : ToolBar, INotifyPropertyChanged
{
public static readonly DependencyProperty ThemeProperty =
DependencyProperty.Register("Theme", typeof(Theme), typeof(WindowCommands),
Expand Down Expand Up @@ -270,6 +270,13 @@ private IEnumerable<WindowCommandsItem> GetWindowCommandsItems()
private void WindowCommands_Loaded(object sender, RoutedEventArgs e)
{
this.Loaded -= WindowCommands_Loaded;

var contentPresenter = this.TryFindParent<ContentPresenter>();
if (contentPresenter != null)
{
this.SetCurrentValue(DockPanel.DockProperty, contentPresenter.GetValue(DockPanel.DockProperty));
}

var parentWindow = this.ParentWindow;
if (null == parentWindow)
{
Expand Down Expand Up @@ -312,21 +319,48 @@ public class WindowCommandsItem : ContentControl
internal PropertyChangeNotifier VisibilityPropertyChangeNotifier { get; set; }

public static readonly DependencyProperty IsSeparatorVisibleProperty =
DependencyProperty.Register("IsSeparatorVisible", typeof(bool), typeof(WindowCommandsItem),
new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.Inherits|FrameworkPropertyMetadataOptions.AffectsArrange | FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender));
DependencyProperty.Register(
nameof(IsSeparatorVisible),
typeof(bool),
typeof(WindowCommandsItem),
new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.Inherits | FrameworkPropertyMetadataOptions.AffectsArrange | FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender));

/// <summary>
/// Gets or sets the value indicating whether to show the separator.
/// </summary>
public bool IsSeparatorVisible
{
get { return (bool)GetValue(IsSeparatorVisibleProperty); }
set { SetValue(IsSeparatorVisibleProperty, value); }
get { return (bool)this.GetValue(IsSeparatorVisibleProperty); }
set { this.SetValue(IsSeparatorVisibleProperty, value); }
}

public static readonly DependencyPropertyKey ParentWindowCommandsPropertyKey =
DependencyProperty.RegisterReadOnly(
nameof(ParentWindowCommands),
typeof(WindowCommands),
typeof(WindowCommandsItem),
new PropertyMetadata(null));

public static readonly DependencyProperty ParentWindowCommandsProperty = ParentWindowCommandsPropertyKey.DependencyProperty;

public WindowCommands ParentWindowCommands
{
get { return (WindowCommands)this.GetValue(ParentWindowCommandsProperty); }
private set { this.SetValue(ParentWindowCommandsPropertyKey, value); }
}

static WindowCommandsItem()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(WindowCommandsItem), new FrameworkPropertyMetadata(typeof(WindowCommandsItem)));
}

/// <inheritdoc />
public override void OnApplyTemplate()
{
base.OnApplyTemplate();

var windowCommands = ItemsControl.ItemsControlFromItemContainer(this) as WindowCommands;
this.SetValue(WindowCommandsItem.ParentWindowCommandsPropertyKey, windowCommands);
}
}
}
Loading

0 comments on commit 2dc993c

Please sign in to comment.