diff --git a/RevitLookup/Application.cs b/RevitLookup/Application.cs index fedad89b8..f515ee7f3 100644 --- a/RevitLookup/Application.cs +++ b/RevitLookup/Application.cs @@ -22,7 +22,7 @@ using System.IO; using System.Windows.Interop; using System.Windows.Media; -using Autodesk.Revit.DB.Events; +using System.Windows.Threading; using Nice3point.Revit.Toolkit.External; using Nice3point.Revit.Toolkit.External.Handlers; using RevitLookup.Core; @@ -34,6 +34,7 @@ namespace RevitLookup; [UsedImplicitly] public class Application : ExternalApplication { + private static Thread _thread; public static ActionEventHandler ActionEventHandler { get; private set; } public static AsyncEventHandler> ExternalElementHandler { get; private set; } public static AsyncEventHandler> ExternalDescriptorHandler { get; private set; } @@ -47,7 +48,7 @@ public override async void OnStartup() var settingsService = Host.GetService(); RibbonController.CreatePanel(Application, settingsService); - EnableHardwareRendering(settingsService); + RunDispatcher(settingsService); } public override async void OnShutdown() @@ -76,17 +77,35 @@ private static void SaveSettings() settingsService.Save(); } - private void EnableHardwareRendering(ISettingsService settingsService) + public static void RunDispatcher(ISettingsService settingsService) { if (!settingsService.IsHardwareRenderingAllowed) return; + if (_thread is not null) return; + + _thread = new Thread(Dispatcher.Run); + _thread.SetApartmentState(ApartmentState.STA); + _thread.Start(); //Revit overrides render mode during initialization - Application.ControlledApplication.ApplicationInitialized += OnInitialized; + //EventHandler is called after initialisation + ActionEventHandler.Raise(_ => RenderOptions.ProcessRenderMode = RenderMode.Default); + } + + public static void TerminateDispatcher(ISettingsService settingsService) + { + if (settingsService.IsHardwareRenderingAllowed) return; + if (_thread is null) return; + if (!_thread.IsAlive) return; - void OnInitialized(object sender, ApplicationInitializedEventArgs args) - { - Application.ControlledApplication.ApplicationInitialized -= OnInitialized; - RenderOptions.ProcessRenderMode = RenderMode.Default; - } + Dispatcher.FromThread(_thread)!.InvokeShutdown(); + _thread = null; + + RenderOptions.ProcessRenderMode = RenderMode.SoftwareOnly; + } + + public static void Invoke(Action action) + { + if (_thread is null) action.Invoke(); + else Dispatcher.FromThread(_thread)!.Invoke(action); } } \ No newline at end of file diff --git a/RevitLookup/Commands/DashboardCommand.cs b/RevitLookup/Commands/DashboardCommand.cs index 710e09946..72eed0b4c 100644 --- a/RevitLookup/Commands/DashboardCommand.cs +++ b/RevitLookup/Commands/DashboardCommand.cs @@ -33,8 +33,11 @@ public class DashboardCommand : ExternalCommand { public override void Execute() { - var window = Host.GetService(); - window.ShowAttached(); - window.Scope.GetService().Navigate(typeof(DashboardView)); + RevitLookup.Application.Invoke(() => + { + var window = Host.GetService(); + window.ShowAttached(); + window.Scope.GetService().Navigate(typeof(DashboardView)); + }); } } \ No newline at end of file diff --git a/RevitLookup/Commands/EventMonitorCommand.cs b/RevitLookup/Commands/EventMonitorCommand.cs index cec0e2f1b..3f866fa7c 100644 --- a/RevitLookup/Commands/EventMonitorCommand.cs +++ b/RevitLookup/Commands/EventMonitorCommand.cs @@ -33,8 +33,11 @@ public class EventMonitorCommand : ExternalCommand { public override void Execute() { - var window = Host.GetService(); - window.ShowAttached(); - window.Scope.GetService().Navigate(typeof(EventsView)); + RevitLookup.Application.Invoke(() => + { + var window = Host.GetService(); + window.ShowAttached(); + window.Scope.GetService().Navigate(typeof(EventsView)); + }); } } \ No newline at end of file diff --git a/RevitLookup/Commands/SearchElementsCommand.cs b/RevitLookup/Commands/SearchElementsCommand.cs index 000e7146e..5afca7c5b 100644 --- a/RevitLookup/Commands/SearchElementsCommand.cs +++ b/RevitLookup/Commands/SearchElementsCommand.cs @@ -34,9 +34,12 @@ public class SearchElementsCommand : ExternalCommand { public override void Execute() { - var window = Host.GetService(); - window.ShowAttached(); - window.Scope.GetService().Navigate(typeof(DashboardView)); - window.Scope.GetService().OpenDialogCommand.Execute("search"); + RevitLookup.Application.Invoke(() => + { + var window = Host.GetService(); + window.ShowAttached(); + window.Scope.GetService().Navigate(typeof(DashboardView)); + window.Scope.GetService().OpenDialogCommand.Execute("search"); + }); } } \ No newline at end of file diff --git a/RevitLookup/Commands/SnoopDatabaseCommand.cs b/RevitLookup/Commands/SnoopDatabaseCommand.cs index bff12777d..4a13f3f19 100644 --- a/RevitLookup/Commands/SnoopDatabaseCommand.cs +++ b/RevitLookup/Commands/SnoopDatabaseCommand.cs @@ -32,8 +32,11 @@ public class SnoopDatabaseCommand : ExternalCommand { public override void Execute() { - var window = Host.GetService(); - window.Initialize(); - window.Scope.GetService()!.Snoop(SnoopableType.Database); + RevitLookup.Application.Invoke(() => + { + var window = Host.GetService(); + window.Initialize(); + window.Scope.GetService()!.Snoop(SnoopableType.Database); + }); } } \ No newline at end of file diff --git a/RevitLookup/Commands/SnoopDocumentCommand.cs b/RevitLookup/Commands/SnoopDocumentCommand.cs index d81f16a9b..eb2fcb76c 100644 --- a/RevitLookup/Commands/SnoopDocumentCommand.cs +++ b/RevitLookup/Commands/SnoopDocumentCommand.cs @@ -32,8 +32,11 @@ public class SnoopDocumentCommand : ExternalCommand { public override void Execute() { - var window = Host.GetService(); - window.Initialize(); - window.Scope.GetService()!.Snoop(SnoopableType.Document); + RevitLookup.Application.Invoke(() => + { + var window = Host.GetService(); + window.Initialize(); + window.Scope.GetService()!.Snoop(SnoopableType.Document); + }); } } \ No newline at end of file diff --git a/RevitLookup/Commands/SnoopEdgeCommand.cs b/RevitLookup/Commands/SnoopEdgeCommand.cs index 3d57a1ada..f6082a845 100644 --- a/RevitLookup/Commands/SnoopEdgeCommand.cs +++ b/RevitLookup/Commands/SnoopEdgeCommand.cs @@ -32,8 +32,11 @@ public class SnoopEdgeCommand : ExternalCommand { public override void Execute() { - var window = Host.GetService(); - window.Initialize(); - window.Scope.GetService()!.Snoop(SnoopableType.Edge); + RevitLookup.Application.Invoke(() => + { + var window = Host.GetService(); + window.Initialize(); + window.Scope.GetService()!.Snoop(SnoopableType.Edge); + }); } } \ No newline at end of file diff --git a/RevitLookup/Commands/SnoopFaceCommand.cs b/RevitLookup/Commands/SnoopFaceCommand.cs index 182e5fc4f..fe1fc800e 100644 --- a/RevitLookup/Commands/SnoopFaceCommand.cs +++ b/RevitLookup/Commands/SnoopFaceCommand.cs @@ -32,8 +32,11 @@ public class SnoopFaceCommand : ExternalCommand { public override void Execute() { - var window = Host.GetService(); - window.Initialize(); - window.Scope.GetService()!.Snoop(SnoopableType.Face); + RevitLookup.Application.Invoke(() => + { + var window = Host.GetService(); + window.Initialize(); + window.Scope.GetService()!.Snoop(SnoopableType.Face); + }); } } \ No newline at end of file diff --git a/RevitLookup/Commands/SnoopLinkedElementCommand.cs b/RevitLookup/Commands/SnoopLinkedElementCommand.cs index f5f673bdb..97fcb16a1 100644 --- a/RevitLookup/Commands/SnoopLinkedElementCommand.cs +++ b/RevitLookup/Commands/SnoopLinkedElementCommand.cs @@ -32,8 +32,11 @@ public class SnoopLinkedElementCommand : ExternalCommand { public override void Execute() { - var window = Host.GetService(); - window.Initialize(); - window.Scope.GetService()!.Snoop(SnoopableType.LinkedElement); + RevitLookup.Application.Invoke(() => + { + var window = Host.GetService(); + window.Initialize(); + window.Scope.GetService()!.Snoop(SnoopableType.LinkedElement); + }); } } \ No newline at end of file diff --git a/RevitLookup/Commands/SnoopPointCommand.cs b/RevitLookup/Commands/SnoopPointCommand.cs index 8c2ec28a6..07e119abe 100644 --- a/RevitLookup/Commands/SnoopPointCommand.cs +++ b/RevitLookup/Commands/SnoopPointCommand.cs @@ -32,8 +32,11 @@ public class SnoopPointCommand : ExternalCommand { public override void Execute() { - var window = Host.GetService(); - window.Initialize(); - window.Scope.GetService()!.Snoop(SnoopableType.Point); + RevitLookup.Application.Invoke(() => + { + var window = Host.GetService(); + window.Initialize(); + window.Scope.GetService()!.Snoop(SnoopableType.Point); + }); } } \ No newline at end of file diff --git a/RevitLookup/Commands/SnoopSelectionCommand.cs b/RevitLookup/Commands/SnoopSelectionCommand.cs index 2aaf1995a..880602e85 100644 --- a/RevitLookup/Commands/SnoopSelectionCommand.cs +++ b/RevitLookup/Commands/SnoopSelectionCommand.cs @@ -32,8 +32,11 @@ public class SnoopSelectionCommand : ExternalCommand { public override void Execute() { - var window = Host.GetService(); - window.Initialize(); - window.Scope.GetService()!.Snoop(SnoopableType.Selection); + RevitLookup.Application.Invoke(() => + { + var window = Host.GetService(); + window.Initialize(); + window.Scope.GetService()!.Snoop(SnoopableType.Selection); + }); } } \ No newline at end of file diff --git a/RevitLookup/Commands/SnoopSubElementCommand.cs b/RevitLookup/Commands/SnoopSubElementCommand.cs index cc1e492bb..153786130 100644 --- a/RevitLookup/Commands/SnoopSubElementCommand.cs +++ b/RevitLookup/Commands/SnoopSubElementCommand.cs @@ -32,8 +32,11 @@ public class SnoopSubElementCommand : ExternalCommand { public override void Execute() { - var window = Host.GetService(); - window.Initialize(); - window.Scope.GetService()!.Snoop(SnoopableType.SubElement); + RevitLookup.Application.Invoke(() => + { + var window = Host.GetService(); + window.Initialize(); + window.Scope.GetService()!.Snoop(SnoopableType.SubElement); + }); } } \ No newline at end of file diff --git a/RevitLookup/Commands/SnoopViewCommand.cs b/RevitLookup/Commands/SnoopViewCommand.cs index aac18aea8..0a7825635 100644 --- a/RevitLookup/Commands/SnoopViewCommand.cs +++ b/RevitLookup/Commands/SnoopViewCommand.cs @@ -32,8 +32,11 @@ public class SnoopViewCommand : ExternalCommand { public override void Execute() { - var window = Host.GetService(); - window.Initialize(); - window.Scope.GetService()!.Snoop(SnoopableType.View); + RevitLookup.Application.Invoke(() => + { + var window = Host.GetService(); + window.Initialize(); + window.Scope.GetService()!.Snoop(SnoopableType.View); + }); } } \ No newline at end of file diff --git a/RevitLookup/Services/SettingsService.cs b/RevitLookup/Services/SettingsService.cs index 0cb4976ff..4bb520265 100644 --- a/RevitLookup/Services/SettingsService.cs +++ b/RevitLookup/Services/SettingsService.cs @@ -36,7 +36,7 @@ internal sealed class Settings { public ThemeType Theme { get; set; } = ThemeType.Light; public WindowBackdropType Background { get; set; } = WindowBackdropType.None; - public int TransitionDuration { get; set; } // = SettingsService.DefaultTransitionDuration; + public int TransitionDuration { get; set; } = SettingsService.DefaultTransitionDuration; public bool IsExtensionsAllowed { get; set; } public bool IsUnsupportedAllowed { get; set; } public bool IsModifyTabAllowed { get; set; } @@ -45,7 +45,7 @@ internal sealed class Settings public sealed class SettingsService : ISettingsService { - private const int DefaultTransitionDuration = 200; + public const int DefaultTransitionDuration = 200; private readonly Settings _settings; private readonly string _settingsFile; diff --git a/RevitLookup/ViewModels/Pages/SettingsViewModel.cs b/RevitLookup/ViewModels/Pages/SettingsViewModel.cs index 2ffbe9c6a..10045eccf 100644 --- a/RevitLookup/ViewModels/Pages/SettingsViewModel.cs +++ b/RevitLookup/ViewModels/Pages/SettingsViewModel.cs @@ -18,8 +18,6 @@ // Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii) // (Rights in Technical Data and Computer Software), as applicable. -using System.Windows.Interop; -using System.Windows.Media; using CommunityToolkit.Mvvm.ComponentModel; using RevitLookup.Services.Contracts; using Wpf.Ui.Appearance; @@ -106,7 +104,8 @@ partial void OnIsModifyTabAllowedChanged(bool value) partial void OnIsHardwareRenderingAllowedChanged(bool value) { - _settingsService.IsModifyTabAllowed = value; - RenderOptions.ProcessRenderMode = value ? RenderMode.Default : RenderMode.SoftwareOnly; + _settingsService.IsHardwareRenderingAllowed = value; + if (value) Application.RunDispatcher(_settingsService); + else Application.TerminateDispatcher(_settingsService); } } \ No newline at end of file