Skip to content

Commit

Permalink
Merge pull request #21 from LittleVaaty/18-retrieve-desktop-info
Browse files Browse the repository at this point in the history
Add get-name and total commands + --on support name or index
  • Loading branch information
LittleVaaty authored Jul 30, 2024
2 parents c8a7dc7 + 3836000 commit 9fc5569
Show file tree
Hide file tree
Showing 29 changed files with 261 additions and 96 deletions.
35 changes: 30 additions & 5 deletions VDesk.Generator/VirtualDesktopProviderGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)

var compilation = context.CompilationProvider.Combine(provider.Collect());

context.RegisterSourceOutput(compilation, (context, data) =>
context.RegisterSourceOutput(compilation, (sourceProductionContext, data) =>
{
foreach (var syntax in data.Right)
{
Expand All @@ -32,20 +32,25 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
var hasMoveToDesktopMethod = syntax.Syntax.Members.Any(m => m is MethodDeclarationSyntax { Identifier.Text: "MoveToDesktop" });
var hasSwitchToDesktopMethod = syntax.Syntax.Members.Any(m => m is MethodDeclarationSyntax { Identifier.Text: "Switch" });
var hasSetDesktopNameMethod = syntax.Syntax.Members.Any(m => m is MethodDeclarationSyntax { Identifier.Text: "SetDesktopName" });
var hasGetDesktopNameMethod = syntax.Syntax.Members.Any(m => m is MethodDeclarationSyntax { Identifier.Text: "GetDesktopName" });
var theCode = GetVirtualDesktopProviderCode(syntax.Symbol.ContainingNamespace.ToDisplayString(), hasCreateDesktopMethod, hasGetDesktopMethod, hasMoveToDesktopMethod, hasSwitchToDesktopMethod, hasSetDesktopNameMethod);
var theCode = GetVirtualDesktopProviderCode(syntax.Symbol.ContainingNamespace.ToDisplayString(), hasCreateDesktopMethod, hasGetDesktopMethod, hasMoveToDesktopMethod, hasSwitchToDesktopMethod, hasSetDesktopNameMethod, hasGetDesktopNameMethod);
context.AddSource($"{syntax.Symbol.ToDisplayString()}.cs", theCode);
sourceProductionContext.AddSource($"{syntax.Symbol.ToDisplayString()}.cs", theCode);
}
});
}

private static string GetVirtualDesktopProviderCode(string fullNamespace, bool hasCreateDesktopMethod, bool hasGetDesktopMethod, bool hasMoveToDesktopMethod, bool hasSwitchToDesktopMethod, bool hasSetDesktopNameMethod)
private static string GetVirtualDesktopProviderCode(string fullNamespace, bool hasCreateDesktopMethod,
bool hasGetDesktopMethod, bool hasMoveToDesktopMethod, bool hasSwitchToDesktopMethod,
bool hasSetDesktopNameMethod, bool hasGetDesktopNameMethod)
{
return $$"""
using FluentResults;
using System.Runtime.InteropServices;
using VDesk.Errors;
using VDesk.Interop.SharedCOM;

namespace {{fullNamespace}};

Expand Down Expand Up @@ -98,18 +103,38 @@ protected static Result<T> CreateInstance<T>(Guid? guidService)
{{(!hasSwitchToDesktopMethod ? GetSwitchToDesktopCode() : string.Empty)}}

{{(!hasSetDesktopNameMethod ? GetSetDesktopNameCode() : string.Empty)}}

{{(!hasGetDesktopNameMethod ? GetGetDesktopNameCode() : string.Empty)}}
}
""";
}

private static string GetGetDesktopNameCode()
{
return """
public string GetDesktopName(Guid virtualDesktopId)
{
if (_knownDesktops.TryGetValue(virtualDesktopId, out var virtualDesktop))
{
var t = _applicationViewCollection.GetViews();
return virtualDesktop.GetName();
}
else
{
throw new KeyNotFoundException($"cannot found virtualdesktop with key {virtualDesktopId}");
}
}
""";
}

