File size: 8,586 Bytes
8c763fb | 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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.Management.Automation;
using System.Windows.Documents;
namespace Microsoft.Management.UI.Internal
{
/// <summary>
/// ViewModel for the Help Dialog used to:
/// build the help document
/// search the help document
/// offer text for labels.
/// </summary>
internal class HelpViewModel : INotifyPropertyChanged
{
/// <summary>
/// The builder for the help FlowDocument Paragraph used in a RichEditText control.
/// </summary>
private readonly HelpParagraphBuilder helpBuilder;
/// <summary>
/// Searcher for selecting current matches in paragraph text.
/// </summary>
private readonly ParagraphSearcher searcher;
/// <summary>
/// Title of the help window.
/// </summary>
private readonly string helpTitle;
/// <summary>
/// the zoom bound to the zoom slider value.
/// </summary>
private double zoom = 100;
/// <summary>
/// Text to be found. This is bound to the find TextBox.
/// </summary>
private string findText;
/// <summary>
/// text for the number of matches found.
/// </summary>
private string matchesLabel;
/// <summary>
/// Initializes a new instance of the HelpViewModel class.
/// </summary>
/// <param name="psObj">Object containing help.</param>
/// <param name="documentParagraph">Paragraph in which help text is built/searched.</param>
internal HelpViewModel(PSObject psObj, Paragraph documentParagraph)
{
Debug.Assert(psObj != null, "ensured by caller");
Debug.Assert(documentParagraph != null, "ensured by caller");
this.helpBuilder = new HelpParagraphBuilder(documentParagraph, psObj);
this.helpBuilder.BuildParagraph();
this.searcher = new ParagraphSearcher();
this.helpBuilder.PropertyChanged += this.HelpBuilder_PropertyChanged;
this.helpTitle = string.Format(
CultureInfo.CurrentCulture,
HelpWindowResources.HelpTitleFormat,
HelpParagraphBuilder.GetPropertyString(psObj, "name"));
}
#region INotifyPropertyChanged Members
/// <summary>
/// Used to notify of property changes.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
#endregion
/// <summary>
/// Gets or sets the Zoom bound to the zoom slider value.
/// </summary>
public double Zoom
{
get
{
return this.zoom;
}
set
{
this.zoom = value;
this.OnNotifyPropertyChanged("Zoom");
this.OnNotifyPropertyChanged("ZoomLabel");
this.OnNotifyPropertyChanged("ZoomLevel");
}
}
/// <summary>
/// Gets the value bound to the RichTextEdit zoom, which is calculated based on the zoom.
/// </summary>
public double ZoomLevel
{
get
{
return this.zoom / 100.0;
}
}
/// <summary>
/// Gets the label to be displayed for the zoom.
/// </summary>
public string ZoomLabel
{
get
{
return string.Format(CultureInfo.CurrentCulture, HelpWindowResources.ZoomLabelTextFormat, this.zoom);
}
}
/// <summary>
/// Gets or sets the text to be found.
/// </summary>
public string FindText
{
get
{
return this.findText;
}
set
{
this.findText = value;
this.Search();
this.SetMatchesLabel();
}
}
/// <summary>
/// Gets the title of the window.
/// </summary>
public string HelpTitle
{
get
{
return this.helpTitle;
}
}
/// <summary>
/// Gets or sets the label for current matches.
/// </summary>
public string MatchesLabel
{
get
{
return this.matchesLabel;
}
set
{
this.matchesLabel = value;
this.OnNotifyPropertyChanged("MatchesLabel");
}
}
/// <summary>
/// Gets a value indicating whether there are matches to go to.
/// </summary>
public bool CanGoToNextOrPrevious
{
get
{
return this.HelpBuilder.HighlightCount != 0;
}
}
/// <summary>
/// Gets the searcher for selecting current matches in paragraph text.
/// </summary>
internal ParagraphSearcher Searcher
{
get { return this.searcher; }
}
/// <summary>
/// Gets the paragraph builder used to write help content.
/// </summary>
internal HelpParagraphBuilder HelpBuilder
{
get { return this.helpBuilder; }
}
/// <summary>
/// Highlights all matches to this.findText
/// Called when findText changes or whenever the search has to be refreshed
/// </summary>
internal void Search()
{
this.HelpBuilder.HighlightAllInstancesOf(this.findText, HelpWindowSettings.Default.HelpSearchMatchCase, HelpWindowSettings.Default.HelpSearchWholeWord);
this.searcher.ResetSearch();
}
/// <summary>
/// Increases Zoom if not above maximum.
/// </summary>
internal void ZoomIn()
{
if (this.Zoom + HelpWindow.ZoomInterval <= HelpWindow.MaximumZoom)
{
this.Zoom += HelpWindow.ZoomInterval;
}
}
/// <summary>
/// Decreases Zoom if not below minimum.
/// </summary>
internal void ZoomOut()
{
if (this.Zoom - HelpWindow.ZoomInterval >= HelpWindow.MinimumZoom)
{
this.Zoom -= HelpWindow.ZoomInterval;
}
}
/// <summary>
/// Called to update the matches label.
/// </summary>
/// <param name="sender">Event sender.</param>
/// <param name="e">Event arguments.</param>
private void HelpBuilder_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "HighlightCount")
{
this.SetMatchesLabel();
this.OnNotifyPropertyChanged("CanGoToNextOrPrevious");
}
}
/// <summary>
/// Sets the current matches label.
/// </summary>
private void SetMatchesLabel()
{
if (this.findText == null || this.findText.Trim().Length == 0)
{
this.MatchesLabel = string.Empty;
}
else
{
if (this.HelpBuilder.HighlightCount == 0)
{
this.MatchesLabel = HelpWindowResources.NoMatches;
}
else
{
if (this.HelpBuilder.HighlightCount == 1)
{
this.MatchesLabel = HelpWindowResources.OneMatch;
}
else
{
this.MatchesLabel = string.Format(
CultureInfo.CurrentCulture,
HelpWindowResources.SomeMatchesFormat,
this.HelpBuilder.HighlightCount);
}
}
}
}
/// <summary>
/// Called internally to notify when a property changed.
/// </summary>
/// <param name="propertyName">Property name.</param>
private void OnNotifyPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
|