-
Notifications
You must be signed in to change notification settings - Fork 53
Move focus to main window of process if it's already running (using ProcessAddin) #60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 7 commits
b347eb4
6fd0288
439d3b3
2722b9c
5d0e9c7
6ecfe0f
96a3c97
2fbe935
2e79b2c
b52fdd5
c50cdf3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
| <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> | ||
| <PropertyGroup> | ||
| <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
| <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | ||
| <ProjectGuid>{AE5C446D-B1D3-4E98-941D-A751B80CA74F}</ProjectGuid> | ||
| <OutputType>Library</OutputType> | ||
| <AppDesignerFolder>Properties</AppDesignerFolder> | ||
| <RootNamespace>Jarvis.Addin.Processes</RootNamespace> | ||
| <AssemblyName>Jarvis.Addin.Processes</AssemblyName> | ||
| <TargetFrameworkVersion>v4.7</TargetFrameworkVersion> | ||
| <FileAlignment>512</FileAlignment> | ||
| </PropertyGroup> | ||
| <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | ||
| <PlatformTarget>AnyCPU</PlatformTarget> | ||
| <DebugSymbols>true</DebugSymbols> | ||
| <DebugType>full</DebugType> | ||
| <Optimize>false</Optimize> | ||
| <OutputPath>bin\Debug\</OutputPath> | ||
| <DefineConstants>DEBUG;TRACE</DefineConstants> | ||
| <ErrorReport>prompt</ErrorReport> | ||
| <WarningLevel>4</WarningLevel> | ||
| </PropertyGroup> | ||
| <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | ||
| <PlatformTarget>AnyCPU</PlatformTarget> | ||
| <DebugType>pdbonly</DebugType> | ||
| <Optimize>true</Optimize> | ||
| <OutputPath>bin\Release\</OutputPath> | ||
| <DefineConstants>TRACE</DefineConstants> | ||
| <ErrorReport>prompt</ErrorReport> | ||
| <WarningLevel>4</WarningLevel> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <Reference Include="Autofac, Version=4.6.2.0, Culture=neutral, PublicKeyToken=17863af14b0044da"> | ||
| <HintPath>..\packages\Autofac.4.6.2\lib\net45\Autofac.dll</HintPath> | ||
| </Reference> | ||
| <Reference Include="JetBrains.Annotations, Version=11.1.0.0, Culture=neutral, PublicKeyToken=1010a0d8d6380325"> | ||
| <HintPath>..\packages\JetBrains.Annotations.11.1.0\lib\net20\JetBrains.Annotations.dll</HintPath> | ||
| </Reference> | ||
| <Reference Include="PresentationCore" /> | ||
| <Reference Include="System" /> | ||
| <Reference Include="System.Core" /> | ||
| <Reference Include="System.Data" /> | ||
| <Reference Include="System.Xml" /> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <Compile Include="ProcessAddin.cs" /> | ||
| <Compile Include="ProcessProvider.cs" /> | ||
| <Compile Include="ProcessResult.cs" /> | ||
| <Compile Include="Properties\AssemblyInfo.cs" /> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <AdditionalFiles Include="..\stylecop.json" Link="stylecop.json" /> | ||
| <Analyzer Include="..\packages\StyleCop.Analyzers.1.0.2\analyzers\dotnet\cs\StyleCop.Analyzers.CodeFixes.dll" /> | ||
| <Analyzer Include="..\packages\StyleCop.Analyzers.1.0.2\analyzers\dotnet\cs\StyleCop.Analyzers.dll" /> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <ProjectReference Include="..\Jarvis.Core\Jarvis.Core.csproj"> | ||
| <Project>{5826d320-9026-4be9-8c82-893c55b80871}</Project> | ||
| <Name>Jarvis.Core</Name> | ||
| </ProjectReference> | ||
| </ItemGroup> | ||
| <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | ||
| <!-- To modify your build process, add your task inside one of the targets below and uncomment it. | ||
| Other similar extension points exist, see Microsoft.Common.targets. | ||
| <Target Name="BeforeBuild"> | ||
| </Target> | ||
| <Target Name="AfterBuild"> | ||
| </Target> | ||
| --> | ||
| </Project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| using Autofac; | ||
| using Jarvis.Core; | ||
|
|
||
| namespace Jarvis.Addin.Processes | ||
| { | ||
| public sealed class ProcessAddin : IAddin | ||
| { | ||
| public void Configure(ContainerBuilder builder) | ||
| { | ||
| builder.RegisterType<ProcessProvider>().As<IQueryProvider>().SingleInstance(); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.Linq; | ||
| using System.Threading.Tasks; | ||
| using System.Windows.Media; | ||
| using System.Windows.Media.Imaging; | ||
| using Jarvis.Addin.Processes; | ||
| using Jarvis.Core; | ||
| using Jarvis.Core.Diagnostics; | ||
| using Jarvis.Core.Interop; | ||
| using Jarvis.Core.Scoring; | ||
| using JetBrains.Annotations; | ||
|
|
||
| namespace Jarvis.Addin.Processes | ||
| { | ||
| [UsedImplicitly] | ||
| internal sealed class ProcessProvider : QueryProvider<ProcessResult> | ||
| { | ||
| private readonly IJarvisLog _log; | ||
|
|
||
| public ProcessProvider(IJarvisLog log) | ||
| { | ||
| _log = log; | ||
| } | ||
|
|
||
| protected override Task<ImageSource> GetIconAsync(ProcessResult result) | ||
| { | ||
| return Task.FromResult(null as ImageSource); | ||
| } | ||
|
|
||
| public override Task<IEnumerable<IQueryResult>> QueryAsync(Query query) | ||
| { | ||
| return Task.Run(() => | ||
| { | ||
| var results = Process.GetProcesses() | ||
| .Where(process => process.MainWindowTitle.IndexOf(query.Raw, StringComparison.OrdinalIgnoreCase) >= 0) | ||
| .Select(process => (IQueryResult) new ProcessResult(process.Id, process.MainWindowTitle, process.ProcessName, LevenshteinScorer.Score(process.MainWindowTitle, query.Raw, false), LevenshteinScorer.Score(process.MainWindowTitle, query.Raw))) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should break line in the middle |
||
| .ToList(); | ||
|
|
||
| return results.AsEnumerable(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can simplify by removing results variable |
||
| }); | ||
| } | ||
|
|
||
| protected override Task ExecuteAsync(ProcessResult result) | ||
| { | ||
| var process = Process.GetProcessById(result.ProcessId); | ||
| Win32.Window.SetForegroundWindow(process.MainWindowHandle); | ||
|
|
||
| var rect = new Win32.W32Rect(); | ||
| Win32.Window.GetWindowRect(process.MainWindowHandle, ref rect); | ||
| if (rect.Top < 0 && rect.Right < 0 && rect.Bottom < 0 && rect.Left < 0) // Window is minimized | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it can be replaced with https://msdn.microsoft.com/en-us/library/windows/desktop/ms633527(v=vs.85).aspx |
||
| { | ||
| Win32.Window.ShowWindow(process.MainWindowHandle, 1); | ||
| } | ||
|
|
||
| return Task.CompletedTask; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| using System; | ||
| using System.Diagnostics; | ||
| using Jarvis.Core; | ||
| using JetBrains.Annotations; | ||
|
|
||
| namespace Jarvis.Addin.Processes | ||
| { | ||
| [UsedImplicitly] | ||
| [DebuggerDisplay("{" + nameof(Title) + ",nq}")] | ||
| internal struct ProcessResult : IQueryResult, IEquatable<ProcessResult> | ||
| { | ||
| public int ProcessId { get; } | ||
| public string Title { get; } | ||
| public string Description { get; } | ||
| public float Distance { get; } | ||
| public float Score { get; } | ||
| public QueryResultType Type => QueryResultType.Application; | ||
|
|
||
| public ProcessResult(int processId, string title, string description, | ||
| float distance, float score) | ||
| { | ||
| ProcessId = processId; | ||
| Title = title; | ||
| Description = description; | ||
| Distance = distance; | ||
| Score = score; | ||
| } | ||
|
|
||
| public bool Equals(IQueryResult obj) | ||
| { | ||
| return obj != null && obj.GetType() == GetType() | ||
| && Equals((ProcessResult) obj); | ||
| } | ||
|
|
||
| public override bool Equals(object obj) | ||
| { | ||
| return obj != null && (obj.GetType() == GetType() | ||
| && Equals((ProcessResult) obj)); | ||
| } | ||
|
|
||
| public bool Equals(ProcessResult other) | ||
| { | ||
| return other.ProcessId.Equals(ProcessId); | ||
| } | ||
|
|
||
| public override int GetHashCode() | ||
| { | ||
| return ProcessId.GetHashCode(); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| using System.Reflection; | ||
| using System.Runtime.InteropServices; | ||
| using Jarvis.Addin.Processes; | ||
| using Jarvis.Core; | ||
|
|
||
| // General Information about an assembly is controlled through the following | ||
| // set of attributes. Change these attribute values to modify the information | ||
| // associated with an assembly. | ||
| [assembly: AssemblyTitle("Jarvis.Addin.Processes")] | ||
| [assembly: AssemblyDescription("")] | ||
|
|
||
| // Setting ComVisible to false makes the types in this assembly not visible | ||
| // to COM components. If you need to access a type in this assembly from | ||
| // COM, set the ComVisible attribute to true on that type. | ||
| [assembly: ComVisible(false)] | ||
|
|
||
| // The following GUID is for the ID of the typelib if this project is exposed to COM | ||
| [assembly: Guid("AE5C446D-B1D3-4E98-941D-A751B80CA74F")] | ||
|
|
||
| // The addin definition. | ||
| [assembly: Addin(typeof(ProcessAddin))] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How is the speed using C# Process class, when I was testing it got pretty slow