Skip to content

Commit

Permalink
Merge pull request #1474 from VladiStep/tabsSwitchFix
Browse files Browse the repository at this point in the history
Tab selection bug fixes.
  • Loading branch information
colinator27 authored Nov 12, 2023
2 parents 1beac06 + dfae57c commit 99381fd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion UndertaleModTool/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@
</Grid.ColumnDefinitions>
<ScrollViewer Grid.Column="0" Name="TabScrollViewer" Height="26"
HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"
PreviewMouseWheel="TabScrollViewer_PreviewMouseWheel">
PreviewMouseWheel="TabScrollViewer_PreviewMouseWheel" PreviewMouseDown="TabScrollViewer_PreviewMouseDown">
<local:TabControlDark x:Name="TabController" Height="22" TabStripPlacement="Top" ItemsSource="{Binding Tabs, Mode=OneWay}" SelectedIndex="{Binding CurrentTabIndex}"
SelectionChanged="TabController_SelectionChanged">
<TabControl.Resources>
Expand Down
21 changes: 13 additions & 8 deletions UndertaleModTool/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3666,8 +3666,6 @@ private void TabController_SelectionChanged(object sender, SelectionChangedEvent
{
CurrentTab?.SaveTabContentState();

ScrollToTab(CurrentTabIndex);

Tab newTab = Tabs[CurrentTabIndex];

if (CurrentTab?.CurrentObject != newTab.CurrentObject)
Expand All @@ -3678,6 +3676,8 @@ private void TabController_SelectionChanged(object sender, SelectionChangedEvent
UpdateObjectLabel(CurrentTab.CurrentObject);

CurrentTab.RestoreTabContentState();

ScrollToTab(CurrentTabIndex);
}
}

Expand Down Expand Up @@ -3754,16 +3754,12 @@ private void ScrollToTab(int tabIndex)
tabItems.RemoveAt(tabItems.Count - 1);
}

TabItem currTabItem = null;
double offset = 0;
int i = 0;
foreach (TabItem item in tabItems)
{
if (i == tabIndex)
{
currTabItem = item;
break;
}

offset += item.ActualWidth;
i++;
Expand All @@ -3772,15 +3768,17 @@ private void ScrollToTab(int tabIndex)
double endOffset = TabScrollViewer.HorizontalOffset + TabScrollViewer.ViewportWidth;
if (offset < TabScrollViewer.HorizontalOffset || offset > endOffset)
TabScrollViewer.ScrollToHorizontalOffset(offset);
else
currTabItem?.BringIntoView();
}
}
private void TabScrollViewer_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
ScrollTabs(e.Delta < 0 ? ScrollDirection.Right : ScrollDirection.Left);
e.Handled = true;
}
private void TabScrollViewer_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
initTabContPos = e.GetPosition(TabScrollViewer);
}
private void TabsScrollLeftButton_Click(object sender, RoutedEventArgs e)
{
ScrollTabs(ScrollDirection.Left);
Expand Down Expand Up @@ -3820,6 +3818,7 @@ private void TabItem_MouseUp(object sender, MouseButtonEventArgs e)
}
}

private Point initTabContPos;
// source - https://stackoverflow.com/a/10738247/12136394
private void TabItem_PreviewMouseMove(object sender, MouseEventArgs e)
{
Expand All @@ -3828,6 +3827,12 @@ private void TabItem_PreviewMouseMove(object sender, MouseEventArgs e)

if (Mouse.PrimaryDevice.LeftButton == MouseButtonState.Pressed)
{
// Filter false mouse move events, because it sometimes
// triggers even on a mouse click
Point currPos = e.GetPosition(TabScrollViewer);
if (Math.Abs(Point.Subtract(currPos, initTabContPos).X) < 2)
return;

CurrentTabIndex = tabItem.TabIndex;
try
{
Expand Down

0 comments on commit 99381fd

Please sign in to comment.