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

Add support for debug modes #10603

Merged
merged 10 commits into from
May 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 134 additions & 0 deletions src/DynamoCore/Configuration/DebugModes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Xml;

namespace Dynamo.Configuration
{
/// <summary>
/// Provide functionality around debug modes. Similar to feature flags.
/// </summary>
internal class DebugModes
{
static private readonly Dictionary<string, DebugMode> debugModes;
private static bool debugModesEnabled;

/// <summary>
/// Represents an instance of a debug mode
/// </summary>
public class DebugMode
{
/// <summary>
/// Name of the debug mode
/// </summary>
public string Name;
/// <summary>
/// Description of the debug mode
/// </summary>
public string Description;
/// <summary>
/// Whether debug mode is enabled or not
/// </summary>
public bool Enabled;
}
private static void AddDebugMode(string name, string description)
{
debugModes[name] = new DebugMode()
{
Name = name,
Description = description,
Enabled = false
};
}
private static void RegisterDebugModes()
{
// Add new debug modes here!!
//
// Example:
// AddDebugMode("TestDebugMode", "Enabe/disable TestDebugMode.");
}

private static void LoadDebugModesStatusFromConfig(string configPath)
{
try
{
debugModesEnabled = false;

XmlDocument xml = new XmlDocument();
xml.Load(configPath);

if (xml == null) { return; }

debugModesEnabled = true;

var debugItems = xml.DocumentElement.SelectNodes("DebugMode");
foreach (XmlNode item in debugItems)
{
var name = item.Attributes["name"].Value;
bool enabled;
Boolean.TryParse(item.Attributes["enabled"].Value, out enabled);

if (!debugModes.ContainsKey(name)) { continue; }
debugModes[name].Enabled = enabled;
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
/// <summary>
/// Static constructor
/// </summary>
static DebugModes()
{
debugModes = new Dictionary<string, DebugMode>();
try
{
RegisterDebugModes();
LoadDebugModesStatusFromConfig(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "debug.config"));
}
catch (Exception)
{ }
}
/// <summary>
/// Enables/Disables a debug mode
/// </summary>
/// <param name="name">Name of the debug mode</param>
/// <param name="enabled">Enable/Disable debug mode</param>
public static void SetDebugModeEnabled(string name, bool enabled)
{
DebugMode dbgMode;
if (debugModes.TryGetValue(name, out dbgMode))
{
dbgMode.Enabled = enabled;
}
}
/// <summary>
/// Returns a dictionary of all the debug modes
/// </summary>
public static Dictionary<string, DebugMode> GetDebugModes()
{
return debugModes;
}
/// <summary>
/// Retrieves a debug mode
/// </summary>
/// <param name="name">Name of the debug mode</param>
public static DebugMode GetDebugMode(string name)
{
DebugMode dMode;
return debugModes.TryGetValue(name, out dMode) ? dMode : null;
}
/// <summary>
/// Retrieves the state of a debug mode (enabled/disabled)
/// </summary>
/// <param name="name">Name of the debug mode</param>
public static bool Enabled(string name)
{
DebugMode dbgMode;
return debugModesEnabled && debugModes.TryGetValue(name, out dbgMode) ? dbgMode.Enabled : false;
}
}
}
1 change: 1 addition & 0 deletions src/DynamoCore/DynamoCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ limitations under the License.
<Compile Include="Interfaces\IDynamoModel.cs" />
<Compile Include="Library\ILibraryLoader.cs" />
<Compile Include="Graph\Workspaces\ICustomNodeWorkspaceModel.cs" />
<Compile Include="Configuration\DebugModes.cs" />
<Compile Include="Visualization\DefaultGraphicPrimitives.cs" />
<Compile Include="Visualization\GeometryHolder.cs" />
<Compile Include="Visualization\IRenderPackageSource.cs" />
Expand Down
7 changes: 7 additions & 0 deletions src/DynamoCoreWpf/DynamoCoreWpf.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,9 @@
</Compile>
<Compile Include="Views\CodeBlocks\CodeBlockEditor.cs" />
<Compile Include="Views\Core\DynamoOpenFileDialog.cs" />
<Compile Include="Views\Debug\DebugModesWindow.xaml.cs">
<DependentUpon>DebugModesWindow.xaml</DependentUpon>
</Compile>
<Compile Include="Views\Gallery\GalleryView.xaml.cs">
<DependentUpon>GalleryView.xaml</DependentUpon>
</Compile>
Expand Down Expand Up @@ -1102,6 +1105,10 @@
</Page>
</ItemGroup>
<ItemGroup>
<Page Include="Views\Debug\DebugModesWindow.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Views\Gallery\GalleryView.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down
9 changes: 9 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.

4 changes: 4 additions & 0 deletions src/DynamoCoreWpf/Properties/Resources.en-US.resx
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,10 @@ You will get a chance to save your work.</value>
<value>Verbose Logging</value>
<comment>Debug menu | Verbose logging</comment>
</data>
<data name="DynamoViewDebugMenuDebugModes" xml:space="preserve">
<value>Debug Modes</value>
<comment>Debug menu | Show debug modes</comment>
</data>
<data name="DynamoViewEditMenu" xml:space="preserve">
<value>_Edit</value>
<comment>Edit menu</comment>
Expand Down
4 changes: 4 additions & 0 deletions src/DynamoCoreWpf/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@
<value>Verbose Logging</value>
<comment>Debug menu | Verbose logging</comment>
</data>
<data name="DynamoViewDebugMenuDebugModes" xml:space="preserve">
<value>Debug Modes</value>
<comment>Debug menu | Show debug modes</comment>
</data>
<data name="DynamoViewEditMenu" xml:space="preserve">
<value>_Edit</value>
<comment>Edit menu</comment>
Expand Down
7 changes: 6 additions & 1 deletion src/DynamoCoreWpf/Views/Core/DynamoView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,12 @@
Header="{x:Static p:Resources.DynamoViewDebugMenuDumpLibrary}"
Command="{Binding DumpLibraryToXmlCommand}"
IsEnabled="True" />
</MenuItem>
<MenuItem Focusable="False"
Name="DebugModes"
Header="{x:Static p:Resources.DynamoViewDebugMenuDebugModes}"
Click="OnDebugModesClick"
IsCheckable="False" />
</MenuItem>
</Menu>

<!--Titlebar buttons-->
Expand Down
8 changes: 8 additions & 0 deletions src/DynamoCoreWpf/Views/Core/DynamoView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
using Dynamo.Wpf.Extensions;
using Dynamo.Wpf.Utilities;
using Dynamo.Wpf.ViewModels.Core;
using Dynamo.Wpf.Views.Debug;
using Dynamo.Wpf.Views.Gallery;
using Dynamo.Wpf.Views.PackageManager;
using HelixToolkit.Wpf.SharpDX;
Expand Down Expand Up @@ -1562,6 +1563,13 @@ private static void OnShowInFolder(object sender, RoutedEventArgs e)
}
#endif

private void OnDebugModesClick(object sender, RoutedEventArgs e)
{
var debugModesWindow = new DebugModesWindow();
debugModesWindow.Owner = this;
debugModesWindow.WindowStartupLocation = WindowStartupLocation.CenterOwner;
debugModesWindow.ShowDialog();
}
/// <summary>
/// Setup the "Samples" sub-menu with contents of samples directory.
/// </summary>
Expand Down
37 changes: 37 additions & 0 deletions src/DynamoCoreWpf/Views/Debug/DebugModesWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<Window x:Class="Dynamo.Wpf.Views.Debug.DebugModesWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:p="clr-namespace:Dynamo.Wpf.Properties"
Title="{x:Static p:Resources.DynamoViewDebugMenuDebugModes}"
ResizeMode="NoResize" Width="350" Height="510"
>
<StackPanel>
<ListView Name="CheckList" Height="320" Margin="10,0">
<ListView.ItemTemplate>
<DataTemplate>
<WrapPanel>
<CheckBox IsChecked="{Binding Enabled}"/>
<TextBlock Margin="10,0,0,0" Text="{Binding Name}" />
</WrapPanel>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="OnDebugModeItemClick" />
</Style>
</ListView.ItemContainerStyle>
</ListView>
<ScrollViewer Margin="10,5,10,0" Height="100">
<TextBox IsReadOnly="True" TextWrapping="Wrap" x:Name="SelectedDbgMode" Text="{Binding SelectedDebugMode}">
</TextBox>
</ScrollViewer>
<StackPanel Orientation="Horizontal" Height="46" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="188">
<Button Content="OK"
Height="25" Margin="10,10,10,10" Width="75" x:Name="btnOK" TabIndex="1600" IsDefault="True" Click="OnOkClick"
VerticalContentAlignment="Center" HorizontalContentAlignment="Center" />
<Button Content="Cancel"
Height="25" Margin="10,10,10,10" Width="75" x:Name="btnCancel" TabIndex="1700" IsCancel="True"
VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Click="OnCancelClick" />
</StackPanel>
</StackPanel>
</Window>
69 changes: 69 additions & 0 deletions src/DynamoCoreWpf/Views/Debug/DebugModesWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using System.Collections.Generic;
using Dynamo.Configuration;
using System.Linq;
using System.Text;
using System.Windows;

namespace Dynamo.Wpf.Views.Debug
{
/// <summary>
/// Interaction logic for DebugModesWindow.xaml
/// </summary>
public partial class DebugModesWindow : Window
{
/// <summary>
/// Constructor
/// </summary>
public DebugModesWindow()
{
InitializeComponent();

var items = new List<DebugModeListItem>();
foreach (var entry in DebugModes.GetDebugModes())
{
items.Add(new DebugModeListItem() {
Name = entry.Value.Name,
Enabled = entry.Value.Enabled
});
}
CheckList.ItemsSource = items;
}
private void OnDebugModeItemClick(object sender, RoutedEventArgs e)
{
var listItem = (System.Windows.Controls.ListBoxItem)sender;
if (null == listItem)
{
throw new Exception("Invalid Control type found. Expected ListBoxItem");
}
var dataContext = (DebugModeListItem)listItem.DataContext;
if (null == dataContext)
{
throw new Exception("Invalid data context type found. Expected DebugModeItem");
}
var debugMode = DebugModes.GetDebugMode(dataContext.Name);
if (null == debugMode)
{
throw new Exception("Invalid debug mode found");
}
SelectedDbgMode.Text = debugMode.Description;
}
private void OnOkClick(object sender, RoutedEventArgs e)
{
foreach (DebugModeListItem item in CheckList.Items)
{
DebugModes.SetDebugModeEnabled(item.Name, item.Enabled);
}
Close();
}

private void OnCancelClick(object sender, RoutedEventArgs e) {}

private class DebugModeListItem
{
public string Name { get; set; }

public bool Enabled { get; set; }
}
}
}
Loading