|
|
|
@ -44,44 +44,66 @@ namespace Perspex.Input |
|
|
|
/// </param>
|
|
|
|
public void Focus(IInputElement control, bool keyboardNavigated = false) |
|
|
|
{ |
|
|
|
Contract.Requires<ArgumentNullException>(control != null); |
|
|
|
if (control != null) |
|
|
|
{ |
|
|
|
var scope = control.GetSelfAndVisualAncestors() |
|
|
|
.OfType<IFocusScope>() |
|
|
|
.FirstOrDefault(); |
|
|
|
|
|
|
|
var current = this.Current as IInteractive; |
|
|
|
var next = control as IInteractive; |
|
|
|
var scope = control.GetSelfAndVisualAncestors() |
|
|
|
.OfType<IFocusScope>() |
|
|
|
.FirstOrDefault(); |
|
|
|
if (scope != null) |
|
|
|
{ |
|
|
|
this.SetFocusedElement(scope, control, keyboardNavigated); |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
this.SetFocusedElement(this.Scope, null); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (scope != null && control != current) |
|
|
|
/// <summary>
|
|
|
|
/// Sets the currently focused element in the specified scope.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="scope">The focus scope.</param>
|
|
|
|
/// <param name="element">The element to focus. May be null.</param>
|
|
|
|
/// <param name="keyboardNavigated">
|
|
|
|
/// Whether the control was focused by a keypress (e.g. the Tab key).
|
|
|
|
/// </param>
|
|
|
|
/// <remarks>
|
|
|
|
/// If the specified scope is the current <see cref="Scope"/> then the keyboard focus
|
|
|
|
/// will change.
|
|
|
|
/// </remarks>
|
|
|
|
public void SetFocusedElement( |
|
|
|
IFocusScope scope, |
|
|
|
IInputElement element, |
|
|
|
bool keyboardNavigated = false) |
|
|
|
{ |
|
|
|
Contract.Requires<ArgumentNullException>(scope != null); |
|
|
|
|
|
|
|
this.focusScopes[scope] = element; |
|
|
|
|
|
|
|
if (this.Scope == scope) |
|
|
|
{ |
|
|
|
this.focusScopes[scope] = control; |
|
|
|
var interactive = this.Current as IInteractive; |
|
|
|
|
|
|
|
if (current != null) |
|
|
|
if (interactive != null) |
|
|
|
{ |
|
|
|
current.RaiseEvent(new RoutedEventArgs |
|
|
|
interactive.RaiseEvent(new RoutedEventArgs |
|
|
|
{ |
|
|
|
RoutedEvent = InputElement.LostFocusEvent, |
|
|
|
Source = current, |
|
|
|
OriginalSource = current, |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
this.Current = control; |
|
|
|
this.Current = element; |
|
|
|
KeyboardDevice.Instance.FocusedElement = element; |
|
|
|
|
|
|
|
IKeyboardDevice keyboard = Locator.Current.GetService<IKeyboardDevice>(); |
|
|
|
interactive = element as IInteractive; |
|
|
|
|
|
|
|
if (keyboard != null) |
|
|
|
if (interactive != null) |
|
|
|
{ |
|
|
|
keyboard.FocusedElement = control; |
|
|
|
} |
|
|
|
|
|
|
|
if (next != null) |
|
|
|
{ |
|
|
|
next.RaiseEvent(new GotFocusEventArgs |
|
|
|
interactive.RaiseEvent(new GotFocusEventArgs |
|
|
|
{ |
|
|
|
RoutedEvent = InputElement.GotFocusEvent, |
|
|
|
Source = next, |
|
|
|
OriginalSource = next, |
|
|
|
KeyboardNavigated = keyboardNavigated, |
|
|
|
}); |
|
|
|
} |
|
|
|
@ -92,10 +114,6 @@ namespace Perspex.Input |
|
|
|
/// Notifies the focus manager of a change in focus scope.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="scope">The new focus scope.</param>
|
|
|
|
/// <remarks>
|
|
|
|
/// This should not be called by client code. It is called by an <see cref="IFocusScope"/>
|
|
|
|
/// when it activates, e.g. when a Window is activated.
|
|
|
|
/// </remarks>
|
|
|
|
public void SetFocusScope(IFocusScope scope) |
|
|
|
{ |
|
|
|
Contract.Requires<ArgumentNullException>(scope != null); |
|
|
|
|