Skip to content
Open
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
8 changes: 5 additions & 3 deletions Sonar.AutoSwitch/Services/AutoSwitchService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using Avalonia.Logging;
using Sonar.AutoSwitch.ViewModels;

namespace Sonar.AutoSwitch.Services;
Expand Down Expand Up @@ -48,11 +50,11 @@ private async void InstanceOnForegroundWindowChanged(object? sender, WindowInfo
autoSwitchProfileViewModels.Concat(AutoSwitchProfilesDatabase.Instance.GithubProfiles);
}

AutoSwitchProfileViewModel? autoSwitchProfileViewModel =
autoSwitchProfileViewModels.FirstOrDefault(p =>
AutoSwitchProfileViewModel? autoSwitchProfileViewModel = autoSwitchProfileViewModels.FirstOrDefault(p =>
(string.IsNullOrEmpty(p.ExeName) ||
string.Equals(p.ExeName, windowExeName, StringComparison.OrdinalIgnoreCase)) &&
(string.IsNullOrEmpty(p.Title) || e.Title.Contains(p.Title, StringComparison.OrdinalIgnoreCase)));
p.MatchTitle(e.Title));

SonarGamingConfiguration? sonarGamingConfiguration = autoSwitchProfileViewModel?.SonarGamingConfiguration;
sonarGamingConfiguration ??= _homeViewModel.DefaultSonarGamingConfiguration;
if (string.IsNullOrEmpty(sonarGamingConfiguration.Id) ||
Expand Down
7 changes: 5 additions & 2 deletions Sonar.AutoSwitch/Services/Win32/Win32WindowEventManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,15 @@ private void WindowEventCallback(nint hWinEventHook, uint eventType, nint hwnd,
int idChild, uint dwEventThread, uint dwmsEventTime)
{
string windowTitle = GetWindowTitle(hwnd);
string? exeName = null;
if (GetWindowThreadProcessId(hwnd, out var pid) == 0)
{
OnForegroundWindowChanged(new WindowInfo(exeName, windowTitle));
return;
string? exeName = null;
}
try
{
using var processById = Process.GetProcessById((int) pid);
using var processById = Process.GetProcessById((int)pid);
string? fileName = processById.MainModule?.FileName;
if (string.IsNullOrEmpty(fileName))
return;
Expand Down
11 changes: 10 additions & 1 deletion Sonar.AutoSwitch/ViewModels/AutoSwitchProfileViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using Sonar.AutoSwitch.Services;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
namespace Sonar.AutoSwitch.ViewModels;

public class AutoSwitchProfileViewModel : ViewModelBase
{
private static readonly RegexOptions REGEX_OPTIONS = RegexOptions.IgnoreCase;
private string _exeName = "MyGame";
private string _title = "";
private Regex _titleRegex = new Regex("", REGEX_OPTIONS);
private SonarGamingConfiguration _sonarGamingConfiguration = new(null, "Unset");

public string Title
Expand All @@ -14,12 +17,18 @@ public string Title
set
{
if (value == _title) return;
_title = value;
_title = value ??= "";
_titleRegex = new Regex(_title, REGEX_OPTIONS);
OnPropertyChanged();
OnPropertyChanged(nameof(DisplayName)); // Notify that DisplayName has changed
}
}

public bool MatchTitle(string title)
{
return _titleRegex.IsMatch(title);
}

public string ExeName
{
get => _exeName;
Expand Down