Skip to content

Commit

Permalink
Separate thread for UI (#166)
Browse files Browse the repository at this point in the history
* Separate UI thread

* Remove debug RenderOptions

* Fix initialisation render mode
  • Loading branch information
Nice3point authored May 26, 2023
1 parent 0658f26 commit adc82da
Show file tree
Hide file tree
Showing 15 changed files with 106 additions and 52 deletions.
37 changes: 28 additions & 9 deletions RevitLookup/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<IReadOnlyCollection<SnoopableObject>> ExternalElementHandler { get; private set; }
public static AsyncEventHandler<IReadOnlyCollection<Descriptor>> ExternalDescriptorHandler { get; private set; }
Expand All @@ -47,7 +48,7 @@ public override async void OnStartup()

var settingsService = Host.GetService<ISettingsService>();
RibbonController.CreatePanel(Application, settingsService);
EnableHardwareRendering(settingsService);
RunDispatcher(settingsService);
}

public override async void OnShutdown()
Expand Down Expand Up @@ -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);
}
}
9 changes: 6 additions & 3 deletions RevitLookup/Commands/DashboardCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ public class DashboardCommand : ExternalCommand
{
public override void Execute()
{
var window = Host.GetService<IWindow>();
window.ShowAttached();
window.Scope.GetService<INavigationService>().Navigate(typeof(DashboardView));
RevitLookup.Application.Invoke(() =>
{
var window = Host.GetService<IWindow>();
window.ShowAttached();
window.Scope.GetService<INavigationService>().Navigate(typeof(DashboardView));
});
}
}
9 changes: 6 additions & 3 deletions RevitLookup/Commands/EventMonitorCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ public class EventMonitorCommand : ExternalCommand
{
public override void Execute()
{
var window = Host.GetService<IWindow>();
window.ShowAttached();
window.Scope.GetService<INavigationService>().Navigate(typeof(EventsView));
RevitLookup.Application.Invoke(() =>
{
var window = Host.GetService<IWindow>();
window.ShowAttached();
window.Scope.GetService<INavigationService>().Navigate(typeof(EventsView));
});
}
}
11 changes: 7 additions & 4 deletions RevitLookup/Commands/SearchElementsCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ public class SearchElementsCommand : ExternalCommand
{
public override void Execute()
{
var window = Host.GetService<IWindow>();
window.ShowAttached();
window.Scope.GetService<INavigationService>().Navigate(typeof(DashboardView));
window.Scope.GetService<DashboardViewModel>().OpenDialogCommand.Execute("search");
RevitLookup.Application.Invoke(() =>
{
var window = Host.GetService<IWindow>();
window.ShowAttached();
window.Scope.GetService<INavigationService>().Navigate(typeof(DashboardView));
window.Scope.GetService<DashboardViewModel>().OpenDialogCommand.Execute("search");
});
}
}
9 changes: 6 additions & 3 deletions RevitLookup/Commands/SnoopDatabaseCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ public class SnoopDatabaseCommand : ExternalCommand
{
public override void Execute()
{
var window = Host.GetService<IWindow>();
window.Initialize();
window.Scope.GetService<ISnoopService>()!.Snoop(SnoopableType.Database);
RevitLookup.Application.Invoke(() =>
{
var window = Host.GetService<IWindow>();
window.Initialize();
window.Scope.GetService<ISnoopService>()!.Snoop(SnoopableType.Database);
});
}
}
9 changes: 6 additions & 3 deletions RevitLookup/Commands/SnoopDocumentCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ public class SnoopDocumentCommand : ExternalCommand
{
public override void Execute()
{
var window = Host.GetService<IWindow>();
window.Initialize();
window.Scope.GetService<ISnoopService>()!.Snoop(SnoopableType.Document);
RevitLookup.Application.Invoke(() =>
{
var window = Host.GetService<IWindow>();
window.Initialize();
window.Scope.GetService<ISnoopService>()!.Snoop(SnoopableType.Document);
});
}
}
9 changes: 6 additions & 3 deletions RevitLookup/Commands/SnoopEdgeCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ public class SnoopEdgeCommand : ExternalCommand
{
public override void Execute()
{
var window = Host.GetService<IWindow>();
window.Initialize();
window.Scope.GetService<ISnoopService>()!.Snoop(SnoopableType.Edge);
RevitLookup.Application.Invoke(() =>
{
var window = Host.GetService<IWindow>();
window.Initialize();
window.Scope.GetService<ISnoopService>()!.Snoop(SnoopableType.Edge);
});
}
}
9 changes: 6 additions & 3 deletions RevitLookup/Commands/SnoopFaceCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ public class SnoopFaceCommand : ExternalCommand
{
public override void Execute()
{
var window = Host.GetService<IWindow>();
window.Initialize();
window.Scope.GetService<ISnoopService>()!.Snoop(SnoopableType.Face);
RevitLookup.Application.Invoke(() =>
{
var window = Host.GetService<IWindow>();
window.Initialize();
window.Scope.GetService<ISnoopService>()!.Snoop(SnoopableType.Face);
});
}
}
9 changes: 6 additions & 3 deletions RevitLookup/Commands/SnoopLinkedElementCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ public class SnoopLinkedElementCommand : ExternalCommand
{
public override void Execute()
{
var window = Host.GetService<IWindow>();
window.Initialize();
window.Scope.GetService<ISnoopService>()!.Snoop(SnoopableType.LinkedElement);
RevitLookup.Application.Invoke(() =>
{
var window = Host.GetService<IWindow>();
window.Initialize();
window.Scope.GetService<ISnoopService>()!.Snoop(SnoopableType.LinkedElement);
});
}
}
9 changes: 6 additions & 3 deletions RevitLookup/Commands/SnoopPointCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ public class SnoopPointCommand : ExternalCommand
{
public override void Execute()
{
var window = Host.GetService<IWindow>();
window.Initialize();
window.Scope.GetService<ISnoopService>()!.Snoop(SnoopableType.Point);
RevitLookup.Application.Invoke(() =>
{
var window = Host.GetService<IWindow>();
window.Initialize();
window.Scope.GetService<ISnoopService>()!.Snoop(SnoopableType.Point);
});
}
}
9 changes: 6 additions & 3 deletions RevitLookup/Commands/SnoopSelectionCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ public class SnoopSelectionCommand : ExternalCommand
{
public override void Execute()
{
var window = Host.GetService<IWindow>();
window.Initialize();
window.Scope.GetService<ISnoopService>()!.Snoop(SnoopableType.Selection);
RevitLookup.Application.Invoke(() =>
{
var window = Host.GetService<IWindow>();
window.Initialize();
window.Scope.GetService<ISnoopService>()!.Snoop(SnoopableType.Selection);
});
}
}
9 changes: 6 additions & 3 deletions RevitLookup/Commands/SnoopSubElementCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ public class SnoopSubElementCommand : ExternalCommand
{
public override void Execute()
{
var window = Host.GetService<IWindow>();
window.Initialize();
window.Scope.GetService<ISnoopService>()!.Snoop(SnoopableType.SubElement);
RevitLookup.Application.Invoke(() =>
{
var window = Host.GetService<IWindow>();
window.Initialize();
window.Scope.GetService<ISnoopService>()!.Snoop(SnoopableType.SubElement);
});
}
}
9 changes: 6 additions & 3 deletions RevitLookup/Commands/SnoopViewCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ public class SnoopViewCommand : ExternalCommand
{
public override void Execute()
{
var window = Host.GetService<IWindow>();
window.Initialize();
window.Scope.GetService<ISnoopService>()!.Snoop(SnoopableType.View);
RevitLookup.Application.Invoke(() =>
{
var window = Host.GetService<IWindow>();
window.Initialize();
window.Scope.GetService<ISnoopService>()!.Snoop(SnoopableType.View);
});
}
}
4 changes: 2 additions & 2 deletions RevitLookup/Services/SettingsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -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;

Expand Down
7 changes: 3 additions & 4 deletions RevitLookup/ViewModels/Pages/SettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

0 comments on commit adc82da

Please sign in to comment.