123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- namespace Terminal.Gui;
- public partial class ColorPicker
- {
- /// <summary>
- /// Open a <see cref="Dialog"/> with two <see cref="ColorPicker"/> or <see cref="ColorPicker16"/>, based on the
- /// <see cref="ConsoleDriver.Force16Colors"/> is false or true, respectively, for <see cref="Attribute.Foreground"/>
- /// and <see cref="Attribute.Background"/> colors.
- /// </summary>
- /// <param name="title">The title to show in the dialog.</param>
- /// <param name="currentAttribute">The current attribute used.</param>
- /// <param name="newAttribute">The new attribute.</param>
- /// <returns><see langword="true"/> if a new color was accepted, otherwise <see langword="false"/>.</returns>
- public static bool Prompt (string title, Attribute? currentAttribute, out Attribute newAttribute)
- {
- var accept = false;
- var d = new Dialog
- {
- Title = title,
- Width = Application.Force16Colors ? 37 : Dim.Auto (DimAutoStyle.Auto, Dim.Percent (80), Dim.Percent (90)),
- Height = 20
- };
- var btnOk = new Button
- {
- X = Pos.Center () - 5,
- Y = Application.Force16Colors ? 6 : 4,
- Text = "Ok",
- Width = Dim.Auto (),
- IsDefault = true
- };
- btnOk.Accepting += (s, e) =>
- {
- accept = true;
- e.Cancel = true;
- Application.RequestStop ();
- };
- var btnCancel = new Button
- {
- X = Pos.Center () + 5,
- Y = 4,
- Text = "Cancel",
- Width = Dim.Auto ()
- };
- btnCancel.Accepting += (s, e) =>
- {
- e.Cancel = true;
- Application.RequestStop ();
- };
- d.Add (btnOk);
- d.Add (btnCancel);
- d.AddButton (btnOk);
- d.AddButton (btnCancel);
- View cpForeground;
- if (Application.Force16Colors)
- {
- cpForeground = new ColorPicker16
- {
- SelectedColor = currentAttribute!.Value.Foreground.GetClosestNamedColor16 (),
- Width = Dim.Fill (),
- BorderStyle = LineStyle.Single,
- Title = "Foreground"
- };
- }
- else
- {
- cpForeground = new ColorPicker
- {
- SelectedColor = currentAttribute!.Value.Foreground,
- Width = Dim.Fill (),
- Style = new () { ShowColorName = true, ShowTextFields = true },
- BorderStyle = LineStyle.Single,
- Title = "Foreground"
- };
- ((ColorPicker)cpForeground).ApplyStyleChanges ();
- }
- View cpBackground;
- if (Application.Force16Colors)
- {
- cpBackground = new ColorPicker16
- {
- SelectedColor = currentAttribute!.Value.Background.GetClosestNamedColor16 (),
- Y = Pos.Bottom (cpForeground) + 1,
- Width = Dim.Fill (),
- BorderStyle = LineStyle.Single,
- Title = "Background"
- };
- }
- else
- {
- cpBackground = new ColorPicker
- {
- SelectedColor = currentAttribute!.Value.Background,
- Width = Dim.Fill (),
- Y = Pos.Bottom (cpForeground) + 1,
- Style = new () { ShowColorName = true, ShowTextFields = true },
- BorderStyle = LineStyle.Single,
- Title = "Background"
- };
- ((ColorPicker)cpBackground).ApplyStyleChanges ();
- }
- d.Add (cpForeground, cpBackground);
- Application.Run (d);
- d.Dispose ();
- Color newForeColor = Application.Force16Colors ? ((ColorPicker16)cpForeground).SelectedColor : ((ColorPicker)cpForeground).SelectedColor;
- Color newBackColor = Application.Force16Colors ? ((ColorPicker16)cpBackground).SelectedColor : ((ColorPicker)cpBackground).SelectedColor;
- newAttribute = new (newForeColor, newBackColor);
- return accept;
- }
- }
|