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

fixed #5 コマンドの結果を名前に反映させる #12

Merged
merged 1 commit into from
Mar 20, 2024
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
10 changes: 10 additions & 0 deletions VdLabel/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ class Config
public Color Foreground { get; set; } = Color.WhiteSmoke;
public Color Background { get; set; } = Color.FromArgb(0x0d1117);
public double Duration { get; set; } = 2.5;
public NamePosition NamePosition { get; set; } = NamePosition.Bottom;
public double CommandInterval { get; set; } = 30;

public List<DesktopConfig> DesktopConfigs { get; init; } = [];
}

Expand All @@ -24,6 +27,7 @@ record DesktopConfig
public Guid Id { get; set; }
public bool IsVisibleName { get; set; } = true;
public string? Name { get; set; }
public string? Command { get; set; }
public string? ImagePath { get; set; }
public IReadOnlyList<WindowConfig> TargetWindows { get; init; } = [];
}
Expand All @@ -45,4 +49,10 @@ enum WindowPatternType
{
Wildcard,
Regex,
}

enum NamePosition
{
Top,
Bottom,
}
9 changes: 7 additions & 2 deletions VdLabel/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ partial class MainViewModel : ObservableObject

public ObservableCollection<DesktopConfigViewModel> DesktopConfigs { get; } = [];

public IReadOnlyList<OverlayPosition> Positions { get; } = Enum.GetValues<OverlayPosition>();
public IReadOnlyList<OverlayPosition> OverlayPositions { get; } = Enum.GetValues<OverlayPosition>();
public IReadOnlyList<NamePosition> NamePositions { get; } = Enum.GetValues<NamePosition>();

public MainViewModel(IConfigStore configStore, IContentDialogService dialogService, IVirualDesktopService virualDesktopService)
{
Expand Down Expand Up @@ -146,6 +147,9 @@ partial class DesktopConfigViewModel(DesktopConfig desktopConfig, IVirualDesktop
[NotifyPropertyChangedFor(nameof(Title))]
private string? name = desktopConfig.Name;

[ObservableProperty]
private string? command = desktopConfig.Command;

[ObservableProperty]
[NotifyPropertyChangedFor(nameof(IsVisibleImage))]
[NotifyCanExecuteChangedFor(nameof(RemoveImageCommand))]
Expand Down Expand Up @@ -200,8 +204,9 @@ public DesktopConfig GetSaveConfig()
=> new()
{
Id = this.Id,
Name = this.Name,
IsVisibleName = this.IsVisibleName,
Name = this.Name,
Command = this.Command,
ImagePath = this.ImagePath,
TargetWindows = this.TargetWindows.ToArray(),
};
Expand Down
301 changes: 168 additions & 133 deletions VdLabel/MainWindow.xaml

Large diffs are not rendered by default.

38 changes: 38 additions & 0 deletions VdLabel/NameCommandService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Cysharp.Diagnostics;
using Microsoft.Extensions.Hosting;

namespace VdLabel;

class NameCommandService(IConfigStore configStore, IVirualDesktopService virualDesktopService) : BackgroundService
{
private readonly IConfigStore configStore = configStore;
private readonly IVirualDesktopService virualDesktopService = virualDesktopService;

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
PeriodicTimer? timer = null;
while (!stoppingToken.IsCancellationRequested)
{
var config = await this.configStore.Load();
stoppingToken.ThrowIfCancellationRequested();
var span = TimeSpan.FromSeconds(config.CommandInterval);
timer ??= new PeriodicTimer(span);
if (timer.Period != span)
{
timer.Period = span;
}
foreach (var desktopConfig in config.DesktopConfigs)
{
if (desktopConfig.Command is not { Length: > 0 })
{
continue;
}
var lines = await ProcessX.StartAsync(desktopConfig.Command).ToTask(stoppingToken);
stoppingToken.ThrowIfCancellationRequested();
this.virualDesktopService.SetName(desktopConfig.Id, string.Join(Environment.NewLine, lines));
}
await timer.WaitForNextTickAsync(stoppingToken);
}
timer?.Dispose();
}
}
16 changes: 16 additions & 0 deletions VdLabel/OverlayViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using CommunityToolkit.Mvvm.ComponentModel;
using System.Drawing;
using System.Windows.Controls;

namespace VdLabel;

Expand All @@ -14,6 +15,9 @@ partial class OverlayViewModel : ObservableObject, IDisposable
[ObservableProperty]
private bool visible;

[ObservableProperty]
private Dock position;

[ObservableProperty]
private string name;

Expand Down Expand Up @@ -48,6 +52,12 @@ public OverlayViewModel(Guid id, string name, IConfigStore configStore)
this.overlaySize = config.OverlaySize;
this.foreground = config.Foreground;
this.duration = config.Duration;
this.position = config.NamePosition switch
{
NamePosition.Top => Dock.Top,
NamePosition.Bottom => Dock.Bottom,
_ => throw new NotImplementedException(),
};
var c = config.DesktopConfigs.FirstOrDefault(c => c.Id == this.id);
this.imagePath = c?.ImagePath;
this.isVisibleName = c?.IsVisibleName ?? true;
Expand All @@ -62,6 +72,12 @@ private async void ConfigStore_Saved(object? sender, EventArgs e)
this.Foreground = config.Foreground;
this.Background = config.Background;
this.duration = config.Duration;
this.Position = config.NamePosition switch
{
NamePosition.Top => Dock.Top,
NamePosition.Bottom => Dock.Bottom,
_ => throw new NotImplementedException(),
};
var c = config.DesktopConfigs.FirstOrDefault(c => c.Id == this.id);
if (c?.Name is not null)
{
Expand Down
13 changes: 7 additions & 6 deletions VdLabel/OverlayWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,18 @@
</Border.Effect>
</Border>
<Border Padding="48">
<StackPanel>
<ui:Image
CornerRadius="8"
Source="{Binding ImagePath}"
Visibility="{Binding IsVisibleImage, Converter={StaticResource b2vConv}}" />
<DockPanel>
<TextBlock
DockPanel.Dock="{Binding Position, Mode=OneWay}"
FontSize="{Binding FontSize}"
Foreground="{Binding Foreground, Converter={x:Static local:SystemColorToSolidBrushConverter.Default}}"
Text="{Binding Name}"
Visibility="{Binding IsVisibleName, Converter={StaticResource b2vConv}}" />
</StackPanel>
<ui:Image
CornerRadius="8"
Source="{Binding ImagePath}"
Visibility="{Binding IsVisibleImage, Converter={StaticResource b2vConv}}" />
</DockPanel>
</Border>
</Grid>
</Window>
1 change: 1 addition & 0 deletions VdLabel/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
builder.Services
.AddHostedService(sp => sp.GetRequiredService<IVirualDesktopService>())
.AddHostedService<WindowMonitor>()
.AddHostedService<NameCommandService>()
.AddSingleton<IVirualDesktopService, VirtualDesktopService>()
.AddSingleton<IConfigStore, ConfigStore>()
.AddSingleton<IContentDialogService, ContentDialogService>()
Expand Down
1 change: 1 addition & 0 deletions VdLabel/VdLabel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<ItemGroup>
<PackageReference Include="Kamishibai.Hosting" Version="2.6.0" />
<PackageReference Include="PInvoke.User32" Version="0.7.124" />
<PackageReference Include="ProcessX" Version="1.5.5" />
<PackageReference Include="Slions.VirtualDesktop.WPF" Version="6.6.0" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
<PackageReference Include="System.Management" Version="8.0.0" />
Expand Down
35 changes: 13 additions & 22 deletions VdLabel/VirtualDesktopService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public async Task StartAsync(CancellationToken cancellationToken)
VirtualDesktop.CurrentChanged += VirtualDesktop_CurrentChanged;
VirtualDesktop.Destroyed += VirtualDesktop_Destroyed;
VirtualDesktop.Created += VirtualDesktop_Created;
VirtualDesktop.Renamed += VirtualDesktop_Renamed;
VirtualDesktop.Moved += VirtualDesktop_Moved;
}

Expand Down Expand Up @@ -92,32 +91,11 @@ private void VirtualDesktop_Created(object? sender, VirtualDesktop e)
await this.configStore.Save(config);
});

