ColorPicker.Prompt.cs 4.2 KB

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