Skip to content

Commit

Permalink
Merge branch 'jeremytammik:dev' into dev-export-family-table
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyNefyodov authored May 27, 2024
2 parents d4940c8 + 2261f29 commit 09c5335
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 46 deletions.
4 changes: 2 additions & 2 deletions source/RevitLookup/RevitLookup.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

<ItemGroup>
<!--Revit references-->
<PackageReference Include="Nice3point.Revit.Build.Tasks" Version="1.*"/>
<PackageReference Include="Nice3point.Revit.Build.Tasks" Version="2.0.0"/>
<PackageReference Include="Nice3point.Revit.Toolkit" Version="$(RevitVersion).*"/>
<PackageReference Include="Nice3point.Revit.Extensions" Version="$(RevitVersion).*"/>
<PackageReference Include="Nice3point.Revit.Api.RevitAPI" Version="$(RevitVersion).*"/>
Expand Down Expand Up @@ -76,7 +76,7 @@
</ItemGroup>

<ItemGroup>
<Resource Include="Resources\Images\RibbonIcon16.png"/>
<Content Include="Resources\Images\RibbonIcon16.png"/>
<Resource Include="Resources\Images\RibbonIcon32.png"/>
<Resource Include="Resources\Images\AboutLogo200.png"/>
<Resource Include="Resources\Images\ShellIcon.ico"/>
Expand Down
88 changes: 44 additions & 44 deletions source/RevitLookup/Services/LookupService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,110 +33,110 @@ public sealed class LookupService : ILookupService
{
private static Dispatcher _dispatcher;
private LookupServiceImpl _lookupService;

static LookupService()
{
var uiThread = new Thread(Dispatcher.Run);
uiThread.SetApartmentState(ApartmentState.STA);
uiThread.Start();

EnsureThreadStart(uiThread);
}

public LookupService(IServiceScopeFactory scopeFactory)
{
if (Thread.CurrentThread == _dispatcher.Thread)
if (_dispatcher.CheckAccess())
{
_lookupService = new LookupServiceImpl(scopeFactory);
}
else
{
_dispatcher.InvokeAsync(() => _lookupService = new LookupServiceImpl(scopeFactory));
_dispatcher.InvokeAsync(() => _lookupService = new LookupServiceImpl(scopeFactory)).Wait();
}
}

public ILookupServiceDependsStage Snoop(SnoopableType snoopableType)
{
if (Thread.CurrentThread == _dispatcher.Thread)
if (_dispatcher.CheckAccess())
{
_lookupService.Snoop(snoopableType);
}
else
{
_dispatcher.InvokeAsync(() => _lookupService.Snoop(snoopableType));
_dispatcher.InvokeAsync(() => _lookupService.Snoop(snoopableType)).Wait();
}

return this;
}

public ILookupServiceDependsStage Snoop(SnoopableObject snoopableObject)
{
if (Thread.CurrentThread == _dispatcher.Thread)
if (_dispatcher.CheckAccess())
{
_lookupService.Snoop(snoopableObject);
}
else
{
_dispatcher.InvokeAsync(() => _lookupService.Snoop(snoopableObject));
_dispatcher.InvokeAsync(() => _lookupService.Snoop(snoopableObject)).Wait();
}

return this;
}

public ILookupServiceDependsStage Snoop(IList<SnoopableObject> snoopableObjects)
{
if (Thread.CurrentThread == _dispatcher.Thread)
if (_dispatcher.CheckAccess())
{
_lookupService.Snoop(snoopableObjects);
}
else
{
_dispatcher.InvokeAsync(() => _lookupService.Snoop(snoopableObjects));
_dispatcher.InvokeAsync(() => _lookupService.Snoop(snoopableObjects)).Wait();
}

return this;
}

public ILookupServiceShowStage DependsOn(IServiceProvider provider)
{
if (Thread.CurrentThread == _dispatcher.Thread)
if (_dispatcher.CheckAccess())
{
_lookupService.DependsOn(provider);
}
else
{
_dispatcher.InvokeAsync(() => _lookupService.DependsOn(provider));
_dispatcher.InvokeAsync(() => _lookupService.DependsOn(provider)).Wait();
}

return this;
}

public ILookupServiceExecuteStage Show<T>() where T : Page
{
if (Thread.CurrentThread == _dispatcher.Thread)
if (_dispatcher.CheckAccess())
{
_lookupService.Show<T>();
}
else
{
_dispatcher.InvokeAsync(() => _lookupService.Show<T>());
_dispatcher.InvokeAsync(() => _lookupService.Show<T>()).Wait();
}

return this;
}

public void Execute<T>(Action<T> handler) where T : class
{
if (Thread.CurrentThread == _dispatcher.Thread)
if (_dispatcher.CheckAccess())
{
_lookupService.Execute(handler);
}
else
{
_dispatcher.InvokeAsync(() => _lookupService.Execute(handler));
_dispatcher.InvokeAsync(() => _lookupService.Execute(handler)).Wait();
}
}

private static void EnsureThreadStart(Thread thread)
{
Dispatcher dispatcher = null;
Expand All @@ -146,50 +146,50 @@ private static void EnsureThreadStart(Thread thread)
spinWait.SpinOnce();
dispatcher = Dispatcher.FromThread(thread);
}

_dispatcher = dispatcher;
}

private class LookupServiceImpl
private sealed class LookupServiceImpl
{
private Window _owner;
private Task _activeTask;
private readonly IServiceScope _scope;
private readonly ISnoopVisualService _visualService;
private readonly INavigationService _navigationService;
private readonly Window _window;

public LookupServiceImpl(IServiceScopeFactory scopeFactory)
{
_scope = scopeFactory.CreateScope();

_window = (Window) _scope.ServiceProvider.GetService<IWindow>();
_visualService = _scope.ServiceProvider.GetService<ISnoopVisualService>();
_navigationService = _scope.ServiceProvider.GetService<INavigationService>();

_window.Closed += (_, _) => _scope.Dispose();
}

public void Snoop(SnoopableType snoopableType)
{
_activeTask = _visualService!.SnoopAsync(snoopableType);
}

public void Snoop(SnoopableObject snoopableObject)
{
_visualService.Snoop(snoopableObject);
}

public void Snoop(IList<SnoopableObject> snoopableObjects)
{
_visualService.Snoop(snoopableObjects);
}

public void DependsOn(IServiceProvider provider)
{
_owner = (Window) provider.GetService<IWindow>();
}

public void Show<T>() where T : Page
{
if (_activeTask is null)
Expand All @@ -201,7 +201,7 @@ public void Show<T>() where T : Page
_activeTask = _activeTask.ContinueWith(_ => ShowPage<T>(), TaskScheduler.FromCurrentSynchronizationContext());
}
}

public void Execute<T>(Action<T> handler) where T : class
{
if (_activeTask is null)
Expand All @@ -213,13 +213,13 @@ public void Execute<T>(Action<T> handler) where T : class
_activeTask = _activeTask.ContinueWith(_ => InvokeHandler(handler), TaskScheduler.FromCurrentSynchronizationContext());
}
}

private void InvokeHandler<T>(Action<T> handler) where T : class
{
var service = _scope.ServiceProvider.GetService<T>();
handler.Invoke(service);
}

private void ShowPage<T>() where T : Page
{
if (_owner is null)
Expand All @@ -231,7 +231,7 @@ private void ShowPage<T>() where T : Page
_window.Left = _owner.Left + 47;
_window.Top = _owner.Top + 49;
}

_window.Show(Context.UiApplication.MainWindowHandle);
_navigationService.Navigate(typeof(T));
}
Expand Down

0 comments on commit 09c5335

Please sign in to comment.