Skip to content

Commit

Permalink
Allow opening windows explorer when the search query is a path that e…
Browse files Browse the repository at this point in the history
…xists
  • Loading branch information
Christian Rondeau committed Sep 1, 2014
1 parent 8533212 commit 873c1fb
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.ComponentModel;
using System.Text.RegularExpressions;
using GoToWindow.Api;
using GoToWindow.Extensibility.Controls;
using GoToWindow.Extensibility.ViewModel;
Expand All @@ -10,6 +11,8 @@ namespace GoToWindow.Plugins.Core.ViewModel
public class WindowSearchCommandResult : SearchResultBase, IBasicCommandResult, INotifyPropertyChanged
{
private static readonly ILog Log = LogManager.GetLogger(typeof(WindowSearchCommandResult).Assembly, "GoToWindow");
private static readonly Regex ValidQuery = new Regex(@"^[^:\/<>|""]+$", RegexOptions.Compiled);


private string _text;
private string _query;
Expand Down Expand Up @@ -51,7 +54,7 @@ public bool IsShown(string searchQuery)
{
_query = searchQuery;
Text = _query;
return !String.IsNullOrWhiteSpace(searchQuery);
return !String.IsNullOrWhiteSpace(searchQuery) && ValidQuery.IsMatch(searchQuery);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Runtime.InteropServices;
using GoToWindow.Extensibility;
using GoToWindow.Extensibility.ViewModel;
using GoToWindow.Plugins.ExplorerExtensions.ViewModel;
using log4net;

namespace GoToWindow.Plugins.ExplorerExtensions
Expand Down Expand Up @@ -31,6 +32,8 @@ public void BuildList(List<ISearchResult> list)
list[i] = ProcessExplorerWindow(window);
}
}

list.Add(new OpenExplorerCommandResult());
}

private static IWindowSearchResult ProcessExplorerWindow(IWindowSearchResult window)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@
</ItemGroup>
<ItemGroup>
<Compile Include="ExplorerExtensionsPlugin.cs" />
<Compile Include="ExplorerWindowSearchResult.cs" />
<Compile Include="ViewModel\ExplorerWindowSearchResult.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ViewModel\OpenExplorerCommandResult.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\GoToWindow.Api\GoToWindow.Api.csproj">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Windows.Media.Imaging;
using GoToWindow.Extensibility.ViewModel;

namespace GoToWindow.Plugins.ExplorerExtensions
namespace GoToWindow.Plugins.ExplorerExtensions.ViewModel
{
public class ExplorerWindowSearchResult : IWindowSearchResult
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using GoToWindow.Extensibility.Controls;
using GoToWindow.Extensibility.ViewModel;
using log4net;

namespace GoToWindow.Plugins.ExplorerExtensions.ViewModel
{
public class OpenExplorerCommandResult : SearchResultBase, IBasicCommandResult, INotifyPropertyChanged
{
private static readonly ILog Log = LogManager.GetLogger(typeof(OpenExplorerCommandResult).Assembly, "GoToWindow");

private string _text;
private string _query;

public event PropertyChangedEventHandler PropertyChanged;

public string BeforeText { get { return "Open Windows Explorer with path '"; } }
public string AfterText { get { return "'"; } }

public string Text
{
get { return _text; }
set
{
_text = value;
OnPropertyChanged("Text");
}
}

public OpenExplorerCommandResult()
: base(() => new BasicCommandEntry())
{

}

protected void OnPropertyChanged(string name)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(name));
}

public void Select()
{
Log.DebugFormat("Launching Explorer with '{0}'.", _query);
Process.Start(_query);
}

public bool IsShown(string searchQuery)
{
_query = searchQuery;
Text = _query;
var isPathQuery = !String.IsNullOrWhiteSpace(searchQuery) && searchQuery.Length >= 2 && searchQuery[1] == ':';

if (!isPathQuery) return false;

return Directory.Exists(searchQuery);
}
}
}
17 changes: 17 additions & 0 deletions GoToWindow/Windows/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,23 @@
Visibility="{Binding IsEmpty, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource InvertAndVisibilityConverter}}"
ItemsSource="{Binding Windows.View}"
SelectedItem="{Binding SelectedWindowEntry, Mode=TwoWay}">
<ListView.Style>
<Style TargetType="ListView">
<Style.Triggers>
<Trigger Property="HasItems" Value="False">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListView">
<Grid Height="48">
<TextBlock Margin="16" HorizontalAlignment="Left" VerticalAlignment="Center" Foreground="Gray">No results</TextBlock>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</ListView.Style>
<ListView.ItemTemplate>
<DataTemplate DataType="model:IWindowSearchResult">
<ContentControl Content="{Binding View}" Height="48" />
Expand Down

0 comments on commit 873c1fb

Please sign in to comment.