View.cs 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954
  1. //
  2. // Authors:
  3. // Miguel de Icaza ([email protected])
  4. //
  5. // Pending:
  6. // - Check for NeedDisplay on the hierarchy and repaint
  7. // - Layout support
  8. // - "Colors" type or "Attributes" type?
  9. // - What to surface as "BackgroundCOlor" when clearing a window, an attribute or colors?
  10. //
  11. // Optimziations
  12. // - Add rendering limitation to the exposed area
  13. using System;
  14. using System.Collections;
  15. using System.Collections.Generic;
  16. using System.ComponentModel;
  17. using System.Diagnostics;
  18. using System.Linq;
  19. using NStack;
  20. namespace Terminal.Gui {
  21. /// <summary>
  22. /// Determines the LayoutStyle for a view, if Absolute, during LayoutSubviews, the
  23. /// value from the Frame will be used, if the value is Computed, then the Frame
  24. /// will be updated from the X, Y Pos objects and the Width and Height Dim objects.
  25. /// </summary>
  26. public enum LayoutStyle {
  27. /// <summary>
  28. /// The position and size of the view are based on the Frame value.
  29. /// </summary>
  30. Absolute,
  31. /// <summary>
  32. /// The position and size of the view will be computed based on the
  33. /// X, Y, Width and Height properties and set on the Frame.
  34. /// </summary>
  35. Computed
  36. }
  37. /// <summary>
  38. /// 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.
  39. /// </summary>
  40. /// <remarks>
  41. /// <para>
  42. /// The View defines the base functionality for user interface elements in Terminal.Gui. Views
  43. /// can contain one or more subviews, can respond to user input and render themselves on the screen.
  44. /// </para>
  45. /// <para>
  46. /// Views supports two layout styles: Absolute or Computed. The choice as to which layout style is used by the View
  47. /// is determined when the View is initizlied. To create a View using Absolute layout, call a constructor that takes a
  48. /// Rect parameter to specify the absolute position and size (the <c>View.<see cref="Frame "/></c>)/. To create a View
  49. /// using Computed layout use a constructor that does not take a Rect parametr and set the X, Y, Width and Height
  50. /// properties on the view. Both approaches use coordinates that are relative to the container they are being added to.
  51. /// </para>
  52. /// <para>
  53. /// To switch between Absolute and Computed layout, use the <see cref="LayoutStyle"/> property.
  54. /// </para>
  55. /// <para>
  56. /// Computed layout is more flexible and supports dynamic console apps where controls adjust layout
  57. /// as the terminal resizes or other Views change size or position. The X, Y, Width and Height
  58. /// properties are Dim and Pos objects that dynamically update the position of a view.
  59. /// The X and Y properties are of type <see cref="Pos"/>
  60. /// and you can use either absolute positions, percentages or anchor
  61. /// points. The Width and Height properties are of type
  62. /// <see cref="Dim"/> and can use absolute position,
  63. /// percentages and anchors. These are useful as they will take
  64. /// care of repositioning views when view's frames are resized or
  65. /// if the terminal size changes.
  66. /// </para>
  67. /// <para>
  68. /// Absolute layout requires specifying coordiantes and sizes of Views explicitly, and the
  69. /// View will typcialy stay in a fixed position and size. To change the position and size use the
  70. /// <see cref="Frame"/> property.
  71. /// </para>
  72. /// <para>
  73. /// Subviews (child views) can be added to a View by calling the <see cref="Add(View)"/> method.
  74. /// The container of a View can be accessed with the <see cref="SuperView"/> property.
  75. /// </para>
  76. /// <para>
  77. /// To flag a region of the View's <see cref="Bounds"/> to be redrawn call <see cref="SetNeedsDisplay(Rect)"/>. To flag the entire view
  78. /// for redraw call <see cref="SetNeedsDisplay()"/>.
  79. /// </para>
  80. /// <para>
  81. /// Views have a <see cref="ColorScheme"/> property that defines the default colors that subviews
  82. /// should use for rendering. This ensures that the views fit in the context where
  83. /// they are being used, and allows for themes to be plugged in. For example, the
  84. /// default colors for windows and toplevels uses a blue background, while it uses
  85. /// a white background for dialog boxes and a red background for errors.
  86. /// </para>
  87. /// <para>
  88. /// Subclasses should not rely on <see cref="ColorScheme"/> being
  89. /// set at construction time. If a <see cref="ColorScheme"/> is not set on a view, the view will inherit the
  90. /// value from its <see cref="SuperView"/> and the value might only be valid once a view has been
  91. /// added to a SuperView.
  92. /// </para>
  93. /// <para>
  94. /// By using <see cref="ColorScheme"/> applications will work both
  95. /// in color as well as black and white displays.
  96. /// </para>
  97. /// <para>
  98. /// Views that are focusable should implement the <see cref="PositionCursor"/> to make sure that
  99. /// the cursor is placed in a location that makes sense. Unix terminals do not have
  100. /// a way of hiding the cursor, so it can be distracting to have the cursor left at
  101. /// the last focused view. So views should make sure that they place the cursor
  102. /// in a visually sensible place.
  103. /// </para>
  104. /// <para>
  105. /// The <see cref="LayoutSubviews"/> method is invoked when the size or layout of a view has
  106. /// changed. The default processing system will keep the size and dimensions
  107. /// for views that use the <see cref="LayoutStyle.Absolute"/>, and will recompute the
  108. /// frames for the vies that use <see cref="LayoutStyle.Computed"/>.
  109. /// </para>
  110. /// </remarks>
  111. public partial class View : Responder, IEnumerable, ISupportInitializeNotification {
  112. internal enum Direction {
  113. Forward,
  114. Backward
  115. }
  116. // container == SuperView
  117. View container = null;
  118. View focused = null;
  119. Direction focusDirection;
  120. TextFormatter textFormatter;
  121. /// <summary>
  122. /// Event fired when a subview is being added to this view.
  123. /// </summary>
  124. public Action<View> Added;
  125. /// <summary>
  126. /// Event fired when a subview is being removed from this view.
  127. /// </summary>
  128. public Action<View> Removed;
  129. /// <summary>
  130. /// Event fired when the view gets focus.
  131. /// </summary>
  132. public Action<FocusEventArgs> Enter;
  133. /// <summary>
  134. /// Event fired when the view looses focus.
  135. /// </summary>
  136. public Action<FocusEventArgs> Leave;
  137. /// <summary>
  138. /// Event fired when the view receives the mouse event for the first time.
  139. /// </summary>
  140. public Action<MouseEventArgs> MouseEnter;
  141. /// <summary>
  142. /// Event fired when the view receives a mouse event for the last time.
  143. /// </summary>
  144. public Action<MouseEventArgs> MouseLeave;
  145. /// <summary>
  146. /// Event fired when a mouse event is generated.
  147. /// </summary>
  148. public Action<MouseEventArgs> MouseClick;
  149. /// <summary>
  150. /// 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.
  151. /// </summary>
  152. public Key HotKey { get => textFormatter.HotKey; set => textFormatter.HotKey = value; }
  153. /// <summary>
  154. /// 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'.
  155. /// </summary>
  156. public Rune HotKeySpecifier { get => textFormatter.HotKeySpecifier; set => textFormatter.HotKeySpecifier = value; }
  157. internal Direction FocusDirection {
  158. get => SuperView?.FocusDirection ?? focusDirection;
  159. set {
  160. if (SuperView != null)
  161. SuperView.FocusDirection = value;
  162. else
  163. focusDirection = value;
  164. }
  165. }
  166. /// <summary>
  167. /// Points to the current driver in use by the view, it is a convenience property
  168. /// for simplifying the development of new views.
  169. /// </summary>
  170. public static ConsoleDriver Driver { get { return Application.Driver; } }
  171. static IList<View> empty = new List<View> (0).AsReadOnly ();
  172. // This is null, and allocated on demand.
  173. List<View> subviews;
  174. /// <summary>
  175. /// This returns a list of the subviews contained by this view.
  176. /// </summary>
  177. /// <value>The subviews.</value>
  178. public IList<View> Subviews => subviews == null ? empty : subviews.AsReadOnly ();
  179. // Internally, we use InternalSubviews rather than subviews, as we do not expect us
  180. // to make the same mistakes our users make when they poke at the Subviews.
  181. internal IList<View> InternalSubviews => subviews ?? empty;
  182. // This is null, and allocated on demand.
  183. List<View> tabIndexes;
  184. /// <summary>
  185. /// This returns a tab index list of the subviews contained by this view.
  186. /// </summary>
  187. /// <value>The tabIndexes.</value>
  188. public IList<View> TabIndexes => tabIndexes == null ? empty : tabIndexes.AsReadOnly ();
  189. int tabIndex = -1;
  190. /// <summary>
  191. /// Indicates the index of the current <see cref="View"/> from the <see cref="TabIndexes"/> list.
  192. /// </summary>
  193. public int TabIndex {
  194. get { return tabIndex; }
  195. set {
  196. if (!CanFocus) {
  197. tabIndex = -1;
  198. return;
  199. } else if (SuperView?.tabIndexes == null || SuperView?.tabIndexes.Count == 1) {
  200. tabIndex = 0;
  201. return;
  202. } else if (tabIndex == value) {
  203. return;
  204. }
  205. tabIndex = value > SuperView.tabIndexes.Count - 1 ? SuperView.tabIndexes.Count - 1 : value < 0 ? 0 : value;
  206. tabIndex = GetTabIndex (tabIndex);
  207. if (SuperView.tabIndexes.IndexOf (this) != tabIndex) {
  208. SuperView.tabIndexes.Remove (this);
  209. SuperView.tabIndexes.Insert (tabIndex, this);
  210. SetTabIndex ();
  211. }
  212. }
  213. }
  214. private int GetTabIndex (int idx)
  215. {
  216. int i = 0;
  217. foreach (var v in SuperView.tabIndexes) {
  218. if (v.tabIndex == -1 || v == this) {
  219. continue;
  220. }
  221. i++;
  222. }
  223. return Math.Min (i, idx);
  224. }
  225. private void SetTabIndex ()
  226. {
  227. int i = 0;
  228. foreach (var v in SuperView.tabIndexes) {
  229. if (v.tabIndex == -1) {
  230. continue;
  231. }
  232. v.tabIndex = i;
  233. i++;
  234. }
  235. }
  236. bool tabStop = true;
  237. /// <summary>
  238. /// This only be <c>true</c> if the <see cref="CanFocus"/> is also <c>true</c> and the focus can be avoided by setting this to <c>false</c>
  239. /// </summary>
  240. public bool TabStop {
  241. get { return tabStop; }
  242. set {
  243. if (tabStop == value) {
  244. return;
  245. }
  246. tabStop = CanFocus && value;
  247. }
  248. }
  249. bool oldCanFocus;
  250. int oldTabIndex;
  251. /// <inheritdoc/>
  252. public override bool CanFocus {
  253. get => base.CanFocus;
  254. set {
  255. if (base.CanFocus != value) {
  256. base.CanFocus = value;
  257. if (!value && tabIndex > -1) {
  258. TabIndex = -1;
  259. }
  260. if (value && SuperView != null && !SuperView.CanFocus) {
  261. SuperView.CanFocus = value;
  262. }
  263. if (value && tabIndex == -1) {
  264. TabIndex = SuperView != null ? SuperView.tabIndexes.IndexOf (this) : -1;
  265. }
  266. TabStop = value;
  267. }
  268. if (subviews != null && IsInitialized) {
  269. foreach (var view in subviews) {
  270. if (view.CanFocus != value) {
  271. if (!value) {
  272. view.oldCanFocus = view.CanFocus;
  273. view.oldTabIndex = view.tabIndex;
  274. view.CanFocus = value;
  275. view.tabIndex = -1;
  276. } else {
  277. view.CanFocus = view.oldCanFocus;
  278. view.tabIndex = view.oldTabIndex;
  279. }
  280. }
  281. }
  282. }
  283. }
  284. }
  285. internal Rect NeedDisplay { get; private set; } = Rect.Empty;
  286. // The frame for the object. Superview relative.
  287. Rect frame;
  288. /// <summary>
  289. /// Gets or sets an identifier for the view;
  290. /// </summary>
  291. /// <value>The identifier.</value>
  292. /// <remarks>The id should be unique across all Views that share a SuperView.</remarks>
  293. public ustring Id { get; set; } = "";
  294. /// <summary>
  295. /// Returns a value indicating if this View is currently on Top (Active)
  296. /// </summary>
  297. public bool IsCurrentTop {
  298. get {
  299. return Application.Current == this;
  300. }
  301. }
  302. /// <summary>
  303. /// Gets or sets a value indicating whether this <see cref="View"/> wants mouse position reports.
  304. /// </summary>
  305. /// <value><c>true</c> if want mouse position reports; otherwise, <c>false</c>.</value>
  306. public virtual bool WantMousePositionReports { get; set; } = false;
  307. /// <summary>
  308. /// Gets or sets a value indicating whether this <see cref="View"/> want continuous button pressed event.
  309. /// </summary>
  310. public virtual bool WantContinuousButtonPressed { get; set; } = false;
  311. /// <summary>
  312. /// Gets or sets the frame for the view. The frame is relative to the view's container (<see cref="SuperView"/>).
  313. /// </summary>
  314. /// <value>The frame.</value>
  315. /// <remarks>
  316. /// <para>
  317. /// Change the Frame when using the <see cref="LayoutStyle.Absolute"/> layout style to move or resize views.
  318. /// </para>
  319. /// <para>
  320. /// Altering the Frame of a view will trigger the redrawing of the
  321. /// view as well as the redrawing of the affected regions of the <see cref="SuperView"/>.
  322. /// </para>
  323. /// </remarks>
  324. public virtual Rect Frame {
  325. get => frame;
  326. set {
  327. if (SuperView != null) {
  328. SuperView.SetNeedsDisplay (frame);
  329. SuperView.SetNeedsDisplay (value);
  330. }
  331. frame = value;
  332. SetNeedsLayout ();
  333. SetNeedsDisplay (frame);
  334. }
  335. }
  336. /// <summary>
  337. /// Gets an enumerator that enumerates the subviews in this view.
  338. /// </summary>
  339. /// <returns>The enumerator.</returns>
  340. public IEnumerator GetEnumerator ()
  341. {
  342. foreach (var v in InternalSubviews)
  343. yield return v;
  344. }
  345. LayoutStyle layoutStyle;
  346. /// <summary>
  347. /// Controls how the View's <see cref="Frame"/> is computed during the LayoutSubviews method, if the style is set to <see cref="LayoutStyle.Absolute"/>,
  348. /// LayoutSubviews does not change the <see cref="Frame"/>. If the style is <see cref="LayoutStyle.Computed"/> the <see cref="Frame"/> is updated using
  349. /// the <see cref="X"/>, <see cref="Y"/>, <see cref="Width"/>, and <see cref="Height"/> properties.
  350. /// </summary>
  351. /// <value>The layout style.</value>
  352. public LayoutStyle LayoutStyle {
  353. get => layoutStyle;
  354. set {
  355. layoutStyle = value;
  356. SetNeedsLayout ();
  357. }
  358. }
  359. /// <summary>
  360. /// The bounds represent the View-relative rectangle used for this view; the area inside of the view.
  361. /// </summary>
  362. /// <value>The bounds.</value>
  363. /// <remarks>
  364. /// <para>
  365. /// Updates to the Bounds update the <see cref="Frame"/>,
  366. /// and has the same side effects as updating the <see cref="Frame"/>.
  367. /// </para>
  368. /// <para>
  369. /// Because <see cref="Bounds"/> coordinates are relative to the upper-left corner of the <see cref="View"/>,
  370. /// the coordinates of the upper-left corner of the rectangle returned by this property are (0,0).
  371. /// Use this property to obtain the size and coordinates of the client area of the
  372. /// control for tasks such as drawing on the surface of the control.
  373. /// </para>
  374. /// </remarks>
  375. public Rect Bounds {
  376. get => new Rect (Point.Empty, Frame.Size);
  377. set {
  378. Frame = new Rect (frame.Location, value.Size);
  379. }
  380. }
  381. Pos x, y;
  382. /// <summary>
  383. /// Gets or sets the X position for the view (the column). Only used whe <see cref="LayoutStyle"/> is <see cref="LayoutStyle.Computed"/>.
  384. /// </summary>
  385. /// <value>The X Position.</value>
  386. /// <remarks>
  387. /// If <see cref="LayoutStyle"/> is <see cref="LayoutStyle.Absolute"/> changing this property has no effect and its value is indeterminate.
  388. /// </remarks>
  389. public Pos X {
  390. get => x;
  391. set {
  392. if (!ValidatePosDim (x, value)) {
  393. throw new ArgumentException ();
  394. }
  395. x = value;
  396. SetNeedsLayout ();
  397. SetNeedsDisplay (frame);
  398. }
  399. }
  400. /// <summary>
  401. /// Gets or sets the Y position for the view (the row). Only used whe <see cref="LayoutStyle"/> is <see cref="LayoutStyle.Computed"/>.
  402. /// </summary>
  403. /// <value>The y position (line).</value>
  404. /// <remarks>
  405. /// If <see cref="LayoutStyle"/> is <see cref="LayoutStyle.Absolute"/> changing this property has no effect and its value is indeterminate.
  406. /// </remarks>
  407. public Pos Y {
  408. get => y;
  409. set {
  410. if (!ValidatePosDim (y, value)) {
  411. throw new ArgumentException ();
  412. }
  413. y = value;
  414. SetNeedsLayout ();
  415. SetNeedsDisplay (frame);
  416. }
  417. }
  418. Dim width, height;
  419. /// <summary>
  420. /// Gets or sets the width of the view. Only used whe <see cref="LayoutStyle"/> is <see cref="LayoutStyle.Computed"/>.
  421. /// </summary>
  422. /// <value>The width.</value>
  423. /// <remarks>
  424. /// If <see cref="LayoutStyle"/> is <see cref="LayoutStyle.Absolute"/> changing this property has no effect and its value is indeterminate.
  425. /// </remarks>
  426. public Dim Width {
  427. get => width;
  428. set {
  429. if (!ValidatePosDim (width, value)) {
  430. throw new ArgumentException ();
  431. }
  432. width = value;
  433. SetNeedsLayout ();
  434. SetNeedsDisplay (frame);
  435. }
  436. }
  437. /// <summary>
  438. /// Gets or sets the height of the view. Only used whe <see cref="LayoutStyle"/> is <see cref="LayoutStyle.Computed"/>.
  439. /// </summary>
  440. /// <value>The height.</value>
  441. /// If <see cref="LayoutStyle"/> is <see cref="LayoutStyle.Absolute"/> changing this property has no effect and its value is indeterminate.
  442. public Dim Height {
  443. get => height;
  444. set {
  445. if (!ValidatePosDim (height, value)) {
  446. throw new ArgumentException ();
  447. }
  448. height = value;
  449. SetNeedsLayout ();
  450. SetNeedsDisplay (frame);
  451. }
  452. }
  453. bool ValidatePosDim (object oldvalue, object newValue)
  454. {
  455. if (!IsInitialized || layoutStyle == LayoutStyle.Absolute || oldvalue == null || oldvalue.GetType () == newValue.GetType () || this is Toplevel) {
  456. return true;
  457. }
  458. if (layoutStyle == LayoutStyle.Computed) {
  459. if (oldvalue.GetType () != newValue.GetType () && !(newValue is Pos.PosAbsolute || newValue is Dim.DimAbsolute)) {
  460. return true;
  461. }
  462. }
  463. return false;
  464. }
  465. /// <summary>
  466. /// Returns the container for this view, or null if this view has not been added to a container.
  467. /// </summary>
  468. /// <value>The super view.</value>
  469. public View SuperView => container;
  470. /// <summary>
  471. /// Initializes a new instance of a <see cref="LayoutStyle.Absolute"/> <see cref="View"/> class with the absolute
  472. /// dimensions specified in the <c>frame</c> parameter.
  473. /// </summary>
  474. /// <param name="frame">The region covered by this view.</param>
  475. /// <remarks>
  476. /// This constructor intitalize a View with a <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Absolute"/>. Use <see cref="View()"/> to
  477. /// initialize a View with <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Computed"/>
  478. /// </remarks>
  479. public View (Rect frame)
  480. {
  481. textFormatter = new TextFormatter ();
  482. this.Text = ustring.Empty;
  483. this.Frame = frame;
  484. LayoutStyle = LayoutStyle.Absolute;
  485. }
  486. /// <summary>
  487. /// Initializes a new instance of <see cref="View"/> using <see cref="LayoutStyle.Computed"/> layout.
  488. /// </summary>
  489. /// <remarks>
  490. /// <para>
  491. /// 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.
  492. /// The <see cref="Label"/> will be created using <see cref="LayoutStyle.Computed"/>
  493. /// coordinates. The initial size (<see cref="View.Frame"/> will be
  494. /// adjusted to fit the contents of <see cref="Text"/>, including newlines ('\n') for multiple lines.
  495. /// </para>
  496. /// <para>
  497. /// If <c>Height</c> is greater than one, word wrapping is provided.
  498. /// </para>
  499. /// <para>
  500. /// This constructor intitalize a View with a <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Computed"/>.
  501. /// 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.
  502. /// </para>
  503. /// </remarks>
  504. public View () : this (text: string.Empty) { }
  505. /// <summary>
  506. /// Initializes a new instance of <see cref="View"/> using <see cref="LayoutStyle.Absolute"/> layout.
  507. /// </summary>
  508. /// <remarks>
  509. /// <para>
  510. /// The <see cref="View"/> will be created at the given
  511. /// coordinates with the given string. The size (<see cref="View.Frame"/> will be
  512. /// adjusted to fit the contents of <see cref="Text"/>, including newlines ('\n') for multiple lines.
  513. /// </para>
  514. /// <para>
  515. /// No line wrapping is provided.
  516. /// </para>
  517. /// </remarks>
  518. /// <param name="x">column to locate the Label.</param>
  519. /// <param name="y">row to locate the Label.</param>
  520. /// <param name="text">text to initialize the <see cref="Text"/> property with.</param>
  521. public View (int x, int y, ustring text) : this (TextFormatter.CalcRect (x, y, text), text) { }
  522. /// <summary>
  523. /// Initializes a new instance of <see cref="View"/> using <see cref="LayoutStyle.Absolute"/> layout.
  524. /// </summary>
  525. /// <remarks>
  526. /// <para>
  527. /// The <see cref="View"/> will be created at the given
  528. /// coordinates with the given string. The initial size (<see cref="View.Frame"/> will be
  529. /// adjusted to fit the contents of <see cref="Text"/>, including newlines ('\n') for multiple lines.
  530. /// </para>
  531. /// <para>
  532. /// If <c>rect.Height</c> is greater than one, word wrapping is provided.
  533. /// </para>
  534. /// </remarks>
  535. /// <param name="rect">Location.</param>
  536. /// <param name="text">text to initialize the <see cref="Text"/> property with.</param>
  537. public View (Rect rect, ustring text) : this (rect)
  538. {
  539. textFormatter = new TextFormatter ();
  540. this.Text = text;
  541. }
  542. /// <summary>
  543. /// Initializes a new instance of <see cref="View"/> using <see cref="LayoutStyle.Computed"/> layout.
  544. /// </summary>
  545. /// <remarks>
  546. /// <para>
  547. /// The <see cref="View"/> will be created using <see cref="LayoutStyle.Computed"/>
  548. /// coordinates with the given string. The initial size (<see cref="View.Frame"/> will be
  549. /// adjusted to fit the contents of <see cref="Text"/>, including newlines ('\n') for multiple lines.
  550. /// </para>
  551. /// <para>
  552. /// If <c>Height</c> is greater than one, word wrapping is provided.
  553. /// </para>
  554. /// </remarks>
  555. /// <param name="text">text to initialize the <see cref="Text"/> property with.</param>
  556. public View (ustring text) : base ()
  557. {
  558. textFormatter = new TextFormatter ();
  559. this.Text = text;
  560. CanFocus = false;
  561. TabIndex = -1;
  562. TabStop = false;
  563. LayoutStyle = LayoutStyle.Computed;
  564. // BUGBUG: CalcRect doesn't account for line wrapping
  565. var r = TextFormatter.CalcRect (0, 0, text);
  566. x = Pos.At (0);
  567. y = Pos.At (0);
  568. Width = r.Width;
  569. Height = r.Height;
  570. }
  571. /// <summary>
  572. /// Sets a flag indicating this view needs to be redisplayed because its state has changed.
  573. /// </summary>
  574. public void SetNeedsDisplay ()
  575. {
  576. SetNeedsDisplay (Bounds);
  577. }
  578. internal bool layoutNeeded = true;
  579. internal void SetNeedsLayout ()
  580. {
  581. if (layoutNeeded)
  582. return;
  583. layoutNeeded = true;
  584. if (SuperView == null)
  585. return;
  586. SuperView.SetNeedsLayout ();
  587. textFormatter.NeedsFormat = true;
  588. }
  589. /// <summary>
  590. /// Flags the view-relative region on this View as needing to be repainted.
  591. /// </summary>
  592. /// <param name="region">The view-relative region that must be flagged for repaint.</param>
  593. public void SetNeedsDisplay (Rect region)
  594. {
  595. if (NeedDisplay == null || NeedDisplay.IsEmpty)
  596. NeedDisplay = region;
  597. else {
  598. var x = Math.Min (NeedDisplay.X, region.X);
  599. var y = Math.Min (NeedDisplay.Y, region.Y);
  600. var w = Math.Max (NeedDisplay.Width, region.Width);
  601. var h = Math.Max (NeedDisplay.Height, region.Height);
  602. NeedDisplay = new Rect (x, y, w, h);
  603. }
  604. if (container != null)
  605. container.ChildNeedsDisplay ();
  606. if (subviews == null)
  607. return;
  608. foreach (var view in subviews)
  609. if (view.Frame.IntersectsWith (region)) {
  610. var childRegion = Rect.Intersect (view.Frame, region);
  611. childRegion.X -= view.Frame.X;
  612. childRegion.Y -= view.Frame.Y;
  613. view.SetNeedsDisplay (childRegion);
  614. }
  615. }
  616. internal bool childNeedsDisplay;
  617. /// <summary>
  618. /// Indicates that any child views (in the <see cref="Subviews"/> list) need to be repainted.
  619. /// </summary>
  620. public void ChildNeedsDisplay ()
  621. {
  622. childNeedsDisplay = true;
  623. if (container != null)
  624. container.ChildNeedsDisplay ();
  625. }
  626. /// <summary>
  627. /// Adds a subview (child) to this view.
  628. /// </summary>
  629. /// <remarks>
  630. /// 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"/>
  631. /// </remarks>
  632. public virtual void Add (View view)
  633. {
  634. if (view == null)
  635. return;
  636. if (subviews == null) {
  637. subviews = new List<View> ();
  638. }
  639. if (tabIndexes == null) {
  640. tabIndexes = new List<View> ();
  641. }
  642. subviews.Add (view);
  643. tabIndexes.Add (view);
  644. view.container = this;
  645. if (view.CanFocus) {
  646. CanFocus = true;
  647. view.tabIndex = tabIndexes.IndexOf (view);
  648. }
  649. SetNeedsLayout ();
  650. SetNeedsDisplay ();
  651. OnAdded (view);
  652. if (IsInitialized) {
  653. view.BeginInit ();
  654. }
  655. }
  656. /// <summary>
  657. /// Adds the specified views (children) to the view.
  658. /// </summary>
  659. /// <param name="views">Array of one or more views (can be optional parameter).</param>
  660. /// <remarks>
  661. /// 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"/>
  662. /// </remarks>
  663. public void Add (params View [] views)
  664. {
  665. if (views == null)
  666. return;
  667. foreach (var view in views)
  668. Add (view);
  669. }
  670. /// <summary>
  671. /// Removes all subviews (children) added via <see cref="Add(View)"/> or <see cref="Add(View[])"/> from this View.
  672. /// </summary>
  673. public virtual void RemoveAll ()
  674. {
  675. if (subviews == null)
  676. return;
  677. while (subviews.Count > 0) {
  678. Remove (subviews [0]);
  679. }
  680. }
  681. /// <summary>
  682. /// Removes a subview added via <see cref="Add(View)"/> or <see cref="Add(View[])"/> from this View.
  683. /// </summary>
  684. /// <remarks>
  685. /// </remarks>
  686. public virtual void Remove (View view)
  687. {
  688. if (view == null || subviews == null)
  689. return;
  690. SetNeedsLayout ();
  691. SetNeedsDisplay ();
  692. var touched = view.Frame;
  693. subviews.Remove (view);
  694. tabIndexes.Remove (view);
  695. view.container = null;
  696. view.tabIndex = -1;
  697. if (subviews.Count < 1) {
  698. CanFocus = false;
  699. }
  700. foreach (var v in subviews) {
  701. if (v.Frame.IntersectsWith (touched))
  702. view.SetNeedsDisplay ();
  703. }
  704. OnRemoved (view);
  705. }
  706. void PerformActionForSubview (View subview, Action<View> action)
  707. {
  708. if (subviews.Contains (subview)) {
  709. action (subview);
  710. }
  711. SetNeedsDisplay ();
  712. subview.SetNeedsDisplay ();
  713. }
  714. /// <summary>
  715. /// Brings the specified subview to the front so it is drawn on top of any other views.
  716. /// </summary>
  717. /// <param name="subview">The subview to send to the front</param>
  718. /// <remarks>
  719. /// <seealso cref="SendSubviewToBack"/>.
  720. /// </remarks>
  721. public void BringSubviewToFront (View subview)
  722. {
  723. PerformActionForSubview (subview, x => {
  724. subviews.Remove (x);
  725. subviews.Add (x);
  726. });
  727. }
  728. /// <summary>
  729. /// Sends the specified subview to the front so it is the first view drawn
  730. /// </summary>
  731. /// <param name="subview">The subview to send to the front</param>
  732. /// <remarks>
  733. /// <seealso cref="BringSubviewToFront(View)"/>.
  734. /// </remarks>
  735. public void SendSubviewToBack (View subview)
  736. {
  737. PerformActionForSubview (subview, x => {
  738. subviews.Remove (x);
  739. subviews.Insert (0, subview);
  740. });
  741. }
  742. /// <summary>
  743. /// Moves the subview backwards in the hierarchy, only one step
  744. /// </summary>
  745. /// <param name="subview">The subview to send backwards</param>
  746. /// <remarks>
  747. /// If you want to send the view all the way to the back use SendSubviewToBack.
  748. /// </remarks>
  749. public void SendSubviewBackwards (View subview)
  750. {
  751. PerformActionForSubview (subview, x => {
  752. var idx = subviews.IndexOf (x);
  753. if (idx > 0) {
  754. subviews.Remove (x);
  755. subviews.Insert (idx - 1, x);
  756. }
  757. });
  758. }
  759. /// <summary>
  760. /// Moves the subview backwards in the hierarchy, only one step
  761. /// </summary>
  762. /// <param name="subview">The subview to send backwards</param>
  763. /// <remarks>
  764. /// If you want to send the view all the way to the back use SendSubviewToBack.
  765. /// </remarks>
  766. public void BringSubviewForward (View subview)
  767. {
  768. PerformActionForSubview (subview, x => {
  769. var idx = subviews.IndexOf (x);
  770. if (idx + 1 < subviews.Count) {
  771. subviews.Remove (x);
  772. subviews.Insert (idx + 1, x);
  773. }
  774. });
  775. }
  776. /// <summary>
  777. /// Clears the view region with the current color.
  778. /// </summary>
  779. /// <remarks>
  780. /// <para>
  781. /// This clears the entire region used by this view.
  782. /// </para>
  783. /// </remarks>
  784. public void Clear ()
  785. {
  786. var h = Frame.Height;
  787. var w = Frame.Width;
  788. for (int line = 0; line < h; line++) {
  789. Move (0, line);
  790. for (int col = 0; col < w; col++)
  791. Driver.AddRune (' ');
  792. }
  793. }
  794. /// <summary>
  795. /// Clears the specified region with the current color.
  796. /// </summary>
  797. /// <remarks>
  798. /// </remarks>
  799. /// <param name="regionScreen">The screen-relative region to clear.</param>
  800. public void Clear (Rect regionScreen)
  801. {
  802. var h = regionScreen.Height;
  803. var w = regionScreen.Width;
  804. for (int line = regionScreen.Y; line < regionScreen.Y + h; line++) {
  805. Driver.Move (regionScreen.X, line);
  806. for (int col = 0; col < w; col++)
  807. Driver.AddRune (' ');
  808. }
  809. }
  810. /// <summary>
  811. /// Converts a view-relative (col,row) position to a screen-relative position (col,row). The values are optionally clamped to the screen dimensions.
  812. /// </summary>
  813. /// <param name="col">View-relative column.</param>
  814. /// <param name="row">View-relative row.</param>
  815. /// <param name="rcol">Absolute column; screen-relative.</param>
  816. /// <param name="rrow">Absolute row; screen-relative.</param>
  817. /// <param name="clipped">Whether to clip the result of the ViewToScreen method, if set to <c>true</c>, the rcol, rrow values are clamped to the screen (terminal) dimensions (0..TerminalDim-1).</param>
  818. internal void ViewToScreen (int col, int row, out int rcol, out int rrow, bool clipped = true)
  819. {
  820. // Computes the real row, col relative to the screen.
  821. rrow = row + frame.Y;
  822. rcol = col + frame.X;
  823. var ccontainer = container;
  824. while (ccontainer != null) {
  825. rrow += ccontainer.frame.Y;
  826. rcol += ccontainer.frame.X;
  827. ccontainer = ccontainer.container;
  828. }
  829. // The following ensures that the cursor is always in the screen boundaries.
  830. if (clipped) {
  831. rrow = Math.Min (rrow, Driver.Rows - 1);
  832. rcol = Math.Min (rcol, Driver.Cols - 1);
  833. }
  834. }
  835. /// <summary>
  836. /// Converts a point from screen-relative coordinates to view-relative coordinates.
  837. /// </summary>
  838. /// <returns>The mapped point.</returns>
  839. /// <param name="x">X screen-coordinate point.</param>
  840. /// <param name="y">Y screen-coordinate point.</param>
  841. public Point ScreenToView (int x, int y)
  842. {
  843. if (SuperView == null) {
  844. return new Point (x - Frame.X, y - frame.Y);
  845. } else {
  846. var parent = SuperView.ScreenToView (x, y);
  847. return new Point (parent.X - frame.X, parent.Y - frame.Y);
  848. }
  849. }
  850. /// <summary>
  851. /// Converts a region in view-relative coordinates to screen-relative coordinates.
  852. /// </summary>
  853. internal Rect ViewToScreen (Rect region)
  854. {
  855. ViewToScreen (region.X, region.Y, out var x, out var y, clipped: false);
  856. return new Rect (x, y, region.Width, region.Height);
  857. }
  858. // Clips a rectangle in screen coordinates to the dimensions currently available on the screen
  859. internal Rect ScreenClip (Rect regionScreen)
  860. {
  861. var x = regionScreen.X < 0 ? 0 : regionScreen.X;
  862. var y = regionScreen.Y < 0 ? 0 : regionScreen.Y;
  863. var w = regionScreen.X + regionScreen.Width >= Driver.Cols ? Driver.Cols - regionScreen.X : regionScreen.Width;
  864. var h = regionScreen.Y + regionScreen.Height >= Driver.Rows ? Driver.Rows - regionScreen.Y : regionScreen.Height;
  865. return new Rect (x, y, w, h);
  866. }
  867. /// <summary>
  868. /// Sets the <see cref="ConsoleDriver"/>'s clip region to the current View's <see cref="Bounds"/>.
  869. /// </summary>
  870. /// <returns>The existing driver's clip region, which can be then re-eapplied by setting <c><see cref="Driver"/>.Clip</c> (<see cref="ConsoleDriver.Clip"/>).</returns>
  871. /// <remarks>
  872. /// <see cref="Bounds"/> is View-relative.
  873. /// </remarks>
  874. public Rect ClipToBounds ()
  875. {
  876. return SetClip (Bounds);
  877. }
  878. /// <summary>
  879. /// Sets the clip region to the specified view-relative region.
  880. /// </summary>
  881. /// <returns>The previous screen-relative clip region.</returns>
  882. /// <param name="region">View-relative clip region.</param>
  883. public Rect SetClip (Rect region)
  884. {
  885. var previous = Driver.Clip;
  886. Driver.Clip = Rect.Intersect (previous, ViewToScreen (region));
  887. return previous;
  888. }
  889. /// <summary>
  890. /// Draws a frame in the current view, clipped by the boundary of this view
  891. /// </summary>
  892. /// <param name="region">View-relative region for the frame to be drawn.</param>
  893. /// <param name="padding">The padding to add around the outside of the drawn frame.</param>
  894. /// <param name="fill">If set to <c>true</c> it fill will the contents.</param>
  895. public void DrawFrame (Rect region, int padding = 0, bool fill = false)
  896. {
  897. var scrRect = ViewToScreen (region);
  898. var savedClip = ClipToBounds ();
  899. Driver.DrawWindowFrame (scrRect, padding + 1, padding + 1, padding + 1, padding + 1, border: true, fill: fill);
  900. Driver.Clip = savedClip;
  901. }
  902. /// <summary>
  903. /// Utility function to draw strings that contain a hotkey.
  904. /// </summary>
  905. /// <param name="text">String to display, the hotkey specifier before a letter flags the next letter as the hotkey.</param>
  906. /// <param name="hotColor">Hot color.</param>
  907. /// <param name="normalColor">Normal color.</param>
  908. /// <remarks>
  909. /// <para>The hotkey is any character following the hotkey specifier, which is the underscore ('_') character by default.</para>
  910. /// <para>The hotkey specifier can be changed via <see cref="HotKeySpecifier"/></para>
  911. /// </remarks>
  912. public void DrawHotString (ustring text, Attribute hotColor, Attribute normalColor)
  913. {
  914. var hotkeySpec = HotKeySpecifier == (Rune)0xffff ? (Rune)'_' : HotKeySpecifier;
  915. Application.Driver.SetAttribute (normalColor);
  916. foreach (var rune in text) {
  917. if (rune == hotkeySpec) {
  918. Application.Driver.SetAttribute (hotColor);
  919. continue;
  920. }
  921. Application.Driver.AddRune (rune);
  922. Application.Driver.SetAttribute (normalColor);
  923. }
  924. }
  925. /// <summary>
  926. /// Utility function to draw strings that contains a hotkey using a <see cref="ColorScheme"/> and the "focused" state.
  927. /// </summary>
  928. /// <param name="text">String to display, the underscoore before a letter flags the next letter as the hotkey.</param>
  929. /// <param name="focused">If set to <c>true</c> this uses the focused colors from the color scheme, otherwise the regular ones.</param>
  930. /// <param name="scheme">The color scheme to use.</param>
  931. public void DrawHotString (ustring text, bool focused, ColorScheme scheme)
  932. {
  933. if (focused)
  934. DrawHotString (text, scheme.HotFocus, scheme.Focus);
  935. else
  936. DrawHotString (text, scheme.HotNormal, scheme.Normal);
  937. }
  938. /// <summary>
  939. /// This moves the cursor to the specified column and row in the view.
  940. /// </summary>
  941. /// <returns>The move.</returns>
  942. /// <param name="col">Col.</param>
  943. /// <param name="row">Row.</param>
  944. public void Move (int col, int row)
  945. {
  946. ViewToScreen (col, row, out var rcol, out var rrow);
  947. Driver.Move (rcol, rrow);
  948. }
  949. /// <summary>
  950. /// Positions the cursor in the right position based on the currently focused view in the chain.
  951. /// </summary>
  952. /// Views that are focusable should override <see cref="PositionCursor"/> to ensure
  953. /// the cursor is placed in a location that makes sense. Unix terminals do not have
  954. /// a way of hiding the cursor, so it can be distracting to have the cursor left at
  955. /// the last focused view. Views should make sure that they place the cursor
  956. /// in a visually sensible place.
  957. public virtual void PositionCursor ()
  958. {
  959. if (focused != null)
  960. focused.PositionCursor ();
  961. else {
  962. if (CanFocus && HasFocus) {
  963. Move (textFormatter.HotKeyPos == -1 ? 0 : textFormatter.HotKeyPos, 0);
  964. } else {
  965. Move (frame.X, frame.Y);
  966. }
  967. }
  968. }
  969. bool hasFocus;
  970. /// <inheritdoc/>
  971. public override bool HasFocus {
  972. get {
  973. return hasFocus;
  974. }
  975. }
  976. void SetHasFocus (bool value, View view)
  977. {
  978. if (hasFocus != value) {
  979. hasFocus = value;
  980. }
  981. if (value) {
  982. OnEnter (view);
  983. } else {
  984. OnLeave (view);
  985. }
  986. SetNeedsDisplay ();
  987. // Remove focus down the chain of subviews if focus is removed
  988. if (!value && focused != null) {
  989. focused.OnLeave (view);
  990. focused.SetHasFocus (false, view);
  991. focused = null;
  992. }
  993. }
  994. /// <summary>
  995. /// Defines the event arguments for <see cref="SetFocus(View)"/>
  996. /// </summary>
  997. public class FocusEventArgs : EventArgs {
  998. /// <summary>
  999. /// Constructs.
  1000. /// </summary>
  1001. /// <param name="view">The view that gets or loses focus.</param>
  1002. public FocusEventArgs (View view) { View = view; }
  1003. /// <summary>
  1004. /// Indicates if the current focus event has already been processed and the driver should stop notifying any other event subscriber.
  1005. /// Its important to set this value to true specially when updating any View's layout from inside the subscriber method.
  1006. /// </summary>
  1007. public bool Handled { get; set; }
  1008. /// <summary>
  1009. /// Indicates the current view that gets or loses focus.
  1010. /// </summary>
  1011. public View View { get; set; }
  1012. }
  1013. /// <summary>
  1014. /// Method invoked when a subview is being added to this view.
  1015. /// </summary>
  1016. /// <param name="view">The subview being added.</param>
  1017. public virtual void OnAdded (View view)
  1018. {
  1019. view.Added?.Invoke (this);
  1020. }
  1021. /// <summary>
  1022. /// Method invoked when a subview is being removed from this view.
  1023. /// </summary>
  1024. /// <param name="view">The subview being removed.</param>
  1025. public virtual void OnRemoved (View view)
  1026. {
  1027. view.Removed?.Invoke (this);
  1028. }
  1029. /// <inheritdoc/>
  1030. public override bool OnEnter (View view)
  1031. {
  1032. FocusEventArgs args = new FocusEventArgs (view);
  1033. Enter?.Invoke (args);
  1034. if (args.Handled)
  1035. return true;
  1036. if (base.OnEnter (view))
  1037. return true;
  1038. return false;
  1039. }
  1040. /// <inheritdoc/>
  1041. public override bool OnLeave (View view)
  1042. {
  1043. FocusEventArgs args = new FocusEventArgs (view);
  1044. Leave?.Invoke (args);
  1045. if (args.Handled)
  1046. return true;
  1047. if (base.OnLeave (view))
  1048. return true;
  1049. return false;
  1050. }
  1051. /// <summary>
  1052. /// Returns the currently focused view inside this view, or null if nothing is focused.
  1053. /// </summary>
  1054. /// <value>The focused.</value>
  1055. public View Focused => focused;
  1056. /// <summary>
  1057. /// Returns the most focused view in the chain of subviews (the leaf view that has the focus).
  1058. /// </summary>
  1059. /// <value>The most focused.</value>
  1060. public View MostFocused {
  1061. get {
  1062. if (Focused == null)
  1063. return null;
  1064. var most = Focused.MostFocused;
  1065. if (most != null)
  1066. return most;
  1067. return Focused;
  1068. }
  1069. }
  1070. /// <summary>
  1071. /// The color scheme for this view, if it is not defined, it returns the <see cref="SuperView"/>'s
  1072. /// color scheme.
  1073. /// </summary>
  1074. public ColorScheme ColorScheme {
  1075. get {
  1076. if (colorScheme == null)
  1077. return SuperView?.ColorScheme;
  1078. return colorScheme;
  1079. }
  1080. set {
  1081. colorScheme = value;
  1082. }
  1083. }
  1084. ColorScheme colorScheme;
  1085. /// <summary>
  1086. /// Displays the specified character in the specified column and row of the View.
  1087. /// </summary>
  1088. /// <param name="col">Column (view-relative).</param>
  1089. /// <param name="row">Row (view-relative).</param>
  1090. /// <param name="ch">Ch.</param>
  1091. public void AddRune (int col, int row, Rune ch)
  1092. {
  1093. if (row < 0 || col < 0)
  1094. return;
  1095. if (row > frame.Height - 1 || col > frame.Width - 1)
  1096. return;
  1097. Move (col, row);
  1098. Driver.AddRune (ch);
  1099. }
  1100. /// <summary>
  1101. /// Removes the <see cref="SetNeedsDisplay()"/> and the <see cref="ChildNeedsDisplay"/> setting on this view.
  1102. /// </summary>
  1103. protected void ClearNeedsDisplay ()
  1104. {
  1105. NeedDisplay = Rect.Empty;
  1106. childNeedsDisplay = false;
  1107. }
  1108. /// <summary>
  1109. /// Redraws this view and its subviews; only redraws the views that have been flagged for a re-display.
  1110. /// </summary>
  1111. /// <param name="bounds">The bounds (view-relative region) to redraw.</param>
  1112. /// <remarks>
  1113. /// <para>
  1114. /// Always use <see cref="Bounds"/> (view-relative) when calling <see cref="Redraw(Rect)"/>, NOT <see cref="Frame"/> (superview-relative).
  1115. /// </para>
  1116. /// <para>
  1117. /// Views should set the color that they want to use on entry, as otherwise this will inherit
  1118. /// the last color that was set globaly on the driver.
  1119. /// </para>
  1120. /// <para>
  1121. /// Overrides of <see cref="Redraw"/> must ensure they do not set <c>Driver.Clip</c> to a clip region
  1122. /// larger than the <c>region</c> parameter.
  1123. /// </para>
  1124. /// </remarks>
  1125. public virtual void Redraw (Rect bounds)
  1126. {
  1127. var clipRect = new Rect (Point.Empty, frame.Size);
  1128. if (ColorScheme != null)
  1129. Driver.SetAttribute (HasFocus ? ColorScheme.Focus : ColorScheme.Normal);
  1130. if (!ustring.IsNullOrEmpty (Text)) {
  1131. Clear ();
  1132. // Draw any Text
  1133. if (textFormatter != null) {
  1134. textFormatter.NeedsFormat = true;
  1135. }
  1136. textFormatter?.Draw (ViewToScreen (Bounds), HasFocus ? ColorScheme.Focus : ColorScheme.Normal, HasFocus ? ColorScheme.HotFocus : ColorScheme.HotNormal);
  1137. }
  1138. // Invoke DrawContentEvent
  1139. OnDrawContent (bounds);
  1140. if (subviews != null) {
  1141. foreach (var view in subviews) {
  1142. if (view.NeedDisplay != null && (!view.NeedDisplay.IsEmpty || view.childNeedsDisplay)) {
  1143. if (view.Frame.IntersectsWith (clipRect) && (view.Frame.IntersectsWith (bounds) || bounds.X < 0 || bounds.Y < 0)) {
  1144. if (view.layoutNeeded)
  1145. view.LayoutSubviews ();
  1146. Application.CurrentView = view;
  1147. // Draw the subview
  1148. // Use the view's bounds (view-relative; Location will always be (0,0) because
  1149. view.Redraw (view.Bounds);
  1150. }
  1151. view.NeedDisplay = Rect.Empty;
  1152. view.childNeedsDisplay = false;
  1153. }
  1154. }
  1155. }
  1156. ClearNeedsDisplay ();
  1157. }
  1158. /// <summary>
  1159. /// Event invoked when the content area of the View is to be drawn.
  1160. /// </summary>
  1161. /// <remarks>
  1162. /// <para>
  1163. /// Will be invoked before any subviews added with <see cref="Add(View)"/> have been drawn.
  1164. /// </para>
  1165. /// <para>
  1166. /// Rect provides the view-relative rectangle describing the currently visible viewport into the <see cref="View"/>.
  1167. /// </para>
  1168. /// </remarks>
  1169. public Action<Rect> DrawContent;
  1170. /// <summary>
  1171. /// Enables overrides to draw infinitely scrolled content and/or a background behind added controls.
  1172. /// </summary>
  1173. /// <param name="viewport">The view-relative rectangle describing the currently visible viewport into the <see cref="View"/></param>
  1174. /// <remarks>
  1175. /// This method will be called before any subviews added with <see cref="Add(View)"/> have been drawn.
  1176. /// </remarks>
  1177. public virtual void OnDrawContent (Rect viewport)
  1178. {
  1179. DrawContent?.Invoke (viewport);
  1180. }
  1181. /// <summary>
  1182. /// Causes the specified subview to have focus.
  1183. /// </summary>
  1184. /// <param name="view">View.</param>
  1185. public void SetFocus (View view)
  1186. {
  1187. if (view == null)
  1188. return;
  1189. //Console.WriteLine ($"Request to focus {view}");
  1190. if (!view.CanFocus)
  1191. return;
  1192. if (focused == view)
  1193. return;
  1194. // Make sure that this view is a subview
  1195. View c;
  1196. for (c = view.container; c != null; c = c.container)
  1197. if (c == this)
  1198. break;
  1199. if (c == null)
  1200. throw new ArgumentException ("the specified view is not part of the hierarchy of this view");
  1201. if (focused != null)
  1202. focused.SetHasFocus (false, view);
  1203. var f = focused;
  1204. focused = view;
  1205. focused.SetHasFocus (true, f);
  1206. focused.EnsureFocus ();
  1207. // Send focus upwards
  1208. SuperView?.SetFocus (this);
  1209. }
  1210. /// <summary>
  1211. /// Defines the event arguments for <see cref="KeyEvent"/>
  1212. /// </summary>
  1213. public class KeyEventEventArgs : EventArgs {
  1214. /// <summary>
  1215. /// Constructs.
  1216. /// </summary>
  1217. /// <param name="ke"></param>
  1218. public KeyEventEventArgs (KeyEvent ke) => KeyEvent = ke;
  1219. /// <summary>
  1220. /// The <see cref="KeyEvent"/> for the event.
  1221. /// </summary>
  1222. public KeyEvent KeyEvent { get; set; }
  1223. /// <summary>
  1224. /// Indicates if the current Key event has already been processed and the driver should stop notifying any other event subscriber.
  1225. /// Its important to set this value to true specially when updating any View's layout from inside the subscriber method.
  1226. /// </summary>
  1227. public bool Handled { get; set; } = false;
  1228. }
  1229. /// <summary>
  1230. /// Invoked when a character key is pressed and occurs after the key up event.
  1231. /// </summary>
  1232. public Action<KeyEventEventArgs> KeyPress;
  1233. /// <inheritdoc/>
  1234. public override bool ProcessKey (KeyEvent keyEvent)
  1235. {
  1236. KeyEventEventArgs args = new KeyEventEventArgs (keyEvent);
  1237. KeyPress?.Invoke (args);
  1238. if (args.Handled)
  1239. return true;
  1240. if (Focused?.ProcessKey (keyEvent) == true)
  1241. return true;
  1242. return false;
  1243. }
  1244. /// <inheritdoc/>
  1245. public override bool ProcessHotKey (KeyEvent keyEvent)
  1246. {
  1247. KeyEventEventArgs args = new KeyEventEventArgs (keyEvent);
  1248. KeyPress?.Invoke (args);
  1249. if (args.Handled)
  1250. return true;
  1251. if (subviews == null || subviews.Count == 0)
  1252. return false;
  1253. foreach (var view in subviews)
  1254. if (view.ProcessHotKey (keyEvent))
  1255. return true;
  1256. return false;
  1257. }
  1258. /// <inheritdoc/>
  1259. public override bool ProcessColdKey (KeyEvent keyEvent)
  1260. {
  1261. KeyEventEventArgs args = new KeyEventEventArgs (keyEvent);
  1262. KeyPress?.Invoke (args);
  1263. if (args.Handled)
  1264. return true;
  1265. if (subviews == null || subviews.Count == 0)
  1266. return false;
  1267. foreach (var view in subviews)
  1268. if (view.ProcessColdKey (keyEvent))
  1269. return true;
  1270. return false;
  1271. }
  1272. /// <summary>
  1273. /// Invoked when a key is pressed
  1274. /// </summary>
  1275. public Action<KeyEventEventArgs> KeyDown;
  1276. /// <param name="keyEvent">Contains the details about the key that produced the event.</param>
  1277. public override bool OnKeyDown (KeyEvent keyEvent)
  1278. {
  1279. KeyEventEventArgs args = new KeyEventEventArgs (keyEvent);
  1280. KeyDown?.Invoke (args);
  1281. if (args.Handled) {
  1282. return true;
  1283. }
  1284. if (Focused?.OnKeyDown (keyEvent) == true) {
  1285. return true;
  1286. }
  1287. return false;
  1288. }
  1289. /// <summary>
  1290. /// Invoked when a key is released
  1291. /// </summary>
  1292. public Action<KeyEventEventArgs> KeyUp;
  1293. /// <param name="keyEvent">Contains the details about the key that produced the event.</param>
  1294. public override bool OnKeyUp (KeyEvent keyEvent)
  1295. {
  1296. KeyEventEventArgs args = new KeyEventEventArgs (keyEvent);
  1297. KeyUp?.Invoke (args);
  1298. if (args.Handled) {
  1299. return true;
  1300. }
  1301. if (Focused?.OnKeyUp (keyEvent) == true) {
  1302. return true;
  1303. }
  1304. return false;
  1305. }
  1306. /// <summary>
  1307. /// Finds the first view in the hierarchy that wants to get the focus if nothing is currently focused, otherwise, it does nothing.
  1308. /// </summary>
  1309. public void EnsureFocus ()
  1310. {
  1311. if (focused == null)
  1312. if (FocusDirection == Direction.Forward)
  1313. FocusFirst ();
  1314. else
  1315. FocusLast ();
  1316. }
  1317. /// <summary>
  1318. /// Focuses the first focusable subview if one exists.
  1319. /// </summary>
  1320. public void FocusFirst ()
  1321. {
  1322. if (tabIndexes == null) {
  1323. SuperView?.SetFocus (this);
  1324. return;
  1325. }
  1326. foreach (var view in tabIndexes) {
  1327. if (view.CanFocus && view.tabStop) {
  1328. SetFocus (view);
  1329. return;
  1330. }
  1331. }
  1332. }
  1333. /// <summary>
  1334. /// Focuses the last focusable subview if one exists.
  1335. /// </summary>
  1336. public void FocusLast ()
  1337. {
  1338. if (tabIndexes == null) {
  1339. SuperView?.SetFocus (this);
  1340. return;
  1341. }
  1342. for (int i = tabIndexes.Count; i > 0;) {
  1343. i--;
  1344. View v = tabIndexes [i];
  1345. if (v.CanFocus && v.tabStop) {
  1346. SetFocus (v);
  1347. return;
  1348. }
  1349. }
  1350. }
  1351. /// <summary>
  1352. /// Focuses the previous view.
  1353. /// </summary>
  1354. /// <returns><c>true</c>, if previous was focused, <c>false</c> otherwise.</returns>
  1355. public bool FocusPrev ()
  1356. {
  1357. FocusDirection = Direction.Backward;
  1358. if (tabIndexes == null || tabIndexes.Count == 0)
  1359. return false;
  1360. if (focused == null) {
  1361. FocusLast ();
  1362. return focused != null;
  1363. }
  1364. int focused_idx = -1;
  1365. for (int i = tabIndexes.Count; i > 0;) {
  1366. i--;
  1367. View w = tabIndexes [i];
  1368. if (w.HasFocus) {
  1369. if (w.FocusPrev ())
  1370. return true;
  1371. focused_idx = i;
  1372. continue;
  1373. }
  1374. if (w.CanFocus && focused_idx != -1 && w.tabStop) {
  1375. focused.SetHasFocus (false, w);
  1376. if (w != null && w.CanFocus && w.tabStop)
  1377. w.FocusLast ();
  1378. SetFocus (w);
  1379. return true;
  1380. }
  1381. }
  1382. if (focused != null) {
  1383. focused.SetHasFocus (false, this);
  1384. focused = null;
  1385. }
  1386. return false;
  1387. }
  1388. /// <summary>
  1389. /// Focuses the next view.
  1390. /// </summary>
  1391. /// <returns><c>true</c>, if next was focused, <c>false</c> otherwise.</returns>
  1392. public bool FocusNext ()
  1393. {
  1394. FocusDirection = Direction.Forward;
  1395. if (tabIndexes == null || tabIndexes.Count == 0)
  1396. return false;
  1397. if (focused == null) {
  1398. FocusFirst ();
  1399. return focused != null;
  1400. }
  1401. int n = tabIndexes.Count;
  1402. int focused_idx = -1;
  1403. for (int i = 0; i < n; i++) {
  1404. View w = tabIndexes [i];
  1405. if (w.HasFocus) {
  1406. if (w.FocusNext ())
  1407. return true;
  1408. focused_idx = i;
  1409. continue;
  1410. }
  1411. if (w.CanFocus && focused_idx != -1 && w.tabStop) {
  1412. focused.SetHasFocus (false, w);
  1413. if (w != null && w.CanFocus && w.tabStop)
  1414. w.FocusFirst ();
  1415. SetFocus (w);
  1416. return true;
  1417. }
  1418. }
  1419. if (focused != null) {
  1420. focused.SetHasFocus (false, this);
  1421. focused = null;
  1422. }
  1423. return false;
  1424. }
  1425. /// <summary>
  1426. /// Sets the View's <see cref="Frame"/> to the relative coordinates if its container, given the <see cref="Frame"/> for its container.
  1427. /// </summary>
  1428. /// <param name="hostFrame">The screen-relative frame for the host.</param>
  1429. /// <remarks>
  1430. /// Reminder: <see cref="Frame"/> is superview-relative; <see cref="Bounds"/> is view-relative.
  1431. /// </remarks>
  1432. internal void SetRelativeLayout (Rect hostFrame)
  1433. {
  1434. int w, h, _x, _y;
  1435. if (x is Pos.PosCenter) {
  1436. if (width == null)
  1437. w = hostFrame.Width;
  1438. else
  1439. w = width.Anchor (hostFrame.Width);
  1440. _x = x.Anchor (hostFrame.Width - w);
  1441. } else {
  1442. if (x == null)
  1443. _x = 0;
  1444. else
  1445. _x = x.Anchor (hostFrame.Width);
  1446. if (width == null)
  1447. w = hostFrame.Width;
  1448. else if (width is Dim.DimFactor && !((Dim.DimFactor)width).IsFromRemaining ())
  1449. w = width.Anchor (hostFrame.Width);
  1450. else
  1451. w = Math.Max (width.Anchor (hostFrame.Width - _x), 0);
  1452. }
  1453. if (y is Pos.PosCenter) {
  1454. if (height == null)
  1455. h = hostFrame.Height;
  1456. else
  1457. h = height.Anchor (hostFrame.Height);
  1458. _y = y.Anchor (hostFrame.Height - h);
  1459. } else {
  1460. if (y == null)
  1461. _y = 0;
  1462. else
  1463. _y = y.Anchor (hostFrame.Height);
  1464. if (height == null)
  1465. h = hostFrame.Height;
  1466. else if (height is Dim.DimFactor && !((Dim.DimFactor)height).IsFromRemaining ())
  1467. h = height.Anchor (hostFrame.Height);
  1468. else
  1469. h = Math.Max (height.Anchor (hostFrame.Height - _y), 0);
  1470. }
  1471. Frame = new Rect (_x, _y, w, h);
  1472. }
  1473. // https://en.wikipedia.org/wiki/Topological_sorting
  1474. List<View> TopologicalSort (HashSet<View> nodes, HashSet<(View From, View To)> edges)
  1475. {
  1476. var result = new List<View> ();
  1477. // Set of all nodes with no incoming edges
  1478. var S = new HashSet<View> (nodes.Where (n => edges.All (e => e.To.Equals (n) == false)));
  1479. while (S.Any ()) {
  1480. // remove a node n from S
  1481. var n = S.First ();
  1482. S.Remove (n);
  1483. // add n to tail of L
  1484. if (n != this?.SuperView)
  1485. result.Add (n);
  1486. // for each node m with an edge e from n to m do
  1487. foreach (var e in edges.Where (e => e.From.Equals (n)).ToArray ()) {
  1488. var m = e.To;
  1489. // remove edge e from the graph
  1490. edges.Remove (e);
  1491. // if m has no other incoming edges then
  1492. if (edges.All (me => me.To.Equals (m) == false) && m != this?.SuperView) {
  1493. // insert m into S
  1494. S.Add (m);
  1495. }
  1496. }
  1497. }
  1498. if (edges.Any ()) {
  1499. if (!object.ReferenceEquals (edges.First ().From, edges.First ().To)) {
  1500. throw new InvalidOperationException ($"TopologicalSort (for Pos/Dim) cannot find {edges.First ().From}. Did you forget to add it to {this}?");
  1501. } else {
  1502. throw new InvalidOperationException ("TopologicalSort encountered a recursive cycle in the relative Pos/Dim in the views of " + this);
  1503. }
  1504. } else {
  1505. // return L (a topologically sorted order)
  1506. return result;
  1507. }
  1508. }
  1509. /// <summary>
  1510. /// Event arguments for the <see cref="LayoutComplete"/> event.
  1511. /// </summary>
  1512. public class LayoutEventArgs : EventArgs {
  1513. /// <summary>
  1514. /// The view-relative bounds of the <see cref="View"/> before it was laid out.
  1515. /// </summary>
  1516. public Rect OldBounds { get; set; }
  1517. }
  1518. /// <summary>
  1519. /// Fired after the Views's <see cref="LayoutSubviews"/> method has completed.
  1520. /// </summary>
  1521. /// <remarks>
  1522. /// Subscribe to this event to perform tasks when the <see cref="View"/> has been resized or the layout has otherwise changed.
  1523. /// </remarks>
  1524. public Action<LayoutEventArgs> LayoutStarted;
  1525. /// <summary>
  1526. /// Raises the <see cref="LayoutStarted"/> event. Called from <see cref="LayoutSubviews"/> before any subviews have been laid out.
  1527. /// </summary>
  1528. internal virtual void OnLayoutStarted (LayoutEventArgs args)
  1529. {
  1530. LayoutStarted?.Invoke (args);
  1531. }
  1532. /// <summary>
  1533. /// Fired after the Views's <see cref="LayoutSubviews"/> method has completed.
  1534. /// </summary>
  1535. /// <remarks>
  1536. /// Subscribe to this event to perform tasks when the <see cref="View"/> has been resized or the layout has otherwise changed.
  1537. /// </remarks>
  1538. public Action<LayoutEventArgs> LayoutComplete;
  1539. /// <summary>
  1540. /// Event called only once when the <see cref="View"/> is being initialized for the first time.
  1541. /// Allows configurations and assignments to be performed before the <see cref="View"/> being shown.
  1542. /// This derived from <see cref="ISupportInitializeNotification"/> to allow notify all the views that are being initialized.
  1543. /// </summary>
  1544. public event EventHandler Initialized;
  1545. /// <summary>
  1546. /// Raises the <see cref="LayoutComplete"/> event. Called from <see cref="LayoutSubviews"/> before all sub-views have been laid out.
  1547. /// </summary>
  1548. internal virtual void OnLayoutComplete (LayoutEventArgs args)
  1549. {
  1550. LayoutComplete?.Invoke (args);
  1551. }
  1552. /// <summary>
  1553. /// Invoked when a view starts executing or when the dimensions of the view have changed, for example in
  1554. /// response to the container view or terminal resizing.
  1555. /// </summary>
  1556. /// <remarks>
  1557. /// Calls <see cref="OnLayoutComplete"/> (which raises the <see cref="LayoutComplete"/> event) before it returns.
  1558. /// </remarks>
  1559. public virtual void LayoutSubviews ()
  1560. {
  1561. if (!layoutNeeded) {
  1562. return;
  1563. }
  1564. Rect oldBounds = Bounds;
  1565. OnLayoutStarted (new LayoutEventArgs () { OldBounds = oldBounds });
  1566. textFormatter.Size = Bounds.Size;
  1567. // Sort out the dependencies of the X, Y, Width, Height properties
  1568. var nodes = new HashSet<View> ();
  1569. var edges = new HashSet<(View, View)> ();
  1570. foreach (var v in InternalSubviews) {
  1571. nodes.Add (v);
  1572. if (v.LayoutStyle == LayoutStyle.Computed) {
  1573. if (v.X is Pos.PosView vX)
  1574. edges.Add ((vX.Target, v));
  1575. if (v.Y is Pos.PosView vY)
  1576. edges.Add ((vY.Target, v));
  1577. if (v.Width is Dim.DimView vWidth)
  1578. edges.Add ((vWidth.Target, v));
  1579. if (v.Height is Dim.DimView vHeight)
  1580. edges.Add ((vHeight.Target, v));
  1581. }
  1582. }
  1583. var ordered = TopologicalSort (nodes, edges);
  1584. foreach (var v in ordered) {
  1585. if (v.LayoutStyle == LayoutStyle.Computed) {
  1586. v.SetRelativeLayout (Frame);
  1587. }
  1588. v.LayoutSubviews ();
  1589. v.layoutNeeded = false;
  1590. }
  1591. if (SuperView == Application.Top && layoutNeeded && ordered.Count == 0 && LayoutStyle == LayoutStyle.Computed) {
  1592. SetRelativeLayout (Frame);
  1593. }
  1594. layoutNeeded = false;
  1595. OnLayoutComplete (new LayoutEventArgs () { OldBounds = oldBounds });
  1596. }
  1597. /// <summary>
  1598. /// The text displayed by the <see cref="View"/>.
  1599. /// </summary>
  1600. /// <remarks>
  1601. /// <para>
  1602. /// If provided, the text will be drawn before any subviews are drawn.
  1603. /// </para>
  1604. /// <para>
  1605. /// The text will be drawn starting at the view origin (0, 0) and will be formatted according
  1606. /// to the <see cref="TextAlignment"/> property. If the view's height is greater than 1, the
  1607. /// text will word-wrap to additional lines if it does not fit horizontally. If the view's height
  1608. /// is 1, the text will be clipped.
  1609. /// </para>
  1610. /// <para>
  1611. /// Set the <see cref="HotKeySpecifier"/> to enable hotkey support. To disable hotkey support set <see cref="HotKeySpecifier"/> to
  1612. /// <c>(Rune)0xffff</c>.
  1613. /// </para>
  1614. /// </remarks>
  1615. public virtual ustring Text {
  1616. get => textFormatter.Text;
  1617. set {
  1618. textFormatter.Text = value;
  1619. SetNeedsDisplay ();
  1620. }
  1621. }
  1622. /// <summary>
  1623. /// Gets or sets how the View's <see cref="Text"/> is aligned horizontally when drawn. Changing this property will redisplay the <see cref="View"/>.
  1624. /// </summary>
  1625. /// <value>The text alignment.</value>
  1626. public virtual TextAlignment TextAlignment {
  1627. get => textFormatter.Alignment;
  1628. set {
  1629. textFormatter.Alignment = value;
  1630. SetNeedsDisplay ();
  1631. }
  1632. }
  1633. /// <summary>
  1634. /// Get or sets if the <see cref="View"/> was already initialized.
  1635. /// This derived from <see cref="ISupportInitializeNotification"/> to allow notify all the views that are being initialized.
  1636. /// </summary>
  1637. public bool IsInitialized { get; set; }
  1638. /// <summary>
  1639. /// Pretty prints the View
  1640. /// </summary>
  1641. /// <returns></returns>
  1642. public override string ToString ()
  1643. {
  1644. return $"{GetType ().Name}({Id})({Frame})";
  1645. }
  1646. /// <summary>
  1647. /// Specifies the event arguments for <see cref="MouseEvent"/>
  1648. /// </summary>
  1649. public class MouseEventArgs : EventArgs {
  1650. /// <summary>
  1651. /// Constructs.
  1652. /// </summary>
  1653. /// <param name="me"></param>
  1654. public MouseEventArgs (MouseEvent me) => MouseEvent = me;
  1655. /// <summary>
  1656. /// The <see cref="MouseEvent"/> for the event.
  1657. /// </summary>
  1658. public MouseEvent MouseEvent { get; set; }
  1659. /// <summary>
  1660. /// Indicates if the current mouse event has already been processed and the driver should stop notifying any other event subscriber.
  1661. /// Its important to set this value to true specially when updating any View's layout from inside the subscriber method.
  1662. /// </summary>
  1663. public bool Handled { get; set; }
  1664. }
  1665. /// <inheritdoc/>
  1666. public override bool OnMouseEnter (MouseEvent mouseEvent)
  1667. {
  1668. MouseEventArgs args = new MouseEventArgs (mouseEvent);
  1669. MouseEnter?.Invoke (args);
  1670. if (args.Handled)
  1671. return true;
  1672. if (base.OnMouseEnter (mouseEvent))
  1673. return true;
  1674. return false;
  1675. }
  1676. /// <inheritdoc/>
  1677. public override bool OnMouseLeave (MouseEvent mouseEvent)
  1678. {
  1679. MouseEventArgs args = new MouseEventArgs (mouseEvent);
  1680. MouseLeave?.Invoke (args);
  1681. if (args.Handled)
  1682. return true;
  1683. if (base.OnMouseLeave (mouseEvent))
  1684. return true;
  1685. return false;
  1686. }
  1687. /// <summary>
  1688. /// Method invoked when a mouse event is generated
  1689. /// </summary>
  1690. /// <param name="mouseEvent"></param>
  1691. /// <returns><c>true</c>, if the event was handled, <c>false</c> otherwise.</returns>
  1692. public virtual bool OnMouseEvent (MouseEvent mouseEvent)
  1693. {
  1694. MouseEventArgs args = new MouseEventArgs (mouseEvent);
  1695. MouseClick?.Invoke (args);
  1696. if (args.Handled)
  1697. return true;
  1698. if (MouseEvent (mouseEvent))
  1699. return true;
  1700. if (mouseEvent.Flags == MouseFlags.Button1Clicked) {
  1701. if (!HasFocus && SuperView != null) {
  1702. SuperView.SetFocus (this);
  1703. SetNeedsDisplay ();
  1704. }
  1705. return true;
  1706. }
  1707. return false;
  1708. }
  1709. /// <inheritdoc/>
  1710. protected override void Dispose (bool disposing)
  1711. {
  1712. for (int i = InternalSubviews.Count - 1; i >= 0; i--) {
  1713. View subview = InternalSubviews [i];
  1714. Remove (subview);
  1715. subview.Dispose ();
  1716. }
  1717. if (Application.Top.focused == this) {
  1718. Application.Top.focused = null;
  1719. }
  1720. base.Dispose (disposing);
  1721. }
  1722. /// <summary>
  1723. /// This derived from <see cref="ISupportInitializeNotification"/> to allow notify all the views that are beginning initialized.
  1724. /// </summary>
  1725. public void BeginInit ()
  1726. {
  1727. if (!IsInitialized) {
  1728. oldCanFocus = CanFocus;
  1729. oldTabIndex = tabIndex;
  1730. Initialized?.Invoke (this, EventArgs.Empty);
  1731. }
  1732. if (subviews?.Count > 0) {
  1733. foreach (var view in subviews) {
  1734. if (!view.IsInitialized) {
  1735. view.BeginInit ();
  1736. }
  1737. }
  1738. }
  1739. }
  1740. /// <summary>
  1741. /// This derived from <see cref="ISupportInitializeNotification"/> to allow notify all the views that are ending initialized.
  1742. /// </summary>
  1743. public void EndInit ()
  1744. {
  1745. IsInitialized = true;
  1746. if (subviews?.Count > 0) {
  1747. foreach (var view in subviews) {
  1748. if (!view.IsInitialized) {
  1749. view.EndInit ();
  1750. }
  1751. }
  1752. }
  1753. }
  1754. }
  1755. }