12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183 |
- //
- // Authors:
- // Miguel de Icaza ([email protected])
- //
- // Pending:
- // - 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?
- //
- // Optimziations
- // - Add rendering limitation to the exposed area
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using NStack;
- namespace Terminal.Gui {
- /// <summary>
- /// Text alignment enumeration, controls how text is displayed.
- /// </summary>
- public enum TextAlignment {
- /// <summary>
- /// Aligns the text to the left of the frame.
- /// </summary>
- Left,
- /// <summary>
- /// Aligns the text to the right side of the frame.
- /// </summary>
- Right,
- /// <summary>
- /// Centers the text in the frame.
- /// </summary>
- Centered,
- /// <summary>
- /// Shows the text as justified text in the frame.
- /// </summary>
- Justified
- }
- /// <summary>
- /// Determines the LayoutStyle for a view, if Absolute, during LayoutSubviews, the
- /// value from the Frame will be used, if the value is Computed, then the Frame
- /// will be updated from the X, Y Pos objects and the Width and Height Dim objects.
- /// </summary>
- public enum LayoutStyle {
- /// <summary>
- /// The position and size of the view are based on the Frame value.
- /// </summary>
- Absolute,
- /// <summary>
- /// The position and size of the view will be computed based on the
- /// X, Y, Width and Height properties and set on the Frame.
- /// </summary>
- Computed
- }
- /// <summary>
- /// View is the base class for all views on the screen and represents a visible element that can render itself and contains zero or more nested views.
- /// </summary>
- /// <remarks>
- /// <para>
- /// The View defines the base functionality for user interface elements in Terminal.Gui. Views
- /// can contain one or more subviews, can respond to user input and render themselves on the screen.
- /// </para>
- /// <para>
- /// Views supports two layout styles: Absolute or Computed. The choice as to which layout style is used by the View
- /// is determined when the View is initizlied. To create a View using Absolute layout, call a constructor that takes a
- /// Rect parameter to specify the absolute position and size (the <c>View.<see cref="Frame "/></c>)/. To create a View
- /// using Computed layout use a constructor that does not take a Rect parametr and set the X, Y, Width and Height
- /// properties on the view. Both approaches use coordinates that are relative to the container they are being added to.
- /// </para>
- /// <para>
- /// To switch between Absolute and Computed layout, use the <see cref="LayoutStyle"/> property.
- /// </para>
- /// <para>
- /// Computed layout is more flexible and supports dynamic console apps where controls adjust layout
- /// as the terminal resizes or other Views change size or position. The X, Y, Width and Height
- /// properties are Dim and Pos objects that dynamically update the position of a view.
- /// The X and Y properties are of type <see cref="Pos"/>
- /// and you can use either absolute positions, percentages or anchor
- /// points. The Width and Height properties are of type
- /// <see cref="Dim"/> and can use absolute position,
- /// percentages and anchors. These are useful as they will take
- /// care of repositioning views when view's frames are resized or
- /// if the terminal size changes.
- /// </para>
- /// <para>
- /// Absolute layout requires specifying coordiantes and sizes of Views explicitly, and the
- /// View will typcialy stay in a fixed position and size. To change the position and size use the
- /// <see cref="Frame"/> property.
- /// </para>
- /// <para>
- /// Subviews (child views) can be added to a View by calling the <see cref="Add(View)"/> method.
- /// The container of a View can be accessed with the <see cref="SuperView"/> property.
- /// </para>
- /// <para>
- /// To flag a region of the View's <see cref="Bounds"/> to be redrawn call <see cref="SetNeedsDisplay(Rect)"/>. To flag the entire view
- /// for redraw call <see cref="SetNeedsDisplay()"/>.
- /// </para>
- /// <para>
- /// Views have a <see cref="ColorScheme"/> property that defines the default colors that subviews
- /// should use for rendering. This ensures that the views fit in the context where
- /// they are being used, and allows for themes to be plugged in. For example, the
- /// default colors for windows and toplevels uses a blue background, while it uses
- /// a white background for dialog boxes and a red background for errors.
- /// </para>
- /// <para>
- /// Subclasses should not rely on <see cref="ColorScheme"/> being
- /// set at construction time. If a <see cref="ColorScheme"/> is not set on a view, the view will inherit the
- /// value from its <see cref="SuperView"/> and the value might only be valid once a view has been
- /// added to a SuperView.
- /// </para>
- /// <para>
- /// By using <see cref="ColorScheme"/> applications will work both
- /// in color as well as black and white displays.
- /// </para>
- /// <para>
- /// Views that are focusable should implement the <see cref="PositionCursor"/> to make sure that
- /// the cursor is placed in a location that makes sense. Unix terminals do not have
- /// a way of hiding the cursor, so it can be distracting to have the cursor left at
- /// the last focused view. So views should make sure that they place the cursor
- /// in a visually sensible place.
- /// </para>
- /// <para>
- /// The <see cref="LayoutSubviews"/> 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 <see cref="LayoutStyle.Absolute"/>, and will recompute the
- /// frames for the vies that use <see cref="LayoutStyle.Computed"/>.
- /// </para>
- /// </remarks>
- public class View : Responder, IEnumerable {
- /// <summary>
- /// Suppports text formatting, including horizontal alignment and word wrap for <see cref="View"/>.
- /// </summary>
- public class ViewText {
- List<ustring> lines = new List<ustring> ();
- ustring text;
- TextAlignment textAlignment;
- Attribute textColor = -1;
- View view;
- /// <summary>
- /// Inititalizes a new <see cref="ViewText"/> object.
- /// </summary>
- /// <param name="view"></param>
- public ViewText (View view)
- {
- this.view = view;
- recalcPending = true;
- }
- /// <summary>
- /// The text to be displayed.
- /// </summary>
- public virtual ustring Text {
- get => text;
- set {
- text = value;
- recalcPending = true;
- view.SetNeedsDisplay ();
- }
- }
- // TODO: Add Vertical Text Alignment
- /// <summary>
- /// Controls the horizontal text-alignment property.
- /// </summary>
- /// <value>The text alignment.</value>
- public TextAlignment TextAlignment {
- get => textAlignment;
- set {
- textAlignment = value;
- recalcPending = true;
- view.SetNeedsDisplay ();
- }
- }
- /// <summary>
- /// The color used for the drawing of the <see cref="Text"/>.
- /// </summary>
- public Attribute TextColor {
- get => textColor;
- set {
- textColor = value;
- recalcPending = true;
- view.SetNeedsDisplay ();
- }
- }
- /// <summary>
- /// Gets the size of the area the text will be drawn in.
- /// </summary>
- public Size TextSize { get; internal set; }
- bool recalcPending = false;
- public int HotKeyPos { get => hotKeyPos; set => hotKeyPos = value; }
- public Rune HotKey { get => hotKey; set => hotKey = value; }
- Rune hotKey;
- /// <summary>
- /// The specifier character for the hotkey (e.g. '_'). Set to '\xffff' to disable hotkey support for this View instance. The default is '\xffff'.
- /// </summary>
- public Rune HotKeySpecifier { get; set; } = (Rune)0xFFFF;
- /// <summary>
- /// Causes the Text to be formatted, based on <see cref="TextAlignment"/> and <see cref="TextSize"/>.
- /// </summary>
- public void ReFormat ()
- {
- // With this check, we protect against subclasses with overrides of Text
- if (ustring.IsNullOrEmpty (Text)) {
- return;
- }
- recalcPending = false;
- var shown_text = ProcessHotKeyText (text, HotKeySpecifier, false, out hotKeyPos, out hotKey);
- Reformat (shown_text, lines, TextSize.Width, textAlignment, TextSize.Height > 1);
- }
- static ustring StripCRLF (ustring str)
- {
- var runes = new List<Rune> ();
- foreach (var r in str.ToRunes ()) {
- if (r != '\r' && r != '\n') {
- runes.Add (r);
- }
- }
- return ustring.Make (runes); ;
- }
- static ustring ReplaceCRLFWithSpace (ustring str)
- {
- var runes = new List<Rune> ();
- foreach (var r in str.ToRunes ()) {
- if (r == '\r' || r == '\n') {
- runes.Add (new Rune (' ')); // r + 0x2400)); // U+25A1 □ WHITE SQUARE
- } else {
- runes.Add (r);
- }
- }
- return ustring.Make (runes); ;
- }
- static List<ustring> WordWrap (ustring text, int margin)
- {
- int start = 0, end;
- var lines = new List<ustring> ();
- text = StripCRLF (text);
- while ((end = start + margin) < text.Length) {
- while (text [end] != ' ' && end > start)
- end -= 1;
- if (end == start)
- end = start + margin;
- lines.Add (text [start, end]);
- start = end + 1;
- }
- if (start < text.Length)
- lines.Add (text.Substring (start));
- return lines;
- }
- static ustring ClipAndJustify (ustring str, int width, TextAlignment talign)
- {
- int slen = str.RuneCount;
- if (slen > width) {
- var uints = str.ToRunes (width);
- var runes = new Rune [uints.Length];
- for (int i = 0; i < uints.Length; i++)
- runes [i] = uints [i];
- return ustring.Make (runes);
- } else {
- if (talign == TextAlignment.Justified) {
- // TODO: ustring needs this
- var words = str.ToString ().Split (whitespace, StringSplitOptions.RemoveEmptyEntries);
- int textCount = words.Sum (arg => arg.Length);
- var spaces = words.Length > 1 ? (width - textCount) / (words.Length - 1) : 0;
- var extras = words.Length > 1 ? (width - textCount) % words.Length : 0;
- var s = new System.Text.StringBuilder ();
- //s.Append ($"tc={textCount} sp={spaces},x={extras} - ");
- for (int w = 0; w < words.Length; w++) {
- var x = words [w];
- s.Append (x);
- if (w + 1 < words.Length)
- for (int i = 0; i < spaces; i++)
- s.Append (' ');
- if (extras > 0) {
- //s.Append ('_');
- extras--;
- }
- }
- return ustring.Make (s.ToString ());
- }
- return str;
- }
- }
- static char [] whitespace = new char [] { ' ', '\t' };
- private int hotKeyPos;
- /// <summary>
- /// Reformats text into lines, applying text alignment and word wraping.
- /// </summary>
- /// <param name="textStr"></param>
- /// <param name="lineResult"></param>
- /// <param name="width"></param>
- /// <param name="talign"></param>
- /// <param name="wordWrap">if <c>false</c>, forces text to fit a single line. Line breaks are converted to spaces.</param>
- static void Reformat (ustring textStr, List<ustring> lineResult, int width, TextAlignment talign, bool wordWrap)
- {
- lineResult.Clear ();
- if (wordWrap == false) {
- textStr = ReplaceCRLFWithSpace (textStr);
- lineResult.Add (ClipAndJustify (textStr, width, talign));
- return;
- }
- int textLen = textStr.Length;
- int lp = 0;
- for (int i = 0; i < textLen; i++) {
- Rune c = textStr [i];
- if (c == '\n') {
- var wrappedLines = WordWrap (textStr [lp, i], width);
- foreach (var line in wrappedLines) {
- lineResult.Add (ClipAndJustify (line, width, talign));
- }
- if (wrappedLines.Count == 0) {
- lineResult.Add (ustring.Empty);
- }
- lp = i + 1;
- }
- }
- foreach (var line in WordWrap (textStr [lp, textLen], width)) {
- lineResult.Add (ClipAndJustify (line, width, talign));
- }
- }
- /// <summary>
- /// Computes the number of lines needed to render the specified text given the width.
- /// </summary>
- /// <returns>Number of lines.</returns>
- /// <param name="text">Text, may contain newlines.</param>
- /// <param name="width">The minimum width for the text.</param>
- public static int MaxLines (ustring text, int width)
- {
- var result = new List<ustring> ();
- ViewText.Reformat (text, result, width, TextAlignment.Left, true);
- return result.Count;
- }
- /// <summary>
- /// Computes the maximum width needed to render the text (single line or multple lines).
- /// </summary>
- /// <returns>Max width of lines.</returns>
- /// <param name="text">Text, may contain newlines.</param>
- /// <param name="width">The minimum width for the text.</param>
- public static int MaxWidth (ustring text, int width)
- {
- var result = new List<ustring> ();
- ViewText.Reformat (text, result, width, TextAlignment.Left, true);
- return result.Max (s => s.RuneCount);
- }
- internal void Draw (Rect bounds)
- {
- // With this check, we protect against subclasses with overrides of Text
- if (ustring.IsNullOrEmpty (text)) {
- return;
- }
- if (recalcPending) {
- ReFormat ();
- }
- if (TextColor != -1)
- Driver.SetAttribute (TextColor);
- else
- Driver.SetAttribute (view.ColorScheme.Normal);
- view.Clear ();
- for (int line = 0; line < lines.Count; line++) {
- if (line < bounds.Top || line >= bounds.Bottom)
- continue;
- var str = lines [line];
- int x;
- switch (textAlignment) {
- case TextAlignment.Left:
- x = 0;
- break;
- case TextAlignment.Justified:
- x = bounds.Left;
- break;
- case TextAlignment.Right:
- x = bounds.Right - str.Length;
- break;
- case TextAlignment.Centered:
- x = bounds.Left + (bounds.Width - str.Length) / 2;
- break;
- default:
- throw new ArgumentOutOfRangeException ();
- }
- view.Move (x, line);
- Driver.AddStr (str);
- }
- if (HotKeyPos != -1) {
- _ = GetAlignedText (lines [0], TextSize.Width, hotKeyPos, out hotKeyPos, textAlignment);
- view.Move (HotKeyPos, 0);
- Driver.SetAttribute (view.HasFocus ? view.ColorScheme.HotFocus : view.ColorScheme.HotNormal);
- Driver.AddRune (hotKey);
- }
- }
- /// <summary>
- /// Calculates the rectangle requried to hold text, assuming no word wrapping.
- /// </summary>
- /// <param name="x">The x location of the rectangle</param>
- /// <param name="y">The y location of the rectangle</param>
- /// <param name="text">The text to measure</param>
- /// <returns></returns>
- public static Rect CalcRect (int x, int y, ustring text)
- {
- if (ustring.IsNullOrEmpty (text))
- return Rect.Empty;
- int mw = 0;
- int ml = 1;
- int cols = 0;
- foreach (var rune in text) {
- if (rune == '\n') {
- ml++;
- if (cols > mw)
- mw = cols;
- cols = 0;
- } else
- cols++;
- }
- if (cols > mw)
- mw = cols;
- return new Rect (x, y, mw, ml);
- }
- /// <summary>
- /// Gets the position and Rune for the hotkey in text and removes the hotkey specifier.
- /// </summary>
- /// <param name="text">The text to manipulate.</param>
- /// <param name="hotKeySpecifier">The hot-key specifier (e.g. '_') to look for.</param>
- /// <param name="firstUpperCase">If <c>true</c> and no hotkey is found via the hotkey specifier, the first upper case char found will be the hotkey.</param>
- /// <param name="hotPos">Returns the postion of the hot-key in the text. -1 if not found.</param>
- /// <param name="showHotKey">Returns the Rune immediately to the right of the hot-key position</param>
- /// <returns>The input text with the hotkey specifier ('_') removed.</returns>
- public static ustring ProcessHotKeyText (ustring text, Rune hotKeySpecifier, bool firstUpperCase, out int hotPos, out Rune showHotKey)
- {
- if (hotKeySpecifier == (Rune)0xFFFF) {
- hotPos = -1;
- showHotKey = (Rune)0xFFFF;
- return text;
- }
- Rune hot_key = (Rune)0;
- int hot_pos = -1;
- ustring shown_text = text;
- // Use first hot_key char passed into 'hotKey'.
- // TODO: Ignore hot_key of two are provided
- int i = 0;
- foreach (Rune c in shown_text) {
- if ((char)c != 0xFFFD) {
- if (c == hotKeySpecifier) {
- hot_pos = i;
- } else if (hot_pos > -1) {
- hot_key = c;
- break;
- }
- }
- i++;
- }
- // Legacy support - use first upper case char if the specifier was not found
- if (hot_pos == -1 && firstUpperCase) {
- i = 0;
- foreach (Rune c in shown_text) {
- if ((char)c != 0xFFFD) {
- if (Rune.IsUpper (c)) {
- hot_key = c;
- hot_pos = i;
- break;
- }
- }
- i++;
- }
- }
- else {
- if (hot_pos != -1) {
- // Use char after 'hotKey'
- ustring start = "";
- i = 0;
- foreach (Rune c in shown_text) {
- start += ustring.Make (c);
- i++;
- if (i == hot_pos)
- break;
- }
- var st = shown_text;
- shown_text = start;
- i = 0;
- foreach (Rune c in st) {
- i++;
- if (i > hot_pos + 1) {
- shown_text += ustring.Make (c);
- }
- }
- }
- }
- hotPos = hot_pos;
- showHotKey = hot_key;
- return shown_text;
- }
- /// <summary>
- /// Formats a single line of text with a hot-key and <see cref="TextAlignment"/>.
- /// </summary>
- /// <param name="shown_text">The text to align.</param>
- /// <param name="width">The maximum width for the text.</param>
- /// <param name="hot_pos">The hot-key position before reformatting.</param>
- /// <param name="c_hot_pos">The hot-key position after reformatting.</param>
- /// <param name="textAlignment">The <see cref="TextAlignment"/> to align to.</param>
- /// <returns>The aligned text.</returns>
- public static ustring GetAlignedText (ustring shown_text, int width, int hot_pos, out int c_hot_pos, TextAlignment textAlignment)
- {
- int start;
- var caption = shown_text;
- c_hot_pos = hot_pos;
- if (width > shown_text.Length + 1) {
- switch (textAlignment) {
- case TextAlignment.Left:
- caption += new string (' ', width - caption.RuneCount);
- break;
- case TextAlignment.Right:
- start = width - caption.RuneCount;
- caption = $"{new string (' ', width - caption.RuneCount)}{caption}";
- if (c_hot_pos > -1) {
- c_hot_pos += start;
- }
- break;
- case TextAlignment.Centered:
- start = width / 2 - caption.RuneCount / 2;
- caption = $"{new string (' ', start)}{caption}{new string (' ', width - caption.RuneCount - start)}";
- if (c_hot_pos > -1) {
- c_hot_pos += start;
- }
- break;
- case TextAlignment.Justified:
- var words = caption.Split (" ");
- var wLen = GetWordsLength (words, c_hot_pos, out int runeCount, out int w_hot_pos);
- var space = (width - runeCount) / (caption.Length - wLen);
- caption = "";
- for (int i = 0; i < words.Length; i++) {
- if (i == words.Length - 1) {
- caption += new string (' ', width - caption.RuneCount - 1);
- caption += words [i];
- } else {
- caption += words [i];
- }
- if (i < words.Length - 1) {
- caption += new string (' ', space);
- }
- }
- if (c_hot_pos > -1) {
- c_hot_pos += w_hot_pos * space - space - w_hot_pos + 1;
- }
- break;
- }
- }
- return caption;
- }
- static int GetWordsLength (ustring [] words, int hotPos, out int runeCount, out int wordHotPos)
- {
- int length = 0;
- int rCount = 0;
- int wHotPos = -1;
- for (int i = 0; i < words.Length; i++) {
- if (wHotPos == -1 && rCount + words [i].RuneCount >= hotPos)
- wHotPos = i;
- length += words [i].Length;
- rCount += words [i].RuneCount;
- }
- if (wHotPos == -1 && hotPos > -1)
- wHotPos = words.Length;
- runeCount = rCount;
- wordHotPos = wHotPos;
- return length;
- }
- }
- internal enum Direction {
- Forward,
- Backward
- }
- // container == SuperView
- View container = null;
- View focused = null;
- Direction focusDirection;
- ViewText viewText;
- /// <summary>
- /// Event fired when the view gets focus.
- /// </summary>
- public Action<FocusEventArgs> Enter;
- /// <summary>
- /// Event fired when the view looses focus.
- /// </summary>
- public Action<FocusEventArgs> Leave;
- /// <summary>
- /// Event fired when the view receives the mouse event for the first time.
- /// </summary>
- public Action<MouseEventArgs> MouseEnter;
- /// <summary>
- /// Event fired when the view receives a mouse event for the last time.
- /// </summary>
- public Action<MouseEventArgs> MouseLeave;
- /// <summary>
- /// Event fired when a mouse event is generated.
- /// </summary>
- public Action<MouseEventArgs> MouseClick;
- /// <summary>
- /// The HotKey defined for this view. A user pressing HotKey on the keyboard while this view has focus will cause the Clicked event to fire.
- /// </summary>
- public Rune HotKey { get => viewText.HotKey; set => viewText.HotKey = value; }
- /// <summary>
- ///
- /// </summary>
- public Rune HotKeySpecifier { get => viewText.HotKeySpecifier; set => viewText.HotKeySpecifier = value; }
- /// <summary>
- /// Clicked <see cref="Action"/>, raised when the user clicks the primary mouse button within the Bounds of this <see cref="View"/>
- /// or if the user presses the action key while this view is focused. (TODO: IsDefault)
- /// </summary>
- /// <remarks>
- /// Client code can hook up to this event, it is
- /// raised when the button is activated either with
- /// the mouse or the keyboard.
- /// </remarks>
- public Action Clicked;
- internal Direction FocusDirection {
- get => SuperView?.FocusDirection ?? focusDirection;
- set {
- if (SuperView != null)
- SuperView.FocusDirection = value;
- else
- focusDirection = value;
- }
- }
- /// <summary>
- /// Points to the current driver in use by the view, it is a convenience property
- /// for simplifying the development of new views.
- /// </summary>
- public static ConsoleDriver Driver { get { return Application.Driver; } }
- static IList<View> empty = new List<View> (0).AsReadOnly ();
- // This is null, and allocated on demand.
- List<View> subviews;
- /// <summary>
- /// This returns a list of the subviews contained by this view.
- /// </summary>
- /// <value>The subviews.</value>
- public IList<View> Subviews => subviews == null ? empty : subviews.AsReadOnly ();
- // 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.
- internal IList<View> InternalSubviews => subviews ?? empty;
- internal Rect NeedDisplay { get; private set; } = Rect.Empty;
- // The frame for the object. Superview relative.
- Rect frame;
- /// <summary>
- /// Gets or sets an identifier for the view;
- /// </summary>
- /// <value>The identifier.</value>
- /// <remarks>The id should be unique across all Views that share a SuperView.</remarks>
- public ustring Id { get; set; } = "";
- /// <summary>
- /// Returns a value indicating if this View is currently on Top (Active)
- /// </summary>
- public bool IsCurrentTop {
- get {
- return Application.Current == this;
- }
- }
- /// <summary>
- /// Gets or sets a value indicating whether this <see cref="View"/> wants mouse position reports.
- /// </summary>
- /// <value><c>true</c> if want mouse position reports; otherwise, <c>false</c>.</value>
- public virtual bool WantMousePositionReports { get; set; } = false;
- /// <summary>
- /// Gets or sets a value indicating whether this <see cref="View"/> want continuous button pressed event.
- /// </summary>
- public virtual bool WantContinuousButtonPressed { get; set; } = false;
- /// <summary>
- /// Gets or sets the frame for the view. The frame is relative to the view's container (<see cref="SuperView"/>).
- /// </summary>
- /// <value>The frame.</value>
- /// <remarks>
- /// <para>
- /// Change the Frame when using the <see cref="LayoutStyle.Absolute"/> layout style to move or resize views.
- /// </para>
- /// <para>
- /// Altering the Frame of a view will trigger the redrawing of the
- /// view as well as the redrawing of the affected regions of the <see cref="SuperView"/>.
- /// </para>
- /// </remarks>
- public virtual Rect Frame {
- get => frame;
- set {
- if (SuperView != null) {
- SuperView.SetNeedsDisplay (frame);
- SuperView.SetNeedsDisplay (value);
- }
- frame = value;
- SetNeedsLayout ();
- SetNeedsDisplay (frame);
- }
- }
- /// <summary>
- /// Gets an enumerator that enumerates the subviews in this view.
- /// </summary>
- /// <returns>The enumerator.</returns>
- public IEnumerator GetEnumerator ()
- {
- foreach (var v in InternalSubviews)
- yield return v;
- }
- LayoutStyle layoutStyle;
- /// <summary>
- /// Controls how the View's <see cref="Frame"/> is computed during the LayoutSubviews method, if the style is set to <see cref="LayoutStyle.Absolute"/>,
- /// LayoutSubviews does not change the <see cref="Frame"/>. If the style is <see cref="LayoutStyle.Computed"/> the <see cref="Frame"/> is updated using
- /// the <see cref="X"/>, <see cref="Y"/>, <see cref="Width"/>, and <see cref="Height"/> properties.
- /// </summary>
- /// <value>The layout style.</value>
- public LayoutStyle LayoutStyle {
- get => layoutStyle;
- set {
- layoutStyle = value;
- SetNeedsLayout ();
- }
- }
- /// <summary>
- /// The bounds represent the View-relative rectangle used for this view; the area inside of the view.
- /// </summary>
- /// <value>The bounds.</value>
- /// <remarks>
- /// <para>
- /// Updates to the Bounds update the <see cref="Frame"/>,
- /// and has the same side effects as updating the <see cref="Frame"/>.
- /// </para>
- /// <para>
- /// Because <see cref="Bounds"/> coordinates are relative to the upper-left corner of the <see cref="View"/>,
- /// the coordinates of the upper-left corner of the rectangle returned by this property are (0,0).
- /// Use this property to obtain the size and coordinates of the client area of the
- /// control for tasks such as drawing on the surface of the control.
- /// </para>
- /// </remarks>
- public Rect Bounds {
- get => new Rect (Point.Empty, Frame.Size);
- set {
- Frame = new Rect (frame.Location, value.Size);
- }
- }
- Pos x, y;
- /// <summary>
- /// Gets or sets the X position for the view (the column). Only used whe <see cref="LayoutStyle"/> is <see cref="LayoutStyle.Computed"/>.
- /// </summary>
- /// <value>The X Position.</value>
- /// <remarks>
- /// If <see cref="LayoutStyle"/> is <see cref="LayoutStyle.Absolute"/> changing this property has no effect and its value is indeterminate.
- /// </remarks>
- public Pos X {
- get => x;
- set {
- x = value;
- SetNeedsLayout ();
- SetNeedsDisplay (frame);
- }
- }
- /// <summary>
- /// Gets or sets the Y position for the view (the row). Only used whe <see cref="LayoutStyle"/> is <see cref="LayoutStyle.Computed"/>.
- /// </summary>
- /// <value>The y position (line).</value>
- /// <remarks>
- /// If <see cref="LayoutStyle"/> is <see cref="LayoutStyle.Absolute"/> changing this property has no effect and its value is indeterminate.
- /// </remarks>
- public Pos Y {
- get => y;
- set {
- y = value;
- SetNeedsLayout ();
- SetNeedsDisplay (frame);
- }
- }
- Dim width, height;
- /// <summary>
- /// Gets or sets the width of the view. Only used whe <see cref="LayoutStyle"/> is <see cref="LayoutStyle.Computed"/>.
- /// </summary>
- /// <value>The width.</value>
- /// <remarks>
- /// If <see cref="LayoutStyle"/> is <see cref="LayoutStyle.Absolute"/> changing this property has no effect and its value is indeterminate.
- /// </remarks>
- public Dim Width {
- get => width;
- set {
- width = value;
- SetNeedsLayout ();
- SetNeedsDisplay (frame);
- }
- }
- /// <summary>
- /// Gets or sets the height of the view. Only used whe <see cref="LayoutStyle"/> is <see cref="LayoutStyle.Computed"/>.
- /// </summary>
- /// <value>The height.</value>
- /// If <see cref="LayoutStyle"/> is <see cref="LayoutStyle.Absolute"/> changing this property has no effect and its value is indeterminate.
- public Dim Height {
- get => height;
- set {
- height = value;
- SetNeedsLayout ();
- SetNeedsDisplay (frame);
- }
- }
- /// <summary>
- /// Returns the container for this view, or null if this view has not been added to a container.
- /// </summary>
- /// <value>The super view.</value>
- public View SuperView => container;
- /// <summary>
- /// Initializes a new instance of a <see cref="LayoutStyle.Absolute"/> <see cref="View"/> class with the absolute
- /// dimensions specified in the <c>frame</c> parameter.
- /// </summary>
- /// <param name="frame">The region covered by this view.</param>
- /// <remarks>
- /// This constructor intitalize a View with a <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Absolute"/>. Use <see cref="View()"/> to
- /// initialize a View with <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Computed"/>
- /// </remarks>
- public View (Rect frame)
- {
- viewText = new ViewText (this);
- this.Text = ustring.Empty;
- this.Frame = frame;
- LayoutStyle = LayoutStyle.Absolute;
- }
- /// <summary>
- /// Initializes a new instance of <see cref="View"/> using <see cref="LayoutStyle.Computed"/> layout.
- /// </summary>
- /// <remarks>
- /// <para>
- /// Use <see cref="X"/>, <see cref="Y"/>, <see cref="Width"/>, and <see cref="Height"/> properties to dynamically control the size and location of the view.
- /// The <see cref="Label"/> will be created using <see cref="LayoutStyle.Computed"/>
- /// coordinates. The initial size (<see cref="View.Frame"/> will be
- /// adjusted to fit the contents of <see cref="Text"/>, including newlines ('\n') for multiple lines.
- /// </para>
- /// <para>
- /// If <c>Height</c> is greater than one, word wrapping is provided.
- /// </para>
- /// <para>
- /// This constructor intitalize a View with a <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Computed"/>.
- /// Use <see cref="X"/>, <see cref="Y"/>, <see cref="Width"/>, and <see cref="Height"/> properties to dynamically control the size and location of the view.
- /// </para>
- /// </remarks>
- public View () : this (text: string.Empty) { }
- /// <summary>
- /// Initializes a new instance of <see cref="View"/> using <see cref="LayoutStyle.Absolute"/> layout.
- /// </summary>
- /// <remarks>
- /// <para>
- /// The <see cref="View"/> will be created at the given
- /// coordinates with the given string. The size (<see cref="View.Frame"/> will be
- /// adjusted to fit the contents of <see cref="Text"/>, including newlines ('\n') for multiple lines.
- /// </para>
- /// <para>
- /// No line wrapping is provided.
- /// </para>
- /// </remarks>
- /// <param name="x">column to locate the Label.</param>
- /// <param name="y">row to locate the Label.</param>
- /// <param name="text">text to initialize the <see cref="Text"/> property with.</param>
- public View (int x, int y, ustring text) : this (ViewText.CalcRect (x, y, text), text) { }
- /// <summary>
- /// Initializes a new instance of <see cref="View"/> using <see cref="LayoutStyle.Absolute"/> layout.
- /// </summary>
- /// <remarks>
- /// <para>
- /// The <see cref="View"/> will be created at the given
- /// coordinates with the given string. The initial size (<see cref="View.Frame"/> will be
- /// adjusted to fit the contents of <see cref="Text"/>, including newlines ('\n') for multiple lines.
- /// </para>
- /// <para>
- /// If <c>rect.Height</c> is greater than one, word wrapping is provided.
- /// </para>
- /// </remarks>
- /// <param name="rect">Location.</param>
- /// <param name="text">text to initialize the <see cref="Text"/> property with.</param>
- public View (Rect rect, ustring text) : this (rect)
- {
- viewText = new ViewText (this);
- this.Text = text;
- }
- /// <summary>
- /// Initializes a new instance of <see cref="View"/> using <see cref="LayoutStyle.Computed"/> layout.
- /// </summary>
- /// <remarks>
- /// <para>
- /// The <see cref="View"/> will be created using <see cref="LayoutStyle.Computed"/>
- /// coordinates with the given string. The initial size (<see cref="View.Frame"/> will be
- /// adjusted to fit the contents of <see cref="Text"/>, including newlines ('\n') for multiple lines.
- /// </para>
- /// <para>
- /// If <c>Height</c> is greater than one, word wrapping is provided.
- /// </para>
- /// </remarks>
- /// <param name="text">text to initialize the <see cref="Text"/> property with.</param>
- public View (ustring text) : base ()
- {
- viewText = new ViewText (this);
- this.Text = text;
- CanFocus = false;
- LayoutStyle = LayoutStyle.Computed;
- var r = ViewText.CalcRect (0, 0, text);
- x = Pos.At (0);
- y = Pos.At (0);
- Width = r.Width;
- Height = r.Height;
- }
- /// <summary>
- /// Sets a flag indicating this view needs to be redisplayed because its state has changed.
- /// </summary>
- public void SetNeedsDisplay ()
- {
- SetNeedsDisplay (Bounds);
- }
- internal bool layoutNeeded = true;
- internal void SetNeedsLayout ()
- {
- if (layoutNeeded)
- return;
- layoutNeeded = true;
- if (SuperView == null)
- return;
- SuperView.SetNeedsLayout ();
- viewText.ReFormat ();
- }
- /// <summary>
- /// Flags the view-relative region on this View as needing to be repainted.
- /// </summary>
- /// <param name="region">The view-relative region that must be flagged for repaint.</param>
- public void SetNeedsDisplay (Rect region)
- {
- if (NeedDisplay == null || NeedDisplay.IsEmpty)
- NeedDisplay = region;
- else {
- var x = Math.Min (NeedDisplay.X, region.X);
- var y = Math.Min (NeedDisplay.Y, region.Y);
- var w = Math.Max (NeedDisplay.Width, region.Width);
- var h = Math.Max (NeedDisplay.Height, region.Height);
- NeedDisplay = new Rect (x, y, w, h);
- }
- if (container != null)
- container.ChildNeedsDisplay ();
- if (subviews == null)
- return;
- foreach (var view in subviews)
- if (view.Frame.IntersectsWith (region)) {
- var childRegion = Rect.Intersect (view.Frame, region);
- childRegion.X -= view.Frame.X;
- childRegion.Y -= view.Frame.Y;
- view.SetNeedsDisplay (childRegion);
- }
- }
- internal bool childNeedsDisplay;
- /// <summary>
- /// Indicates that any child views (in the <see cref="Subviews"/> list) need to be repainted.
- /// </summary>
- public void ChildNeedsDisplay ()
- {
- childNeedsDisplay = true;
- if (container != null)
- container.ChildNeedsDisplay ();
- }
- /// <summary>
- /// Adds a subview (child) to this view.
- /// </summary>
- /// <remarks>
- /// The Views that have been added to this view can be retrieved via the <see cref="Subviews"/> property. See also <seealso cref="Remove(View)"/> <seealso cref="RemoveAll"/>
- /// </remarks>
- public virtual void Add (View view)
- {
- if (view == null)
- return;
- if (subviews == null)
- subviews = new List<View> ();
- subviews.Add (view);
- view.container = this;
- if (view.CanFocus)
- CanFocus = true;
- SetNeedsLayout ();
- SetNeedsDisplay ();
- }
- /// <summary>
- /// Adds the specified views (children) to the view.
- /// </summary>
- /// <param name="views">Array of one or more views (can be optional parameter).</param>
- /// <remarks>
- /// The Views that have been added to this view can be retrieved via the <see cref="Subviews"/> property. See also <seealso cref="Remove(View)"/> <seealso cref="RemoveAll"/>
- /// </remarks>
- public void Add (params View [] views)
- {
- if (views == null)
- return;
- foreach (var view in views)
- Add (view);
- }
- /// <summary>
- /// Removes all subviews (children) added via <see cref="Add(View)"/> or <see cref="Add(View[])"/> from this View.
- /// </summary>
- public virtual void RemoveAll ()
- {
- if (subviews == null)
- return;
- while (subviews.Count > 0) {
- Remove (subviews [0]);
- }
- }
- /// <summary>
- /// Removes a subview added via <see cref="Add(View)"/> or <see cref="Add(View[])"/> from this View.
- /// </summary>
- /// <remarks>
- /// </remarks>
- public virtual void Remove (View view)
- {
- if (view == null || subviews == null)
- return;
- SetNeedsLayout ();
- SetNeedsDisplay ();
- var touched = view.Frame;
- subviews.Remove (view);
- view.container = null;
- if (subviews.Count < 1)
- this.CanFocus = false;
- foreach (var v in subviews) {
- if (v.Frame.IntersectsWith (touched))
- view.SetNeedsDisplay ();
- }
- }
- void PerformActionForSubview (View subview, Action<View> action)
- {
- if (subviews.Contains (subview)) {
- action (subview);
- }
- SetNeedsDisplay ();
- subview.SetNeedsDisplay ();
- }
- /// <summary>
- /// Brings the specified subview to the front so it is drawn on top of any other views.
- /// </summary>
- /// <param name="subview">The subview to send to the front</param>
- /// <remarks>
- /// <seealso cref="SendSubviewToBack"/>.
- /// </remarks>
- public void BringSubviewToFront (View subview)
- {
- PerformActionForSubview (subview, x => {
- subviews.Remove (x);
- subviews.Add (x);
- });
- }
- /// <summary>
- /// Sends the specified subview to the front so it is the first view drawn
- /// </summary>
- /// <param name="subview">The subview to send to the front</param>
- /// <remarks>
- /// <seealso cref="BringSubviewToFront(View)"/>.
- /// </remarks>
- public void SendSubviewToBack (View subview)
- {
- PerformActionForSubview (subview, x => {
- subviews.Remove (x);
- subviews.Insert (0, subview);
- });
- }
- /// <summary>
- /// Moves the subview backwards in the hierarchy, only one step
- /// </summary>
- /// <param name="subview">The subview to send backwards</param>
- /// <remarks>
- /// If you want to send the view all the way to the back use SendSubviewToBack.
- /// </remarks>
- public void SendSubviewBackwards (View subview)
- {
- PerformActionForSubview (subview, x => {
- var idx = subviews.IndexOf (x);
- if (idx > 0) {
- subviews.Remove (x);
- subviews.Insert (idx - 1, x);
- }
- });
- }
- /// <summary>
- /// Moves the subview backwards in the hierarchy, only one step
- /// </summary>
- /// <param name="subview">The subview to send backwards</param>
- /// <remarks>
- /// If you want to send the view all the way to the back use SendSubviewToBack.
- /// </remarks>
- public void BringSubviewForward (View subview)
- {
- PerformActionForSubview (subview, x => {
- var idx = subviews.IndexOf (x);
- if (idx + 1 < subviews.Count) {
- subviews.Remove (x);
- subviews.Insert (idx + 1, x);
- }
- });
- }
- /// <summary>
- /// Clears the view region with the current color.
- /// </summary>
- /// <remarks>
- /// <para>
- /// This clears the entire region used by this view.
- /// </para>
- /// </remarks>
- public void Clear ()
- {
- var h = Frame.Height;
- var w = Frame.Width;
- for (int line = 0; line < h; line++) {
- Move (0, line);
- for (int col = 0; col < w; col++)
- Driver.AddRune (' ');
- }
- }
- /// <summary>
- /// Clears the specified region with the current color.
- /// </summary>
- /// <remarks>
- /// </remarks>
- /// <param name="regionScreen">The screen-relative region to clear.</param>
- public void Clear (Rect regionScreen)
- {
- var h = regionScreen.Height;
- var w = regionScreen.Width;
- for (int line = regionScreen.Y; line < regionScreen.Y + h; line++) {
- Driver.Move (regionScreen.X, line);
- for (int col = 0; col < w; col++)
- Driver.AddRune (' ');
- }
- }
- /// <summary>
- /// Converts a view-relative (col,row) position to a screen-relative position (col,row). The values are optionally clamped to the screen dimensions.
- /// </summary>
- /// <param name="col">View-relative column.</param>
- /// <param name="row">View-relative row.</param>
- /// <param name="rcol">Absolute column; screen-relative.</param>
- /// <param name="rrow">Absolute row; screen-relative.</param>
- /// <param name="clipped">Whether to clip the result of the ViewToScreen method, if set to <c>true</c>, the rcol, rrow values are clamped to the screen (terminal) dimensions (0..TerminalDim-1).</param>
- internal void ViewToScreen (int col, int row, out int rcol, out int rrow, bool clipped = true)
- {
- // 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;
- }
- // The following ensures that the cursor is always in the screen boundaries.
- if (clipped) {
- rrow = Math.Min (rrow, Driver.Rows - 1);
- rcol = Math.Min (rcol, Driver.Cols - 1);
- }
- }
- /// <summary>
- /// Converts a point from screen-relative coordinates to view-relative coordinates.
- /// </summary>
- /// <returns>The mapped point.</returns>
- /// <param name="x">X screen-coordinate point.</param>
- /// <param name="y">Y screen-coordinate point.</param>
- public Point ScreenToView (int x, int y)
- {
- if (SuperView == null) {
- return new Point (x - Frame.X, y - frame.Y);
- } else {
- var parent = SuperView.ScreenToView (x, y);
- return new Point (parent.X - frame.X, parent.Y - frame.Y);
- }
- }
- /// <summary>
- /// Converts a region in view-relative coordinates to screen-relative coordinates.
- /// </summary>
- internal Rect ViewToScreen (Rect region)
- {
- ViewToScreen (region.X, region.Y, out var x, out var y, clipped: false);
- return new Rect (x, y, region.Width, region.Height);
- }
- // Clips a rectangle in screen coordinates to the dimensions currently available on the screen
- internal Rect ScreenClip (Rect regionScreen)
- {
- var x = regionScreen.X < 0 ? 0 : regionScreen.X;
- var y = regionScreen.Y < 0 ? 0 : regionScreen.Y;
- var w = regionScreen.X + regionScreen.Width >= Driver.Cols ? Driver.Cols - regionScreen.X : regionScreen.Width;
- var h = regionScreen.Y + regionScreen.Height >= Driver.Rows ? Driver.Rows - regionScreen.Y : regionScreen.Height;
- return new Rect (x, y, w, h);
- }
- /// <summary>
- /// Sets the <see cref="ConsoleDriver"/>'s clip region to the current View's <see cref="Bounds"/>.
- /// </summary>
- /// <returns>The existing driver's clip region, which can be then re-eapplied by setting <c><see cref="Driver"/>.Clip</c> (<see cref="ConsoleDriver.Clip"/>).</returns>
- /// <remarks>
- /// <see cref="Bounds"/> is View-relative.
- /// </remarks>
- public Rect ClipToBounds ()
- {
- return SetClip (Bounds);
- }
- /// <summary>
- /// Sets the clip region to the specified view-relative region.
- /// </summary>
- /// <returns>The previous screen-relative clip region.</returns>
- /// <param name="region">View-relative clip region.</param>
- public Rect SetClip (Rect region)
- {
- var previous = Driver.Clip;
- Driver.Clip = Rect.Intersect (previous, ViewToScreen (region));
- return previous;
- }
- /// <summary>
- /// Draws a frame in the current view, clipped by the boundary of this view
- /// </summary>
- /// <param name="region">View-relative region for the frame to be drawn.</param>
- /// <param name="padding">The padding to add around the outside of the drawn frame.</param>
- /// <param name="fill">If set to <c>true</c> it fill will the contents.</param>
- public void DrawFrame (Rect region, int padding = 0, bool fill = false)
- {
- var scrRect = ViewToScreen (region);
- var savedClip = ClipToBounds ();
- Driver.DrawWindowFrame (scrRect, padding + 1, padding + 1, padding + 1, padding + 1, border: true, fill: fill);
- Driver.Clip = savedClip;
- }
- /// <summary>
- /// Utility function to draw strings that contain a hotkey.
- /// </summary>
- /// <param name="text">String to display, the underscoore before a letter flags the next letter as the hotkey.</param>
- /// <param name="hotColor">Hot color.</param>
- /// <param name="normalColor">Normal color.</param>
- /// <remarks>
- /// The hotkey is any character following an underscore ('_') character.</remarks>
- public void DrawHotString (ustring text, Attribute hotColor, Attribute normalColor)
- {
- Driver.SetAttribute (normalColor);
- foreach (var rune in text) {
- if (rune == '_') {
- Driver.SetAttribute (hotColor);
- continue;
- }
- Driver.AddRune (rune);
- Driver.SetAttribute (normalColor);
- }
- }
- /// <summary>
- /// Utility function to draw strings that contains a hotkey using a <see cref="ColorScheme"/> and the "focused" state.
- /// </summary>
- /// <param name="text">String to display, the underscoore before a letter flags the next letter as the hotkey.</param>
- /// <param name="focused">If set to <c>true</c> this uses the focused colors from the color scheme, otherwise the regular ones.</param>
- /// <param name="scheme">The color scheme to use.</param>
- public void DrawHotString (ustring text, bool focused, ColorScheme scheme)
- {
- if (focused)
- DrawHotString (text, scheme.HotFocus, scheme.Focus);
- else
- DrawHotString (text, scheme.HotNormal, scheme.Normal);
- }
- /// <summary>
- /// This moves the cursor to the specified column and row in the view.
- /// </summary>
- /// <returns>The move.</returns>
- /// <param name="col">Col.</param>
- /// <param name="row">Row.</param>
- public void Move (int col, int row)
- {
- ViewToScreen (col, row, out var rcol, out var rrow);
- Driver.Move (rcol, rrow);
- }
- /// <summary>
- /// Positions the cursor in the right position based on the currently focused view in the chain.
- /// </summary>
- /// Views that are focusable should override <see cref="PositionCursor"/> to ensure
- /// the cursor is placed in a location that makes sense. Unix terminals do not have
- /// a way of hiding the cursor, so it can be distracting to have the cursor left at
- /// the last focused view. Views should make sure that they place the cursor
- /// in a visually sensible place.
- public virtual void PositionCursor ()
- {
- if (focused != null)
- focused.PositionCursor ();
- else {
- if (CanFocus && HasFocus) {
- Move (viewText.HotKeyPos == -1 ? 1 : viewText.HotKeyPos, 0);
- } else {
- Move (frame.X, frame.Y);
- }
- }
- }
- /// <inheritdoc/>
- public override bool HasFocus {
- get {
- return base.HasFocus;
- }
- internal set {
- if (base.HasFocus != value)
- if (value)
- OnEnter ();
- else
- OnLeave ();
- SetNeedsDisplay ();
- base.HasFocus = value;
- // Remove focus down the chain of subviews if focus is removed
- if (!value && focused != null) {
- focused.OnLeave ();
- focused.HasFocus = false;
- focused = null;
- }
- }
- }
- /// <summary>
- /// Defines the event arguments for <see cref="SetFocus(View)"/>
- /// </summary>
- public class FocusEventArgs : EventArgs {
- /// <summary>
- /// Constructs.
- /// </summary>
- public FocusEventArgs () { }
- /// <summary>
- /// Indicates if the current focus event has already been processed and the driver should stop notifying any other event subscriber.
- /// Its important to set this value to true specially when updating any View's layout from inside the subscriber method.
- /// </summary>
- public bool Handled { get; set; }
- }
- /// <inheritdoc/>
- public override bool OnEnter ()
- {
- FocusEventArgs args = new FocusEventArgs ();
- Enter?.Invoke (args);
- if (args.Handled)
- return true;
- if (base.OnEnter ())
- return true;
- return false;
- }
- /// <inheritdoc/>
- public override bool OnLeave ()
- {
- FocusEventArgs args = new FocusEventArgs ();
- Leave?.Invoke (args);
- if (args.Handled)
- return true;
- if (base.OnLeave ())
- return true;
- return false;
- }
- /// <summary>
- /// Returns the currently focused view inside this view, or null if nothing is focused.
- /// </summary>
- /// <value>The focused.</value>
- public View Focused => focused;
- /// <summary>
- /// Returns the most focused view in the chain of subviews (the leaf view that has the focus).
- /// </summary>
- /// <value>The most focused.</value>
- public View MostFocused {
- get {
- if (Focused == null)
- return null;
- var most = Focused.MostFocused;
- if (most != null)
- return most;
- return Focused;
- }
- }
- /// <summary>
- /// The color scheme for this view, if it is not defined, it returns the <see cref="SuperView"/>'s
- /// color scheme.
- /// </summary>
- public ColorScheme ColorScheme {
- get {
- if (colorScheme == null)
- return SuperView?.ColorScheme;
- return colorScheme;
- }
- set {
- colorScheme = value;
- }
- }
- ColorScheme colorScheme;
- /// <summary>
- /// Displays the specified character in the specified column and row of the View.
- /// </summary>
- /// <param name="col">Column (view-relative).</param>
- /// <param name="row">Row (view-relative).</param>
- /// <param name="ch">Ch.</param>
- public void AddRune (int col, int row, Rune ch)
- {
- if (row < 0 || col < 0)
- return;
- if (row > frame.Height - 1 || col > frame.Width - 1)
- return;
- Move (col, row);
- Driver.AddRune (ch);
- }
- /// <summary>
- /// Removes the <see cref="SetNeedsDisplay()"/> and the <see cref="ChildNeedsDisplay"/> setting on this view.
- /// </summary>
- protected void ClearNeedsDisplay ()
- {
- NeedDisplay = Rect.Empty;
- childNeedsDisplay = false;
- }
- /// <summary>
- /// Redraws this view and its subviews; only redraws the views that have been flagged for a re-display.
- /// </summary>
- /// <param name="bounds">The bounds (view-relative region) to redraw.</param>
- /// <remarks>
- /// <para>
- /// Always use <see cref="Bounds"/> (view-relative) when calling <see cref="Redraw(Rect)"/>, NOT <see cref="Frame"/> (superview-relative).
- /// </para>
- /// <para>
- /// Views should set the color that they want to use on entry, as otherwise this will inherit
- /// the last color that was set globaly on the driver.
- /// </para>
- /// <para>
- /// Overrides of <see cref="Redraw"/> must ensure they do not set <c>Driver.Clip</c> to a clip region
- /// larger than the <c>region</c> parameter.
- /// </para>
- /// </remarks>
- public virtual void Redraw (Rect bounds)
- {
- var clipRect = new Rect (Point.Empty, frame.Size);
- Driver.SetAttribute (HasFocus ? ColorScheme.Focus : ColorScheme.Normal);
- // Draw any Text
- // TODO: Figure out if this should go here or after OnDrawContent
- viewText?.ReFormat ();
- viewText?.Draw (bounds);
- // Invoke DrawContentEvent
- OnDrawContent (bounds);
- if (subviews != null) {
- foreach (var view in subviews) {
- if (view.NeedDisplay != null && (!view.NeedDisplay.IsEmpty || view.childNeedsDisplay)) {
- if (view.Frame.IntersectsWith (clipRect) && (view.Frame.IntersectsWith (bounds) || bounds.X < 0 || bounds.Y < 0)) {
- // FIXED: optimize this by computing the intersection of region and view.Bounds
- if (view.layoutNeeded)
- view.LayoutSubviews ();
- Application.CurrentView = view;
- // Draw the subview
- // Use the view's bounds (view-relative; Location will always be (0,0) because
- view.Redraw (view.Bounds);
- }
- view.NeedDisplay = Rect.Empty;
- view.childNeedsDisplay = false;
- }
- }
- }
- ClearNeedsDisplay ();
- }
- /// <summary>
- /// Event invoked when the content area of the View is to be drawn.
- /// </summary>
- /// <remarks>
- /// <para>
- /// Will be invoked before any subviews added with <see cref="Add(View)"/> have been drawn.
- /// </para>
- /// <para>
- /// Rect provides the view-relative rectangle describing the currently visible viewport into the <see cref="View"/>.
- /// </para>
- /// </remarks>
- public Action<Rect> DrawContent;
- /// <summary>
- /// Enables overrides to draw infinitely scrolled content and/or a background behind added controls.
- /// </summary>
- /// <param name="viewport">The view-relative rectangle describing the currently visible viewport into the <see cref="View"/></param>
- /// <remarks>
- /// This method will be called before any subviews added with <see cref="Add(View)"/> have been drawn.
- /// </remarks>
- public virtual void OnDrawContent (Rect viewport)
- {
- DrawContent?.Invoke (viewport);
- }
- /// <summary>
- /// Causes the specified subview to have focus.
- /// </summary>
- /// <param name="view">View.</param>
- public void SetFocus (View view)
- {
- if (view == null)
- return;
- //Console.WriteLine ($"Request to focus {view}");
- if (!view.CanFocus)
- return;
- if (focused == view)
- return;
- // Make sure that this view is a subview
- View c;
- for (c = view.container; c != null; c = c.container)
- if (c == this)
- break;
- if (c == null)
- throw new ArgumentException ("the specified view is not part of the hierarchy of this view");
- if (focused != null)
- focused.HasFocus = false;
- focused = view;
- focused.HasFocus = true;
- focused.EnsureFocus ();
- // Send focus upwards
- SuperView?.SetFocus (this);
- }
- /// <summary>
- /// Defines the event arguments for <see cref="KeyEvent"/>
- /// </summary>
- public class KeyEventEventArgs : EventArgs {
- /// <summary>
- /// Constructs.
- /// </summary>
- /// <param name="ke"></param>
- public KeyEventEventArgs (KeyEvent ke) => KeyEvent = ke;
- /// <summary>
- /// The <see cref="KeyEvent"/> for the event.
- /// </summary>
- public KeyEvent KeyEvent { get; set; }
- /// <summary>
- /// Indicates if the current Key event has already been processed and the driver should stop notifying any other event subscriber.
- /// Its important to set this value to true specially when updating any View's layout from inside the subscriber method.
- /// </summary>
- public bool Handled { get; set; } = false;
- }
- /// <summary>
- /// Invoked when a character key is pressed and occurs after the key up event.
- /// </summary>
- public Action<KeyEventEventArgs> KeyPress;
- /// <inheritdoc/>
- public override bool ProcessKey (KeyEvent keyEvent)
- {
- KeyEventEventArgs args = new KeyEventEventArgs (keyEvent);
- KeyPress?.Invoke (args);
- if (args.Handled)
- return true;
- if (Focused?.ProcessKey (keyEvent) == true)
- return true;
- var c = keyEvent.KeyValue;
- if (c == '\n' || c == ' ' || Rune.ToUpper ((uint)c) == HotKey) {
- Clicked?.Invoke ();
- return true;
- }
- return false;
- }
- /// <inheritdoc/>
- public override bool ProcessHotKey (KeyEvent keyEvent)
- {
- KeyEventEventArgs args = new KeyEventEventArgs (keyEvent);
- KeyPress?.Invoke (args);
- if (args.Handled)
- return true;
- if (subviews == null || subviews.Count == 0)
- return false;
- foreach (var view in subviews)
- if (view.SuperView.IsCurrentTop && view.ProcessHotKey (keyEvent))
- return true;
- return false;
- }
- /// <inheritdoc/>
- public override bool ProcessColdKey (KeyEvent keyEvent)
- {
- KeyEventEventArgs args = new KeyEventEventArgs (keyEvent);
- KeyPress?.Invoke (args);
- if (args.Handled)
- return true;
- if (subviews == null || subviews.Count == 0)
- return false;
- foreach (var view in subviews)
- if (view.SuperView.IsCurrentTop && view.ProcessColdKey (keyEvent))
- return true;
- return false;
- }
- /// <summary>
- /// Invoked when a key is pressed
- /// </summary>
- public Action<KeyEventEventArgs> KeyDown;
- /// <param name="keyEvent">Contains the details about the key that produced the event.</param>
- public override bool OnKeyDown (KeyEvent keyEvent)
- {
- KeyEventEventArgs args = new KeyEventEventArgs (keyEvent);
- KeyDown?.Invoke (args);
- if (args.Handled)
- return true;
- if (subviews == null || subviews.Count == 0)
- return false;
- foreach (var view in subviews)
- if (view.SuperView.IsCurrentTop && view.OnKeyDown (keyEvent))
- return true;
- return false;
- }
- /// <summary>
- /// Invoked when a key is released
- /// </summary>
- public Action<KeyEventEventArgs> KeyUp;
- /// <param name="keyEvent">Contains the details about the key that produced the event.</param>
- public override bool OnKeyUp (KeyEvent keyEvent)
- {
- KeyEventEventArgs args = new KeyEventEventArgs (keyEvent);
- KeyUp?.Invoke (args);
- if (args.Handled)
- return true;
- if (subviews == null || subviews.Count == 0)
- return false;
- foreach (var view in subviews)
- if (view.SuperView.IsCurrentTop && view.OnKeyUp (keyEvent))
- return true;
- return false;
- }
- /// <summary>
- /// Finds the first view in the hierarchy that wants to get the focus if nothing is currently focused, otherwise, it does nothing.
- /// </summary>
- public void EnsureFocus ()
- {
- if (focused == null)
- if (FocusDirection == Direction.Forward)
- FocusFirst ();
- else
- FocusLast ();
- }
- /// <summary>
- /// Focuses the first focusable subview if one exists.
- /// </summary>
- public void FocusFirst ()
- {
- if (subviews == null) {
- SuperView?.SetFocus (this);
- return;
- }
- foreach (var view in subviews) {
- if (view.CanFocus) {
- SetFocus (view);
- return;
- }
- }
- }
- /// <summary>
- /// Focuses the last focusable subview if one exists.
- /// </summary>
- public void FocusLast ()
- {
- if (subviews == null) {
- SuperView?.SetFocus (this);
- return;
- }
- for (int i = subviews.Count; i > 0;) {
- i--;
- View v = subviews [i];
- if (v.CanFocus) {
- SetFocus (v);
- return;
- }
- }
- }
- /// <summary>
- /// Focuses the previous view.
- /// </summary>
- /// <returns><c>true</c>, if previous was focused, <c>false</c> otherwise.</returns>
- public bool FocusPrev ()
- {
- FocusDirection = Direction.Backward;
- if (subviews == null || subviews.Count == 0)
- return false;
- if (focused == null) {
- FocusLast ();
- return focused != null;
- }
- int focused_idx = -1;
- for (int i = subviews.Count; i > 0;) {
- i--;
- View w = subviews [i];
- if (w.HasFocus) {
- if (w.FocusPrev ())
- return true;
- focused_idx = i;
- continue;
- }
- if (w.CanFocus && focused_idx != -1) {
- focused.HasFocus = false;
- if (w != null && w.CanFocus)
- w.FocusLast ();
- SetFocus (w);
- return true;
- }
- }
- if (focused != null) {
- focused.HasFocus = false;
- focused = null;
- }
- return false;
- }
- /// <summary>
- /// Focuses the next view.
- /// </summary>
- /// <returns><c>true</c>, if next was focused, <c>false</c> otherwise.</returns>
- public bool FocusNext ()
- {
- FocusDirection = Direction.Forward;
- if (subviews == null || subviews.Count == 0)
- return false;
- if (focused == null) {
- FocusFirst ();
- return focused != null;
- }
- int n = subviews.Count;
- int focused_idx = -1;
- for (int i = 0; i < n; i++) {
- View w = subviews [i];
- if (w.HasFocus) {
- if (w.FocusNext ())
- return true;
- focused_idx = i;
- continue;
- }
- if (w.CanFocus && focused_idx != -1) {
- focused.HasFocus = false;
- if (w != null && w.CanFocus)
- w.FocusFirst ();
- SetFocus (w);
- return true;
- }
- }
- if (focused != null) {
- focused.HasFocus = false;
- focused = null;
- }
- return false;
- }
- /// <summary>
- /// Sets the View's <see cref="Frame"/> to the relative coordinates if its container, given the <see cref="Frame"/> for its container.
- /// </summary>
- /// <param name="hostFrame">The screen-relative frame for the host.</param>
- /// <remarks>
- /// Reminder: <see cref="Frame"/> is superview-relative; <see cref="Bounds"/> is view-relative.
- /// </remarks>
- internal void SetRelativeLayout (Rect hostFrame)
- {
- int w, h, _x, _y;
- if (x is Pos.PosCenter) {
- if (width == null)
- w = hostFrame.Width;
- else
- w = width.Anchor (hostFrame.Width);
- _x = x.Anchor (hostFrame.Width - w);
- } else {
- if (x == null)
- _x = 0;
- else
- _x = x.Anchor (hostFrame.Width);
- if (width == null)
- w = hostFrame.Width;
- else
- w = width.Anchor (hostFrame.Width - _x);
- }
- if (y is Pos.PosCenter) {
- if (height == null)
- h = hostFrame.Height;
- else
- h = height.Anchor (hostFrame.Height);
- _y = y.Anchor (hostFrame.Height - h);
- } else {
- if (y == null)
- _y = 0;
- else
- _y = y.Anchor (hostFrame.Height);
- if (height == null)
- h = hostFrame.Height;
- else
- h = height.Anchor (hostFrame.Height - _y);
- }
- Frame = new Rect (_x, _y, w, h);
- }
- // https://en.wikipedia.org/wiki/Topological_sorting
- List<View> TopologicalSort (HashSet<View> nodes, HashSet<(View From, View To)> edges)
- {
- var result = new List<View> ();
- // Set of all nodes with no incoming edges
- var S = new HashSet<View> (nodes.Where (n => edges.All (e => e.To.Equals (n) == false)));
- while (S.Any ()) {
- // remove a node n from S
- var n = S.First ();
- S.Remove (n);
- // add n to tail of L
- if (n != this?.SuperView)
- result.Add (n);
- // for each node m with an edge e from n to m do
- foreach (var e in edges.Where (e => e.From.Equals (n)).ToArray ()) {
- var m = e.To;
- // remove edge e from the graph
- edges.Remove (e);
- // if m has no other incoming edges then
- if (edges.All (me => me.To.Equals (m) == false) && m != this?.SuperView) {
- // insert m into S
- S.Add (m);
- }
- }
- }
- if (edges.Any ()) {
- if (!object.ReferenceEquals (edges.First ().From, edges.First ().To)) {
- throw new InvalidOperationException ($"TopologicalSort (for Pos/Dim) cannot find {edges.First ().From}. Did you forget to add it to {this}?");
- } else {
- throw new InvalidOperationException ("TopologicalSort encountered a recursive cycle in the relative Pos/Dim in the views of " + this);
- }
- } else {
- // return L (a topologically sorted order)
- return result;
- }
- }
- /// <summary>
- /// Event arguments for the <see cref="LayoutComplete"/> event.
- /// </summary>
- public class LayoutEventArgs : EventArgs {
- /// <summary>
- /// The view-relative bounds of the <see cref="View"/> before it was laid out.
- /// </summary>
- public Rect OldBounds { get; set; }
- }
- /// <summary>
- /// Fired after the Views's <see cref="LayoutSubviews"/> method has completed.
- /// </summary>
- /// <remarks>
- /// Subscribe to this event to perform tasks when the <see cref="View"/> has been resized or the layout has otherwise changed.
- /// </remarks>
- public Action<LayoutEventArgs> LayoutComplete;
- /// <summary>
- /// Raises the <see cref="LayoutComplete"/> event. Called from <see cref="LayoutSubviews"/> after all sub-views have been laid out.
- /// </summary>
- internal virtual void OnLayoutComplete (LayoutEventArgs args)
- {
- LayoutComplete?.Invoke (args);
- }
- /// <summary>
- /// Invoked when a view starts executing or when the dimensions of the view have changed, for example in
- /// response to the container view or terminal resizing.
- /// </summary>
- /// <remarks>
- /// Calls <see cref="OnLayoutComplete"/> (which raises the <see cref="LayoutComplete"/> event) before it returns.
- /// </remarks>
- public virtual void LayoutSubviews ()
- {
- if (!layoutNeeded)
- return;
- viewText.TextSize = Bounds.Size;
- viewText.ReFormat ();
- Rect oldBounds = Bounds;
- // Sort out the dependencies of the X, Y, Width, Height properties
- var nodes = new HashSet<View> ();
- var edges = new HashSet<(View, View)> ();
- foreach (var v in InternalSubviews) {
- nodes.Add (v);
- if (v.LayoutStyle == LayoutStyle.Computed) {
- if (v.X is Pos.PosView vX)
- edges.Add ((vX.Target, v));
- if (v.Y is Pos.PosView vY)
- edges.Add ((vY.Target, v));
- if (v.Width is Dim.DimView vWidth)
- edges.Add ((vWidth.Target, v));
- if (v.Height is Dim.DimView vHeight)
- edges.Add ((vHeight.Target, v));
- }
- }
- var ordered = TopologicalSort (nodes, edges);
- foreach (var v in ordered) {
- if (v.LayoutStyle == LayoutStyle.Computed)
- v.SetRelativeLayout (Frame);
- v.LayoutSubviews ();
- v.layoutNeeded = false;
- }
- if (SuperView == Application.Top && layoutNeeded && ordered.Count == 0 && LayoutStyle == LayoutStyle.Computed) {
- SetRelativeLayout (Frame);
- }
- layoutNeeded = false;
- OnLayoutComplete (new LayoutEventArgs () { OldBounds = oldBounds });
- }
- /// <summary>
- /// The text displayed by the <see cref="View"/>.
- /// </summary>
- /// <remarks>
- /// The text will only be displayed if the View has no subviews.
- /// </remarks>
- public virtual ustring Text {
- get => viewText.Text;
- set {
- viewText.Text = value;
- SetNeedsDisplay ();
- }
- }
- /// <summary>
- /// Controls the text-alignment property of the View. Changing this property will redisplay the <see cref="View"/>.
- /// </summary>
- /// <value>The text alignment.</value>
- public virtual TextAlignment TextAlignment {
- get => viewText.TextAlignment;
- set {
- viewText.TextAlignment = value;
- SetNeedsDisplay ();
- }
- }
- /// <summary>
- /// The color used for the <see cref="Label"/>.
- /// </summary>
- public virtual Attribute TextColor {
- get => viewText.TextColor;
- set {
- viewText.TextColor = value;
- SetNeedsDisplay ();
- }
- }
- /// <summary>
- /// Pretty prints the View
- /// </summary>
- /// <returns></returns>
- public override string ToString ()
- {
- return $"{GetType ().Name}({Id})({Frame})";
- }
- /// <summary>
- /// Specifies the event arguments for <see cref="MouseEvent"/>
- /// </summary>
- public class MouseEventArgs : EventArgs {
- /// <summary>
- /// Constructs.
- /// </summary>
- /// <param name="me"></param>
- public MouseEventArgs (MouseEvent me) => MouseEvent = me;
- /// <summary>
- /// The <see cref="MouseEvent"/> for the event.
- /// </summary>
- public MouseEvent MouseEvent { get; set; }
- /// <summary>
- /// Indicates if the current mouse event has already been processed and the driver should stop notifying any other event subscriber.
- /// Its important to set this value to true specially when updating any View's layout from inside the subscriber method.
- /// </summary>
- public bool Handled { get; set; }
- }
- /// <inheritdoc/>
- public override bool OnMouseEnter (MouseEvent mouseEvent)
- {
- MouseEventArgs args = new MouseEventArgs (mouseEvent);
- MouseEnter?.Invoke (args);
- if (args.Handled)
- return true;
- if (base.OnMouseEnter (mouseEvent))
- return true;
- return false;
- }
- /// <inheritdoc/>
- public override bool OnMouseLeave (MouseEvent mouseEvent)
- {
- MouseEventArgs args = new MouseEventArgs (mouseEvent);
- MouseLeave?.Invoke (args);
- if (args.Handled)
- return true;
- if (base.OnMouseLeave (mouseEvent))
- return true;
- return false;
- }
- /// <summary>
- /// Method invoked when a mouse event is generated
- /// </summary>
- /// <param name="mouseEvent"></param>
- /// <returns><c>true</c>, if the event was handled, <c>false</c> otherwise.</returns>
- public virtual bool OnMouseEvent (MouseEvent mouseEvent)
- {
- MouseEventArgs args = new MouseEventArgs (mouseEvent);
- MouseClick?.Invoke (args);
- if (args.Handled)
- return true;
- if (MouseEvent (mouseEvent))
- return true;
- if (mouseEvent.Flags == MouseFlags.Button1Clicked) {
- if (!HasFocus) {
- SuperView.SetFocus (this);
- SetNeedsDisplay ();
- }
- Clicked?.Invoke ();
- return true;
- }
- return false;
- }
- }
- }
|