Skip to content

Commit

Permalink
Add an option to remember last opened Extensions in Dynamo (#13812)
Browse files Browse the repository at this point in the history
Add an option to remember last opened Extensions in Dynamo
  • Loading branch information
zeusongit authored Mar 14, 2023
1 parent 228e017 commit 58a4593
Show file tree
Hide file tree
Showing 18 changed files with 435 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,22 @@
using System.IO;
using System.Linq;
using System.Security.Permissions;
using System.Web.UI.WebControls;
using System.Threading;
using System.Windows;
using System.Windows.Threading;
using DesignScript.Builtin;
using Dynamo.Configuration;
using Dynamo.Core;
using Dynamo.DocumentationBrowser.Properties;
using Dynamo.Graph;
using Dynamo.Graph.Annotations;
using Dynamo.Graph.Workspaces;
using Dynamo.Logging;
using Dynamo.Selection;
using Dynamo.Models;
using DynamoProperties = Dynamo.Properties;
using Dynamo.PackageManager;
using Dynamo.Selection;
using Dynamo.ViewModels;
using Dynamo.Wpf.Extensions;
using Dynamo.Wpf.Interfaces;
using DynamoProperties = Dynamo.Properties;
using MenuItem = System.Windows.Controls.MenuItem;

namespace Dynamo.DocumentationBrowser
Expand Down Expand Up @@ -179,8 +176,7 @@ public override void Loaded(ViewLoadedParams viewLoadedParams)
this.ViewModel.DynamoView = viewLoadedParams.DynamoWindow;
}


public void Shutdown()
public override void Shutdown()
{
Dispose();
}
Expand Down
17 changes: 17 additions & 0 deletions src/DynamoCore/Configuration/PreferenceSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class PreferenceSettings : NotificationObject, IPreferences, IRenderPreci
private double defaultScaleFactor;
private bool disableTrustWarnings = false;
private bool isNotificationCenterEnabled;
private bool isEnablePersistExtensionsEnabled;
private bool isStaticSplashScreenEnabled;
private bool isCreatedFromValidFile = true;
private bool isADPChecked = false;
Expand Down Expand Up @@ -575,6 +576,22 @@ public bool EnableNotificationCenter
}
}

/// <summary>
/// This defines if user wants the Extensions settings to persist across sessions.
/// </summary>
public bool EnablePersistExtensions
{
get
{
return isEnablePersistExtensionsEnabled;
}
set
{
isEnablePersistExtensionsEnabled = value;
RaisePropertyChanged(nameof(EnablePersistExtensions));
}
}


/// <summary>
/// This defines if the user wants to see the static splash screen again
Expand Down
8 changes: 6 additions & 2 deletions src/DynamoCore/Configuration/ViewExtensionSettings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Dynamo.Configuration
namespace Dynamo.Configuration
{
/// <summary>
/// Settings that apply to a view extension specifically.
Expand All @@ -21,6 +21,10 @@ public class ViewExtensionSettings
/// Window settings for the extension control when displayed in FloatingWindow mode.
/// </summary>
public WindowSettings WindowSettings { get; set; }
/// <summary>
/// Specifies if the extension was Open in the last session before closing Dynamo, if the property to remember view extension status was enabled.<br/> <c>Default: False</c>
/// </summary>
public bool IsOpen { get; set; }
}

/// <summary>
Expand Down Expand Up @@ -83,4 +87,4 @@ public enum WindowStatus
/// </summary>
Maximized
}
}
}
17 changes: 10 additions & 7 deletions src/DynamoCoreWpf/Extensions/ViewExtensionBase.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dynamo.Wpf.Extensions
{
/// <summary>
Expand Down Expand Up @@ -70,6 +64,15 @@ public virtual void Startup(ViewStartupParams viewStartupParams)

}

/// <summary>
/// Action to be invoked when Dynamo starts up, to re-open the view extension which was open in the last session, if the preference setting
/// to remember the last opened extensions was enabled.
/// </summary>
public virtual void ReOpen()
{

}

