Skip to content

Commit

Permalink
Add CanClose
Browse files Browse the repository at this point in the history
  • Loading branch information
erri120 committed Jun 20, 2024
1 parent f35c3bb commit 84c3af8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/NexusMods.App.UI/WorkspaceSystem/Page/APageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,7 @@ public string TabTitle
public PanelTabId TabId { get; set; }

protected PageIdBundle IdBundle => new(WindowId, WorkspaceId, PanelId, TabId);

/// <inheritdoc/>
public virtual bool CanClose() => true;
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,49 @@
using JetBrains.Annotations;
using NexusMods.App.UI.Windows;
using NexusMods.Icons;

namespace NexusMods.App.UI.WorkspaceSystem;

[PublicAPI]
public interface IPageViewModelInterface : IViewModelInterface
{
/// <summary>
/// Gets or sets the icon of this page to be shown in the tab header.
/// </summary>
public IconValue TabIcon { get; }
IconValue TabIcon { get; }

/// <summary>
/// Gets or sets the title of this page in the tab header.
/// Gets the title of this page in the tab header.
/// </summary>
public string TabTitle { get; }
string TabTitle { get; }

/// <summary>
/// Gets or sets the ID of the window this page is in.
/// </summary>
public WindowId WindowId { get; set; }
WindowId WindowId { get; set; }

/// <summary>
/// Gets or sets the ID of the workspace this page is in.
/// </summary>
public WorkspaceId WorkspaceId { get; set; }
WorkspaceId WorkspaceId { get; set; }

/// <summary>
/// Gets or sets the ID of the panel this page is in.
/// </summary>
public PanelId PanelId { get; set; }
PanelId PanelId { get; set; }

/// <summary>
/// Gets or sets the ID of the tab this page is in.
/// </summary>
public PanelTabId TabId { get; set; }
PanelTabId TabId { get; set; }

/// <summary>
/// Called before the tab is closed.
/// </summary>
/// <remarks>
/// Use this method for pages that might contain unsaved data or need to run
/// logic on close.
/// </remarks>
/// <returns><c>true</c> if the tab can be closed, <c>false</c> if not.</returns>
bool CanClose();
}
5 changes: 5 additions & 0 deletions src/NexusMods.App.UI/WorkspaceSystem/Panel/PanelViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ public void SelectTab(PanelTabId tabId)

public void CloseTab(PanelTabId id)
{
var tab = _tabs.FirstOrDefault(tab => tab.Id == id);
if (tab is null) return;

if (!tab.Contents.ViewModel.CanClose()) return;

_tabsList.Edit(updater =>
{
var index = updater.LinearSearch(item => item.Id == id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,8 @@ private void OpenPageReplaceTab(PageData pageData, OpenPageBehavior.ReplaceTab r
return;
}

if (!tab.Contents.ViewModel.CanClose()) return;

// Replace the tab contents
var newTabPage = _factoryController.Create(pageData, WindowId, Id, panel.Id, tab.Id);
tab.Header.Icon = newTabPage.ViewModel.TabIcon;
Expand Down

0 comments on commit 84c3af8

Please sign in to comment.