/// <summary> /// Focuses a control. /// </summary> /// <param name="control">The control to focus.</param> /// <param name="keyboardNavigated"> /// Whether the control was focused by a keypress (e.g. the Tab key). /// </param> public void Focus(IInputElement control, bool keyboardNavigated = false) { if (control != null) { var scope = GetFocusScopeAncestors(control) .FirstOrDefault(); if (scope != null) { this.Scope = scope; this.SetFocusedElement(scope, control, keyboardNavigated); System.Diagnostics.Debug.WriteLine("Focused " + control.GetType().Name); } } else if (this.Current != null) { // If control is null, set focus to the topmost focus scope. foreach (var scope in GetFocusScopeAncestors(this.Current).Reverse().ToList()) { IInputElement element; if (this.focusScopes.TryGetValue(scope, out element)) { this.Focus(element, keyboardNavigated); break; } } } }
/// <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="method">The method by which focus was changed.</param> /// <param name="keyModifiers">Any key modifiers active at the time of focus.</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, NavigationMethod method = NavigationMethod.Unspecified, KeyModifiers keyModifiers = KeyModifiers.None) { scope = scope ?? throw new ArgumentNullException(nameof(scope)); if (_focusScopes.TryGetValue(scope, out var existingElement)) { if (element != existingElement) { _focusScopes.Remove(scope); _focusScopes.Add(scope, element); } } else { _focusScopes.Add(scope, element); } if (Scope == scope) { KeyboardDevice.Instance?.SetFocusedElement(element, method, keyModifiers); } }
/// <summary> /// Focuses a control. /// </summary> /// <param name="control">The control to focus.</param> /// <param name="method">The method by which focus was changed.</param> /// <param name="keyModifiers">Any key modifiers active at the time of focus.</param> public void Focus( IInputElement control, NavigationMethod method = NavigationMethod.Unspecified, KeyModifiers keyModifiers = KeyModifiers.None) { if (control != null) { var scope = GetFocusScopeAncestors(control) .FirstOrDefault(); if (scope != null) { Scope = scope; SetFocusedElement(scope, control, method, keyModifiers); } } else if (Current != null) { // If control is null, set focus to the topmost focus scope. foreach (var scope in GetFocusScopeAncestors(Current).Reverse().ToList()) { IInputElement element; if (_focusScopes.TryGetValue(scope, out element) && element != null) { Focus(element, method); return; } } // Couldn't find a focus scope, clear focus. SetFocusedElement(Scope, null); } }
/// <summary> /// Focuses a control. /// </summary> /// <param name="control">The control to focus.</param> /// <param name="method">The method by which focus was changed.</param> /// <param name="modifiers">Any input modifiers active at the time of focus.</param> public void Focus( IInputElement control, NavigationMethod method = NavigationMethod.Unspecified, InputModifiers modifiers = InputModifiers.None) { if (control != null) { var scope = GetFocusScopeAncestors(control) .FirstOrDefault(); if (scope != null) { Scope = scope; SetFocusedElement(scope, control, method, modifiers); } } else if (Current != null) { // If control is null, set focus to the topmost focus scope. foreach (var scope in GetFocusScopeAncestors(Current).Reverse().ToList()) { IInputElement element; if (_focusScopes.TryGetValue(scope, out element)) { Focus(element, method); break; } } } }
/// <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="method">The method by which focus was changed.</param> /// <param name="keyModifiers">Any key modifiers active at the time of focus.</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, NavigationMethod method = NavigationMethod.Unspecified, KeyModifiers keyModifiers = KeyModifiers.None) { Contract.Requires <ArgumentNullException>(scope != null); if (_focusScopes.TryGetValue(scope, out IInputElement existingElement)) { if (element != existingElement) { _focusScopes.Remove(scope); _focusScopes.Add(scope, element); } } else { _focusScopes.Add(scope, element); } if (Scope == scope) { KeyboardDevice.Instance?.SetFocusedElement(element, method, keyModifiers); } }
/// <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="method">The method by which focus was changed.</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, NavigationMethod method = NavigationMethod.Unspecified) { Contract.Requires <ArgumentNullException>(scope != null); _focusScopes[scope] = element; if (Scope == scope) { KeyboardDevice.Instance.SetFocusedElement(element, method); } }
public void RemoveFocusScope(IFocusScope scope) { scope = scope ?? throw new ArgumentNullException(nameof(scope)); if (_focusScopes.TryGetValue(scope, out _)) { SetFocusedElement(scope, null); _focusScopes.Remove(scope); } if (Scope == scope) { Scope = null; } }
/// <summary> /// Notifies the focus manager of a change in focus scope. /// </summary> /// <param name="scope">The new focus scope.</param> public void SetFocusScope(IFocusScope scope) { scope = scope ?? throw new ArgumentNullException(nameof(scope)); if (!_focusScopes.TryGetValue(scope, out var e)) { // TODO: Make this do something useful, i.e. select the first focusable // control, select a control that the user has specified to have default // focus etc. e = scope as IInputElement; _focusScopes.Add(scope, e); } Scope = scope; Focus(e); }
/// <summary> /// 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); IInputElement e; if (!this.focusScopes.TryGetValue(scope, out e)) { // TODO: Make this do something useful, i.e. select the first focusable // control, select a control that the user has specified to have default // focus etc. e = scope as IInputElement; this.focusScopes.Add(scope, e); } this.Scope = scope; this.Focus(e); }
/// <summary> /// Notifies the focus manager of a change in focus scope. /// </summary> /// <param name="scope">The new focus scope.</param> public void SetFocusScope(IFocusScope scope) { Contract.Requires<ArgumentNullException>(scope != null); IInputElement e; if (!this.focusScopes.TryGetValue(scope, out e)) { // TODO: Make this do something useful, i.e. select the first focusable // control, select a control that the user has specified to have default // focus etc. e = scope as IInputElement; this.focusScopes.Add(scope, e); } this.Scope = scope; this.Focus(e); }
/// <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) { KeyboardDevice.Instance.SetFocusedElement(element, keyboardNavigated); } }
/// <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="method">The method by which focus was changed.</param> /// <param name="modifiers">Any input modifiers active at the time of focus.</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, NavigationMethod method = NavigationMethod.Unspecified, InputModifiers modifiers = InputModifiers.None) { Contract.Requires<ArgumentNullException>(scope != null); _focusScopes[scope] = element; if (Scope == scope) { KeyboardDevice.Instance.SetFocusedElement(element, method, modifiers); } }
/// <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="method">The method by which focus was changed.</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, NavigationMethod method = NavigationMethod.Unspecified) { Contract.Requires<ArgumentNullException>(scope != null); this.focusScopes[scope] = element; if (this.Scope == scope) { KeyboardDevice.Instance.SetFocusedElement(element, method); } }