/// <summary>
/// Action to be invoked when the view extension is closed.
/// </summary>
Expand All @@ -78,4 +81,4 @@ public virtual void Closed()

}
}
}
}
18 changes: 18 additions & 0 deletions src/DynamoCoreWpf/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions src/DynamoCoreWpf/Properties/Resources.en-US.resx
Original file line number Diff line number Diff line change
Expand Up @@ -3494,6 +3494,13 @@ In certain complex graphs or host program scenarios, Automatic mode may cause in
<data name="DynamoViewSettingLibraryZoomScale" xml:space="preserve">
<value>Library</value>
</data>
<data name="PreferencesViewEnablePersistExtensions" xml:space="preserve">
<value>On launch, remember open extensions panels from last session</value>
</data>
<data name="PreferencesViewExtensions" xml:space="preserve">
<value>Extensions</value>
<comment>Preferences | Features | Extensions</comment>
</data>
<data name="PythonZoomScaleTooltipText" xml:space="preserve">
<value>Drag to adjust Python Editor zoom scale between 25% and 300%. You can also adjust this in Python Editor by pressing "Ctrl" and scrolling the mouse wheel.</value>
</data>
Expand Down
7 changes: 7 additions & 0 deletions src/DynamoCoreWpf/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -3481,6 +3481,13 @@ In certain complex graphs or host program scenarios, Automatic mode may cause in
<data name="DynamoViewSettingLibraryZoomScale" xml:space="preserve">
<value>Library</value>
</data>
<data name="PreferencesViewEnablePersistExtensions" xml:space="preserve">
<value>On launch, remember open extensions panels from last session</value>
</data>
<data name="PreferencesViewExtensions" xml:space="preserve">
<value>Extensions</value>
<comment>Preferences | Features | Extensions</comment>
</data>
<data name="PythonZoomScaleTooltipText" xml:space="preserve">
<value>Drag to adjust Python Editor zoom scale between 25% and 300%. You can also adjust this in Python Editor by pressing "Ctrl" and scrolling the mouse wheel.</value>
</data>
Expand Down
23 changes: 20 additions & 3 deletions src/DynamoCoreWpf/ViewModels/Menu/PreferencesViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ public string SelectedPythonEngine
}

/// <summary>
/// Controls the IsChecked property in the "Hide IronPython alerts" toogle button
/// Controls the IsChecked property in the "Hide IronPython alerts" toggle button
/// </summary>
public bool HideIronPythonAlertsIsChecked
{
Expand All @@ -787,7 +787,7 @@ public bool HideIronPythonAlertsIsChecked
}

/// <summary>
/// Controls the IsChecked property in the "Show Whitespace in Python editor" toogle button
/// Controls the IsChecked property in the "Show Whitespace in Python editor" toggle button
/// </summary>
public bool ShowWhitespaceIsChecked
{
Expand All @@ -804,7 +804,7 @@ public bool ShowWhitespaceIsChecked
}

/// <summary>
/// Controls the IsChecked property in the "Notification Center" toogle button
/// Controls the IsChecked property in the "Notification Center" toggle button
/// </summary>
public bool NotificationCenterIsChecked
{
Expand All @@ -819,6 +819,23 @@ public bool NotificationCenterIsChecked
}
}

/// <summary>
/// Controls the IsChecked property in the "Extensions" toggle button, to enable persisted extensions, that will remember
/// extensions setting as per the last session.
/// </summary>
public bool PersistExtensionsIsChecked
{
get
{
return preferenceSettings.EnablePersistExtensions;
}
set
{
preferenceSettings.EnablePersistExtensions = value;
RaisePropertyChanged(nameof(PersistExtensionsIsChecked));
}
}

#region [ Node Autocomplete ]

