Skip to content

Commit

Permalink
fix searchlist focus
Browse files Browse the repository at this point in the history
  • Loading branch information
HendrikMennen committed Dec 7, 2023
1 parent 68a78d3 commit 5113737
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 52 deletions.
7 changes: 6 additions & 1 deletion src/OneWare.Core/Services/DockService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,19 @@ public async Task<IExtendedDocument> OpenFileAsync(IFile pf)
_documentViewRegistrations.TryGetValue(pf.Extension, out var type);
type ??= typeof(EditViewModel);
var viewModel = ContainerLocator.Current.Resolve(type, (typeof(string), pf.FullPath)) as IExtendedDocument;

if (viewModel == null) throw new NullReferenceException($"{type} could not be resolved!");

Show(viewModel, DockShowLocation.Document);

if (_mainDocumentDockViewModel.VisibleDockables?.Contains(_welcomeScreenViewModel) ?? false)
_mainDocumentDockViewModel.VisibleDockables.Remove(_welcomeScreenViewModel);

if (viewModel is EditViewModel evm)
{
await evm.WaitForEditorReadyAsync();
}

return viewModel;
}

Expand Down
2 changes: 2 additions & 0 deletions src/OneWare.SDK/Commands/MenuItemApplicationCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public MenuItemApplicationCommand(MenuItemViewModel menuItem, string path) : bas
MenuItem = menuItem;
IconObservable = menuItem.WhenValueChanged(x => x.Icon);

DefaultGesture = menuItem.InputGesture;

