Files
spark/src/client/spark/Modules/Support/Browser/BrowserWindow.cs
T

103 lines
3.4 KiB
C#

using mbts;
using mbts.config;
using mbts.ui;
using mbts.ui.shell;
using System;
using System.Windows.Controls;
namespace bts.shell {
public class BrowserWindow : IShellWindow {
const string _window_type = "Browser";
readonly WebBrowser _browser;
readonly ISaveWindow<BrowserWindowState> _isw;
public BrowserWindow(Uri uri, ISaveWindow<BrowserWindowState> isw) {
_isw = isw;
this.Owner = isw.module;
this.Title = "Loading...";
initial_uri = uri;
this.uri = coerce_uri(uri);
_browser = new WebBrowser() { Width = 1024 };
_browser.Navigated += _browser_Navigated;
_browser.Navigate(this.uri);
_browser.LoadCompleted += _browser_Navigated;
}
public Uri uri { get; private set; }
public Uri initial_uri { get; private set; }
void _browser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e) {
uri = e.Uri;
dynamic doc = _browser.Document;
if (doc != null) {
try {
Title = doc.Title;
TitleChanged();
//doc.document.is_bts_shell = true;
}
catch { }
}
}
private Uri coerce_uri(Uri uri) {
if (uri.Scheme == "bts") {
string uristr = uri.ToString();
string http_base =
"http://{0}:{1}/".fmt(
Configuration.get<bts.config.System>().config_host,
Configuration.get<bts.config.System>().config_port);
string temp = uristr;
if (uristr.StartsWith("bts://m/")) {
temp = uristr.Replace("bts://m/", http_base + "docs/");
}
else {
temp = uristr.Replace("bts://", http_base);
}
return new Uri(temp, UriKind.Absolute);
}
return uri;
}
#region IShellWindow
public string Title { get; set; }
public event Action TitleChanged = () => { };
public System.Windows.Media.ImageSource Icon { get { return Icons.Browser; } }
public event Action IconChanged = () => { };
public string window_type => _window_type;
public IShellModule Owner {get; set; }
public ShellWindowUI GetContent() { return new ShellWindowUI(_browser); }
public void GetState(System.IO.Stream save_here) {
_isw.save(new BrowserWindowState() { uri = uri.ToString() }, save_here);
}
public void on_shell_window_vm(ShellWindowVM shell_window_vm) { }
public void Dispose() {
if (_browser != null) {
_browser.Dispose();
}
}
#endregion
#region Fixup Browser control to use IE11 if available
static BrowserWindow() {
string ke_name = @"Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION";
var emulation_key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(ke_name, true);
if (emulation_key == null) {
emulation_key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(ke_name);
}
var process_name = Configuration.get<bts.config.System>().process_name;
emulation_key.SetValue(process_name, 11001); // IE11
}
#endregion
}
}