Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #1720 - ColorPicker pop up example (in ProgressBarStyles Scenario) #3154

Merged
merged 8 commits into from
Jan 13, 2024
8 changes: 7 additions & 1 deletion Terminal.Gui/Views/ProgressBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,22 @@ public bool BidirectionalMarquee {
/// </remarks>
public void Pulse ()
{
if (_activityPos == null) {
if (_activityPos == null || _activityPos.Length == 0) {
PopulateActivityPos ();
}

if (_activityPos!.Length == 0) {
return;
}

if (!_isActivity) {
_isActivity = true;
_delta = 1;
} else {
for (var i = 0; i < _activityPos.Length; i++) {
_activityPos [i] += _delta;
}

if (_activityPos [^1] < 0) {
for (var i = 0; i < _activityPos.Length; i++) {
_activityPos [i] = i - _activityPos.Length + 2;
Expand Down
131 changes: 101 additions & 30 deletions UICatalog/Scenarios/ProgressBarStyles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,92 @@ public override void Init ()
editor.ColorScheme = Colors.ColorSchemes [TopLevelColorScheme];

const float fractionStep = 0.01F;
const int pbWidth = 25;

var pbFormatEnum = Enum.GetValues (typeof (ProgressBarFormat)).Cast<ProgressBarFormat> ().ToList ();

var rbPBFormat = new RadioGroup (pbFormatEnum.Select (e => e.ToString ()).ToArray ()) {
var pbList = new ListView () {
Title = "Focused ProgressBar",
Y = 0,
X = Pos.Center (),
Y = 10,
Orientation = Orientation.Horizontal,
Width = 30,
Height = 7,
BorderStyle = LineStyle.Single
};
pbList.SelectedItemChanged += (sender, e) => {
editor.ViewToEdit = editor.Subviews.First (v => v.GetType () == typeof (ProgressBar) && v.Title == (string)e.Value);
};
editor.Add (pbList);
pbList.SelectedItem = 0;

#region ColorPicker
ColorName ChooseColor (string text, ColorName colorName)
{

var colorPicker = new ColorPicker {
Title = text,
SelectedColor = colorName
};

var dialog = new Dialog {
Title = text
};

dialog.LayoutComplete += (sender, args) => {
// TODO: Replace with Dim.Auto
dialog.X = pbList.Frame.X;
dialog.Y = pbList.Frame.Height;

dialog.Bounds = new Rect (0, 0, colorPicker.Frame.Width, colorPicker.Frame.Height);

Application.Top.LayoutSubviews ();
};

dialog.Add (colorPicker);
colorPicker.ColorChanged += (s, e) => {
dialog.RequestStop ();
};
Application.Run (dialog);

var retColor = colorPicker.SelectedColor;
colorPicker.Dispose ();

return retColor;
}

var fgColorPickerBtn = new Button {
Text = "Foreground HotNormal Color",
X = Pos.Center (),
Y = Pos.Bottom (pbList),
};
editor.Add (fgColorPickerBtn);
fgColorPickerBtn.Clicked += (s, e) => {
var newColor = ChooseColor (fgColorPickerBtn.Text, editor.ViewToEdit.ColorScheme.HotNormal.Foreground.ColorName);
var cs = new ColorScheme (editor.ViewToEdit.ColorScheme) {
HotNormal = new Attribute (newColor, editor.ViewToEdit.ColorScheme.HotNormal.Background)
};
editor.ViewToEdit.ColorScheme = cs;
};

var bgColorPickerBtn = new Button {
X = Pos.Center (),
Y = Pos.Bottom (fgColorPickerBtn),
Text = "Background HotNormal Color"
};
editor.Add (bgColorPickerBtn);
bgColorPickerBtn.Clicked += (s, e) => {
var newColor = ChooseColor (fgColorPickerBtn.Text, editor.ViewToEdit.ColorScheme.HotNormal.Background.ColorName);
var cs = new ColorScheme (editor.ViewToEdit.ColorScheme) {
HotNormal = new Attribute (editor.ViewToEdit.ColorScheme.HotNormal.Foreground, newColor)
};
editor.ViewToEdit.ColorScheme = cs;
};
#endregion

var pbFormatEnum = Enum.GetValues (typeof (ProgressBarFormat)).Cast<ProgressBarFormat> ().ToList ();
var rbPBFormat = new RadioGroup (pbFormatEnum.Select (e => e.ToString ()).ToArray ()) {
BorderStyle = LineStyle.Single,
Title = "ProgressBarFormat",
X = Pos.Left (pbList),
Y = Pos.Bottom (bgColorPickerBtn) + 1,
};
editor.Add (rbPBFormat);

var button = new Button ("Start timer") {
Expand All @@ -54,18 +130,20 @@ public override void Init ()
Title = "Blocks",
X = Pos.Center (),
Y = Pos.Bottom (button) + 1,
Width = pbWidth,
BorderStyle = LineStyle.Single
Width = Dim.Width (pbList),
BorderStyle = LineStyle.Single,
CanFocus = true
};
editor.Add (blocksPB);

var continuousPB = new ProgressBar {
Title = "Continuous",
X = Pos.Center (),
Y = Pos.Bottom (blocksPB) + 1,
Width = pbWidth,
Width = Dim.Width (pbList),
ProgressBarStyle = ProgressBarStyle.Continuous,
BorderStyle = LineStyle.Single
BorderStyle = LineStyle.Single,
CanFocus = true
};
editor.Add (continuousPB);

Expand Down Expand Up @@ -99,22 +177,27 @@ public override void Init ()
Title = "Marquee Blocks",
X = Pos.Center (),
Y = Pos.Bottom (ckbBidirectional) + 1,
Width = pbWidth,
Width = Dim.Width (pbList),
ProgressBarStyle = ProgressBarStyle.MarqueeBlocks,
BorderStyle = LineStyle.Single
BorderStyle = LineStyle.Single,
CanFocus = true
};
editor.Add (marqueesBlocksPB);

var marqueesContinuousPB = new ProgressBar {
Title = "Marquee Continuous",
X = Pos.Center (),
Y = Pos.Bottom (marqueesBlocksPB) + 1,
Width = pbWidth,
Width = Dim.Width (pbList),
ProgressBarStyle = ProgressBarStyle.MarqueeContinuous,
BorderStyle = LineStyle.Single
BorderStyle = LineStyle.Single,
CanFocus = true
};
editor.Add (marqueesContinuousPB);

pbList.SetSource (editor.Subviews.Where (v => v.GetType () == typeof (ProgressBar)).Select (v => v.Title).ToList ());
pbList.SelectedItem = 0;

rbPBFormat.SelectedItemChanged += (s, e) => {
blocksPB.ProgressBarFormat = (ProgressBarFormat)e.SelectedItem;
continuousPB.ProgressBarFormat = (ProgressBarFormat)e.SelectedItem;
Expand All @@ -133,6 +216,7 @@ public override void Init ()
Application.Wakeup ();
}, null, 0, 300);


Application.Top.Unloaded += Top_Unloaded;

void Top_Unloaded (object sender, EventArgs args)
Expand All @@ -148,24 +232,11 @@ void Top_Unloaded (object sender, EventArgs args)
Application.Top.Unloaded -= Top_Unloaded;
}

var pbs = editor.Subviews.Where (v => v.GetType () == typeof (ProgressBar)).Select (v => v.Title).ToList ();
var pbList = new ListView (pbs) {
Title = "Focused ProgressBar",
Y = 0,
X = Pos.Center (),
Width = 30,
Height = 7,
BorderStyle = LineStyle.Single
};
pbList.SelectedItemChanged += (sender, e) => {
editor.ViewToEdit = editor.Subviews.First (v => v.GetType () == typeof (ProgressBar) && v.Title == (string)e.Value);
};
editor.Add (pbList);
pbList.SelectedItem = 0;

Application.Run (editor);
Application.Shutdown ();
}

public override void Run () { }
public override void Run ()
{
}
}
Loading