MainWindow.xaml.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. namespace Xilium.CefGlue.Demo.WPF
  5. {
  6. /// <summary>
  7. /// Interaction logic for MainWindow.xaml
  8. /// </summary>
  9. public partial class MainWindow : Window
  10. {
  11. public MainWindow()
  12. {
  13. InitializeComponent();
  14. CreateNewTab();
  15. #if WINDOWLESS
  16. Title += " - OSR mode";
  17. #endif
  18. }
  19. private BrowserView ActiveBrowserView => (BrowserView) tabControl.SelectedContent;
  20. private void CreateNewTab()
  21. {
  22. var view = new BrowserView();
  23. var tab = new TabItem();
  24. var headerPanel = new DockPanel();
  25. tab.Header = headerPanel;
  26. var closeButton = new Button()
  27. {
  28. Content = "X",
  29. Padding = new Thickness(2),
  30. Margin = new Thickness(5, 0, 0, 0)
  31. };
  32. closeButton.Click += delegate
  33. {
  34. view.Dispose();
  35. tabControl.Items.Remove(tab);
  36. };
  37. DockPanel.SetDock(closeButton, Dock.Right);
  38. var tabTitle = new TextBlock()
  39. {
  40. Text = "New Tab"
  41. };
  42. headerPanel.Children.Add(tabTitle);
  43. headerPanel.Children.Add(closeButton);
  44. view.TitleChanged += title =>
  45. {
  46. Application.Current.Dispatcher.BeginInvoke((Action)(() =>
  47. {
  48. tabTitle.Text = title;
  49. tab.ToolTip = title;
  50. }));
  51. };
  52. tab.Content = view;
  53. tabControl.Items.Add(tab);
  54. }
  55. private void OnNewTabMenuItemClick(object sender, RoutedEventArgs e)
  56. {
  57. CreateNewTab();
  58. }
  59. private void OnCloseTabMenuItemClick(object sender, RoutedEventArgs e)
  60. {
  61. if (tabControl.SelectedIndex >= 0)
  62. {
  63. var browser = ActiveBrowserView;
  64. tabControl.Items.RemoveAt(tabControl.SelectedIndex);
  65. browser.Dispose();
  66. }
  67. }
  68. private void OnEvaluateJavascriptMenuItemClick(object sender, RoutedEventArgs e)
  69. {
  70. ActiveBrowserView.EvaluateJavascript();
  71. }
  72. private void OnBindJavascriptObjectMenuItemClick(object sender, RoutedEventArgs e)
  73. {
  74. ActiveBrowserView.BindJavascriptObject();
  75. }
  76. private void OnOpenDevToolsMenuItemClick(object sender, RoutedEventArgs e)
  77. {
  78. ActiveBrowserView.OpenDevTools();
  79. }
  80. }
  81. }