-
Notifications
You must be signed in to change notification settings - Fork 36
/
PreviewToolWindow.cs
110 lines (95 loc) · 3.23 KB
/
PreviewToolWindow.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
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Shell;
using System.Windows.Controls;
namespace MarkdownMode
{
[Guid("acd82a5f-9c35-400b-b9d0-f97925f3b312")]
public class MarkdownPreviewToolWindow : ToolWindowPane
{
private WebBrowser browser;
const string EmptyWindowHtml = "Open a markdown file to see a preview.";
int? scrollBackTo = null;
/// <summary>
/// Standard constructor for the tool window.
/// </summary>
public MarkdownPreviewToolWindow() : base(null)
{
this.Caption = "Markdown Preview";
this.BitmapResourceID = 301;
this.BitmapIndex = 1;
browser = new WebBrowser();
browser.NavigateToString(EmptyWindowHtml);
browser.LoadCompleted += (sender, args) =>
{
if (scrollBackTo.HasValue)
{
var document = browser.Document as mshtml.IHTMLDocument2;
if (document != null)
{
var element = document.body as mshtml.IHTMLElement2;
if (element != null)
{
element.scrollTop = scrollBackTo.Value;
}
}
}
scrollBackTo = null;
};
}
public override object Content
{
get { return browser; }
}
bool isVisible = false;
public bool IsVisible
{
get { return isVisible; }
}
public override void OnToolWindowCreated()
{
isVisible = true;
}
public void SetPreviewContent(object source, string html, string title)
{
this.Caption = "Markdown Preview - " + title;
if (source == CurrentSource)
{
// If the scroll back to already has a value, it means the current content hasn't finished loading yet,
// so the current scroll position isn't ready for us to use. Just use the existing scroll position.
if (!scrollBackTo.HasValue)
{
var document = browser.Document as mshtml.IHTMLDocument2;
if (document != null)
{
var element = document.body as mshtml.IHTMLElement2;
if (element != null)
{
scrollBackTo = element.scrollTop;
}
}
}
}
else
{
scrollBackTo = null;
}
CurrentSource = source;
browser.NavigateToString(html);
}
public void ClearPreviewContent()
{
this.Caption = "Markdown Preview";
scrollBackTo = null;
CurrentSource = null;
browser.NavigateToString(EmptyWindowHtml);
}
public object CurrentSource { get; private set; }
}
}