ColorPicker.Prompt.cs 4.3 KB

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