private void VirtualDesktop_Renamed(object? sender, VirtualDesktopRenamedEventArgs e)
=> this.app.Dispatcher.Invoke(async () =>
{
var config = await this.configStore.Load();
var c = config.DesktopConfigs.First(c => c.Id == e.Desktop.Id);
if (!string.IsNullOrEmpty(e.Name))
{
c.Name = e.Name;
}
else
{
c.Name = null;
}
if (this.windows.TryGetValue(c.Id, out var pair))
{
pair.vm.Name = c.Name ?? $"Desktop {config.DesktopConfigs.IndexOf(c)}";
}
await this.configStore.Save(config);
});

public Task StopAsync(CancellationToken cancellationToken)
{
VirtualDesktop.CurrentChanged -= VirtualDesktop_CurrentChanged;
VirtualDesktop.Destroyed -= VirtualDesktop_Destroyed;
VirtualDesktop.Created -= VirtualDesktop_Created;
VirtualDesktop.Renamed -= VirtualDesktop_Renamed;
VirtualDesktop.Moved -= VirtualDesktop_Moved;
return Task.CompletedTask;
}
Expand Down Expand Up @@ -175,6 +153,18 @@ public async ValueTask ReloadDesktops()
}
await this.configStore.Save(config);
}

public void SetName(Guid id, string v)
{
if (this.windows.TryGetValue(id, out var pair))
{
pair.vm.Name = v;
}
if (this.IsSupportedName && VirtualDesktop.FromId(id) is { } desktop)
{
desktop.Name = v.ReplaceLineEndings(string.Empty);
}
}
}

public interface IVirualDesktopService : IHostedService
Expand All @@ -183,6 +173,7 @@ public interface IVirualDesktopService : IHostedService
bool IsSupportedName { get; }
void Pin(Window window);
ValueTask ReloadDesktops();
void SetName(Guid id, string v);
}

public class DesktopChangedEventArgs(Guid desktopId) : EventArgs
Expand Down