fix selected item

part of #412
This commit is contained in:
bao-qian 2016-05-06 03:24:14 +01:00
parent fe5bb1d885
commit 407f58da54
6 changed files with 81 additions and 122 deletions

View File

@ -41,8 +41,8 @@
Style="{DynamicResource PendingLineStyle}" Visibility="{Binding ProgressBarVisibility, Mode=TwoWay}"
Y1="0" Y2="0" X2="100" Height="2" Width="752" StrokeThickness="1">
</Line>
<ContentControl Content="{Binding Results}" Visibility="{Binding ResultListBoxVisibility}" />
<ContentControl Content="{Binding ContextMenu}" Visibility="{Binding ContextMenuVisibility}" />
<ContentControl Name="Results" Content="{Binding Results}" Visibility="{Binding ResultListBoxVisibility}" />
<ContentControl Name ="ContextMenu" Content="{Binding ContextMenu}" Visibility="{Binding ContextMenuVisibility}" />
</StackPanel>
</Border>
</Window>

View File

@ -333,15 +333,6 @@ namespace Wox
var vm = DataContext as MainViewModel;
if (vm != null)
{
if (vm.ContextMenuVisibility.IsVisible())
{
vm.ContextMenu.SelectResult(result);
}
else
{
vm.Results.SelectResult(result);
}
if (e.ChangedButton == MouseButton.Left)
{
vm.OpenResultCommand.Execute(null);

View File

@ -7,11 +7,16 @@
xmlns:vm="clr-namespace:Wox.ViewModel"
mc:Ignorable="d" d:DesignWidth="100" d:DesignHeight="100"
d:DataContext="{d:DesignInstance vm:ResultsViewModel}"
MaxHeight="{Binding MaxHeight}" SelectedItem="{Binding SelectedResult}"
MaxHeight="{Binding MaxHeight}"
SelectedIndex="{Binding SelectedIndex, Mode=TwoWay}"
SelectedItem="{Binding SelectedItem, Mode=OneWayToSource}"
HorizontalContentAlignment="Stretch" ItemsSource="{Binding Results}" Margin="{Binding Margin}"
Style="{DynamicResource BaseListboxStyle}" SelectionChanged="lbResults_SelectionChanged" Focusable="False"
Style="{DynamicResource BaseListboxStyle}" Focusable="False"
KeyboardNavigation.DirectionalNavigation="Cycle" SelectionMode="Single"
VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Standard">
VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Standard"
SelectionChanged="OnSelectionChanged"
IsSynchronizedWithCurrentItem="True">
<!--IsSynchronizedWithCurrentItem: http://stackoverflow.com/a/7833798/2833083-->
<ListBox.ItemTemplate>
<DataTemplate>
<DataTemplate.DataType>
@ -65,6 +70,7 @@
<!--http://stackoverflow.com/questions/16819577/setting-background-color-or-wpf-4-0-listbox-windows-8/#16820062-->
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<EventSetter Event="MouseEnter" Handler="OnMouseEnter"></EventSetter>
<Setter Property="Height" Value="50" />
<Setter Property="Margin" Value="0" />
<Setter Property="Padding" Value="0"/>

View File

@ -3,6 +3,7 @@ using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Wox.Plugin;
using Wox.ViewModel;
@ -13,10 +14,10 @@ namespace Wox
{
public void AddResults(List<Result> newRawResults)
{
var vm = DataContext as ResultsViewModel;
var vm = (ResultsViewModel) DataContext;
var newResults = newRawResults.Select(r => new ResultViewModel(r)).ToList();
vm.Results.Update(newResults);
vm.SelectedResult = vm.Results[0];
vm.SelectedIndex = 0;
}
@ -25,7 +26,7 @@ namespace Wox
InitializeComponent();
}
private void lbResults_SelectionChanged(object sender, SelectionChangedEventArgs e)
private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count > 0 && e.AddedItems[0] != null)
{
@ -33,5 +34,9 @@ namespace Wox
}
}
private void OnMouseEnter(object sender, MouseEventArgs e)
{
((ListBoxItem) sender).IsSelected = true;
}
}
}

View File

