Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
HelpViewModel.cs284 linesDownload Raw Back to HelpWindow
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System;5using System.ComponentModel;6using System.Diagnostics;7using System.Globalization;8using System.Management.Automation;9using System.Windows.Documents;10 11namespace Microsoft.Management.UI.Internal12{13    /// <summary>14    /// ViewModel for the Help Dialog used to:15    ///     build the help document16    ///     search the help document17    ///     offer text for labels.18    /// </summary>19    internal class HelpViewModel : INotifyPropertyChanged20    {21        /// <summary>22        /// The builder for the help FlowDocument Paragraph  used in a RichEditText control.23        /// </summary>24        private readonly HelpParagraphBuilder helpBuilder;25 26        /// <summary>27        /// Searcher for selecting current matches in paragraph text.28        /// </summary>29        private readonly ParagraphSearcher searcher;30 31        /// <summary>32        /// Title of the help window.33        /// </summary>34        private readonly string helpTitle;35 36        /// <summary>37        /// the zoom bound to the zoom slider value.38        /// </summary>39        private double zoom = 100;40 41        /// <summary>42        /// Text to be found. This is bound to the find TextBox.43        /// </summary>44        private string findText;45 46        /// <summary>47        /// text for the number of matches found.48        /// </summary>49        private string matchesLabel;50 51        /// <summary>52        /// Initializes a new instance of the HelpViewModel class.53        /// </summary>54        /// <param name="psObj">Object containing help.</param>55        /// <param name="documentParagraph">Paragraph in which help text is built/searched.</param>56        internal HelpViewModel(PSObject psObj, Paragraph documentParagraph)57        {58            Debug.Assert(psObj != null, "ensured by caller");59            Debug.Assert(documentParagraph != null, "ensured by caller");60 61            this.helpBuilder = new HelpParagraphBuilder(documentParagraph, psObj);62            this.helpBuilder.BuildParagraph();63            this.searcher = new ParagraphSearcher();64            this.helpBuilder.PropertyChanged += this.HelpBuilder_PropertyChanged;65            this.helpTitle = string.Format(66                CultureInfo.CurrentCulture,67                HelpWindowResources.HelpTitleFormat,68                HelpParagraphBuilder.GetPropertyString(psObj, "name"));69        }70 71        #region INotifyPropertyChanged Members72        /// <summary>73        /// Used to notify of property changes.74        /// </summary>75        public event PropertyChangedEventHandler PropertyChanged;76        #endregion77 78        /// <summary>79        /// Gets or sets the Zoom bound to the zoom slider value.80        /// </summary>81        public double Zoom82        {83            get84            {85                return this.zoom;86            }87 88            set89            {90                this.zoom = value;91                this.OnNotifyPropertyChanged("Zoom");92                this.OnNotifyPropertyChanged("ZoomLabel");93                this.OnNotifyPropertyChanged("ZoomLevel");94            }95        }96 97        /// <summary>98        /// Gets the value bound to the RichTextEdit zoom, which is calculated based on the zoom.99        /// </summary>100        public double ZoomLevel101        {102            get103            {104                return this.zoom / 100.0;105            }106        }107 108        /// <summary>109        /// Gets the label to be displayed for the zoom.110        /// </summary>111        public string ZoomLabel112        {113            get114            {115                return string.Format(CultureInfo.CurrentCulture, HelpWindowResources.ZoomLabelTextFormat, this.zoom);116            }117        }118 119        /// <summary>120        /// Gets or sets the text to be found.121        /// </summary>122        public string FindText123        {124            get125            {126                return this.findText;127            }128 129            set130            {131                this.findText = value;132                this.Search();133                this.SetMatchesLabel();134            }135        }136 137        /// <summary>138        /// Gets the title of the window.139        /// </summary>140        public string HelpTitle141        {142            get143            {144                return this.helpTitle;145            }146        }147 148        /// <summary>149        /// Gets or sets the label for current matches.150        /// </summary>151        public string MatchesLabel152        {153            get154            {155                return this.matchesLabel;156            }157 158            set159            {160                this.matchesLabel = value;161                this.OnNotifyPropertyChanged("MatchesLabel");162            }163        }164 165        /// <summary>166        /// Gets a value indicating whether there are matches to go to.167        /// </summary>168        public bool CanGoToNextOrPrevious169        {170            get171            {172                return this.HelpBuilder.HighlightCount != 0;173            }174        }175 176        /// <summary>177        /// Gets the searcher for selecting current matches in paragraph text.178        /// </summary>179        internal ParagraphSearcher Searcher180        {181            get { return this.searcher; }182        }183 184        /// <summary>185        /// Gets the paragraph builder used to write help content.186        /// </summary>187        internal HelpParagraphBuilder HelpBuilder188        {189            get { return this.helpBuilder; }190        }191 192        /// <summary>193        /// Highlights all matches to this.findText194        /// Called when findText changes or whenever the search has to be refreshed195        /// </summary>196        internal void Search()197        {198            this.HelpBuilder.HighlightAllInstancesOf(this.findText, HelpWindowSettings.Default.HelpSearchMatchCase, HelpWindowSettings.Default.HelpSearchWholeWord);199            this.searcher.ResetSearch();200        }201 202        /// <summary>203        /// Increases Zoom if not above maximum.204        /// </summary>205        internal void ZoomIn()206        {207            if (this.Zoom + HelpWindow.ZoomInterval <= HelpWindow.MaximumZoom)208            {209                this.Zoom += HelpWindow.ZoomInterval;210            }211        }212 213        /// <summary>214        /// Decreases Zoom if not below minimum.215        /// </summary>216        internal void ZoomOut()217        {218            if (this.Zoom - HelpWindow.ZoomInterval >= HelpWindow.MinimumZoom)219            {220                this.Zoom -= HelpWindow.ZoomInterval;221            }222        }223 224        /// <summary>225        /// Called to update the matches label.226        /// </summary>227        /// <param name="sender">Event sender.</param>228        /// <param name="e">Event arguments.</param>229        private void HelpBuilder_PropertyChanged(object sender, PropertyChangedEventArgs e)230        {231            if (e.PropertyName == "HighlightCount")232            {233                this.SetMatchesLabel();234                this.OnNotifyPropertyChanged("CanGoToNextOrPrevious");235            }236        }237 238        /// <summary>239        /// Sets the current matches label.240        /// </summary>241        private void SetMatchesLabel()242        {243            if (this.findText == null || this.findText.Trim().Length == 0)244            {245                this.MatchesLabel = string.Empty;246            }247            else248            {249                if (this.HelpBuilder.HighlightCount == 0)250                {251                    this.MatchesLabel = HelpWindowResources.NoMatches;252                }253                else254                {255                    if (this.HelpBuilder.HighlightCount == 1)256                    {257                        this.MatchesLabel = HelpWindowResources.OneMatch;258                    }259                    else260                    {261                        this.MatchesLabel = string.Format(262                            CultureInfo.CurrentCulture,263                            HelpWindowResources.SomeMatchesFormat,264                            this.HelpBuilder.HighlightCount);265                    }266                }267            }268        }269 270        /// <summary>271        /// Called internally to notify when a property changed.272        /// </summary>273        /// <param name="propertyName">Property name.</param>274        private void OnNotifyPropertyChanged(string propertyName)275        {276            PropertyChangedEventHandler handler = this.PropertyChanged;277            if (handler != null)278            {279                handler(this, new PropertyChangedEventArgs(propertyName));280            }281        }282    }283}284