Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a public API on the View extension that gets triggered when the view extension is closed. #11260

Merged
merged 9 commits into from
Nov 19, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Dynamo.DocumentationBrowser
/// The DocumentationBrowser view extension displays web or local html files on the Dynamo right panel.
/// It reacts to documentation display request events in Dynamo to know what and when to display documentation.
/// </summary>
public class DocumentationBrowserViewExtension : IViewExtension, ILogSource
public class DocumentationBrowserViewExtension : ViewExtensionBase, IViewExtension, ILogSource
{
private ViewLoadedParams viewLoadedParamsReference;
private MenuItem documentationBrowserMenuItem;
Expand All @@ -41,6 +41,7 @@ public DocumentationBrowserViewExtension()
#region ILogSource

public event Action<ILogMessage> MessageLogged;

internal void OnMessageLogged(ILogMessage msg)
{
MessageLogged?.Invoke(msg);
Expand Down Expand Up @@ -68,9 +69,6 @@ public void Loaded(ViewLoadedParams viewLoadedParams)
this.documentationBrowserMenuItem.Unchecked += MenuItemUnCheckedHandler;
this.viewLoadedParamsReference.AddMenuItem(MenuBarType.View, this.documentationBrowserMenuItem);


DynamoView.CloseExtension += OnCloseExtension;

// subscribe to the documentation open request event from Dynamo
this.viewLoadedParamsReference.RequestOpenDocumentationLink += HandleRequestOpenDocumentationLink;

Expand All @@ -96,7 +94,6 @@ public void Shutdown()
protected virtual void Dispose(bool disposing)
{
// Cleanup
DynamoView.CloseExtension -= OnCloseExtension;
this.viewLoadedParamsReference.RequestOpenDocumentationLink -= HandleRequestOpenDocumentationLink;
this.ViewModel.MessageLogged -= OnViewModelMessageLogged;
documentationBrowserMenuItem.Checked -= MenuItemCheckHandler;
Expand Down Expand Up @@ -138,14 +135,6 @@ public void HandleRequestOpenDocumentationLink(OpenDocumentationLinkEventArgs ar
this.ViewModel?.HandleOpenDocumentationLinkEvent(args);
}

private void OnCloseExtension(String extensionTabName)
{
if (extensionTabName.Equals(Name))
{
this.documentationBrowserMenuItem.IsChecked = false;
}
}

private void OnViewModelMessageLogged(ILogMessage msg)
{
OnMessageLogged(msg);
Expand Down Expand Up @@ -181,5 +170,10 @@ private void HandleStartPageVisibilityChange(object sender, PropertyChangedEvent
ViewModel.ShowBrowser = !dynamoViewModel.ShowStartPage;
}
}

public override void Closed()
{
this.documentationBrowserMenuItem.IsChecked = false;
}
}
}
1 change: 1 addition & 0 deletions src/DynamoCoreWpf/DynamoCoreWpf.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
<Compile Include="Controls\UseLevelSpinner.cs" />
<Compile Include="Extensions\ViewModelCommandExecutive.cs" />
<Compile Include="Extensions\IViewExtension.cs" />
<Compile Include="Extensions\ViewExtensionBase.cs" />
<Compile Include="Extensions\IViewExtensionLoader.cs" />
<Compile Include="Extensions\IViewExtensionManager.cs" />
<Compile Include="Extensions\MenuBarTypeExtensions.cs" />
Expand Down
84 changes: 84 additions & 0 deletions src/DynamoCoreWpf/Extensions/ViewExtensionBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dynamo.Wpf.Extensions
{
/// <summary>
/// Base class for View Extension.
/// </summary>
public abstract class ViewExtensionBase : IViewExtension
{
/// <summary>
/// A unique id for this extension instance.
///
/// There may be multiple instances of the same type, but the application
/// will *not* allow two instances to coexist with the same id.
/// </summary>
public virtual string UniqueId { get; }

/// <summary>
/// A name for the extension instance. This is used for more user-readable logging.
/// </summary>
public virtual string Name { get; }

/// <summary>
/// Dispose method for the view extension.
/// </summary>
public virtual void Dispose()
{

}

/// <summary>
/// Action to be invoked when DynamoView begins to start up. This is guaranteed to happen
/// after the DynamoModel has been created.
///
/// This method is *not* guaranteed to be invoked unless the extension is present
/// at startup.
///
/// Exceptions thrown from this method will be caught by Dynamo and
/// displayed.
/// </summary>
public virtual void Loaded(ViewLoadedParams viewLoadedParams)
{

}

/// <summary>
/// Action to be invoked when shutdown has begun. This gives the UI a last minute chance to interact
/// with the user.
///
/// This action is guaranteed to be invoked before the associated model layer method on IExtension.
/// </summary>
public virtual void Shutdown()
{

}

/// <summary>
/// Action to be invoked when DynamoView begins to start up. This is guaranteed to happen
/// after the DynamoModel has been created.
///
/// This method is *not* guaranteed to be invoked unless the extension is present
/// at startup.
///
/// Exceptions thrown from this method will be caught by Dynamo and
/// displayed.
/// </summary>
public virtual void Startup(ViewStartupParams viewStartupParams)
{

}

/// <summary>
/// Action to be invoked when the view extenion is closed.
reddyashish marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
public virtual void Closed()
{

}
}
}
15 changes: 9 additions & 6 deletions src/DynamoCoreWpf/Views/Core/DynamoView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,9 @@ internal void CloseExtensionControl(IViewExtension viewExtension)
{
string tabName = viewExtension.Name;
TabItem tabitem = ExtensionTabItems.OfType<TabItem>().SingleOrDefault(n => n.Header.ToString() == tabName);
CloseExtension?.Invoke(tabName);

var viewExtensionBaseClass = (ViewExtensionBase) viewExtension;
viewExtensionBaseClass.Closed();
CloseExtensionTab(tabitem);
CloseExtensionWindow(tabName);
}
Expand All @@ -302,10 +304,10 @@ internal void CloseExtensionControl(IViewExtension viewExtension)
internal void CloseExtensionTab(object sender, RoutedEventArgs e)
{
string tabName = (sender as Button).DataContext.ToString();

CloseExtension?.Invoke(tabName);

TabItem tabitem = ExtensionTabItems.OfType<TabItem>().SingleOrDefault(n => n.Header.ToString() == tabName);

var viewExtensionBaseClass = (ViewExtensionBase) tabitem.Tag;
reddyashish marked this conversation as resolved.
Show resolved Hide resolved
viewExtensionBaseClass.Closed();
CloseExtensionTab(tabitem);
}

Expand All @@ -320,7 +322,7 @@ private void CloseExtensionTab(TabItem tabitem)
// get the selected tab
TabItem selectedTab = tabDynamic.SelectedItem as TabItem;

if (tabToBeRemoved != null)
if (tabToBeRemoved != null && ExtensionTabItems.Count > 0)
reddyashish marked this conversation as resolved.
Show resolved Hide resolved
{
// clear tab control binding and bind to the new tab-list.
tabDynamic.DataContext = null;
Expand Down Expand Up @@ -416,7 +418,8 @@ private void ExtensionWindow_Closed(object sender, EventArgs e)
}
else
{
CloseExtension?.Invoke(extName);
var viewExtensionBaseClass = (ViewExtensionBase)ext.Tag;
viewExtensionBaseClass.Closed();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ public WorkspaceDependencyView(WorkspaceDependencyViewExtension viewExtension, V
packageInstaller = p.PackageInstaller;
dependencyViewExtension = viewExtension;
DependencyRegen(currentWorkspace);
DynamoView.CloseExtension += this.OnExtensionTabClosedHandler;
HomeWorkspaceModel.WorkspaceClosed += this.CloseExtensionTab;
}

Expand All @@ -189,20 +188,6 @@ internal void CloseExtensionTab()
loadedParams.CloseExtensioninInSideBar(dependencyViewExtension);
}

/// <summary>
/// This event is raised when an extension tab is closed and this event
/// is subscribed by the Workspace dependency view extension.
/// <param name="extensionTabName"></param>
/// </summary>
internal event Action<String> OnExtensionTabClosed;
private void OnExtensionTabClosedHandler(String extensionTabName)
{
if (OnExtensionTabClosed != null)
{
OnExtensionTabClosed(extensionTabName);
}
}

/// <summary>
/// Send a request to the package manager client to download this package and its dependencies
/// </summary>
Expand Down Expand Up @@ -282,7 +267,6 @@ public void Dispose()
loadedParams.CurrentWorkspaceCleared -= OnWorkspaceCleared;
currentWorkspace.PropertyChanged -= OnWorkspacePropertyChanged;
WorkspaceModel.DummyNodesReloaded -= TriggerDependencyRegen;
DynamoView.CloseExtension -= this.OnExtensionTabClosedHandler;
HomeWorkspaceModel.WorkspaceClosed -= this.CloseExtensionTab;
PackageDependencyTable.ItemsSource = null;
dataRows = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Linq;
using System.Windows.Controls;
using Dynamo.Controls;
using Dynamo.Extensions;
using Dynamo.Graph.Workspaces;
using Dynamo.Logging;
Expand All @@ -15,7 +16,7 @@ namespace Dynamo.WorkspaceDependency
/// which tracks graph dependencies (currently only packages) on the Dynamo right panel.
/// It reacts to workspace modified/ cleared events to refresh.
/// </summary>
public class WorkspaceDependencyViewExtension : IViewExtension, ILogSource
public class WorkspaceDependencyViewExtension : ViewExtensionBase, IViewExtension, ILogSource
{
internal MenuItem workspaceReferencesMenuItem;
private readonly String extensionName = Properties.Resources.ExtensionName;
Expand Down Expand Up @@ -55,7 +56,6 @@ public string UniqueId
/// </summary>
public void Dispose()
{
DependencyView.OnExtensionTabClosed -= OnCloseExtension;
DependencyView.Dispose();
}

Expand All @@ -82,14 +82,6 @@ internal void OnMessageLogged(ILogMessage msg)
this.MessageLogged?.Invoke(msg);
}

internal void OnCloseExtension(String extensionTabName)
{
if (extensionTabName.Equals(extensionName))
{
this.workspaceReferencesMenuItem.IsChecked = false;
}
}

public void Loaded(ViewLoadedParams viewLoadedParams)
{
DependencyView = new WorkspaceDependencyView(this, viewLoadedParams);
Expand All @@ -101,8 +93,6 @@ public void Loaded(ViewLoadedParams viewLoadedParams)
DependencyView.DependencyRegen(viewLoadedParams.CurrentWorkspaceModel as WorkspaceModel);
};

DependencyView.OnExtensionTabClosed += OnCloseExtension;

// Adding a button in view menu to refresh and show manually
workspaceReferencesMenuItem = new MenuItem { Header = Resources.MenuItemString, IsCheckable = true, IsChecked = false };
workspaceReferencesMenuItem.Click += (sender, args) =>
Expand All @@ -119,10 +109,13 @@ public void Loaded(ViewLoadedParams viewLoadedParams)
viewLoadedParams.CloseExtensioninInSideBar(this);
workspaceReferencesMenuItem.IsChecked = false;
}

};
viewLoadedParams.AddMenuItem(MenuBarType.View, workspaceReferencesMenuItem);
}

public override void Closed()
{
this.workspaceReferencesMenuItem.IsChecked = false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,35 @@ public void APItoCloseViewExtensionTabTest()
Assert.AreEqual(0, View.ExtensionTabItems.Count);
}

/// <summary>
/// This test will verify that the Closed() will be triggered on the extension that is closed.
/// </summary>
[Test]
public void OnViewExtensionClosedTest()
{
RaiseLoadedEvent(this.View);
var extensionManager = View.viewExtensionManager;
WorkspaceDependencyViewExtension workspaceDependencyViewExtension = (WorkspaceDependencyViewExtension) extensionManager.ViewExtensions
.Where(ve => ve.Name.Equals("Workspace References")).FirstOrDefault();
// Open a graph which should bring up the Workspace References view extension window with one tab
Open(@"pkgs\Dynamo Samples\extra\CustomRenderExample.dyn");
Assert.AreEqual(1, View.ExtensionTabItems.Count);

var loadedParams = new ViewLoadedParams(View, ViewModel);

// Assert that the workspace references menu item is checked.
Assert.IsTrue(workspaceDependencyViewExtension.workspaceReferencesMenuItem.IsChecked);

// Closing the view extension side bar should trigger the Closed() on the workspace dependency view extension.
// This will un-check the workspace references menu item.
loadedParams.CloseExtensioninInSideBar(workspaceDependencyViewExtension);

Assert.AreEqual(0, View.ExtensionTabItems.Count);

// Assert that the workspace references menu item is un-checked.
Assert.IsFalse(workspaceDependencyViewExtension.workspaceReferencesMenuItem.IsChecked);
}

/// <summary>
/// This test will make sure that the extension tab is closed upon closing the home workspace.
/// </summary>
Expand Down