Skip to content

Commit 3bee9e4

Browse files
committed
optimizing startup time: cleanup setfocustogrid, use delayed setfocustogrid
1 parent bb7fb4d commit 3bee9e4

File tree

2 files changed

+19
-24
lines changed

2 files changed

+19
-24
lines changed

UnityLauncherPro/MainWindow.xaml.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using System.Windows.Media;
2323
using System.Windows.Media.Imaging;
2424
using System.Windows.Shell;
25+
using System.Windows.Threading;
2526
using UnityLauncherPro.Data;
2627
using UnityLauncherPro.Helpers;
2728
using UnityLauncherPro.Properties;
@@ -1318,7 +1319,8 @@ private void GridRecent_Loaded(object sender, RoutedEventArgs e)
13181319
// if coming from explorer launch, and missing unity version, projectsource is still null?
13191320
if (projectsSource != null) SetStatus("Ready (" + projectsSource.Count + " projects)");
13201321
RefreshSorting();
1321-
Tools.SetFocusToGrid(gridRecent);
1322+
//Tools.SetFocusToGrid(gridRecent);
1323+
Dispatcher.InvokeAsync(() => Tools.SetFocusToGrid(gridRecent), DispatcherPriority.ApplicationIdle);
13221324
}
13231325

13241326
void RefreshSorting()

UnityLauncherPro/Tools.cs

+16-23
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System.Windows.Controls;
1818
using System.Windows.Input;
1919
using System.Windows.Media;
20+
using System.Windows.Threading;
2021
using UnityLauncherPro.Helpers;
2122

2223
namespace UnityLauncherPro
@@ -1592,47 +1593,39 @@ public static bool HasFocus(DependencyObject obj, Control control, bool checkChi
15921593

15931594
public static void SetFocusToGrid(DataGrid targetGrid, int index = -1)
15941595
{
1595-
// set main component focus
1596-
//targetGrid.Focus();
1597-
//Keyboard.Focus(targetGrid);
1598-
1599-
// no items
16001596
if (targetGrid.Items.Count < 1) return;
16011597

1602-
// keep current row selected
16031598
if (index == -1 && targetGrid.SelectedIndex > -1) index = targetGrid.SelectedIndex;
1604-
1605-
// if no item selected, pick first
16061599
if (index == -1) index = 0;
16071600

16081601
targetGrid.SelectedIndex = index;
16091602

1610-
// set full focus
1603+
// Try get the row, if not realized yet, defer
16111604
DataGridRow row = (DataGridRow)targetGrid.ItemContainerGenerator.ContainerFromIndex(index);
16121605
if (row == null)
16131606
{
1614-
targetGrid.UpdateLayout();
1615-
if (index < targetGrid.Items.Count)
1616-
{
1617-
// scroll selected into view
1618-
targetGrid.ScrollIntoView(targetGrid.Items[index]);
1619-
row = (DataGridRow)targetGrid.ItemContainerGenerator.ContainerFromIndex(index);
1620-
}
1621-
else
1607+
targetGrid.ScrollIntoView(targetGrid.Items[index]);
1608+
// Defer the focus once row is generated
1609+
targetGrid.Dispatcher.InvokeAsync(() =>
16221610
{
1623-
Console.WriteLine("selected row out of bounds: " + index);
1624-
}
1611+
var newRow = (DataGridRow)targetGrid.ItemContainerGenerator.ContainerFromIndex(index);
1612+
if (newRow != null)
1613+
{
1614+
newRow.MoveFocus(new TraversalRequest(FocusNavigationDirection.Up));
1615+
newRow.Focus();
1616+
Keyboard.Focus(newRow);
1617+
}
1618+
}, DispatcherPriority.Background);
16251619
}
1626-
// NOTE does this causes move below?
1627-
//row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
1628-
if (row != null)
1620+
else
16291621
{
1630-
row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Up)); // works better than Up
1622+
row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Up));
16311623
row.Focus();
16321624
Keyboard.Focus(row);
16331625
}
16341626
}
16351627

1628+
16361629
public static string BrowseForOutputFolder(string title, string initialDirectory = null)
16371630
{
16381631
// https://stackoverflow.com/a/50261723/5452781

0 commit comments

Comments
 (0)