this.WhenValueChanged(x => x.ActiveGesture).Subscribe(x =>
{
MenuItem.InputGesture = x;
Expand Down
2 changes: 1 addition & 1 deletion src/OneWare.SDK/Controls/SearchBox.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="200" d:DesignHeight="24" Focusable="False"
mc:Ignorable="d" d:DesignWidth="200" d:DesignHeight="24" Focusable="True"
x:Class="OneWare.SDK.Controls.SearchBox" Name="SearchBoxView">

<Grid VerticalAlignment="Center" RowDefinitions="Auto" ColumnDefinitions="*,Auto,Auto">
Expand Down
14 changes: 11 additions & 3 deletions src/OneWare.SDK/Controls/SearchBox.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Avalonia.Data;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Threading;
using DynamicData.Binding;

namespace OneWare.SDK.Controls
Expand Down Expand Up @@ -33,7 +34,7 @@ public SearchBox()
{
InitializeComponent();

NotifyPropertyChangedEx.WhenValueChanged<TextBox, string>(SearchTextBox, x => x.Text).Subscribe(x => OnTextAddedEvent());
SearchTextBox.WhenValueChanged(x => x.Text).Subscribe(x => OnTextAddedEvent());

KeyDown += SearchTextBox_KeyDown;
}
Expand All @@ -60,8 +61,15 @@ public bool SearchButtonVisible

protected override void OnGotFocus(GotFocusEventArgs e)
{
SearchTextBox.Focus();
SearchTextBox.SelectAll();
Dispatcher.UIThread.Post(() =>
{
if (!SearchTextBox.IsFocused)
{
SearchTextBox.Focus();
}
});

//SearchTextBox.SelectAll();
}

public void Reset()
Expand Down
11 changes: 9 additions & 2 deletions src/OneWare.SearchList/SearchListModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Avalonia.Controls;
using Avalonia.Input;
using CommunityToolkit.Mvvm.Input;
using OneWare.SDK.Helpers;
using OneWare.SearchList.ViewModels;
using Prism.Ioc;
using Prism.Modularity;
Expand Down Expand Up @@ -32,8 +33,14 @@ public void OnInitialized(IContainerProvider containerProvider)
_windowService.RegisterMenuItem("MainWindow_MainMenu/View/Tool Windows", new MenuItemViewModel("Search")
{
Header = "Search",
Command = new RelayCommand(() => _dockService.Show(containerProvider.Resolve<SearchListViewModel>())),
IconObservable = Application.Current?.GetResourceObservable(SearchListViewModel.IconKey),
Command = new RelayCommand(() =>
{
var vm = containerProvider.Resolve<SearchListViewModel>();
vm.SearchString = string.Empty;
_dockService.Show(vm);
}),
IconObservable = Application.Current!.GetResourceObservable(SearchListViewModel.IconKey),
InputGesture = new KeyGesture(Key.F, KeyModifiers.Shift | PlatformHelper.ControlKey)
});
}
}
23 changes: 16 additions & 7 deletions src/OneWare.SearchList/ViewModels/SearchListViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,21 @@ public class SearchListViewModel : ExtendedTool
{
public const string IconKey = "VsImageLib.Search16XMd";

private IDockService _dockService;
private IProjectExplorerService _projectExplorerService;
private readonly IDockService _dockService;
private readonly IProjectExplorerService _projectExplorerService;

private CancellationTokenSource? _lastCancellationToken;

public string LastSearchString { get; set; } = "";

private string _searchString = string.Empty;
public string SearchString
{
get => _searchString;
set
{
SetProperty(ref _searchString, value);
Search(_searchString);
}
}

public ObservableCollection<SearchResultModel> Items { get; } = new();

Expand Down Expand Up @@ -78,8 +87,8 @@ public SearchListViewModel(IDockService dockService, IProjectExplorerService pro
Title = "Search";
Id = "Search";
}
public void Search(string searchText)

private void Search(string searchText)
{
Items.Clear();
_lastCancellationToken?.Cancel();
Expand All @@ -103,7 +112,7 @@ await SearchFolderRecursiveAsync(_projectExplorerService.Items, searchText,
await SearchFolderRecursiveAsync(_projectExplorerService.ActiveProject.Items, searchText,
_lastCancellationToken.Token);
break;
case 2 when _dockService.CurrentDocument is IEditor editor:
case 2 when _dockService.CurrentDocument is IEditor {CurrentFile: not null} editor:
Items.AddRange(await FindAllIndexesAsync(editor.CurrentFile,
searchText, CaseSensitive, UseRegex, WholeWord, _lastCancellationToken.Token));
break;
Expand Down
45 changes: 27 additions & 18 deletions src/OneWare.SearchList/Views/SearchListView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<Border Background="{DynamicResource ThemeBackgroundBrush}" Padding="2">
<DockPanel Name="InformationPanel">
<StackPanel DockPanel.Dock="Right" Orientation="Horizontal" Spacing="3">

<ToggleButton CornerRadius="3" Width="24" Height="24" Background="Transparent"
IsChecked="{Binding CaseSensitive}" ToolTip.Tip="Case Sensitive">
<Image Height="16" Width="16" Source="{DynamicResource VsImageLib.CaseSensitive16X}" />
Expand All @@ -61,14 +61,21 @@
<ComboBoxItem>Current file</ComboBoxItem>
</ComboBox>
</Border>

</StackPanel>
<Border DockPanel.Dock="Right" Classes="SplitterBorder" Margin="6 0" />

<Border DockPanel.Dock="Left" Classes="RoundToolBar">
<controls:SearchBox SearchText="{Binding LastSearchString}" Name="Search" BorderThickness="0"
BorderBrush="{DynamicResource ThemeBorderLowBrush}"
HorizontalAlignment="Stretch" />
<controls:SearchBox SearchText="{Binding SearchString}" Name="Search" BorderThickness="0"
BorderBrush="{DynamicResource ThemeBorderLowBrush}"
HorizontalAlignment="Stretch">
<Interaction.Behaviors>
<FocusOnAttachedToVisualTreeBehavior/>
<behaviours:DataGridNavigationBehaviour AssociatedDataGrid="{Binding #SearchList}" />
<behaviours:CommandOnKeyPressedBehaviour TriggerKey="Enter" HandledEventsToo="False"
Command="{Binding GoToSearchResultAsync}" />
</Interaction.Behaviors>
</controls:SearchBox>
</Border>
</DockPanel>
</Border>
Expand All @@ -89,25 +96,26 @@
<DataGridTemplateColumn.CellTemplate>
<DataTemplate DataType="models:SearchResultModel">
<Border Padding="5 0">
<StackPanel Orientation="Horizontal" Height="18">
<TextBlock Text="{Binding DescriptionLeft}"
ToolTip.Tip="{Binding Description}" />
<TextBlock Background="#774287f5" Text="{Binding DescriptionMid}"
ToolTip.Tip="{Binding Description}" />
<TextBlock Text="{Binding DescriptionRight}" ToolTip.Tip="{Binding Description}" />
</StackPanel>
<StackPanel Orientation="Horizontal" Height="18">
<TextBlock Text="{Binding DescriptionLeft}"
ToolTip.Tip="{Binding Description}" />
<TextBlock Background="{DynamicResource ThemeAccentBrush2}" Text="{Binding DescriptionMid}"
ToolTip.Tip="{Binding Description}" />
<TextBlock Text="{Binding DescriptionRight}" ToolTip.Tip="{Binding Description}" />
</StackPanel>
</Border>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.Header>
<TextBlock Text="Code"/>
<TextBlock Text="Code" />
</DataGridTemplateColumn.Header>
</DataGridTemplateColumn>

<DataGridTemplateColumn CanUserResize="True" Width="*" SortMemberPath="Project.Header">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate DataType="models:SearchResultModel">
<TextBlock Padding="5 0" VerticalAlignment="Center" Text="{Binding Project.Header, FallbackValue={x:Null}}" />
<TextBlock Padding="5 0" VerticalAlignment="Center"
Text="{Binding Project.Header, FallbackValue={x:Null}}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.Header>
Expand All @@ -118,7 +126,8 @@
<DataGridTemplateColumn CanUserResize="True" Width="*" SortMemberPath="File.Header">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate DataType="models:SearchResultModel">
<TextBlock Padding="5 0" VerticalAlignment="Center" Text="{Binding File.Header, FallbackValue={x:Null}}" />
<TextBlock Padding="5 0" VerticalAlignment="Center"
Text="{Binding File.Header, FallbackValue={x:Null}}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.Header>
Expand All @@ -143,8 +152,8 @@
<StackPanel Orientation="Horizontal" Grid.Row="0" HorizontalAlignment="Right"
VerticalAlignment="Top">
<controls:Spinner Height="14" Width="14" Margin="0, 8, 295, 0"
IsVisible="{Binding IsLoading}"
Foreground="{DynamicResource DarkBlue}" />
IsVisible="{Binding IsLoading}"
Foreground="{DynamicResource DarkBlue}" />
</StackPanel>
</Grid>
</UserControl>
20 changes: 0 additions & 20 deletions src/OneWare.SearchList/Views/SearchListView.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,11 @@ public partial class SearchListView : UserControl
public SearchListView()
{
InitializeComponent();

Search.Search += (o, i) =>
{
if (DataContext is SearchListViewModel evm) evm.Search(Search.SearchText);
};

KeyDown += (o, i) =>
{
if (i.Key == Key.Escape) (VisualRoot as Window)?.Close();
};
}

protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnAttachedToVisualTree(e);
Dispatcher.UIThread.Post(() =>
{
_ = FocusSearchBoxAsync();
});
}

private async Task FocusSearchBoxAsync()
{
await Task.Delay(100);
Search.Focus();
}
}
}

0 comments on commit 5113737

Please sign in to comment.