Skip to content

Commit b7dfa07

Browse files
author
tznind
committed
Move everything back to original positions if error
1 parent e68e6ae commit b7dfa07

4 files changed

Lines changed: 33 additions & 9 deletions

File tree

src/Operations/DragOperation.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,4 +287,13 @@ private void ContinueDrag(DragMemento mem, Point dest)
287287

288288
this.DestinationY = dest.Y;
289289
}
290+
291+
/// <summary>
292+
/// Restores all mementos to original locations
293+
/// </summary>
294+
/// <exception cref="NotImplementedException"></exception>
295+
public void Abandon()
296+
{
297+
UndoImpl();
298+
}
290299
}

src/UI/Editor.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace TerminalGuiDesigner.UI;
2626
/// application. Hooks key and mouse events and mounts as a sub-view whatever file
2727
/// the user opens.
2828
/// </summary>
29-
public class Editor : Toplevel
29+
public class Editor : Toplevel, IErrorReporter
3030
{
3131
private KeyMap keyMap;
3232
private readonly KeyboardManager keyboardManager;
@@ -82,7 +82,10 @@ public Editor()
8282
LoadKeyMap();
8383

8484
this.keyboardManager = new KeyboardManager(this.keyMap);
85-
this.mouseManager = new MouseManager();
85+
this.mouseManager = new MouseManager()
86+
{
87+
ErrorReporter = this
88+
};
8689
this.Closing += this.Editor_Closing;
8790

8891
this.BuildRootMenu();
@@ -1177,7 +1180,7 @@ private void Delete()
11771180
}
11781181
}
11791182

1180-
private void ShowErrorThatViewIsUsedByOthers(Design[] usedBy)
1183+
public void ShowErrorThatViewIsUsedByOthers(Design[] usedBy)
11811184
{
11821185
if (usedBy.Length == 1)
11831186
{
@@ -1504,4 +1507,4 @@ private void ShowEditProperties(Design d)
15041507
var edit = new EditDialog(d);
15051508
Application.Run(edit, this.ErrorHandler);
15061509
}
1507-
}
1510+
}

src/UI/IErrorReporter.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace TerminalGuiDesigner.UI;
2+
3+
public interface IErrorReporter
4+
{
5+
public void ShowErrorThatViewIsUsedByOthers(Design[] usedBy);
6+
}

src/UI/MouseManager.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ public class MouseManager
3333
/// </summary>
3434
public Rectangle? SelectionBox => RectExtensions.FromBetweenPoints(this.selectionStart, this.selectionEnd);
3535

36+
/// <summary>
37+
/// Set to report impossible commands as they are executed i.e. to give info
38+
/// to user about why operation failed etc.
39+
/// </summary>
40+
public IErrorReporter? ErrorReporter;
41+
3642
/// <summary>
3743
/// Responds to <see cref="Application.MouseEvent"/>(by changing a 'drag a box' selection area
3844
/// or starting a resize etc).
@@ -198,12 +204,12 @@ public void HandleMouse(MouseEventArgs m, Design viewBeingEdited)
198204
// we are dragging into a new container
199205
this.dragOperation.DropInto = into.View;
200206

201-
/*
202-
TODO: Do this
203-
if (this.dragOperation.IsImpossible)
207+
208+
if (this.dragOperation.IsImpossible && this.dragOperation.PreventDrag.Any())
204209
{
205-
Editor.FlashMessageIfAble($"{Editor.Error}")
206-
}*/
210+
ErrorReporter?.ShowErrorThatViewIsUsedByOthers(this.dragOperation.PreventDrag);
211+
this.dragOperation.Abandon();
212+
}
207213

208214
// end drag
209215
OperationManager.Instance.Do(this.dragOperation);

0 commit comments

Comments
 (0)