using System.Collections.Specialized; using Godot; using ObservableCollections; using SharpIDE.Application.Features.SolutionDiscovery.VsPersistence; using SharpIDE.Godot.Features.Problems; namespace SharpIDE.Godot; public static class ControlExtensions { // extension(Control control) // { // public void BindChildren(ObservableHashSet list, PackedScene scene) // { // var view = list.CreateView(x => // { // var node = scene.Instantiate(); // node.Project = x; // Callable.From(() => control.AddChild(node)).CallDeferred(); // return node; // }); // view.ViewChanged += OnViewChanged; // } // private static void OnViewChanged(in SynchronizedViewChangedEventArgs eventArgs) // { // GD.Print("View changed: " + eventArgs.Action); // if (eventArgs.Action == NotifyCollectionChangedAction.Remove) // { // eventArgs.OldItem.View.QueueFree(); // } // } // } } public static class NodeExtensions { extension(Node node) { public Task InvokeAsync(Action workItem) { var taskCompletionSource = new TaskCompletionSource(); //WorkerThreadPool.AddTask(); Dispatcher.SynchronizationContext.Post(_ => { try { workItem(); taskCompletionSource.SetResult(); } catch (Exception ex) { taskCompletionSource.SetException(ex); } }, null); return taskCompletionSource.Task; } public Task InvokeAsync(Func workItem) { var taskCompletionSource = new TaskCompletionSource(); Dispatcher.SynchronizationContext.Post(async void (_) => { try { await workItem(); taskCompletionSource.SetResult(); } catch (Exception ex) { taskCompletionSource.SetException(ex); } }, null); return taskCompletionSource.Task; } public Task InvokeDeferredAsync(Action workItem) { var taskCompletionSource = new TaskCompletionSource(); //WorkerThreadPool.AddTask(); Callable.From(() => { try { workItem(); taskCompletionSource.SetResult(); } catch (Exception ex) { taskCompletionSource.SetException(ex); } }).CallDeferred(); return taskCompletionSource.Task; } public Task InvokeDeferredAsync(Func workItem) { var taskCompletionSource = new TaskCompletionSource(); //WorkerThreadPool.AddTask(); Callable.From(async void () => { try { await workItem(); taskCompletionSource.SetResult(); } catch (Exception ex) { taskCompletionSource.SetException(ex); } }).CallDeferred(); return taskCompletionSource.Task; } } } public static class GodotTask { extension(Task task) { public static async Task GodotRun(Action action) { await Task.Run(() => { try { action(); } catch (Exception ex) { GD.PrintErr($"Error: {ex}"); } }); } public static async Task GodotRun(Func action) { await Task.Run(async () => { try { await action(); } catch (Exception ex) { GD.PrintErr($"Error: {ex}"); } }); } } }