Browse Source

Added Button.IsDefault

pull/39/head
Steven Kirk 12 years ago
parent
commit
b116d94c3a
  1. 79
      Perspex.Controls/Button.cs
  2. 19
      Perspex.Interactivity/IInteractive.cs
  3. 11
      TestApplication/Program.cs

79
Perspex.Controls/Button.cs

@ -7,9 +7,12 @@
namespace Perspex.Controls namespace Perspex.Controls
{ {
using System; using System;
using System.Linq;
using System.Windows.Input; using System.Windows.Input;
using Perspex.Input; using Perspex.Input;
using Perspex.Interactivity; using Perspex.Interactivity;
using Perspex.Rendering;
using Perspex.VisualTree;
public enum ClickMode public enum ClickMode
{ {
@ -28,6 +31,9 @@ namespace Perspex.Controls
public static readonly PerspexProperty<object> CommandParameterProperty = public static readonly PerspexProperty<object> CommandParameterProperty =
PerspexProperty.Register<Button, object>("CommandParameter"); PerspexProperty.Register<Button, object>("CommandParameter");
public static readonly PerspexProperty<bool> IsDefaultProperty =
PerspexProperty.Register<Button, bool>("IsDefault");
public static readonly RoutedEvent<RoutedEventArgs> ClickEvent = public static readonly RoutedEvent<RoutedEventArgs> ClickEvent =
RoutedEvent.Register<Button, RoutedEventArgs>("Click", RoutingStrategies.Bubble); RoutedEvent.Register<Button, RoutedEventArgs>("Click", RoutingStrategies.Bubble);
@ -36,6 +42,7 @@ namespace Perspex.Controls
FocusableProperty.OverrideDefaultValue(typeof(Button), true); FocusableProperty.OverrideDefaultValue(typeof(Button), true);
ClickEvent.AddClassHandler<Button>(x => x.OnClick); ClickEvent.AddClassHandler<Button>(x => x.OnClick);
CommandProperty.Changed.Subscribe(CommandChanged); CommandProperty.Changed.Subscribe(CommandChanged);
IsDefaultProperty.Changed.Subscribe(IsDefaultChanged);
} }
public event EventHandler<RoutedEventArgs> Click public event EventHandler<RoutedEventArgs> Click
@ -62,6 +69,12 @@ namespace Perspex.Controls
set { this.SetValue(CommandParameterProperty, value); } set { this.SetValue(CommandParameterProperty, value); }
} }
public bool IsDefault
{
get { return this.GetValue(IsDefaultProperty); }
set { this.SetValue(IsDefaultProperty, value); }
}
protected override Size MeasureOverride(Size availableSize) protected override Size MeasureOverride(Size availableSize)
{ {
return base.MeasureOverride(availableSize); return base.MeasureOverride(availableSize);
@ -72,6 +85,34 @@ namespace Perspex.Controls
return base.ArrangeOverride(finalSize); return base.ArrangeOverride(finalSize);
} }
protected override void OnAttachedToVisualTree(IRenderRoot root)
{
base.OnAttachedToVisualTree(root);
if (this.IsDefault)
{
var inputElement = root as IInputElement;
if (inputElement != null)
{
this.ListenForDefault(inputElement);
}
}
}
protected override void OnDetachedFromVisualTree(IRenderRoot oldRoot)
{
if (this.IsDefault)
{
var inputElement = oldRoot as IInputElement;
if (inputElement != null)
{
this.StopListeningForDefault(inputElement);
}
}
}
protected virtual void OnClick(RoutedEventArgs e) protected virtual void OnClick(RoutedEventArgs e)
{ {
if (this.Command != null) if (this.Command != null)
@ -131,6 +172,26 @@ namespace Perspex.Controls
} }
} }
private static void IsDefaultChanged(PerspexPropertyChangedEventArgs e)
{
var button = e.Sender as Button;
var isDefault = (bool)e.NewValue;
var root = button.GetSelfAndVisualAncestors().OfType<IRenderRoot>().FirstOrDefault();
var inputElement = root as IInputElement;
if (inputElement != null)
{
if (isDefault)
{
button.ListenForDefault(inputElement);
}
else
{
button.StopListeningForDefault(inputElement);
}
}
}
private void CanExecuteChanged(object sender, EventArgs e) private void CanExecuteChanged(object sender, EventArgs e)
{ {
// HACK: Just set the IsEnabled property for the moment. This needs to be changed to // HACK: Just set the IsEnabled property for the moment. This needs to be changed to
@ -138,6 +199,16 @@ namespace Perspex.Controls
this.IsEnabled = this.Command == null || this.Command.CanExecute(this.CommandParameter); this.IsEnabled = this.Command == null || this.Command.CanExecute(this.CommandParameter);
} }
private void ListenForDefault(IInputElement root)
{
root.AddHandler(InputElement.KeyDownEvent, this.RootKeyDown, RoutingStrategies.Tunnel);
}
private void StopListeningForDefault(IInputElement root)
{
root.RemoveHandler(InputElement.KeyDownEvent, this.RootKeyDown);
}
private void RaiseClickEvent() private void RaiseClickEvent()
{ {
RoutedEventArgs click = new RoutedEventArgs RoutedEventArgs click = new RoutedEventArgs
@ -147,5 +218,13 @@ namespace Perspex.Controls
this.RaiseEvent(click); this.RaiseEvent(click);
} }
private void RootKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter && this.IsVisible && this.IsEnabled)
{
this.RaiseClickEvent();
}
}
} }
} }

