ColorPicker.Prompt.cs 4.4 KB

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