|
4 | 4 |
|
5 | 5 | using System.Collections.ObjectModel;
|
6 | 6 | using CommunityToolkit.Mvvm.ComponentModel;
|
7 |
| -using CommunityToolkit.Mvvm.Messaging; |
8 |
| -using Microsoft.CmdPal.UI.ViewModels.Messages; |
| 7 | +using ManagedCommon; |
9 | 8 | using Microsoft.CmdPal.UI.ViewModels.Settings;
|
10 | 9 | using Microsoft.CommandPalette.Extensions;
|
11 | 10 | using Microsoft.CommandPalette.Extensions.Toolkit;
|
@@ -237,32 +236,46 @@ private void DoOnUiThread(Action action)
|
237 | 236 | }
|
238 | 237 | }
|
239 | 238 |
|
240 |
| - public void TryUpdateFallbackText(string newQuery) |
| 239 | + internal bool SafeUpdateFallbackTextSynchronous(string newQuery) |
241 | 240 | {
|
242 | 241 | if (!IsFallback)
|
243 | 242 | {
|
244 |
| - return; |
| 243 | + return false; |
245 | 244 | }
|
246 | 245 |
|
247 |
| - _ = Task.Run(() => |
| 246 | + try |
248 | 247 | {
|
249 |
| - try |
250 |
| - { |
251 |
| - var model = _commandItemViewModel.Model.Unsafe; |
252 |
| - if (model is IFallbackCommandItem fallback) |
253 |
| - { |
254 |
| - var wasEmpty = string.IsNullOrEmpty(Title); |
255 |
| - fallback.FallbackHandler.UpdateQuery(newQuery); |
256 |
| - var isEmpty = string.IsNullOrEmpty(Title); |
257 |
| - if (wasEmpty != isEmpty) |
258 |
| - { |
259 |
| - WeakReferenceMessenger.Default.Send<UpdateFallbackItemsMessage>(); |
260 |
| - } |
261 |
| - } |
262 |
| - } |
263 |
| - catch (Exception) |
264 |
| - { |
265 |
| - } |
266 |
| - }); |
| 248 | + return UnsafeUpdateFallbackSynchronous(newQuery); |
| 249 | + } |
| 250 | + catch (Exception ex) |
| 251 | + { |
| 252 | + Logger.LogError(ex.ToString()); |
| 253 | + } |
| 254 | + |
| 255 | + return false; |
| 256 | + } |
| 257 | + |
| 258 | + /// <summary> |
| 259 | + /// Calls UpdateQuery on our command, if we're a fallback item. This does |
| 260 | + /// RPC work, so make sure you're calling it on a BG thread. |
| 261 | + /// </summary> |
| 262 | + /// <param name="newQuery">The new search text to pass to the extension</param> |
| 263 | + /// <returns>true if our Title changed across this call</returns> |
| 264 | + private bool UnsafeUpdateFallbackSynchronous(string newQuery) |
| 265 | + { |
| 266 | + var model = _commandItemViewModel.Model.Unsafe; |
| 267 | + |
| 268 | + // RPC to check type |
| 269 | + if (model is IFallbackCommandItem fallback) |
| 270 | + { |
| 271 | + var wasEmpty = string.IsNullOrEmpty(Title); |
| 272 | + |
| 273 | + // RPC for method |
| 274 | + fallback.FallbackHandler.UpdateQuery(newQuery); |
| 275 | + var isEmpty = string.IsNullOrEmpty(Title); |
| 276 | + return wasEmpty != isEmpty; |
| 277 | + } |
| 278 | + |
| 279 | + return false; |
267 | 280 | }
|
268 | 281 | }
|
0 commit comments