private static string GetSetDesktopNameCode()
{
return """
public void SetDesktopName(Guid virtualDesktopId, string name)
{
if (_knownDesktops.TryGetValue(virtualDesktopId, out var virtualDesktop))
{
_virtualDesktopManagerInternal.SetDesktopName(virtualDesktop, name);
_virtualDesktopManagerInternal.SetDesktopName(virtualDesktop, HString.FromString(name));
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions VDesk/Commands/CreateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public class CreateCommand(ILogger<CreateCommand> logger, IVirtualDesktopProvide
[Range(1, 100)]
[Required]
public int Number { get; set; } = 1;
public override int Execute(CommandLineApplication app)

protected override int Execute(CommandLineApplication app)
{
var desktopIds = VirtualDesktopProvider.GetDesktop();

Expand Down
37 changes: 37 additions & 0 deletions VDesk/Commands/GetNameCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.ComponentModel.DataAnnotations;
using McMaster.Extensions.CommandLineUtils;
using Microsoft.Extensions.Logging;
using VDesk.Interop;
using IConsole = VDesk.Utils.IConsole;

namespace VDesk.Commands;

public class GetNameCommand(ILogger<GetNameCommand> logger, IVirtualDesktopProvider virtualDesktopProvider, IConsole console)
: VdeskCommandBase(logger, virtualDesktopProvider)
{
[Option("-o|--on", CommandOptionType.SingleValue, Description = "Desktop on witch the command is run")]
[Range(1, 10)]
public int? DesktopNumber { get; set; }

protected override int Execute(CommandLineApplication app)
{
var desktopIds = VirtualDesktopProvider.GetDesktop();

if (DesktopNumber is not null)
{
var name = VirtualDesktopProvider.GetDesktopName(desktopIds[DesktopNumber.Value - 1]);
name = string.IsNullOrEmpty(name) ? $"Desktop {DesktopNumber}" : name;
console.WriteLine($"The name of desktop {DesktopNumber} is {name}");
return 0;
}

for (var i = 0; i < desktopIds.Count; i++)
{
var name = VirtualDesktopProvider.GetDesktopName(desktopIds[i]);
name = string.IsNullOrEmpty(name) ? $"Desktop {i + 1}" : name;
console.WriteLine($"The name of desktop {i + 1} is {name}");
}

return 0;
}
}
18 changes: 7 additions & 11 deletions VDesk/Commands/MoveCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ public class MoveCommand(ILogger<MoveCommand> logger, IWindowService windowServi
private readonly IProcessService _processService = processService;

[Option("-o|--on", CommandOptionType.SingleValue, Description = "Desktop on witch the command is run")]
[Range(1, 10)]
public int DesktopNumber { get; set; } = 1;
public string DesktopNameOrNumber { get; set; }

[Argument(0, Description = "Process to move")]
[Required]
Expand All @@ -28,7 +27,7 @@ public class MoveCommand(ILogger<MoveCommand> logger, IWindowService windowServi
[Option("--half-split")]
public HalfSplit? HalfSplit { get; set; }

public override int Execute(CommandLineApplication app)
protected override int Execute(CommandLineApplication app)
{
var process = Process.GetProcessesByName(ProcessName).FirstOrDefault();
if (process is null)
Expand All @@ -40,21 +39,18 @@ public override int Execute(CommandLineApplication app)
var hWnd = _processService.GetMainWindowHandle(process);
var desktopIds = VirtualDesktopProvider.GetDesktop();

while (DesktopNumber > desktopIds.Count)
{
desktopIds.Add(VirtualDesktopProvider.CreateDesktop());
}

var desktopId = desktopIds[DesktopNumber - 1];
var desktopId = GetDesktopIdByNameOrIndex(desktopIds, DesktopNameOrNumber);
if (desktopId is null)
return -1;

VirtualDesktopProvider.MoveToDesktop(hWnd, desktopId);
VirtualDesktopProvider.MoveToDesktop(hWnd, desktopId.Value);

_windowService.MoveHalfSplit(hWnd, HalfSplit);

if (NoSwitch.HasValue && NoSwitch.Value)
return 0;

VirtualDesktopProvider.Switch(desktopId);
VirtualDesktopProvider.Switch(desktopId.Value);

return 0;
}
Expand Down
25 changes: 8 additions & 17 deletions VDesk/Commands/RunCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ internal class RunCommand(ILogger<RunCommand> logger,
private readonly IWindowService _windowService = windowService;

[Option("-o|--on", CommandOptionType.SingleValue, Description = "Desktop on witch the command is run")]
[Range(1, 100)]
public int DesktopNumber { get; set; } = 1;

public string DesktopNameOrNumber { get; set; }

[Argument(0, Description = "Command to execute")]
[Required]
public string Command { get; set; }
Expand All @@ -34,32 +33,24 @@ internal class RunCommand(ILogger<RunCommand> logger,
[Option("-w|--waiting", Description = "Time in milisecond to wait after the star of the process before trying to move it.")]
public int? WaitingTime { get; set; }

public override int Execute(CommandLineApplication app)
protected override int Execute(CommandLineApplication app)
{
if (Verbose.HasValue && Verbose.Value)
{
Logger.LogInformation($"Provider: {VirtualDesktopProvider.GetType()}");
}

var desktopIds = VirtualDesktopProvider.GetDesktop();

while (DesktopNumber > desktopIds.Count)
{
desktopIds.Add(VirtualDesktopProvider.CreateDesktop());
}

var desktopId = desktopIds[DesktopNumber - 1];
var desktopId = GetDesktopIdByNameOrIndex(desktopIds, DesktopNameOrNumber);
if (desktopId is null)
return -1;

if (!NoSwitch.HasValue || !NoSwitch.Value)
VirtualDesktopProvider.Switch(desktopId);
VirtualDesktopProvider.Switch(desktopId.Value);

_processService.Start(Command, Arguments ?? string.Empty, out var hWnd);

if (NoSwitch.HasValue && NoSwitch.Value)
{
// For unknown reason, without it the view is not found
Thread.Sleep(WaitingTime ?? 1);
VirtualDesktopProvider.MoveToDesktop(hWnd, desktopId);
VirtualDesktopProvider.MoveToDesktop(hWnd, desktopId.Value);
}

_windowService.MoveHalfSplit(hWnd, HalfSplit);
Expand Down
4 changes: 2 additions & 2 deletions VDesk/Commands/SetNameCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ public class SetNameCommand(ILogger<VdeskCommandBase> logger, IVirtualDesktopPro
public int DesktopNumber { get; set; } = 1;

[Argument(0, "name of the desktop")]
public string DesktopName { get; set; }
public required string DesktopName { get; set; }

public override int Execute(CommandLineApplication app)
protected override int Execute(CommandLineApplication app)
{
var desktops = VirtualDesktopProvider.GetDesktop();

Expand Down
19 changes: 9 additions & 10 deletions VDesk/Commands/SwitchCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,19 @@ namespace VDesk.Commands
[Command(Description = "Switch to a specific virtual desktop")]
internal class SwitchCommand(ILogger<SwitchCommand> logger, IVirtualDesktopProvider virtualDesktopProvider) : VdeskCommandBase(logger, virtualDesktopProvider)
{
[Argument(0, Description = "Number of the virtual desktop to go to")]
[Range(1, 100)]
public int DesktopNumber { get; } = 1;

public override int Execute(CommandLineApplication app)
[Argument(0, Description = "Name or Id of the virtual desktop")]
public string DesktopNameOrNumber { get; }

protected override int Execute(CommandLineApplication app)
{
var desktopIds = VirtualDesktopProvider.GetDesktop();

while (DesktopNumber > desktopIds.Count)
{
desktopIds.Add(VirtualDesktopProvider.CreateDesktop());
}
var desktopId = GetDesktopIdByNameOrIndex(desktopIds, DesktopNameOrNumber);

VirtualDesktopProvider.Switch(desktopIds[DesktopNumber - 1]);
if (desktopId is null)
return -1;

VirtualDesktopProvider.Switch(desktopId.Value);

return 0;
}
Expand Down
19 changes: 19 additions & 0 deletions VDesk/Commands/TotalCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using McMaster.Extensions.CommandLineUtils;
using Microsoft.Extensions.Logging;
using VDesk.Interop;
using IConsole = VDesk.Utils.IConsole;

namespace VDesk.Commands;

[Command(Description = "Get the total number of desktop")]
public class TotalCommand(ILogger<TotalCommand> logger, IVirtualDesktopProvider virtualDesktopProvider, IConsole console)
: VdeskCommandBase(logger, virtualDesktopProvider)
{
protected override int Execute(CommandLineApplication app)
{
var desktopIds = VirtualDesktopProvider.GetDesktop();
console.WriteLine($"Number of desktopIds: {desktopIds.Count}");

return 0;
}
}
8 changes: 4 additions & 4 deletions VDesk/Commands/VdeskCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ namespace VDesk.Commands
{
[Command(Name = "vdesk", FullName = "vdesk", Description = "Manage application accros virtual desktop")]
[VersionOptionFromMember(MemberName = nameof(GetVersion))]
[Subcommand(typeof(CreateCommand), typeof(MoveCommand), typeof(RunCommand), typeof(SwitchCommand), typeof(SetNameCommand))]
[Subcommand(typeof(CreateCommand), typeof(MoveCommand), typeof(RunCommand), typeof(SwitchCommand), typeof(SetNameCommand), typeof(TotalCommand), typeof(GetNameCommand))]
internal class VdeskCommand(ILogger<VdeskCommand> logger, IVirtualDesktopProvider virtualDesktopProvider) : VdeskCommandBase(logger, virtualDesktopProvider)
{
public override int Execute(CommandLineApplication app)
protected override int Execute(CommandLineApplication app)
{
Console.WriteLine("Specify a subcommand");
app.ShowHelp();
return 1;
}

private static string GetVersion()
private static string? GetVersion()
=> typeof(VdeskCommand).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
.InformationalVersion;
?.InformationalVersion;
}
}
38 changes: 27 additions & 11 deletions VDesk/Commands/VdeskCommandBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,52 @@
namespace VDesk.Commands
{
[HelpOption("--help")]
public abstract class VdeskCommandBase(ILogger<VdeskCommandBase> logger, IVirtualDesktopProvider virtualDesktopProvider)
public abstract class VdeskCommandBase(
ILogger<VdeskCommandBase> logger,
IVirtualDesktopProvider virtualDesktopProvider)
{
[Option("-v|--verbose", Description = "user verbose")]
public bool? Verbose { get; set; }

protected readonly ILogger<VdeskCommandBase> Logger = logger;
protected IVirtualDesktopProvider VirtualDesktopProvider = virtualDesktopProvider;

public abstract int Execute(CommandLineApplication app);
protected abstract int Execute(CommandLineApplication app);

// ReSharper disable once UnusedMember.Global
public int OnExecute(CommandLineApplication app)
{
try
{
if (Verbose.HasValue && Verbose.Value)
{
Logger.LogInformation($"OS Build: {Os.Build}");
Logger.LogInformation($"Working Directory: {Directory.GetCurrentDirectory()}");
Logger.LogInformation($"Provider: {VirtualDesktopProvider.GetType()}");
}
if (!Verbose.HasValue || !Verbose.Value) return Execute(app);
Logger.LogInformation($"OS Build: {Os.Build}");
Logger.LogInformation($"Working Directory: {Directory.GetCurrentDirectory()}");
Logger.LogInformation($"Provider: {VirtualDesktopProvider.GetType()}");

return Execute(app);
}
catch (Exception e)
{
if(Verbose.HasValue && Verbose.Value) Logger.LogError(e, $"{e.Message}\n\r \tWindows version: {Os.Build} ");
if (Verbose.HasValue && Verbose.Value)
Logger.LogError(e, $"{e.Message}\n\r \tWindows version: {Os.Build} ");
else Logger.LogError($"{e.Message}\n\r \tWindows version: {Os.Build}");
return 1;
}
}

protected Guid? GetDesktopIdByNameOrIndex(IList<Guid> desktopIds, string desktopNameOrIndex)
{
if (int.TryParse(desktopNameOrIndex, out var virtualDesktopId))
return desktopIds[virtualDesktopId - 1];

for (var i = 0; i < desktopIds.Count; i++)
{
var name = VirtualDesktopProvider.GetDesktopName(desktopIds[i]);
name = string.IsNullOrEmpty(name) ? $"Desktop {i + 1}" : name;
if (name == desktopNameOrIndex)
return desktopIds[i];
}

return null;
}
}
}
5 changes: 5 additions & 0 deletions VDesk/Interop/Build10240_0000/VirtualDesktopProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@ public void SetDesktopName(Guid virtualDesktopId, string name)
{
throw new NotImplementedException();
}

public string GetDesktopName(Guid virtualDesktopId)
{
throw new NotImplementedException();
}
}
5 changes: 5 additions & 0 deletions VDesk/Interop/Build17134_0000/VirtualDesktopProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@ public void SetDesktopName(Guid virtualDesktopId, string name)
{
throw new NotImplementedException();
}

public string GetDesktopName(Guid virtualDesktopId)
{
throw new NotImplementedException();
}
}
Loading

0 comments on commit 9fc5569

Please sign in to comment.