Program.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. // Copyright (c) The Perspex Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. using System.IO;
  5. using System.Reactive.Linq;
  6. using Perspex;
  7. using Perspex.Animation;
  8. using Perspex.Collections;
  9. using Perspex.Controls;
  10. using Perspex.Controls.Html;
  11. using Perspex.Controls.Primitives;
  12. using Perspex.Controls.Shapes;
  13. using Perspex.Controls.Templates;
  14. using Perspex.Diagnostics;
  15. using Perspex.Layout;
  16. using Perspex.Media;
  17. using Perspex.Media.Imaging;
  18. #if PERSPEX_GTK
  19. using Perspex.Gtk;
  20. #endif
  21. using ReactiveUI;
  22. namespace TestApplication
  23. {
  24. internal class Program
  25. {
  26. private static readonly PerspexList<Node> s_treeData = new PerspexList<Node>
  27. {
  28. new Node
  29. {
  30. Name = "Root 1",
  31. Children = new PerspexList<Node>
  32. {
  33. new Node
  34. {
  35. Name = "Child 1",
  36. },
  37. new Node
  38. {
  39. Name = "Child 2",
  40. Children = new PerspexList<Node>
  41. {
  42. new Node
  43. {
  44. Name = "Grandchild 1",
  45. },
  46. new Node
  47. {
  48. Name = "Grandmaster Flash",
  49. },
  50. }
  51. },
  52. new Node
  53. {
  54. Name = "Child 3",
  55. },
  56. }
  57. },
  58. new Node
  59. {
  60. Name = "Root 2",
  61. },
  62. };
  63. private static readonly PerspexList<Item> s_listBoxData = new PerspexList<Item>
  64. {
  65. new Item { Name = "Item 1", Value = "Item 1 Value" },
  66. new Item { Name = "Item 2", Value = "Item 2 Value" },
  67. new Item { Name = "Item 3", Value = "Item 3 Value" },
  68. new Item { Name = "Item 4", Value = "Item 4 Value" },
  69. new Item { Name = "Item 5", Value = "Item 5 Value" },
  70. new Item { Name = "Item 6", Value = "Item 6 Value" },
  71. new Item { Name = "Item 7", Value = "Item 7 Value" },
  72. new Item { Name = "Item 8", Value = "Item 8 Value" },
  73. };
  74. private static void Main(string[] args)
  75. {
  76. // The version of ReactiveUI currently included is for WPF and so expects a WPF
  77. // dispatcher. This makes sure it's initialized.
  78. System.Windows.Threading.Dispatcher foo = System.Windows.Threading.Dispatcher.CurrentDispatcher;
  79. App application = new App
  80. {
  81. DataTemplates = new DataTemplates
  82. {
  83. new TreeDataTemplate<Node>(
  84. x => new TextBlock { Text = x.Name },
  85. x => x.Children,
  86. x => true),
  87. },
  88. };
  89. TextBlock fps;
  90. var testCommand = ReactiveCommand.Create();
  91. testCommand.Subscribe(_ => System.Diagnostics.Debug.WriteLine("Test command executed."));
  92. TabControl container;
  93. Window window = new Window
  94. {
  95. Title = "Perspex Test Application",
  96. Width = 800,
  97. Height = 300,
  98. Content = new Grid
  99. {
  100. ColumnDefinitions = new ColumnDefinitions
  101. {
  102. new ColumnDefinition(1, GridUnitType.Star),
  103. new ColumnDefinition(1, GridUnitType.Star),
  104. },
  105. RowDefinitions = new RowDefinitions
  106. {
  107. new RowDefinition(GridLength.Auto),
  108. new RowDefinition(1, GridUnitType.Star),
  109. new RowDefinition(GridLength.Auto),
  110. },
  111. Children = new Controls
  112. {
  113. (container = new TabControl
  114. {
  115. Padding = new Thickness(5),
  116. Items = new[]
  117. {
  118. ButtonsTab(),
  119. TextTab(),
  120. HtmlTab(),
  121. ImagesTab(),
  122. ListsTab(),
  123. LayoutTab(),
  124. AnimationsTab(),
  125. },
  126. Transition = new CrossFade(TimeSpan.FromSeconds(0.25)),
  127. [Grid.RowProperty] = 1,
  128. [Grid.ColumnSpanProperty] = 2,
  129. })
  130. }
  131. },
  132. };
  133. container.Classes.Add(":container");
  134. window.Show();
  135. Application.Current.Run(window);
  136. }
  137. private static TabItem ButtonsTab()
  138. {
  139. Button defaultButton;
  140. var showDialog = ReactiveCommand.Create();
  141. Button showDialogButton;
  142. var result = new TabItem
  143. {
  144. Header = "Button",
  145. Content = new StackPanel
  146. {
  147. Margin = new Thickness(10),
  148. Orientation = Orientation.Vertical,
  149. Gap = 4,
  150. Children = new Controls
  151. {
  152. new TextBlock
  153. {
  154. Text = "Button",
  155. FontWeight = FontWeight.Medium,
  156. FontSize = 22,
  157. Foreground = SolidColorBrush.Parse("#212121"),
  158. },
  159. new TextBlock
  160. {
  161. Text = "A button control",
  162. FontSize = 14,
  163. Foreground = SolidColorBrush.Parse("#727272"),
  164. Margin = new Thickness(0, 0, 0, 10)
  165. },
  166. new Button
  167. {
  168. Width = 150,
  169. Content = "Button"
  170. },
  171. new Button
  172. {
  173. Width = 150,
  174. Content = "Disabled",
  175. IsEnabled = false,
  176. },
  177. new TabControl
  178. {
  179. Margin = new Thickness(0, 20, 0, 0),
  180. Items = new []
  181. {
  182. new TabItem
  183. {
  184. Header = new TextBlock { FontWeight = FontWeight.Medium, Text = "CSHARP" },
  185. Content = new HtmlLabel
  186. {
  187. Text = "CSHRP CODEZ"
  188. }
  189. },
  190. new TabItem
  191. {
  192. Header = new TextBlock { FontWeight = FontWeight.Medium, Text = "XAML" },
  193. Content = new HtmlLabel
  194. {
  195. Text = "XAML CODEZ"
  196. }
  197. }
  198. }
  199. }
  200. }
  201. },
  202. };
  203. return result;
  204. }
  205. private static TabItem HtmlTab()
  206. {
  207. var htmlText =
  208. new StreamReader(typeof (Program).Assembly.GetManifestResourceStream("TestApplication.html.htm"))
  209. .ReadToEnd();
  210. return new TabItem
  211. {
  212. Header = "HTML Label",
  213. Content = new ScrollViewer()
  214. {
  215. Width = 600,
  216. MaxHeight = 600,
  217. HorizontalAlignment = HorizontalAlignment.Center,
  218. CanScrollHorizontally = false,
  219. VerticalScrollBarVisibility = ScrollBarVisibility.Visible,
  220. Content =
  221. new HtmlLabel()
  222. {
  223. Text = htmlText
  224. }
  225. }
  226. };
  227. }
  228. private static TabItem TextTab()
  229. {
  230. return new TabItem
  231. {
  232. Header = "Input",
  233. Content = new StackPanel
  234. {
  235. Margin = new Thickness(10),
  236. Orientation = Orientation.Vertical,
  237. Gap = 4,
  238. Children = new Controls
  239. {
  240. new TextBlock
  241. {
  242. Text = "Check box",
  243. FontWeight = FontWeight.Medium,
  244. FontSize = 22,
  245. Foreground = SolidColorBrush.Parse("#212121"),
  246. },
  247. new TextBlock
  248. {
  249. Text = "A check box control",
  250. FontSize = 14,
  251. Foreground = SolidColorBrush.Parse("#373749"),
  252. Margin = new Thickness(0, 0, 0, 10)
  253. },
  254. new CheckBox { IsChecked = true, Margin = new Thickness(0, 0, 0, 5), Content = "Checked" },
  255. new CheckBox { IsChecked = false, Content = "Unchecked" },
  256. new TabControl
  257. {
  258. Margin = new Thickness(0, 20, 0, 0),
  259. Items = new []
  260. {
  261. new TabItem
  262. {
  263. Header = new TextBlock { FontWeight = FontWeight.Medium, Text = "CSHARP" },
  264. Content = new HtmlLabel
  265. {
  266. Text = "CSHRP CODEZ"
  267. }
  268. },
  269. new TabItem
  270. {
  271. Header = new TextBlock { FontWeight = FontWeight.Medium, Text = "XAML" },
  272. Content = new HtmlLabel
  273. {
  274. Text = "XAML CODEZ"
  275. }
  276. }
  277. }
  278. },
  279. new TextBlock
  280. {
  281. Margin = new Thickness(0, 40, 0, 0),
  282. Text = "Radio button",
  283. FontWeight = FontWeight.Medium,
  284. FontSize = 22,
  285. Foreground = SolidColorBrush.Parse("#373749"),
  286. },
  287. new TextBlock
  288. {
  289. Text = "A radio button control",
  290. FontSize = 14,
  291. Foreground = SolidColorBrush.Parse("#373749"),
  292. Margin = new Thickness(0, 0, 0, 10)
  293. },
  294. new RadioButton { IsChecked = true, Margin = new Thickness(0, 0, 0, 5), Content = "Option 1" },
  295. new RadioButton { IsChecked = false, Content = "Option 2" },
  296. new RadioButton { IsChecked = false, Content = "Option 3" },
  297. new TabControl
  298. {
  299. Margin = new Thickness(0, 20, 0, 0),
  300. Items = new []
  301. {
  302. new TabItem
  303. {
  304. Header = new TextBlock { FontWeight = FontWeight.Medium, Text = "CSHARP" },
  305. Content = new HtmlLabel
  306. {
  307. Text = "CSHRP CODEZ"
  308. }
  309. },
  310. new TabItem
  311. {
  312. Header = new TextBlock { FontWeight = FontWeight.Medium, Text = "XAML" },
  313. Content = new HtmlLabel
  314. {
  315. Text = "XAML CODEZ"
  316. }
  317. }
  318. }
  319. }
  320. }
  321. }
  322. };
  323. }
  324. private static TabItem ImagesTab()
  325. {
  326. ScrollBar size;
  327. return new TabItem
  328. {
  329. Header = "Images",
  330. Content = new StackPanel
  331. {
  332. Orientation = Orientation.Vertical,
  333. HorizontalAlignment = HorizontalAlignment.Center,
  334. VerticalAlignment = VerticalAlignment.Center,
  335. Gap = 8,
  336. Children = new Controls
  337. {
  338. (size = new ScrollBar
  339. {
  340. Minimum = 100,
  341. Maximum = 400,
  342. Value = 100,
  343. Orientation = Orientation.Horizontal,
  344. }),
  345. new ScrollViewer
  346. {
  347. Width = 200,
  348. Height = 200,
  349. CanScrollHorizontally = true,
  350. Content = new Image
  351. {
  352. Source = new Bitmap("github_icon.png"),
  353. [!Layoutable.WidthProperty] = size[!RangeBase.ValueProperty],
  354. [!Layoutable.HeightProperty] = size[!RangeBase.ValueProperty],
  355. },
  356. },
  357. new ProgressBar
  358. {
  359. [!RangeBase.MinimumProperty] = size[!RangeBase.MinimumProperty],
  360. [!RangeBase.MaximumProperty] = size[!RangeBase.MaximumProperty],
  361. [!RangeBase.ValueProperty] = size[!RangeBase.ValueProperty],
  362. }
  363. }
  364. },
  365. };
  366. }
  367. private static TabItem ListsTab()
  368. {
  369. ListBox listBox;
  370. return new TabItem
  371. {
  372. Header = "Lists",
  373. Content = new StackPanel
  374. {
  375. DataTemplates = new DataTemplates
  376. {
  377. new DataTemplate<Item>(x =>
  378. new StackPanel
  379. {
  380. Children = new Controls
  381. {
  382. new TextBlock { Text = x.Name, FontSize = 24 },
  383. new TextBlock { Text = x.Value },
  384. }
  385. })
  386. },
  387. Orientation = Orientation.Horizontal,
  388. HorizontalAlignment = HorizontalAlignment.Center,
  389. VerticalAlignment = VerticalAlignment.Center,
  390. Gap = 8,
  391. Children = new Controls
  392. {
  393. new TreeView
  394. {
  395. Name = "treeView",
  396. Items = s_treeData,
  397. },
  398. (listBox = new ListBox
  399. {
  400. Items = s_listBoxData,
  401. MaxHeight = 300,
  402. }),
  403. new DropDown
  404. {
  405. Items = s_listBoxData,
  406. SelectedItem = s_listBoxData[0],
  407. VerticalAlignment = VerticalAlignment.Center,
  408. }
  409. }
  410. },
  411. };
  412. }
  413. private static TabItem LayoutTab()
  414. {
  415. return new TabItem
  416. {
  417. Header = "Layout",
  418. Content = new Grid
  419. {
  420. ColumnDefinitions = new ColumnDefinitions
  421. {
  422. new ColumnDefinition(1, GridUnitType.Star),
  423. new ColumnDefinition(1, GridUnitType.Star),
  424. },
  425. Margin = new Thickness(50),
  426. Children = new Controls
  427. {
  428. new StackPanel
  429. {
  430. Orientation = Orientation.Vertical,
  431. Gap = 8,
  432. Children = new Controls
  433. {
  434. new Button { HorizontalAlignment = HorizontalAlignment.Left, Content = "Left Aligned" },
  435. new Button { HorizontalAlignment = HorizontalAlignment.Center, Content = "Center Aligned" },
  436. new Button { HorizontalAlignment = HorizontalAlignment.Right, Content = "Right Aligned" },
  437. new Button { HorizontalAlignment = HorizontalAlignment.Stretch, Content = "Stretch" },
  438. },
  439. [Grid.ColumnProperty] = 0,
  440. },
  441. new StackPanel
  442. {
  443. Orientation = Orientation.Horizontal,
  444. Gap = 8,
  445. Children = new Controls
  446. {
  447. new Button { VerticalAlignment = VerticalAlignment.Top, Content = "Top Aligned" },
  448. new Button { VerticalAlignment = VerticalAlignment.Center, Content = "Center Aligned" },
  449. new Button { VerticalAlignment = VerticalAlignment.Bottom, Content = "Bottom Aligned" },
  450. new Button { VerticalAlignment = VerticalAlignment.Stretch, Content = "Stretch" },
  451. },
  452. [Grid.ColumnProperty] = 1,
  453. },
  454. },
  455. }
  456. };
  457. }
  458. private static TabItem AnimationsTab()
  459. {
  460. Border border1;
  461. Border border2;
  462. RotateTransform rotate;
  463. Button button1;
  464. var result = new TabItem
  465. {
  466. Header = "Animations",
  467. Content = new Grid
  468. {
  469. ColumnDefinitions = new ColumnDefinitions
  470. {
  471. new ColumnDefinition(1, GridUnitType.Star),
  472. new ColumnDefinition(1, GridUnitType.Star),
  473. },
  474. RowDefinitions = new RowDefinitions
  475. {
  476. new RowDefinition(1, GridUnitType.Star),
  477. new RowDefinition(GridLength.Auto),
  478. },
  479. Children = new Controls
  480. {
  481. (border1 = new Border
  482. {
  483. Width = 100,
  484. Height = 100,
  485. HorizontalAlignment = HorizontalAlignment.Center,
  486. VerticalAlignment = VerticalAlignment.Center,
  487. Background = Brushes.Crimson,
  488. RenderTransform = new RotateTransform(),
  489. Child = new TextBox
  490. {
  491. Background = Brushes.White,
  492. Text = "Hello!",
  493. HorizontalAlignment = HorizontalAlignment.Center,
  494. VerticalAlignment = VerticalAlignment.Center,
  495. },
  496. }),
  497. (border2 = new Border
  498. {
  499. Width = 100,
  500. Height = 100,
  501. HorizontalAlignment = HorizontalAlignment.Center,
  502. VerticalAlignment = VerticalAlignment.Center,
  503. Background = Brushes.Coral,
  504. Child = new Image
  505. {
  506. Source = new Bitmap("github_icon.png"),
  507. HorizontalAlignment = HorizontalAlignment.Center,
  508. VerticalAlignment = VerticalAlignment.Center,
  509. },
  510. RenderTransform = (rotate = new RotateTransform
  511. {
  512. PropertyTransitions = new PropertyTransitions
  513. {
  514. RotateTransform.AngleProperty.Transition(500),
  515. }
  516. }),
  517. PropertyTransitions = new PropertyTransitions
  518. {
  519. Layoutable.WidthProperty.Transition(300),
  520. Layoutable.HeightProperty.Transition(1000),
  521. },
  522. [Grid.ColumnProperty] = 1,
  523. }),
  524. (button1 = new Button
  525. {
  526. HorizontalAlignment = HorizontalAlignment.Center,
  527. Content = "Animate",
  528. [Grid.ColumnProperty] = 1,
  529. [Grid.RowProperty] = 1,
  530. }),
  531. },
  532. },
  533. };
  534. button1.Click += (s, e) =>
  535. {
  536. if (border2.Width == 100)
  537. {
  538. border2.Width = border2.Height = 400;
  539. rotate.Angle = 180;
  540. }
  541. else
  542. {
  543. border2.Width = border2.Height = 100;
  544. rotate.Angle = 0;
  545. }
  546. };
  547. var start = Animate.Stopwatch.Elapsed;
  548. var degrees = Animate.Timer
  549. .Select(x =>
  550. {
  551. var elapsed = (x - start).TotalSeconds;
  552. var cycles = elapsed / 4;
  553. var progress = cycles % 1;
  554. return 360.0 * progress;
  555. });
  556. border1.RenderTransform.Bind(
  557. RotateTransform.AngleProperty,
  558. degrees,
  559. BindingPriority.Animation);
  560. return result;
  561. }
  562. }
  563. }