using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using Rune = System.Rune;
namespace Terminal.Gui {
///
/// Renders an overlay on another view at a given point that allows selecting
/// from a range of 'autocomplete' options.
///
public abstract class Autocomplete : IAutocomplete {
private class Popup : View {
Autocomplete autocomplete;
public Popup (Autocomplete autocomplete)
{
this.autocomplete = autocomplete;
CanFocus = true;
WantMousePositionReports = true;
}
public override Rect Frame {
get => base.Frame;
set {
base.Frame = value;
X = value.X;
Y = value.Y;
Width = value.Width;
Height = value.Height;
}
}
public override void Redraw (Rect bounds)
{
if (autocomplete.LastPopupPos == null) {
return;
}
autocomplete.RenderOverlay ((Point)autocomplete.LastPopupPos);
}
public override bool MouseEvent (MouseEvent mouseEvent)
{
return autocomplete.MouseEvent (mouseEvent);
}
}
private View top, popup;
private bool closed;
int toRenderLength;
private Point? LastPopupPos { get; set; }
private ColorScheme colorScheme;
private View hostControl;
///
/// The host control to handle.
///
public virtual View HostControl {
get => hostControl;
set {
hostControl = value;
top = hostControl.SuperView;
if (top != null) {
top.DrawContent += Top_DrawContent;
top.DrawContentComplete += Top_DrawContentComplete;
top.Removed += Top_Removed;
}
}
}
private void Top_Removed (View obj)
{
Visible = false;
ManipulatePopup ();
}
private void Top_DrawContentComplete (Rect obj)
{
ManipulatePopup ();
}
private void Top_DrawContent (Rect obj)
{
if (!closed) {
ReopenSuggestions ();
}
ManipulatePopup ();
if (Visible) {
top.BringSubviewToFront (popup);
}
}
private void ManipulatePopup ()
{
if (Visible && popup == null) {
popup = new Popup (this) {
Frame = Rect.Empty
};
top?.Add (popup);
}
if (!Visible && popup != null) {
top.Remove (popup);
popup.Dispose ();
popup = null;
}
}
///
/// Gets or sets If the popup is displayed inside or outside the host limits.
///
public bool PopupInsideContainer { get; set; } = true;
///
/// The maximum width of the autocomplete dropdown
///
public virtual int MaxWidth { get; set; } = 10;
///
/// The maximum number of visible rows in the autocomplete dropdown to render
///
public virtual int MaxHeight { get; set; } = 6;
///
/// True if the autocomplete should be considered open and visible
///
public virtual bool Visible { get; set; }
///
/// The strings that form the current list of suggestions to render
/// based on what the user has typed so far.
///
public virtual ReadOnlyCollection Suggestions { get; set; } = new ReadOnlyCollection (new string [0]);
///
/// The full set of all strings that can be suggested.
///
///
public virtual List AllSuggestions { get; set; } = new List ();
///
/// The currently selected index into that the user has highlighted
///
public virtual int SelectedIdx { get; set; }
///
/// When more suggestions are available than can be rendered the user
/// can scroll down the dropdown list. This indicates how far down they
/// have gone
///
public virtual int ScrollOffset { get; set; }
///
/// The colors to use to render the overlay. Accessing this property before
/// the Application has been initialized will cause an error
///
public virtual ColorScheme ColorScheme {
get {
if (colorScheme == null) {
colorScheme = Colors.Menu;
}
return colorScheme;
}
set {
colorScheme = value;
}
}
///
/// The key that the user must press to accept the currently selected autocomplete suggestion
///
public virtual Key SelectionKey { get; set; } = Key.Enter;
///
/// The key that the user can press to close the currently popped autocomplete menu
///
public virtual Key CloseKey { get; set; } = Key.Esc;
///
/// The key that the user can press to reopen the currently popped autocomplete menu
///
public virtual Key Reopen { get; set; } = Key.Space | Key.CtrlMask | Key.AltMask;
///
/// Renders the autocomplete dialog inside or outside the given at the
/// given point.
///
///
public virtual void RenderOverlay (Point renderAt)
{
if (!Visible || HostControl?.HasFocus == false || Suggestions.Count == 0) {
LastPopupPos = null;
Visible = false;
return;
}
LastPopupPos = renderAt;
int height, width;
if (PopupInsideContainer) {
// don't overspill vertically
height = Math.Min (HostControl.Bounds.Height - renderAt.Y, MaxHeight);
// There is no space below, lets see if can popup on top
if (height < Suggestions.Count && HostControl.Bounds.Height - renderAt.Y >= height) {
// Verifies that the upper limit available is greater than the lower limit
if (renderAt.Y > HostControl.Bounds.Height - renderAt.Y) {
renderAt.Y = Math.Max (renderAt.Y - Math.Min (Suggestions.Count + 1, MaxHeight + 1), 0);
height = Math.Min (Math.Min (Suggestions.Count, MaxHeight), LastPopupPos.Value.Y - 1);
}
}
} else {
// don't overspill vertically
height = Math.Min (Math.Min (top.Bounds.Height - HostControl.Frame.Bottom, MaxHeight), Suggestions.Count);
// There is no space below, lets see if can popup on top
if (height < Suggestions.Count && HostControl.Frame.Y - top.Frame.Y >= height) {
// Verifies that the upper limit available is greater than the lower limit
if (HostControl.Frame.Y > top.Bounds.Height - HostControl.Frame.Y) {
renderAt.Y = Math.Max (HostControl.Frame.Y - Math.Min (Suggestions.Count, MaxHeight), 0);
height = Math.Min (Math.Min (Suggestions.Count, MaxHeight), HostControl.Frame.Y);
}
} else {
renderAt.Y = HostControl.Frame.Bottom;
}
}
if (ScrollOffset > Suggestions.Count - height) {
ScrollOffset = 0;
}
var toRender = Suggestions.Skip (ScrollOffset).Take (height).ToArray ();
toRenderLength = toRender.Length;
if (toRender.Length == 0) {
return;
}
width = Math.Min (MaxWidth, toRender.Max (s => s.Length));
if (PopupInsideContainer) {
// don't overspill horizontally, let's see if can be displayed on the left
if (width > HostControl.Bounds.Width - renderAt.X) {
// Verifies that the left limit available is greater than the right limit
if (renderAt.X > HostControl.Bounds.Width - renderAt.X) {
renderAt.X -= Math.Min (width, LastPopupPos.Value.X);
width = Math.Min (width, LastPopupPos.Value.X);
} else {
width = Math.Min (width, HostControl.Bounds.Width - renderAt.X);
}
}
} else {
// don't overspill horizontally, let's see if can be displayed on the left
if (width > top.Bounds.Width - (renderAt.X + HostControl.Frame.X)) {
// Verifies that the left limit available is greater than the right limit
if (renderAt.X + HostControl.Frame.X > top.Bounds.Width - (renderAt.X + HostControl.Frame.X)) {
renderAt.X -= Math.Min (width, LastPopupPos.Value.X);
width = Math.Min (width, LastPopupPos.Value.X);
} else {
width = Math.Min (width, top.Bounds.Width - renderAt.X);
}
}
}
if (PopupInsideContainer) {
popup.Frame = new Rect (
new Point (HostControl.Frame.X + renderAt.X, HostControl.Frame.Y + renderAt.Y),
new Size (width, height));
} else {
popup.Frame = new Rect (
new Point (HostControl.Frame.X + renderAt.X, renderAt.Y),
new Size (width, height));
}
popup.Move (0, 0);
for (int i = 0; i < toRender.Length; i++) {
if (i == SelectedIdx - ScrollOffset) {
Application.Driver.SetAttribute (ColorScheme.Focus);
} else {
Application.Driver.SetAttribute (ColorScheme.Normal);
}
popup.Move (0, i);
var text = TextFormatter.ClipOrPad (toRender [i], width);
Application.Driver.AddStr (text);
}
}
///
/// Updates to be a valid index within
///
public virtual void EnsureSelectedIdxIsValid ()
{
SelectedIdx = Math.Max (0, Math.Min (Suggestions.Count - 1, SelectedIdx));
// if user moved selection up off top of current scroll window
if (SelectedIdx < ScrollOffset) {
ScrollOffset = SelectedIdx;
}
// if user moved selection down past bottom of current scroll window
while (toRenderLength > 0 && SelectedIdx >= ScrollOffset + toRenderLength) {
ScrollOffset++;
}
}
///
/// Handle key events before e.g. to make key events like
/// up/down apply to the autocomplete control instead of changing the cursor position in
/// the underlying text view.
///
/// The key event.
/// trueif the key can be handled falseotherwise.
public virtual bool ProcessKey (KeyEvent kb)
{
if (IsWordChar ((char)kb.Key)) {
Visible = true;
closed = false;
}
if (kb.Key == Reopen) {
return ReopenSuggestions ();
}
if (closed || Suggestions.Count == 0) {
Visible = false;
return false;
}
if (kb.Key == Key.CursorDown) {
MoveDown ();
return true;
}
if (kb.Key == Key.CursorUp) {
MoveUp ();
return true;
}
if (kb.Key == SelectionKey) {
return Select ();
}
if (kb.Key == CloseKey) {
Close ();
return true;
}
return false;
}
///
/// 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.
///
/// The mouse event.
/// If was called from the popup or from the host.
/// trueif the mouse can be handled falseotherwise.
public virtual bool MouseEvent (MouseEvent me, bool fromHost = false)
{
if (fromHost) {
GenerateSuggestions ();
if (Visible && Suggestions.Count == 0) {
Visible = false;
HostControl?.SetNeedsDisplay ();
return true;
} else if (!Visible && Suggestions.Count > 0) {
Visible = true;
HostControl?.SetNeedsDisplay ();
Application.UngrabMouse ();
return false;
} else {
// not in the popup
if (Visible && HostControl != null) {
Visible = false;
closed = false;
}
HostControl?.SetNeedsDisplay ();
}
return false;
}
if (popup == null || Suggestions.Count == 0) {
ManipulatePopup ();
return false;
}
if (me.Flags == MouseFlags.ReportMousePosition) {
RenderSelectedIdxByMouse (me);
return true;
}
if (me.Flags == MouseFlags.Button1Clicked) {
SelectedIdx = me.Y - ScrollOffset;
return Select ();
}
if (me.Flags == MouseFlags.WheeledDown) {
MoveDown ();
return true;
}
if (me.Flags == MouseFlags.WheeledUp) {
MoveUp ();
return true;
}
return false;
}
///
/// Render the current selection in the Autocomplete context menu by the mouse reporting.
///
///
protected void RenderSelectedIdxByMouse (MouseEvent me)
{
if (SelectedIdx != me.Y - ScrollOffset) {
SelectedIdx = me.Y - ScrollOffset;
if (LastPopupPos != null) {
RenderOverlay ((Point)LastPopupPos);
}
}
}
///
/// Clears
///
public virtual void ClearSuggestions ()
{
Suggestions = Enumerable.Empty ().ToList ().AsReadOnly ();
}
///
/// Populates with all strings in that
/// match with the current cursor position/text in the
///
public virtual void GenerateSuggestions ()
{
// if there is nothing to pick from
if (AllSuggestions.Count == 0) {
ClearSuggestions ();
return;
}
var currentWord = GetCurrentWord ();
if (string.IsNullOrWhiteSpace (currentWord)) {
ClearSuggestions ();
} else {
Suggestions = AllSuggestions.Where (o =>
o.StartsWith (currentWord, StringComparison.CurrentCultureIgnoreCase) &&
!o.Equals (currentWord, StringComparison.CurrentCultureIgnoreCase)
).ToList ().AsReadOnly ();
EnsureSelectedIdxIsValid ();
}
}
///
/// Return true if the given symbol should be considered part of a word
/// and can be contained in matches. Base behavior is to use
///
///
///
public virtual bool IsWordChar (Rune rune)
{
return Char.IsLetterOrDigit ((char)rune);
}
///
/// Completes the autocomplete selection process. Called when user hits the .
///
///
protected bool Select ()
{
if (SelectedIdx >= 0 && SelectedIdx < Suggestions.Count) {
var accepted = Suggestions [SelectedIdx];
return InsertSelection (accepted);
}
return false;
}
///
/// Called when the user confirms a selection at the current cursor location in
/// the . The string
/// is the full autocomplete word to be inserted. Typically a host will have to
/// remove some characters such that the string
/// completes the word instead of simply being appended.
///
///
/// True if the insertion was possible otherwise false
protected virtual bool InsertSelection (string accepted)
{
var typedSoFar = GetCurrentWord () ?? "";
if (typedSoFar.Length < accepted.Length) {
// delete the text
for (int i = 0; i < typedSoFar.Length; i++) {
DeleteTextBackwards ();
}
InsertText (accepted);
return true;
}
return false;
}
///
/// Returns the currently selected word from the .
///
/// When overriding this method views can make use of
///
///
///
protected abstract string GetCurrentWord ();
///
///
/// Given a of characters, returns the word which ends at
/// or null. Also returns null if the is positioned in the middle of a word.
///
///
/// Use this method to determine whether autocomplete should be shown when the cursor is at
/// a given point in a line and to get the word from which suggestions should be generated.
///
///
///
///
protected virtual string IdxToWord (List line, int idx)
{
StringBuilder sb = new StringBuilder ();
// do not generate suggestions if the cursor is positioned in the middle of a word
bool areMidWord;
if (idx == line.Count) {
// the cursor positioned at the very end of the line
areMidWord = false;
} else {
// we are in the middle of a word if the cursor is over a letter/number
areMidWord = IsWordChar (line [idx]);
}
// if we are in the middle of a word then there is no way to autocomplete that word
if (areMidWord) {
return null;
}
// we are at the end of a word. Work out what has been typed so far
while (idx-- > 0) {
if (IsWordChar (line [idx])) {
sb.Insert (0, (char)line [idx]);
} else {
break;
}
}
return sb.ToString ();
}
///
/// Deletes the text backwards before insert the selected text in the .
///
protected abstract void DeleteTextBackwards ();
///
/// Inser the selected text in the .
///
///
protected abstract void InsertText (string accepted);
///
/// Closes the Autocomplete context menu if it is showing and
///
protected void Close ()
{
ClearSuggestions ();
Visible = false;
closed = true;
HostControl?.SetNeedsDisplay ();
ManipulatePopup ();
}
///
/// Moves the selection in the Autocomplete context menu up one
///
protected void MoveUp ()
{
SelectedIdx--;
if (SelectedIdx < 0) {
SelectedIdx = Suggestions.Count - 1;
}
EnsureSelectedIdxIsValid ();
HostControl?.SetNeedsDisplay ();
}
///
/// Moves the selection in the Autocomplete context menu down one
///
protected void MoveDown ()
{
SelectedIdx++;
if (SelectedIdx > Suggestions.Count - 1) {
SelectedIdx = 0;
}
EnsureSelectedIdxIsValid ();
HostControl?.SetNeedsDisplay ();
}
///
/// Reopen the popup after it has been closed.
///
///
protected bool ReopenSuggestions ()
{
GenerateSuggestions ();
if (Suggestions.Count > 0) {
Visible = true;
closed = false;
HostControl?.SetNeedsDisplay ();
return true;
}
return false;
}
}
}