Skip to content

Commit cf6b8ec

Browse files
author
tznind
committed
When trying to delete a referenced view, give feedback about the error
1 parent 5ae21fd commit cf6b8ec

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

src/Operations/DeleteViewOperation.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ public class DeleteViewOperation : Operation
1313
private readonly View[] from;
1414
private readonly Design[] originalSelection;
1515

16+
17+
/// <summary>
18+
/// Views which reference the <see cref="Design"/> being operated on
19+
/// </summary>
20+
public Design[] PreventDeleting { get; set; }
21+
1622
/// <summary>
1723
/// Initializes a new instance of the <see cref="DeleteViewOperation"/> class.
1824
/// </summary>
@@ -34,7 +40,9 @@ public DeleteViewOperation(params Design[] delete)
3440

3541
// there are view(s) that depend on us (e.g. for positioning)
3642
// that are not also being deleted themselves
37-
if (design.GetDependantDesigns().Any(dep => !delete.Contains(dep)))
43+
PreventDeleting = design.GetDependantDesigns().Where(dep => !delete.Contains(dep)).ToArray();
44+
45+
if (PreventDeleting.Length > 0)
3846
{
3947
// Prevent deleting - so there are no orphan references from existing Views e.g. Pos.Right(thingIJustDeleted);
4048
this.IsImpossible = true;

src/UI/Editor.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ public class Editor : Toplevel
6161
/// </summary>
6262
public static bool Quiet = false;
6363

64+
/// <summary>
65+
/// Start your message with this if you want it to be visually highlighted as bad.
66+
/// </summary>
67+
public const string Error = "Error";
68+
6469
/// <summary>
6570
/// Initializes a new instance of the <see cref="Editor"/> class.
6671
/// </summary>
@@ -293,6 +298,13 @@ protected override void OnDrawComplete(DrawContext? context)
293298
// and have a designable view focused
294299
if (toDisplay != null)
295300
{
301+
var before = GetCurrentAttribute();
302+
303+
if (toDisplay.StartsWith(Error))
304+
{
305+
SetAttribute(new Attribute(Color.Red, Color.Black));
306+
}
307+
296308
// write its name in the lower right
297309
int y = this.GetContentSize().Height - 1;
298310
int right = bounds.Width - 1;
@@ -303,6 +315,8 @@ protected override void OnDrawComplete(DrawContext? context)
303315
{
304316
this.AddRune(right - len + i, y, runes[i]);
305317
}
318+
319+
SetAttribute(before);
306320
}
307321
}
308322

@@ -1151,10 +1165,31 @@ private void Delete()
11511165
if (SelectionManager.Instance.Selected.Any())
11521166
{
11531167
var cmd = new DeleteViewOperation(SelectionManager.Instance.Selected.ToArray());
1168+
1169+
1170+
if (cmd.IsImpossible && cmd.PreventDeleting.Any())
1171+
{
1172+
ShowErrorThatViewIsUsedByOthers(cmd.PreventDeleting);
1173+
return;
1174+
}
1175+
11541176
OperationManager.Instance.Do(cmd);
11551177
}
11561178
}
11571179

1180+
private void ShowErrorThatViewIsUsedByOthers(Design[] usedBy)
1181+
{
1182+
if (usedBy.Length == 1)
1183+
{
1184+
flashMessage = $"{Error}, view referenced by " + usedBy[0].FieldName;
1185+
}
1186+
else
1187+
{
1188+
flashMessage = $"{Error}, view referenced by {usedBy.Length} views";
1189+
}
1190+
this.SetNeedsDraw();
1191+
}
1192+
11581193
private void DoForSelectedViews(Func<Design, Operation> operationFunc, bool allowOnRoot = false)
11591194
{
11601195
if (this.viewBeingEdited == null)

0 commit comments

Comments
 (0)