1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882 |
- using System.Globalization;
- using Terminal.Gui.Resources;
- namespace Terminal.Gui;
- /// <summary>Single-line text entry <see cref="View"/></summary>
- /// <remarks>The <see cref="TextField"/> <see cref="View"/> provides editing functionality and mouse support.</remarks>
- public class TextField : View
- {
- private readonly HistoryText _historyText;
- private CultureInfo _currentCulture;
- private int _cursorPosition;
- private bool _isButtonPressed;
- private bool _isButtonReleased;
- private bool _isDrawing;
- private int _preTextChangedCursorPos;
- private int _selectedStart; // -1 represents there is no text selection.
- private string _selectedText;
- private int _start;
- private List<Rune> _text;
- /// <summary>
- /// Initializes a new instance of the <see cref="TextField"/> class.
- /// </summary>
- public TextField ()
- {
- _historyText = new ();
- _isButtonReleased = true;
- _selectedStart = -1;
- _text = new ();
- CaptionColor = new (Color.DarkGray);
- ReadOnly = false;
- Autocomplete = new TextFieldAutocomplete ();
- Height = Dim.Auto (DimAutoStyle.Text, 1);
- CanFocus = true;
- CursorVisibility = CursorVisibility.Default;
- Used = true;
- WantMousePositionReports = true;
- // By default, disable hotkeys (in case someome sets Title)
- HotKeySpecifier = new ('\xffff');
- _historyText.ChangeText += HistoryText_ChangeText;
- Initialized += TextField_Initialized;
- Added += TextField_Added;
- Removed += TextField_Removed;
- // Things this view knows how to do
- AddCommand (
- Command.DeleteCharRight,
- () =>
- {
- DeleteCharRight ();
- return true;
- }
- );
- AddCommand (
- Command.DeleteCharLeft,
- () =>
- {
- DeleteCharLeft (false);
- return true;
- }
- );
- AddCommand (
- Command.LeftStartExtend,
- () =>
- {
- MoveHomeExtend ();
- return true;
- }
- );
- AddCommand (
- Command.RightEndExtend,
- () =>
- {
- MoveEndExtend ();
- return true;
- }
- );
- AddCommand (
- Command.LeftStart,
- () =>
- {
- MoveHome ();
- return true;
- }
- );
- AddCommand (
- Command.LeftExtend,
- () =>
- {
- MoveLeftExtend ();
- return true;
- }
- );
- AddCommand (
- Command.RightExtend,
- () =>
- {
- MoveRightExtend ();
- return true;
- }
- );
- AddCommand (
- Command.WordLeftExtend,
- () =>
- {
- MoveWordLeftExtend ();
- return true;
- }
- );
- AddCommand (
- Command.WordRightExtend,
- () =>
- {
- MoveWordRightExtend ();
- return true;
- }
- );
- AddCommand (Command.Left, () => MoveLeft ());
- AddCommand (
- Command.RightEnd,
- () =>
- {
- MoveEnd ();
- return true;
- }
- );
- AddCommand (Command.Right, () => MoveRight ());
- AddCommand (
- Command.CutToEndLine,
- () =>
- {
- KillToEnd ();
- return true;
- }
- );
- AddCommand (
- Command.CutToStartLine,
- () =>
- {
- KillToStart ();
- return true;
- }
- );
- AddCommand (
- Command.Undo,
- () =>
- {
- Undo ();
- return true;
- }
- );
- AddCommand (
- Command.Redo,
- () =>
- {
- Redo ();
- return true;
- }
- );
- AddCommand (
- Command.WordLeft,
- () =>
- {
- MoveWordLeft ();
- return true;
- }
- );
- AddCommand (
- Command.WordRight,
- () =>
- {
- MoveWordRight ();
- return true;
- }
- );
- AddCommand (
- Command.KillWordForwards,
- () =>
- {
- KillWordForwards ();
- return true;
- }
- );
- AddCommand (
- Command.KillWordBackwards,
- () =>
- {
- KillWordBackwards ();
- return true;
- }
- );
- AddCommand (
- Command.ToggleOverwrite,
- () =>
- {
- SetOverwrite (!Used);
- return true;
- }
- );
- AddCommand (
- Command.EnableOverwrite,
- () =>
- {
- SetOverwrite (true);
- return true;
- }
- );
- AddCommand (
- Command.DisableOverwrite,
- () =>
- {
- SetOverwrite (false);
- return true;
- }
- );
- AddCommand (
- Command.Copy,
- () =>
- {
- Copy ();
- return true;
- }
- );
- AddCommand (
- Command.Cut,
- () =>
- {
- Cut ();
- return true;
- }
- );
- AddCommand (
- Command.Paste,
- () =>
- {
- Paste ();
- return true;
- }
- );
- AddCommand (
- Command.SelectAll,
- () =>
- {
- SelectAll ();
- return true;
- }
- );
- AddCommand (
- Command.DeleteAll,
- () =>
- {
- DeleteAll ();
- return true;
- }
- );
- AddCommand (
- Command.Context,
- () =>
- {
- ShowContextMenu ();
- return true;
- }
- );
- // Default keybindings for this view
- // We follow this as closely as possible: https://en.wikipedia.org/wiki/Table_of_keyboard_shortcuts
- KeyBindings.Add (Key.Delete, Command.DeleteCharRight);
- KeyBindings.Add (Key.D.WithCtrl, Command.DeleteCharRight);
- KeyBindings.Add (Key.Backspace, Command.DeleteCharLeft);
- KeyBindings.Add (Key.Home.WithShift, Command.LeftStartExtend);
- KeyBindings.Add (Key.Home.WithShift.WithCtrl, Command.LeftStartExtend);
- KeyBindings.Add (Key.A.WithShift.WithCtrl, Command.LeftStartExtend);
- KeyBindings.Add (Key.End.WithShift, Command.RightEndExtend);
- KeyBindings.Add (Key.End.WithShift.WithCtrl, Command.RightEndExtend);
- KeyBindings.Add (Key.E.WithShift.WithCtrl, Command.RightEndExtend);
- KeyBindings.Add (Key.Home, Command.LeftStart);
- KeyBindings.Add (Key.Home.WithCtrl, Command.LeftStart);
- KeyBindings.Add (Key.A.WithCtrl, Command.LeftStart);
- KeyBindings.Add (Key.CursorLeft.WithShift, Command.LeftExtend);
- KeyBindings.Add (Key.CursorUp.WithShift, Command.LeftExtend);
- KeyBindings.Add (Key.CursorRight.WithShift, Command.RightExtend);
- KeyBindings.Add (Key.CursorDown.WithShift, Command.RightExtend);
- KeyBindings.Add (Key.CursorLeft.WithShift.WithCtrl, Command.WordLeftExtend);
- KeyBindings.Add (Key.CursorUp.WithShift.WithCtrl, Command.WordLeftExtend);
- KeyBindings.Add (Key.CursorRight.WithShift.WithCtrl, Command.WordRightExtend);
- KeyBindings.Add (Key.CursorDown.WithShift.WithCtrl, Command.WordRightExtend);
- KeyBindings.Add (Key.CursorLeft, Command.Left);
- KeyBindings.Add (Key.B.WithCtrl, Command.Left);
- KeyBindings.Add (Key.End, Command.RightEnd);
- KeyBindings.Add (Key.End.WithCtrl, Command.RightEnd);
- KeyBindings.Add (Key.E.WithCtrl, Command.RightEnd);
- KeyBindings.Add (Key.CursorRight, Command.Right);
- KeyBindings.Add (Key.F.WithCtrl, Command.Right);
- KeyBindings.Add (Key.K.WithCtrl, Command.CutToEndLine);
- KeyBindings.Add (Key.K.WithCtrl.WithShift, Command.CutToStartLine);
- KeyBindings.Add (Key.Z.WithCtrl, Command.Undo);
- KeyBindings.Add (Key.Y.WithCtrl, Command.Redo);
- KeyBindings.Add (Key.CursorLeft.WithCtrl, Command.WordLeft);
- KeyBindings.Add (Key.CursorUp.WithCtrl, Command.WordLeft);
- KeyBindings.Add (Key.CursorRight.WithCtrl, Command.WordRight);
- KeyBindings.Add (Key.CursorDown.WithCtrl, Command.WordRight);
- #if UNIX_KEY_BINDINGS
- KeyBindings.Add (Key.F.WithShift.WithAlt, Command.WordRightExtend);
- KeyBindings.Add (Key.K.WithAlt, Command.CutToStartLine);
- KeyBindings.Add (Key.B.WithShift.WithAlt, Command.WordLeftExtend);
- KeyBindings.Add (Key.B.WithAlt, Command.WordLeft);
- KeyBindings.Add (Key.F.WithAlt, Command.WordRight);
- KeyBindings.Add (Key.Backspace.WithAlt, Command.Undo);
- #endif
- KeyBindings.Add (Key.Delete.WithCtrl, Command.KillWordForwards);
- KeyBindings.Add (Key.Backspace.WithCtrl, Command.KillWordBackwards);
- KeyBindings.Add (Key.InsertChar, Command.ToggleOverwrite);
- KeyBindings.Add (Key.C.WithCtrl, Command.Copy);
- KeyBindings.Add (Key.X.WithCtrl, Command.Cut);
- KeyBindings.Add (Key.V.WithCtrl, Command.Paste);
- KeyBindings.Add (Key.T.WithCtrl, Command.SelectAll);
- KeyBindings.Add (Key.R.WithCtrl, Command.DeleteAll);
- KeyBindings.Add (Key.D.WithCtrl.WithShift, Command.DeleteAll);
- _currentCulture = Thread.CurrentThread.CurrentUICulture;
- ContextMenu = new() { Host = this };
- ContextMenu.KeyChanged += ContextMenu_KeyChanged;
- KeyBindings.Add (ContextMenu.Key, KeyBindingScope.HotKey, Command.Context);
- KeyBindings.Remove (Key.Space);
- }
- /// <summary>
- /// Provides autocomplete context menu based on suggestions at the current cursor position. Configure
- /// <see cref="ISuggestionGenerator"/> to enable this feature.
- /// </summary>
- public IAutocomplete Autocomplete { get; set; }
- /// <summary>
- /// Gets or sets the text to render in control when no value has been entered yet and the <see cref="View"/> does
- /// not yet have input focus.
- /// </summary>
- public string Caption { get; set; }
- /// <summary>Gets or sets the foreground <see cref="Color"/> to use when rendering <see cref="Caption"/>.</summary>
- public Color CaptionColor { get; set; }
- /// <summary>Get the <see cref="ContextMenu"/> for this view.</summary>
- public ContextMenu ContextMenu { get; }
- /// <summary>Sets or gets the current cursor position.</summary>
- public virtual int CursorPosition
- {
- get => _cursorPosition;
- set
- {
- if (value < 0)
- {
- _cursorPosition = 0;
- }
- else if (value > _text.Count)
- {
- _cursorPosition = _text.Count;
- }
- else
- {
- _cursorPosition = value;
- }
- PrepareSelection (_selectedStart, _cursorPosition - _selectedStart);
- }
- }
- /// <summary>
- /// Indicates whatever the text has history changes or not. <see langword="true"/> if the text has history changes
- /// <see langword="false"/> otherwise.
- /// </summary>
- public bool HasHistoryChanges => _historyText.HasHistoryChanges;
- /// <summary>
- /// Indicates whatever the text was changed or not. <see langword="true"/> if the text was changed
- /// <see langword="false"/> otherwise.
- /// </summary>
- public bool IsDirty => _historyText.IsDirty ([Cell.StringToCells (Text)]);
- /// <summary>If set to true its not allow any changes in the text.</summary>
- public bool ReadOnly { get; set; }
- /// <summary>Gets the left offset position.</summary>
- public int ScrollOffset { get; private set; }
- /// <summary>
- /// Sets the secret property.
- /// <remarks>This makes the text entry suitable for entering passwords.</remarks>
- /// </summary>
- public bool Secret { get; set; }
- /// <summary>Length of the selected text.</summary>
- public int SelectedLength { get; private set; }
- /// <summary>Start position of the selected text.</summary>
- public int SelectedStart
- {
- get => _selectedStart;
- set
- {
- if (value < -1)
- {
- _selectedStart = -1;
- }
- else if (value > _text.Count)
- {
- _selectedStart = _text.Count;
- }
- else
- {
- _selectedStart = value;
- }
- PrepareSelection (_selectedStart, _cursorPosition - _selectedStart);
- }
- }
- /// <summary>The selected text.</summary>
- public string SelectedText
- {
- get => Secret ? null : _selectedText;
- private set => _selectedText = value;
- }
- /// <summary>Sets or gets the text held by the view.</summary>
- public new string Text
- {
- get => StringExtensions.ToString (_text);
- set
- {
- var oldText = StringExtensions.ToString (_text);
- if (oldText == value)
- {
- return;
- }
- string newText = value.Replace ("\t", "").Split ("\n") [0];
- CancelEventArgs<string> args = new (ref oldText, ref newText);
- OnTextChanging (args);
- if (args.Cancel)
- {
- if (_cursorPosition > _text.Count)
- {
- _cursorPosition = _text.Count;
- }
- return;
- }
- ClearAllSelection ();
- // Note we use NewValue here; TextChanging subscribers may have changed it
- _text = args.NewValue.EnumerateRunes ().ToList ();
- if (!Secret && !_historyText.IsFromHistory)
- {
- _historyText.Add (
- new () { Cell.ToCellList (oldText) },
- new (_cursorPosition, 0)
- );
- _historyText.Add (
- new () { Cell.ToCells (_text) },
- new (_cursorPosition, 0),
- HistoryText.LineStatus.Replaced
- );
- }
- OnTextChanged ();
- ProcessAutocomplete ();
- if (_cursorPosition > _text.Count)
- {
- _cursorPosition = Math.Max (TextModel.DisplaySize (_text, 0).size - 1, 0);
- }
- Adjust ();
- SetNeedsDisplay ();
- }
- }
- /// <summary>
- /// Tracks whether the text field should be considered "used", that is, that the user has moved in the entry, so
- /// new input should be appended at the cursor position, rather than clearing the entry
- /// </summary>
- public bool Used { get; set; }
- /// <summary>Clear the selected text.</summary>
- public void ClearAllSelection ()
- {
- if (_selectedStart == -1 && SelectedLength == 0 && string.IsNullOrEmpty (_selectedText))
- {
- return;
- }
- _selectedStart = -1;
- SelectedLength = 0;
- _selectedText = null;
- _start = 0;
- SelectedLength = 0;
- SetNeedsDisplay ();
- }
- /// <summary>Allows clearing the <see cref="HistoryText.HistoryTextItemEventArgs"/> items updating the original text.</summary>
- public void ClearHistoryChanges () { _historyText.Clear ([Cell.StringToCells (Text)]); }
- /// <summary>Copy the selected text to the clipboard.</summary>
- public virtual void Copy ()
- {
- if (Secret || SelectedLength == 0)
- {
- return;
- }
- Clipboard.Contents = SelectedText;
- }
- /// <summary>Cut the selected text to the clipboard.</summary>
- public virtual void Cut ()
- {
- if (ReadOnly || Secret || SelectedLength == 0)
- {
- return;
- }
- Clipboard.Contents = SelectedText;
- List<Rune> newText = DeleteSelectedText ();
- Text = StringExtensions.ToString (newText);
- Adjust ();
- }
- /// <summary>Deletes all text.</summary>
- public void DeleteAll ()
- {
- if (_text.Count == 0)
- {
- return;
- }
- _selectedStart = 0;
- MoveEndExtend ();
- DeleteCharLeft (false);
- SetNeedsDisplay ();
- }
- /// <summary>Deletes the character to the left.</summary>
- /// <param name="usePreTextChangedCursorPos">
- /// If set to <see langword="true">true</see> use the cursor position cached ;
- /// otherwise use <see cref="CursorPosition"/>. use .
- /// </param>
- public virtual void DeleteCharLeft (bool usePreTextChangedCursorPos)
- {
- if (ReadOnly)
- {
- return;
- }
- _historyText.Add (
- new () { Cell.ToCells (_text) },
- new (_cursorPosition, 0)
- );
- if (SelectedLength == 0)
- {
- if (_cursorPosition == 0)
- {
- return;
- }
- if (!usePreTextChangedCursorPos)
- {
- _preTextChangedCursorPos = _cursorPosition;
- }
- _cursorPosition--;
- if (_preTextChangedCursorPos < _text.Count)
- {
- SetText (
- _text.GetRange (0, _preTextChangedCursorPos - 1)
- .Concat (
- _text.GetRange (
- _preTextChangedCursorPos,
- _text.Count - _preTextChangedCursorPos
- )
- )
- );
- }
- else
- {
- SetText (_text.GetRange (0, _preTextChangedCursorPos - 1));
- }
- Adjust ();
- }
- else
- {
- List<Rune> newText = DeleteSelectedText ();
- Text = StringExtensions.ToString (newText);
- Adjust ();
- }
- }
- /// <summary>Deletes the character to the right.</summary>
- public virtual void DeleteCharRight ()
- {
- if (ReadOnly)
- {
- return;
- }
- _historyText.Add (
- new () { Cell.ToCells (_text) },
- new (_cursorPosition, 0)
- );
- if (SelectedLength == 0)
- {
- if (_text.Count == 0 || _text.Count == _cursorPosition)
- {
- return;
- }
- SetText (
- _text.GetRange (0, _cursorPosition)
- .Concat (_text.GetRange (_cursorPosition + 1, _text.Count - (_cursorPosition + 1)))
- );
- Adjust ();
- }
- else
- {
- List<Rune> newText = DeleteSelectedText ();
- Text = StringExtensions.ToString (newText);
- Adjust ();
- }
- }
- /// <inheritdoc/>
- public override Attribute GetNormalColor () { return GetFocusColor (); }
- /// <summary>
- /// Inserts the given <paramref name="toAdd"/> text at the current cursor position exactly as if the user had just
- /// typed it
- /// </summary>
- /// <param name="toAdd">Text to add</param>
- /// <param name="useOldCursorPos">Use the previous cursor position.</param>
- public void InsertText (string toAdd, bool useOldCursorPos = true)
- {
- foreach (char ch in toAdd)
- {
- Key key;
- try
- {
- key = ch;
- }
- catch (Exception)
- {
- throw new ArgumentException (
- $"Cannot insert character '{ch}' because it does not map to a Key"
- );
- }
- InsertText (key, useOldCursorPos);
- }
- }
- /// <summary>Deletes word backwards.</summary>
- public virtual void KillWordBackwards ()
- {
- ClearAllSelection ();
- (int col, int row)? newPos = GetModel ().WordBackward (_cursorPosition, 0);
- if (newPos is null)
- {
- return;
- }
- if (newPos.Value.col != -1)
- {
- SetText (
- _text.GetRange (0, newPos.Value.col)
- .Concat (_text.GetRange (_cursorPosition, _text.Count - _cursorPosition))
- );
- _cursorPosition = newPos.Value.col;
- }
- Adjust ();
- }
- /// <summary>Deletes word forwards.</summary>
- public virtual void KillWordForwards ()
- {
- ClearAllSelection ();
- (int col, int row)? newPos = GetModel ().WordForward (_cursorPosition, 0);
- if (newPos is null)
- {
- return;
- }
- if (newPos.Value.col != -1)
- {
- SetText (
- _text.GetRange (0, _cursorPosition)
- .Concat (_text.GetRange (newPos.Value.col, _text.Count - newPos.Value.col))
- );
- }
- Adjust ();
- }
- /// <inheritdoc/>
- protected internal override bool OnMouseEvent (MouseEvent ev)
- {
- if (!ev.Flags.HasFlag (MouseFlags.Button1Pressed)
- && !ev.Flags.HasFlag (MouseFlags.ReportMousePosition)
- && !ev.Flags.HasFlag (MouseFlags.Button1Released)
- && !ev.Flags.HasFlag (MouseFlags.Button1DoubleClicked)
- && !ev.Flags.HasFlag (MouseFlags.Button1TripleClicked)
- && !ev.Flags.HasFlag (ContextMenu.MouseFlags))
- {
- return base.OnMouseEvent (ev);
- }
- if (!CanFocus)
- {
- return true;
- }
- if (!HasFocus && ev.Flags != MouseFlags.ReportMousePosition)
- {
- SetFocus ();
- }
- // Give autocomplete first opportunity to respond to mouse clicks
- if (SelectedLength == 0 && Autocomplete.OnMouseEvent (ev, true))
- {
- return true;
- }
- if (ev.Flags == MouseFlags.Button1Pressed)
- {
- EnsureHasFocus ();
- PositionCursor (ev);
- if (_isButtonReleased)
- {
- ClearAllSelection ();
- }
- _isButtonReleased = true;
- _isButtonPressed = true;
- }
- else if (ev.Flags == (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition) && _isButtonPressed)
- {
- int x = PositionCursor (ev);
- _isButtonReleased = false;
- PrepareSelection (x);
- if (Application.MouseGrabView is null)
- {
- Application.GrabMouse (this);
- }
- }
- else if (ev.Flags == MouseFlags.Button1Released)
- {
- _isButtonReleased = true;
- _isButtonPressed = false;
- Application.UngrabMouse ();
- }
- else if (ev.Flags == MouseFlags.Button1DoubleClicked)
- {
- EnsureHasFocus ();
- int x = PositionCursor (ev);
- int sbw = x;
- if (x == _text.Count
- || (x > 0 && (char)_text [x - 1].Value != ' ')
- || (x > 0 && (char)_text [x].Value == ' '))
- {
- (int col, int row)? newPosBw = GetModel ().WordBackward (x, 0);
- if (newPosBw is null)
- {
- return true;
- }
- sbw = newPosBw.Value.col;
- }
- if (sbw != -1)
- {
- x = sbw;
- PositionCursor (x);
- }
- (int col, int row)? newPosFw = GetModel ().WordForward (x, 0);
- if (newPosFw is null)
- {
- return true;
- }
- ClearAllSelection ();
- if (newPosFw.Value.col != -1 && sbw != -1)
- {
- _cursorPosition = newPosFw.Value.col;
- }
- PrepareSelection (sbw, newPosFw.Value.col - sbw);
- }
- else if (ev.Flags == MouseFlags.Button1TripleClicked)
- {
- EnsureHasFocus ();
- PositionCursor (0);
- ClearAllSelection ();
- PrepareSelection (0, _text.Count);
- }
- else if (ev.Flags == ContextMenu.MouseFlags)
- {
- ShowContextMenu ();
- }
- //SetNeedsDisplay ();
- return true;
- void EnsureHasFocus ()
- {
- if (!HasFocus)
- {
- SetFocus ();
- }
- }
- }
- /// <summary>Moves cursor to the end of the typed text.</summary>
- public void MoveEnd ()
- {
- ClearAllSelection ();
- _cursorPosition = _text.Count;
- Adjust ();
- }
- /// <inheritdoc/>
- public override void OnDrawContent (Rectangle viewport)
- {
- _isDrawing = true;
- var selColor = new Attribute (GetFocusColor ().Background, GetFocusColor ().Foreground);
- SetSelectedStartSelectedLength ();
- Driver?.SetAttribute (GetNormalColor ());
- Move (0, 0);
- int p = ScrollOffset;
- var col = 0;
- int width = Viewport.Width + OffSetBackground ();
- int tcount = _text.Count;
- Attribute roc = GetReadOnlyColor ();
- for (int idx = p; idx < tcount; idx++)
- {
- Rune rune = _text [idx];
- int cols = rune.GetColumns ();
- if (idx == _cursorPosition && HasFocus && !Used && SelectedLength == 0 && !ReadOnly)
- {
- Driver?.SetAttribute (selColor);
- }
- else if (ReadOnly)
- {
- Driver?.SetAttribute (
- idx >= _start && SelectedLength > 0 && idx < _start + SelectedLength
- ? selColor
- : roc
- );
- }
- else if (!HasFocus && Enabled)
- {
- Driver?.SetAttribute (GetFocusColor ());
- }
- else if (!Enabled)
- {
- Driver?.SetAttribute (roc);
- }
- else
- {
- Driver?.SetAttribute (
- idx >= _start && SelectedLength > 0 && idx < _start + SelectedLength
- ? selColor
- : ColorScheme.Focus
- );
- }
- if (col + cols <= width)
- {
- Driver?.AddRune (Secret ? Glyphs.Dot : rune);
- }
- if (!TextModel.SetCol (ref col, width, cols))
- {
- break;
- }
- if (idx + 1 < tcount && col + _text [idx + 1].GetColumns () > width)
- {
- break;
- }
- }
- Driver.SetAttribute (GetFocusColor ());
- for (int i = col; i < width; i++)
- {
- Driver.AddRune ((Rune)' ');
- }
- PositionCursor ();
- RenderCaption ();
- DrawAutocomplete ();
- _isDrawing = false;
- }
- /// <inheritdoc/>
- public override bool? OnInvokingKeyBindings (Key a, KeyBindingScope scope)
- {
- // Give autocomplete first opportunity to respond to key presses
- if (SelectedLength == 0 && Autocomplete.Suggestions.Count > 0 && Autocomplete.ProcessKey (a))
- {
- return true;
- }
- return base.OnInvokingKeyBindings (a, scope);
- }
- /// <inheritdoc/>
- protected override void OnHasFocusChanged (bool newHasFocus, View previousFocusedView, View view)
- {
- if (Application.MouseGrabView is { } && Application.MouseGrabView == this)
- {
- Application.UngrabMouse ();
- }
- //if (SelectedLength != 0 && !(Application.MouseGrabView is MenuBar))
- // ClearAllSelection ();
- }
- /// TODO: Flush out these docs
- /// <summary>
- /// Processes key presses for the <see cref="TextField"/>.
- /// <remarks>
- /// The <see cref="TextField"/> control responds to the following keys:
- /// <list type="table">
- /// <listheader>
- /// <term>Keys</term> <description>Function</description>
- /// </listheader>
- /// <item>
- /// <term><see cref="Key.Delete"/>, <see cref="Key.Backspace"/></term>
- /// <description>Deletes the character before cursor.</description>
- /// </item>
- /// </list>
- /// </remarks>
- /// </summary>
- /// <param name="a"></param>
- /// <returns></returns>
- public override bool OnProcessKeyDown (Key a)
- {
- // Remember the cursor position because the new calculated cursor position is needed
- // to be set BEFORE the TextChanged event is triggered.
- // Needed for the Elmish Wrapper issue https://github.com/DieselMeister/Terminal.Gui.Elmish/issues/2
- _preTextChangedCursorPos = _cursorPosition;
- // Ignore other control characters.
- if (!a.IsKeyCodeAtoZ && (a.KeyCode < KeyCode.Space || a.KeyCode > KeyCode.CharMask))
- {
- return false;
- }
- if (ReadOnly)
- {
- return true;
- }
- InsertText (a, true);
- return true;
- }
- /// <summary>Virtual method that invoke the <see cref="TextChanging"/> event if it's defined.</summary>
- /// <param name="args">The event arguments.</param>
- /// <returns><see langword="true"/> if the event was cancelled.</returns>
- public bool OnTextChanging (CancelEventArgs<string> args)
- {
- TextChanging?.Invoke (this, args);
- return args.Cancel;
- }
- /// <summary>Paste the selected text from the clipboard.</summary>
- public virtual void Paste ()
- {
- if (ReadOnly || string.IsNullOrEmpty (Clipboard.Contents))
- {
- return;
- }
- SetSelectedStartSelectedLength ();
- int selStart = _start == -1 ? CursorPosition : _start;
- string cbTxt = Clipboard.Contents.Split ("\n") [0] ?? "";
- Text = StringExtensions.ToString (_text.GetRange (0, selStart))
- + cbTxt
- + StringExtensions.ToString (
- _text.GetRange (
- selStart + SelectedLength,
- _text.Count - (selStart + SelectedLength)
- )
- );
- _cursorPosition = Math.Min (selStart + cbTxt.GetRuneCount (), _text.Count);
- ClearAllSelection ();
- SetNeedsDisplay ();
- Adjust ();
- }
- /// <summary>Sets the cursor position.</summary>
- public override Point? PositionCursor ()
- {
- ProcessAutocomplete ();
- var col = 0;
- for (int idx = ScrollOffset < 0 ? 0 : ScrollOffset; idx < _text.Count; idx++)
- {
- if (idx == _cursorPosition)
- {
- break;
- }
- int cols = _text [idx].GetColumns ();
- TextModel.SetCol (ref col, Viewport.Width - 1, cols);
- }
- int pos = _cursorPosition - ScrollOffset + Math.Min (Viewport.X, 0);
- Move (pos, 0);
- return new Point (pos, 0);
- }
- /// <summary>Redoes the latest changes.</summary>
- public void Redo ()
- {
- if (ReadOnly)
- {
- return;
- }
- _historyText.Redo ();
- }
- /// <summary>Selects all text.</summary>
- public void SelectAll ()
- {
- if (_text.Count == 0)
- {
- return;
- }
- _selectedStart = 0;
- MoveEndExtend ();
- SetNeedsDisplay ();
- }
- ///// <summary>
- ///// Changed event, raised when the text has changed.
- ///// <remarks>
- ///// This event is raised when the <see cref="Text"/> changes. The passed <see cref="EventArgs"/> is a
- ///// <see cref="string"/> containing the old value.
- ///// </remarks>
- ///// </summary>
- //public event EventHandler<StateEventArgs<string>> TextChanged;
- /// <summary>Changing event, raised before the <see cref="Text"/> changes and can be canceled or changing the new text.</summary>
- public event EventHandler<CancelEventArgs<string>> TextChanging;
- /// <summary>Undoes the latest changes.</summary>
- public void Undo ()
- {
- if (ReadOnly)
- {
- return;
- }
- _historyText.Undo ();
- }
- /// <summary>
- /// Returns <see langword="true"/> if the current cursor position is at the end of the <see cref="Text"/>. This
- /// includes when it is empty.
- /// </summary>
- /// <returns></returns>
- internal bool CursorIsAtEnd () { return CursorPosition == Text.Length; }
- /// <summary>Returns <see langword="true"/> if the current cursor position is at the start of the <see cref="TextField"/>.</summary>
- /// <returns></returns>
- internal bool CursorIsAtStart () { return CursorPosition <= 0; }
- private void Adjust ()
- {
- if (!IsAdded)
- {
- return;
- }
- // TODO: This is a lame prototype proving it should be easy for TextField to
- // TODO: support Width = Dim.Auto (DimAutoStyle: Content).
- //SetContentSize(new (TextModel.DisplaySize (_text).size, 1));
- int offB = OffSetBackground ();
- bool need = NeedsDisplay || !Used;
- if (_cursorPosition < ScrollOffset)
- {
- ScrollOffset = _cursorPosition;
- need = true;
- }
- else if (Viewport.Width > 0
- && (ScrollOffset + _cursorPosition - (Viewport.Width + offB) == 0
- || TextModel.DisplaySize (_text, ScrollOffset, _cursorPosition).size >= Viewport.Width + offB))
- {
- ScrollOffset = Math.Max (
- TextModel.CalculateLeftColumn (
- _text,
- ScrollOffset,
- _cursorPosition,
- Viewport.Width + offB
- ),
- 0
- );
- need = true;
- }
- if (need)
- {
- SetNeedsDisplay ();
- }
- else
- {
- PositionCursor ();
- }
- }
- private MenuBarItem BuildContextMenuBarItem ()
- {
- return new (
- new MenuItem []
- {
- new (
- Strings.ctxSelectAll,
- "",
- () => SelectAll (),
- null,
- null,
- (KeyCode)KeyBindings.GetKeyFromCommands (Command.SelectAll)
- ),
- new (
- Strings.ctxDeleteAll,
- "",
- () => DeleteAll (),
- null,
- null,
- (KeyCode)KeyBindings.GetKeyFromCommands (Command.DeleteAll)
- ),
- new (
- Strings.ctxCopy,
- "",
- () => Copy (),
- null,
- null,
- (KeyCode)KeyBindings.GetKeyFromCommands (Command.Copy)
- ),
- new (
- Strings.ctxCut,
- "",
- () => Cut (),
- null,
- null,
- (KeyCode)KeyBindings.GetKeyFromCommands (Command.Cut)
- ),
- new (
- Strings.ctxPaste,
- "",
- () => Paste (),
- null,
- null,
- (KeyCode)KeyBindings.GetKeyFromCommands (Command.Paste)
- ),
- new (
- Strings.ctxUndo,
- "",
- () => Undo (),
- null,
- null,
- (KeyCode)KeyBindings.GetKeyFromCommands (Command.Undo)
- ),
- new (
- Strings.ctxRedo,
- "",
- () => Redo (),
- null,
- null,
- (KeyCode)KeyBindings.GetKeyFromCommands (Command.Redo)
- )
- }
- );
- }
- private void ContextMenu_KeyChanged (object sender, KeyChangedEventArgs e) { KeyBindings.ReplaceKey (e.OldKey.KeyCode, e.NewKey.KeyCode); }
- private List<Rune> DeleteSelectedText ()
- {
- SetSelectedStartSelectedLength ();
- int selStart = SelectedStart > -1 ? _start : _cursorPosition;
- string newText = StringExtensions.ToString (_text.GetRange (0, selStart))
- + StringExtensions.ToString (
- _text.GetRange (
- selStart + SelectedLength,
- _text.Count - (selStart + SelectedLength)
- )
- );
- ClearAllSelection ();
- _cursorPosition = selStart >= newText.GetRuneCount () ? newText.GetRuneCount () : selStart;
- return newText.ToRuneList ();
- }
- private void GenerateSuggestions ()
- {
- List<Cell> currentLine = Cell.ToCellList (Text);
- int cursorPosition = Math.Min (CursorPosition, currentLine.Count);
- Autocomplete.Context = new (
- currentLine,
- cursorPosition,
- Autocomplete.Context != null
- ? Autocomplete.Context.Canceled
- : false
- );
- Autocomplete.GenerateSuggestions (
- Autocomplete.Context
- );
- }
- private TextModel GetModel ()
- {
- var model = new TextModel ();
- model.LoadString (Text);
- return model;
- }
- private Attribute GetReadOnlyColor ()
- {
- ColorScheme cs = ColorScheme;
- if (ColorScheme is null)
- {
- cs = new ();
- }
- if (cs.Disabled.Foreground == cs.Focus.Background)
- {
- return new (cs.Focus.Foreground, cs.Focus.Background);
- }
- return new (cs.Disabled.Foreground, cs.Focus.Background);
- }
- private void HistoryText_ChangeText (object sender, HistoryText.HistoryTextItemEventArgs obj)
- {
- if (obj is null)
- {
- return;
- }
- Text = Cell.ToString (obj?.Lines [obj.CursorPosition.Y]);
- CursorPosition = obj.CursorPosition.X;
- Adjust ();
- }
- private void InsertText (Key a, bool usePreTextChangedCursorPos)
- {
- _historyText.Add (
- new () { Cell.ToCells (_text) },
- new (_cursorPosition, 0)
- );
- List<Rune> newText = _text;
- if (SelectedLength > 0)
- {
- newText = DeleteSelectedText ();
- _preTextChangedCursorPos = _cursorPosition;
- }
- if (!usePreTextChangedCursorPos)
- {
- _preTextChangedCursorPos = _cursorPosition;
- }
- StringRuneEnumerator kbstr = a.AsRune.ToString ().EnumerateRunes ();
- if (Used)
- {
- _cursorPosition++;
- if (_cursorPosition == newText.Count + 1)
- {
- SetText (newText.Concat (kbstr).ToList ());
- }
- else
- {
- if (_preTextChangedCursorPos > newText.Count)
- {
- _preTextChangedCursorPos = newText.Count;
- }
- SetText (
- newText.GetRange (0, _preTextChangedCursorPos)
- .Concat (kbstr)
- .Concat (
- newText.GetRange (
- _preTextChangedCursorPos,
- Math.Min (
- newText.Count - _preTextChangedCursorPos,
- newText.Count
- )
- )
- )
- );
- }
- }
- else
- {
- SetText (
- newText.GetRange (0, _preTextChangedCursorPos)
- .Concat (kbstr)
- .Concat (
- newText.GetRange (
- Math.Min (_preTextChangedCursorPos + 1, newText.Count),
- Math.Max (newText.Count - _preTextChangedCursorPos - 1, 0)
- )
- )
- );
- _cursorPosition++;
- }
- Adjust ();
- }
- private void KillToEnd ()
- {
- if (ReadOnly)
- {
- return;
- }
- ClearAllSelection ();
- if (_cursorPosition >= _text.Count)
- {
- return;
- }
- SetClipboard (_text.GetRange (_cursorPosition, _text.Count - _cursorPosition));
- SetText (_text.GetRange (0, _cursorPosition));
- Adjust ();
- }
- private void KillToStart ()
- {
- if (ReadOnly)
- {
- return;
- }
- ClearAllSelection ();
- if (_cursorPosition == 0)
- {
- return;
- }
- SetClipboard (_text.GetRange (0, _cursorPosition));
- SetText (_text.GetRange (_cursorPosition, _text.Count - _cursorPosition));
- _cursorPosition = 0;
- Adjust ();
- }
- private void MoveEndExtend ()
- {
- if (_cursorPosition <= _text.Count)
- {
- int x = _cursorPosition;
- _cursorPosition = _text.Count;
- PrepareSelection (x, _cursorPosition - x);
- }
- }
- private void MoveHome ()
- {
- ClearAllSelection ();
- _cursorPosition = 0;
- Adjust ();
- }
- private void MoveHomeExtend ()
- {
- if (_cursorPosition > 0)
- {
- int x = _cursorPosition;
- _cursorPosition = 0;
- PrepareSelection (x, _cursorPosition - x);
- }
- }
- private bool MoveLeft ()
- {
- if (_cursorPosition > 0)
- {
- ClearAllSelection ();
- _cursorPosition--;
- Adjust ();
- return true;
- }
- return false;
- }
- private void MoveLeftExtend ()
- {
- if (_cursorPosition > 0)
- {
- PrepareSelection (_cursorPosition--, -1);
- }
- }
- private bool MoveRight ()
- {
- if (_cursorPosition == _text.Count)
- {
- return false;
- }
- ClearAllSelection ();
- _cursorPosition++;
- Adjust ();
- return true;
- }
- private void MoveRightExtend ()
- {
- if (_cursorPosition < _text.Count)
- {
- PrepareSelection (_cursorPosition++, 1);
- }
- }
- private void MoveWordLeft ()
- {
- ClearAllSelection ();
- (int col, int row)? newPos = GetModel ().WordBackward (_cursorPosition, 0);
- if (newPos is null)
- {
- return;
- }
- if (newPos.Value.col != -1)
- {
- _cursorPosition = newPos.Value.col;
- }
- Adjust ();
- }
- private void MoveWordLeftExtend ()
- {
- if (_cursorPosition > 0)
- {
- int x = Math.Min (
- _start > -1 && _start > _cursorPosition ? _start : _cursorPosition,
- _text.Count
- );
- if (x > 0)
- {
- (int col, int row)? newPos = GetModel ().WordBackward (x, 0);
- if (newPos is null)
- {
- return;
- }
- if (newPos.Value.col != -1)
- {
- _cursorPosition = newPos.Value.col;
- }
- PrepareSelection (x, newPos.Value.col - x);
- }
- }
- }
- private void MoveWordRight ()
- {
- ClearAllSelection ();
- (int col, int row)? newPos = GetModel ().WordForward (_cursorPosition, 0);
- if (newPos is null)
- {
- return;
- }
- if (newPos.Value.col != -1)
- {
- _cursorPosition = newPos.Value.col;
- }
- Adjust ();
- }
- private void MoveWordRightExtend ()
- {
- if (_cursorPosition < _text.Count)
- {
- int x = _start > -1 && _start > _cursorPosition ? _start : _cursorPosition;
- (int col, int row)? newPos = GetModel ().WordForward (x, 0);
- if (newPos is null)
- {
- return;
- }
- if (newPos.Value.col != -1)
- {
- _cursorPosition = newPos.Value.col;
- }
- PrepareSelection (x, newPos.Value.col - x);
- }
- }
- // BUGBUG: This assumes Frame == Viewport. It's also not clear what the intention is. For now, changed to always return 0.
- private int OffSetBackground ()
- {
- var offB = 0;
- if (SuperView?.Frame.Right - Frame.Right < 0)
- {
- offB = SuperView.Frame.Right - Frame.Right - 1;
- }
- return 0; //offB;
- }
- private int PositionCursor (MouseEvent ev) { return PositionCursor (TextModel.GetColFromX (_text, ScrollOffset, ev.Position.X), false); }
- private int PositionCursor (int x, bool getX = true)
- {
- int pX = x;
- if (getX)
- {
- pX = TextModel.GetColFromX (_text, ScrollOffset, x);
- }
- if (ScrollOffset + pX > _text.Count)
- {
- _cursorPosition = _text.Count;
- }
- else if (ScrollOffset + pX < ScrollOffset)
- {
- _cursorPosition = 0;
- }
- else
- {
- _cursorPosition = ScrollOffset + pX;
- }
- return _cursorPosition;
- }
- private void PrepareSelection (int x, int direction = 0)
- {
- x = x + ScrollOffset < -1 ? 0 : x;
- _selectedStart = _selectedStart == -1 && _text.Count > 0 && x >= 0 && x <= _text.Count
- ? x
- : _selectedStart;
- if (_selectedStart > -1)
- {
- SelectedLength = Math.Abs (
- x + direction <= _text.Count
- ? x + direction - _selectedStart
- : _text.Count - _selectedStart
- );
- SetSelectedStartSelectedLength ();
- if (_start > -1 && SelectedLength > 0)
- {
- _selectedText = SelectedLength > 0
- ? StringExtensions.ToString (
- _text.GetRange (
- _start < 0 ? 0 : _start,
- SelectedLength > _text.Count
- ? _text.Count
- : SelectedLength
- )
- )
- : "";
- if (ScrollOffset > _start)
- {
- ScrollOffset = _start;
- }
- }
- else if (_start > -1 && SelectedLength == 0)
- {
- _selectedText = null;
- }
- SetNeedsDisplay ();
- }
- else if (SelectedLength > 0 || _selectedText is { })
- {
- ClearAllSelection ();
- }
- Adjust ();
- }
- private void ProcessAutocomplete ()
- {
- if (_isDrawing)
- {
- return;
- }
- if (SelectedLength > 0)
- {
- return;
- }
- GenerateSuggestions ();
- }
- private void DrawAutocomplete ()
- {
- if (SelectedLength > 0)
- {
- return;
- }
- if (Autocomplete?.Context == null)
- {
- return;
- }
- var renderAt = new Point (
- Autocomplete.Context.CursorPosition,
- 0
- );
- Autocomplete.RenderOverlay (renderAt);
- }
- private void RenderCaption ()
- {
- if (HasFocus
- || Caption == null
- || Caption.Length == 0
- || Text?.Length > 0)
- {
- return;
- }
- var color = new Attribute (CaptionColor, GetNormalColor ().Background);
- Driver.SetAttribute (color);
- Move (0, 0);
- string render = Caption;
- if (render.GetColumns () > Viewport.Width)
- {
- render = render [..Viewport.Width];
- }
- Driver.AddStr (render);
- }
- private void SetClipboard (IEnumerable<Rune> text)
- {
- if (!Secret)
- {
- Clipboard.Contents = StringExtensions.ToString (text.ToList ());
- }
- }
- private void SetOverwrite (bool overwrite)
- {
- Used = overwrite;
- SetNeedsDisplay ();
- }
- private void SetSelectedStartSelectedLength ()
- {
- if (SelectedStart > -1 && _cursorPosition < SelectedStart)
- {
- _start = _cursorPosition;
- }
- else
- {
- _start = SelectedStart;
- }
- }
- private void SetText (List<Rune> newText) { Text = StringExtensions.ToString (newText); }
- private void SetText (IEnumerable<Rune> newText) { SetText (newText.ToList ()); }
- private void ShowContextMenu ()
- {
- if (!Equals (_currentCulture, Thread.CurrentThread.CurrentUICulture))
- {
- _currentCulture = Thread.CurrentThread.CurrentUICulture;
- }
- ContextMenu.Show (BuildContextMenuBarItem ());
- }
- private void TextField_Added (object sender, SuperViewChangedEventArgs e)
- {
- if (Autocomplete.HostControl is null)
- {
- Autocomplete.HostControl = this;
- Autocomplete.PopupInsideContainer = false;
- }
- }
- private void TextField_Removed (object sender, SuperViewChangedEventArgs e) { Autocomplete.HostControl = null; }
- private void TextField_Initialized (object sender, EventArgs e)
- {
- _cursorPosition = Text.GetRuneCount ();
- if (Viewport.Width > 0)
- {
- ScrollOffset = _cursorPosition > Viewport.Width + 1 ? _cursorPosition - Viewport.Width + 1 : 0;
- }
- if (Autocomplete.HostControl is null)
- {
- Autocomplete.HostControl = this;
- Autocomplete.PopupInsideContainer = false;
- }
- }
- }
- /// <summary>
- /// Renders an overlay on another view at a given point that allows selecting from a range of 'autocomplete'
- /// options. An implementation on a TextField.
- /// </summary>
- public class TextFieldAutocomplete : PopupAutocomplete
- {
- /// <inheritdoc/>
- protected override void DeleteTextBackwards () { ((TextField)HostControl).DeleteCharLeft (false); }
- /// <inheritdoc/>
- protected override void InsertText (string accepted) { ((TextField)HostControl).InsertText (accepted, false); }
- /// <inheritdoc/>
- protected override void SetCursorPosition (int column) { ((TextField)HostControl).CursorPosition = column; }
- }
|