Skip to content

Commit

Permalink
Merge branch 'pr/n276_blooksa'
Browse files Browse the repository at this point in the history
Conflicts:
	MahApps.Metro/Controls/MetroWindow.cs
	MahApps.Metro/Controls/TextboxHelper.cs
	MahApps.Metro/Properties/AssemblyInfo.cs
	MahApps.Metro/Styles/Accents/BaseDark.xaml
	MahApps.Metro/Styles/Accents/BaseLight.xaml
	MetroDemo/MainWindow.xaml
	MetroDemo/MainWindow.xaml.cs
  • Loading branch information
vikingcode committed Dec 27, 2012
2 parents 1ac0076 + 58845a4 commit 8eda481
Show file tree
Hide file tree
Showing 11 changed files with 205 additions and 108 deletions.
91 changes: 42 additions & 49 deletions MahApps.Metro/Controls/MetroWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ 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 @@ -26,16 +28,12 @@ public class MetroWindow : Window
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); }
set{ SetValue(IgnoreTaskbarOnMaximizeProperty, value); }
set { SetValue(IgnoreTaskbarOnMaximizeProperty, value); }
}

public Brush TitleForeground
Expand All @@ -50,6 +48,18 @@ 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 @@ -97,16 +107,6 @@ 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 @@ -140,55 +140,48 @@ protected override void OnStateChanged(EventArgs e)

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

if (e.ChangedButton == MouseButton.Left)
if (e.ClickCount == 2 && (ResizeMode == ResizeMode.CanResizeWithGrip || ResizeMode == ResizeMode.CanResize))
{
if (isIconClick)
{
if (e.ClickCount == 2)
{
Close();
}
else
{
ShowSystemMenuPhysicalCoordinates(this, PointToScreen(new Point(0, TitlebarHeight)));
}
}
else
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 (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;
}
Close();
return;
}
lastMouseClick = DateTime.Now;

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

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

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

if (isDragging
&& WindowState == WindowState.Maximized
if (e.RightButton != MouseButtonState.Pressed && e.MiddleButton != MouseButtonState.Pressed
&& e.LeftButton == MouseButtonState.Pressed && WindowState == WindowState.Maximized
&& ResizeMode != ResizeMode.NoResize)
{
// Calculating correct left coordinate for multi-screen system.
Expand Down
31 changes: 18 additions & 13 deletions MahApps.Metro/Controls/ProgressIndicator.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.ComponentModel;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
Expand Down Expand Up @@ -27,7 +28,9 @@ public ProgressIndicator()
{
InitializeComponent();
this.DataContext = this;
IsVisibleChanged += StartStopAnimation;
IsVisibleChanged += (s, e) => ((ProgressIndicator)s).StartStopAnimation();
DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(VisibilityProperty, GetType());
dpd.AddValueChanged(this, (s, e) => ((ProgressIndicator)s).StartStopAnimation());
}

public static readonly DependencyProperty ProgressColourProperty = DependencyProperty.RegisterAttached("ProgressColour", typeof(Brush), typeof(ProgressIndicator), new UIPropertyMetadata(null));
Expand All @@ -38,18 +41,20 @@ public Brush ProgressColour
set { SetValue(ProgressColourProperty, value); }
}

private void StartStopAnimation(object sender, DependencyPropertyChangedEventArgs e)
private void StartStopAnimation()
{
this.Dispatcher.BeginInvoke(new Action(() =>
{
var s = this.Resources["animate"] as Storyboard;
if ((bool) e.NewValue)
s.Begin();
else
s.Stop();

})
);
bool shouldAnimate = (Visibility == Visibility.Visible && IsVisible);
Dispatcher.BeginInvoke(new Action(() =>
{
var s = Resources["animate"] as Storyboard;
if (s != null)
{
if (shouldAnimate)
s.Begin();
else
s.Stop();
}
}));
}
}
}
}
122 changes: 116 additions & 6 deletions MahApps.Metro/Controls/TextboxHelper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
Expand All @@ -13,9 +12,21 @@ public class TextboxHelper : DependencyObject
{
public static readonly DependencyProperty IsMonitoringProperty = DependencyProperty.RegisterAttached("IsMonitoring", typeof(bool), typeof(TextboxHelper), new UIPropertyMetadata(false, OnIsMonitoringChanged));
public static readonly DependencyProperty WatermarkProperty = DependencyProperty.RegisterAttached("Watermark", typeof(string), typeof(TextboxHelper), new UIPropertyMetadata(string.Empty));
private static readonly DependencyProperty HasTextProperty = DependencyProperty.RegisterAttached("HasText", typeof(bool), typeof(TextboxHelper), new FrameworkPropertyMetadata(false));
public static readonly DependencyProperty TextLengthProperty = DependencyProperty.RegisterAttached("TextLength", typeof(int), typeof(TextboxHelper), new UIPropertyMetadata(0));
public static readonly DependencyProperty ClearTextButtonProperty = DependencyProperty.RegisterAttached("ClearTextButton", typeof(bool), typeof(TextboxHelper), new FrameworkPropertyMetadata(false, ClearTextChanged));
public static readonly DependencyProperty SelectAllOnFocusProperty = DependencyProperty.RegisterAttached("SelectAllOnFocus", typeof(bool), typeof(TextboxHelper), new FrameworkPropertyMetadata(false));

private static readonly DependencyProperty hasTextProperty = DependencyProperty.RegisterAttached("HasText", typeof(bool), typeof(TextboxHelper), new FrameworkPropertyMetadata(false));

public static void SetSelectAllOnFocus(DependencyObject obj, bool value)
{
obj.SetValue(SelectAllOnFocusProperty, value);
}

public static bool GetSelectAllOnFocus(DependencyObject obj)
{
return (bool)obj.GetValue(SelectAllOnFocusProperty);
}

public static void SetIsMonitoring(DependencyObject obj, bool value)
{
Expand All @@ -35,12 +46,12 @@ public static void SetWatermark(DependencyObject obj, string value)
private static void SetTextLength(DependencyObject obj, int value)
{
obj.SetValue(TextLengthProperty, value);
obj.SetValue(HasTextProperty, value >= 1);
obj.SetValue(hasTextProperty, value >= 1);
}

public bool HasText
{
get { return (bool)GetValue(HasTextProperty); }
get { return (bool)GetValue(hasTextProperty); }
}

static void OnIsMonitoringChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
Expand All @@ -50,18 +61,30 @@ static void OnIsMonitoringChanged(DependencyObject d, DependencyPropertyChangedE
var txtBox = d as TextBox;

if ((bool)e.NewValue)
{
txtBox.TextChanged += TextChanged;
txtBox.GotFocus += TextBoxGotFocus;
}
else
{
txtBox.TextChanged -= TextChanged;
txtBox.GotFocus -= TextBoxGotFocus;
}
}
else if (d is PasswordBox)
{
var passBox = d as PasswordBox;

if ((bool)e.NewValue)
{
passBox.PasswordChanged += PasswordChanged;
passBox.GotFocus += PasswordGotFocus;
}
else
{
passBox.PasswordChanged -= PasswordChanged;
passBox.GotFocus -= PasswordGotFocus;
}
}
}

Expand All @@ -81,6 +104,28 @@ static void PasswordChanged(object sender, RoutedEventArgs e)
SetTextLength(passBox, passBox.Password.Length);
}

