Sliders.cs 28 KB

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