Skip to content

Commit

Permalink
Merge pull request #1163 from mcneel/1.23
Browse files Browse the repository at this point in the history
Fixes #1161
  • Loading branch information
kike-garbo authored Aug 16, 2024
2 parents 5d0a348 + af8985c commit d01b6de
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 47 deletions.
69 changes: 25 additions & 44 deletions src/RhinoInside.Revit.External/ActivationGate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ internal static T Open<T>(Func<T> func, object state = default)

if (!wasOpen && IsActive && state as UI.ExternalApplication is null)
{
while (UI.HostedApplication.Active.DoEvents()) { }
while (UI.HostedApplication.Active.DoEvents() && windowToActivate is null) { }
}

return result;
Expand All @@ -251,9 +251,14 @@ internal static T Open<T>(Func<T> func, object state = default)

if (IsActive && !IsOpen)
{
// Return control to Revit
HostMainWindow.Enabled = true;
WindowHandle.ActiveWindow = HostMainWindow;
try
{
// Return control to Revit
HostMainWindow.Enabled = true;
WindowHandle.ActiveWindow = windowToActivate ?? HostMainWindow;
}
catch { /* Window failed to be activated */ }
finally { windowToActivate = default; }
}
}
}
Expand Down Expand Up @@ -410,25 +415,18 @@ public static OpenAwaitable Open([CallerMemberName] string name = "") =>
#endregion

#region Implementation
private static void ActivateWindow()
{
if (windowToActivate?.IsInvalid == false)
{
try { WindowHandle.ActiveWindow = windowToActivate; }
catch { /* Windows failed to be activated */ }
finally { windowToActivate = default; }
}
}
private static async void ActivateWindowAsync()
{
await System.Threading.Tasks.Task.Yield();
ActivateWindow();
}

class TryActivateEventHandler : UI.ExternalEventHandler
{
public override string GetName() => "RhinoInside.Revit.External.ActivationGate";
protected override void Execute(UIApplication app) => ActivateWindow();
protected override void Execute(UIApplication app)
{
if (windowToActivate?.IsInvalid == false)
{
try { WindowHandle.ActiveWindow = windowToActivate; }
catch { /* Window failed to be activated */ }
finally { windowToActivate = default; }
}
}
}

class Hook : ComputerBasedTrainingHook
Expand All @@ -442,32 +440,16 @@ protected override int DispatchHook(int nCode, IntPtr wParam, IntPtr lParam)
if (IsOpen)
{
var window = (WindowHandle) wParam;
if (!IsExternalWindow(window, out var _))
if (HostMainWindow.Enabled && !IsExternalWindow(window, out var _))
{
if (window == HostMainWindow && !window.Enabled)
foreach (var gate in gates)
{
foreach (var gate in gates)
{
try { gate.Value.Window.BringToFront(); }
catch { }
}
try { gate.Value.Window.BringToFront(); }
catch { }
}
else if(window != (windowToActivate ?? WindowHandle.Zero))
{
var isPending = windowToActivate is object;
windowToActivate = window;

if (isPending)
{
// HACK : If Rhino is in a Get we should allow it to finish.
if (RhinoDoc.ActiveDoc is RhinoDoc rhinoDoc && RhinoGet.InGet(rhinoDoc))
WindowHandle.ActiveWindow.Flash();
}
else
ActivateWindowAsync();

return 1; // Prevents activation now.
}
windowToActivate = window; // Will be activated when our message loop is Idle.
return 1; // Prevents activation now.
}
}
else
Expand All @@ -488,8 +470,7 @@ protected override int DispatchHook(int nCode, IntPtr wParam, IntPtr lParam)

WindowHandle.ActiveWindow.Flash();
}
else
externalEvent.Raise();
else externalEvent.Raise();

return 1; // Prevents activation now.
}
Expand Down
4 changes: 2 additions & 2 deletions src/RhinoInside.Revit.External/DB/Extensions/Element.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ public static bool IsEquivalent(this Element self, Element other)
if (ReferenceEquals(self, other))
return true;

if (self?.Id != other?.Id)
if (!self.IsValidObject || !other.IsValidObject)
return false;

if (!self.IsValidObject || !other.IsValidObject)
if (self?.Id != other?.Id)
return false;

return self.Document.Equals(other.Document);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,12 @@ public override IntPtr MainWindowHandle
internal override bool IsViewOpen(View view) => OpenViews.Contains(view);

private ISet<View> OpenViews = new HashSet<View>(ElementEqualityComparer.InterDocument);
private void UpdateOpenViewsList(object sender, ViewActivatedEventArgs e)
private async void UpdateOpenViewsList(object sender, ViewActivatedEventArgs e)
{
OpenViews.Clear();

// Calling GetOpenViews here is not safe, so we yeld until Idle to do so.
await ActivationGate.Yield();
OpenViews = new HashSet<View>(_app.Application.GetOpenViews(), ElementEqualityComparer.InterDocument);
}
#endregion
Expand Down

0 comments on commit d01b6de

Please sign in to comment.