static void TextBoxGotFocus(object sender, RoutedEventArgs e)
{
var txtBox = sender as TextBox;
if (txtBox == null)
return;
if (GetSelectAllOnFocus(txtBox))
{
txtBox.Dispatcher.BeginInvoke((Action)(txtBox.SelectAll));
}
}

static void PasswordGotFocus(object sender, RoutedEventArgs e)
{
var passBox = sender as PasswordBox;
if (passBox == null)
return;
if (GetSelectAllOnFocus(passBox))
{
passBox.Dispatcher.BeginInvoke((Action)(passBox.SelectAll));
}
}

public static bool GetClearTextButton(DependencyObject d)
{
return (bool)d.GetValue(ClearTextButtonProperty);
Expand All @@ -95,9 +140,58 @@ private static void ClearTextChanged(DependencyObject d, DependencyPropertyChang
{
var textbox = d as TextBox;
if (textbox != null)
textbox.Loaded += TextBoxLoaded;
{
if ((bool)e.NewValue)
{
textbox.Loaded += TextBoxLoaded;
}
else
{
textbox.Loaded -= TextBoxLoaded;
}
}
var passbox = d as PasswordBox;
if (passbox != null)
{
if ((bool)e.NewValue)
{
passbox.Loaded += PassBoxLoaded;
}
else
{
passbox.Loaded -= PassBoxLoaded;
}
}
}

static void PassBoxLoaded(object sender, RoutedEventArgs e)
{
if (!(sender is PasswordBox))
return;

var passbox = sender as PasswordBox;
if (passbox.Style == null)
return;

var template = passbox.Template;
if (template == null)
return;

var button = template.FindName("PART_ClearText", passbox) as Button;
if (button == null)
return;

if (GetClearTextButton(passbox))
{
button.Click += ClearPassClicked;
}
else
{
button.Click -= ClearPassClicked;
}
}


static void TextBoxLoaded(object sender, RoutedEventArgs e)
{
if (!(sender is TextBox))
Expand All @@ -116,9 +210,13 @@ static void TextBoxLoaded(object sender, RoutedEventArgs e)
return;

if (GetClearTextButton(textbox))
{
button.Click += ClearTextClicked;
}
else
{
button.Click -= ClearTextClicked;
}
}

static void ClearTextClicked(object sender, RoutedEventArgs e)
Expand All @@ -130,7 +228,19 @@ static void ClearTextClicked(object sender, RoutedEventArgs e)
parent = VisualTreeHelper.GetParent(parent);
}

((TextBox)parent).Text = string.Empty;
((TextBox)parent).Clear();
}

static void ClearPassClicked(object sender, RoutedEventArgs e)
{
var button = ((Button)sender);
var parent = VisualTreeHelper.GetParent(button);
while (!(parent is PasswordBox))
{
parent = VisualTreeHelper.GetParent(parent);
}

((PasswordBox)parent).Clear();
}
}

Expand Down
Loading

0 comments on commit 8eda481

Please sign in to comment.