Skip to content

Commit c1e1ae4

Browse files
committed
improve setting selected row on recent projects (fixed selected row from minimized, keep selected row on clear search)
1 parent ce792f6 commit c1e1ae4

File tree

3 files changed

+42
-51
lines changed

3 files changed

+42
-51
lines changed

Diff for: UnityLauncherPro/MainWindow.xaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
xmlns:local="clr-namespace:UnityLauncherPro"
77
xmlns:System="clr-namespace:System;assembly=mscorlib" x:Class="UnityLauncherPro.MainWindow"
88
mc:Ignorable="d"
9-
Title="UnityLauncherPro" Height="650" Width="600" WindowStartupLocation="CenterScreen" Background="{DynamicResource DarkestBackground}" MinWidth="600" MinHeight="650" AllowsTransparency="True" WindowStyle="None" Margin="0" KeyDown="OnWindowKeyDown" Closing="Window_Closing" SizeChanged="Window_SizeChanged" Icon="Images/icon.ico" >
9+
Title="UnityLauncherPro" Height="650" Width="600" WindowStartupLocation="CenterScreen" Background="{DynamicResource DarkestBackground}" MinWidth="600" MinHeight="650" AllowsTransparency="True" WindowStyle="None" Margin="0" KeyDown="OnWindowKeyDown" Closing="Window_Closing" SizeChanged="Window_SizeChanged" Icon="Images/icon.ico" Activated="Window_Activated" >
1010

1111
<Window.Resources>
1212

Diff for: UnityLauncherPro/MainWindow.xaml.cs

+20-46
Original file line numberDiff line numberDiff line change
@@ -385,48 +385,14 @@ void RefreshRecentProjects()
385385
//
386386
//
387387

388-
389-
//private void OnSearchPreviewKeyDown(object sender, KeyEventArgs e)
390-
//{
391-
// switch (e.Key)
392-
// {
393-
// case Key.Escape:
394-
// if (((TextBox)sender).Text == "")
395-
// {
396-
// Console.WriteLine(1);
397-
// Keyboard.Focus(gridRecent);
398-
// e.Handled = true;
399-
// }
400-
// ((TextBox)sender).Text = "";
401-
// break;
402-
// default:
403-
// break;
404-
// }
405-
//}
406-
388+
// maximize window
407389
void NotifyIcon_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
408390
{
409391
this.Show();
410392
this.WindowState = WindowState.Normal;
411393
notifyIcon.Visible = false;
412-
}
413-
414-
// hide/show notifyicon based on window state
415-
private void Window_StateChanged(object sender, EventArgs e)
416-
{
417-
if (this.WindowState == WindowState.Minimized)
418-
{
419-
this.ShowInTaskbar = false;
420-
notifyIcon.BalloonTipTitle = "Minimize Sucessful";
421-
notifyIcon.BalloonTipText = "Minimized the app ";
422-
notifyIcon.ShowBalloonTip(400);
423-
notifyIcon.Visible = true;
424-
}
425-
else if (this.WindowState == WindowState.Normal)
426-
{
427-
notifyIcon.Visible = false;
428-
this.ShowInTaskbar = true;
429-
}
394+
// NOTE workaround for grid not focused when coming back from minimized window
395+
Tools.SetFocusToGrid(gridRecent, GetSelectedProjectIndex());
430396
}
431397

