forked from github/copilot-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
89 lines (78 loc) · 2.79 KB
/
Program.cs
File metadata and controls
89 lines (78 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text.Json;
using System.Windows.Automation;
namespace UIAWrapper
{
class Program
{
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
static void Main(string[] args)
{
IntPtr handle = GetForegroundWindow();
if (handle == IntPtr.Zero) return;
AutomationElement root = AutomationElement.FromHandle(handle);
var node = BuildTree(root);
string json = JsonSerializer.Serialize(node, new JsonSerializerOptions { WriteIndented = true });
Console.WriteLine(json);
}
static UIANode BuildTree(AutomationElement element)
{
var rectangle = element.Current.BoundingRectangle;
var node = new UIANode
{
id = element.Current.AutomationId,
name = element.Current.Name,
role = element.Current.ControlType.ProgrammaticName.Replace("ControlType.", ""),
bounds = new Bounds
{
x = SafeNumber(rectangle.X),
y = SafeNumber(rectangle.Y),
width = SafeNumber(rectangle.Width),
height = SafeNumber(rectangle.Height)
},
isClickable = (bool)element.GetCurrentPropertyValue(AutomationElement.IsInvokePatternAvailableProperty) || element.Current.IsKeyboardFocusable,
isFocusable = element.Current.IsKeyboardFocusable,
children = new List<UIANode>()
};
var walker = TreeWalker.ControlViewWalker;
var child = walker.GetFirstChild(element);
while (child != null)
{
try
{
if (!child.Current.IsOffscreen)
{
node.children.Add(BuildTree(child));
}
}
catch (ElementNotAvailableException) { }
child = walker.GetNextSibling(child);
}
return node;
}
static double SafeNumber(double value)
{
return double.IsFinite(value) ? value : 0;
}
}
class UIANode
{
public string id { get; set; }
public string name { get; set; }
public string role { get; set; }
public Bounds bounds { get; set; }
public bool isClickable { get; set; }
public bool isFocusable { get; set; }
public List<UIANode> children { get; set; }
}
class Bounds
{
public double x { get; set; }
public double y { get; set; }
public double width { get; set; }
public double height { get; set; }
}
}