ColorPicker.Prompt.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. namespace Terminal.Gui;
  2. public partial class ColorPicker
  3. {
  4. /// <summary>
  5. /// Open a <see cref="Dialog"/> with two <see cref="ColorPicker"/> or <see cref="ColorPicker16"/>, based on the
  6. /// <see cref="ConsoleDriver.Force16Colors"/> is false or true, respectively, for <see cref="Attribute.Foreground"/>
  7. /// and <see cref="Attribute.Background"/> colors.
  8. /// </summary>
  9. /// <param name="title">The title to show in the dialog.</param>
  10. /// <param name="currentAttribute">The current attribute used.</param>
  11. /// <param name="newAttribute">The new attribute.</param>
  12. /// <returns><see langword="true"/> if a new color was accepted, otherwise <see langword="false"/>.</returns>
  13. public static bool Prompt (string title, Attribute? currentAttribute, out Attribute newAttribute)
  14. {
  15. var accept = false;
  16. var d = new Dialog
  17. {
  18. Title = title,
  19. Width = Application.Force16Colors ? 37 : Dim.Auto (DimAutoStyle.Auto, Dim.Percent (80), Dim.Percent (90)),
  20. Height = 20
  21. };
  22. var btnOk = new Button
  23. {
  24. X = Pos.Center () - 5,
  25. Y = Application.Force16Colors ? 6 : 4,
  26. Text = "Ok",
  27. Width = Dim.Auto (),
  28. IsDefault = true
  29. };
  30. btnOk.Accepting += (s, e) =>
  31. {
  32. accept = true;
  33. e.Cancel = true;
  34. Application.RequestStop ();
  35. };
  36. var btnCancel = new Button
  37. {
  38. X = Pos.Center () + 5,
  39. Y = 4,
  40. Text = "Cancel",
  41. Width = Dim.Auto ()
  42. };
  43. btnCancel.Accepting += (s, e) =>
  44. {
  45. e.Cancel = true;
  46. Application.RequestStop ();
  47. };
  48. d.Add (btnOk);
  49. d.Add (btnCancel);
  50. d.AddButton (btnOk);
  51. d.AddButton (btnCancel);
  52. View cpForeground;
  53. if (Application.Force16Colors)
  54. {
  55. cpForeground = new ColorPicker16
  56. {
  57. SelectedColor = currentAttribute!.Value.Foreground.GetClosestNamedColor16 (),
  58. Width = Dim.Fill (),
  59. BorderStyle = LineStyle.Single,
  60. Title = "Foreground"
  61. };
  62. }
  63. else
  64. {
  65. cpForeground = new ColorPicker
  66. {
  67. SelectedColor = currentAttribute!.Value.Foreground,
  68. Width = Dim.Fill (),
  69. Style = new () { ShowColorName = true, ShowTextFields = true },
  70. BorderStyle = LineStyle.Single,
  71. Title = "Foreground"
  72. };
  73. ((ColorPicker)cpForeground).ApplyStyleChanges ();
  74. }
  75. View cpBackground;
  76. if (Application.Force16Colors)
  77. {
  78. cpBackground = new ColorPicker16
  79. {
  80. SelectedColor = currentAttribute!.Value.Background.GetClosestNamedColor16 (),
  81. Y = Pos.Bottom (cpForeground) + 1,
  82. Width = Dim.Fill (),
  83. BorderStyle = LineStyle.Single,
  84. Title = "Background"
  85. };
  86. }
  87. else
  88. {
  89. cpBackground = new ColorPicker
  90. {
  91. SelectedColor = currentAttribute!.Value.Background,
  92. Width = Dim.Fill (),
  93. Y = Pos.Bottom (cpForeground) + 1,
  94. Style = new () { ShowColorName = true, ShowTextFields = true },
  95. BorderStyle = LineStyle.Single,
  96. Title = "Background"
  97. };
  98. ((ColorPicker)cpBackground).ApplyStyleChanges ();
  99. }
  100. d.Add (cpForeground, cpBackground);
  101. Application.Run (d);
  102. d.Dispose ();
  103. Color newForeColor = Application.Force16Colors ? ((ColorPicker16)cpForeground).SelectedColor : ((ColorPicker)cpForeground).SelectedColor;
  104. Color newBackColor = Application.Force16Colors ? ((ColorPicker16)cpBackground).SelectedColor : ((ColorPicker)cpBackground).SelectedColor;
  105. newAttribute = new (newForeColor, newBackColor);
  106. return accept;
  107. }
  108. }