Skip to content

Commit

Permalink
Workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
flyingpie committed Mar 17, 2024
1 parent 9e09818 commit 6a39b94
Show file tree
Hide file tree
Showing 38 changed files with 713 additions and 626 deletions.
32 changes: 32 additions & 0 deletions src/10-Core/Wtq.Core/Configuration/AttachMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace Wtq.Core.Configuration;

/// <summary>
/// How WTQ should try to attach to a process.
/// </summary>
public enum AttachMode
{
/// <summary>
/// Used to detect serialization issues.
/// </summary>
None = 0,

/// <summary>
/// Always create a new process.
/// </summary>
Start,

/// <summary>
/// Only look for existing process.
/// </summary>
Find,

/// <summary>
/// Look for existing process, create one of one does not exist yet.
/// </summary>
FindOrStart,

/// <summary>
/// Attach to the foreground process when pressing the assigned hot key.
/// </summary>
Manual,
}
10 changes: 0 additions & 10 deletions src/10-Core/Wtq.Core/Configuration/CreateProcessOptions.cs

This file was deleted.

23 changes: 0 additions & 23 deletions src/10-Core/Wtq.Core/Configuration/FindProcessOptions.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/10-Core/Wtq.Core/Configuration/HotkeyOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Wtq.Core.Configuration;

