Skip to content

Commit

Permalink
chore: use correct add menu message
Browse files Browse the repository at this point in the history
  • Loading branch information
rafael-rosa-knowcode committed Oct 16, 2024
1 parent a9b14fb commit 785517a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
23 changes: 15 additions & 8 deletions src/Uno.UI.RemoteControl.VS/Commands/UnoMenuCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ internal sealed class UnoMenuCommand
private static readonly int UnoMainMenu = 0x4100;

Check notice on line 21 in src/Uno.UI.RemoteControl.VS/Commands/UnoMenuCommand.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Uno.UI.RemoteControl.VS/Commands/UnoMenuCommand.cs#L21

Replace this 'static readonly' declaration with 'const'.
private static readonly int DynamicMenuCommandId = 0x4103;

Check notice on line 22 in src/Uno.UI.RemoteControl.VS/Commands/UnoMenuCommand.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Uno.UI.RemoteControl.VS/Commands/UnoMenuCommand.cs#L22

Replace this 'static readonly' declaration with 'const'.

public List<CommandRequestIdeMessage> CommandList { get; set; } = [];
public List<AddMenuItemRequestIdeMessage> CommandList { get; set; } = [];
public static UnoMenuCommand? Instance { get; private set; }

private UnoMenuCommand(AsyncPackage package, IdeChannelClient ideChannelClient, OleMenuCommandService commandService, CommandRequestIdeMessage cr)
private UnoMenuCommand(AsyncPackage package, IdeChannelClient ideChannelClient, OleMenuCommandService commandService, AddMenuItemRequestIdeMessage cr)
{
_package = package ?? throw new ArgumentNullException(nameof(_package));
CommandService = commandService = commandService ?? throw new ArgumentNullException(nameof(commandService));
Expand All @@ -43,7 +43,7 @@ private UnoMenuCommand(AsyncPackage package, IdeChannelClient ideChannelClient,
}
}

public static async Task InitializeAsync(AsyncPackage package, IdeChannelClient ideChannelClient, CommandRequestIdeMessage cr)
public static async Task InitializeAsync(AsyncPackage package, IdeChannelClient ideChannelClient, AddMenuItemRequestIdeMessage cr)
{
// Switch to the main thread - the call to AddCommand in DynamicMenu's constructor requires the UI thread.
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(package.DisposalToken);
Expand Down Expand Up @@ -91,10 +91,17 @@ private void OnInvokedDynamicItem(object sender, EventArgs args)
{
if (IdeChannelClient != null &&
sender is DynamicItemMenuCommand matchedCommand &&
TryGetCommandRequestIdeMessage(matchedCommand, out var currentCommand))
TryGetCommandRequestIdeMessage(matchedCommand, out var currentMenu) &&
currentMenu.Command is Command cmd)
{
var cmdMessage =
new CommandRequestIdeMessage(
System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle.ToInt64(),
cmd.Name,
cmd.Parameter);

// Ignoring the async call as the OleMenuCommand.execHandler is private and not async
_ = IdeChannelClient.SendToDevServerAsync(currentCommand, _package.DisposalToken);
_ = IdeChannelClient.SendToDevServerAsync(cmdMessage, _package.DisposalToken);
}
}

Expand All @@ -109,9 +116,9 @@ private void OnBeforeQueryStatusDynamicItem(object sender, EventArgs args)
matchedCommand.Enabled = true;
matchedCommand.Visible = true;

if (TryGetCommandRequestIdeMessage(matchedCommand, out var currentCommand))
if (TryGetCommandRequestIdeMessage(matchedCommand, out var currentMenu))
{
matchedCommand.Text = currentCommand.Command;
matchedCommand.Text = currentMenu.Command.Text;
}
// Clear the ID because we are done with this item.
matchedCommand.MatchedCommandId = 0;
Expand All @@ -121,6 +128,6 @@ private static int GetCurrentPosition(DynamicItemMenuCommand matchedCommand) =>
// The position of the command is the command ID minus the ID of the root dynamic start item.
matchedCommand.MatchedCommandId == 0 ? 0 : matchedCommand.MatchedCommandId - (int)DynamicMenuCommandId;

private bool TryGetCommandRequestIdeMessage(DynamicItemMenuCommand matchedCommand, [NotNullWhen(true)] out CommandRequestIdeMessage result)
private bool TryGetCommandRequestIdeMessage(DynamicItemMenuCommand matchedCommand, [NotNullWhen(true)] out AddMenuItemRequestIdeMessage result)
=> (result = CommandList.Skip(GetCurrentPosition(matchedCommand)).FirstOrDefault()) != null;
}
6 changes: 3 additions & 3 deletions src/Uno.UI.RemoteControl.VS/EntryPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ private async Task EnsureServerAsync()
_ideChannelClient = new IdeChannelClient(pipeGuid, new Logger(this));
_ideChannelClient.ForceHotReloadRequested += OnForceHotReloadRequestedAsync;
_ideChannelClient.OnMessageReceived += OnMessageReceivedAsync;
_ideChannelClient.OnCommandIdeMessageRequested += OnCommandIdeMessageRequestedAsync;
_ideChannelClient.OnAddMenuItemIdeMessageRequested += OnAddMenuItemRequestIdeMessageAsync;
_ideChannelClient.ConnectToHost();

// Set the port to the projects
Expand Down Expand Up @@ -441,7 +441,7 @@ await _asyncPackage.GetServiceAsync(typeof(SVsInfoBarUIFactory)) is IVsInfoBarUI
}
}

private async Task OnCommandIdeMessageRequestedAsync(object? sender, CommandRequestIdeMessage cr)
private async Task OnAddMenuItemRequestIdeMessageAsync(object? sender, AddMenuItemRequestIdeMessage cr)
{
try
{
Expand All @@ -461,7 +461,7 @@ private async Task OnCommandIdeMessageRequestedAsync(object? sender, CommandRequ
}
catch (Exception e)
{
_debugAction?.Invoke($"Using Command Ide Message Requested fail {e.Message}");
_debugAction?.Invoke($"Using AddMenuItem Ide Message Requested fail {e.Message}");
throw;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Uno.UI.RemoteControl.VS/IDEChannel/IDEChannelClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal class IdeChannelClient

public event AsyncEventHandler<ForceHotReloadIdeMessage>? ForceHotReloadRequested;
public event AsyncEventHandler<NotificationRequestIdeMessage>? OnMessageReceived;
public event AsyncEventHandler<CommandRequestIdeMessage>? OnCommandIdeMessageRequested;
public event AsyncEventHandler<AddMenuItemRequestIdeMessage>? OnAddMenuItemIdeMessageRequested;

public IdeChannelClient(Guid pipeGuid, ILogger logger)
{
Expand Down Expand Up @@ -93,9 +93,9 @@ private void ProcessDevServerMessage(object sender, IdeMessageEnvelope devServer
var process = Task.CompletedTask;
switch (devServerMessage)
{
case CommandRequestIdeMessage cr:
case AddMenuItemRequestIdeMessage cr:
_logger.Debug("Command Ide Message Requested");
process = OnCommandIdeMessageRequested.InvokeAsync(this, cr);
process = OnAddMenuItemIdeMessageRequested.InvokeAsync(this, cr);
break;

case ForceHotReloadIdeMessage forceHotReloadMessage when ForceHotReloadRequested is { } hrRequested:
Expand Down

0 comments on commit 785517a

Please sign in to comment.