2020-08-06 05:06:55 +08:00
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
2021-08-12 21:17:27 +08:00
using System.Windows ;
2020-08-06 05:06:55 +08:00
using System.Windows.Controls ;
2021-08-12 21:17:27 +08:00
using System.Windows.Media ;
2020-06-02 03:35:37 +08:00
namespace PowerLauncher
{
/// <summary>
/// Interaction logic for ResultList.xaml
/// </summary>
public partial class ResultList : UserControl
{
public ResultList ( )
{
InitializeComponent ( ) ;
}
2021-08-12 21:17:27 +08:00
private ToolTip _previouslyOpenedToolTip ;
// From https://docs.microsoft.com/en-us/dotnet/desktop/wpf/data/how-to-find-datatemplate-generated-elements
private TypeChildItem FindVisualChild < TypeChildItem > ( DependencyObject obj )
where TypeChildItem : DependencyObject
{
for ( int i = 0 ; i < VisualTreeHelper . GetChildrenCount ( obj ) ; i + + )
{
DependencyObject child = VisualTreeHelper . GetChild ( obj , i ) ;
if ( child ! = null & & child is TypeChildItem )
{
return ( TypeChildItem ) child ;
}
else
{
TypeChildItem childOfChild = FindVisualChild < TypeChildItem > ( child ) ;
if ( childOfChild ! = null )
{
return childOfChild ;
}
}
}
return null ;
}
private void HideCurrentToolTip ( )
{
if ( _previouslyOpenedToolTip ! = null )
{
_previouslyOpenedToolTip . IsOpen = false ;
}
}
private void ContextMenuListView_SelectionChanged ( object sender , SelectionChangedEventArgs e )
{
var listView = sender as ListView ;
if ( listView . SelectedItem ! = null )
{
ListBoxItem listBoxItem = ( ListBoxItem ) listView . ItemContainerGenerator . ContainerFromItem ( listView . SelectedItem ) ;
2021-09-28 22:30:44 +08:00
if ( listBoxItem ! = null )
{
ContentPresenter contentPresenter = FindVisualChild < ContentPresenter > ( listBoxItem ) ;
DataTemplate dataTemplate = contentPresenter . ContentTemplate ;
Button button = ( Button ) dataTemplate . FindName ( "commandButton" , contentPresenter ) ;
ToolTip tooltip = button . ToolTip as ToolTip ;
tooltip . PlacementTarget = button ;
tooltip . Placement = System . Windows . Controls . Primitives . PlacementMode . Right ;
tooltip . PlacementRectangle = new Rect ( 0 , button . Height , 0 , 0 ) ;
tooltip . IsOpen = true ;
}
else
{
HideCurrentToolTip ( ) ;
}
}
else
{
HideCurrentToolTip ( ) ;
2021-08-12 21:17:27 +08:00
}
}
2022-03-09 20:08:12 +08:00
[System.Diagnostics.CodeAnalysis.SuppressMessage("Globalization", "CA1309:Use ordinal string comparison", Justification = "Using StringComparison.InvariantCulture since this is user facing")]
2021-08-12 21:17:27 +08:00
private void ToolTip_Opened ( object sender , RoutedEventArgs e )
{
if ( string . Equals ( sender . GetType ( ) . FullName , "System.Windows.Controls.ToolTip" , System . StringComparison . InvariantCulture ) )
{
HideCurrentToolTip ( ) ;
_previouslyOpenedToolTip = ( ToolTip ) sender ;
}
}
2022-03-09 20:08:12 +08:00
[System.Diagnostics.CodeAnalysis.SuppressMessage("Globalization", "CA1309:Use ordinal string comparison", Justification = "Using StringComparison.InvariantCulture since this is user facing")]
2021-08-12 21:17:27 +08:00
private void SuggestionsListView_SelectionChanged ( object sender , SelectionChangedEventArgs e )
{
if ( string . Equals ( ( ( ListView ) e . OriginalSource ) . Name , "SuggestionsList" , System . StringComparison . InvariantCulture ) )
{
HideCurrentToolTip ( ) ;
}
}
2020-06-02 03:35:37 +08:00
}
2020-08-06 05:06:55 +08:00
}