123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Diagnostics;
- using System.Linq;
- using System.Reflection;
- using NStack;
- namespace Terminal.Gui {
- /// <summary>
- /// Determines the LayoutStyle for a <see cref="View"/>, if Absolute, during <see cref="View.LayoutSubviews"/>, the
- /// value from the <see cref="View.Frame"/> will be used, if the value is Computed, then <see cref="View.Frame"/>
- /// will be updated from the X, Y <see cref="Pos"/> objects and the Width and Height <see cref="Dim"/> objects.
- /// </summary>
- public enum LayoutStyle {
- /// <summary>
- /// The position and size of the view are based <see cref="View.Frame"/>.
- /// </summary>
- Absolute,
- /// <summary>
- /// The position and size of the view will be computed based on
- /// <see cref="X"/>, <see cref="Y"/>, <see cref="Width"/>, and <see cref="Height"/>. <see cref="View.Frame"/> will
- /// provide the absolute computed values.
- /// </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, called SubViews.
- /// </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: <see cref="LayoutStyle.Absolute"/> or <see cref="LayoutStyle.Computed"/>.
- /// The choice as to which layout style is used by the View
- /// is determined when the View is initialized. To create a View using Absolute layout, call a constructor that takes a
- /// Rect parameter to specify the absolute position and size (the View.<see cref="View.Frame "/>). To create a View
- /// using Computed layout use a constructor that does not take a Rect parameter 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 coordinates and sizes of Views explicitly, and the
- /// View will typically 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>
- /// <para>
- /// Views can also opt-in to more sophisticated initialization
- /// by implementing overrides to <see cref="ISupportInitialize.BeginInit"/> and
- /// <see cref="ISupportInitialize.EndInit"/> which will be called
- /// when the view is added to a <see cref="SuperView"/>.
- /// </para>
- /// <para>
- /// If first-run-only initialization is preferred, overrides to <see cref="ISupportInitializeNotification"/>
- /// can be implemented, in which case the <see cref="ISupportInitialize"/>
- /// methods will only be called if <see cref="ISupportInitializeNotification.IsInitialized"/>
- /// is <see langword="false"/>. This allows proper <see cref="View"/> inheritance hierarchies
- /// to override base class layout code optimally by doing so only on first run,
- /// instead of on every run.
- /// </para>
- /// </remarks>
- public partial class View : Responder, ISupportInitializeNotification {
- internal enum Direction {
- Forward,
- Backward
- }
- // container == SuperView
- View _superView = null;
- View focused = null;
- Direction focusDirection;
- bool autoSize;
- ShortcutHelper shortcutHelper;
- /// <summary>
- /// Event fired when this view is added to another.
- /// </summary>
- public event EventHandler<SuperViewChangedEventArgs> Added;
- /// <summary>
- /// Event fired when this view is removed from another.
- /// </summary>
- public event EventHandler<SuperViewChangedEventArgs> Removed;
- /// <summary>
- /// Event fired when the view gets focus.
- /// </summary>
- public event EventHandler<FocusEventArgs> Enter;
- /// <summary>
- /// Event fired when the view looses focus.
- /// </summary>
- public event EventHandler<FocusEventArgs> Leave;
- /// <summary>
- /// Event fired when the view receives the mouse event for the first time.
- /// </summary>
- public event EventHandler<MouseEventEventArgs> MouseEnter;
- /// <summary>
- /// Event fired when the view receives a mouse event for the last time.
- /// </summary>
- public event EventHandler<MouseEventEventArgs> MouseLeave;
- /// <summary>
- /// Event fired when a mouse event is generated.
- /// </summary>
- public event EventHandler<MouseEventEventArgs> MouseClick;
- /// <summary>
- /// Event fired when the <see cref="CanFocus"/> value is being changed.
- /// </summary>
- public event EventHandler CanFocusChanged;
- /// <summary>
- /// Event fired when the <see cref="Enabled"/> value is being changed.
- /// </summary>
- public event EventHandler EnabledChanged;
- /// <summary>
- /// Event fired when the <see cref="Visible"/> value is being changed.
- /// </summary>
- public event EventHandler VisibleChanged;
- /// <summary>
- /// Event invoked when the <see cref="HotKey"/> is changed.
- /// </summary>
- public event EventHandler<KeyChangedEventArgs> HotKeyChanged;
- Key hotKey = Key.Null;
- /// <summary>
- /// Gets or sets 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 virtual Key HotKey {
- get => hotKey;
- set {
- if (hotKey != value) {
- hotKey = TextFormatter.HotKey = (value == Key.Unknown ? Key.Null : value);
- }
- }
- }
- /// <summary>
- /// Gets or sets 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 virtual Rune HotKeySpecifier {
- get {
- if (TextFormatter != null) {
- return TextFormatter.HotKeySpecifier;
- } else {
- return new Rune ('\xFFFF');
- }
- }
- set {
- TextFormatter.HotKeySpecifier = value;
- SetHotKey ();
- }
- }
- /// <summary>
- /// This is the global setting that can be used as a global shortcut to invoke an action if provided.
- /// </summary>
- public Key Shortcut {
- get => shortcutHelper.Shortcut;
- set {
- if (shortcutHelper.Shortcut != value && (ShortcutHelper.PostShortcutValidation (value) || value == Key.Null)) {
- shortcutHelper.Shortcut = value;
- }
- }
- }
- /// <summary>
- /// The keystroke combination used in the <see cref="Shortcut"/> as string.
- /// </summary>
- public ustring ShortcutTag => ShortcutHelper.GetShortcutTag (shortcutHelper.Shortcut);
- /// <summary>
- /// The action to run if the <see cref="Shortcut"/> is defined.
- /// </summary>
- public virtual Action ShortcutAction { get; set; }
- /// <summary>
- /// Gets or sets arbitrary data for the view.
- /// </summary>
- /// <remarks>This property is not used internally.</remarks>
- public object Data { get; set; }
- 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 => Application.Driver;
- static readonly 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?.AsReadOnly () ?? empty;
- // Internally, we use InternalSubviews rather than subviews, as we do not expect us
- // to make the same mistakes our users make when they poke at the Subviews.
- internal IList<View> InternalSubviews => subviews ?? empty;
- // This is null, and allocated on demand.
- List<View> tabIndexes;
- /// <summary>
- /// Configurable keybindings supported by the control
- /// </summary>
- private Dictionary<Key, Command []> KeyBindings { get; set; } = new Dictionary<Key, Command []> ();
- private Dictionary<Command, Func<bool?>> CommandImplementations { get; set; } = new Dictionary<Command, Func<bool?>> ();
- /// <summary>
- /// This returns a tab index list of the subviews contained by this view.
- /// </summary>
- /// <value>The tabIndexes.</value>
- public IList<View> TabIndexes => tabIndexes?.AsReadOnly () ?? empty;
- int tabIndex = -1;
- /// <summary>
- /// Indicates the index of the current <see cref="View"/> from the <see cref="TabIndexes"/> list.
- /// </summary>
- public int TabIndex {
- get { return tabIndex; }
- set {
- if (!CanFocus) {
- tabIndex = -1;
- return;
- } else if (SuperView?.tabIndexes == null || SuperView?.tabIndexes.Count == 1) {
- tabIndex = 0;
- return;
- } else if (tabIndex == value) {
- return;
- }
- tabIndex = value > SuperView.tabIndexes.Count - 1 ? SuperView.tabIndexes.Count - 1 : value < 0 ? 0 : value;
- tabIndex = GetTabIndex (tabIndex);
- if (SuperView.tabIndexes.IndexOf (this) != tabIndex) {
- SuperView.tabIndexes.Remove (this);
- SuperView.tabIndexes.Insert (tabIndex, this);
- SetTabIndex ();
- }
- }
- }
- int GetTabIndex (int idx)
- {
- var i = 0;
- foreach (var v in SuperView.tabIndexes) {
- if (v.tabIndex == -1 || v == this) {
- continue;
- }
- i++;
- }
- return Math.Min (i, idx);
- }
- void SetTabIndex ()
- {
- var i = 0;
- foreach (var v in SuperView.tabIndexes) {
- if (v.tabIndex == -1) {
- continue;
- }
- v.tabIndex = i;
- i++;
- }
- }
- bool tabStop = true;
- /// <summary>
- /// This only be <see langword="true"/> if the <see cref="CanFocus"/> is also <see langword="true"/>
- /// and the focus can be avoided by setting this to <see langword="false"/>
- /// </summary>
- public bool TabStop {
- get => tabStop;
- set {
- if (tabStop == value) {
- return;
- }
- tabStop = CanFocus && value;
- }
- }
- bool oldCanFocus;
- int oldTabIndex;
- /// <inheritdoc/>
- public override bool CanFocus {
- get => base.CanFocus;
- set {
- if (!addingView && IsInitialized && SuperView?.CanFocus == false && value) {
- throw new InvalidOperationException ("Cannot set CanFocus to true if the SuperView CanFocus is false!");
- }
- if (base.CanFocus != value) {
- base.CanFocus = value;
- switch (value) {
- case false when tabIndex > -1:
- TabIndex = -1;
- break;
- case true when SuperView?.CanFocus == false && addingView:
- SuperView.CanFocus = true;
- break;
- }
- if (value && tabIndex == -1) {
- TabIndex = SuperView != null ? SuperView.tabIndexes.IndexOf (this) : -1;
- }
- TabStop = value;
- if (!value && SuperView?.Focused == this) {
- SuperView.focused = null;
- }
- if (!value && HasFocus) {
- SetHasFocus (false, this);
- SuperView?.EnsureFocus ();
- if (SuperView != null && SuperView.Focused == null) {
- SuperView.FocusNext ();
- if (SuperView.Focused == null) {
- Application.Current.FocusNext ();
- }
- Application.EnsuresTopOnFront ();
- }
- }
- if (subviews != null && IsInitialized) {
- foreach (var view in subviews) {
- if (view.CanFocus != value) {
- if (!value) {
- view.oldCanFocus = view.CanFocus;
- view.oldTabIndex = view.tabIndex;
- view.CanFocus = false;
- view.tabIndex = -1;
- } else {
- if (addingView) {
- view.addingView = true;
- }
- view.CanFocus = view.oldCanFocus;
- view.tabIndex = view.oldTabIndex;
- view.addingView = false;
- }
- }
- }
- }
- OnCanFocusChanged ();
- SetNeedsDisplay ();
- }
- }
- }
- // 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 string Id { get; set; } = "";
- /// <summary>
- /// Returns a value indicating if this View is currently on Top (Active)
- /// </summary>
- public bool IsCurrentTop => Application.Current == this;
- /// <summary>
- /// Gets or sets a value indicating whether this <see cref="View"/> wants mouse position reports.
- /// </summary>
- /// <value><see langword="true"/> if want mouse position reports; otherwise, <see langword="false"/>.</value>
- public virtual bool WantMousePositionReports { get; set; }
- /// <summary>
- /// Gets or sets a value indicating whether this <see cref="View"/> want continuous button pressed event.
- /// </summary>
- public virtual bool WantContinuousButtonPressed { get; set; }
- /// <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="Terminal.Gui.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 {
- frame = new Rect (value.X, value.Y, Math.Max (value.Width, 0), Math.Max (value.Height, 0));
- TextFormatter.Size = GetSizeNeededForTextAndHotKey ();
- SetNeedsLayout ();
- SetNeedsDisplay ();
- }
- }
- /// <summary>
- /// The Thickness that separates a View from other SubViews of the same SuperView.
- /// The Margin is not part of the View's content and is not clipped by the View's Clip Area.
- /// </summary>
- public Frame Margin { get; private set; }
- // TODO: Rename BorderFrame to Border
- /// <summary>
- /// Thickness where a visual border (drawn using line-drawing glyphs) and the Title are drawn.
- /// The Border expands inward; in other words if `Border.Thickness.Top == 2` the border and
- /// title will take up the first row and the second row will be filled with spaces.
- /// The Border is not part of the View's content and is not clipped by the View's `ClipArea`.
- /// </summary>
- public Frame BorderFrame { get; private set; }
- /// <summary>
- /// Means the Thickness inside of an element that offsets the `Content` from the Border.
- /// Padding is `{0, 0, 0, 0}` by default. Padding is not part of the View's content and is not clipped by the View's `ClipArea`.
- /// </summary>
- /// <remarks>
- /// (NOTE: in v1 `Padding` is OUTSIDE of the `Border`).
- /// </remarks>
- public Frame Padding { get; private set; }
- /// <summary>
- /// Helper to get the X and Y offset of the Bounds from the Frame. This is the sum of the Left and Top properties of
- /// <see cref="Margin"/>, <see cref="BorderFrame"/> and <see cref="Padding"/>.
- /// </summary>
- public Point GetBoundsOffset () => new Point (Padding?.Thickness.GetInside (Padding.Frame).X ?? 0, Padding?.Thickness.GetInside (Padding.Frame).Y ?? 0);
- /// <summary>
- /// Creates the view's <see cref="Frame"/> objects. This internal method is overridden by Frame to do nothing
- /// to prevent recursion during View construction.
- /// </summary>
- internal virtual void CreateFrames ()
- {
- void ThicknessChangedHandler (object sender, EventArgs e)
- {
- SetNeedsLayout ();
- }
- if (Margin != null) {
- Margin.ThicknessChanged -= ThicknessChangedHandler;
- Margin.Dispose ();
- }
- Margin = new Frame () { Id = "Margin", Thickness = new Thickness (0) };
- Margin.ThicknessChanged += ThicknessChangedHandler;
- Margin.Parent = this;
- if (BorderFrame != null) {
- BorderFrame.ThicknessChanged -= ThicknessChangedHandler;
- BorderFrame.Dispose ();
- }
- // TODO: create default for borderstyle
- BorderFrame = new Frame () { Id = "BorderFrame", Thickness = new Thickness (0), BorderStyle = BorderStyle.Single };
- BorderFrame.ThicknessChanged += ThicknessChangedHandler;
- BorderFrame.Parent = this;
- // TODO: Create View.AddAdornment
- if (Padding != null) {
- Padding.ThicknessChanged -= ThicknessChangedHandler;
- Padding.Dispose ();
- }
- Padding = new Frame () { Id = "Padding", Thickness = new Thickness (0) };
- Padding.ThicknessChanged += ThicknessChangedHandler;
- Padding.Parent = this;
- }
- ustring title;
- /// <summary>
- /// The title to be displayed for this <see cref="View"/>. The title will be displayed if <see cref="BorderFrame"/>.<see cref="Thickness.Top"/>
- /// is greater than 0.
- /// </summary>
- /// <value>The title.</value>
- public ustring Title {
- get => title;
- set {
- title = value;
- SetNeedsDisplay ();
- #if DEBUG
- if (title != null && string.IsNullOrEmpty (Id)) {
- Id = title.ToString ();
- }
- #endif
- }
- }
- 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="Terminal.Gui.LayoutStyle.Absolute"/>,
- /// LayoutSubviews does not change the <see cref="Frame"/>. If the style is <see cref="Terminal.Gui.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 View-relative rectangle where View content is displayed. SubViews are positioned relative to
- /// Bounds.<see cref="Rect.Location">Location</see> (which is always (0, 0)) and <see cref="Redraw(Rect)"/> clips drawing to
- /// Bounds.<see cref="Rect.Size">Size</see>.
- /// </summary>
- /// <value>The bounds.</value>
- /// <remarks>
- /// <para>
- /// The <see cref="Rect.Location"/> of Bounds is always (0, 0). To obtain the offset of the Bounds from the Frame use
- /// <see cref="GetBoundsOffset"/>.
- /// </para>
- /// <para>
- /// To obtain the SuperView-relative location of the content area use <see cref="ContentArea"/>.
- /// </para>
- /// </remarks>
- public virtual Rect Bounds {
- get {
- #if DEBUG
- if (LayoutStyle == LayoutStyle.Computed && !IsInitialized) {
- Debug.WriteLine ($"WARNING: Bounds is being accessed before the View has been initialized. This is likely a bug. View: {this}");
- }
- #endif // DEBUG
- var frameRelativeBounds = Padding?.Thickness.GetInside (Padding.Frame) ?? new Rect (default, Frame.Size);
- return new Rect (default, frameRelativeBounds.Size);
- }
- set {
- // BUGBUG: Margin etc.. can be null (if typeof(Frame))
- Frame = new Rect (Frame.Location, value.Size
- + new Size (Margin.Thickness.Right, Margin.Thickness.Bottom)
- + new Size (BorderFrame.Thickness.Right, BorderFrame.Thickness.Bottom)
- + new Size (Padding.Thickness.Right, Padding.Thickness.Bottom));
- }
- }
- // Diagnostics to highlight when X or Y is read before the view has been initialized
- private Pos VerifyIsIntialized (Pos pos)
- {
- #if DEBUG
- if (LayoutStyle == LayoutStyle.Computed && (!IsInitialized)) {
- Debug.WriteLine ($"WARNING: \"{this}\" has not been initialized; position is indeterminate {pos}. This is likely a bug.");
- }
- #endif // DEBUG
- return pos;
- }
- // Diagnostics to highlight when Width or Height is read before the view has been initialized
- private Dim VerifyIsIntialized (Dim dim)
- {
- #if DEBUG
- if (LayoutStyle == LayoutStyle.Computed && (!IsInitialized)) {
- Debug.WriteLine ($"WARNING: \"{this}\" has not been initialized; dimension is indeterminate: {dim}. This is likely a bug.");
- }
- #endif // DEBUG
- return dim;
- }
- Pos x, y;
- /// <summary>
- /// Gets or sets the X position for the view (the column). Only used if the <see cref="LayoutStyle"/> is <see cref="Terminal.Gui.LayoutStyle.Computed"/>.
- /// </summary>
- /// <value>The X Position.</value>
- /// <remarks>
- /// If <see cref="LayoutStyle"/> is <see cref="Terminal.Gui.LayoutStyle.Absolute"/> changing this property has no effect and its value is indeterminate.
- /// </remarks>
- public Pos X {
- get => VerifyIsIntialized (x);
- set {
- if (ForceValidatePosDim && !ValidatePosDim (x, value)) {
- throw new ArgumentException ();
- }
- x = value;
- OnResizeNeeded ();
- }
- }
- /// <summary>
- /// Gets or sets the Y position for the view (the row). Only used if the <see cref="LayoutStyle"/> is <see cref="Terminal.Gui.LayoutStyle.Computed"/>.
- /// </summary>
- /// <value>The y position (line).</value>
- /// <remarks>
- /// If <see cref="LayoutStyle"/> is <see cref="Terminal.Gui.LayoutStyle.Absolute"/> changing this property has no effect and its value is indeterminate.
- /// </remarks>
- public Pos Y {
- get => VerifyIsIntialized (y);
- set {
- if (ForceValidatePosDim && !ValidatePosDim (y, value)) {
- throw new ArgumentException ();
- }
- y = value;
- OnResizeNeeded ();
- }
- }
- Dim width, height;
- /// <summary>
- /// Gets or sets the width of the view. Only used the <see cref="LayoutStyle"/> is <see cref="Terminal.Gui.LayoutStyle.Computed"/>.
- /// </summary>
- /// <value>The width.</value>
- /// <remarks>
- /// If <see cref="LayoutStyle"/> is <see cref="Terminal.Gui.LayoutStyle.Absolute"/> changing this property has no effect and its value is indeterminate.
- /// </remarks>
- public Dim Width {
- get => VerifyIsIntialized (width);
- set {
- if (ForceValidatePosDim && !ValidatePosDim (width, value)) {
- throw new ArgumentException ("ForceValidatePosDim is enabled", nameof (Width));
- }
- width = value;
- if (ForceValidatePosDim) {
- var isValidNewAutSize = autoSize && IsValidAutoSizeWidth (width);
- if (IsAdded && autoSize && !isValidNewAutSize) {
- throw new InvalidOperationException ("Must set AutoSize to false before set the Width.");
- }
- }
- OnResizeNeeded ();
- }
- }
- /// <summary>
- /// Gets or sets the height of the view. Only used the <see cref="LayoutStyle"/> is <see cref="Terminal.Gui.LayoutStyle.Computed"/>.
- /// </summary>
- /// <value>The height.</value>
- /// If <see cref="LayoutStyle"/> is <see cref="Terminal.Gui.LayoutStyle.Absolute"/> changing this property has no effect and its value is indeterminate.
- public Dim Height {
- get => VerifyIsIntialized (height);
- set {
- if (ForceValidatePosDim && !ValidatePosDim (height, value)) {
- throw new ArgumentException ("ForceValidatePosDim is enabled", nameof (Height));
- }
- height = value;
- if (ForceValidatePosDim) {
- var isValidNewAutSize = autoSize && IsValidAutoSizeHeight (height);
- if (IsAdded && autoSize && !isValidNewAutSize) {
- throw new InvalidOperationException ("Must set AutoSize to false before set the Height.");
- }
- }
- OnResizeNeeded ();
- }
- }
- /// <summary>
- /// Forces validation with <see cref="Terminal.Gui.LayoutStyle.Computed"/> layout
- /// to avoid breaking the <see cref="Pos"/> and <see cref="Dim"/> settings.
- /// </summary>
- public bool ForceValidatePosDim { get; set; }
- bool ValidatePosDim (object oldValue, object newValue)
- {
- if (!IsInitialized || _layoutStyle == LayoutStyle.Absolute || oldValue == null || oldValue.GetType () == newValue.GetType () || this is Toplevel) {
- return true;
- }
- if (_layoutStyle == LayoutStyle.Computed) {
- if (oldValue.GetType () != newValue.GetType () && !(newValue is Pos.PosAbsolute || newValue is Dim.DimAbsolute)) {
- return true;
- }
- }
- return false;
- }
- // BUGBUG: This API is broken - It should be renamed to `GetMinimumBoundsForFrame and
- // should not assume Frame.Height == Bounds.Height
- /// <summary>
- /// Gets the minimum dimensions required to fit the View's <see cref="Text"/>, factoring in <see cref="TextDirection"/>.
- /// </summary>
- /// <param name="size">The minimum dimensions required.</param>
- /// <returns><see langword="true"/> if the dimensions fit within the View's <see cref="Bounds"/>, <see langword="false"/> otherwise.</returns>
- /// <remarks>
- /// Always returns <see langword="false"/> if <see cref="AutoSize"/> is <see langword="true"/> or
- /// if <see cref="Height"/> (Horizontal) or <see cref="Width"/> (Vertical) are not not set or zero.
- /// Does not take into account word wrapping.
- /// </remarks>
- public bool GetMinimumBounds (out Size size)
- {
- size = Bounds.Size;
- if (!AutoSize && !ustring.IsNullOrEmpty (TextFormatter.Text)) {
- switch (TextFormatter.IsVerticalDirection (TextDirection)) {
- case true:
- var colWidth = TextFormatter.GetSumMaxCharWidth (new List<ustring> { TextFormatter.Text }, 0, 1);
- // TODO: v2 - This uses frame.Width; it should only use Bounds
- if (frame.Width < colWidth &&
- (Width == null ||
- (Bounds.Width >= 0 &&
- Width is Dim.DimAbsolute &&
- Width.Anchor (0) >= 0 &&
- Width.Anchor (0) < colWidth))) {
- size = new Size (colWidth, Bounds.Height);
- return true;
- }
- break;
- default:
- if (frame.Height < 1 &&
- (Height == null ||
- (Height is Dim.DimAbsolute &&
- Height.Anchor (0) == 0))) {
- size = new Size (Bounds.Width, 1);
- return true;
- }
- break;
- }
- }
- return false;
- }
- // BUGBUG - v2 - Should be renamed "SetBoundsToFitFrame"
- /// <summary>
- /// Sets the size of the View to the minimum width or height required to fit <see cref="Text"/> (see <see cref="GetMinimumBounds(out Size)"/>.
- /// </summary>
- /// <returns><see langword="true"/> if the size was changed, <see langword="false"/> if <see cref="Text"/>
- /// will not fit.</returns>
- public bool SetMinWidthHeight ()
- {
- if (IsInitialized && GetMinimumBounds (out Size size)) {
- Bounds = new Rect (Bounds.Location, size);
- return true;
- }
- return false;
- }
- /// <summary>
- /// Gets or sets the <see cref="Terminal.Gui.TextFormatter"/> which can be handled differently by any derived class.
- /// </summary>
- public TextFormatter? TextFormatter { get; set; }
- /// <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 virtual View SuperView {
- get {
- return _superView;
- }
- set {
- throw new NotImplementedException ();
- }
- }
- /// <summary>
- /// Initializes a new instance of a <see cref="Terminal.Gui.LayoutStyle.Absolute"/> <see cref="View"/> class with the absolute
- /// dimensions specified in the <see langword="frame"/> parameter.
- /// </summary>
- /// <param name="frame">The region covered by this view.</param>
- /// <remarks>
- /// This constructor initialize a View with a <see cref="LayoutStyle"/> of <see cref="Terminal.Gui.LayoutStyle.Absolute"/>.
- /// Use <see cref="View"/> to initialize a View with <see cref="LayoutStyle"/> of <see cref="Terminal.Gui.LayoutStyle.Computed"/>
- /// </remarks>
- public View (Rect frame) : this (frame, null, null) { }
- /// <summary>
- /// Initializes a new instance of <see cref="View"/> using <see cref="Terminal.Gui.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="View"/> will be created using <see cref="Terminal.Gui.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 <see cref="Height"/> is greater than one, word wrapping is provided.
- /// </para>
- /// <para>
- /// This constructor initialize a View with a <see cref="LayoutStyle"/> of <see cref="Terminal.Gui.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, direction: TextDirection.LeftRight_TopBottom) { }
- /// <summary>
- /// Initializes a new instance of <see cref="View"/> using <see cref="Terminal.Gui.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 View.</param>
- /// <param name="y">row to locate the View.</param>
- /// <param name="text">text to initialize the <see cref="Text"/> property with.</param>
- public View (int x, int y, ustring text) : this (TextFormatter.CalcRect (x, y, text), text) { }
- /// <summary>
- /// Initializes a new instance of <see cref="View"/> using <see cref="Terminal.Gui.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>
- /// <param name="border">The <see cref="Border"/>.</param>
- public View (Rect rect, ustring text, Border border = null)
- {
- SetInitialProperties (text, rect, LayoutStyle.Absolute, TextDirection.LeftRight_TopBottom, border);
- }
- /// <summary>
- /// Initializes a new instance of <see cref="View"/> using <see cref="Terminal.Gui.LayoutStyle.Computed"/> layout.
- /// </summary>
- /// <remarks>
- /// <para>
- /// The <see cref="View"/> will be created using <see cref="Terminal.Gui.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 <see cref="Height"/> is greater than one, word wrapping is provided.
- /// </para>
- /// </remarks>
- /// <param name="text">text to initialize the <see cref="Text"/> property with.</param>
- /// <param name="direction">The text direction.</param>
- /// <param name="border">The <see cref="Border"/>.</param>
- public View (ustring text, TextDirection direction = TextDirection.LeftRight_TopBottom, Border border = null)
- {
- SetInitialProperties (text, Rect.Empty, LayoutStyle.Computed, direction, border);
- }
- // TODO: v2 - Remove constructors with parameters
- /// <summary>
- /// Private helper to set the initial properties of the View that were provided via constructors.
- /// </summary>
- /// <param name="text"></param>
- /// <param name="rect"></param>
- /// <param name="layoutStyle"></param>
- /// <param name="direction"></param>
- /// <param name="border"></param>
- void SetInitialProperties (ustring text, Rect rect, LayoutStyle layoutStyle = LayoutStyle.Computed,
- TextDirection direction = TextDirection.LeftRight_TopBottom, Border border = null)
- {
- TextFormatter = new TextFormatter ();
- TextFormatter.HotKeyChanged += TextFormatter_HotKeyChanged;
- TextDirection = direction;
- shortcutHelper = new ShortcutHelper ();
- CanFocus = false;
- TabIndex = -1;
- TabStop = false;
- LayoutStyle = layoutStyle;
- Border = border;
- // TODO: v2 - Hack for now
- if (Border != null) Border.BorderChanged += Border_BorderChanged;
- Text = text;
- LayoutStyle = layoutStyle;
- var r = rect.IsEmpty ? TextFormatter.CalcRect (0, 0, text, direction) : rect;
- Frame = r;
- OnResizeNeeded ();
- CreateFrames ();
- }
- private void Border_BorderChanged (Border border)
- {
- BorderFrame.Thickness = border.BorderThickness;
- BorderFrame.BorderStyle = border.BorderStyle;
- if (!border.DrawMarginFrame) BorderFrame.BorderStyle = BorderStyle.None;
- }
- /// <summary>
- /// Can be overridden if the <see cref="Terminal.Gui.TextFormatter.Text"/> has
- /// different format than the default.
- /// </summary>
- protected virtual void UpdateTextFormatterText ()
- {
- if (TextFormatter != null) {
- TextFormatter.Text = text;
- }
- }
- /// <summary>
- /// Called whenever the view needs to be resized.
- /// Can be overridden if the view resize behavior is
- /// different than the default.
- /// </summary>
- protected virtual void OnResizeNeeded ()
- {
- var actX = x is Pos.PosAbsolute ? x.Anchor (0) : frame.X;
- var actY = y is Pos.PosAbsolute ? y.Anchor (0) : frame.Y;
- if (AutoSize) {
- var s = GetAutoSize ();
- var w = width is Dim.DimAbsolute && width.Anchor (0) > s.Width ? width.Anchor (0) : s.Width;
- var h = height is Dim.DimAbsolute && height.Anchor (0) > s.Height ? height.Anchor (0) : s.Height;
- frame = new Rect (new Point (actX, actY), new Size (w, h));
- } else {
- var w = width is Dim.DimAbsolute ? width.Anchor (0) : frame.Width;
- var h = height is Dim.DimAbsolute ? height.Anchor (0) : frame.Height;
- frame = new Rect (new Point (actX, actY), new Size (w, h));
- SetMinWidthHeight ();
- }
- // BUGBUG: I think these calls are redundant or should be moved into just the AutoSize case
- TextFormatter.Size = GetSizeNeededForTextAndHotKey ();
- SetNeedsLayout ();
- SetNeedsDisplay ();
- }
- void TextFormatter_HotKeyChanged (object sender, KeyChangedEventArgs e)
- {
- HotKeyChanged?.Invoke (this, e);
- }
- internal bool LayoutNeeded { get; private set; } = true;
- internal void SetNeedsLayout ()
- {
- if (LayoutNeeded)
- return;
- LayoutNeeded = true;
- if (SuperView == null)
- return;
- SuperView.SetNeedsLayout ();
- foreach (var view in Subviews) {
- view.SetNeedsLayout ();
- }
- TextFormatter.NeedsFormat = true;
- }
- /// <summary>
- /// Removes the <see cref="SetNeedsLayout"/> setting on this view.
- /// </summary>
- protected void ClearLayoutNeeded ()
- {
- LayoutNeeded = false;
- }
- // The view-relative region that needs to be redrawn
- internal Rect _needsDisplay { get; private set; } = Rect.Empty;
- /// <summary>
- /// Sets a flag indicating this view needs to be redisplayed because its state has changed.
- /// </summary>
- public void SetNeedsDisplay ()
- {
- SetNeedsDisplay (Bounds);
- }
- /// <summary>
- /// Flags the view-relative region on this View as needing to be redrawn.
- /// </summary>
- /// <param name="region">The view-relative region that needs to be redrawn.</param>
- public void SetNeedsDisplay (Rect region)
- {
- if (_needsDisplay.IsEmpty)
- _needsDisplay = region;
- else {
- var x = Math.Min (_needsDisplay.X, region.X);
- var y = Math.Min (_needsDisplay.Y, region.Y);
- var w = Math.Max (_needsDisplay.Width, region.Width);
- var h = Math.Max (_needsDisplay.Height, region.Height);
- _needsDisplay = new Rect (x, y, w, h);
- }
- _superView?.SetSubViewNeedsDisplay ();
- 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);
- }
- }
- private Rect GetNeedsDisplayRectScreen (Rect containerBounds)
- {
- Rect rect = ViewToScreen (_needsDisplay);
- if (!containerBounds.IsEmpty) {
- rect.Width = Math.Min (_needsDisplay.Width, containerBounds.Width);
- rect.Height = Math.Min (_needsDisplay.Height, containerBounds.Height);
- }
- return rect;
- }
- internal bool _childNeedsDisplay { get; private set; }
- /// <summary>
- /// Indicates that any Subviews (in the <see cref="Subviews"/> list) need to be repainted.
- /// </summary>
- public void SetSubViewNeedsDisplay ()
- {
- _childNeedsDisplay = true;
- if (_superView != null)
- _superView.SetSubViewNeedsDisplay ();
- }
- internal bool addingView;
- /// <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> ();
- }
- if (tabIndexes == null) {
- tabIndexes = new List<View> ();
- }
- subviews.Add (view);
- tabIndexes.Add (view);
- view._superView = this;
- if (view.CanFocus) {
- addingView = true;
- if (SuperView?.CanFocus == false) {
- SuperView.addingView = true;
- SuperView.CanFocus = true;
- SuperView.addingView = false;
- }
- CanFocus = true;
- view.tabIndex = tabIndexes.IndexOf (view);
- addingView = false;
- }
- if (view.Enabled && !Enabled) {
- view.oldEnabled = true;
- view.Enabled = false;
- }
- SetNeedsLayout ();
- SetNeedsDisplay ();
- OnAdded (new SuperViewChangedEventArgs (this, view));
- if (IsInitialized && !view.IsInitialized) {
- view.BeginInit ();
- view.EndInit ();
- }
- }
- /// <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;
- var touched = view.Frame;
- subviews.Remove (view);
- tabIndexes.Remove (view);
- view._superView = null;
- view.tabIndex = -1;
- SetNeedsLayout ();
- SetNeedsDisplay ();
- if (subviews.Count < 1) CanFocus = false;
- foreach (var v in subviews) {
- if (v.Frame.IntersectsWith (touched))
- view.SetNeedsDisplay ();
- }
- OnRemoved (new SuperViewChangedEventArgs (this, view));
- if (focused == view) {
- focused = null;
- }
- }
- 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 (var line = 0; line < h; line++) {
- Move (0, line);
- for (var col = 0; col < w; col++)
- Driver.AddRune (' ');
- }
- }
- // BUGBUG: Stupid that this takes screen-relative. We should have a tenet that says
- // "View APIs only deal with View-relative coords".
- /// <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 (var line = regionScreen.Y; line < regionScreen.Y + h; line++) {
- Driver.Move (regionScreen.X, line);
- for (var col = 0; col < w; col++)
- Driver.AddRune (' ');
- }
- }
- /// <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 point from screen-relative coordinates to bounds-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 ScreenToBounds (int x, int y)
- {
- if (SuperView == null) {
- return new Point (x - Frame.X + GetBoundsOffset ().X, y - Frame.Y + GetBoundsOffset ().Y);
- } else {
- var parent = SuperView.ScreenToView (x, y);
- return new Point (parent.X - frame.X, parent.Y - frame.Y);
- }
- }
- /// <summary>
- /// Converts a view-relative location to a screen-relative location (col,row). The output is 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="clamped">If <see langword="true"/>, <paramref name="rcol"/> and <paramref name="rrow"/> will be clamped to the
- /// screen dimensions (they never be negative and will always be less than to <see cref="ConsoleDriver.Cols"/> and
- /// <see cref="ConsoleDriver.Rows"/>, respectively.</param>
- public virtual void ViewToScreen (int col, int row, out int rcol, out int rrow, bool clamped = true)
- {
- rcol = col + Frame.X + GetBoundsOffset ().X;
- rrow = row + Frame.Y + GetBoundsOffset ().Y;
- var super = SuperView;
- while (super != null) {
- rcol += super.Frame.X + super.GetBoundsOffset ().X;
- rrow += super.Frame.Y + super.GetBoundsOffset ().Y;
- super = super.SuperView;
- }
- // The following ensures that the cursor is always in the screen boundaries.
- if (clamped) {
- rrow = Math.Min (rrow, Driver.Rows - 1);
- rcol = Math.Min (rcol, Driver.Cols - 1);
- }
- }
- /// <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, clamped: 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 <see cref="Bounds"/>.
- /// </summary>
- /// <returns>The current screen-relative clip region, which can be then re-applied by setting <see cref="ConsoleDriver.Clip"/>.</returns>
- /// <remarks>
- /// <para>
- /// <see cref="Bounds"/> is View-relative.
- /// </para>
- /// <para>
- /// If <see cref="ConsoleDriver.Clip"/> and <see cref="Bounds"/> do not intersect, the clip region will be set to <see cref="Rect.Empty"/>.
- /// </para>
- /// </remarks>
- public Rect ClipToBounds ()
- {
- var clip = Bounds;
- return SetClip (clip);
- }
- // BUGBUG: v2 - SetClip should return VIEW-relative so that it can be used to reset it; using Driver.Clip directly should not be necessary.
- /// <summary>
- /// Sets the clip region to the specified view-relative region.
- /// </summary>
- /// <returns>The current screen-relative clip region, which can be then re-applied by setting <see cref="ConsoleDriver.Clip"/>.</returns>
- /// <param name="region">View-relative clip region.</param>
- /// <remarks>
- /// If <see cref="ConsoleDriver.Clip"/> and <paramref name="region"/> do not intersect, the clip region will be set to <see cref="Rect.Empty"/>.
- /// </remarks>
- 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 <see langword="true"/> it fill will the contents.</param>
- [ObsoleteAttribute ("This method is obsolete in v2. Use use LineCanvas or Frame instead instead.", false)]
- 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 hotkey specifier before a letter flags the next letter as the hotkey.</param>
- /// <param name="hotColor">Hot color.</param>
- /// <param name="normalColor">Normal color.</param>
- /// <remarks>
- /// <para>The hotkey is any character following the hotkey specifier, which is the underscore ('_') character by default.</para>
- /// <para>The hotkey specifier can be changed via <see cref="HotKeySpecifier"/></para>
- /// </remarks>
- public void DrawHotString (ustring text, Attribute hotColor, Attribute normalColor)
- {
- var hotkeySpec = HotKeySpecifier == (Rune)0xffff ? (Rune)'_' : HotKeySpecifier;
- Application.Driver.SetAttribute (normalColor);
- foreach (var rune in text) {
- if (rune == hotkeySpec) {
- Application.Driver.SetAttribute (hotColor);
- continue;
- }
- Application.Driver.AddRune (rune);
- Application.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 underscore before a letter flags the next letter as the hotkey.</param>
- /// <param name="focused">If set to <see langword="true"/> 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, Enabled ? scheme.HotNormal : scheme.Disabled, Enabled ? scheme.Normal : scheme.Disabled);
- }
- /// <summary>
- /// This moves the cursor to the specified column and row in the view.
- /// </summary>
- /// <returns>The move.</returns>
- /// <param name="col">The column to move to, in view-relative coordinates.</param>
- /// <param name="row">the row to move to, in view-relative coordinates.</param>
- /// <param name="clipped">Whether to clip the result of the ViewToScreen method,
- /// If <see langword="true"/>, the <paramref name="col"/> and <paramref name="row"/> values are clamped to the screen (terminal) dimensions (0..TerminalDim-1).</param>
- public void Move (int col, int row, bool clipped = true)
- {
- if (Driver.Rows == 0) {
- return;
- }
- ViewToScreen (col, row, out var rCol, out var rRow, clipped);
- 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 (!CanBeVisible (this) || !Enabled) {
- return;
- }
- // BUGBUG: v2 - This needs to support children of Frames too
- if (focused == null && SuperView != null) {
- SuperView.EnsureFocus ();
- } else if (focused?.Visible == true && focused?.Enabled == true && focused?.Frame.Width > 0 && focused.Frame.Height > 0) {
- focused.PositionCursor ();
- } else if (focused?.Visible == true && focused?.Enabled == false) {
- focused = null;
- } else if (CanFocus && HasFocus && Visible && Frame.Width > 0 && Frame.Height > 0) {
- Move (TextFormatter.HotKeyPos == -1 ? 0 : TextFormatter.CursorPosition, 0);
- } else {
- Move (frame.X, frame.Y);
- }
- }
- // BUGBUG: v2 - Seems weird that this is in View and not Responder.
- bool _hasFocus;
- /// <inheritdoc/>
- public override bool HasFocus => _hasFocus;
- void SetHasFocus (bool value, View view, bool force = false)
- {
- if (_hasFocus != value || force) {
- _hasFocus = value;
- if (value) {
- OnEnter (view);
- } else {
- OnLeave (view);
- }
- SetNeedsDisplay ();
- }
- // Remove focus down the chain of subviews if focus is removed
- if (!value && focused != null) {
- var f = focused;
- f.OnLeave (view);
- f.SetHasFocus (false, view);
- focused = null;
- }
- }
- /// <summary>
- /// Method invoked when a subview is being added to this view.
- /// </summary>
- /// <param name="e">Event where <see cref="ViewEventArgs.View"/> is the subview being added.</param>
- public virtual void OnAdded (SuperViewChangedEventArgs e)
- {
- var view = e.Child;
- view.IsAdded = true;
- view.x ??= view.frame.X;
- view.y ??= view.frame.Y;
- view.width ??= view.frame.Width;
- view.height ??= view.frame.Height;
- view.Added?.Invoke (this, e);
- }
- /// <summary>
- /// Method invoked when a subview is being removed from this view.
- /// </summary>
- /// <param name="e">Event args describing the subview being removed.</param>
- public virtual void OnRemoved (SuperViewChangedEventArgs e)
- {
- var view = e.Child;
- view.IsAdded = false;
- view.Removed?.Invoke (this, e);
- }
- /// <inheritdoc/>
- public override bool OnEnter (View view)
- {
- var args = new FocusEventArgs (view);
- Enter?.Invoke (this, args);
- if (args.Handled)
- return true;
- if (base.OnEnter (view))
- return true;
- return false;
- }
- /// <inheritdoc/>
- public override bool OnLeave (View view)
- {
- var args = new FocusEventArgs (view);
- Leave?.Invoke (this, args);
- if (args.Handled)
- return true;
- if (base.OnLeave (view))
- 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 View.</value>
- public View MostFocused {
- get {
- if (Focused == null)
- return null;
- var most = Focused.MostFocused;
- if (most != null)
- return most;
- return Focused;
- }
- }
- ColorScheme colorScheme;
- /// <summary>
- /// The color scheme for this view, if it is not defined, it returns the <see cref="SuperView"/>'s
- /// color scheme.
- /// </summary>
- public virtual ColorScheme ColorScheme {
- get {
- if (colorScheme == null) {
- return SuperView?.ColorScheme;
- }
- return colorScheme;
- }
- set {
- if (colorScheme != value) {
- colorScheme = value;
- SetNeedsDisplay ();
- }
- }
- }
- /// <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 ()
- {
- _needsDisplay = Rect.Empty;
- _childNeedsDisplay = false;
- }
- // TODO: Make this cancelable
- /// <summary>
- ///
- /// </summary>
- /// <returns></returns>
- public virtual bool OnDrawFrames (Rect bounds)
- {
- var prevClip = Driver.Clip;
- if (SuperView != null) {
- Driver.Clip = SuperView.ClipToBounds ();
- }
- Margin?.Redraw (Margin.Frame);
- BorderFrame?.Redraw (BorderFrame.Frame);
- Padding?.Redraw (Padding.Frame);
- Driver.Clip = prevClip;
- return true;
- }
- /// <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 globally 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 <ref name="bounds"/> parameter, as this will cause the driver to clip the entire region.
- /// </para>
- /// </remarks>
- public virtual void Redraw (Rect bounds)
- {
- if (!CanBeVisible (this)) {
- return;
- }
- OnDrawFrames (Frame);
- var prevClip = ClipToBounds ();
- // TODO: Implement complete event
- // OnDrawFramesComplete (Frame)
- if (ColorScheme != null) {
- //Driver.SetAttribute (HasFocus ? GetFocusColor () : GetNormalColor ());
- Driver.SetAttribute (GetNormalColor ());
- }
- Clear (ViewToScreen (bounds));
- // Invoke DrawContentEvent
- OnDrawContent (bounds);
- // Draw subviews
- // TODO: Implement OnDrawSubviews (cancelable);
- if (subviews != null) {
- foreach (var view in subviews) {
- if (true) { //!view._needsDisplay.IsEmpty || view._childNeedsDisplay || view.LayoutNeeded) {
- if (true) { //view.Frame.IntersectsWith (bounds)) { // && (view.Frame.IntersectsWith (bounds) || bounds.X < 0 || bounds.Y < 0)) {
- if (view.LayoutNeeded) {
- view.LayoutSubviews ();
- }
- // Draw the subview
- // Use the view's bounds (view-relative; Location will always be (0,0)
- //if (view.Visible && view.Frame.Width > 0 && view.Frame.Height > 0) {
- view.Redraw (view.Bounds);
- //}
- }
- view.ClearNeedsDisplay ();
- }
- }
- }
- // Invoke DrawContentCompleteEvent
- OnDrawContentComplete (bounds);
- // BUGBUG: v2 - We should be able to use View.SetClip here and not have to resort to knowing Driver details.
- Driver.Clip = prevClip;
- ClearLayoutNeeded ();
- 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 event EventHandler<DrawEventArgs> DrawContent;
- /// <summary>
- /// Enables overrides to draw infinitely scrolled content and/or a background behind added controls.
- /// </summary>
- /// <param name="contentArea">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 contentArea)
- {
- // TODO: Make DrawContent a cancelable event
- // if (!DrawContent?.Invoke(this, new DrawEventArgs (viewport)) {
- DrawContent?.Invoke (this, new DrawEventArgs (contentArea));
- if (!ustring.IsNullOrEmpty (TextFormatter.Text)) {
- if (TextFormatter != null) {
- TextFormatter.NeedsFormat = true;
- }
- TextFormatter?.Draw (ViewToScreen (contentArea), HasFocus ? GetFocusColor () : GetNormalColor (),
- HasFocus ? ColorScheme.HotFocus : GetHotNormalColor (),
- new Rect (ViewToScreen (contentArea).Location, Bounds.Size), true);
- SetSubViewNeedsDisplay ();
- }
- }
- /// <summary>
- /// Event invoked when the content area of the View is completed drawing.
- /// </summary>
- /// <remarks>
- /// <para>
- /// Will be invoked after any subviews removed with <see cref="Remove(View)"/> have been completed drawing.
- /// </para>
- /// <para>
- /// Rect provides the view-relative rectangle describing the currently visible viewport into the <see cref="View"/>.
- /// </para>
- /// </remarks>
- public event EventHandler<DrawEventArgs> DrawContentComplete;
- /// <summary>
- /// Enables overrides after completed drawing infinitely scrolled content and/or a background behind removed 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 after any subviews removed with <see cref="Remove(View)"/> have been completed drawing.
- /// </remarks>
- public virtual void OnDrawContentComplete (Rect viewport)
- {
- DrawContentComplete?.Invoke (this, new DrawEventArgs (viewport));
- }
- /// <summary>
- /// Causes the specified subview to have focus.
- /// </summary>
- /// <param name="view">View.</param>
- void SetFocus (View view)
- {
- if (view == null)
- return;
- //Console.WriteLine ($"Request to focus {view}");
- if (!view.CanFocus || !view.Visible || !view.Enabled)
- return;
- if (focused?._hasFocus == true && focused == view)
- return;
- // Make sure that this view is a subview
- View c;
- for (c = view._superView; c != null; c = c._superView)
- 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.SetHasFocus (false, view);
- var f = focused;
- focused = view;
- focused.SetHasFocus (true, f);
- focused.EnsureFocus ();
- // Send focus upwards
- SuperView?.SetFocus (this);
- }
- /// <summary>
- /// Causes the specified view and the entire parent hierarchy to have the focused order updated.
- /// </summary>
- public void SetFocus ()
- {
- if (!CanBeVisible (this) || !Enabled) {
- if (HasFocus) {
- SetHasFocus (false, this);
- }
- return;
- }
- SuperView?.SetFocus (this);
- }
- /// <summary>
- /// Invoked when a character key is pressed and occurs after the key up event.
- /// </summary>
- public event EventHandler<KeyEventEventArgs> KeyPress;
- /// <inheritdoc/>
- public override bool ProcessKey (KeyEvent keyEvent)
- {
- if (!Enabled) {
- return false;
- }
- var args = new KeyEventEventArgs (keyEvent);
- KeyPress?.Invoke (this, args);
- if (args.Handled)
- return true;
- if (Focused?.Enabled == true) {
- Focused?.KeyPress?.Invoke (this, args);
- if (args.Handled)
- return true;
- }
- return Focused?.Enabled == true && Focused?.ProcessKey (keyEvent) == true;
- }
- /// <summary>
- /// Invokes any binding that is registered on this <see cref="View"/>
- /// and matches the <paramref name="keyEvent"/>
- /// </summary>
- /// <param name="keyEvent">The key event passed.</param>
- protected bool? InvokeKeybindings (KeyEvent keyEvent)
- {
- bool? toReturn = null;
- if (KeyBindings.ContainsKey (keyEvent.Key)) {
- foreach (var command in KeyBindings [keyEvent.Key]) {
- if (!CommandImplementations.ContainsKey (command)) {
- throw new NotSupportedException ($"A KeyBinding was set up for the command {command} ({keyEvent.Key}) but that command is not supported by this View ({GetType ().Name})");
- }
- // each command has its own return value
- var thisReturn = CommandImplementations [command] ();
- // if we haven't got anything yet, the current command result should be used
- if (toReturn == null) {
- toReturn = thisReturn;
- }
- // if ever see a true then that's what we will return
- if (thisReturn ?? false) {
- toReturn = true;
- }
- }
- }
- return toReturn;
- }
- /// <summary>
- /// <para>Adds a new key combination that will trigger the given <paramref name="command"/>
- /// (if supported by the View - see <see cref="GetSupportedCommands"/>)
- /// </para>
- /// <para>If the key is already bound to a different <see cref="Command"/> it will be
- /// rebound to this one</para>
- /// <remarks>Commands are only ever applied to the current <see cref="View"/>(i.e. this feature
- /// cannot be used to switch focus to another view and perform multiple commands there) </remarks>
- /// </summary>
- /// <param name="key"></param>
- /// <param name="command">The command(s) to run on the <see cref="View"/> when <paramref name="key"/> is pressed.
- /// When specifying multiple commands, all commands will be applied in sequence. The bound <paramref name="key"/> strike
- /// will be consumed if any took effect.</param>
- public void AddKeyBinding (Key key, params Command [] command)
- {
- if (command.Length == 0) {
- throw new ArgumentException ("At least one command must be specified", nameof (command));
- }
- if (KeyBindings.ContainsKey (key)) {
- KeyBindings [key] = command;
- } else {
- KeyBindings.Add (key, command);
- }
- }
- /// <summary>
- /// Replaces a key combination already bound to <see cref="Command"/>.
- /// </summary>
- /// <param name="fromKey">The key to be replaced.</param>
- /// <param name="toKey">The new key to be used.</param>
- protected void ReplaceKeyBinding (Key fromKey, Key toKey)
- {
- if (KeyBindings.ContainsKey (fromKey)) {
- var value = KeyBindings [fromKey];
- KeyBindings.Remove (fromKey);
- KeyBindings [toKey] = value;
- }
- }
- /// <summary>
- /// Checks if the key binding already exists.
- /// </summary>
- /// <param name="key">The key to check.</param>
- /// <returns><see langword="true"/> If the key already exist, <see langword="false"/> otherwise.</returns>
- public bool ContainsKeyBinding (Key key)
- {
- return KeyBindings.ContainsKey (key);
- }
- /// <summary>
- /// Removes all bound keys from the View and resets the default bindings.
- /// </summary>
- public void ClearKeybindings ()
- {
- KeyBindings.Clear ();
- }
- /// <summary>
- /// Clears the existing keybinding (if any) for the given <paramref name="key"/>.
- /// </summary>
- /// <param name="key"></param>
- public void ClearKeybinding (Key key)
- {
- KeyBindings.Remove (key);
- }
- /// <summary>
- /// Removes all key bindings that trigger the given command. Views can have multiple different
- /// keys bound to the same command and this method will clear all of them.
- /// </summary>
- /// <param name="command"></param>
- public void ClearKeybinding (params Command [] command)
- {
- foreach (var kvp in KeyBindings.Where (kvp => kvp.Value.SequenceEqual (command)).ToArray ()) {
- KeyBindings.Remove (kvp.Key);
- }
- }
- /// <summary>
- /// <para>States that the given <see cref="View"/> supports a given <paramref name="command"/>
- /// and what <paramref name="f"/> to perform to make that command happen
- /// </para>
- /// <para>If the <paramref name="command"/> already has an implementation the <paramref name="f"/>
- /// will replace the old one</para>
- /// </summary>
- /// <param name="command">The command.</param>
- /// <param name="f">The function.</param>
- protected void AddCommand (Command command, Func<bool?> f)
- {
- // if there is already an implementation of this command
- if (CommandImplementations.ContainsKey (command)) {
- // replace that implementation
- CommandImplementations [command] = f;
- } else {
- // else record how to perform the action (this should be the normal case)
- CommandImplementations.Add (command, f);
- }
- }
- /// <summary>
- /// Returns all commands that are supported by this <see cref="View"/>.
- /// </summary>
- /// <returns></returns>
- public IEnumerable<Command> GetSupportedCommands ()
- {
- return CommandImplementations.Keys;
- }
- /// <summary>
- /// Gets the key used by a command.
- /// </summary>
- /// <param name="command">The command to search.</param>
- /// <returns>The <see cref="Key"/> used by a <see cref="Command"/></returns>
- public Key GetKeyFromCommand (params Command [] command)
- {
- return KeyBindings.First (kb => kb.Value.SequenceEqual (command)).Key;
- }
- /// <inheritdoc/>
- public override bool ProcessHotKey (KeyEvent keyEvent)
- {
- if (!Enabled) {
- return false;
- }
- var args = new KeyEventEventArgs (keyEvent);
- if (MostFocused?.Enabled == true) {
- MostFocused?.KeyPress?.Invoke (this, args);
- if (args.Handled)
- return true;
- }
- if (MostFocused?.Enabled == true && MostFocused?.ProcessKey (keyEvent) == true)
- return true;
- if (subviews == null || subviews.Count == 0)
- return false;
- foreach (var view in subviews)
- if (view.Enabled && view.ProcessHotKey (keyEvent))
- return true;
- return false;
- }
- /// <inheritdoc/>
- public override bool ProcessColdKey (KeyEvent keyEvent)
- {
- if (!Enabled) {
- return false;
- }
- var args = new KeyEventEventArgs (keyEvent);
- KeyPress?.Invoke (this, args);
- if (args.Handled)
- return true;
- if (MostFocused?.Enabled == true) {
- MostFocused?.KeyPress?.Invoke (this, args);
- if (args.Handled)
- return true;
- }
- if (MostFocused?.Enabled == true && MostFocused?.ProcessKey (keyEvent) == true)
- return true;
- if (subviews == null || subviews.Count == 0)
- return false;
- foreach (var view in subviews)
- if (view.Enabled && view.ProcessColdKey (keyEvent))
- return true;
- return false;
- }
- /// <summary>
- /// Invoked when a key is pressed.
- /// </summary>
- public event EventHandler<KeyEventEventArgs> KeyDown;
- /// <inheritdoc/>
- public override bool OnKeyDown (KeyEvent keyEvent)
- {
- if (!Enabled) {
- return false;
- }
- var args = new KeyEventEventArgs (keyEvent);
- KeyDown?.Invoke (this, args);
- if (args.Handled) {
- return true;
- }
- if (Focused?.Enabled == true) {
- Focused.KeyDown?.Invoke (this, args);
- if (args.Handled) {
- return true;
- }
- if (Focused?.OnKeyDown (keyEvent) == true) {
- return true;
- }
- }
- return false;
- }
- /// <summary>
- /// Invoked when a key is released.
- /// </summary>
- public event EventHandler<KeyEventEventArgs> KeyUp;
- /// <inheritdoc/>
- public override bool OnKeyUp (KeyEvent keyEvent)
- {
- if (!Enabled) {
- return false;
- }
- var args = new KeyEventEventArgs (keyEvent);
- KeyUp?.Invoke (this, args);
- if (args.Handled) {
- return true;
- }
- if (Focused?.Enabled == true) {
- Focused.KeyUp?.Invoke (this, args);
- if (args.Handled) {
- return true;
- }
- if (Focused?.OnKeyUp (keyEvent) == true) {
- return true;
- }
- }
- return false;
- }
- /// <summary>
- /// Finds the first view in the hierarchy that wants to get the focus if nothing is currently focused, otherwise, does nothing.
- /// </summary>
- public void EnsureFocus ()
- {
- if (focused == null && subviews?.Count > 0) {
- if (FocusDirection == Direction.Forward) {
- FocusFirst ();
- } else {
- FocusLast ();
- }
- }
- }
- /// <summary>
- /// Focuses the first focusable subview if one exists.
- /// </summary>
- public void FocusFirst ()
- {
- if (!CanBeVisible (this)) {
- return;
- }
- if (tabIndexes == null) {
- SuperView?.SetFocus (this);
- return;
- }
- foreach (var view in tabIndexes) {
- if (view.CanFocus && view.tabStop && view.Visible && view.Enabled) {
- SetFocus (view);
- return;
- }
- }
- }
- /// <summary>
- /// Focuses the last focusable subview if one exists.
- /// </summary>
- public void FocusLast ()
- {
- if (!CanBeVisible (this)) {
- return;
- }
- if (tabIndexes == null) {
- SuperView?.SetFocus (this);
- return;
- }
- for (var i = tabIndexes.Count; i > 0;) {
- i--;
- var v = tabIndexes [i];
- if (v.CanFocus && v.tabStop && v.Visible && v.Enabled) {
- SetFocus (v);
- return;
- }
- }
- }
- /// <summary>
- /// Focuses the previous view.
- /// </summary>
- /// <returns><see langword="true"/> if previous was focused, <see langword="false"/> otherwise.</returns>
- public bool FocusPrev ()
- {
- if (!CanBeVisible (this)) {
- return false;
- }
- FocusDirection = Direction.Backward;
- if (tabIndexes == null || tabIndexes.Count == 0)
- return false;
- if (focused == null) {
- FocusLast ();
- return focused != null;
- }
- var focusedIdx = -1;
- for (var i = tabIndexes.Count; i > 0;) {
- i--;
- var w = tabIndexes [i];
- if (w.HasFocus) {
- if (w.FocusPrev ())
- return true;
- focusedIdx = i;
- continue;
- }
- if (w.CanFocus && focusedIdx != -1 && w.tabStop && w.Visible && w.Enabled) {
- focused.SetHasFocus (false, w);
- if (w.CanFocus && w.tabStop && w.Visible && w.Enabled)
- w.FocusLast ();
- SetFocus (w);
- return true;
- }
- }
- if (focused != null) {
- focused.SetHasFocus (false, this);
- focused = null;
- }
- return false;
- }
- /// <summary>
- /// Focuses the next view.
- /// </summary>
- /// <returns><see langword="true"/> if next was focused, <see langword="false"/> otherwise.</returns>
- public bool FocusNext ()
- {
- if (!CanBeVisible (this)) {
- return false;
- }
- FocusDirection = Direction.Forward;
- if (tabIndexes == null || tabIndexes.Count == 0)
- return false;
- if (focused == null) {
- FocusFirst ();
- return focused != null;
- }
- var n = tabIndexes.Count;
- var focusedIdx = -1;
- for (var i = 0; i < n; i++) {
- var w = tabIndexes [i];
- if (w.HasFocus) {
- if (w.FocusNext ())
- return true;
- focusedIdx = i;
- continue;
- }
- if (w.CanFocus && focusedIdx != -1 && w.tabStop && w.Visible && w.Enabled) {
- focused.SetHasFocus (false, w);
- if (w.CanFocus && w.tabStop && w.Visible && w.Enabled)
- w.FocusFirst ();
- SetFocus (w);
- return true;
- }
- }
- if (focused != null) {
- focused.SetHasFocus (false, this);
- focused = null;
- }
- return false;
- }
- View GetMostFocused (View view)
- {
- if (view == null) {
- return null;
- }
- return view.focused != null ? GetMostFocused (view.focused) : view;
- }
- /// <summary>
- /// Sets the View's <see cref="Frame"/> to the frame-relative coordinates if its container. The
- /// container size and location are specified by <paramref name="superviewFrame"/> and are relative to the
- /// View's superview.
- /// </summary>
- /// <param name="superviewFrame">The supserview-relative rectangle describing View's container (nominally the
- /// same as <c>this.SuperView.Frame</c>).</param>
- internal void SetRelativeLayout (Rect superviewFrame)
- {
- int newX, newW, newY, newH;
- var autosize = Size.Empty;
- if (AutoSize) {
- // Note this is global to this function and used as such within the local functions defined
- // below. In v2 AutoSize will be re-factored to not need to be dealt with in this function.
- autosize = GetAutoSize ();
- }
- // Returns the new dimension (width or height) and location (x or y) for the View given
- // the superview's Frame.X or Frame.Y
- // the superview's width or height
- // the current Pos (View.X or View.Y)
- // the current Dim (View.Width or View.Height)
- (int newLocation, int newDimension) GetNewLocationAndDimension (int superviewLocation, int superviewDimension, Pos pos, Dim dim, int autosizeDimension)
- {
- int newDimension, newLocation;
- switch (pos) {
- case Pos.PosCenter:
- if (dim == null) {
- newDimension = AutoSize ? autosizeDimension : superviewDimension;
- } else {
- newDimension = dim.Anchor (superviewDimension);
- newDimension = AutoSize && autosizeDimension > newDimension ? autosizeDimension : newDimension;
- }
- newLocation = pos.Anchor (superviewDimension - newDimension);
- break;
- case Pos.PosCombine combine:
- int left, right;
- (left, newDimension) = GetNewLocationAndDimension (superviewLocation, superviewDimension, combine.left, dim, autosizeDimension);
- (right, newDimension) = GetNewLocationAndDimension (superviewLocation, superviewDimension, combine.right, dim, autosizeDimension);
- if (combine.add) {
- newLocation = left + right;
- } else {
- newLocation = left - right;
- }
- newDimension = Math.Max (CalculateNewDimension (dim, newLocation, superviewDimension, autosizeDimension), 0);
- break;
- case Pos.PosAbsolute:
- case Pos.PosAnchorEnd:
- case Pos.PosFactor:
- case Pos.PosFunc:
- case Pos.PosView:
- default:
- newLocation = pos?.Anchor (superviewDimension) ?? 0;
- newDimension = Math.Max (CalculateNewDimension (dim, newLocation, superviewDimension, autosizeDimension), 0);
- break;
- }
- return (newLocation, newDimension);
- }
- // Recursively calculates the new dimension (width or height) of the given Dim given:
- // the current location (x or y)
- // the current dimension (width or height)
- int CalculateNewDimension (Dim d, int location, int dimension, int autosize)
- {
- int newDimension;
- switch (d) {
- case null:
- newDimension = AutoSize ? autosize : dimension;
- break;
- case Dim.DimCombine combine:
- int leftNewDim = CalculateNewDimension (combine.left, location, dimension, autosize);
- int rightNewDim = CalculateNewDimension (combine.right, location, dimension, autosize);
- if (combine.add) {
- newDimension = leftNewDim + rightNewDim;
- } else {
- newDimension = leftNewDim - rightNewDim;
- }
- newDimension = AutoSize && autosize > newDimension ? autosize : newDimension;
- break;
- case Dim.DimFactor factor when !factor.IsFromRemaining ():
- newDimension = d.Anchor (dimension);
- newDimension = AutoSize && autosize > newDimension ? autosize : newDimension;
- break;
- case Dim.DimFill:
- default:
- newDimension = Math.Max (d.Anchor (dimension - location), 0);
- newDimension = AutoSize && autosize > newDimension ? autosize : newDimension;
- break;
- }
- return newDimension;
- }
- // horizontal
- (newX, newW) = GetNewLocationAndDimension (superviewFrame.X, superviewFrame.Width, x, width, autosize.Width);
- // vertical
- (newY, newH) = GetNewLocationAndDimension (superviewFrame.Y, superviewFrame.Height, y, height, autosize.Height);
- var r = new Rect (newX, newY, newW, newH);
- if (Frame != r) {
- Frame = r;
- // BUGBUG: Why is this AFTER setting Frame? Seems duplicative.
- if (!SetMinWidthHeight ()) {
- TextFormatter.Size = GetSizeNeededForTextAndHotKey ();
- }
- }
- }
- /// <summary>
- /// Fired after the View'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 event EventHandler<LayoutEventArgs> LayoutStarted;
- /// <summary>
- /// Raises the <see cref="LayoutStarted"/> event. Called from <see cref="LayoutSubviews"/> before any subviews have been laid out.
- /// </summary>
- internal virtual void OnLayoutStarted (LayoutEventArgs args)
- {
- LayoutStarted?.Invoke (this, args);
- }
- /// <summary>
- /// Fired after the View'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 event EventHandler<LayoutEventArgs> LayoutComplete;
- /// <summary>
- /// Event called only once when the <see cref="View"/> is being initialized for the first time.
- /// Allows configurations and assignments to be performed before the <see cref="View"/> being shown.
- /// This derived from <see cref="ISupportInitializeNotification"/> to allow notify all the views that are being initialized.
- /// </summary>
- public event EventHandler Initialized;
- /// <summary>
- /// Raises the <see cref="LayoutComplete"/> event. Called from <see cref="LayoutSubviews"/> before all sub-views have been laid out.
- /// </summary>
- internal virtual void OnLayoutComplete (LayoutEventArgs args)
- {
- LayoutComplete?.Invoke (this, args);
- }
- internal void CollectPos (Pos pos, View from, ref HashSet<View> nNodes, ref HashSet<(View, View)> nEdges)
- {
- switch (pos) {
- case Pos.PosView pv:
- if (pv.Target != this) {
- nEdges.Add ((pv.Target, from));
- }
- foreach (var v in from.InternalSubviews) {
- CollectAll (v, ref nNodes, ref nEdges);
- }
- return;
- case Pos.PosCombine pc:
- foreach (var v in from.InternalSubviews) {
- CollectPos (pc.left, from, ref nNodes, ref nEdges);
- CollectPos (pc.right, from, ref nNodes, ref nEdges);
- }
- break;
- }
- }
- internal void CollectDim (Dim dim, View from, ref HashSet<View> nNodes, ref HashSet<(View, View)> nEdges)
- {
- switch (dim) {
- case Dim.DimView dv:
- if (dv.Target != this) {
- nEdges.Add ((dv.Target, from));
- }
- foreach (var v in from.InternalSubviews) {
- CollectAll (v, ref nNodes, ref nEdges);
- }
- return;
- case Dim.DimCombine dc:
- foreach (var v in from.InternalSubviews) {
- CollectDim (dc.left, from, ref nNodes, ref nEdges);
- CollectDim (dc.right, from, ref nNodes, ref nEdges);
- }
- break;
- }
- }
- internal void CollectAll (View from, ref HashSet<View> nNodes, ref HashSet<(View, View)> nEdges)
- {
- foreach (var v in from.InternalSubviews) {
- nNodes.Add (v);
- if (v._layoutStyle != LayoutStyle.Computed) {
- continue;
- }
- CollectPos (v.X, v, ref nNodes, ref nEdges);
- CollectPos (v.Y, v, ref nNodes, ref nEdges);
- CollectDim (v.Width, v, ref nNodes, ref nEdges);
- CollectDim (v.Height, v, ref nNodes, ref nEdges);
- }
- }
- // https://en.wikipedia.org/wiki/Topological_sorting
- internal static List<View> TopologicalSort (View superView, IEnumerable<View> nodes, ICollection<(View From, View To)> edges)
- {
- var result = new List<View> ();
- // Set of all nodes with no incoming edges
- var noEdgeNodes = new HashSet<View> (nodes.Where (n => edges.All (e => !e.To.Equals (n))));
- while (noEdgeNodes.Any ()) {
- // remove a node n from S
- var n = noEdgeNodes.First ();
- noEdgeNodes.Remove (n);
- // add n to tail of L
- if (n != 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)) && m != superView) {
- // insert m into S
- noEdgeNodes.Add (m);
- }
- }
- }
- if (edges.Any ()) {
- (var from, var to) = edges.First ();
- if (from != superView?.GetTopSuperView (to, from)) {
- if (!ReferenceEquals (from, to)) {
- if (ReferenceEquals (from.SuperView, to)) {
- throw new InvalidOperationException ($"ComputedLayout for \"{superView}\": \"{to}\" references a SubView (\"{from}\").");
- } else {
- throw new InvalidOperationException ($"ComputedLayout for \"{superView}\": \"{from}\" linked with \"{to}\" was not found. Did you forget to add it to {superView}?");
- }
- } else {
- throw new InvalidOperationException ($"ComputedLayout for \"{superView}\": A recursive cycle was found in the relative Pos/Dim of the SubViews.");
- }
- }
- }
- // return L (a topologically sorted order)
- return result;
- } // TopologicalSort
- /// <summary>
- /// Overriden by <see cref="Frame"/> to do nothing, as the <see cref="Frame"/> does not have frames.
- /// </summary>
- internal virtual void LayoutFrames ()
- {
- Margin.X = 0;
- Margin.Y = 0;
- Margin.Width = Frame.Size.Width;
- Margin.Height = Frame.Size.Height;
- Margin.SetNeedsLayout ();
- Margin.LayoutSubviews ();
- Margin.SetNeedsDisplay ();
- var border = Margin.Thickness.GetInside (Margin.Frame);
- BorderFrame.X = border.Location.X;
- BorderFrame.Y = border.Location.Y;
- BorderFrame.Width = border.Size.Width;
- BorderFrame.Height = border.Size.Height;
- BorderFrame.SetNeedsLayout ();
- BorderFrame.LayoutSubviews ();
- BorderFrame.SetNeedsDisplay ();
- var padding = BorderFrame.Thickness.GetInside (BorderFrame.Frame);
- Padding.X = padding.Location.X;
- Padding.Y = padding.Location.Y;
- Padding.Width = padding.Size.Width;
- Padding.Height = padding.Size.Height;
- Padding.SetNeedsLayout ();
- Padding.LayoutSubviews ();
- Padding.SetNeedsDisplay ();
- }
- /// <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;
- }
- LayoutFrames ();
- var oldBounds = Bounds;
- OnLayoutStarted (new LayoutEventArgs () { OldBounds = oldBounds });
- TextFormatter.Size = GetSizeNeededForTextAndHotKey ();
- // Sort out the dependencies of the X, Y, Width, Height properties
- var nodes = new HashSet<View> ();
- var edges = new HashSet<(View, View)> ();
- CollectAll (this, ref nodes, ref edges);
- var ordered = View.TopologicalSort (SuperView, nodes, edges);
- foreach (var v in ordered) {
- LayoutSubview (v, new Rect (GetBoundsOffset (), Bounds.Size));
- }
- // If the 'to' is rooted to 'from' and the layoutstyle is Computed it's a special-case.
- // Use LayoutSubview with the Frame of the 'from'
- if (SuperView != null && GetTopSuperView () != null && LayoutNeeded
- && ordered.Count == 0 && edges.Count > 0 && LayoutStyle == LayoutStyle.Computed) {
- (var from, var to) = edges.First ();
- LayoutSubview (to, from.Frame);
- }
- LayoutNeeded = false;
- OnLayoutComplete (new LayoutEventArgs () { OldBounds = oldBounds });
- }
- private void LayoutSubview (View v, Rect contentArea)
- {
- if (v.LayoutStyle == LayoutStyle.Computed) {
- v.SetRelativeLayout (contentArea);
- }
- v.LayoutSubviews ();
- v.LayoutNeeded = false;
- }
- ustring text;
- /// <summary>
- /// The text displayed by the <see cref="View"/>.
- /// </summary>
- /// <remarks>
- /// <para>
- /// If provided, the text will be drawn before any subviews are drawn.
- /// </para>
- /// <para>
- /// The text will be drawn starting at the view origin (0, 0) and will be formatted according
- /// to the <see cref="TextAlignment"/> property. If the view's height is greater than 1, the
- /// text will word-wrap to additional lines if it does not fit horizontally. If the view's height
- /// is 1, the text will be clipped.
- /// </para>
- /// <para>
- /// Set the <see cref="HotKeySpecifier"/> to enable hotkey support. To disable hotkey support set <see cref="HotKeySpecifier"/> to
- /// <c>(Rune)0xffff</c>.
- /// </para>
- /// </remarks>
- public virtual ustring Text {
- get => text;
- set {
- text = value;
- if (IsInitialized) {
- SetHotKey ();
- UpdateTextFormatterText ();
- OnResizeNeeded ();
- }
- // BUGBUG: v2 - This is here as a HACK until we fix the unit tests to not check a view's dims until
- // after it's been initialized. See #2450
- UpdateTextFormatterText ();
- #if DEBUG
- if (text != null && string.IsNullOrEmpty (Id)) {
- Id = text.ToString ();
- }
- #endif
- }
- }
- /// <summary>
- /// Gets or sets a flag that determines whether the View will be automatically resized to fit the <see cref="Text"/>
- /// within <see cref="Bounds"/>
- /// <para>
- /// The default is <see langword="false"/>. Set to <see langword="true"/> to turn on AutoSize. If <see langword="true"/> then
- /// <see cref="Width"/> and <see cref="Height"/> will be used if <see cref="Text"/> can fit;
- /// if <see cref="Text"/> won't fit the view will be resized as needed.
- /// </para>
- /// <para>
- /// In addition, if <see cref="ForceValidatePosDim"/> is <see langword="true"/> the new values of <see cref="Width"/> and
- /// <see cref="Height"/> must be of the same types of the existing one to avoid breaking the <see cref="Dim"/> settings.
- /// </para>
- /// </summary>
- public virtual bool AutoSize {
- get => autoSize;
- set {
- var v = ResizeView (value);
- TextFormatter.AutoSize = v;
- if (autoSize != v) {
- autoSize = v;
- TextFormatter.NeedsFormat = true;
- UpdateTextFormatterText ();
- OnResizeNeeded ();
- }
- }
- }
- /// <summary>
- /// Gets or sets a flag that determines whether <see cref="Terminal.Gui.TextFormatter.Text"/> will have trailing spaces preserved
- /// or not when <see cref="Terminal.Gui.TextFormatter.WordWrap"/> is enabled. If <see langword="true"/>
- /// any trailing spaces will be trimmed when either the <see cref="Text"/> property is changed or
- /// when <see cref="Terminal.Gui.TextFormatter.WordWrap"/> is set to <see langword="true"/>.
- /// The default is <see langword="false"/>.
- /// </summary>
- public virtual bool PreserveTrailingSpaces {
- get => TextFormatter.PreserveTrailingSpaces;
- set {
- if (TextFormatter.PreserveTrailingSpaces != value) {
- TextFormatter.PreserveTrailingSpaces = value;
- TextFormatter.NeedsFormat = true;
- }
- }
- }
- /// <summary>
- /// Gets or sets how the View's <see cref="Text"/> is aligned horizontally when drawn. Changing this property will redisplay the <see cref="View"/>.
- /// </summary>
- /// <value>The text alignment.</value>
- public virtual TextAlignment TextAlignment {
- get => TextFormatter.Alignment;
- set {
- TextFormatter.Alignment = value;
- UpdateTextFormatterText ();
- OnResizeNeeded ();
- }
- }
- /// <summary>
- /// Gets or sets how the View's <see cref="Text"/> is aligned vertically when drawn. Changing this property will redisplay the <see cref="View"/>.
- /// </summary>
- /// <value>The text alignment.</value>
- public virtual VerticalTextAlignment VerticalTextAlignment {
- get => TextFormatter.VerticalAlignment;
- set {
- TextFormatter.VerticalAlignment = value;
- SetNeedsDisplay ();
- }
- }
- /// <summary>
- /// Gets or sets the direction of the View's <see cref="Text"/>. Changing this property will redisplay the <see cref="View"/>.
- /// </summary>
- /// <value>The text alignment.</value>
- public virtual TextDirection TextDirection {
- get => TextFormatter.Direction;
- set {
- UpdateTextDirection (value);
- }
- }
- private void UpdateTextDirection (TextDirection newDirection)
- {
- var directionChanged = TextFormatter.IsHorizontalDirection (TextFormatter.Direction)
- != TextFormatter.IsHorizontalDirection (newDirection);
- TextFormatter.Direction = newDirection;
-
- if (!IsInitialized) return;
- var isValidOldAutoSize = autoSize && IsValidAutoSize (out var _);
- UpdateTextFormatterText ();
- if ((!ForceValidatePosDim && directionChanged && AutoSize)
- || (ForceValidatePosDim && directionChanged && AutoSize && isValidOldAutoSize)) {
- OnResizeNeeded ();
- } else if (directionChanged && IsAdded) {
- SetWidthHeight (Bounds.Size);
- SetMinWidthHeight ();
- } else {
- SetMinWidthHeight ();
- }
- TextFormatter.Size = GetSizeNeededForTextAndHotKey ();
- SetNeedsDisplay ();
- }
- /// <summary>
- /// Get or sets if the <see cref="View"/> has been initialized (via <see cref="ISupportInitialize.BeginInit"/>
- /// and <see cref="ISupportInitialize.EndInit"/>).
- /// </summary>
- /// <para>
- /// If first-run-only initialization is preferred, overrides to <see cref="ISupportInitializeNotification.IsInitialized"/>
- /// can be implemented, in which case the <see cref="ISupportInitialize"/>
- /// methods will only be called if <see cref="ISupportInitializeNotification.IsInitialized"/>
- /// is <see langword="false"/>. This allows proper <see cref="View"/> inheritance hierarchies
- /// to override base class layout code optimally by doing so only on first run,
- /// instead of on every run.
- /// </para>
- public virtual bool IsInitialized { get; set; }
- /// <summary>
- /// Gets information if the view was already added to the <see cref="SuperView"/>.
- /// </summary>
- public bool IsAdded { get; private set; }
- bool oldEnabled;
- /// <inheritdoc/>
- public override bool Enabled {
- get => base.Enabled;
- set {
- if (base.Enabled != value) {
- if (value) {
- if (SuperView == null || SuperView?.Enabled == true) {
- base.Enabled = value;
- }
- } else {
- base.Enabled = value;
- }
- if (!value && HasFocus) {
- SetHasFocus (false, this);
- }
- OnEnabledChanged ();
- SetNeedsDisplay ();
- if (subviews != null) {
- foreach (var view in subviews) {
- if (!value) {
- view.oldEnabled = view.Enabled;
- view.Enabled = false;
- } else {
- view.Enabled = view.oldEnabled;
- view.addingView = false;
- }
- }
- }
- }
- }
- }
- /// <summary>
- /// Gets or sets whether a view is cleared if the <see cref="Visible"/> property is <see langword="false"/>.
- /// </summary>
- public bool ClearOnVisibleFalse { get; set; } = true;
- /// <inheritdoc/>>
- public override bool Visible {
- get => base.Visible;
- set {
- if (base.Visible != value) {
- base.Visible = value;
- if (!value) {
- if (HasFocus) {
- SetHasFocus (false, this);
- }
- if (ClearOnVisibleFalse) {
- Clear ();
- }
- }
- OnVisibleChanged ();
- SetNeedsDisplay ();
- }
- }
- }
- // TODO: v2 Nuke teh Border property (rename BorderFrame to Border)
- Border border;
- /// <inheritdoc/>
- public virtual Border Border {
- get => border;
- set {
- if (border != value) {
- border = value;
- SetNeedsDisplay ();
- if (border != null) border.BorderChanged += Border_BorderChanged;
- }
- }
- }
- // TODO: v2 nuke This
- /// <summary>
- /// </summary>
- public virtual bool IgnoreBorderPropertyOnRedraw { get; set; }
- /// <summary>
- /// Pretty prints the View
- /// </summary>
- /// <returns></returns>
- public override string ToString ()
- {
- return $"{GetType ().Name}({Id})({Frame})";
- }
- void SetHotKey ()
- {
- if (TextFormatter == null) {
- return; // throw new InvalidOperationException ("Can't set HotKey unless a TextFormatter has been created");
- }
- TextFormatter.FindHotKey (text, HotKeySpecifier, true, out _, out var hk);
- if (hotKey != hk) {
- HotKey = hk;
- }
- }
- bool ResizeView (bool autoSize)
- {
- if (!autoSize) {
- return false;
- }
- var aSize = true;
- var nBoundsSize = GetAutoSize ();
- if (nBoundsSize != Bounds.Size) {
- if (ForceValidatePosDim) {
- aSize = SetWidthHeight (nBoundsSize);
- } else {
- Height = nBoundsSize.Height;
- Width = nBoundsSize.Width; // = new Rect (Bounds.X, Bounds.Y, nBoundsSize.Width, nBoundsSize.Height);
- }
- }
- // BUGBUG: This call may be redundant
- TextFormatter.Size = GetSizeNeededForTextAndHotKey ();
- return aSize;
- }
- /// <summary>
- /// Resizes the View to fit the specified <see cref="Bounds"/> size.
- /// </summary>
- /// <param name="nBounds"></param>
- /// <returns></returns>
- bool SetWidthHeight (Size nBounds)
- {
- var aSize = false;
- var canSizeW = TrySetWidth (nBounds.Width - GetHotKeySpecifierLength (), out var rW);
- var canSizeH = TrySetHeight (nBounds.Height - GetHotKeySpecifierLength (false), out var rH);
- if (canSizeW) {
- aSize = true;
- width = rW;
- }
- if (canSizeH) {
- aSize = true;
- height = rH;
- }
- if (aSize) {
- Bounds = new Rect (Bounds.X, Bounds.Y, canSizeW ? rW : Bounds.Width, canSizeH ? rH : Bounds.Height);
- }
- return aSize;
- }
- /// <summary>
- /// Gets the dimensions required to fit <see cref="Text"/> using the text <see cref="Direction"/> specified by the
- /// <see cref="TextFormatter"/> property and accounting for any <see cref="HotKeySpecifier"/> characters.
- /// .
- /// </summary>
- /// <returns>The <see cref="Size"/> required to fit the text.</returns>
- public Size GetAutoSize ()
- {
- var rect = TextFormatter.CalcRect (Bounds.X, Bounds.Y, TextFormatter.Text, TextFormatter.Direction);
- return new Size (rect.Size.Width - GetHotKeySpecifierLength (),
- rect.Size.Height - GetHotKeySpecifierLength (false));
- }
- bool IsValidAutoSize (out Size autoSize)
- {
- var rect = TextFormatter.CalcRect (frame.X, frame.Y, TextFormatter.Text, TextDirection);
- autoSize = new Size (rect.Size.Width - GetHotKeySpecifierLength (),
- rect.Size.Height - GetHotKeySpecifierLength (false));
- return !(ForceValidatePosDim && (!(Width is Dim.DimAbsolute) || !(Height is Dim.DimAbsolute))
- || frame.Size.Width != rect.Size.Width - GetHotKeySpecifierLength ()
- || frame.Size.Height != rect.Size.Height - GetHotKeySpecifierLength (false));
- }
- bool IsValidAutoSizeWidth (Dim width)
- {
- var rect = TextFormatter.CalcRect (frame.X, frame.Y, TextFormatter.Text, TextDirection);
- var dimValue = width.Anchor (0);
- return !(ForceValidatePosDim && (!(width is Dim.DimAbsolute)) || dimValue != rect.Size.Width
- - GetHotKeySpecifierLength ());
- }
- bool IsValidAutoSizeHeight (Dim height)
- {
- var rect = TextFormatter.CalcRect (frame.X, frame.Y, TextFormatter.Text, TextDirection);
- var dimValue = height.Anchor (0);
- return !(ForceValidatePosDim && (!(height is Dim.DimAbsolute)) || dimValue != rect.Size.Height
- - GetHotKeySpecifierLength (false));
- }
- /// <summary>
- /// Gets the width or height of the <see cref="Terminal.Gui.TextFormatter.HotKeySpecifier"/> characters
- /// in the <see cref="Text"/> property.
- /// </summary>
- /// <remarks>
- /// Only the first hotkey specifier found in <see cref="Text"/> is supported.
- /// </remarks>
- /// <param name="isWidth">If <see langword="true"/> (the default) the width required for the hotkey specifier is returned. Otherwise the height is returned.</param>
- /// <returns>The number of characters required for the <see cref="Terminal.Gui.TextFormatter.HotKeySpecifier"/>. If the text direction specified
- /// by <see cref="TextDirection"/> does not match the <paramref name="isWidth"/> parameter, <c>0</c> is returned.</returns>
- public int GetHotKeySpecifierLength (bool isWidth = true)
- {
- if (isWidth) {
- return TextFormatter.IsHorizontalDirection (TextDirection) &&
- TextFormatter.Text?.Contains (HotKeySpecifier) == true
- ? Math.Max (Rune.ColumnWidth (HotKeySpecifier), 0) : 0;
- } else {
- return TextFormatter.IsVerticalDirection (TextDirection) &&
- TextFormatter.Text?.Contains (HotKeySpecifier) == true
- ? Math.Max (Rune.ColumnWidth (HotKeySpecifier), 0) : 0;
- }
- }
- /// <summary>
- /// Gets the dimensions required for <see cref="Text"/> ignoring a <see cref="Terminal.Gui.TextFormatter.HotKeySpecifier"/>.
- /// </summary>
- /// <returns></returns>
- public Size GetSizeNeededForTextWithoutHotKey ()
- {
- return new Size (TextFormatter.Size.Width - GetHotKeySpecifierLength (),
- TextFormatter.Size.Height - GetHotKeySpecifierLength (false));
- }
- /// <summary>
- /// Gets the dimensions required for <see cref="Text"/> accounting for a <see cref="Terminal.Gui.TextFormatter.HotKeySpecifier"/> .
- /// </summary>
- /// <returns></returns>
- public Size GetSizeNeededForTextAndHotKey ()
- {
- if (ustring.IsNullOrEmpty (TextFormatter.Text)) {
- return Bounds.Size;
- }
- // BUGBUG: This IGNORES what Text is set to, using on only the current View size. This doesn't seem to make sense.
- // BUGBUG: This uses Frame; in v2 it should be Bounds
- return new Size (frame.Size.Width + GetHotKeySpecifierLength (),
- frame.Size.Height + GetHotKeySpecifierLength (false));
- }
- /// <inheritdoc/>
- public override bool OnMouseEnter (MouseEvent mouseEvent)
- {
- if (!Enabled) {
- return true;
- }
- if (!CanBeVisible (this)) {
- return false;
- }
- var args = new MouseEventEventArgs (mouseEvent);
- MouseEnter?.Invoke (this, args);
- return args.Handled || base.OnMouseEnter (mouseEvent);
- }
- /// <inheritdoc/>
- public override bool OnMouseLeave (MouseEvent mouseEvent)
- {
- if (!Enabled) {
- return true;
- }
- if (!CanBeVisible (this)) {
- return false;
- }
- var args = new MouseEventEventArgs (mouseEvent);
- MouseLeave?.Invoke (this, args);
- return args.Handled || base.OnMouseLeave (mouseEvent);
- }
- /// <summary>
- /// Method invoked when a mouse event is generated
- /// </summary>
- /// <param name="mouseEvent"></param>
- /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
- public virtual bool OnMouseEvent (MouseEvent mouseEvent)
- {
- if (!Enabled) {
- return true;
- }
- if (!CanBeVisible (this)) {
- return false;
- }
- var args = new MouseEventEventArgs (mouseEvent);
- if (OnMouseClick (args))
- return true;
- if (MouseEvent (mouseEvent))
- return true;
- if (mouseEvent.Flags == MouseFlags.Button1Clicked) {
- if (CanFocus && !HasFocus && SuperView != null) {
- SuperView.SetFocus (this);
- SetNeedsDisplay ();
- }
- return true;
- }
- return false;
- }
- /// <summary>
- /// Invokes the MouseClick event.
- /// </summary>
- protected bool OnMouseClick (MouseEventEventArgs args)
- {
- if (!Enabled) {
- return true;
- }
- MouseClick?.Invoke (this, args);
- return args.Handled;
- }
- /// <inheritdoc/>
- public override void OnCanFocusChanged () => CanFocusChanged?.Invoke (this, EventArgs.Empty);
- /// <inheritdoc/>
- public override void OnEnabledChanged () => EnabledChanged?.Invoke (this, EventArgs.Empty);
- /// <inheritdoc/>
- public override void OnVisibleChanged () => VisibleChanged?.Invoke (this, EventArgs.Empty);
- /// <inheritdoc/>
- protected override void Dispose (bool disposing)
- {
- Margin?.Dispose ();
- Margin = null;
- BorderFrame?.Dispose ();
- Border = null;
- Padding?.Dispose ();
- Padding = null;
- for (var i = InternalSubviews.Count - 1; i >= 0; i--) {
- var subview = InternalSubviews [i];
- Remove (subview);
- subview.Dispose ();
- }
- base.Dispose (disposing);
- }
- /// <summary>
- /// Signals the View that initialization is starting. See <see cref="ISupportInitialize"/>.
- /// </summary>
- /// <remarks>
- /// <para>
- /// Views can opt-in to more sophisticated initialization
- /// by implementing overrides to <see cref="ISupportInitialize.BeginInit"/> and
- /// <see cref="ISupportInitialize.EndInit"/> which will be called
- /// when the view is added to a <see cref="SuperView"/>.
- /// </para>
- /// <para>
- /// If first-run-only initialization is preferred, overrides to <see cref="ISupportInitializeNotification"/>
- /// can be implemented too, in which case the <see cref="ISupportInitialize"/>
- /// methods will only be called if <see cref="ISupportInitializeNotification.IsInitialized"/>
- /// is <see langword="false"/>. This allows proper <see cref="View"/> inheritance hierarchies
- /// to override base class layout code optimally by doing so only on first run,
- /// instead of on every run.
- /// </para>
- /// </remarks>
- public virtual void BeginInit ()
- {
- if (!IsInitialized) {
- oldCanFocus = CanFocus;
- oldTabIndex = tabIndex;
- UpdateTextDirection (TextDirection);
- UpdateTextFormatterText ();
- // TODO: Figure out why ScrollView and other tests fail if this call is put here
- // instead of the constructor.
- OnResizeNeeded ();
- //InitializeFrames ();
- } else {
- //throw new InvalidOperationException ("The view is already initialized.");
- }
- if (subviews?.Count > 0) {
- foreach (var view in subviews) {
- if (!view.IsInitialized) {
- view.BeginInit ();
- }
- }
- }
- }
- /// <summary>
- /// Signals the View that initialization is ending. See <see cref="ISupportInitialize"/>.
- /// </summary>
- public void EndInit ()
- {
- IsInitialized = true;
- if (subviews != null) {
- foreach (var view in subviews) {
- if (!view.IsInitialized) {
- view.EndInit ();
- }
- }
- }
- Initialized?.Invoke (this, EventArgs.Empty);
- }
- bool CanBeVisible (View view)
- {
- if (!view.Visible) {
- return false;
- }
- for (var c = view.SuperView; c != null; c = c.SuperView) {
- if (!c.Visible) {
- return false;
- }
- }
- return true;
- }
- /// <summary>
- /// Determines if the View's <see cref="Width"/> can be set to a new value.
- /// </summary>
- /// <param name="desiredWidth"></param>
- /// <param name="resultWidth">Contains the width that would result if <see cref="Width"/> were set to <paramref name="desiredWidth"/>"/> </param>
- /// <returns><see langword="true"/> if the View's <see cref="Width"/> can be changed to the specified value. False otherwise.</returns>
- internal bool TrySetWidth (int desiredWidth, out int resultWidth)
- {
- var w = desiredWidth;
- bool canSetWidth;
- if (Width is Dim.DimCombine || Width is Dim.DimView || Width is Dim.DimFill) {
- // It's a Dim.DimCombine and so can't be assigned. Let it have it's width anchored.
- w = Width.Anchor (w);
- canSetWidth = !ForceValidatePosDim;
- } else if (Width is Dim.DimFactor factor) {
- // Tries to get the SuperView width otherwise the view width.
- var sw = SuperView != null ? SuperView.Frame.Width : w;
- if (factor.IsFromRemaining ()) {
- sw -= Frame.X;
- }
- w = Width.Anchor (sw);
- canSetWidth = !ForceValidatePosDim;
- } else {
- canSetWidth = true;
- }
- resultWidth = w;
- return canSetWidth;
- }
- /// <summary>
- /// Determines if the View's <see cref="Height"/> can be set to a new value.
- /// </summary>
- /// <param name="desiredHeight"></param>
- /// <param name="resultHeight">Contains the width that would result if <see cref="Height"/> were set to <paramref name="desiredHeight"/>"/> </param>
- /// <returns><see langword="true"/> if the View's <see cref="Height"/> can be changed to the specified value. False otherwise.</returns>
- internal bool TrySetHeight (int desiredHeight, out int resultHeight)
- {
- var h = desiredHeight;
- bool canSetHeight;
- switch (Height) {
- case Dim.DimCombine _:
- case Dim.DimView _:
- case Dim.DimFill _:
- // It's a Dim.DimCombine and so can't be assigned. Let it have it's height anchored.
- h = Height.Anchor (h);
- canSetHeight = !ForceValidatePosDim;
- break;
- case Dim.DimFactor factor:
- // Tries to get the SuperView height otherwise the view height.
- var sh = SuperView != null ? SuperView.Frame.Height : h;
- if (factor.IsFromRemaining ()) {
- sh -= Frame.Y;
- }
- h = Height.Anchor (sh);
- canSetHeight = !ForceValidatePosDim;
- break;
- default:
- canSetHeight = true;
- break;
- }
- resultHeight = h;
- return canSetHeight;
- }
- /// <summary>
- /// Determines the current <see cref="ColorScheme"/> based on the <see cref="Enabled"/> value.
- /// </summary>
- /// <returns><see cref="Terminal.Gui.ColorScheme.Normal"/> if <see cref="Enabled"/> is <see langword="true"/>
- /// or <see cref="Terminal.Gui.ColorScheme.Disabled"/> if <see cref="Enabled"/> is <see langword="false"/>.
- /// If it's overridden can return other values.</returns>
- public virtual Attribute GetNormalColor ()
- {
- return Enabled ? ColorScheme.Normal : ColorScheme.Disabled;
- }
- /// <summary>
- /// Determines the current <see cref="ColorScheme"/> based on the <see cref="Enabled"/> value.
- /// </summary>
- /// <returns><see cref="Terminal.Gui.ColorScheme.Focus"/> if <see cref="Enabled"/> is <see langword="true"/>
- /// or <see cref="Terminal.Gui.ColorScheme.Disabled"/> if <see cref="Enabled"/> is <see langword="false"/>.
- /// If it's overridden can return other values.</returns>
- public virtual Attribute GetFocusColor ()
- {
- return Enabled ? ColorScheme.Focus : ColorScheme.Disabled;
- }
- /// <summary>
- /// Determines the current <see cref="ColorScheme"/> based on the <see cref="Enabled"/> value.
- /// </summary>
- /// <returns><see cref="Terminal.Gui.ColorScheme.HotNormal"/> if <see cref="Enabled"/> is <see langword="true"/>
- /// or <see cref="Terminal.Gui.ColorScheme.Disabled"/> if <see cref="Enabled"/> is <see langword="false"/>.
- /// If it's overridden can return other values.</returns>
- public virtual Attribute GetHotNormalColor ()
- {
- return Enabled ? ColorScheme.HotNormal : ColorScheme.Disabled;
- }
- /// <summary>
- /// Get the top superview of a given <see cref="View"/>.
- /// </summary>
- /// <returns>The superview view.</returns>
- public View GetTopSuperView (View view = null, View superview = null)
- {
- View top = superview ?? Application.Top;
- for (var v = view?.SuperView ?? (this?.SuperView); v != null; v = v.SuperView) {
- top = v;
- if (top == superview) {
- break;
- }
- }
- return top;
- }
- }
- }
|