This repository has been archived by the owner on Aug 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathToolOptions.cs
198 lines (173 loc) · 5.86 KB
/
ToolOptions.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
using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms.Design;
using System.Xml;
using System.Xml.Serialization;
using Microsoft.VisualStudio.Shell;
namespace VSWindowTitleChanger
{
public enum EExtensionActivationRule
{
AlwaysActive,
AlwaysInactive,
ActiveWithMultipleVSInstances,
ActiveWithMultipleVSInstancesOfTheSameVersion,
}
public interface ISerializedOptions
{
EExtensionActivationRule ExtensionActivationRule { get; set; }
TitleSetup TitleSetup { get; set; }
bool Debug { get; set; }
}
[ClassInterface(ClassInterfaceType.AutoDual)]
[ComVisible(true)]
public class ToolOptions : DialogPage, ISerializedOptions
{
EExtensionActivationRule m_ExtensionActivationRule;
TitleSetup m_TitleSetup;
bool m_Debug;
[Category("Window Title Changer Options")]
[DisplayName("Extension Activation")]
[Description("Turn on/off the extension. You can also ask it to be active only in case of multiple Visual Studio instances.")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public EExtensionActivationRule ExtensionActivationRule { get { return m_ExtensionActivationRule; } set { m_ExtensionActivationRule = value; } }
[Category("Window Title Changer Options")]
[DisplayName("Window Title Setup")]
[Description("A simple or complex expression that evaluates into the title text of the Visual Studio main window.")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public TitleSetup TitleSetup { get { return m_TitleSetup; } set { m_TitleSetup = value; } }
[Category("Window Title Changer Options")]
[DisplayName("Debug")]
[Description("Turns on/off debug output. May come handy if your plugin crashes/misbehaves and you want to investigate the reasons.")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public bool Debug { get { return m_Debug; } set { m_Debug = value; } }
public override void ResetSettings()
{
CopyOptions(new SerializedOptions(), this);
Fixup();
}
public class SerializedOptions : ISerializedOptions
{
EExtensionActivationRule m_ExtensionActivationRule;
TitleSetup m_TitleSetup;
bool m_Debug;
public EExtensionActivationRule ExtensionActivationRule { get { return m_ExtensionActivationRule; } set { m_ExtensionActivationRule = value; } }
public TitleSetup TitleSetup { get { return m_TitleSetup; } set { m_TitleSetup = value; } }
public bool Debug { get { return m_Debug; } set { m_Debug = value; } }
}
private static void CopyOptions(ISerializedOptions src, ISerializedOptions dest)
{
foreach (PropertyInfo pi in typeof(ISerializedOptions).GetProperties())
pi.SetValue(dest, pi.GetValue(src, null), null);
}
[Browsable(false)]
public string TitleSetupString
{
get
{
try
{
SerializedOptions options = new SerializedOptions();
CopyOptions(this, options);
XmlSerializer xs = new XmlSerializer(typeof(SerializedOptions));
StringWriter sw = new StringWriter();
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.Indent = false;
settings.NewLineChars = "";
settings.NewLineHandling = NewLineHandling.Entitize;
settings.NewLineOnAttributes = false;
XmlWriter xtw = XmlTextWriter.Create(sw, settings);
xs.Serialize(xtw, options);
return sw.ToString();
}
catch (System.Exception)
{
return "";
}
}
set
{
try
{
if (string.IsNullOrEmpty(value))
{
ResetSettings();
return;
}
XmlSerializer xs = new XmlSerializer(typeof(SerializedOptions));
StringReader sr = new StringReader(value);
SerializedOptions options = (SerializedOptions)xs.Deserialize(sr);
CopyOptions(options, this);
Fixup();
}
catch (System.Exception)
{
}
}
}
private void Fixup()
{
if (m_TitleSetup == null)
m_TitleSetup = new TitleSetup();
else
m_TitleSetup.Fixup();
}
public ToolOptions()
{
Fixup();
}
}
//---------------------------------------------------------------------------------------------
[Editor(typeof(TitleSetupUITypeEditor), typeof(System.Drawing.Design.UITypeEditor))]
[Serializable]
public class TitleSetup : IComparable<TitleSetup>
{
string m_TitleExpression;
public string TitleExpression { get { return m_TitleExpression; } set { m_TitleExpression = value; } }
public void Fixup()
{
if (TitleExpression == null)
TitleExpression = "orig_title";
}
public TitleSetup()
{
Fixup();
}
public override string ToString()
{
return GetType().Name;
}
public virtual int CompareTo(TitleSetup other)
{
return string.Compare(m_TitleExpression, other.m_TitleExpression, true);
}
}
//---------------------------------------------------------------------------------------------
public class TitleSetupUITypeEditor : UITypeEditor
{
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
if (context != null && context.Instance != null && provider != null)
{
IWindowsFormsEditorService edsvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
if (edsvc != null)
{
// Intentionally showing a non-modal dialog to allow the user to
// play around with the IDE/ while changing the plugin settings.
PackageGlobals.Instance().ShowTitleExpressionEditor();
return value;
}
}
return value;
}
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal;
}
}
}