Skip to content

Commit 2dc993c

Browse files
authored
Merge pull request #3503 from MahApps/feature/TitleBar_Overflow
Improvement for title bar and window commands
2 parents 3f782d7 + 1a234ca commit 2dc993c

File tree

4 files changed

+298
-119
lines changed

4 files changed

+298
-119
lines changed

src/MahApps.Metro/Controls/MetroWindow.cs

+6-3
Original file line numberDiff line numberDiff line change
@@ -1030,7 +1030,8 @@ private void MetroWindow_SizeChanged(object sender, RoutedEventArgs e)
10301030
// Half of this MetroWindow
10311031
var halfDistance = this.ActualWidth / 2;
10321032
// Distance between center and left/right
1033-
var distanceToCenter = this.titleBar.DesiredSize.Width / 2;
1033+
var margin = (Thickness)this.titleBar.GetValue(MarginProperty);
1034+
var distanceToCenter = (this.titleBar.DesiredSize.Width - margin.Left - margin.Right) / 2;
10341035
// Distance between right edge from LeftWindowCommands to left window side
10351036
var distanceFromLeft = this.icon.ActualWidth + this.LeftWindowCommands.ActualWidth;
10361037
// Distance between left edge from RightWindowCommands to right window side
@@ -1042,12 +1043,14 @@ private void MetroWindow_SizeChanged(object sender, RoutedEventArgs e)
10421043
var dRight = distanceFromRight + distanceToCenter + horizontalMargin;
10431044
if ((dLeft < halfDistance) && (dRight < halfDistance))
10441045
{
1046+
this.titleBar.SetCurrentValue(MarginProperty, default(Thickness));
10451047
Grid.SetColumn(this.titleBar, 0);
1046-
Grid.SetColumnSpan(this.titleBar, 7);
1048+
Grid.SetColumnSpan(this.titleBar, 5);
10471049
}
10481050
else
10491051
{
1050-
Grid.SetColumn(this.titleBar, 3);
1052+
this.titleBar.SetCurrentValue(MarginProperty, new Thickness(this.LeftWindowCommands.ActualWidth, 0, this.RightWindowCommands.ActualWidth, 0));
1053+
Grid.SetColumn(this.titleBar, 2);
10511054
Grid.SetColumnSpan(this.titleBar, 1);
10521055
}
10531056
}

src/MahApps.Metro/Controls/WindowCommands.cs

+39-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
namespace MahApps.Metro.Controls
1212
{
1313
[StyleTypedProperty(Property = "ItemContainerStyle", StyleTargetType = typeof(WindowCommands))]
14-
public class WindowCommands : ItemsControl, INotifyPropertyChanged
14+
public class WindowCommands : ToolBar, INotifyPropertyChanged
1515
{
1616
public static readonly DependencyProperty ThemeProperty =
1717
DependencyProperty.Register("Theme", typeof(Theme), typeof(WindowCommands),
@@ -270,6 +270,13 @@ private IEnumerable<WindowCommandsItem> GetWindowCommandsItems()
270270
private void WindowCommands_Loaded(object sender, RoutedEventArgs e)
271271
{
272272
this.Loaded -= WindowCommands_Loaded;
273+
274+
var contentPresenter = this.TryFindParent<ContentPresenter>();
275+
if (contentPresenter != null)
276+
{
277+
this.SetCurrentValue(DockPanel.DockProperty, contentPresenter.GetValue(DockPanel.DockProperty));
278+
}
279+
273280
var parentWindow = this.ParentWindow;
274281
if (null == parentWindow)
275282
{
@@ -312,21 +319,48 @@ public class WindowCommandsItem : ContentControl
312319
internal PropertyChangeNotifier VisibilityPropertyChangeNotifier { get; set; }
313320

314321
public static readonly DependencyProperty IsSeparatorVisibleProperty =
315-
DependencyProperty.Register("IsSeparatorVisible", typeof(bool), typeof(WindowCommandsItem),
316-
new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.Inherits|FrameworkPropertyMetadataOptions.AffectsArrange | FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender));
322+
DependencyProperty.Register(
323+
nameof(IsSeparatorVisible),
324+
typeof(bool),
325+
typeof(WindowCommandsItem),
326+
new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.Inherits | FrameworkPropertyMetadataOptions.AffectsArrange | FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender));
317327

318328
/// <summary>
319329
/// Gets or sets the value indicating whether to show the separator.
320330
/// </summary>
321331
public bool IsSeparatorVisible
322332
{
323-
get { return (bool)GetValue(IsSeparatorVisibleProperty); }
324-
set { SetValue(IsSeparatorVisibleProperty, value); }
333+
get { return (bool)this.GetValue(IsSeparatorVisibleProperty); }
334+
set { this.SetValue(IsSeparatorVisibleProperty, value); }
335+
}
336+
337+
public static readonly DependencyPropertyKey ParentWindowCommandsPropertyKey =
338+
DependencyProperty.RegisterReadOnly(
339+
nameof(ParentWindowCommands),
340+
typeof(WindowCommands),
341+
typeof(WindowCommandsItem),
342+
new PropertyMetadata(null));
343+
344+
public static readonly DependencyProperty ParentWindowCommandsProperty = ParentWindowCommandsPropertyKey.DependencyProperty;
345+
346+
public WindowCommands ParentWindowCommands
347+
{
348+
get { return (WindowCommands)this.GetValue(ParentWindowCommandsProperty); }
349+
private set { this.SetValue(ParentWindowCommandsPropertyKey, value); }
325350
}
326351

327352
static WindowCommandsItem()
328353
{
329354
DefaultStyleKeyProperty.OverrideMetadata(typeof(WindowCommandsItem), new FrameworkPropertyMetadata(typeof(WindowCommandsItem)));
330355
}
356+
357+
/// <inheritdoc />
358+
public override void OnApplyTemplate()
359+
{
360+
base.OnApplyTemplate();
361+
362+
var windowCommands = ItemsControl.ItemsControlFromItemContainer(this) as WindowCommands;
363+
this.SetValue(WindowCommandsItem.ParentWindowCommandsPropertyKey, windowCommands);
364+
}
331365
}
332366
}

0 commit comments

Comments
 (0)