19
Perspex.Interactivity/IInteractive.cs

@ -6,8 +6,27 @@
namespace Perspex.Interactivity namespace Perspex.Interactivity
{ {
using System;
public interface IInteractive : IVisual public interface IInteractive : IVisual
{ {
IDisposable AddHandler(
RoutedEvent routedEvent,
Delegate handler,
RoutingStrategies routes = RoutingStrategies.Direct | RoutingStrategies.Bubble,
bool handledEventsToo = false);
IDisposable AddHandler<TEventArgs>(
RoutedEvent<TEventArgs> routedEvent,
EventHandler<TEventArgs> handler,
RoutingStrategies routes = RoutingStrategies.Direct | RoutingStrategies.Bubble,
bool handledEventsToo = false) where TEventArgs : RoutedEventArgs;
void RemoveHandler(RoutedEvent routedEvent, Delegate handler);
void RemoveHandler<TEventArgs>(RoutedEvent<TEventArgs> routedEvent, EventHandler<TEventArgs> handler)
where TEventArgs : RoutedEventArgs;
void RaiseEvent(RoutedEventArgs e); void RaiseEvent(RoutedEventArgs e);
} }
} }

11
TestApplication/Program.cs

@ -188,6 +188,7 @@ namespace TestApplication
private static TabItem ButtonsTab() private static TabItem ButtonsTab()
{ {
Button defaultButton;
var showDialog = ReactiveCommand.Create(); var showDialog = ReactiveCommand.Create();
Button showDialogButton; Button showDialogButton;
@ -213,6 +214,11 @@ namespace TestApplication
Content = "Button", Content = "Button",
Background = new SolidColorBrush(0xcc119eda), Background = new SolidColorBrush(0xcc119eda),
}, },
(defaultButton = new Button
{
Content = "Default",
IsDefault = true,
}),
new Button new Button
{ {
Content = "Disabled", Content = "Disabled",
@ -250,6 +256,11 @@ namespace TestApplication
}, },
}; };
defaultButton.Click += (s, e) =>
{
defaultButton.Content = ((string)defaultButton.Content == "Default") ? "Clicked" : "Default";
};
showDialog.Subscribe(async _ => showDialog.Subscribe(async _ =>
{ {
var close = ReactiveCommand.Create(); var close = ReactiveCommand.Create();

Loading…
Cancel
Save