Skip to content

Commit

Permalink
Initial Chrome support
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Vallat committed Dec 26, 2020
1 parent 6d3f399 commit fc7c32b
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 3 deletions.
34 changes: 34 additions & 0 deletions KeyLayoutAutoSwitch/Chrome.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Accessibility;
using CommandLine;

namespace KeyLayoutAutoSwitch
{
internal class Chrome : Browser
{
public override FocusType GetFocusType(IAccessible accessibleObject, out string url)
{
url = null;

// Walk up tree finding parent
var parent = accessibleObject;
while (parent != null)
{
var role = AccessibleObjectHelper.GetRole(parent);
if (role == AccessibleRole.Document && AccessibleObjectHelper.HasState(accessibleObject, AccessibleStates.Focusable))
{
// This is a web page
url = AccessibleObjectHelper.GetValue(parent);
return FocusType.Page;
}

parent = parent.accParent as IAccessible;
}

return FocusType.Other;
}
}
}
45 changes: 45 additions & 0 deletions KeyLayoutAutoSwitch/ChromeWidgets.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Accessibility;
using CommandLine;

namespace KeyLayoutAutoSwitch
{
internal class ChromeWidgets : Browser
{
public override FocusType GetFocusType(IAccessible accessibleObject, out string url)
{
url = null;

// Walk up tree finding parent
var parent = accessibleObject;
while (parent != null)
{
var role = AccessibleObjectHelper.GetRole(parent);
if (role == AccessibleRole.Text && AccessibleObjectHelper.HasState(accessibleObject, AccessibleStates.Focusable))
{
// Could be the location bar, if the parent is a grouping
if (accessibleObject.accParent is IAccessible immediateParent)
{
if (AccessibleObjectHelper.GetRole(immediateParent) == AccessibleRole.Grouping && (AccessibleStates)immediateParent.accState[0] == AccessibleStates.None)
{
return FocusType.Location;
}
else
{
if (AccessibleObjectHelper.FindAncestor(immediateParent, AccessibleRole.Dialog) != null)
{
return FocusType.FindInPage;
}
}
}
}
parent = parent.accParent as IAccessible;
}

return FocusType.Other;
}
}
}
2 changes: 2 additions & 0 deletions KeyLayoutAutoSwitch/KeyLayoutAutoSwitch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@
<ItemGroup>
<Compile Include="AccessibleObjectHelper.cs" />
<Compile Include="Browser.cs" />
<Compile Include="ChromeWidgets.cs" />
<Compile Include="ExtensionMethods.cs" />
<Compile Include="Chrome.cs" />
<Compile Include="GlobalSuppressions.cs" />
<Compile Include="PreviouslyVisitedPageRuleEditor.cs">
<SubType>Form</SubType>
Expand Down
14 changes: 11 additions & 3 deletions KeyLayoutAutoSwitch/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,20 @@ private void OnFocusChanged(IntPtr hwnd, uint idObject, uint idChild)
{
var className = NativeMethods.GetWindowClassName(hwnd);

//Debug.WriteLine($"Focus changed to window: {hwnd} ({className}), object {idObject}, child {idChild}, layout {NativeMethods.GetKeyboardLayout(hwnd)}");
Debug.WriteLine($"Focus changed to window: {hwnd} ({className}), object {idObject}, child {idChild}, layout {NativeMethods.GetKeyboardLayout(hwnd)}");

Browser browser = null;
if (className == "MozillaWindowClass")
switch (className)
{
browser = new Firefox();
case "MozillaWindowClass":
browser = new Firefox();
break;
case "Chrome_RenderWidgetHostHWND":
browser = new Chrome();
break;
case "Chrome_WidgetWin_1":
browser = new ChromeWidgets();
break;
}
if (browser != null)
{
Expand Down

0 comments on commit fc7c32b

Please sign in to comment.