Skip to content

Commit 091f0ec

Browse files
committed
Fix possible handle issue
1 parent b690ca1 commit 091f0ec

File tree

1 file changed

+42
-18
lines changed

1 file changed

+42
-18
lines changed

Flow.Launcher.Infrastructure/QuickSwitch/Models/WindowsDialog.cs

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -80,20 +80,26 @@ internal class WindowsDialogTab : IQuickSwitchDialogWindowTab
8080

8181
private static readonly string ClassName = nameof(WindowsDialogTab);
8282

83-
private readonly bool _legacy = false;
83+
private bool _legacy { get; set; } = false;
8484

85-
private readonly HWND _pathControl;
86-
private readonly HWND _pathEditor;
87-
private readonly HWND _fileEditor;
88-
private readonly HWND _openButton;
85+
private HWND _pathControl { get; set; } = HWND.Null;
86+
private HWND _pathEditor { get; set; } = HWND.Null;
87+
private HWND _fileEditor { get; set; } = HWND.Null;
88+
private HWND _openButton { get; set; } = HWND.Null;
8989

9090
public WindowsDialogTab(HWND handle)
9191
{
9292
Handle = handle;
93+
GetPathControlEditor();
94+
GetFileEditor();
95+
GetOpenButton();
96+
}
9397

98+
private bool GetPathControlEditor()
99+
{
94100
// Get the handle of the path editor
95101
// The window with class name "ComboBoxEx32" is not visible when the path editor is not with the keyboard focus
96-
_pathControl = PInvoke.GetDlgItem(handle, 0x0000); // WorkerW
102+
_pathControl = PInvoke.GetDlgItem(Handle, 0x0000); // WorkerW
97103
_pathControl = PInvoke.GetDlgItem(_pathControl, 0xA005); // ReBarWindow32
98104
_pathControl = PInvoke.GetDlgItem(_pathControl, 0xA205); // Address Band Root
99105
_pathControl = PInvoke.GetDlgItem(_pathControl, 0x0000); // msctls_progress32
@@ -108,49 +114,65 @@ public WindowsDialogTab(HWND handle)
108114
}
109115
else
110116
{
117+
_legacy = false;
111118
_pathEditor = PInvoke.GetDlgItem(_pathControl, 0xA205); // ComboBox
112119
_pathEditor = PInvoke.GetDlgItem(_pathEditor, 0xA205); // Edit
113120
if (_pathEditor == HWND.Null)
114121
{
115122
Log.Error(ClassName, "Failed to find path editor handle");
123+
return false;
116124
}
117125
}
118126

127+
return true;
128+
}
129+
130+
private bool GetFileEditor()
131+
{
119132
// Get the handle of the file name editor of Open file dialog
120-
_fileEditor = PInvoke.GetDlgItem(handle, 0x047C); // ComboBoxEx32
133+
_fileEditor = PInvoke.GetDlgItem(Handle, 0x047C); // ComboBoxEx32
121134
_fileEditor = PInvoke.GetDlgItem(_fileEditor, 0x047C); // ComboBox
122135
_fileEditor = PInvoke.GetDlgItem(_fileEditor, 0x047C); // Edit
123136
if (_fileEditor == HWND.Null)
124137
{
125138
// Get the handle of the file name editor of Save/SaveAs file dialog
126-
_fileEditor = PInvoke.GetDlgItem(handle, 0x0000); // DUIViewWndClassName
139+
_fileEditor = PInvoke.GetDlgItem(Handle, 0x0000); // DUIViewWndClassName
127140
_fileEditor = PInvoke.GetDlgItem(_fileEditor, 0x0000); // DirectUIHWND
128141
_fileEditor = PInvoke.GetDlgItem(_fileEditor, 0x0000); // FloatNotifySink
129142
_fileEditor = PInvoke.GetDlgItem(_fileEditor, 0x0000); // ComboBox
130143
_fileEditor = PInvoke.GetDlgItem(_fileEditor, 0x03E9); // Edit
131144
if (_fileEditor == HWND.Null)
132145
{
133146
Log.Error(ClassName, "Failed to find file name editor handle");
147+
return false;
134148
}
135149
}
136150

151+
return true;
152+
}
153+
154+
private bool GetOpenButton()
155+
{
137156
// Get the handle of the open button
138-
_openButton = PInvoke.GetDlgItem(handle, 0x0001); // Open/Save/SaveAs Button
157+
_openButton = PInvoke.GetDlgItem(Handle, 0x0001); // Open/Save/SaveAs Button
139158
if (_openButton == HWND.Null)
140159
{
141160
Log.Error(ClassName, "Failed to find open button handle");
161+
return false;
142162
}
163+
164+
return true;
143165
}
144166

145167
public string GetCurrentFolder()
146168
{
147-
if (_pathEditor.IsNull) return string.Empty;
169+
if (_pathEditor.IsNull && !GetPathControlEditor()) return string.Empty;
148170
return GetWindowText(_pathEditor);
149171
}
150172

151173
public string GetCurrentFile()
152174
{
153-
if (_fileEditor.IsNull) return string.Empty;
175+
if (_fileEditor.IsNull && !GetFileEditor()) return string.Empty;
154176
return GetWindowText(_fileEditor);
155177
}
156178

@@ -160,16 +182,16 @@ public bool JumpFolder(string path, bool auto)
160182
{
161183
// https://github.com/idkidknow/Flow.Launcher.Plugin.DirQuickJump/issues/1
162184
// The dialog is a legacy one, so we edit file name text box directly
163-
if (_fileEditor.IsNull) return false;
185+
if (_fileEditor.IsNull && !GetFileEditor()) return false;
164186
SetWindowText(_fileEditor, path);
165187

166-
if (_openButton.IsNull) return false;
188+
if (_openButton.IsNull && !GetOpenButton()) return false;
167189
PInvoke.SendMessage(_openButton, PInvoke.BM_CLICK, 0, 0);
168190

169191
return true;
170192
}
171193

172-
if (_pathControl.IsNull) return false;
194+
if (_pathControl.IsNull && !GetPathControlEditor()) return false;
173195

174196
var timeOut = !SpinWait.SpinUntil(() =>
175197
{
@@ -182,23 +204,25 @@ public bool JumpFolder(string path, bool auto)
182204
return false;
183205
}
184206

185-
if (_pathEditor.IsNull) return false;
186-
207+
if (_pathEditor.IsNull && !GetPathControlEditor()) return false;
187208
SetWindowText(_pathEditor, path);
209+
188210
return true;
189211
}
190212

191213
public bool JumpFile(string path)
192214
{
193-
if (_fileEditor.IsNull) return false;
215+
if (_fileEditor.IsNull && !GetPathControlEditor()) return false;
194216
SetWindowText(_fileEditor, path);
217+
195218
return true;
196219
}
197220

198221
public bool Open()
199222
{
200-
if (_openButton.IsNull) return false;
223+
if (_openButton.IsNull && !GetOpenButton()) return false;
201224
PInvoke.PostMessage(_openButton, PInvoke.BM_CLICK, 0, 0);
225+
202226
return true;
203227
}
204228

0 commit comments

Comments
 (0)