ColorPicker.Prompt.cs 4.2 KB

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