Skip to content

Commit

Permalink
Using a different approach to ensure focus on tab item (fixes #856)
Browse files Browse the repository at this point in the history
Setting the focus to the content was never a good idea...
  • Loading branch information
batzen committed Oct 22, 2020
1 parent 7d262a3 commit 1d08483
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 99 deletions.
3 changes: 2 additions & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Changelog for Fluent.Ribbon

## Next
## 8.0.1

- ### Bug fixes

Expand All @@ -16,6 +16,7 @@
- [#849](../../issues/849) - QuickAccessToolBar not editable anymore (thanks @chrfin)
- [#851](../../issues/851) - Tab KeyTips Are Shown Together With Tab-Item KeyTips (different to 7.0.0)
- [#855](../../issues/855) - Method Fluent.StartScreen.Show lacks documentation return value
- [#856](../../issues/856) - RibbonTabItem.IsSelected is briefly set to true for a tab that's not selected anymore, when a modal dialog is closed.

## 8.0.0

Expand Down
35 changes: 20 additions & 15 deletions Fluent.Ribbon/Controls/RibbonTabControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ protected override void OnSelectionChanged(SelectionChangedEventArgs e)
{
// If keyboard focus is within the control, make sure it is going to the correct place
var item = this.GetSelectedTabItem();
item?.SetFocus();
item?.Focus();
}

if (e.AddedItems.Count > 0)
Expand Down Expand Up @@ -614,7 +614,8 @@ protected override void OnKeyDown(KeyEventArgs e)
if (nextTabItem != null
&& ReferenceEquals(nextTabItem, this.SelectedItem) == false)
{
e.Handled = nextTabItem.SetFocus();
e.Handled = true;
nextTabItem.IsSelected = true;
}

if (e.Handled == false)
Expand Down Expand Up @@ -735,7 +736,7 @@ private RibbonTabItem FindNextTabItem(int startIndex, int direction)
index = this.Items.Count - 1;
}

if (this.ItemContainerGenerator.ContainerOrContainerContentFromIndex<RibbonTabItem>(index) is RibbonTabItem nextItem
if (this.ItemContainerGenerator.ContainerOrContainerContentFromIndex<RibbonTabItem>(index) is { } nextItem
&& nextItem.IsEnabled
&& nextItem.Visibility == Visibility.Visible)
{
Expand Down Expand Up @@ -792,32 +793,36 @@ private void OnGeneratorStatusChanged(object sender, EventArgs e)
/// </summary>
public void SelectFirstTab()
{
if (this.IsMinimized == false)
if (this.IsMinimized)
{
this.SelectedItem = this.GetFirstVisibleAndEnabledItem();
return;
}

if (this.SelectedItem == null
&& this.IsEnabled == false)
{
this.SelectedItem = this.GetFirstVisibleItem();
}
this.SelectedItem = this.GetFirstVisibleAndEnabledItem();

if (this.SelectedItem is null
&& this.IsEnabled == false)
{
this.SelectedItem = this.GetFirstVisibleItem();
}

this.SelectedTabItem?.Focus();
}

// Handles IsMinimized changed
private static void OnIsMinimizedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var tab = (RibbonTabControl)d;
var tabControl = (RibbonTabControl)d;

if (!tab.IsMinimized)
if (tabControl.IsMinimized == false)
{
tab.IsDropDownOpen = false;
tabControl.IsDropDownOpen = false;
}

if ((bool)e.NewValue == false
&& tab.SelectedIndex < 0)
&& tabControl.SelectedIndex < 0)
{
var item = tab.FindNextTabItem(-1, 1);
var item = tabControl.FindNextTabItem(-1, 1);

if (item != null)
{
Expand Down
83 changes: 0 additions & 83 deletions Fluent.Ribbon/Controls/RibbonTabItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -530,87 +530,6 @@ public RibbonTabItem()

#region Overrides

internal bool SetFocus()
{
if (this.SettingFocus)
{
return false;
}

var currentFocus = Keyboard.FocusedElement as RibbonTabItem;

// If current focus was another TabItem in the same TabControl - dont set focus on content
var setFocusOnContent = ReferenceEquals(currentFocus, this)
|| currentFocus == null
|| ReferenceEquals(currentFocus.TabControlParent, this.TabControlParent) == false;
this.SettingFocus = true;
this.SetFocusOnContent = setFocusOnContent;

try
{
return this.Focus()
|| setFocusOnContent;
}
finally
{
this.SettingFocus = false;
this.SetFocusOnContent = false;
}
}

private bool SetFocusOnContent { get; set; }

private bool SettingFocus { get; set; }

/// <inheritdoc />
protected override void OnPreviewGotKeyboardFocus(KeyboardFocusChangedEventArgs e)
{
base.OnPreviewGotKeyboardFocus(e);

if (e.Handled
|| ReferenceEquals(e.NewFocus, this) == false)
{
return;
}

if (this.IsSelected
|| this.TabControlParent == null)
{
return;
}

this.IsSelected = true;

// If focus moved in result of selection - handle the event to prevent setting focus back on the new item
if (ReferenceEquals(e.OldFocus, Keyboard.FocusedElement) == false)
{
e.Handled = true;
}
else if (this.SetFocusOnContent)
{
var parentTabControl = this.TabControlParent;

if (parentTabControl != null)
{
// Save the parent and check for null to make sure that SetCurrentValue didn't have a change handler
// that removed the TabItem from the tree.
var selectedContentPresenter = parentTabControl.SelectedContentPresenter;

if (selectedContentPresenter != null)
{
parentTabControl.UpdateLayout(); // Wait for layout
var success = selectedContentPresenter.MoveFocus(new TraversalRequest(FocusNavigationDirection.First));

// If we successfully move focus inside the content then don't set focus to the header
if (success)
{
e.Handled = true;
}
}
}
}
}

/// <inheritdoc />
protected override Size MeasureOverride(Size constraint)
{
Expand Down Expand Up @@ -726,8 +645,6 @@ protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
this.IsSelected = true;
}

this.SetFocus();

e.Handled = true;
}
}
Expand Down

0 comments on commit 1d08483

Please sign in to comment.