Skip to content
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
13 changes: 9 additions & 4 deletions src/GameLibrary.Contracts/Localization/ui.json
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,10 @@
"Minimize to tray when closing",
"閉じるときにトレイへ最小化"
],
"关闭后程序继续在后台运行,可从托盘图标重新打开。": [
"關閉后程式繼續在后臺運行,可從系統匣圖標重新打開。",
"Keep running in the background. Reopen from the tray icon.",
"バックグラウンドで動作を続けます。トレイから再表示できます。"
"关闭后程序继续在后台运行。单击通知区域的 GameLibrary 图标即可重新打开;右键可选择「显示 GameLibrary」或「退出」。再次启动本程序也会打开正在运行的窗口。": [
"關閉后程式繼續在后臺運行。單擊通知區域的 GameLibrary 圖標即可重新打開;右鍵可選擇「顯示 GameLibrary」或「結束」。再次啟動本程式也會打開正在運行的窗口。",
"Keep running in the background. Click the GameLibrary icon in the notification area to reopen it; right-click and choose Show GameLibrary or Quit. Starting the app again also opens the running window.",
"閉じたあともバックグラウンドで動作します。通知領域の GameLibrary アイコンをクリックすると再表示できます。右クリックで「GameLibrary を表示」または「終了」。もう一度起動しても、実行中のウィンドウが開きます。"
],
"后台核对周期(分钟)": [
"后臺核對周期(分鐘)",
Expand Down Expand Up @@ -377,6 +377,11 @@
"Loading…",
"読み込み中…"
],
"正在加载游戏库…": [
"正在載入游戲庫…",
"Loading the library…",
"ライブラリを読み込み中…"
],
"正在读取设置…": [
"正在讀取設定…",
"Loading settings…",
Expand Down
2 changes: 2 additions & 0 deletions src/GameLibrary.Desktop/MainWindow.Library.cs
Original file line number Diff line number Diff line change
Expand Up @@ -993,7 +993,9 @@ private async void OnSortSelectorChanged(object sender, SelectionChangedEventArg
}

_sortMode = newSort;
_persistSort = true;
_selectedGameIds.Clear();
SaveUiPrefs();
await RefreshAsync();
}

Expand Down
58 changes: 56 additions & 2 deletions src/GameLibrary.Desktop/MainWindow.Lifetime.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Diagnostics;
using System.IO;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
Expand Down Expand Up @@ -42,7 +44,21 @@ private void SaveUiPrefs()
{
var path = UiPrefsPath;
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
File.WriteAllText(path, JsonSerializer.Serialize(new { leftWidth = LeftColumn.Width.Value }));
// 合并进已有对象。整文件重写会抹掉 Tauri 记下的 sort / layout。
var root = ReadUiPrefsObject(path);
root["leftWidth"] = JsonValue.Create(LeftColumn.Width.Value);
if (_persistSort && IsPersistedSort(_sortMode))
{
root["sort"] = JsonValue.Create(_sortMode);
}

var json = root.ToJsonString(new JsonSerializerOptions
{
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
});
var temporary = path + "." + Guid.NewGuid().ToString("N") + ".tmp";
File.WriteAllText(temporary, json);
File.Move(temporary, path, overwrite: true);
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
Expand All @@ -67,13 +83,51 @@ private void LoadUiPrefs()
{
LeftColumn.Width = new GridLength(parsed);
}

// 文件里没有 sort 时保持构造时的默认值,关窗也不补写,避免把 Tauri 的默认排序改掉。
if (document.RootElement.TryGetProperty("sort", out var sortElement)
&& sortElement.ValueKind == JsonValueKind.String
&& sortElement.GetString() is string sort
&& IsPersistedSort(sort))
{
_sortMode = sort;
_persistSort = true;
for (var index = 0; index < SortSelector.Items.Count; index++)
{
if (SortSelector.Items[index] is ComboBoxItem item && item.Tag is string tag && tag == sort)
{
SortSelector.SelectedIndex = index;
break;
}
}
}
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or JsonException)
{
// 记忆缺失走默认 290。
// 记忆缺失走默认 290 / 名称 A–Z。
}
}

private static JsonObject ReadUiPrefsObject(string path)
{
if (!File.Exists(path))
{
return new JsonObject();
}

try
{
return JsonNode.Parse(File.ReadAllText(path)) as JsonObject ?? new JsonObject();
}
catch (JsonException)
{
return new JsonObject();
}
}

private static bool IsPersistedSort(string? sort) =>
sort is "title-asc" or "title-desc" or "updated-desc" or "accepted-desc";

private static string UiPrefsPath => Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"GameLibrary", "ui.json");
Expand Down
3 changes: 3 additions & 0 deletions src/GameLibrary.Desktop/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ private bool TryGetSetting(string name, out JsonElement value)
private string? _preferredGameId;
private bool _renderingSidebar;
private string _sortMode = "title-asc";
/// <summary>文件里已经有 sort,或用户改过排序。否则关窗不写 sort,免得覆盖 Tauri 的默认值。</summary>
private bool _persistSort;
private bool _updatingTagFilter;

private void RefreshLocalizedSelectors()
Expand Down Expand Up @@ -163,6 +165,7 @@ public MainWindow()
Loaded += (_, _) => EnsureTrayIconVisible();
Loaded += async (_, _) => await ConnectAsync();
Closing += SaveSplitterOnClose;
LoadUiPrefs();
}

}
Loading
Loading