Sliders.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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.LayoutStarted += (s, e) =>
  84. {
  85. if (single.Orientation == Orientation.Horizontal)
  86. {
  87. single.Style.SpaceChar = new Cell { Rune = CM.Glyphs.HLine };
  88. single.Style.OptionChar = new Cell { Rune = CM.Glyphs.HLine };
  89. }
  90. else
  91. {
  92. single.Style.SpaceChar = new Cell { Rune = CM.Glyphs.VLine };
  93. single.Style.OptionChar = new Cell { Rune = CM.Glyphs.VLine };
  94. }
  95. };
  96. single.Style.SetChar = new Cell { Rune = CM.Glyphs.ContinuousMeterSegment };
  97. single.Style.DragChar = new Cell { Rune = CM.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 = "Configuration",
  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. if (app.IsInitialized)
  200. {
  201. app.LayoutSubviews ();
  202. }
  203. };
  204. optionsSlider.SetOption (0); // Legends
  205. optionsSlider.SetOption (1); // RangeAllowSingle
  206. optionsSlider.SetOption (3); // DimAuto
  207. CheckBox dimAutoUsesMin = new ()
  208. {
  209. Text = "Use minimum size (vs. ideal)",
  210. X = 0,
  211. Y = Pos.Bottom (optionsSlider)
  212. };
  213. dimAutoUsesMin.Toggle += (sender, e) =>
  214. {
  215. foreach (Slider s in app.Subviews.OfType<Slider> ())
  216. {
  217. s.UseMinimumSize = !s.UseMinimumSize;
  218. }
  219. };
  220. configView.Add (dimAutoUsesMin);
  221. #region Slider Orientation Slider
  222. Slider<string> orientationSlider = new (new List<string> { "Horizontal", "Vertical" })
  223. {
  224. Title = "Slider Orientation",
  225. X = 0,
  226. Y = Pos.Bottom (dimAutoUsesMin) + 1,
  227. BorderStyle = LineStyle.Single
  228. };
  229. orientationSlider.SetOption (0);
  230. configView.Add (orientationSlider);
  231. orientationSlider.OptionsChanged += (sender, e) =>
  232. {
  233. View prev = null;
  234. foreach (Slider s in app.Subviews.OfType<Slider> ())
  235. {
  236. if (e.Options.ContainsKey (0))
  237. {
  238. s.Orientation = Orientation.Horizontal;
  239. s.Style.SpaceChar = new Cell { Rune = CM.Glyphs.HLine };
  240. if (prev == null)
  241. {
  242. s.Y = 0;
  243. }
  244. else
  245. {
  246. s.Y = Pos.Bottom (prev) + 1;
  247. }
  248. s.X = 0;
  249. prev = s;
  250. }
  251. else if (e.Options.ContainsKey (1))
  252. {
  253. s.Orientation = Orientation.Vertical;
  254. s.Style.SpaceChar = new Cell { Rune = CM.Glyphs.VLine };
  255. if (prev == null)
  256. {
  257. s.X = 0;
  258. }
  259. else
  260. {
  261. s.X = Pos.Right (prev) + 2;
  262. }
  263. s.Y = 0;
  264. prev = s;
  265. }
  266. if (optionsSlider.GetSetOptions ().Contains (3))
  267. {
  268. s.Width = Dim.Auto (DimAutoStyle.Content);
  269. s.Height = Dim.Auto (DimAutoStyle.Content);
  270. }
  271. else
  272. {
  273. if (s.Orientation == Orientation.Horizontal)
  274. {
  275. s.Width = Dim.Percent (50);
  276. int h = s.ShowLegends && s.LegendsOrientation == Orientation.Vertical
  277. ? s.Options.Max (o => o.Legend.Length) + 3
  278. : 4;
  279. s.Height = h;
  280. }
  281. else
  282. {
  283. int w = s.ShowLegends ? s.Options.Max (o => o.Legend.Length) + 3 : 3;
  284. s.Width = w;
  285. s.Height = Dim.Fill ();
  286. }
  287. }
  288. }
  289. app.LayoutSubviews ();
  290. };
  291. #endregion Slider Orientation Slider
  292. #region Legends Orientation Slider
  293. Slider<string> legendsOrientationSlider = new (new List<string> { "Horizontal", "Vertical" })
  294. {
  295. Title = "Legends Orientation",
  296. X = 0,
  297. Y = Pos.Bottom (orientationSlider) + 1,
  298. BorderStyle = LineStyle.Single
  299. };
  300. legendsOrientationSlider.SetOption (0);
  301. configView.Add (legendsOrientationSlider);
  302. legendsOrientationSlider.OptionsChanged += (sender, e) =>
  303. {
  304. foreach (Slider s in app.Subviews.OfType<Slider> ())
  305. {
  306. if (e.Options.ContainsKey (0))
  307. {
  308. s.LegendsOrientation = Orientation.Horizontal;
  309. }
  310. else if (e.Options.ContainsKey (1))
  311. {
  312. s.LegendsOrientation = Orientation.Vertical;
  313. }
  314. if (optionsSlider.GetSetOptions ().Contains (3))
  315. {
  316. s.Width = Dim.Auto (DimAutoStyle.Content);
  317. s.Height = Dim.Auto (DimAutoStyle.Content);
  318. }
  319. else
  320. {
  321. if (s.Orientation == Orientation.Horizontal)
  322. {
  323. s.Width = Dim.Percent (50);
  324. int h = s.ShowLegends && s.LegendsOrientation == Orientation.Vertical
  325. ? s.Options.Max (o => o.Legend.Length) + 3
  326. : 4;
  327. s.Height = h;
  328. }
  329. else
  330. {
  331. int w = s.ShowLegends ? s.Options.Max (o => o.Legend.Length) + 3 : 3;
  332. s.Width = w;
  333. s.Height = Dim.Fill ();
  334. }
  335. }
  336. }
  337. app.LayoutSubviews ();
  338. };
  339. #endregion Legends Orientation Slider
  340. #region Spacing Options
  341. FrameView spacingOptions = new ()
  342. {
  343. Title = "Spacing Options",
  344. X = Pos.Right (orientationSlider),
  345. Y = Pos.Top (orientationSlider),
  346. Width = Dim.Fill (),
  347. Height = Dim.Auto (),
  348. BorderStyle = LineStyle.Single
  349. };
  350. Label label = new ()
  351. {
  352. Text = "Min _Inner Spacing:",
  353. };
  354. Buttons.NumericUpDown<int> innerSpacingUpDown = new ()
  355. {
  356. X = Pos.Right (label) + 1
  357. };
  358. innerSpacingUpDown.Value = app.Subviews.OfType<Slider> ().First ().MinimumInnerSpacing;
  359. innerSpacingUpDown.ValueChanging += (sender, e) =>
  360. {
  361. if (e.NewValue < 0)
  362. {
  363. e.Cancel = true;
  364. return;
  365. }
  366. foreach (Slider s in app.Subviews.OfType<Slider> ())
  367. {
  368. s.MinimumInnerSpacing = e.NewValue;
  369. }
  370. };
  371. spacingOptions.Add (label, innerSpacingUpDown);
  372. configView.Add (spacingOptions);
  373. #endregion
  374. #region Color Slider
  375. foreach (Slider s in app.Subviews.OfType<Slider> ())
  376. {
  377. s.Style.OptionChar = s.Style.OptionChar with { Attribute = app.GetNormalColor () };
  378. s.Style.SetChar = s.Style.SetChar with { Attribute = app.GetNormalColor () };
  379. s.Style.LegendAttributes.SetAttribute = app.GetNormalColor ();
  380. s.Style.RangeChar = s.Style.RangeChar with { Attribute = app.GetNormalColor () };
  381. }
  382. Slider<(Color, Color)> sliderFGColor = new ()
  383. {
  384. Title = "FG Color",
  385. X = 0,
  386. Y = Pos.Bottom (
  387. legendsOrientationSlider
  388. )
  389. + 1,
  390. Type = SliderType.Single,
  391. BorderStyle = LineStyle.Single,
  392. AllowEmpty = false,
  393. Orientation = Orientation.Vertical,
  394. LegendsOrientation = Orientation.Horizontal,
  395. MinimumInnerSpacing = 0,
  396. UseMinimumSize = true
  397. };
  398. sliderFGColor.Style.SetChar = sliderFGColor.Style.SetChar with { Attribute = new Attribute (Color.BrightGreen, Color.Black) };
  399. sliderFGColor.Style.LegendAttributes.SetAttribute = new Attribute (Color.Green, Color.Blue);
  400. List<SliderOption<(Color, Color)>> colorOptions = new ();
  401. foreach (ColorName colorIndex in Enum.GetValues<ColorName> ())
  402. {
  403. var colorName = colorIndex.ToString ();
  404. colorOptions.Add (
  405. new SliderOption<(Color, Color)>
  406. {
  407. Data = (new Color (colorIndex),
  408. new Color (colorIndex)),
  409. Legend = colorName,
  410. LegendAbbr = (Rune)colorName [0]
  411. }
  412. );
  413. }
  414. sliderFGColor.Options = colorOptions;
  415. configView.Add (sliderFGColor);
  416. sliderFGColor.OptionsChanged += (sender, e) =>
  417. {
  418. if (e.Options.Count != 0)
  419. {
  420. (Color, Color) data = e.Options.First ().Value.Data;
  421. foreach (Slider s in app.Subviews.OfType<Slider> ())
  422. {
  423. s.ColorScheme = new ColorScheme (s.ColorScheme);
  424. s.ColorScheme = new ColorScheme (s.ColorScheme)
  425. {
  426. Normal = new Attribute (
  427. data.Item2,
  428. s.ColorScheme.Normal.Background
  429. )
  430. };
  431. s.Style.OptionChar = s.Style.OptionChar with
  432. {
  433. Attribute = new Attribute (data.Item1, s.ColorScheme.Normal.Background)
  434. };
  435. s.Style.SetChar = s.Style.SetChar with
  436. {
  437. Attribute = new Attribute (
  438. data.Item1,
  439. s.Style.SetChar.Attribute?.Background
  440. ?? s.ColorScheme.Normal.Background
  441. )
  442. };
  443. s.Style.LegendAttributes.SetAttribute = new Attribute (data.Item1, s.ColorScheme.Normal.Background);
  444. s.Style.RangeChar = s.Style.RangeChar with
  445. {
  446. Attribute = new Attribute (data.Item1, s.ColorScheme.Normal.Background)
  447. };
  448. s.Style.SpaceChar = s.Style.SpaceChar with
  449. {
  450. Attribute = new Attribute (data.Item1, s.ColorScheme.Normal.Background)
  451. };
  452. s.Style.LegendAttributes.NormalAttribute =
  453. new Attribute (data.Item1, s.ColorScheme.Normal.Background);
  454. }
  455. }
  456. };
  457. Slider<(Color, Color)> sliderBGColor = new ()
  458. {
  459. Title = "BG Color",
  460. X = Pos.Right (sliderFGColor),
  461. Y = Pos.Top (sliderFGColor),
  462. Type = SliderType.Single,
  463. BorderStyle = LineStyle.Single,
  464. AllowEmpty = false,
  465. Orientation = Orientation.Vertical,
  466. LegendsOrientation = Orientation.Horizontal,
  467. MinimumInnerSpacing = 0,
  468. UseMinimumSize = true
  469. };
  470. sliderBGColor.Style.SetChar = sliderBGColor.Style.SetChar with { Attribute = new Attribute (Color.BrightGreen, Color.Black) };
  471. sliderBGColor.Style.LegendAttributes.SetAttribute = new Attribute (Color.Green, Color.Blue);
  472. sliderBGColor.Options = colorOptions;
  473. configView.Add (sliderBGColor);
  474. sliderBGColor.OptionsChanged += (sender, e) =>
  475. {
  476. if (e.Options.Count != 0)
  477. {
  478. (Color, Color) data = e.Options.First ().Value.Data;
  479. foreach (Slider s in app.Subviews.OfType<Slider> ())
  480. {
  481. s.ColorScheme = new ColorScheme (s.ColorScheme)
  482. {
  483. Normal = new Attribute (
  484. s.ColorScheme.Normal.Foreground,
  485. data.Item2
  486. )
  487. };
  488. }
  489. }
  490. };
  491. #endregion Color Slider
  492. #endregion Config Slider
  493. ObservableCollection<string> eventSource = new ();
  494. var eventLog = new ListView
  495. {
  496. X = Pos.Right (sliderBGColor),
  497. Y = Pos.Bottom (spacingOptions),
  498. Width = Dim.Fill (),
  499. Height = Dim.Fill (),
  500. ColorScheme = Colors.ColorSchemes ["Toplevel"],
  501. Source = new ListWrapper<string> (eventSource)
  502. };
  503. configView.Add (eventLog);
  504. foreach (Slider slider in app.Subviews.Where (v => v is Slider)!)
  505. {
  506. slider.Accept += (o, args) =>
  507. {
  508. eventSource.Add ($"Accept: {string.Join(",", slider.GetSetOptions ())}");
  509. eventLog.MoveDown ();
  510. args.Handled = true;
  511. };
  512. slider.OptionsChanged += (o, args) =>
  513. {
  514. eventSource.Add ($"OptionsChanged: {string.Join (",", slider.GetSetOptions ())}");
  515. eventLog.MoveDown ();
  516. args.Cancel = true;
  517. };
  518. }
  519. app.FocusFirst (null);
  520. Application.Run (app);
  521. app.Dispose ();
  522. Application.Shutdown ();
  523. }
  524. }