Skip to content

Commit

Permalink
Replace Application.Current with HostAnalyticsInfo.HostName check (#1…
Browse files Browse the repository at this point in the history
…4574)

* Fix PostDiff job

* replace application.current

* tests

* refactor after Bogdans PR

---------

Co-authored-by: Aaron (Qilong) <[email protected]>
  • Loading branch information
zeusongit and QilongTang authored Nov 27, 2023
1 parent 11b1f7c commit c5df6f9
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/DynamoCore/Models/DynamoModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ public interface IStartConfiguration
bool NoNetworkMode => false;

/// <summary>
/// Configuration object that contains host information like Host name, version and session id.
/// Configuration object that contains host information like Host name, parent id and session id.
/// </summary>
HostAnalyticsInfo HostAnalyticsInfo { get; set; }
}
Expand Down
11 changes: 7 additions & 4 deletions src/DynamoCoreWpf/Controls/InPortContextMenu.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System;
using System.Windows;
using System.Windows.Controls;
using Dynamo.Models;
using Dynamo.ViewModels;

namespace Dynamo.UI.Controls
Expand All @@ -15,8 +16,10 @@ public partial class InPortContextMenu : UserControl
public InPortContextMenu()
{
InitializeComponent();

if (Application.Current != null) Application.Current.Deactivated += CurrentApplicationDeactivated;
if (string.IsNullOrEmpty(DynamoModel.HostAnalyticsInfo.HostName) && Application.Current != null)
{
Application.Current.Deactivated += CurrentApplicationDeactivated;
}
Unloaded += InPortContextMenuControl_Unloaded;
}

