-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathNtUiLib.AutoCompletion.pas
More file actions
283 lines (236 loc) · 7.3 KB
/
NtUiLib.AutoCompletion.pas
File metadata and controls
283 lines (236 loc) · 7.3 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
unit NtUiLib.AutoCompletion;
{
The module provides functions for creating custom auto-completion lists
similar to those created by SHAutoComplete.
}
interface
uses
Ntapi.WinNt, Ntapi.Shlwapi, Ntapi.ObjBase, NtUtils;
type
TAutoCompletionCallback = reference to function (
const Root: String;
out Suggestions: TArray<String>
): TNtxStatus;
IAutoCompletionSuggestions = interface
['{049B6656-ACB8-46E1-B02B-E3C9A933B5A7}']
function GetSuggestions: TArray<String>;
function Expand(const Root: String): TNtxStatus;
end;
// Prepare a static list of suggestions
function ShlxPrepareStatisSuggestions(
const Strings: TArray<String>
): IAutoCompletionSuggestions;
// Prepare a dynamic (hierarchical) list of suggestions
function ShlxPrepareDynamicSuggestions(
const Callback: TAutoCompletionCallback
): IAutoCompletionSuggestions;
// Add auto-completion suggestions to an Edit-derived control.
// Note: The caller is responsible for keeping the passed provider alive for as
// long as they want to see suggestions (such as up to control destruction).
// This is necessary because Windows 11 started leaking auto-completion list
// objects, and we don't want it to indefinitely retain our suggestion
// provider's resources. As a workaround, we use a proxy with a weak reference
// that will disconnect the provider upon its destruction and only leak a small
// proxy instead.
[RequiresCOM]
function ShlxEnableSuggestions(
EditControl: THwnd;
Provider: IAutoCompletionSuggestions;
Options: Cardinal = ACO_AUTOSUGGEST or ACO_UPDOWNKEYDROPSLIST
): TNtxStatus;
implementation
uses
Ntapi.ObjIdl, Ntapi.WinError, Ntapi.ShellApi, Ntapi.Versions, NtUtils.WinUser,
DelphiApi.Reflection, NtUtils.Com, DelphiUtils.AutoObjects;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
{ TACListProxy }
type
// Note: we want to implementation to be able to disconnect from the
// enumerator since Windows 11 leaks the interface by not correctly managing
// its lifetime.
TACListProxy = class (TAutoInterfacedObject, IEnumString, IACList)
private
FEditControl: THwnd;
FProvider: Weak<IAutoCompletionSuggestions>;
FIndex: Integer;
function Next(
[in, NumberOfElements] Count: Integer;
[out, WritesTo, ReleaseWith('CoTaskMemFree')] out Elements:
TAnysizeArray<PWideChar>;
[out, NumberOfElements] out Fetched: Integer
): HResult; stdcall;
function Skip(
[in, NumberOfElements] Count: Integer
): HResult; stdcall;
function Reset(
): HResult; stdcall;
function Clone(
[out] out Enm: IEnumString
): HResult; stdcall;
function Expand(Root: PWideChar): HResult; stdcall;
constructor Create(
EditControl: THwnd;
const Provider: IAutoCompletionSuggestions;
Index: Integer = 0
);
end;
function TACListProxy.Clone;
var
StrongRef: IAutoCompletionSuggestions;
begin
if FProvider.Upgrade(StrongRef) then
begin
Enm := TACListProxy.Create(FEditControl, StrongRef, FIndex);
Result := S_OK
end
else
Result := RPC_E_DISCONNECTED;
end;
constructor TACListProxy.Create;
begin
inherited Create;
FEditControl := EditControl;
FProvider := Provider;
// Windows 11's implementation of IAutoComplete leaks our objects. We
// cannot do much about it aside from using a weak reference to the suggestion
// provider (so we don't keep it alive indefinitely) and registering this
// object as an expected memor leak (to prevent ReportMemoryLeaksOnShutdown
// from complaining).
if RtlOsVersionAtLeast(OsWin11) then
begin
SysRegisterExpectedMemoryLeak(Self);
SysRegisterExpectedMemoryLeak(FProvider.WeakReference as TObject);
end;
end;
function TACListProxy.Expand;
var
StrongRef: IAutoCompletionSuggestions;
begin
if FProvider.Upgrade(StrongRef) then
Result := StrongRef.Expand(String(Root)).HResult
else
Result := RPC_E_DISCONNECTED;
end;
function TACListProxy.Next;
var
StrongRef: IAutoCompletionSuggestions;
Strings: TArray<String>;
Buffer: PWideChar;
begin
if not FProvider.Upgrade(StrongRef) then
Exit(RPC_E_DISCONNECTED);
// Collect suggestions from the provider
Strings := StrongRef.GetSuggestions;
Fetched := 0;
// Return strings until we satisfy the count or have nothing left
while (Fetched < Count) and (FIndex >= 0) and (FIndex <= High(Strings)) do
begin
// The caller is responsible for freeing each string
Buffer := CoTaskMemAlloc(StringSizeZero(Strings[FIndex]));
if not Assigned(Buffer) then
begin
// Undo previous allocations on failure mid-way
Dec(Fetched);
while Fetched >= 0 do
begin
CoTaskMemFree(Elements{$R-}[Fetched]{$IFDEF R+}{$R+}{$ENDIF});
Elements{$R-}[Fetched]{$IFDEF R+}{$R+}{$ENDIF} := nil;
Dec(Fetched);
Dec(FIndex);
end;
Fetched := 0;
Exit(E_OUTOFMEMORY);
end;
MarshalString(Strings[FIndex], Buffer);
Elements{$R-}[Fetched]{$IFDEF R+}{$R+}{$ENDIF} := Buffer;
Inc(Fetched);
Inc(FIndex);
end;
if Fetched = Count then
Result := S_OK
else
Result := S_FALSE;
end;
function TACListProxy.Reset;
var
CurrentText: String;
begin
// For some reason, AutoComplete does not call Expand on the root; fix it.
if UsrxGetWindowText(FEditControl, CurrentText).IsSuccess and
(Pos('\', CurrentText) <= 0) then
Expand(nil);
FIndex := 0;
Result := S_OK;
end;
function TACListProxy.Skip;
begin
Inc(FIndex, Count);
Result := S_OK;
end;
{ TAutoCompletionSuggestions }
type
TAutoCompletionSuggestions = class (TAutoInterfacedObject,
IAutoCompletionSuggestions)
private
FStrings: TArray<String>;
FCallback: TAutoCompletionCallback;
public
function GetSuggestions: TArray<String>;
function Expand(const Root: String): TNtxStatus;
constructor Create(
const InitialStrings: TArray<String>;
const Callback: TAutoCompletionCallback
);
end;
constructor TAutoCompletionSuggestions.Create;
begin
inherited Create;
FStrings := InitialStrings;
FCallback := Callback;
end;
function TAutoCompletionSuggestions.Expand;
begin
if Assigned(FCallback) then
// Update the suggestions list
Result := FCallback(Root, FStrings)
else
// Use the initial list
Result := NtxSuccess;
end;
function TAutoCompletionSuggestions.GetSuggestions;
begin
Result := FStrings;
end;
{ Functions }
function ShlxPrepareStatisSuggestions;
begin
Result := TAutoCompletionSuggestions.Create(Strings, nil);
end;
function ShlxPrepareDynamicSuggestions;
begin
Result := TAutoCompletionSuggestions.Create(nil, Callback);
end;
function ShlxEnableSuggestions;
var
AutoComplete: IAutoComplete2;
ACList: IACList;
begin
// Create an instance of CLSID_AutoComplete (provided by the OS)
Result := ComxCreateInstanceWithFallback(shell32, CLSID_AutoComplete,
IAutoComplete2, AutoComplete, 'CLSID_AutoComplete');
if not Result.IsSuccess then
Exit;
// Adjust options
Result.Location := 'IAutoComplete2::SetOptions';
Result.HResult := AutoComplete.SetOptions(Options);
if not Result.IsSuccess then
Exit;
// Create our custom IACList that [weak-]references the suggestioon provider
ACList := TACListProxy.Create(EditControl, Provider);
// Register our suggestions
Result.Location := 'IAutoComplete::Init';
Result.HResult := AutoComplete.Init(EditControl, ACList, nil, nil);
end;
end.