GuiTestContext.ViewBase.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
  2. namespace TerminalGuiFluentTesting;
  3. public partial class GuiTestContext
  4. {
  5. /// <summary>
  6. /// Adds the given <paramref name="v"/> to the current top level view
  7. /// and performs layout.
  8. /// </summary>
  9. /// <param name="v"></param>
  10. /// <returns></returns>
  11. public GuiTestContext Add (View v)
  12. {
  13. WaitIteration ((app) =>
  14. {
  15. Toplevel top = app.Current ?? throw new ("Top was null so could not add view");
  16. top.Add (v);
  17. top.Layout ();
  18. _lastView = v;
  19. });
  20. return this;
  21. }
  22. private View? _lastView;
  23. /// <summary>
  24. /// The last view added (e.g. with <see cref="Add"/>) or the root/current top.
  25. /// </summary>
  26. public View LastView => _lastView ?? App?.Current ?? throw new ("Could not determine which view to add to");
  27. private T Find<T> (Func<T, bool> evaluator) where T : View
  28. {
  29. Toplevel? t = App?.Current;
  30. if (t == null)
  31. {
  32. Fail ("App.Current was null when attempting to find view");
  33. }
  34. T? f = FindRecursive (t!, evaluator);
  35. if (f == null)
  36. {
  37. Fail ("Failed to tab to a view which matched the Type and evaluator constraints in any SubViews of top");
  38. }
  39. return f!;
  40. }
  41. private T? FindRecursive<T> (View current, Func<T, bool> evaluator) where T : View
  42. {
  43. foreach (View subview in current.SubViews)
  44. {
  45. if (subview is T match && evaluator (match))
  46. {
  47. return match;
  48. }
  49. // Recursive call
  50. T? result = FindRecursive (subview, evaluator);
  51. if (result != null)
  52. {
  53. return result;
  54. }
  55. }
  56. return null;
  57. }
  58. }