From 97c0f5349a7259a10025e8fe487faaa124296e66 Mon Sep 17 00:00:00 2001 From: Alexandru Ciobanu Date: Tue, 18 Oct 2022 21:11:20 +0100 Subject: [PATCH 1/2] cleanup and fix docs in View class --- Terminal.Gui/Core/View.cs | 423 +++++++++++++++++++------------------- 1 file changed, 214 insertions(+), 209 deletions(-) diff --git a/Terminal.Gui/Core/View.cs b/Terminal.Gui/Core/View.cs index ea9d80885..668191451 100644 --- a/Terminal.Gui/Core/View.cs +++ b/Terminal.Gui/Core/View.cs @@ -107,8 +107,8 @@ namespace Terminal.Gui { /// /// The method is invoked when the size or layout of a view has /// changed. The default processing system will keep the size and dimensions - /// for views that use the , and will recompute the - /// frames for the vies that use . + /// for views that use the , and will recompute the + /// frames for the vies that use . /// /// public partial class View : Responder, ISupportInitializeNotification { @@ -248,9 +248,9 @@ namespace Terminal.Gui { /// Points to the current driver in use by the view, it is a convenience property /// for simplifying the development of new views. /// - public static ConsoleDriver Driver { get { return Application.Driver; } } + public static ConsoleDriver Driver => Application.Driver; - static IList empty = new List (0).AsReadOnly (); + static readonly IList empty = new List (0).AsReadOnly (); // This is null, and allocated on demand. List subviews; @@ -259,7 +259,7 @@ namespace Terminal.Gui { /// This returns a list of the subviews contained by this view. /// /// The subviews. - public IList Subviews => subviews == null ? empty : subviews.AsReadOnly (); + public IList Subviews => subviews?.AsReadOnly () ?? empty; // Internally, we use InternalSubviews rather than subviews, as we do not expect us // to make the same mistakes our users make when they poke at the Subviews. @@ -278,7 +278,7 @@ namespace Terminal.Gui { /// This returns a tab index list of the subviews contained by this view. /// /// The tabIndexes. - public IList TabIndexes => tabIndexes == null ? empty : tabIndexes.AsReadOnly (); + public IList TabIndexes => tabIndexes?.AsReadOnly () ?? empty; int tabIndex = -1; @@ -309,7 +309,7 @@ namespace Terminal.Gui { int GetTabIndex (int idx) { - int i = 0; + var i = 0; foreach (var v in SuperView.tabIndexes) { if (v.tabIndex == -1 || v == this) { continue; @@ -321,7 +321,7 @@ namespace Terminal.Gui { void SetTabIndex () { - int i = 0; + var i = 0; foreach (var v in SuperView.tabIndexes) { if (v.tabIndex == -1) { continue; @@ -337,7 +337,7 @@ namespace Terminal.Gui { /// This only be true if the is also true and the focus can be avoided by setting this to false /// public bool TabStop { - get { return tabStop; } + get => tabStop; set { if (tabStop == value) { return; @@ -358,12 +358,16 @@ namespace Terminal.Gui { } if (base.CanFocus != value) { base.CanFocus = value; - if (!value && tabIndex > -1) { + + switch (value) { + case false when tabIndex > -1: TabIndex = -1; + break; + case true when SuperView?.CanFocus == false && addingView: + SuperView.CanFocus = true; + break; } - if (value && SuperView?.CanFocus == false && addingView) { - SuperView.CanFocus = value; - } + if (value && tabIndex == -1) { TabIndex = SuperView != null ? SuperView.tabIndexes.IndexOf (this) : -1; } @@ -375,7 +379,7 @@ namespace Terminal.Gui { if (!value && HasFocus) { SetHasFocus (false, this); SuperView?.EnsureFocus (); - if (SuperView != null && SuperView?.Focused == null) { + if (SuperView != null && SuperView.Focused == null) { SuperView.FocusNext (); if (SuperView.Focused == null) { Application.Current.FocusNext (); @@ -389,7 +393,7 @@ namespace Terminal.Gui { if (!value) { view.oldCanFocus = view.CanFocus; view.oldTabIndex = view.tabIndex; - view.CanFocus = value; + view.CanFocus = false; view.tabIndex = -1; } else { if (addingView) { @@ -423,22 +427,18 @@ namespace Terminal.Gui { /// /// Returns a value indicating if this View is currently on Top (Active) /// - public bool IsCurrentTop { - get { - return Application.Current == this; - } - } + public bool IsCurrentTop => Application.Current == this; /// /// Gets or sets a value indicating whether this wants mouse position reports. /// /// true if want mouse position reports; otherwise, false. - public virtual bool WantMousePositionReports { get; set; } = false; + public virtual bool WantMousePositionReports { get; set; } /// /// Gets or sets a value indicating whether this want continuous button pressed event. /// - public virtual bool WantContinuousButtonPressed { get; set; } = false; + public virtual bool WantContinuousButtonPressed { get; set; } /// /// Gets or sets the frame for the view. The frame is relative to the view's container (). @@ -446,7 +446,7 @@ namespace Terminal.Gui { /// The frame. /// /// - /// Change the Frame when using the layout style to move or resize views. + /// Change the Frame when using the layout style to move or resize views. /// /// /// Altering the Frame of a view will trigger the redrawing of the @@ -480,8 +480,10 @@ namespace Terminal.Gui { LayoutStyle layoutStyle; /// - /// Controls how the View's is computed during the LayoutSubviews method, if the style is set to , - /// LayoutSubviews does not change the . If the style is the is updated using + /// Controls how the View's is computed during the LayoutSubviews method, if the style is set to + /// , + /// LayoutSubviews does not change the . If the style is + /// the is updated using /// the , , , and properties. /// /// The layout style. @@ -511,19 +513,17 @@ namespace Terminal.Gui { /// public Rect Bounds { get => new Rect (Point.Empty, Frame.Size); - set { - Frame = new Rect (frame.Location, value.Size); - } + set => Frame = new Rect (frame.Location, value.Size); } Pos x, y; /// - /// Gets or sets the X position for the view (the column). Only used the is . + /// Gets or sets the X position for the view (the column). Only used the is . /// /// The X Position. /// - /// If is changing this property has no effect and its value is indeterminate. + /// If is changing this property has no effect and its value is indeterminate. /// public Pos X { get => x; @@ -539,11 +539,11 @@ namespace Terminal.Gui { } /// - /// Gets or sets the Y position for the view (the row). Only used the is . + /// Gets or sets the Y position for the view (the row). Only used the is . /// /// The y position (line). /// - /// If is changing this property has no effect and its value is indeterminate. + /// If is changing this property has no effect and its value is indeterminate. /// public Pos Y { get => y; @@ -560,11 +560,11 @@ namespace Terminal.Gui { Dim width, height; /// - /// Gets or sets the width of the view. Only used the is . + /// Gets or sets the width of the view. Only used the is . /// /// The width. /// - /// If is changing this property has no effect and its value is indeterminate. + /// If is changing this property has no effect and its value is indeterminate. /// public Dim Width { get => width; @@ -587,10 +587,10 @@ namespace Terminal.Gui { } /// - /// Gets or sets the height of the view. Only used the is . + /// Gets or sets the height of the view. Only used the is . /// /// The height. - /// If is changing this property has no effect and its value is indeterminate. + /// If is changing this property has no effect and its value is indeterminate. public Dim Height { get => height; set { @@ -612,18 +612,18 @@ namespace Terminal.Gui { } /// - /// Forces validation with layout + /// Forces validation with layout /// to avoid breaking the and settings. /// public bool ForceValidatePosDim { get; set; } - bool ValidatePosDim (object oldvalue, object newValue) + bool ValidatePosDim (object oldValue, object newValue) { - if (!IsInitialized || layoutStyle == LayoutStyle.Absolute || oldvalue == null || oldvalue.GetType () == newValue.GetType () || this is Toplevel) { + if (!IsInitialized || layoutStyle == LayoutStyle.Absolute || oldValue == null || oldValue.GetType () == newValue.GetType () || this is Toplevel) { return true; } if (layoutStyle == LayoutStyle.Computed) { - if (oldvalue.GetType () != newValue.GetType () && !(newValue is Pos.PosAbsolute || newValue is Dim.DimAbsolute)) { + if (oldValue.GetType () != newValue.GetType () && !(newValue is Pos.PosAbsolute || newValue is Dim.DimAbsolute)) { return true; } } @@ -666,7 +666,7 @@ namespace Terminal.Gui { /// if the size can be set, otherwise. public bool SetMinWidthHeight () { - if (GetMinWidthHeight (out Size size)) { + if (GetMinWidthHeight (out var size)) { Bounds = new Rect (Bounds.Location, size); TextFormatter.Size = GetBoundsTextFormatterSize (); return true; @@ -686,13 +686,13 @@ namespace Terminal.Gui { public View SuperView => container; /// - /// Initializes a new instance of a class with the absolute + /// Initializes a new instance of a class with the absolute /// dimensions specified in the frame parameter. /// /// The region covered by this view. /// - /// This constructor initialize a View with a of . Use to - /// initialize a View with of + /// This constructor initialize a View with a of . + /// Use to initialize a View with of /// public View (Rect frame) { @@ -700,12 +700,12 @@ namespace Terminal.Gui { } /// - /// Initializes a new instance of using layout. + /// Initializes a new instance of using layout. /// /// /// /// Use , , , and properties to dynamically control the size and location of the view. - /// The will be created using + /// The will be created using /// coordinates. The initial size ( will be /// adjusted to fit the contents of , including newlines ('\n') for multiple lines. /// @@ -713,14 +713,14 @@ namespace Terminal.Gui { /// If Height is greater than one, word wrapping is provided. /// /// - /// This constructor initialize a View with a of . + /// This constructor initialize a View with a of . /// Use , , , and properties to dynamically control the size and location of the view. /// /// public View () : this (text: string.Empty, direction: TextDirection.LeftRight_TopBottom) { } /// - /// Initializes a new instance of using layout. + /// Initializes a new instance of using layout. /// /// /// @@ -738,7 +738,7 @@ namespace Terminal.Gui { public View (int x, int y, ustring text) : this (TextFormatter.CalcRect (x, y, text), text) { } /// - /// Initializes a new instance of using layout. + /// Initializes a new instance of using layout. /// /// /// @@ -759,11 +759,11 @@ namespace Terminal.Gui { } /// - /// Initializes a new instance of using layout. + /// Initializes a new instance of using layout. /// /// /// - /// The will be created using + /// The will be created using /// coordinates with the given string. The initial size ( will be /// adjusted to fit the contents of , including newlines ('\n') for multiple lines. /// @@ -795,12 +795,8 @@ namespace Terminal.Gui { TabStop = false; LayoutStyle = layoutStyle; // BUGBUG: CalcRect doesn't account for line wrapping - Rect r; - if (rect.IsEmpty) { - r = TextFormatter.CalcRect (0, 0, text, direction); - } else { - r = rect; - } + + var r = rect.IsEmpty ? TextFormatter.CalcRect (0, 0, text, direction) : rect; Frame = r; Text = text; @@ -809,7 +805,7 @@ namespace Terminal.Gui { } /// - /// Can be overridden if the has + /// Can be overridden if the has /// different format than the default. /// protected virtual void UpdateTextFormatterText () @@ -823,18 +819,18 @@ namespace Terminal.Gui { /// protected virtual void ProcessResizeView () { - var _x = x is Pos.PosAbsolute ? x.Anchor (0) : frame.X; - var _y = y is Pos.PosAbsolute ? y.Anchor (0) : frame.Y; + var actX = x is Pos.PosAbsolute ? x.Anchor (0) : frame.X; + var actY = y is Pos.PosAbsolute ? y.Anchor (0) : frame.Y; if (AutoSize) { var s = GetAutoSize (); var w = width is Dim.DimAbsolute && width.Anchor (0) > s.Width ? width.Anchor (0) : s.Width; var h = height is Dim.DimAbsolute && height.Anchor (0) > s.Height ? height.Anchor (0) : s.Height; - frame = new Rect (new Point (_x, _y), new Size (w, h)); + frame = new Rect (new Point (actX, actY), new Size (w, h)); } else { var w = width is Dim.DimAbsolute ? width.Anchor (0) : frame.Width; var h = height is Dim.DimAbsolute ? height.Anchor (0) : frame.Height; - frame = new Rect (new Point (_x, _y), new Size (w, h)); + frame = new Rect (new Point (actX, actY), new Size (w, h)); SetMinWidthHeight (); } TextFormatter.Size = GetBoundsTextFormatterSize (); @@ -842,7 +838,7 @@ namespace Terminal.Gui { SetNeedsDisplay (); } - private void TextFormatter_HotKeyChanged (Key obj) + void TextFormatter_HotKeyChanged (Key obj) { HotKeyChanged?.Invoke (obj); } @@ -894,10 +890,11 @@ namespace Terminal.Gui { var h = Math.Max (NeedDisplay.Height, region.Height); NeedDisplay = new Rect (x, y, w, h); } - if (container != null) - container.SetChildNeedsDisplay (); + container?.SetChildNeedsDisplay (); + if (subviews == null) return; + foreach (var view in subviews) if (view.Frame.IntersectsWith (region)) { var childRegion = Rect.Intersect (view.Frame, region); @@ -919,7 +916,7 @@ namespace Terminal.Gui { container.SetChildNeedsDisplay (); } - internal bool addingView = false; + internal bool addingView; /// /// Adds a subview (child) to this view. @@ -1106,9 +1103,9 @@ namespace Terminal.Gui { { var h = Frame.Height; var w = Frame.Width; - for (int line = 0; line < h; line++) { + for (var line = 0; line < h; line++) { Move (0, line); - for (int col = 0; col < w; col++) + for (var col = 0; col < w; col++) Driver.AddRune (' '); } } @@ -1123,9 +1120,9 @@ namespace Terminal.Gui { { var h = regionScreen.Height; var w = regionScreen.Width; - for (int line = regionScreen.Y; line < regionScreen.Y + h; line++) { + for (var line = regionScreen.Y; line < regionScreen.Y + h; line++) { Driver.Move (regionScreen.X, line); - for (int col = 0; col < w; col++) + for (var col = 0; col < w; col++) Driver.AddRune (' '); } } @@ -1143,11 +1140,12 @@ namespace Terminal.Gui { // Computes the real row, col relative to the screen. rrow = row + frame.Y; rcol = col + frame.X; - var ccontainer = container; - while (ccontainer != null) { - rrow += ccontainer.frame.Y; - rcol += ccontainer.frame.X; - ccontainer = ccontainer.container; + + var curContainer = container; + while (curContainer != null) { + rrow += curContainer.frame.Y; + rcol += curContainer.frame.X; + curContainer = curContainer.container; } // The following ensures that the cursor is always in the screen boundaries. @@ -1283,8 +1281,8 @@ namespace Terminal.Gui { return; } - ViewToScreen (col, row, out var rcol, out var rrow, clipped); - Driver.Move (rcol, rrow); + ViewToScreen (col, row, out var rCol, out var rRow, clipped); + Driver.Move (rCol, rRow); } /// @@ -1313,12 +1311,9 @@ namespace Terminal.Gui { } bool hasFocus; + /// - public override bool HasFocus { - get { - return hasFocus; - } - } + public override bool HasFocus => hasFocus; void SetHasFocus (bool value, View view, bool force = false) { @@ -1388,7 +1383,7 @@ namespace Terminal.Gui { /// public override bool OnEnter (View view) { - FocusEventArgs args = new FocusEventArgs (view); + var args = new FocusEventArgs (view); Enter?.Invoke (args); if (args.Handled) return true; @@ -1401,7 +1396,7 @@ namespace Terminal.Gui { /// public override bool OnLeave (View view) { - FocusEventArgs args = new FocusEventArgs (view); + var args = new FocusEventArgs (view); Leave?.Invoke (args); if (args.Handled) return true; @@ -1695,7 +1690,7 @@ namespace Terminal.Gui { return false; } - KeyEventEventArgs args = new KeyEventEventArgs (keyEvent); + var args = new KeyEventEventArgs (keyEvent); KeyPress?.Invoke (args); if (args.Handled) return true; @@ -1704,10 +1699,8 @@ namespace Terminal.Gui { if (args.Handled) return true; } - if (Focused?.Enabled == true && Focused?.ProcessKey (keyEvent) == true) - return true; - - return false; + + return Focused?.Enabled == true && Focused?.ProcessKey (keyEvent) == true; } /// @@ -1737,7 +1730,7 @@ namespace Terminal.Gui { // if ever see a true then that's what we will return if (thisReturn ?? false) { - toReturn = thisReturn.Value; + toReturn = true; } } } @@ -1863,7 +1856,7 @@ namespace Terminal.Gui { /// The used by a public Key GetKeyFromCommand (params Command [] command) { - return KeyBindings.First (x => x.Value.SequenceEqual (command)).Key; + return KeyBindings.First (kb => kb.Value.SequenceEqual (command)).Key; } /// @@ -1873,7 +1866,7 @@ namespace Terminal.Gui { return false; } - KeyEventEventArgs args = new KeyEventEventArgs (keyEvent); + var args = new KeyEventEventArgs (keyEvent); if (MostFocused?.Enabled == true) { MostFocused?.KeyPress?.Invoke (args); if (args.Handled) @@ -1883,6 +1876,7 @@ namespace Terminal.Gui { return true; if (subviews == null || subviews.Count == 0) return false; + foreach (var view in subviews) if (view.Enabled && view.ProcessHotKey (keyEvent)) return true; @@ -1896,7 +1890,7 @@ namespace Terminal.Gui { return false; } - KeyEventEventArgs args = new KeyEventEventArgs (keyEvent); + var args = new KeyEventEventArgs (keyEvent); KeyPress?.Invoke (args); if (args.Handled) return true; @@ -1909,6 +1903,7 @@ namespace Terminal.Gui { return true; if (subviews == null || subviews.Count == 0) return false; + foreach (var view in subviews) if (view.Enabled && view.ProcessColdKey (keyEvent)) return true; @@ -1927,7 +1922,7 @@ namespace Terminal.Gui { return false; } - KeyEventEventArgs args = new KeyEventEventArgs (keyEvent); + var args = new KeyEventEventArgs (keyEvent); KeyDown?.Invoke (args); if (args.Handled) { return true; @@ -1957,7 +1952,7 @@ namespace Terminal.Gui { return false; } - KeyEventEventArgs args = new KeyEventEventArgs (keyEvent); + var args = new KeyEventEventArgs (keyEvent); KeyUp?.Invoke (args); if (args.Handled) { return true; @@ -2025,10 +2020,10 @@ namespace Terminal.Gui { return; } - for (int i = tabIndexes.Count; i > 0;) { + for (var i = tabIndexes.Count; i > 0;) { i--; - View v = tabIndexes [i]; + var v = tabIndexes [i]; if (v.CanFocus && v.tabStop && v.Visible && v.Enabled) { SetFocus (v); return; @@ -2054,21 +2049,22 @@ namespace Terminal.Gui { FocusLast (); return focused != null; } - int focused_idx = -1; - for (int i = tabIndexes.Count; i > 0;) { + + var focusedIdx = -1; + for (var i = tabIndexes.Count; i > 0;) { i--; - View w = tabIndexes [i]; + var w = tabIndexes [i]; if (w.HasFocus) { if (w.FocusPrev ()) return true; - focused_idx = i; + focusedIdx = i; continue; } - if (w.CanFocus && focused_idx != -1 && w.tabStop && w.Visible && w.Enabled) { + if (w.CanFocus && focusedIdx != -1 && w.tabStop && w.Visible && w.Enabled) { focused.SetHasFocus (false, w); - if (w != null && w.CanFocus && w.tabStop && w.Visible && w.Enabled) + if (w.CanFocus && w.tabStop && w.Visible && w.Enabled) w.FocusLast (); SetFocus (w); @@ -2100,21 +2096,21 @@ namespace Terminal.Gui { FocusFirst (); return focused != null; } - int n = tabIndexes.Count; - int focused_idx = -1; - for (int i = 0; i < n; i++) { - View w = tabIndexes [i]; + var n = tabIndexes.Count; + var focusedIdx = -1; + for (var i = 0; i < n; i++) { + var w = tabIndexes [i]; if (w.HasFocus) { if (w.FocusNext ()) return true; - focused_idx = i; + focusedIdx = i; continue; } - if (w.CanFocus && focused_idx != -1 && w.tabStop && w.Visible && w.Enabled) { + if (w.CanFocus && focusedIdx != -1 && w.tabStop && w.Visible && w.Enabled) { focused.SetHasFocus (false, w); - if (w != null && w.CanFocus && w.tabStop && w.Visible && w.Enabled) + if (w.CanFocus && w.tabStop && w.Visible && w.Enabled) w.FocusFirst (); SetFocus (w); @@ -2131,14 +2127,10 @@ namespace Terminal.Gui { View GetMostFocused (View view) { if (view == null) { - return view; + return null; } - if (view.focused != null) { - return GetMostFocused (view.focused); - } else { - return view; - } + return view.focused != null ? GetMostFocused (view.focused) : view; } /// @@ -2150,7 +2142,7 @@ namespace Terminal.Gui { /// internal void SetRelativeLayout (Rect hostFrame) { - int w, h, _x, _y; + int actW, actH, actX, actY; var s = Size.Empty; if (AutoSize) { @@ -2159,71 +2151,76 @@ namespace Terminal.Gui { if (x is Pos.PosCenter) { if (width == null) { - w = AutoSize ? s.Width : hostFrame.Width; + actW = AutoSize ? s.Width : hostFrame.Width; } else { - w = width.Anchor (hostFrame.Width); - w = AutoSize && s.Width > w ? s.Width : w; + actW = width.Anchor (hostFrame.Width); + actW = AutoSize && s.Width > actW ? s.Width : actW; } - _x = x.Anchor (hostFrame.Width - w); + actX = x.Anchor (hostFrame.Width - actW); } else { - if (x == null) - _x = 0; - else - _x = x.Anchor (hostFrame.Width); - if (width == null) { - w = AutoSize ? s.Width : hostFrame.Width; - } else if (width is Dim.DimFactor && !((Dim.DimFactor)width).IsFromRemaining ()) { - w = width.Anchor (hostFrame.Width); - w = AutoSize && s.Width > w ? s.Width : w; - } else { - w = Math.Max (width.Anchor (hostFrame.Width - _x), 0); - w = AutoSize && s.Width > w ? s.Width : w; + actX = x?.Anchor (hostFrame.Width) ?? 0; + + switch (width) { + case null: + actW = AutoSize ? s.Width : hostFrame.Width; + break; + case Dim.DimFactor factor when !factor.IsFromRemaining (): + actW = width.Anchor (hostFrame.Width); + actW = AutoSize && s.Width > actW ? s.Width : actW; + break; + default: + actW = Math.Max (width.Anchor (hostFrame.Width - actX), 0); + actW = AutoSize && s.Width > actW ? s.Width : actW; + break; } } if (y is Pos.PosCenter) { if (height == null) { - h = AutoSize ? s.Height : hostFrame.Height; + actH = AutoSize ? s.Height : hostFrame.Height; } else { - h = height.Anchor (hostFrame.Height); - h = AutoSize && s.Height > h ? s.Height : h; + actH = height.Anchor (hostFrame.Height); + actH = AutoSize && s.Height > actH ? s.Height : actH; } - _y = y.Anchor (hostFrame.Height - h); + actY = y.Anchor (hostFrame.Height - actH); } else { - if (y == null) - _y = 0; - else - _y = y.Anchor (hostFrame.Height); - if (height == null) { - h = AutoSize ? s.Height : hostFrame.Height; - } else if (height is Dim.DimFactor && !((Dim.DimFactor)height).IsFromRemaining ()) { - h = height.Anchor (hostFrame.Height); - h = AutoSize && s.Height > h ? s.Height : h; - } else { - h = Math.Max (height.Anchor (hostFrame.Height - _y), 0); - h = AutoSize && s.Height > h ? s.Height : h; + actY = y?.Anchor (hostFrame.Height) ?? 0; + + switch (height) { + case null: + actH = AutoSize ? s.Height : hostFrame.Height; + break; + case Dim.DimFactor factor when !factor.IsFromRemaining (): + actH = height.Anchor (hostFrame.Height); + actH = AutoSize && s.Height > actH ? s.Height : actH; + break; + default: + actH = Math.Max (height.Anchor (hostFrame.Height - actY), 0); + actH = AutoSize && s.Height > actH ? s.Height : actH; + break; } } - var r = new Rect (_x, _y, w, h); + + var r = new Rect (actX, actY, actW, actH); if (Frame != r) { - Frame = new Rect (_x, _y, w, h); + Frame = new Rect (actX, actY, actW, actH); if (!SetMinWidthHeight ()) TextFormatter.Size = GetBoundsTextFormatterSize (); } } // https://en.wikipedia.org/wiki/Topological_sorting - List TopologicalSort (HashSet nodes, HashSet<(View From, View To)> edges) + List TopologicalSort (IEnumerable nodes, ICollection<(View From, View To)> edges) { var result = new List (); // Set of all nodes with no incoming edges - var S = new HashSet (nodes.Where (n => edges.All (e => !e.To.Equals (n)))); + var noEdgeNodes = new HashSet (nodes.Where (n => edges.All (e => !e.To.Equals (n)))); - while (S.Any ()) { + while (noEdgeNodes.Any ()) { // remove a node n from S - var n = S.First (); - S.Remove (n); + var n = noEdgeNodes.First (); + noEdgeNodes.Remove (n); // add n to tail of L if (n != this?.SuperView) @@ -2239,13 +2236,13 @@ namespace Terminal.Gui { // if m has no other incoming edges then if (edges.All (me => !me.To.Equals (m)) && m != this?.SuperView) { // insert m into S - S.Add (m); + noEdgeNodes.Add (m); } } } if (edges.Any ()) { - var (from, to) = edges.First (); + (var from, var to) = edges.First (); if (from != Application.Top) { if (!ReferenceEquals (from, to)) { throw new InvalidOperationException ($"TopologicalSort (for Pos/Dim) cannot find {from} linked with {to}. Did you forget to add it to {this}?"); @@ -2270,7 +2267,7 @@ namespace Terminal.Gui { } /// - /// Fired after the Views's method has completed. + /// Fired after the View's method has completed. /// /// /// Subscribe to this event to perform tasks when the has been resized or the layout has otherwise changed. @@ -2286,7 +2283,7 @@ namespace Terminal.Gui { } /// - /// Fired after the Views's method has completed. + /// Fired after the View's method has completed. /// /// /// Subscribe to this event to perform tasks when the has been resized or the layout has otherwise changed. @@ -2321,7 +2318,7 @@ namespace Terminal.Gui { return; } - Rect oldBounds = Bounds; + var oldBounds = Bounds; OnLayoutStarted (new LayoutEventArgs () { OldBounds = oldBounds }); TextFormatter.Size = GetBoundsTextFormatterSize (); @@ -2333,7 +2330,9 @@ namespace Terminal.Gui { void CollectPos (Pos pos, View from, ref HashSet nNodes, ref HashSet<(View, View)> nEdges) { - if (pos is Pos.PosView pv) { + switch (pos) { + case Pos.PosView pv: + { if (pv.Target != this) { nEdges.Add ((pv.Target, from)); } @@ -2342,17 +2341,22 @@ namespace Terminal.Gui { } return; } - if (pos is Pos.PosCombine pc) { + case Pos.PosCombine pc: + { foreach (var v in from.InternalSubviews) { CollectPos (pc.left, from, ref nNodes, ref nEdges); CollectPos (pc.right, from, ref nNodes, ref nEdges); } + break; + } } } void CollectDim (Dim dim, View from, ref HashSet nNodes, ref HashSet<(View, View)> nEdges) { - if (dim is Dim.DimView dv) { + switch (dim) { + case Dim.DimView dv: + { if (dv.Target != this) { nEdges.Add ((dv.Target, from)); } @@ -2361,11 +2365,14 @@ namespace Terminal.Gui { } return; } - if (dim is Dim.DimCombine dc) { + case Dim.DimCombine dc: + { foreach (var v in from.InternalSubviews) { CollectDim (dc.left, from, ref nNodes, ref nEdges); CollectDim (dc.right, from, ref nNodes, ref nEdges); } + break; + } } } @@ -2459,9 +2466,9 @@ namespace Terminal.Gui { } /// - /// Gets or sets a flag that determines whether will have trailing spaces preserved - /// or not when is enabled. If `true` any trailing spaces will be trimmed when - /// either the property is changed or when is set to `true`. + /// Gets or sets a flag that determines whether will have trailing spaces preserved + /// or not when is enabled. If `true` any trailing spaces will be trimmed when + /// either the property is changed or when is set to `true`. /// The default is `false`. /// public virtual bool PreserveTrailingSpaces { @@ -2488,7 +2495,7 @@ namespace Terminal.Gui { } /// - /// Gets or sets how the View's is aligned verticaly when drawn. Changing this property will redisplay the . + /// Gets or sets how the View's is aligned vertically when drawn. Changing this property will redisplay the . /// /// The text alignment. public virtual VerticalTextAlignment VerticalTextAlignment { @@ -2507,7 +2514,7 @@ namespace Terminal.Gui { get => TextFormatter.Direction; set { if (TextFormatter.Direction != value) { - var isValidOldAutSize = autoSize && IsValidAutoSize (out Size autSize); + var isValidOldAutSize = autoSize && IsValidAutoSize (out var _); var directionChanged = TextFormatter.IsHorizontalDirection (TextFormatter.Direction) != TextFormatter.IsHorizontalDirection (value); @@ -2558,7 +2565,7 @@ namespace Terminal.Gui { foreach (var view in subviews) { if (!value) { view.oldEnabled = view.Enabled; - view.Enabled = value; + view.Enabled = false; } else { view.Enabled = view.oldEnabled; view.addingView = false; @@ -2618,7 +2625,7 @@ namespace Terminal.Gui { void SetHotKey () { - TextFormatter.FindHotKey (text, HotKeySpecifier, true, out _, out Key hk); + TextFormatter.FindHotKey (text, HotKeySpecifier, true, out _, out var hk); if (hotKey != hk) { HotKey = hk; } @@ -2645,9 +2652,9 @@ namespace Terminal.Gui { bool SetWidthHeight (Size nBounds) { - bool aSize = false; - var canSizeW = SetWidth (nBounds.Width - GetHotKeySpecifierLength (), out int rW); - var canSizeH = SetHeight (nBounds.Height - GetHotKeySpecifierLength (false), out int rH); + var aSize = false; + var canSizeW = SetWidth (nBounds.Width - GetHotKeySpecifierLength (), out var rW); + var canSizeH = SetHeight (nBounds.Height - GetHotKeySpecifierLength (false), out var rH); if (canSizeW) { aSize = true; width = rW; @@ -2702,10 +2709,10 @@ namespace Terminal.Gui { } /// - /// Get the width or height of the length. + /// Get the width or height of the length. /// /// trueif is the width (default)falseif is the height. - /// The length of the . + /// The length of the . public int GetHotKeySpecifierLength (bool isWidth = true) { if (isWidth) { @@ -2720,9 +2727,9 @@ namespace Terminal.Gui { } /// - /// Gets the bounds size from a . + /// Gets the bounds size from a . /// - /// The bounds size minus the length. + /// The bounds size minus the length. public Size GetTextFormatterBoundsSize () { return new Size (TextFormatter.Size.Width - GetHotKeySpecifierLength (), @@ -2732,7 +2739,7 @@ namespace Terminal.Gui { /// /// Gets the text formatter size from a size. /// - /// The text formatter size more the length. + /// The text formatter size more the length. public Size GetBoundsTextFormatterSize () { if (ustring.IsNullOrEmpty (TextFormatter.Text)) @@ -2773,14 +2780,10 @@ namespace Terminal.Gui { return false; } - MouseEventArgs args = new MouseEventArgs (mouseEvent); + var args = new MouseEventArgs (mouseEvent); MouseEnter?.Invoke (args); - if (args.Handled) - return true; - if (base.OnMouseEnter (mouseEvent)) - return true; - - return false; + + return args.Handled || base.OnMouseEnter (mouseEvent); } /// @@ -2794,14 +2797,10 @@ namespace Terminal.Gui { return false; } - MouseEventArgs args = new MouseEventArgs (mouseEvent); + var args = new MouseEventArgs (mouseEvent); MouseLeave?.Invoke (args); - if (args.Handled) - return true; - if (base.OnMouseLeave (mouseEvent)) - return true; - - return false; + + return args.Handled || base.OnMouseLeave (mouseEvent); } /// @@ -2819,7 +2818,7 @@ namespace Terminal.Gui { return false; } - MouseEventArgs args = new MouseEventArgs (mouseEvent); + var args = new MouseEventArgs (mouseEvent); if (OnMouseClick (args)) return true; if (MouseEvent (mouseEvent)) @@ -2861,8 +2860,8 @@ namespace Terminal.Gui { /// protected override void Dispose (bool disposing) { - for (int i = InternalSubviews.Count - 1; i >= 0; i--) { - View subview = InternalSubviews [i]; + for (var i = InternalSubviews.Count - 1; i >= 0; i--) { + var subview = InternalSubviews [i]; Remove (subview); subview.Dispose (); } @@ -2919,7 +2918,7 @@ namespace Terminal.Gui { bool CanSetWidth (int desiredWidth, out int resultWidth) { - int w = desiredWidth; + var w = desiredWidth; bool canSetWidth; if (Width is Dim.DimCombine || Width is Dim.DimView || Width is Dim.DimFill) { // It's a Dim.DimCombine and so can't be assigned. Let it have it's width anchored. @@ -2943,13 +2942,18 @@ namespace Terminal.Gui { bool CanSetHeight (int desiredHeight, out int resultHeight) { - int h = desiredHeight; + var h = desiredHeight; bool canSetHeight; - if (Height is Dim.DimCombine || Height is Dim.DimView || Height is Dim.DimFill) { + switch (Height) { + case Dim.DimCombine _: + case Dim.DimView _: + case Dim.DimFill _: // It's a Dim.DimCombine and so can't be assigned. Let it have it's height anchored. h = Height.Anchor (h); canSetHeight = !ForceValidatePosDim; - } else if (Height is Dim.DimFactor factor) { + break; + case Dim.DimFactor factor: + { // Tries to get the SuperView height otherwise the view height. var sh = SuperView != null ? SuperView.Frame.Height : h; if (factor.IsFromRemaining ()) { @@ -2957,8 +2961,11 @@ namespace Terminal.Gui { } h = Height.Anchor (sh); canSetHeight = !ForceValidatePosDim; - } else { + break; + } + default: canSetHeight = true; + break; } resultHeight = h; @@ -2994,7 +3001,7 @@ namespace Terminal.Gui { /// true if the width can be directly assigned, false otherwise. public bool GetCurrentWidth (out int currentWidth) { - SetRelativeLayout (SuperView == null ? frame : SuperView.frame); + SetRelativeLayout (SuperView?.frame ?? frame); currentWidth = frame.Width; return CanSetWidth (0, out _); @@ -3007,7 +3014,7 @@ namespace Terminal.Gui { /// true if the height can be directly assigned, false otherwise. public bool GetCurrentHeight (out int currentHeight) { - SetRelativeLayout (SuperView == null ? frame : SuperView.frame); + SetRelativeLayout (SuperView?.frame ?? frame); currentHeight = frame.Height; return CanSetHeight (0, out _); @@ -3016,8 +3023,8 @@ namespace Terminal.Gui { /// /// Determines the current based on the value. /// - /// if is - /// or if is . + /// if is + /// or if is . /// If it's overridden can return other values. public virtual Attribute GetNormalColor () { @@ -3032,9 +3039,7 @@ namespace Terminal.Gui { { View top = Application.Top; for (var v = this?.SuperView; v != null; v = v.SuperView) { - if (v != null) { - top = v; - } + top = v; } return top; From 3e19bf70edc82b873cc0a6b71a6e70b4530693a3 Mon Sep 17 00:00:00 2001 From: Alexandru Ciobanu Date: Wed, 19 Oct 2022 13:55:34 +0100 Subject: [PATCH 2/2] Correct for the comments on the PR and removed two unused imports. --- Terminal.Gui/Core/View.cs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Terminal.Gui/Core/View.cs b/Terminal.Gui/Core/View.cs index 668191451..3496f82b3 100644 --- a/Terminal.Gui/Core/View.cs +++ b/Terminal.Gui/Core/View.cs @@ -6,15 +6,13 @@ // - Check for NeedDisplay on the hierarchy and repaint // - Layout support // - "Colors" type or "Attributes" type? -// - What to surface as "BackgroundCOlor" when clearing a window, an attribute or colors? +// - What to surface as "BackgroundColor" when clearing a window, an attribute or colors? // // Optimizations // - Add rendering limitation to the exposed area using System; -using System.Collections; using System.Collections.Generic; using System.ComponentModel; -using System.Diagnostics; using System.Linq; using NStack; @@ -666,7 +664,7 @@ namespace Terminal.Gui { /// if the size can be set, otherwise. public bool SetMinWidthHeight () { - if (GetMinWidthHeight (out var size)) { + if (GetMinWidthHeight (out Size size)) { Bounds = new Rect (Bounds.Location, size); TextFormatter.Size = GetBoundsTextFormatterSize (); return true;