using Telerik.WinControls; using Telerik.WinControls.Svg; using Telerik.WinControls.UI; using Telerik.WinControls.UI.SplashScreen; namespace Pilz.UI.Telerik.Dialogs; partial class RadFlyoutBase { public delegate void FlyoutCreatedEventHandler(FlyoutCreatedEventArgs e); public delegate void FlyoutClosedEventHandler(FlyoutClosedEventArgs e); public static event FlyoutCreatedEventHandler FlyoutCreated { add => flyoutCreatedHandlers.Add(value); remove => flyoutCreatedHandlers.Remove(value); } public static event FlyoutClosedEventHandler FlyoutClosed { add => flyoutCloseHandlers.Add(value); remove => flyoutCloseHandlers.Remove(value); } private static readonly List flyoutCreatedHandlers = []; private static readonly List flyoutCloseHandlers = []; private static object? tagToAssign = null; private static string? titleToAssing = null; private static RadSvgImage? iconToAssign = null; public static Control? ParentContext { get; private set; } = null; static RadFlyoutBase() { RadFlyoutManager.ContentCreated += RadFlyoutManager_ContentCreated; RadFlyoutManager.FlyoutClosed += RadFlyoutManager_FlyoutClosed; } private static void RadFlyoutManager_ContentCreated(ContentCreatedEventArgs e) { if (e.Content is RadFlyoutBase dialogBase) { if (tagToAssign != null) dialogBase.Tag = tagToAssign; if (titleToAssing != null) dialogBase.Title = titleToAssing; if (iconToAssign != null) dialogBase.TitleIcon = iconToAssign; var eventArgs = new FlyoutCreatedEventArgs(dialogBase); if (dialogBase is ILoadContent iLoadContent) iLoadContent.LoadContent(); else if (dialogBase is ILoadContentAsync iLoadContentAsync) Task.Run(iLoadContentAsync.LoadContentAsync).Wait(); foreach (var args in flyoutCreatedHandlers.ToArray()) { if (ParentContext != null) ParentContext?.Invoke(args, eventArgs); else args.Invoke(eventArgs); } } } private static void RadFlyoutManager_FlyoutClosed(global::Telerik.WinControls.UI.SplashScreen.FlyoutClosedEventArgs e) { if (e.Content is RadFlyoutBase dialogBase) { var eventArgs = new FlyoutClosedEventArgs((RadFlyoutBase)e.Content); foreach (var args in flyoutCloseHandlers.ToArray()) { if (ParentContext != null) ParentContext?.Invoke(args, eventArgs); else args.Invoke(eventArgs); } } ParentContext = null; } public static void Show(Control controlToAssociate, object? tag = null) { Show(controlToAssociate, null, null, tag: tag); } public static void Show(Control controlToAssociate, string? title, RadSvgImage? icon, object? tag = null) { Show(controlToAssociate, typeof(T), title, icon, tag: tag); } public static void Show(Control controlToAssociate, Type flyoutContentType, string? title, RadSvgImage? icon, object? tag = null) { tagToAssign = tag; titleToAssing = title; iconToAssign = icon; ParentContext = controlToAssociate; CloseFlyout(false); // Ensure it's closed! RadFlyoutManager.Show(controlToAssociate, flyoutContentType); } protected static void CloseFlyout() { CloseFlyout(true); } protected static void CloseFlyout(bool throwOnError) { if (throwOnError && ParentContext is null) throw new NullReferenceException(nameof(ParentContext)); if (ParentContext is null) TryCloseFlyout(); else ParentContext.BeginInvoke(TryCloseFlyout); } private static void TryCloseFlyout() { try { RadFlyoutManager.Close(); } catch (Exception) { // Ignore } } }