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

Fixed titlebar behavior #259

Merged
1 commit merged into from
Dec 16, 2012
Merged
Changes from all commits
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
93 changes: 49 additions & 44 deletions MahApps.Metro/Controls/MetroWindow.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
Expand All @@ -16,8 +14,6 @@ public class MetroWindow : Window
{
private const string PART_TitleBar = "PART_TitleBar";
private const string PART_WindowCommands = "PART_WindowCommands";
private readonly int doubleclick = UnsafeNativeMethods.GetDoubleClickTime();
private DateTime lastMouseClick;

public static readonly DependencyProperty ShowIconOnTitleBarProperty = DependencyProperty.Register("ShowIconOnTitleBar", typeof(bool), typeof(MetroWindow), new PropertyMetadata(true));
public static readonly DependencyProperty ShowTitleBarProperty = DependencyProperty.Register("ShowTitleBar", typeof(bool), typeof(MetroWindow), new PropertyMetadata(true));
Expand All @@ -29,9 +25,13 @@ public class MetroWindow : Window
public static readonly DependencyProperty SavePositionProperty = DependencyProperty.Register("SaveWindowPosition", typeof(bool), typeof(MetroWindow), new PropertyMetadata(false));
public static readonly DependencyProperty TitleForegroundProperty = DependencyProperty.Register("TitleForeground", typeof(Brush), typeof(MetroWindow));
public static readonly DependencyProperty IgnoreTaskbarOnMaximizeProperty = DependencyProperty.Register("IgnoreTaskbar", typeof(bool), typeof(MetroWindow), new PropertyMetadata(false));


bool isDragging;

public ObservableCollection<Flyout> Flyouts { get; set; }

public WindowCommands WindowCommands { get; set; }

public bool IgnoreTaskbarOnMaximize
{
get { return (bool)this.GetValue(IgnoreTaskbarOnMaximizeProperty); }
Expand All @@ -50,18 +50,6 @@ public bool SaveWindowPosition
set { SetValue(SavePositionProperty, value); }
}

public MetroWindow()
{
if (Flyouts == null)
Flyouts = new ObservableCollection<Flyout>();
}
static MetroWindow()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MetroWindow), new FrameworkPropertyMetadata(typeof(MetroWindow)));
}

public WindowCommands WindowCommands { get; set; }

public bool ShowIconOnTitleBar
{
get { return (bool)GetValue(ShowIconOnTitleBarProperty); }
Expand Down Expand Up @@ -109,6 +97,16 @@ public string WindowTitle
get { return TitleCaps ? Title.ToUpper() : Title; }
}

public MetroWindow()
{
Flyouts = new ObservableCollection<Flyout>();
}

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

public override void OnApplyTemplate()
{
base.OnApplyTemplate();
Expand Down Expand Up @@ -142,48 +140,55 @@ protected override void OnStateChanged(EventArgs e)

protected void TitleBarMouseDown(object sender, MouseButtonEventArgs e)
{
if (e.RightButton != MouseButtonState.Pressed && e.MiddleButton != MouseButtonState.Pressed && e.LeftButton == MouseButtonState.Pressed)
DragMove();
var mousePosition = e.GetPosition(this);
bool isIconClick = ShowIconOnTitleBar && mousePosition.X <= TitlebarHeight && mousePosition.Y <= TitlebarHeight;

if (e.ClickCount == 2 && (ResizeMode == ResizeMode.CanResizeWithGrip || ResizeMode == ResizeMode.CanResize))
if (e.ChangedButton == MouseButton.Left)
{
WindowState = WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
}
}

protected void TitleBarMouseUp(object sender, MouseButtonEventArgs e)
{
if (!ShowIconOnTitleBar) return;
var mousePosition = GetCorrectPosition(this);

if (mousePosition.X <= TitlebarHeight && mousePosition.Y <= TitlebarHeight)
{
if ((DateTime.Now - lastMouseClick).TotalMilliseconds <= doubleclick)
if (isIconClick)
{
Close();
return;
if (e.ClickCount == 2)
{
Close();
}
else
{
ShowSystemMenuPhysicalCoordinates(this, PointToScreen(new Point(0, TitlebarHeight)));
}
}
else
{
if (e.ClickCount == 1)
{
isDragging = true;
DragMove();
}
else if (e.ClickCount == 2 && (ResizeMode == ResizeMode.CanResizeWithGrip || ResizeMode == ResizeMode.CanResize))
{
WindowState = WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
}
}
lastMouseClick = DateTime.Now;

ShowSystemMenuPhysicalCoordinates(this, PointToScreen(new Point(0, TitlebarHeight)));
}
else if (e.ChangedButton == MouseButton.Right)
{
ShowSystemMenuPhysicalCoordinates(this, PointToScreen(GetCorrectPosition(this)));
ShowSystemMenuPhysicalCoordinates(this, PointToScreen(mousePosition));
}
}

private static Point GetCorrectPosition(Visual relativeTo)
protected void TitleBarMouseUp(object sender, MouseButtonEventArgs e)
{
UnsafeNativeMethods.Win32Point w32Mouse;
UnsafeNativeMethods.GetCursorPos(out w32Mouse);
return relativeTo.PointFromScreen(new Point(w32Mouse.X, w32Mouse.Y));
isDragging = false;
}

private void TitleBarMouseMove(object sender, MouseEventArgs e)
{
if (e.RightButton != MouseButtonState.Pressed && e.MiddleButton != MouseButtonState.Pressed
&& e.LeftButton == MouseButtonState.Pressed && WindowState == WindowState.Maximized
if (e.LeftButton != MouseButtonState.Pressed)
{
isDragging = false;
}

if (isDragging
&& WindowState == WindowState.Maximized
&& ResizeMode != ResizeMode.NoResize)
{
// Calculating correct left coordinate for multi-screen system.
Expand Down