feat(ui): Display tab + settings.json persistence — first non-keybind tab lands
Phase L.0 (cont.) — first concrete tab on the new Settings shell, in the Easy-wins build order agreed in the brainstorm (Display → Audio → Gameplay → Chat → Character). DisplaySettings (immutable record): Resolution / Fullscreen / VSync / FieldOfView (30-120°) / Gamma (0.5-2.0) / ShowFps. Six common 16:9 resolutions in the dropdown. Defaults: 1920×1080, windowed, vsync on, 75° FOV, gamma 1.0, FPS off — matches the brainstorm UX agreement. SettingsStore: JSON persistence at %LOCALAPPDATA%\acdream\settings.json (coexists with keybinds.json — own load/save path stays put, no migration needed). LoadDisplay falls back per-field when keys are missing (partial-file tolerant) and falls back to defaults when the file is corrupt or the JSON is unparseable. SaveDisplay round-trips preserved — unknown top-level keys (e.g. an `audio` section written by a future client) are kept on save so older builds don't silently drop newer-tab data. SettingsVM gains a parallel display-state machine: persistedDisplay + draftDisplay, SetDisplay mutator, HasUnsavedChanges checks both keybinds and display deltas, Save/Cancel/ResetAll cover both atomically from the user's POV (one Save commits everything, one Cancel reverts everything). Constructor signature extends with two new params; existing keybinds-only callers updated. SettingsPanel.RenderDisplayTab replaces the L.0-shell placeholder — Combo for resolution, Checkboxes for fullscreen/vsync/show-fps, SliderFloat for FOV + gamma. Live-preview note in the panel body matches the agreed UX: FOV + gamma update visibly while the user drags; resolution / fullscreen / vsync apply on Save (live preview would be too jarring). GameWindow wires SettingsStore into the existing SettingsVM construct site — load on startup, save on each tab Save. Errors print to console and don't crash the panel. 19 new tests: · DisplaySettings record (4) — defaults pinned, value equality, with- expressions, AvailableResolutions sorted ascending · SettingsStore (6) — round trip, missing-file → defaults, corrupt- file → defaults, partial-file → per-field fallback, unknown-key preservation, DefaultPath shape · SettingsVM display (6) — initial draft tracks persisted, SetDisplay marks dirty, Save invokes display callback, Cancel reverts, ResetAllToDefaults covers display, Save-then-Cancel is no-op · SettingsPanel display tab (3) — widgets render only when active, resolution combo uses AvailableResolutions, no Combo emitted on inactive tabs dotnet build green (0 warnings); dotnet test 1,246 / 1,246 green (243 Core.Net + 330 UI.Abstractions + 673 Core). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
7665cdf642
commit
382f0ad3fa
9 changed files with 653 additions and 33 deletions
|
|
@ -0,0 +1,119 @@
|
|||
using System.IO;
|
||||
using AcDream.UI.Abstractions.Panels.Settings;
|
||||
|
||||
namespace AcDream.UI.Abstractions.Tests.Panels.Settings;
|
||||
|
||||
/// <summary>
|
||||
/// L.0: <see cref="SettingsStore"/> reads / writes <c>settings.json</c>.
|
||||
/// Tests use a temp-file path so they don't touch the user's
|
||||
/// %LOCALAPPDATA% file.
|
||||
/// </summary>
|
||||
public sealed class SettingsStoreTests : System.IDisposable
|
||||
{
|
||||
private readonly string _tempPath;
|
||||
|
||||
public SettingsStoreTests()
|
||||
{
|
||||
// Unique per-test file under the system temp dir so parallel test
|
||||
// runners don't clobber each other.
|
||||
_tempPath = Path.Combine(
|
||||
Path.GetTempPath(),
|
||||
$"acdream-settings-test-{System.Guid.NewGuid():N}.json");
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (File.Exists(_tempPath)) File.Delete(_tempPath);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LoadDisplay_returns_defaults_when_file_is_missing()
|
||||
{
|
||||
var store = new SettingsStore(_tempPath);
|
||||
var loaded = store.LoadDisplay();
|
||||
Assert.Equal(DisplaySettings.Default, loaded);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SaveDisplay_then_LoadDisplay_round_trips_all_fields()
|
||||
{
|
||||
var store = new SettingsStore(_tempPath);
|
||||
var original = new DisplaySettings(
|
||||
Resolution: "2560x1440",
|
||||
Fullscreen: true,
|
||||
VSync: false,
|
||||
FieldOfView: 100f,
|
||||
Gamma: 1.4f,
|
||||
ShowFps: true);
|
||||
|
||||
store.SaveDisplay(original);
|
||||
var loaded = store.LoadDisplay();
|
||||
|
||||
Assert.Equal(original, loaded);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LoadDisplay_falls_back_to_defaults_when_file_is_corrupt()
|
||||
{
|
||||
File.WriteAllText(_tempPath, "{ this is not valid json");
|
||||
var store = new SettingsStore(_tempPath);
|
||||
|
||||
var loaded = store.LoadDisplay();
|
||||
|
||||
Assert.Equal(DisplaySettings.Default, loaded);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LoadDisplay_falls_back_per_field_when_keys_missing()
|
||||
{
|
||||
// Partial file — only resolution set; everything else should
|
||||
// pick up DisplaySettings.Default values.
|
||||
File.WriteAllText(_tempPath, """
|
||||
{
|
||||
"version": 1,
|
||||
"display": { "resolution": "1366x768" }
|
||||
}
|
||||
""");
|
||||
var store = new SettingsStore(_tempPath);
|
||||
|
||||
var loaded = store.LoadDisplay();
|
||||
|
||||
Assert.Equal("1366x768", loaded.Resolution);
|
||||
Assert.Equal(DisplaySettings.Default.Fullscreen, loaded.Fullscreen);
|
||||
Assert.Equal(DisplaySettings.Default.VSync, loaded.VSync);
|
||||
Assert.Equal(DisplaySettings.Default.FieldOfView, loaded.FieldOfView);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SaveDisplay_preserves_unknown_top_level_keys()
|
||||
{
|
||||
// Forward-compat: a newer client may have written sections we
|
||||
// don't know about (audio, gameplay). Saving display must not
|
||||
// delete those, otherwise running an older client would silently
|
||||
// drop the user's other-tab preferences.
|
||||
File.WriteAllText(_tempPath, """
|
||||
{
|
||||
"version": 1,
|
||||
"display": { "resolution": "1280x720" },
|
||||
"audio": { "master": 0.5, "music": 0.7 }
|
||||
}
|
||||
""");
|
||||
var store = new SettingsStore(_tempPath);
|
||||
|
||||
store.SaveDisplay(DisplaySettings.Default with { Resolution = "1920x1080" });
|
||||
|
||||
var raw = File.ReadAllText(_tempPath);
|
||||
Assert.Contains("\"audio\"", raw);
|
||||
Assert.Contains("\"master\"", raw);
|
||||
Assert.Contains("0.5", raw);
|
||||
// And the new display value did get written.
|
||||
Assert.Contains("1920x1080", raw);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DefaultPath_is_under_LocalAppData_acdream()
|
||||
{
|
||||
var path = SettingsStore.DefaultPath();
|
||||
Assert.EndsWith("acdream" + Path.DirectorySeparatorChar + "settings.json", path);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue