Compare commits

...

3 Commits

Author SHA1 Message Date
oguzhankoral 7d14b85a9d wip wip 2024-11-30 01:36:40 +03:00
oguzhankoral 00404c9b27 wip 2024-11-30 01:36:40 +03:00
Adam Hathcock dbfdce568f update UI only every 50 times 2024-11-30 01:36:40 +03:00
7 changed files with 100 additions and 14 deletions
@@ -52,7 +52,12 @@ public sealed class AutocadReceiveBinding : IReceiveBinding
public void CancelReceive(string modelCardId) => _cancellationManager.CancelOperation(modelCardId);
public async Task Receive(string modelCardId)
public async Task Receive(string modelCardId) =>
await Parent
.RunOnMainThreadAsync(async () => await ReceiveInternal(modelCardId).ConfigureAwait(false))
.ConfigureAwait(false);
public async Task ReceiveInternal(string modelCardId)
{
using var scope = _serviceProvider.CreateScope();
scope
@@ -129,8 +129,7 @@ public class AutocadHostObjectBuilder : IHostObjectBuilder
onOperationProgressed.Report(new("Converting objects", (double)++count / atomicObjects.Count));
try
{
List<Entity> convertedObjects = await ConvertObject(atomicObject, layerPath, baseLayerPrefix)
.ConfigureAwait(true);
List<Entity> convertedObjects = ConvertObject(atomicObject, layerPath, baseLayerPrefix);
applicationIdMap[objectId] = convertedObjects;
@@ -181,7 +180,7 @@ public class AutocadHostObjectBuilder : IHostObjectBuilder
_materialBaker.PurgeMaterials(baseLayerPrefix);
}
private async Task<List<Entity>> ConvertObject(Base obj, Collection[] layerPath, string baseLayerNamePrefix)
private List<Entity> ConvertObject(Base obj, Collection[] layerPath, string baseLayerNamePrefix)
{
string layerName = _layerBaker.CreateLayerForReceive(layerPath, baseLayerNamePrefix);
var convertedEntities = new List<Entity>();
@@ -204,7 +203,6 @@ public class AutocadHostObjectBuilder : IHostObjectBuilder
}
tr.Commit();
await Task.Delay(10).ConfigureAwait(true);
return convertedEntities;
}
@@ -79,6 +79,20 @@ public sealed partial class DUI3ControlWebView : UserControl, IBrowserScriptExec
_ = await callbackTask.ConfigureAwait(false);
}
public void ExecuteScriptSyncMethod(string script, CancellationToken cancellationToken)
{
if (!Browser.IsInitialized)
{
throw new InvalidOperationException("Failed to execute script, Webview2 is not initialized yet.");
}
Browser.Dispatcher.Invoke(
() => Browser.ExecuteScriptAsync(script),
DispatcherPriority.Background,
cancellationToken
);
}
private void OnInitialized(object? sender, CoreWebView2InitializationCompletedEventArgs e)
{
if (!e.IsSuccess)
@@ -30,15 +30,16 @@ public class OperationProgressManager : IOperationProgressManager
)
{
var progress = new NonUIThreadProgress<CardProgress>(args =>
bridge.TopLevelExceptionHandler.FireAndForget(
() =>
SetModelProgress(
bridge,
modelCardId,
new ModelCardProgress(modelCardId, args.Status, args.Progress),
cancellationToken
)
)
bridge.TopLevelExceptionHandler.FireAndForget(() =>
{
SetModelProgressSync(
bridge,
modelCardId,
new ModelCardProgress(modelCardId, args.Status, args.Progress),
cancellationToken
);
return Task.CompletedTask;
})
);
return progress;
}
@@ -75,6 +76,41 @@ public class OperationProgressManager : IOperationProgressManager
await SendProgress(bridge, modelCardId, progress).ConfigureAwait(false);
}
public void SetModelProgressSync(
IBrowserBridge bridge,
string modelCardId,
ModelCardProgress progress,
CancellationToken cancellationToken
)
{
if (cancellationToken.IsCancellationRequested)
{
return;
}
if (!s_lastProgressValues.TryGetValue(modelCardId, out (DateTime, string) t))
{
t.Item1 = DateTime.Now;
s_lastProgressValues[modelCardId] = (t.Item1, progress.Status);
// Since it's the first time we get a call for this model card, we should send it out
SendProgressSync(bridge, modelCardId, progress);
return;
}
var currentTime = DateTime.Now;
var elapsedMs = (currentTime - t.Item1).Milliseconds;
if (elapsedMs < THROTTLE_INTERVAL_MS && t.Item2 == progress.Status)
{
return;
}
s_lastProgressValues[modelCardId] = (currentTime, progress.Status);
SendProgressSync(bridge, modelCardId, progress);
}
private static async Task SendProgress(IBrowserBridge bridge, string modelCardId, ModelCardProgress progress) =>
await bridge.Send(SET_MODEL_PROGRESS_UI_COMMAND_NAME, new { modelCardId, progress }).ConfigureAwait(false);
private static void SendProgressSync(IBrowserBridge bridge, string modelCardId, ModelCardProgress progress) =>
bridge.SendSync(SET_MODEL_PROGRESS_UI_COMMAND_NAME, new { modelCardId, progress });
}
@@ -365,4 +365,30 @@ public sealed class BrowserBridge : IBrowserBridge
var script = $"{FrontendBoundName}.emitResponseReady('{eventName}', '{requestId}')";
await _browserScriptExecutor.ExecuteScriptAsyncMethod(script, cancellationToken).ConfigureAwait(false);
}
public void SendSync(string eventName, CancellationToken cancellationToken = default)
{
if (_binding is null)
{
throw new InvalidOperationException("Bridge was not initialized with a binding");
}
var script = $"{FrontendBoundName}.emit('{eventName}')";
_browserScriptExecutor.ExecuteScriptSyncMethod(script, cancellationToken);
}
public void SendSync<T>(string eventName, T data, CancellationToken cancellationToken = default)
where T : class
{
if (_binding is null)
{
throw new InvalidOperationException("Bridge was not initialized with a binding");
}
string payload = _jsonSerializer.Serialize(data);
string requestId = $"{Guid.NewGuid()}_{eventName}";
_resultsStore[requestId] = payload;
var script = $"{FrontendBoundName}.emitResponseReady('{eventName}', '{requestId}')";
_browserScriptExecutor.ExecuteScriptSyncMethod(script, cancellationToken);
}
}
@@ -49,6 +49,8 @@ public interface IBrowserBridge
/// <exception cref="InvalidOperationException">Bridge was not initialized with a binding</exception>
public Task Send(string eventName, CancellationToken cancellationToken = default);
public void SendSync(string eventName, CancellationToken cancellationToken = default);
/// <inheritdoc cref="Send(string, CancellationToken)"/>
/// <param name="data">data to store</param>
/// <typeparam name="T"></typeparam>
@@ -56,5 +58,8 @@ public interface IBrowserBridge
public Task Send<T>(string eventName, T data, CancellationToken cancellationToken = default)
where T : class;
public void SendSync<T>(string eventName, T data, CancellationToken cancellationToken = default)
where T : class;
public ITopLevelExceptionHandler TopLevelExceptionHandler { get; }
}
@@ -6,6 +6,8 @@ public interface IBrowserScriptExecutor
/// <param name="script">The (constant string) script to execute on the browser</param>
public Task ExecuteScriptAsyncMethod(string script, CancellationToken cancellationToken);
public void ExecuteScriptSyncMethod(string script, CancellationToken cancellationToken);
public bool IsBrowserInitialized { get; }
public object BrowserElement { get; }