mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-11-20 04:28:53 -05:00
feat(desktop): track the open nests as tabs
NestTabs holds the nests open in the desktop app and which one is shown. Opening a file that is already open switches to its tab instead of loading it again, and a file that fails to load adds no tab. Closing the shown tab shows the one to its right, else the one to its left. Each tab records whether it has unsaved changes, so the host can ask before closing it or exiting.
This commit is contained in:
1 parent
0d7207e58e
commit
c32f3e5df3
2 files changed
+261
No files matched your search
@@ -0,0 +1,148 @@
|
||||
namespace OpenNest.Desktop.Tests;
|
||||
|
||||
public class NestTabsTests
|
||||
{
|
||||
private readonly List<string> 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<IOException>(() =>
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace OpenNest.Desktop
|
||||
{
|
||||
/// <summary>An open nest: its document, its file and whether it has unsaved changes.</summary>
|
||||
public sealed class NestTab
|
||||
{
|
||||
internal NestTab(Document document, string path)
|
||||
{
|
||||
Document = document;
|
||||
Path = path;
|
||||
}
|
||||
|
||||
public Document Document { get; }
|
||||
|
||||
/// <summary>The full path of the file it was opened from, else null.</summary>
|
||||
public string Path { get; }
|
||||
|
||||
/// <summary>True after a change this session that is not saved.</summary>
|
||||
public bool Modified { get; private set; }
|
||||
|
||||
/// <summary>The nest's name, else the file name.</summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
public sealed class NestTabs
|
||||
{
|
||||
private readonly List<NestTab> tabs = new();
|
||||
|
||||
/// <summary>Raised after a tab is added, closed or activated.</summary>
|
||||
public event EventHandler Changed;
|
||||
|
||||
public IReadOnlyList<NestTab> Tabs => tabs;
|
||||
|
||||
/// <summary>The active tab's index, or -1 when no nest is open.</summary>
|
||||
public int ActiveIndex { get; private set; } = -1;
|
||||
|
||||
public NestTab Active => ActiveIndex >= 0 ? tabs[ActiveIndex] : null;
|
||||
|
||||
/// <summary>True when any open nest has unsaved changes.</summary>
|
||||
public bool AnyModified => tabs.Any(t => t.Modified);
|
||||
|
||||
/// <summary>
|
||||
/// Activates the tab already showing <paramref name="path"/>, or loads it with
|
||||
/// <paramref name="load"/> into a new tab after the others. A load that throws adds no tab.
|
||||
/// </summary>
|
||||
public NestTab Open(string path, Func<string, Document> 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);
|
||||
}
|
||||
|
||||
/// <summary>Removes the tab; the host asks about unsaved changes first.</summary>
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user