/// <summary>
/// Tries to set focus on the given control.
/// </summary>
/// <param name="newFocusControl">the control to focus. </param>
/// <returns>True if the focus was set or if it was already set, false if the control cannot be focused</returns>
public bool TrySetFocus(ConsoleControl newFocusControl)
{
var index = focusStack.Peek().Controls.IndexOf(newFocusControl);
if (index < 0)
{
throw new InvalidOperationException("The given control is not in the control tree");
}
if (newFocusControl.CanFocus == false)
{
return(false);
}
else if (newFocusControl == FocusedControl)
{
return(true);
}
else
{
if (FocusedControl != null)
{
ClearFocus();
}
newFocusControl.HasFocus = true;
FocusedControl = newFocusControl;
focusStack.Peek().FocusIndex = index;
if (FocusedControl != null)
{
FocusedControl.FireFocused(true);
}
return(true);
}
}