432398
private void OnRectangleMouseDown(object sender, MouseButtonEventArgs e)
@@ -512,9 +478,14 @@ private void OnWindowKeyDown(object sender, KeyEventArgs e)
512478
case Key.Escape: // clear project search
513479
if (txtSearchBox.Text == "")
514480
{
515-
if (txtSearchBox.IsFocused) Tools.SetFocusToGrid(gridRecent);
481+
// its already clear
516482
}
517-
txtSearchBox.Text = "";
483+
else // we have text in searchbox
484+
{
485+
txtSearchBox.Text = "";
486+
}
487+
// try to keep selected row selected and in view
488+
Tools.SetFocusToGrid(gridRecent);
518489
break;
519490
case Key.F5:
520491
txtSearchBox.Text = "";
@@ -523,6 +494,7 @@ private void OnWindowKeyDown(object sender, KeyEventArgs e)
523494
case Key.Left:
524495
case Key.Right:
525496
case Key.Down:
497+
break;
526498
case Key.F2: // edit arguments
527499
break;
528500
default: // any key
@@ -757,8 +729,8 @@ private void TxtSearchBox_PreviewKeyDown(object sender, KeyEventArgs e)
757729
//}
758730
//else
759731
//{
760-
Tools.SetFocusToGrid(gridRecent);
761-
// }
732+
Tools.SetFocusToGrid(gridRecent);
733+
// }
762734
e.Handled = true; // to stay in first row
763735
break;
764736
default:
@@ -795,15 +767,11 @@ private void BtnRemoveInstallationFolder_Click(object sender, RoutedEventArgs e)
795767
}
796768
}
797769

798-
// need to manually move into next/prev rows? https://stackoverflow.com/a/11652175/5452781
770+
// need to manually move into next/prev rows? Not using https://stackoverflow.com/a/11652175/5452781
799771
private void GridRecent_PreviewKeyDown(object sender, KeyEventArgs e)
800772
{
801773
switch (e.Key)
802774
{
803-
case Key.Up:
804-
case Key.Down:
805-
break;
806-
807775
case Key.F5: // refresh projects
808776
RefreshRecentProjects();
809777
break;
@@ -1153,5 +1121,11 @@ private void MenuItemSetPreferredUnityVersion_Click(object sender, RoutedEventAr
11531121
UpdateUnityInstallationsList();
11541122

11551123
}
1124+
1125+
private void Window_Activated(object sender, EventArgs e)
1126+
{
1127+
//Console.WriteLine("gridRecent.IsFocused=" + gridRecent.IsFocused);
1128+
//Console.WriteLine("gridRecent.IsFocused=" + gridRecent.IsKeyboardFocused);
1129+
}
11561130
} // class
11571131
} //namespace

Diff for: UnityLauncherPro/Tools.cs

+21-4
Original file line numberDiff line numberDiff line change
@@ -665,20 +665,37 @@ public static string ReadCustomLaunchArguments(string projectPath, string launch
665665

666666
public static void SetFocusToGrid(DataGrid targetGrid, int index = -1)
667667
{
668+
// set main component focus
669+
//targetGrid.Focus();
670+
//Keyboard.Focus(targetGrid);
671+
672+
// no items
668673
if (targetGrid.Items.Count < 1) return;
669-
if (index == -1 && targetGrid.SelectedIndex > -1) index = targetGrid.SelectedIndex; // keep current row selected
674+
675+
// keep current row selected
676+
if (index == -1 && targetGrid.SelectedIndex > -1) index = targetGrid.SelectedIndex;
677+
678+
// if no item selected, pick first
670679
if (index == -1) index = 0;
671680

672-
targetGrid.Focus();
681+
targetGrid.SelectedIndex = index;
682+
683+
// set full focus
673684
DataGridRow row = (DataGridRow)targetGrid.ItemContainerGenerator.ContainerFromIndex(index);
674685
if (row == null)
675686
{
676687
targetGrid.UpdateLayout();
688+
// scroll to view if outside
677689
targetGrid.ScrollIntoView(targetGrid.Items[index]);
678690
row = (DataGridRow)targetGrid.ItemContainerGenerator.ContainerFromIndex(index);
679691
}
680-
row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
681-
targetGrid.SelectedIndex = index;
692+
// NOTE does this causes move below?
693+
//row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
694+
row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Up)); // works better than Up
695+
696+
row.Focus();
697+
Keyboard.Focus(row);
698+
682699
}
683700

684701
} // class

0 commit comments

Comments
 (0)