-
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
Open
jonstodle
wants to merge
11
commits into
spectresystems:develop
Choose a base branch
from
jonstodle:focus-on-window-via-process
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b347eb4
6fd0288
439d3b3
2722b9c
5d0e9c7
6ecfe0f
96a3c97
2fbe935
2e79b2c
b52fdd5
c50cdf3
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| 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; | ||
| private readonly TimeSpan _cacheExpirationDuration; | ||
| private bool _isUpdatingCache; | ||
| private Process[] _processesCache; | ||
| private DateTimeOffset _cacheTimestamp; | ||
|
|
||
| public ProcessProvider(IJarvisLog log) | ||
| { | ||
| _log = log; | ||
| _cacheExpirationDuration = TimeSpan.FromSeconds(10); | ||
| _isUpdatingCache = false; | ||
| _processesCache = new Process[0]; | ||
| _cacheTimestamp = DateTimeOffset.MinValue; | ||
|
|
||
| UpdateProcessesCache(); | ||
| } | ||
|
|
||
| protected override Task<ImageSource> GetIconAsync(ProcessResult result) | ||
| { | ||
| return Task.FromResult(null as ImageSource); | ||
| } | ||
|
|
||
| public override Task<IEnumerable<IQueryResult>> QueryAsync(Query query) | ||
| { | ||
| return Task.Run(() => | ||
| { | ||
| UpdateProcessesCache(); | ||
|
|
||
| return (IEnumerable<IQueryResult>) _processesCache | ||
| .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))) | ||
| .ToList(); | ||
| }); | ||
| } | ||
|
|
||
| protected override Task ExecuteAsync(ProcessResult result) | ||
| { | ||
| var process = Process.GetProcessById(result.ProcessId); | ||
| Win32.Window.SetForegroundWindow(process.MainWindowHandle); | ||
|
|
||
| if (Win32.Window.IsIconic(process.MainWindowHandle)) // Window is minimized | ||
| { | ||
| Win32.Window.ShowWindow(process.MainWindowHandle, 1); | ||
| } | ||
|
|
||
| return Task.CompletedTask; | ||
| } | ||
|
|
||
| private void UpdateProcessesCache() | ||
| { | ||
| if(DateTimeOffset.Now - _cacheTimestamp < _cacheExpirationDuration) return; | ||
| if (_isUpdatingCache) return; | ||
|
|
||
| _isUpdatingCache = true; | ||
|
|
||
| _cacheTimestamp = DateTimeOffset.Now; | ||
| _processesCache = Process.GetProcesses() | ||
| .Where(process => process.Responding && | ||
| !string.IsNullOrWhiteSpace(process.MainWindowTitle)) | ||
| .ToArray(); | ||
|
|
||
| _isUpdatingCache = false; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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))] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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