diff --git a/OpenNest.Desktop.Tests/NestTabsTests.cs b/OpenNest.Desktop.Tests/NestTabsTests.cs new file mode 100644 index 00000000..e67385ff --- /dev/null +++ b/OpenNest.Desktop.Tests/NestTabsTests.cs @@ -0,0 +1,148 @@ +namespace OpenNest.Desktop.Tests; + +public class NestTabsTests +{ + private readonly List loaded = new(); + + private Document Load(string path) + { + loaded.Add(path); + return new Document { Nest = new Nest { Name = Path.GetFileNameWithoutExtension(path) } }; + } + + private static string FullPath(string name) => Path.GetFullPath(Path.Combine("nests", name)); + + [Fact] + public void Empty_HasNoActiveTab() + { + var tabs = new NestTabs(); + + Assert.Equal(-1, tabs.ActiveIndex); + Assert.Null(tabs.Active); + Assert.False(tabs.AnyModified); + } + + [Fact] + public void Open_AddsATabAfterTheOthersAndActivatesIt() + { + var tabs = new NestTabs(); + + tabs.Open(FullPath("a.nest"), Load); + var b = tabs.Open(FullPath("b.nest"), Load); + + Assert.Equal(["a", "b"], tabs.Tabs.Select(t => t.Name)); + Assert.Same(b, tabs.Active); + Assert.Equal(FullPath("b.nest"), b.Path); + } + + [Fact] + public void Open_AFileAlreadyOpen_ActivatesItsTabWithoutLoadingAgain() + { + var tabs = new NestTabs(); + var a = tabs.Open(FullPath("a.nest"), Load); + tabs.Open(FullPath("b.nest"), Load); + + var again = tabs.Open(Path.Combine("nests", "..", "nests", "a.nest"), Load); + + Assert.Same(a, again); + Assert.Equal(0, tabs.ActiveIndex); + Assert.Equal(2, tabs.Tabs.Count); + Assert.Equal(2, loaded.Count); + } + + [Fact] + public void Open_ALoadThatFails_AddsNoTabAndKeepsTheActiveOne() + { + var tabs = new NestTabs(); + var a = tabs.Open(FullPath("a.nest"), Load); + + Assert.Throws(() => + tabs.Open(FullPath("broken.nest"), _ => throw new IOException("bad file"))); + + Assert.Single(tabs.Tabs); + Assert.Same(a, tabs.Active); + } + + [Fact] + public void Close_TheActiveTab_ActivatesTheOneToItsRight() + { + var tabs = Three(out var a, out var b, out var c); + tabs.Activate(1); + + tabs.Close(b); + + Assert.Same(c, tabs.Active); + Assert.Equal([a, c], tabs.Tabs); + } + + [Fact] + public void Close_TheLastActiveTab_ActivatesTheOneToItsLeft() + { + var tabs = Three(out _, out var b, out var c); + + tabs.Close(c); + + Assert.Same(b, tabs.Active); + } + + [Fact] + public void Close_ATabLeftOfTheActiveOne_KeepsTheSameNestActive() + { + var tabs = Three(out var a, out _, out var c); + + tabs.Close(a); + + Assert.Same(c, tabs.Active); + Assert.Equal(1, tabs.ActiveIndex); + } + + [Fact] + public void Close_TheOnlyTab_LeavesNoneActive() + { + var tabs = new NestTabs(); + var a = tabs.Open(FullPath("a.nest"), Load); + + tabs.Close(a); + + Assert.Empty(tabs.Tabs); + Assert.Null(tabs.Active); + } + + [Fact] + public void Modified_IsPerTabAndSavedClearsIt() + { + var tabs = Three(out var a, out var b, out _); + + b.MarkModified(); + + Assert.True(tabs.AnyModified); + Assert.False(a.Modified); + b.MarkSaved(); + Assert.False(tabs.AnyModified); + } + + [Fact] + public void Changed_IsRaisedForOpenActivateAndClose_ButNotReactivation() + { + var tabs = new NestTabs(); + var count = 0; + tabs.Changed += (_, _) => count++; + + var a = tabs.Open(FullPath("a.nest"), Load); + tabs.Open(FullPath("b.nest"), Load); + tabs.Activate(0); + tabs.Activate(0); + tabs.Close(a); + + Assert.Equal(4, count); + } + + private NestTabs Three(out NestTab a, out NestTab b, out NestTab c) + { + var tabs = new NestTabs(); + a = tabs.Open(FullPath("a.nest"), Load); + b = tabs.Open(FullPath("b.nest"), Load); + c = tabs.Open(FullPath("c.nest"), Load); + return tabs; + } +} diff --git a/OpenNest.Desktop/NestTabs.cs b/OpenNest.Desktop/NestTabs.cs new file mode 100644 index 00000000..a4395f00 --- /dev/null +++ b/OpenNest.Desktop/NestTabs.cs @@ -0,0 +1,113 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; + +namespace OpenNest.Desktop +{ + /// An open nest: its document, its file and whether it has unsaved changes. + public sealed class NestTab + { + internal NestTab(Document document, string path) + { + Document = document; + Path = path; + } + + public Document Document { get; } + + /// The full path of the file it was opened from, else null. + public string Path { get; } + + /// True after a change this session that is not saved. + public bool Modified { get; private set; } + + /// The nest's name, else the file name. + public string Name => !string.IsNullOrWhiteSpace(Document.Name) + ? Document.Name + : System.IO.Path.GetFileNameWithoutExtension(Path ?? string.Empty); + + public void MarkModified() => Modified = true; + + public void MarkSaved() => Modified = false; + } + + /// + /// The nests open in the desktop app, one tab each, and which one is active. Opening a file + /// that is already open activates its tab; closing the active tab activates the one to its + /// right, else the one to its left. + /// + public sealed class NestTabs + { + private readonly List tabs = new(); + + /// Raised after a tab is added, closed or activated. + public event EventHandler Changed; + + public IReadOnlyList Tabs => tabs; + + /// The active tab's index, or -1 when no nest is open. + public int ActiveIndex { get; private set; } = -1; + + public NestTab Active => ActiveIndex >= 0 ? tabs[ActiveIndex] : null; + + /// True when any open nest has unsaved changes. + public bool AnyModified => tabs.Any(t => t.Modified); + + /// + /// Activates the tab already showing , or loads it with + /// into a new tab after the others. A load that throws adds no tab. + /// + public NestTab Open(string path, Func load) + { + ArgumentNullException.ThrowIfNull(path); + ArgumentNullException.ThrowIfNull(load); + var fullPath = System.IO.Path.GetFullPath(path); + var existing = tabs.FindIndex(t => t.Path != null && SamePath(t.Path, fullPath)); + if (existing >= 0) + { + Activate(existing); + return tabs[existing]; + } + + var tab = new NestTab(load(fullPath), fullPath); + tabs.Add(tab); + ActiveIndex = tabs.Count - 1; + Changed?.Invoke(this, EventArgs.Empty); + return tab; + } + + public void Activate(int index) + { + if (index < 0 || index >= tabs.Count) + throw new ArgumentOutOfRangeException(nameof(index)); + if (index == ActiveIndex) + return; + ActiveIndex = index; + Changed?.Invoke(this, EventArgs.Empty); + } + + /// Removes the tab; the host asks about unsaved changes first. + public void Close(NestTab tab) + { + var index = tabs.IndexOf(tab); + if (index < 0) + return; + + tabs.RemoveAt(index); + if (tabs.Count == 0) + ActiveIndex = -1; + else if (index < ActiveIndex) + ActiveIndex--; + else if (index == ActiveIndex) + ActiveIndex = System.Math.Min(index, tabs.Count - 1); + Changed?.Invoke(this, EventArgs.Empty); + } + + // Windows paths ignore case; Linux and macOS paths do not. + private static bool SamePath(string a, string b) => string.Equals(a, b, + OperatingSystem.IsWindows() + ? StringComparison.OrdinalIgnoreCase + : StringComparison.Ordinal); + } +}