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 : ViewExtensionBaseClass, 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 OnViewExtensionClosed(string extensionTabName)
{
this.documentationBrowserMenuItem.IsChecked = false;
}
}
}
73 changes: 73 additions & 0 deletions src/DynamoCoreWpf/Extensions/IViewExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,77 @@ public interface IViewExtension: IDisposable
/// </summary>
void Shutdown();
}

/// <summary>
/// Base class for View Extension.
/// </summary>
public abstract class ViewExtensionBaseClass : IViewExtension
reddyashish marked this conversation as resolved.
Show resolved Hide resolved
{
/// <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 string UniqueId { get; set; }
reddyashish marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// A name for the extension instance. This is used for more user-readable logging.
/// </summary>
public string Name { get; set; }
reddyashish marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Dispose method for the view extension.
/// </summary>
public void Dispose()
reddyashish marked this conversation as resolved.
Show resolved Hide resolved
{

}

/// <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 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 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 void Startup(ViewStartupParams viewStartupParams)
{

}

/// <summary>
/// Action to be invoked when the view extenion is closed.
/// </summary>
public abstract void OnViewExtensionClosed(String extensionTabName);
reddyashish marked this conversation as resolved.
Show resolved Hide resolved
}
}
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 = (ViewExtensionBaseClass) viewExtension;
viewExtensionBaseClass.OnViewExtensionClosed(tabName);
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 = (ViewExtensionBaseClass) tabitem.Tag;
viewExtensionBaseClass.OnViewExtensionClosed(tabName);
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 = (ViewExtensionBaseClass)ext.Tag;
viewExtensionBaseClass.OnViewExtensionClosed(extName);
}
}

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 : ViewExtensionBaseClass, 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 OnViewExtensionClosed(string extensionTabName)
{
this.workspaceReferencesMenuItem.IsChecked = false;
}
}
}