Change icon and name
This commit is contained in:
1 parent
81d1ed8379
commit
79c1d88b58
6 files changed
+313
-20
No files matched your search
@@ -0,0 +1,48 @@
|
||||
using System.IO;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace FoxBrowser.Core;
|
||||
|
||||
public sealed class AppSettings
|
||||
{
|
||||
public string AppDisplayName { get; set; } = "FoxBrowser";
|
||||
public string? AppIconPath { get; set; }
|
||||
}
|
||||
|
||||
public static class AppSettingsStore
|
||||
{
|
||||
public static string SettingsPath => Path.Combine(AppContext.BaseDirectory, "settings.json");
|
||||
|
||||
private static readonly JsonSerializerOptions JsonOptions = new()
|
||||
{
|
||||
WriteIndented = true,
|
||||
DefaultIgnoreCondition = JsonIgnoreCondition.Never
|
||||
};
|
||||
|
||||
public static AppSettings Load()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!File.Exists(SettingsPath)) return new AppSettings();
|
||||
var settings = JsonSerializer.Deserialize<AppSettings>(File.ReadAllText(SettingsPath), JsonOptions);
|
||||
if (settings is null) return new AppSettings();
|
||||
if (string.IsNullOrWhiteSpace(settings.AppDisplayName)) settings.AppDisplayName = "FoxBrowser";
|
||||
if (!string.IsNullOrWhiteSpace(settings.AppIconPath) && !File.Exists(settings.AppIconPath))
|
||||
settings.AppIconPath = null;
|
||||
return settings;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return new AppSettings();
|
||||
}
|
||||
}
|
||||
|
||||
public static void Save(AppSettings settings)
|
||||
{
|
||||
Directory.CreateDirectory(AppContext.BaseDirectory);
|
||||
var tempPath = SettingsPath + ".tmp";
|
||||
File.WriteAllText(tempPath, JsonSerializer.Serialize(settings, JsonOptions));
|
||||
File.Move(tempPath, SettingsPath, overwrite: true);
|
||||
}
|
||||
}
|
||||
@@ -13,23 +13,32 @@ public sealed class AudioSessionLabeler : IDisposable
|
||||
{
|
||||
private const uint SnapshotProcess = 0x00000002;
|
||||
private readonly uint _rootProcessId;
|
||||
private readonly string _iconPath;
|
||||
private string _displayName;
|
||||
private string _iconPath;
|
||||
private readonly MMDeviceEnumerator _devices = new();
|
||||
private readonly AudioSessionManager _sessions;
|
||||
private readonly System.Threading.Timer _refreshTimer;
|
||||
private readonly object _gate = new();
|
||||
private bool _disposed;
|
||||
|
||||
public AudioSessionLabeler(uint rootProcessId, string iconPath)
|
||||
public AudioSessionLabeler(uint rootProcessId, string iconPath, string displayName = "FoxBrowser")
|
||||
{
|
||||
_rootProcessId = rootProcessId;
|
||||
_iconPath = iconPath;
|
||||
_displayName = string.IsNullOrWhiteSpace(displayName) ? "FoxBrowser" : displayName;
|
||||
var device = _devices.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
|
||||
_sessions = device.AudioSessionManager;
|
||||
_sessions.OnSessionCreated += OnSessionCreated;
|
||||
_refreshTimer = new System.Threading.Timer(_ => RefreshAndLabel(), null, TimeSpan.Zero, TimeSpan.FromSeconds(2));
|
||||
}
|
||||
|
||||
public void UpdateIdentity(string displayName, string iconPath)
|
||||
{
|
||||
_displayName = string.IsNullOrWhiteSpace(displayName) ? "FoxBrowser" : displayName;
|
||||
_iconPath = iconPath;
|
||||
RefreshAndLabel();
|
||||
}
|
||||
|
||||
private void OnSessionCreated(object sender, IAudioSessionControl nativeSession)
|
||||
{
|
||||
LabelSession(new AudioSessionControl(nativeSession), disposeAfter: true);
|
||||
@@ -64,7 +73,7 @@ public sealed class AudioSessionLabeler : IDisposable
|
||||
{
|
||||
var ownedPids = knownOwnedPids ?? GetFoxBrowserWebViewPids();
|
||||
if (!ownedPids.Contains(session.GetProcessID)) return;
|
||||
session.DisplayName = "FoxBrowser";
|
||||
session.DisplayName = _displayName;
|
||||
if (File.Exists(_iconPath)) session.IconPath = _iconPath;
|
||||
}
|
||||
catch { /* A session may close between PID lookup and property update. */ }
|
||||
|
||||
Reference in new issue
Block a user