Skip to content

Commit

Permalink
Remember last selected tab and restore on load
Browse files Browse the repository at this point in the history
  • Loading branch information
micahmo committed Aug 20, 2019
1 parent 4a5dc1a commit abcffde
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
22 changes: 22 additions & 0 deletions SoundBoard/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#region Usings

using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;

#endregion

Expand Down Expand Up @@ -71,4 +73,24 @@ public static bool IsBlack(this System.Windows.Media.Color color)
}

#endregion

#region TabItemExtensions class

/// <summary>
/// Extensions on the <see cref="TabItem"/> class.
/// </summary>
internal static class TabItemExtensions
{
/// <summary>
/// Returns <see langword="true"/> if the given <paramref name="tabItem"/> is the <see cref="Selector.SelectedItem"/> of its <see cref="FrameworkElement.Parent"/> <see cref="TabControl"/>.
/// </summary>
/// <param name="tabItem"></param>
/// <returns></returns>
public static bool IsSelectedItem(this TabItem tabItem)
{
return tabItem == (tabItem.Parent as TabControl)?.SelectedItem;
}
}

#endregion
}
14 changes: 14 additions & 0 deletions SoundBoard/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,21 @@ private void LoadSettings(string configFilePath)

if (tabNodes != null)
{
TabItem selectedTab = null;

foreach (XmlNode node in tabNodes)
{
string name = node["name"]?.InnerText;

MetroTabItem tab = new MyMetroTabItem {Header = name};
Tabs.Items.Add(tab);

if (node.Attributes?["focused"]?.Value is string focusedString &&
bool.TryParse(focusedString, out bool focused) && focused)
{
selectedTab = tab;
}

List<SoundButtonUndoState> buttons = new List<SoundButtonUndoState>();

// Read the button data
Expand Down Expand Up @@ -222,6 +230,11 @@ private void LoadSettings(string configFilePath)
CreatePageContent(tab, buttons);
}

if (selectedTab is null == false)
{
Tabs.SelectedItem = selectedTab;
}

CreateTabContextMenus();
}
}
Expand Down Expand Up @@ -538,6 +551,7 @@ private void SaveSettings(string configFilePath)
{
string name = tab.Header.ToString();
textWriter.WriteStartElement("tab");
textWriter.WriteAttributeString("focused", tab.IsSelectedItem().ToString());

textWriter.WriteElementString("name", name);

Expand Down

0 comments on commit abcffde

Please sign in to comment.