Skip to content

Commit

Permalink
CM-39043 - Add back to home, expand all, and collapse all toolbar act…
Browse files Browse the repository at this point in the history
…ions (#22)
  • Loading branch information
MarshalX authored Sep 6, 2024
1 parent f7698c6 commit 486211e
Showing 22 changed files with 195 additions and 17 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -4,6 +4,11 @@

## [Unreleased]

## [1.4.0] - 2024-09-06

- Add Violation Cards
- Add new toolbar actions: `Back to Home`, `Expand All`, `Collapse All`

## [1.3.0] - 2024-08-28

- Add Tree View
@@ -45,6 +50,8 @@

The first public release of the extension.

[1.4.0]: https://github.com/cycodehq/visual-studio-extension/releases/tag/v1.4.0

[1.3.0]: https://github.com/cycodehq/visual-studio-extension/releases/tag/v1.3.0

[1.2.0]: https://github.com/cycodehq/visual-studio-extension/releases/tag/v1.2.0
@@ -63,4 +70,4 @@ The first public release of the extension.

[1.0.0]: https://github.com/cycodehq/visual-studio-extension/releases/tag/v1.0.0

[Unreleased]: https://github.com/cycodehq/visual-studio-extension/compare/v1.3.0...HEAD
[Unreleased]: https://github.com/cycodehq/visual-studio-extension/compare/v1.4.0...HEAD
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
<Metadata>
<Identity Id="Cycode.7e1a0714-9b3b-4e0e-9c0a-d23fb20ab86e" Version="1.3.0" Language="en-US" Publisher="cycodehq" />
<Identity Id="Cycode.7e1a0714-9b3b-4e0e-9c0a-d23fb20ab86e" Version="1.4.0" Language="en-US" Publisher="cycodehq" />
<DisplayName>Cycode</DisplayName>
<Description xml:space="preserve">Cycode for Visual Studio IDE</Description>
<MoreInfo>https://github.com/cycodehq/visual-studio-extension</MoreInfo>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
<Metadata>
<Identity Id="Cycode.f2c5020e-67a2-46f8-a888-609412fd59db" Version="1.3.0" Language="en-US" Publisher="cycodehq" />
<Identity Id="Cycode.f2c5020e-67a2-46f8-a888-609412fd59db" Version="1.4.0" Language="en-US" Publisher="cycodehq" />
<DisplayName>Cycode</DisplayName>
<Description xml:space="preserve">Cycode for Visual Studio IDE</Description>
<MoreInfo>https://github.com/cycodehq/visual-studio-extension</MoreInfo>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Cycode.VisualStudio.Extension.Shared.DTO;
using Cycode.VisualStudio.Extension.Shared.Services;

namespace Cycode.VisualStudio.Extension.Shared;

[Command(PackageIds.BackToHomeScreenCommand)]
internal sealed class BackToHomeScreenCommand : BaseCommand<BackToHomeScreenCommand> {
protected override void Execute(object sender, EventArgs e) {
IToolWindowMessengerService toolWindowMessengerService =
ServiceLocator.GetService<IToolWindowMessengerService>();
toolWindowMessengerService.Send(new MessageEventArgs(MessengerCommand.BackToHomeScreen));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Cycode.VisualStudio.Extension.Shared.DTO;
using Cycode.VisualStudio.Extension.Shared.Services;

namespace Cycode.VisualStudio.Extension.Shared;

[Command(PackageIds.TreeViewCollapseAllCommand)]
internal sealed class TreeViewCollapseAllCommand : BaseCommand<TreeViewCollapseAllCommand> {
protected override void Execute(object sender, EventArgs e) {
IToolWindowMessengerService toolWindowMessengerService =
ServiceLocator.GetService<IToolWindowMessengerService>();
toolWindowMessengerService.Send(new MessageEventArgs(MessengerCommand.TreeViewCollapseAll));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Cycode.VisualStudio.Extension.Shared.DTO;
using Cycode.VisualStudio.Extension.Shared.Services;

namespace Cycode.VisualStudio.Extension.Shared;

[Command(PackageIds.TreeViewExpandAllCommand)]
internal sealed class TreeViewExpandAllCommand : BaseCommand<TreeViewExpandAllCommand> {
protected override void Execute(object sender, EventArgs e) {
IToolWindowMessengerService toolWindowMessengerService =
ServiceLocator.GetService<IToolWindowMessengerService>();
toolWindowMessengerService.Send(new MessageEventArgs(MessengerCommand.TreeViewExpandAll));
}
}
Original file line number Diff line number Diff line change
@@ -10,6 +10,8 @@
namespace Cycode.VisualStudio.Extension.Shared.Components.ToolWindows;

public class CycodeToolWindowViewModel : INotifyPropertyChanged {
private readonly ExtensionState _state = ServiceLocator.GetService<IStateService>().Load();

private readonly CycodeTreeViewControl _cycodeTreeView;
private UserControl _leftSideView;
private UserControl _rightSideView;
@@ -45,16 +47,44 @@ private void OnPropertyChanged(string propertyName) {
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

private UserControl GetStateDependentRightSideView() {
UserControl newRightSideView;

if (!_state.CliInstalled) {
newRightSideView = new LoadingControl();
} else {
newRightSideView = _state.CliAuthed switch {
true => new MainControl(),
false => new AuthControl()
};
}

// return the new view if it's different from the current one
return newRightSideView.GetType() == RightSideView.GetType() ? RightSideView : newRightSideView;
}

private void OnMessageReceived(object sender, MessageEventArgs args) {
RightSideView = args.Command switch {
MessengerCommand.LoadLoadingControl => new LoadingControl(),
MessengerCommand.LoadAuthControl => new AuthControl(),
MessengerCommand.LoadMainControl => new MainControl(),
MessengerCommand.LoadSecretViolationCardControl => new SecretViolationCardControl((SecretDetection) args.Data),
MessengerCommand.LoadScaViolationCardControl => new ScaViolationCardControl((ScaDetection) args.Data),
MessengerCommand.LoadSecretViolationCardControl => new SecretViolationCardControl(
(SecretDetection)args.Data),
MessengerCommand.LoadScaViolationCardControl => new ScaViolationCardControl((ScaDetection)args.Data),
MessengerCommand.BackToHomeScreen => GetStateDependentRightSideView(),
_ => RightSideView
};

if (args.Command == MessengerCommand.RefreshTreeView) _cycodeTreeView.RefreshTree();
switch (args.Command) {
case MessengerCommand.TreeViewRefresh:
_cycodeTreeView.RefreshTree();
break;
case MessengerCommand.TreeViewExpandAll:
_cycodeTreeView.ExpandAllNodes();
break;
case MessengerCommand.TreeViewCollapseAll:
_cycodeTreeView.CollapseAllNodes();
break;
}
}
}
Original file line number Diff line number Diff line change
@@ -29,6 +29,18 @@
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
<TreeView.Resources>
<Style TargetType="TreeViewItem">
<Setter Property="IsExpanded" Value="{Binding IsExpanded}" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsExpanded}" Value="True">
<Setter Property="IsExpanded" Value="True" />
</DataTrigger>
<DataTrigger Binding="{Binding IsExpanded}" Value="False">
<Setter Property="IsExpanded" Value="False" />
</DataTrigger>
</Style.Triggers>
</Style>

<!-- Style the inactive selection the same as active -->
<!-- Ref: https://stackoverflow.com/a/37447594/8032027 -->
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}"
Original file line number Diff line number Diff line change
@@ -161,4 +161,25 @@ public void RefreshTree() {
TreeView.Items.Clear();
CreateNodes();
}

private void SetIsExpandedToAllNodes(bool isExpanded) {
Stack<BaseNode> stack = new(TreeView.Items.Cast<BaseNode>());
while (stack.Any()) {
BaseNode node = stack.Pop();
foreach (BaseNode child in node.Items) stack.Push(child);
node.IsExpanded = isExpanded;
}

TreeView.UpdateLayout();
}

public void ExpandAllNodes() {
_logger.Debug("Expand all nodes");
SetIsExpandedToAllNodes(true);
}

public void CollapseAllNodes() {
_logger.Debug("Collapse all nodes");
SetIsExpandedToAllNodes(false);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
using System.Collections.ObjectModel;
using System.Windows;

namespace Cycode.VisualStudio.Extension.Shared.Components.TreeView.Nodes;

public class BaseNode {
public class BaseNode : DependencyObject {
public string Title { get; set; }
public string Summary { get; set; }
public string Icon { get; set; }
public ObservableCollection<BaseNode> Items { get; set; } = [];

public static readonly DependencyProperty IsExpandedProperty = DependencyProperty.Register(
nameof(IsExpanded), typeof(bool), typeof(BaseNode), new PropertyMetadata(false));

public bool IsExpanded {
get { return (bool)GetValue(IsExpandedProperty); }
set { SetValue(IsExpandedProperty, value); }
}
}
Original file line number Diff line number Diff line change
@@ -33,11 +33,14 @@
<Compile Include="$(MSBuildThisFileDirectory)Cli\ErrorHandling.cs"/>
<Compile Include="$(MSBuildThisFileDirectory)Cli\ExitCode.cs"/>
<Compile Include="$(MSBuildThisFileDirectory)Cli\UserAgent.cs"/>
<Compile Include="$(MSBuildThisFileDirectory)Commands\BackToHomeScreenCommand.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Commands\ClearScanResultsCommand.cs"/>
<Compile Include="$(MSBuildThisFileDirectory)Commands\OpenToolWindowCommand.cs"/>
<Compile Include="$(MSBuildThisFileDirectory)Commands\OpenSettingsCommand.cs"/>
<Compile Include="$(MSBuildThisFileDirectory)Commands\OpenWebDocsCommand.cs"/>
<Compile Include="$(MSBuildThisFileDirectory)Commands\RunAllScansCommand.cs"/>
<Compile Include="$(MSBuildThisFileDirectory)Commands\TreeViewCollapseAllCommand.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Commands\TreeViewExpandAllCommand.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Components\TreeView\Nodes\BaseNode.cs"/>
<Compile Include="$(MSBuildThisFileDirectory)Components\TreeView\Nodes\DetectionNodes\ScaDetectionNode.cs"/>
<Compile Include="$(MSBuildThisFileDirectory)Components\TreeView\Nodes\DetectionNodes\SecretDetectionNode.cs"/>
@@ -119,6 +122,15 @@
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<IncludeInVSIX>true</IncludeInVSIX>
</Content>
<Content Include="$(MSBuildThisFileDirectory)Resources\KnownMonikers\CollapseAll.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="$(MSBuildThisFileDirectory)Resources\KnownMonikers\ExpandAll.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="$(MSBuildThisFileDirectory)Resources\KnownMonikers\Previous.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Resource Include="$(MSBuildThisFileDirectory)Resources\CardSeverity\C.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Resource>
Original file line number Diff line number Diff line change
@@ -44,6 +44,9 @@ protected override async Task InitializeAsync(
IStateService stateService = ServiceLocator.GetService<IStateService>();
stateService.Load();

ICliService cliService = ServiceLocator.GetService<ICliService>();
cliService.ResetPluginCliState();

await this.RegisterCommandsAsync();
this.RegisterToolWindows();

Original file line number Diff line number Diff line change
@@ -4,9 +4,13 @@ public static class MessengerCommand {
public const string LoadLoadingControl = "load_loading_control";
public const string LoadAuthControl = "load_auth_control";
public const string LoadMainControl = "load_main_control";

public const string RefreshTreeView = "refresh_tree_view";


public const string TreeViewRefresh = "tree_view_refresh";
public const string TreeViewExpandAll = "tree_view_expand_all";
public const string TreeViewCollapseAll = "tree_view_collapse_all";

public const string LoadSecretViolationCardControl = "load_secret_violation_card_control";
public const string LoadScaViolationCardControl = "load_sca_violation_card_control";

public const string BackToHomeScreen = "back_to_home_screen";
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -115,7 +115,7 @@ public async Task ScanPathsScaAsync(
ShowScanFileResultNotification(CliScanType.Sca, detectionsCount, onDemand);
}

private void ResetPluginCliState() {
public void ResetPluginCliState() {
logger.Debug("Resetting plugin CLI state");

_pluginState.CliAuthed = false;
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ public async Task RecreateAsync() {
await CreateErrorTasksAsync(scanResultsService.GetScaResults());

CycodePackage.ErrorTaggerProvider.Rerender();
toolWindowMessengerService.Send(new MessageEventArgs(MessengerCommand.RefreshTreeView));
toolWindowMessengerService.Send(new MessageEventArgs(MessengerCommand.TreeViewRefresh));
}

public async Task ClearErrorsAsync() {
Original file line number Diff line number Diff line change
@@ -5,6 +5,8 @@
namespace Cycode.VisualStudio.Extension.Shared.Services;

public interface ICliService {
void ResetPluginCliState();

Task<bool> HealthCheckAsync(CancellationToken cancellationToken = default);
Task<bool> CheckAuthAsync(CancellationToken cancellationToken = default);
Task<bool> DoAuthAsync(CancellationToken cancellationToken = default);
Original file line number Diff line number Diff line change
@@ -19,6 +19,9 @@ internal sealed class PackageIds {
public const int ToolbarRunAllScansCommand = 0x1053;
public const int ToolbarOpenWebDocsCommand = 0x1054;
public const int ToolbarClearScanResultsCommand = 0x1055;
public const int BackToHomeScreenCommand = 0x1056;
public const int TreeViewExpandAllCommand = 0x1057;
public const int TreeViewCollapseAllCommand = 0x1058;

public const int TopMenuCycodeCommand = 0x1103;
public const int TopMenuOpenSettingsCommand = 0x1104;
Loading

0 comments on commit 486211e

Please sign in to comment.