From 47bb262e2c85aebc3696ea5cf2132ac2d78eb898 Mon Sep 17 00:00:00 2001 From: Tig Date: Sat, 24 Aug 2024 06:12:44 -0700 Subject: [PATCH] Code cleanu p: Autocomplete --- .../Text/Autocomplete/PopupAutocomplete.cs | 94 +++++++++---------- 1 file changed, 44 insertions(+), 50 deletions(-) diff --git a/Terminal.Gui/Text/Autocomplete/PopupAutocomplete.cs b/Terminal.Gui/Text/Autocomplete/PopupAutocomplete.cs index 16a45e918..fe52b9f99 100644 --- a/Terminal.Gui/Text/Autocomplete/PopupAutocomplete.cs +++ b/Terminal.Gui/Text/Autocomplete/PopupAutocomplete.cs @@ -1,3 +1,5 @@ +using System.Diagnostics; + namespace Terminal.Gui; /// @@ -6,15 +8,15 @@ namespace Terminal.Gui; /// public abstract partial class PopupAutocomplete : AutocompleteBase { - /// Creates a new instance of the class. - public PopupAutocomplete () { PopupInsideContainer = true; } - private bool _closed; private ColorScheme _colorScheme; private View _hostControl; - private View _top; // _hostControl's SuperView + private View _top; // The _hostControl's SuperView private View _popup; - private int toRenderLength; + private int _toRenderLength; + + /// Creates a new instance of the class. + public PopupAutocomplete () { PopupInsideContainer = true; } /// /// The colors to use to render the overlay. Accessing this property before the Application has been initialized @@ -34,6 +36,29 @@ public abstract partial class PopupAutocomplete : AutocompleteBase set => _colorScheme = value; } + /// The host control to handle. + public override View HostControl + { + get => _hostControl; + set + { + Debug.Assert (_hostControl is null); + _hostControl = value; + _top = _hostControl.SuperView; + + if (_top is { }) + { + _top.Initialized += _top_Initialized; + _top.Removed += _top_Removed; + } + } + } + + private void _top_Initialized (object sender, EventArgs e) + { + AddPopupToTop (); + } + /// public override void EnsureSelectedIdxIsValid () { @@ -46,30 +71,12 @@ public abstract partial class PopupAutocomplete : AutocompleteBase } // if user moved selection down past bottom of current scroll window - while (toRenderLength > 0 && SelectedIdx >= ScrollOffset + toRenderLength) + while (_toRenderLength > 0 && SelectedIdx >= ScrollOffset + _toRenderLength) { ScrollOffset++; } } - /// The host control to handle. - public override View HostControl - { - get => _hostControl; - set - { - _hostControl = value; - _top = _hostControl.SuperView; - - if (_top is { }) - { - _top.DrawContent += Top_DrawContent; - _top.DrawContentComplete += Top_DrawContentComplete; - _top.Removed += Top_Removed; - } - } - } - /// /// Handle mouse events before e.g. to make mouse events like report/click apply to the /// autocomplete control instead of changing the cursor position in the underlying text view. @@ -120,7 +127,8 @@ public abstract partial class PopupAutocomplete : AutocompleteBase if (_popup is null || Suggestions.Count == 0) { - ManipulatePopup (); + //AddPopupToTop (); + //Debug.Fail ("popup is null"); return false; } @@ -167,7 +175,6 @@ public abstract partial class PopupAutocomplete : AutocompleteBase if (SuggestionGenerator.IsWordChar ((Rune)key)) { Visible = true; - ManipulatePopup (); _closed = false; return false; @@ -302,7 +309,7 @@ public abstract partial class PopupAutocomplete : AutocompleteBase } Suggestion [] toRender = Suggestions.Skip (ScrollOffset).Take (height).ToArray (); - toRenderLength = toRender.Length; + _toRenderLength = toRender.Length; if (toRender.Length == 0) { @@ -397,7 +404,7 @@ public abstract partial class PopupAutocomplete : AutocompleteBase Visible = false; _closed = true; HostControl?.SetNeedsDisplay (); - ManipulatePopup (); + //RemovePopupFromTop (); } /// Deletes the text backwards before insert the selected text in the . @@ -517,42 +524,29 @@ public abstract partial class PopupAutocomplete : AutocompleteBase private Point? LastPopupPos { get; set; } #nullable restore - private void ManipulatePopup () + private void AddPopupToTop () { - if (Visible && _popup is null) + if (_popup is null) { _popup = new Popup (this) { Frame = Rectangle.Empty }; _top?.Add (_popup); } + } - if (!Visible && _popup is { }) + private void RemovePopupFromTop () + { + if (_popup is { } && _top.Subviews.Contains (_popup)) { _top?.Remove (_popup); _popup.Dispose (); _popup = null; + } } - private void Top_DrawContent (object sender, DrawEventArgs e) - { - if (!_closed) - { - ReopenSuggestions (); - } - - ManipulatePopup (); - - if (Visible) - { - _top.BringSubviewToFront (_popup); - } - } - - private void Top_DrawContentComplete (object sender, DrawEventArgs e) { ManipulatePopup (); } - - private void Top_Removed (object sender, SuperViewChangedEventArgs e) + private void _top_Removed (object sender, SuperViewChangedEventArgs e) { Visible = false; - ManipulatePopup (); + RemovePopupFromTop (); } }