forked from cefsharp/CefSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
218 lines (177 loc) · 7.26 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Copyright © 2010-2017 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
using System;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using CefSharp.Example;
using CefSharp.Wpf.Example.Controls;
using CefSharp.Wpf.Example.ViewModels;
using Microsoft.Win32;
namespace CefSharp.Wpf.Example
{
public partial class MainWindow : Window
{
private const string DefaultUrlForAddedTabs = "https://www.google.com";
public ObservableCollection<BrowserTabViewModel> BrowserTabs { get; set; }
public MainWindow()
{
InitializeComponent();
DataContext = this;
BrowserTabs = new ObservableCollection<BrowserTabViewModel>();
CommandBindings.Add(new CommandBinding(ApplicationCommands.New, OpenNewTab));
CommandBindings.Add(new CommandBinding(ApplicationCommands.Close, CloseTab));
CommandBindings.Add(new CommandBinding(CefSharpCommands.Exit, Exit));
CommandBindings.Add(new CommandBinding(CefSharpCommands.OpenTabCommand, OpenTabCommandBinding));
CommandBindings.Add(new CommandBinding(CefSharpCommands.PrintTabToPdfCommand, PrintToPdfCommandBinding));
CommandBindings.Add(new CommandBinding(CefSharpCommands.CustomCommand, CustomCommandBinding));
Loaded += MainWindowLoaded;
var bitness = Environment.Is64BitProcess ? "x64" : "x86";
Title += " - " + bitness;
}
private void CloseTab(object sender, ExecutedRoutedEventArgs e)
{
if (BrowserTabs.Count > 0)
{
//Obtain the original source element for this event
var originalSource = (FrameworkElement)e.OriginalSource;
BrowserTabViewModel browserViewModel;
if (originalSource is MainWindow)
{
browserViewModel = BrowserTabs[TabControl.SelectedIndex];
BrowserTabs.RemoveAt(TabControl.SelectedIndex);
}
else
{
//Remove the matching DataContext from the BrowserTabs collection
browserViewModel = (BrowserTabViewModel)originalSource.DataContext;
BrowserTabs.Remove(browserViewModel);
}
browserViewModel.WebBrowser.Dispose();
}
}
private void OpenNewTab(object sender, ExecutedRoutedEventArgs e)
{
CreateNewTab();
TabControl.SelectedIndex = TabControl.Items.Count - 1;
}
private void MainWindowLoaded(object sender, RoutedEventArgs e)
{
CreateNewTab(CefExample.DefaultUrl, true);
}
private void CreateNewTab(string url = DefaultUrlForAddedTabs, bool showSideBar = false)
{
BrowserTabs.Add(new BrowserTabViewModel(url) { ShowSidebar = showSideBar });
}
private void CustomCommandBinding(object sender, ExecutedRoutedEventArgs e)
{
var param = e.Parameter.ToString();
if (BrowserTabs.Count > 0)
{
var originalSource = (FrameworkElement)e.OriginalSource;
//TODO: Remove duplicate code
BrowserTabViewModel browserViewModel;
if (originalSource is MainWindow)
{
browserViewModel = BrowserTabs[TabControl.SelectedIndex];
}
else
{
browserViewModel = (BrowserTabViewModel)originalSource.DataContext;
}
if (param == "CustomRequest")
{
browserViewModel.LoadCustomRequestExample();
}
if(param == "OpenDevTools")
{
browserViewModel.WebBrowser.ShowDevTools();
}
if (param == "ZoomIn")
{
var cmd = browserViewModel.WebBrowser.ZoomInCommand;
cmd.Execute(null);
}
if (param == "ZoomOut")
{
var cmd = browserViewModel.WebBrowser.ZoomOutCommand;
cmd.Execute(null);
}
if (param == "ZoomReset")
{
var cmd = browserViewModel.WebBrowser.ZoomResetCommand;
cmd.Execute(null);
}
if (param == "ToggleSidebar")
{
browserViewModel.ShowSidebar = !browserViewModel.ShowSidebar;
}
if (param == "ToggleDownloadInfo")
{
browserViewModel.ShowDownloadInfo = !browserViewModel.ShowDownloadInfo;
}
//NOTE: Add as required
//else if (param == "CustomRequest123")
//{
// browserViewModel.LoadCustomRequestExample();
//}
}
}
private async void PrintToPdfCommandBinding(object sender, ExecutedRoutedEventArgs e)
{
if (BrowserTabs.Count > 0)
{
var originalSource = (FrameworkElement)e.OriginalSource;
BrowserTabViewModel browserViewModel;
if (originalSource is MainWindow)
{
browserViewModel = BrowserTabs[TabControl.SelectedIndex];
}
else
{
browserViewModel = (BrowserTabViewModel)originalSource.DataContext;
}
var dialog = new SaveFileDialog
{
DefaultExt = ".pdf",
Filter = "Pdf documents (.pdf)|*.pdf"
};
if (dialog.ShowDialog() == true)
{
var success = await browserViewModel.WebBrowser.PrintToPdfAsync(dialog.FileName, new PdfPrintSettings
{
MarginType = CefPdfPrintMarginType.Custom,
MarginBottom = 10,
MarginTop = 0,
MarginLeft = 20,
MarginRight = 10,
});
if(success)
{
MessageBox.Show("Pdf was saved to " + dialog.FileName);
}
else
{
MessageBox.Show("Unable to save Pdf, check you have write permissions to " + dialog.FileName);
}
}
}
}
private void OpenTabCommandBinding(object sender, ExecutedRoutedEventArgs e)
{
var url = e.Parameter.ToString();
if (string.IsNullOrEmpty(url))
{
throw new Exception("Please provide a valid command parameter for binding");
}
CreateNewTab(url, true);
TabControl.SelectedIndex = TabControl.Items.Count - 1;
}
private void Exit(object sender, ExecutedRoutedEventArgs e)
{
Close();
}
}
}