@ -167,30 +167,33 @@ namespace Wox.ViewModel
Process.Start("http://doc.getwox.com");
});
OpenResultCommand = new RelayCommand(o =>
OpenResultCommand = new RelayCommand(index =>
{
var results = ContextMenuVisibility.IsVisible() ? ContextMenu : Results;
if (o != null)
if (index != null)
{
var index = int.Parse(o.ToString());
results.SelectResult(index);
results.SelectedIndex = int.Parse(index.ToString());
}
var result = results.SelectedResult.RawResult;
bool hideWindow = result.Action(new ActionContext
var result = results.SelectedItem?.RawResult;
if (result != null) // SelectedItem returns null if selection is empty.
{
SpecialKeyState = GlobalHotkey.Instance.CheckModifiers()
});
if (hideWindow)
{
MainWindowVisibility = Visibility.Collapsed;
}
bool hideWindow = result.Action(new ActionContext
{
SpecialKeyState = GlobalHotkey.Instance.CheckModifiers()
});
if (!ContextMenuVisibility.IsVisible())
{
_userSelectedRecord.Add(result);
_queryHistory.Add(result.OriginQuery.RawQuery);
if (hideWindow)
{
MainWindowVisibility = Visibility.Collapsed;
}
if (!ContextMenuVisibility.IsVisible())
{
_userSelectedRecord.Add(result);
_queryHistory.Add(result.OriginQuery.RawQuery);
}
}
});
@ -198,19 +201,23 @@ namespace Wox.ViewModel
{
if (!ContextMenuVisibility.IsVisible())
{
var result = Results.SelectedResult.RawResult;
var id = result.PluginID;
var result = Results.SelectedItem?.RawResult;
var menus = PluginManager.GetContextMenusForPlugin(result);
menus.Add(ContextMenuTopMost(result));
menus.Add(ContextMenuPluginInfo(id));
ContextMenu.Clear();
Task.Run(() =>
if (result != null) // SelectedItem returns null if selection is empty.
{
ContextMenu.AddResults(menus, id);
}, _updateToken);
ContextMenuVisibility = Visibility.Visible;
var id = result.PluginID;
var menus = PluginManager.GetContextMenusForPlugin(result);
menus.Add(ContextMenuTopMost(result));
menus.Add(ContextMenuPluginInfo(id));
ContextMenu.Clear();
Task.Run(() =>
{
ContextMenu.AddResults(menus, id);
}, _updateToken);
ContextMenuVisibility = Visibility.Visible;
}
}
else
{

View File

@ -4,12 +4,10 @@ using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using Wox.Core.UserSettings;
using Wox.Plugin;
using Wox.Storage;
namespace Wox.ViewModel
{
@ -17,7 +15,7 @@ namespace Wox.ViewModel
{
#region Private Fields
private ResultViewModel _selectedResult;
private int _selectedIndex;
public ResultCollection Results { get; }
private Thickness _margin;
@ -38,35 +36,17 @@ namespace Wox.ViewModel
public int MaxHeight => _settings.MaxResultsToShow * 50;
public ResultViewModel SelectedResult
public int SelectedIndex
{
get
{
return _selectedResult;
}
get { return _selectedIndex; }
set
{
if (value != null)
{
if (_selectedResult != null)
{
_selectedResult.IsSelected = false;
}
_selectedResult = value;
if (_selectedResult != null)
{
_selectedResult.IsSelected = true;
}
}
_selectedIndex = value;
OnPropertyChanged();
}
}
public ResultViewModel SelectedItem { get; set; }
public Thickness Margin
{
get
@ -98,76 +78,44 @@ namespace Wox.ViewModel
return index;
}
private int NewIndex(int i)
{
var n = Results.Count;
if (n > 0)
{
i = (n + i) % n;
return i;
}
else
{
// SelectedIndex returns -1 if selection is empty.
return -1;
}
}
#endregion
#region Public Methods
public void SelectResult(int index)
{
if (index <= Results.Count - 1)
{
SelectedResult = Results[index];
}
}
public void SelectResult(ResultViewModel result)
{
int i = Results.IndexOf(result);
SelectResult(i);
}
public void SelectNextResult()
{
if (Results.Count > 0 && SelectedResult != null)
{
var index = Results.IndexOf(SelectedResult);
if (index == Results.Count - 1)
{
index = -1;
}
SelectedResult = Results.ElementAt(index + 1);
}
SelectedIndex = NewIndex(SelectedIndex + 1);
}
public void SelectPrevResult()
{
if (Results.Count > 0 && SelectedResult != null)
{
var index = Results.IndexOf(SelectedResult);
if (index == 0)
{
index = Results.Count;
}
SelectedResult = Results.ElementAt(index - 1);
}
SelectedIndex = NewIndex(SelectedIndex - 1);
}
public void SelectNextPage()
{
if (Results.Count > 0 && SelectedResult != null)
{
var index = Results.IndexOf(SelectedResult);
index += 5;
if (index > Results.Count - 1)
{
index = Results.Count - 1;
}
SelectedResult = Results.ElementAt(index);
}
SelectedIndex = NewIndex(SelectedIndex + _settings.MaxResultsToShow);
}
public void SelectPrevPage()
{
if (Results.Count > 0 && SelectedResult != null)
{
var index = Results.IndexOf(SelectedResult);
index -= 5;
if (index < 0)
{
index = 0;
}
SelectedResult = Results.ElementAt(index);
}
SelectedIndex = NewIndex(SelectedIndex - _settings.MaxResultsToShow);
}
public void Clear()
@ -239,7 +187,7 @@ namespace Wox.ViewModel
if (Results.Count > 0)
{
Margin = new Thickness { Top = 8 };
SelectedResult = Results[0];
SelectedIndex = 0;
}
else
{
@ -314,6 +262,8 @@ namespace Wox.ViewModel
}
}
public ResultViewModel this[int selectedIndex] => Results[selectedIndex];
}
}