public class HotkeyOptions
public class HotKeyOptions
{
public WtqKeys Key { get; set; }

Expand Down
83 changes: 75 additions & 8 deletions src/10-Core/Wtq.Core/Configuration/WtqAppOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@ namespace Wtq.Core.Configuration;

public class WtqAppOptions
{
public AttachMode? AttachMode { get; set; }

// TODO: Use dict key?
public string Name { get; set; }

public IEnumerable<HotkeyOptions> Hotkeys { get; set; } = Array.Empty<HotkeyOptions>();
public IEnumerable<HotKeyOptions> HotKeys { get; set; } = [];

[NotNull]
[Required]
public FindProcessOptions? FindExisting { get; set; }
public string? FileName { get; set; }

[NotNull]
[Required]
public CreateProcessOptions? StartNew { get; set; }
public string? ProcessName { get; set; }

public string? Arguments { get; set; }

/// <summary>
/// <para>If "PreferMonitor" is set to "AtIndex", this setting determines what monitor to choose.</para>
Expand All @@ -30,13 +32,78 @@ public class WtqAppOptions
/// </summary>
public PreferMonitor? PreferMonitor { get; set; }

public bool HasHotkey(WtqKeys key, WtqKeyModifiers modifiers)
public bool Filter(Process process, bool isStartedByWtq)
{
ArgumentNullException.ThrowIfNull(process);

if (process.MainWindowHandle == nint.Zero)
{
return false;
}

// TODO: Make some notes about webbrowsers being a pain with starting a new process.
try
{
if (process.MainModule == null)
{
return false;
}

if (process.MainWindowHandle == nint.Zero)
{
return false;
}

// TODO: Handle extensions either or not being there.
var fn1 = Path.GetFileNameWithoutExtension(ProcessName ?? FileName);
var fn2 = Path.GetFileNameWithoutExtension(process.MainModule.FileName);

if (fn2.Contains("windowsterminal", StringComparison.OrdinalIgnoreCase))
{
var dbg = 2;
}

if (!fn1.Equals(fn2, StringComparison.OrdinalIgnoreCase))
{
return false;
}

if (isStartedByWtq)
{
if (!process.StartInfo.Environment.TryGetValue("WTQ_START", out var val))
{
return false;
}

if (string.IsNullOrWhiteSpace(val))
{
return false;
}

if (!val.Equals(Name, StringComparison.OrdinalIgnoreCase))
{
return false;
}
}

return true;
}
catch
{
// TODO: Remove try/catch, use safe property access methods on "Process" instead.
}

return false;
}

public bool HasHotKey(WtqKeys key, WtqKeyModifiers modifiers)
{
return Hotkeys.Any(hk => hk.Key == key && hk.Modifiers == modifiers);
return HotKeys.Any(hk => hk.Key == key && hk.Modifiers == modifiers);
}

public override string ToString()
{
return FindExisting?.ProcessName ?? StartNew?.FileName ?? "<unknown>";
//return FindExisting?.ProcessName ?? StartNew?.FileName ?? "<unknown>";
return Name;
}
}
4 changes: 3 additions & 1 deletion src/10-Core/Wtq.Core/Configuration/WtqOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ public sealed class WtqOptions
[Required]
public IEnumerable<WtqAppOptions> Apps { get; set; } = [];

public AttachMode AttachMode { get; set; } = AttachMode.FindOrStart;

[Required]
public IEnumerable<HotkeyOptions> Hotkeys { get; set; } = [];
public IEnumerable<HotKeyOptions> HotKeys { get; set; } = [];

/// <summary>
/// <para>Gets or sets if "PreferMonitor" is set to "AtIndex", this setting determines what monitor to choose.</para>
Expand Down
2 changes: 1 addition & 1 deletion src/10-Core/Wtq.Core/Data/HotkeyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Wtq.Core.Data;

public sealed class HotkeyInfo
public sealed class HotKeyInfo
{
public WtqKeys Key { get; set; }

Expand Down
4 changes: 2 additions & 2 deletions src/10-Core/Wtq.Core/Data/WtqActionType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public enum WtqActionType
{
ToggleApp,

HotkeyPressed,
HotKeyPressed,

RegisterHotkeys,
RegisterHotKeys,
}
2 changes: 2 additions & 0 deletions src/10-Core/Wtq.Core/Data/WtqRect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,6 @@ public override readonly int GetHashCode()
{
return HashCode.Combine(Height, Width, X, Y);
}

public override readonly string ToString() => $"X:{X}, Y:{Y}, Width:{Width}, Height:{Height}";
}
2 changes: 1 addition & 1 deletion src/10-Core/Wtq.Core/Events/WtqHotkeyPressedEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Wtq.Core.Events;

public sealed class WtqHotkeyPressedEvent : IWtqEvent
public sealed class WtqHotKeyPressedEvent : IWtqEvent
{
public WtqKeys Key { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion src/10-Core/Wtq.Core/Events/WtqRegisterHotkeyEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Wtq.Core.Events;

public sealed class WtqRegisterHotkeyEvent : IWtqEvent
public sealed class WtqRegisterHotKeyEvent : IWtqEvent
{
public WtqActionType ActionType { get; set; }

Expand Down
2 changes: 2 additions & 0 deletions src/10-Core/Wtq.Core/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
global using System.ComponentModel.DataAnnotations;
global using System.Diagnostics;
global using System.Diagnostics.CodeAnalysis;
global using System.IO;
global using System.Linq;
global using System.Threading;
global using System.Threading.Tasks;
global using Wtq.Utils;
8 changes: 8 additions & 0 deletions src/10-Core/Wtq.Core/Services/IWtqAppFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Wtq.Core.Configuration;

namespace Wtq.Services;

public interface IWtqAppFactory
{
WtqApp Create(WtqAppOptions app);
}
2 changes: 1 addition & 1 deletion src/10-Core/Wtq.Core/Services/IWtqAppRepo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Wtq.Core.Services;

public interface IWtqAppRepo : IDisposable
public interface IWtqAppRepo : IAsyncDisposable
{
IReadOnlyCollection<WtqApp> Apps { get; }

Expand Down
3 changes: 3 additions & 0 deletions src/10-Core/Wtq.Core/Services/IWtqBus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ public interface IWtqBus
{
void On(Func<IWtqEvent, bool> predicate, Func<IWtqEvent, Task> onEvent);

void On<TEvent>(Func<TEvent, Task> onEvent)
where TEvent : IWtqEvent;

void Publish(IWtqEvent eventType);
}
4 changes: 2 additions & 2 deletions src/10-Core/Wtq.Core/Services/IWtqHotkeyService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Wtq.Core.Service;

public interface IWtqHotkeyService
public interface IWtqHotKeyService
{
// void OnHotkey(Func<HotkeyInfo, Task> onHotkey);
// void OnHotKey(Func<HotKeyInfo, Task> onHotKey);
}
Loading

0 comments on commit 6a39b94

Please sign in to comment.