Sliders.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Diagnostics.Tracing;
  5. using System.Linq;
  6. using System.Text;
  7. using Terminal.Gui;
  8. namespace UICatalog.Scenarios;
  9. [ScenarioMetadata ("Sliders", "Demonstrates the Slider view.")]
  10. [ScenarioCategory ("Controls")]
  11. public class Sliders : Scenario
  12. {
  13. public void MakeSliders (View v, List<object> options)
  14. {
  15. List<SliderType> types = Enum.GetValues (typeof (SliderType)).Cast<SliderType> ().ToList ();
  16. Slider prev = null;
  17. foreach (SliderType type in types)
  18. {
  19. var view = new Slider (options)
  20. {
  21. Title = type.ToString (),
  22. X = 0,
  23. Y = prev == null ? 0 : Pos.Bottom (prev),
  24. BorderStyle = LineStyle.Single,
  25. Type = type,
  26. AllowEmpty = true
  27. };
  28. //view.Padding.Thickness = new (0,1,0,0);
  29. v.Add (view);
  30. prev = view;
  31. }
  32. List<object> singleOptions = new ()
  33. {
  34. 1,
  35. 2,
  36. 3,
  37. 4,
  38. 5,
  39. 6,
  40. 7,
  41. 8,
  42. 9,
  43. 10,
  44. 11,
  45. 12,
  46. 13,
  47. 14,
  48. 15,
  49. 16,
  50. 17,
  51. 18,
  52. 19,
  53. 20,
  54. 21,
  55. 22,
  56. 23,
  57. 24,
  58. 25,
  59. 26,
  60. 27,
  61. 28,
  62. 29,
  63. 30,
  64. 31,
  65. 32,
  66. 33,
  67. 34,
  68. 35,
  69. 36,
  70. 37,
  71. 38,
  72. 39
  73. };
  74. var single = new Slider (singleOptions)
  75. {
  76. Title = "_Continuous",
  77. X = 0,
  78. Y = prev == null ? 0 : Pos.Bottom (prev),
  79. Type = SliderType.Single,
  80. BorderStyle = LineStyle.Single,
  81. AllowEmpty = false
  82. };
  83. single.SubviewLayout += (s, e) =>
  84. {
  85. if (single.Orientation == Orientation.Horizontal)
  86. {
  87. single.Style.SpaceChar = new Cell { Rune = Glyphs.HLine };
  88. single.Style.OptionChar = new Cell { Rune = Glyphs.HLine };
  89. }
  90. else
  91. {
  92. single.Style.SpaceChar = new Cell { Rune = Glyphs.VLine };
  93. single.Style.OptionChar = new Cell { Rune = Glyphs.VLine };
  94. }
  95. };
  96. single.Style.SetChar = new Cell { Rune = Glyphs.ContinuousMeterSegment };
  97. single.Style.DragChar = new Cell { Rune = Glyphs.ContinuousMeterSegment };
  98. v.Add (single);
  99. single.OptionsChanged += (s, e) =>
  100. {
  101. single.Title = $"_Continuous {e.Options.FirstOrDefault ().Key}";
  102. };
  103. List<object> oneOption = new () { "The Only Option" };
  104. var one = new Slider (oneOption)
  105. {
  106. Title = "_One Option",
  107. X = 0,
  108. Y = prev == null ? 0 : Pos.Bottom (single),
  109. Type = SliderType.Single,
  110. BorderStyle = LineStyle.Single,
  111. AllowEmpty = false
  112. };
  113. v.Add (one);
  114. }
  115. public override void Main ()
  116. {
  117. Application.Init ();
  118. Window app = new ()
  119. {
  120. Title = GetQuitKeyAndName ()
  121. };
  122. MakeSliders (
  123. app,
  124. new List<object>
  125. {
  126. 500,
  127. 1000,
  128. 1500,
  129. 2000,
  130. 2500,
  131. 3000,
  132. 3500,
  133. 4000,
  134. 4500,
  135. 5000
  136. }
  137. );
  138. var configView = new FrameView
  139. {
  140. Title = "Confi_guration",
  141. X = Pos.Percent (50),
  142. Y = 0,
  143. Width = Dim.Fill (),
  144. Height = Dim.Fill (),
  145. ColorScheme = Colors.ColorSchemes ["Dialog"]
  146. };
  147. app.Add (configView);
  148. #region Config Slider
  149. Slider<string> optionsSlider = new ()
  150. {
  151. Title = "Options",
  152. X = 0,
  153. Y = 0,
  154. Width = Dim.Fill (),
  155. Type = SliderType.Multiple,
  156. AllowEmpty = true,
  157. BorderStyle = LineStyle.Single
  158. };
  159. optionsSlider.Style.SetChar = optionsSlider.Style.SetChar with { Attribute = new Attribute (Color.BrightGreen, Color.Black) };
  160. optionsSlider.Style.LegendAttributes.SetAttribute = new Attribute (Color.Green, Color.Black);
  161. optionsSlider.Options = new List<SliderOption<string>>
  162. {
  163. new () { Legend = "Legends" },
  164. new () { Legend = "RangeAllowSingle" },
  165. new () { Legend = "EndSpacing" },
  166. new () { Legend = "DimAuto" }
  167. };
  168. configView.Add (optionsSlider);
  169. optionsSlider.OptionsChanged += (sender, e) =>
  170. {
  171. foreach (Slider s in app.Subviews.OfType<Slider> ())
  172. {
  173. s.ShowLegends = e.Options.ContainsKey (0);
  174. s.RangeAllowSingle = e.Options.ContainsKey (1);
  175. s.ShowEndSpacing = e.Options.ContainsKey (2);
  176. if (e.Options.ContainsKey (3))
  177. {
  178. s.Width = Dim.Auto (DimAutoStyle.Content);
  179. s.Height = Dim.Auto (DimAutoStyle.Content);
  180. }
  181. else
  182. {
  183. if (s.Orientation == Orientation.Horizontal)
  184. {
  185. s.Width = Dim.Percent (50);
  186. int h = s.ShowLegends && s.LegendsOrientation == Orientation.Vertical
  187. ? s.Options.Max (o => o.Legend.Length) + 3
  188. : 4;
  189. s.Height = h;
  190. }
  191. else
  192. {
  193. int w = s.ShowLegends ? s.Options.Max (o => o.Legend.Length) + 3 : 3;
  194. s.Width = w;
  195. s.Height = Dim.Fill ();
  196. }
  197. }
  198. }
  199. };
  200. optionsSlider.SetOption (0); // Legends
  201. optionsSlider.SetOption (1); // RangeAllowSingle
  202. optionsSlider.SetOption (3); // DimAuto
  203. CheckBox dimAutoUsesMin = new ()
  204. {
  205. Text = "Use minimum size (vs. ideal)",
  206. X = 0,
  207. Y = Pos.Bottom (optionsSlider)
  208. };
  209. dimAutoUsesMin.CheckedStateChanging += (sender, e) =>
  210. {
  211. foreach (Slider s in app.Subviews.OfType<Slider> ())
  212. {
  213. s.UseMinimumSize = !s.UseMinimumSize;
  214. }
  215. };
  216. configView.Add (dimAutoUsesMin);
  217. #region Slider Orientation Slider
  218. Slider<string> orientationSlider = new (new List<string> { "Horizontal", "Vertical" })
  219. {
  220. Title = "Slider Orientation",
  221. X = 0,
  222. Y = Pos.Bottom (dimAutoUsesMin) + 1,
  223. BorderStyle = LineStyle.Single
  224. };
  225. orientationSlider.SetOption (0);
  226. configView.Add (orientationSlider);
  227. orientationSlider.OptionsChanged += (sender, e) =>
  228. {
  229. View prev = null;
  230. foreach (Slider s in app.Subviews.OfType<Slider> ())
  231. {
  232. if (e.Options.ContainsKey (0))
  233. {
  234. s.Orientation = Orientation.Horizontal;
  235. s.Style.SpaceChar = new Cell { Rune = Glyphs.HLine };
  236. if (prev == null)
  237. {
  238. s.Y = 0;
  239. }
  240. else
  241. {
  242. s.Y = Pos.Bottom (prev) + 1;
  243. }
  244. s.X = 0;
  245. prev = s;
  246. }
  247. else if (e.Options.ContainsKey (1))
  248. {
  249. s.Orientation = Orientation.Vertical;
  250. s.Style.SpaceChar = new Cell { Rune = Glyphs.VLine };
  251. if (prev == null)
  252. {
  253. s.X = 0;
  254. }
  255. else
  256. {
  257. s.X = Pos.Right (prev) + 2;
  258. }
  259. s.Y = 0;
  260. prev = s;
  261. }
  262. if (optionsSlider.GetSetOptions ().Contains (3))
  263. {
  264. s.Width = Dim.Auto (DimAutoStyle.Content);
  265. s.Height = Dim.Auto (DimAutoStyle.Content);
  266. }
  267. else
  268. {
  269. if (s.Orientation == Orientation.Horizontal)
  270. {
  271. s.Width = Dim.Percent (50);
  272. int h = s.ShowLegends && s.LegendsOrientation == Orientation.Vertical
  273. ? s.Options.Max (o => o.Legend.Length) + 3
  274. : 4;
  275. s.Height = h;
  276. }
  277. else
  278. {
  279. int w = s.ShowLegends ? s.Options.Max (o => o.Legend.Length) + 3 : 3;
  280. s.Width = w;
  281. s.Height = Dim.Fill ();
  282. }
  283. }
  284. }
  285. };
  286. #endregion Slider Orientation Slider
  287. #region Legends Orientation Slider
  288. Slider<string> legendsOrientationSlider = new (new List<string> { "Horizontal", "Vertical" })
  289. {
  290. Title = "Legends Orientation",
  291. X = 0,
  292. Y = Pos.Bottom (orientationSlider) + 1,
  293. BorderStyle = LineStyle.Single
  294. };
  295. legendsOrientationSlider.SetOption (0);
  296. configView.Add (legendsOrientationSlider);
  297. legendsOrientationSlider.OptionsChanged += (sender, e) =>
  298. {
  299. foreach (Slider s in app.Subviews.OfType<Slider> ())
  300. {
  301. if (e.Options.ContainsKey (0))
  302. {
  303. s.LegendsOrientation = Orientation.Horizontal;
  304. }
  305. else if (e.Options.ContainsKey (1))
  306. {
  307. s.LegendsOrientation = Orientation.Vertical;
  308. }
  309. if (optionsSlider.GetSetOptions ().Contains (3))
  310. {
  311. s.Width = Dim.Auto (DimAutoStyle.Content);
  312. s.Height = Dim.Auto (DimAutoStyle.Content);
  313. }
  314. else
  315. {
  316. if (s.Orientation == Orientation.Horizontal)
  317. {
  318. s.Width = Dim.Percent (50);
  319. int h = s.ShowLegends && s.LegendsOrientation == Orientation.Vertical
  320. ? s.Options.Max (o => o.Legend.Length) + 3
  321. : 4;
  322. s.Height = h;
  323. }
  324. else
  325. {
  326. int w = s.ShowLegends ? s.Options.Max (o => o.Legend.Length) + 3 : 3;
  327. s.Width = w;
  328. s.Height = Dim.Fill ();
  329. }
  330. }
  331. }
  332. };
  333. #endregion Legends Orientation Slider
  334. #region Spacing Options
  335. FrameView spacingOptions = new ()
  336. {
  337. Title = "Spacing Options",
  338. X = Pos.Right (orientationSlider),
  339. Y = Pos.Top (orientationSlider),
  340. Width = Dim.Fill (),
  341. Height = Dim.Auto (),
  342. BorderStyle = LineStyle.Single
  343. };
  344. Label label = new ()
  345. {
  346. Text = "Min _Inner Spacing:",
  347. };
  348. NumericUpDown<int> innerSpacingUpDown = new ()
  349. {
  350. X = Pos.Right (label) + 1
  351. };
  352. innerSpacingUpDown.Value = app.Subviews.OfType<Slider> ().First ().MinimumInnerSpacing;
  353. innerSpacingUpDown.ValueChanging += (sender, e) =>
  354. {
  355. if (e.NewValue < 0)
  356. {
  357. e.Cancel = true;
  358. return;
  359. }
  360. foreach (Slider s in app.Subviews.OfType<Slider> ())
  361. {
  362. s.MinimumInnerSpacing = e.NewValue;
  363. }
  364. };
  365. spacingOptions.Add (label, innerSpacingUpDown);
  366. configView.Add (spacingOptions);
  367. #endregion
  368. #region Color Slider
  369. foreach (Slider s in app.Subviews.OfType<Slider> ())
  370. {
  371. s.Style.OptionChar = s.Style.OptionChar with { Attribute = app.GetNormalColor () };
  372. s.Style.SetChar = s.Style.SetChar with { Attribute = app.GetNormalColor () };
  373. s.Style.LegendAttributes.SetAttribute = app.GetNormalColor ();
  374. s.Style.RangeChar = s.Style.RangeChar with { Attribute = app.GetNormalColor () };
  375. }
  376. Slider<(Color, Color)> sliderFGColor = new ()
  377. {
  378. Title = "FG Color",
  379. X = 0,
  380. Y = Pos.Bottom (
  381. legendsOrientationSlider
  382. )
  383. + 1,
  384. Type = SliderType.Single,
  385. BorderStyle = LineStyle.Single,
  386. AllowEmpty = false,
  387. Orientation = Orientation.Vertical,
  388. LegendsOrientation = Orientation.Horizontal,
  389. MinimumInnerSpacing = 0,
  390. UseMinimumSize = true
  391. };
  392. sliderFGColor.Style.SetChar = sliderFGColor.Style.SetChar with { Attribute = new Attribute (Color.BrightGreen, Color.Black) };
  393. sliderFGColor.Style.LegendAttributes.SetAttribute = new Attribute (Color.Green, Color.Blue);
  394. List<SliderOption<(Color, Color)>> colorOptions = new ();
  395. foreach (ColorName16 colorIndex in Enum.GetValues<ColorName16> ())
  396. {
  397. var colorName = colorIndex.ToString ();
  398. colorOptions.Add (
  399. new SliderOption<(Color, Color)>
  400. {
  401. Data = (new Color (colorIndex),
  402. new Color (colorIndex)),
  403. Legend = colorName,
  404. LegendAbbr = (Rune)colorName [0]
  405. }
  406. );
  407. }
  408. sliderFGColor.Options = colorOptions;
  409. configView.Add (sliderFGColor);
  410. sliderFGColor.OptionsChanged += (sender, e) =>
  411. {
  412. if (e.Options.Count != 0)
  413. {
  414. (Color, Color) data = e.Options.First ().Value.Data;
  415. foreach (Slider s in app.Subviews.OfType<Slider> ())
  416. {
  417. s.ColorScheme = new ColorScheme (s.ColorScheme);
  418. s.ColorScheme = new ColorScheme (s.ColorScheme)
  419. {
  420. Normal = new Attribute (
  421. data.Item2,
  422. s.ColorScheme.Normal.Background
  423. )
  424. };
  425. s.Style.OptionChar = s.Style.OptionChar with
  426. {
  427. Attribute = new Attribute (data.Item1, s.ColorScheme.Normal.Background)
  428. };
  429. s.Style.SetChar = s.Style.SetChar with
  430. {
  431. Attribute = new Attribute (
  432. data.Item1,
  433. s.Style.SetChar.Attribute?.Background
  434. ?? s.ColorScheme.Normal.Background
  435. )
  436. };
  437. s.Style.LegendAttributes.SetAttribute = new Attribute (data.Item1, s.ColorScheme.Normal.Background);
  438. s.Style.RangeChar = s.Style.RangeChar with
  439. {
  440. Attribute = new Attribute (data.Item1, s.ColorScheme.Normal.Background)
  441. };
  442. s.Style.SpaceChar = s.Style.SpaceChar with
  443. {
  444. Attribute = new Attribute (data.Item1, s.ColorScheme.Normal.Background)
  445. };
  446. s.Style.LegendAttributes.NormalAttribute =
  447. new Attribute (data.Item1, s.ColorScheme.Normal.Background);
  448. }
  449. }
  450. };
  451. Slider<(Color, Color)> sliderBGColor = new ()
  452. {
  453. Title = "BG Color",
  454. X = Pos.Right (sliderFGColor),
  455. Y = Pos.Top (sliderFGColor),
  456. Type = SliderType.Single,
  457. BorderStyle = LineStyle.Single,
  458. AllowEmpty = false,
  459. Orientation = Orientation.Vertical,
  460. LegendsOrientation = Orientation.Horizontal,
  461. MinimumInnerSpacing = 0,
  462. UseMinimumSize = true
  463. };
  464. sliderBGColor.Style.SetChar = sliderBGColor.Style.SetChar with { Attribute = new Attribute (Color.BrightGreen, Color.Black) };
  465. sliderBGColor.Style.LegendAttributes.SetAttribute = new Attribute (Color.Green, Color.Blue);
  466. sliderBGColor.Options = colorOptions;
  467. configView.Add (sliderBGColor);
  468. sliderBGColor.OptionsChanged += (sender, e) =>
  469. {
  470. if (e.Options.Count != 0)
  471. {
  472. (Color, Color) data = e.Options.First ().Value.Data;
  473. foreach (Slider s in app.Subviews.OfType<Slider> ())
  474. {
  475. s.ColorScheme = new ColorScheme (s.ColorScheme)
  476. {
  477. Normal = new Attribute (
  478. s.ColorScheme.Normal.Foreground,
  479. data.Item2
  480. )
  481. };
  482. }
  483. }
  484. };
  485. #endregion Color Slider
  486. #endregion Config Slider
  487. ObservableCollection<string> eventSource = new ();
  488. var eventLog = new ListView
  489. {
  490. X = Pos.Right (sliderBGColor),
  491. Y = Pos.Bottom (spacingOptions),
  492. Width = Dim.Fill (),
  493. Height = Dim.Fill (),
  494. ColorScheme = Colors.ColorSchemes ["Toplevel"],
  495. Source = new ListWrapper<string> (eventSource)
  496. };
  497. configView.Add (eventLog);
  498. foreach (Slider slider in app.Subviews.Where (v => v is Slider)!)
  499. {
  500. slider.Accepting += (o, args) =>
  501. {
  502. eventSource.Add ($"Accept: {string.Join(",", slider.GetSetOptions ())}");
  503. eventLog.MoveDown ();
  504. args.Cancel = true;
  505. };
  506. slider.OptionsChanged += (o, args) =>
  507. {
  508. eventSource.Add ($"OptionsChanged: {string.Join (",", slider.GetSetOptions ())}");
  509. eventLog.MoveDown ();
  510. args.Cancel = true;
  511. };
  512. }
  513. app.FocusDeepest (NavigationDirection.Forward, null);
  514. Application.Run (app);
  515. app.Dispose ();
  516. Application.Shutdown ();
  517. }
  518. }