using Google.Protobuf.WellKnownTypes;
using Hi.Disp;
using Hi.Geom;
using System.Collections.Concurrent;
using System.Windows;
namespace Hi.WpfPlus.Disp;
///
/// Registers WPF as the display framework for ,
/// supporting multiple windows identified by key.
///
///
///
/// Usage pattern: call to queue display content,
/// then call to start the WPF application and show windows.
///
///
/// Each unique key creates a separate .
/// Calling with the same key updates the existing window.
///
///
///
/// // Queue display content (before or after Run)
/// DispFrameUtil.CallDispFrame("Window1", displayee1);
/// DispFrameUtil.CallDispFrame("Window2", displayee2);
/// // Start the WPF application (blocks until all windows are closed)
/// DispFrameWpf.Run();
///
///
///
public static class WpfDisp
{
static readonly ConcurrentDictionary KeyToWindowDictionary = new();
static WpfDisp()
{
DispFrameUtil.UpdateByDispEngineConfigFunc = ApplyConfig;
}
public static void Init() { }
static void ApplyConfig(string key, DispEngineConfig config)
{
if (!KeyToWindowDictionary.TryGetValue(key, out var window))
{
window = new RenderingWindow() { Title = key };
window.Closed += (s, e) => KeyToWindowDictionary.TryRemove(key, out _);
KeyToWindowDictionary[key] = window;
window.Show();
}
var dispEngine = window.GetDispEngine();
if (config.Displayee != null)
dispEngine.Displayee = config.Displayee;
if (config.SketchView == null)
config.SketchView = config.Displayee?.GetBox3d()?.FrontView;
if (config.SketchView != null)
dispEngine.SketchView = config.SketchView;
}
}