/// <summary>
Expand Down
81 changes: 76 additions & 5 deletions src/DynamoCoreWpf/Views/Core/DynamoView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ namespace Dynamo.Controls
public partial class DynamoView : Window, IDisposable
{
public const string BackgroundPreviewName = "BackgroundPreview";
private const int SideBarCollapseThreshold = 20;
private const int navigationInterval = 100;
// This is used to determine whether ESC key is being held down
private bool IsEscKeyPressed = false;
Expand Down Expand Up @@ -324,6 +325,11 @@ internal bool AddOrFocusExtensionControl(IViewExtension viewExtension, UIElement
this.dynamoViewModel.PreferenceSettings.ViewExtensionSettings.Add(settings);
}

if (this.dynamoViewModel.PreferenceSettings.EnablePersistExtensions)
{
settings.IsOpen = true;
}

if (settings.DisplayMode == ViewExtensionDisplayMode.FloatingWindow)
{
window = AddExtensionWindow(viewExtension, content, settings.WindowSettings);
Expand Down Expand Up @@ -969,6 +975,7 @@ private void DynamoLoadedViewExtensionHandler(ViewLoadedParams loadedParams, IEn
try
{
ext.Loaded(loadedParams);
ReOpenSavedExtensionOnDynamoStartup(ext);
}
catch (Exception exc)
{
Expand All @@ -977,6 +984,27 @@ private void DynamoLoadedViewExtensionHandler(ViewLoadedParams loadedParams, IEn
}
}

/// <summary>
/// This method will re-open saved extension from last session,
/// if the setting to remember last opened extensions was enabled.
/// </summary>
/// <param name="ext">Extension to be re-opened, if saved from last session.</param>
private void ReOpenSavedExtensionOnDynamoStartup(IViewExtension ext)
{
var viewExtensionSettings = dynamoViewModel.PreferenceSettings.EnablePersistExtensions ? dynamoViewModel.PreferenceSettings.ViewExtensionSettings : null;
if (viewExtensionSettings != null && viewExtensionSettings.Count > 0)
{
var setting = viewExtensionSettings.Find(s => s.UniqueId == ext.UniqueId);
if (setting != null && setting.IsOpen)
{
if (ext is ViewExtensionBase viewExtensionBase)
{
viewExtensionBase.ReOpen();
}
}
}
}

private void DynamoView_Loaded(object sender, EventArgs e)
{
// Do an initial load of the cursor collection
Expand Down Expand Up @@ -1586,7 +1614,7 @@ private bool PerformShutdownSequenceOnViewModel()

private void WindowClosing(object sender, CancelEventArgs e)
{
SaveExtensionWindowsState();
SaveExtensionsState();

if (!PerformShutdownSequenceOnViewModel() && !DynamoModel.IsTestMode)
{
Expand All @@ -1599,14 +1627,57 @@ private void WindowClosing(object sender, CancelEventArgs e)
}

/// <summary>
/// Saves the state of currently displayed extension windows. This is needed because the closing event is
/// Saves the state of currently displayed extension windows and tabs. This is needed because the closing event is
/// not called on child windows: https://docs.microsoft.com/en-us/dotnet/api/system.windows.window.closing
/// </summary>
private void SaveExtensionWindowsState()
private void SaveExtensionsState()
{
//loop over all active extension windows and tabs.
foreach (var window in ExtensionWindows.Values)
{
SaveExtensionWindowSettings(window);
SaveExtensionOpenState(window);
}
//for any new extensions that are opened for the first time
foreach (var tab in ExtensionTabItems)
{
SaveExtensionOpenState(tab);
}
//update open state of all existing view extension in setting, if option to remember extensions is enabled in preferences
var settings = dynamoViewModel.PreferenceSettings.ViewExtensionSettings;
foreach (var setting in settings)
{
if (!ExtensionTabItems.Any(e => e.Uid == setting.UniqueId) && !ExtensionWindows.Values.Any(e => e.Uid == setting.UniqueId))
{
setting.IsOpen = false;
}
}
}
//This method is to ensure that the extensions states are correctly saved for newly added extensions.
private void SaveExtensionOpenState(object o)
{
if (!dynamoViewModel.PreferenceSettings.EnablePersistExtensions || o == null) return;

var extId = string.Empty;
switch (o)
{
case TabItem t:
extId = t.Uid;
break;
case ExtensionWindow w:
extId = w.Uid;
break;
default:
Log("Incorrect extension type, could not save extension state.");
break;
}

if (string.IsNullOrEmpty(extId)) return;

var setting = dynamoViewModel.Model.PreferenceSettings.ViewExtensionSettings?.Find(ext => ext.UniqueId == extId);
if (setting != null)
{
setting.IsOpen = true;
}
}

Expand Down Expand Up @@ -2166,7 +2237,7 @@ public bool LibraryCollapsed
get
{
// Threshold that determines if button should be displayed
if (LeftExtensionsViewColumn.Width.Value < 20)
if (LeftExtensionsViewColumn.Width.Value < SideBarCollapseThreshold)
{ libraryCollapsed = true; }

else
Expand All @@ -2192,7 +2263,7 @@ public bool ExtensionsCollapsed
}
else
{
extensionsCollapsed = RightExtensionsViewColumn.Width.Value < 20;
extensionsCollapsed = RightExtensionsViewColumn.Width.Value < SideBarCollapseThreshold;
}

return extensionsCollapsed;
Expand Down
Loading

0 comments on commit 58a4593

Please sign in to comment.