Expand All @@ -27,7 +30,7 @@ public InPortContextMenu()
/// <param name="e"></param>
private void InPortContextMenuControl_Unloaded(object sender, RoutedEventArgs e)
{
if (Application.Current != null)
if (string.IsNullOrEmpty(DynamoModel.HostAnalyticsInfo.HostName) && Application.Current != null)
{
Application.Current.Deactivated -= CurrentApplicationDeactivated;
}
Expand Down
5 changes: 3 additions & 2 deletions src/DynamoCoreWpf/Controls/IncanvasSearchControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Windows.Input;
using System.Windows.Threading;
using Dynamo.Logging;
using Dynamo.Models;
using Dynamo.Utilities;
using Dynamo.ViewModels;
using Dynamo.Wpf.ViewModels;
Expand All @@ -31,7 +32,7 @@ public SearchViewModel ViewModel
public InCanvasSearchControl()
{
InitializeComponent();
if (Application.Current != null)
if (string.IsNullOrEmpty(DynamoModel.HostAnalyticsInfo.HostName) && Application.Current != null)
{
Application.Current.Deactivated += CurrentApplicationDeactivated;
Application.Current.MainWindow.Closing += InCanvasSearchControl_Unloaded;
Expand All @@ -40,7 +41,7 @@ public InCanvasSearchControl()

private void InCanvasSearchControl_Unloaded(object sender, System.ComponentModel.CancelEventArgs e)
{
if (Application.Current != null)
if (string.IsNullOrEmpty(DynamoModel.HostAnalyticsInfo.HostName) && Application.Current != null)
{
Application.Current.Deactivated -= CurrentApplicationDeactivated;
Application.Current.MainWindow.Closing -= InCanvasSearchControl_Unloaded;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ public partial class NodeAutoCompleteSearchControl : IDisposable
public NodeAutoCompleteSearchControl()
{
InitializeComponent();
if (Application.Current != null)
if (string.IsNullOrEmpty(DynamoModel.HostAnalyticsInfo.HostName) && Application.Current != null)
{
Application.Current.Deactivated += CurrentApplicationDeactivated;
if (Application.Current.MainWindow != null)
if (Application.Current?.MainWindow != null)
{
Application.Current.MainWindow.Closing += NodeAutoCompleteSearchControl_Unloaded;
}
Expand All @@ -55,10 +55,10 @@ public NodeAutoCompleteSearchControl()

private void NodeAutoCompleteSearchControl_Unloaded(object sender, System.ComponentModel.CancelEventArgs e)
{
if (Application.Current != null)
if (string.IsNullOrEmpty(DynamoModel.HostAnalyticsInfo.HostName) && Application.Current != null)
{
Application.Current.Deactivated -= CurrentApplicationDeactivated;
if (Application.Current.MainWindow != null)
if (Application.Current?.MainWindow != null)
{
Application.Current.MainWindow.Closing -= NodeAutoCompleteSearchControl_Unloaded;
}
Expand Down
8 changes: 6 additions & 2 deletions src/DynamoCoreWpf/Controls/OutPortContextMenu.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Windows;
using System.Windows.Controls;
using Dynamo.Models;
using Dynamo.ViewModels;

namespace Dynamo.UI.Controls
Expand All @@ -19,7 +20,10 @@ public OutPortContextMenu()
{
InitializeComponent();

if (Application.Current != null) Application.Current.Deactivated += CurrentApplicationDeactivated;
if (string.IsNullOrEmpty(DynamoModel.HostAnalyticsInfo.HostName) && Application.Current != null)
{
Application.Current.Deactivated += CurrentApplicationDeactivated;
}
Unloaded += OutPortContextMenuControl_Unloaded;
}

Expand All @@ -30,7 +34,7 @@ public OutPortContextMenu()
/// <param name="e"></param>
private void OutPortContextMenuControl_Unloaded(object sender, RoutedEventArgs e)
{
if (Application.Current != null)
if (string.IsNullOrEmpty(DynamoModel.HostAnalyticsInfo.HostName) && Application.Current != null)
{
Application.Current.Deactivated -= CurrentApplicationDeactivated;
}
Expand Down
5 changes: 2 additions & 3 deletions src/DynamoCoreWpf/Services/UsageReportingManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,14 @@ public void ToggleIsAnalyticsReportingApproved(object parameter)
private void ShowUsageReportingPrompt(Window ownerWindow)
{
// If an owner window is not supplied, then we will fallback onto
// using the application's main window. In native host application
// scenario (e.g. Revit), the "Application.Current" will be "null".
// using the application's main window.
// The owner window is important so that usage report window always
// get shown on top of the owner window, otherwise it is possible
// for usage report window to show up in the background (behind all
// other full screen windows), and Dynamo main window will appear
// to be frozen because control cannot return to it.
//
if (ownerWindow == null && (null != Application.Current))
if (ownerWindow == null && string.IsNullOrEmpty(DynamoModel.HostAnalyticsInfo.HostName) && Application.Current != null)
ownerWindow = Application.Current.MainWindow;

var usageReportingPrompt = new UsageReportingAgreementPrompt(resourceProvider, dynamoViewModel)
Expand Down
4 changes: 2 additions & 2 deletions src/DynamoCoreWpf/ViewModels/RunSettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,9 @@ void NotifyOfGraphRunChanged()
RaisePropertyChanged(nameof(RunButtonEnabled));
RaisePropertyChanged(nameof(RunButtonToolTip));

if (Application.Current != null)
if (string.IsNullOrEmpty(DynamoModel.HostAnalyticsInfo.HostName))
{
Application.Current.Dispatcher.Invoke(new Action(() =>
Application.Current?.Dispatcher.Invoke(new Action(() =>
{
dynamoViewModel.ShowOpenDialogAndOpenResultCommand.RaiseCanExecuteChanged();
dynamoViewModel.NewHomeWorkspaceCommand.RaiseCanExecuteChanged();
Expand Down
9 changes: 5 additions & 4 deletions src/DynamoCoreWpf/Views/Core/DynamoView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ public DynamoView(DynamoViewModel dynamoViewModel)
{
fileTrustWarningPopup = new FileTrustWarning(this);
}
if (!DynamoModel.IsTestMode && Application.Current != null)
if (!DynamoModel.IsTestMode && string.IsNullOrEmpty(DynamoModel.HostAnalyticsInfo.HostName) && Application.Current != null)
{
Application.Current.MainWindow = this;
}
Expand Down Expand Up @@ -1372,9 +1372,10 @@ private void DynamoView_Loaded(object sender, EventArgs e)

TrackStartupAnalytics();

// In native host scenario (e.g. Revit), the "Application.Current" will be "null". Therefore, the InCanvasSearchControl.OnRequestShowInCanvasSearch
// will not work. Instead, we have to check if the Owner Window (DynamoView) is deactivated or not.
if (Application.Current == null)
// In native host scenario (e.g. Revit), the InCanvasSearchControl.OnRequestShowInCanvasSearch
// will not work. Instead, we have to check if the Owner Window (DynamoView) is deactivated or not.

if (!string.IsNullOrEmpty(DynamoModel.HostAnalyticsInfo.HostName))
{
this.Deactivated += (s, args) => { HidePopupWhenWindowDeactivated(null); };
}
Expand Down

0 comments on commit c5df6f9

Please sign in to comment.