//
// Authors:
// Miguel de Icaza (miguel@gnome.org)
//
// Pending:
// - Check for NeedDisplay on the hierarchy and repaint
// - Layout support
// - "Colors" type or "Attributes" type?
// - What to surface as "BackgroundCOlor" when clearing a window, an attribute or colors?
//
// Optimziations
// - Add rendering limitation to the exposed area
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using NStack;
namespace Terminal.Gui {
///
/// Determines the LayoutStyle for a view, if Absolute, during LayoutSubviews, the
/// value from the Frame will be used, if the value is Computed, then the Frame
/// will be updated from the X, Y Pos objects and the Width and Height Dim objects.
///
public enum LayoutStyle {
///
/// The position and size of the view are based on the Frame value.
///
Absolute,
///
/// The position and size of the view will be computed based on the
/// X, Y, Width and Height properties and set on the Frame.
///
Computed
}
///
/// 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.
///
///
///
/// 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.
///
///
/// Views supports two layout styles: Absolute or Computed. The choice as to which layout style is used by the View
/// is determined when the View is initizlied. To create a View using Absolute layout, call a constructor that takes a
/// Rect parameter to specify the absolute position and size (the View.)/. To create a View
/// using Computed layout use a constructor that does not take a Rect parametr and set the X, Y, Width and Height
/// properties on the view. Both approaches use coordinates that are relative to the container they are being added to.
///
///
/// To switch between Absolute and Computed layout, use the property.
///
///
/// 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
/// and you can use either absolute positions, percentages or anchor
/// points. The Width and Height properties are of type
/// 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.
///
///
/// Absolute layout requires specifying coordiantes and sizes of Views explicitly, and the
/// View will typcialy stay in a fixed position and size. To change the position and size use the
/// property.
///
///
/// Subviews (child views) can be added to a View by calling the method.
/// The container of a View can be accessed with the property.
///
///
/// To flag a region of the View's to be redrawn call . To flag the entire view
/// for redraw call .
///
///
/// Views have a 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.
///
///
/// Subclasses should not rely on being
/// set at construction time. If a is not set on a view, the view will inherit the
/// value from its and the value might only be valid once a view has been
/// added to a SuperView.
///
///
/// By using applications will work both
/// in color as well as black and white displays.
///
///
/// Views that are focusable should implement the 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.
///
///
/// The 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 , and will recompute the
/// frames for the vies that use .
///
///
public class View : Responder, IEnumerable {
internal enum Direction {
Forward,
Backward
}
// container == SuperView
View container = null;
View focused = null;
Direction focusDirection;
///
/// Event fired when the view gets focus.
///
public event Action Enter;
///
/// Event fired when the view looses focus.
///
public event Action Leave;
///
/// Event fired when the view receives the mouse event for the first time.
///
public event Action MouseEnter;
///
/// Event fired when the view receives a mouse event for the last time.
///
public event Action MouseLeave;
///
/// Event fired when a mouse event is generated.
///
public event Action MouseClick;
internal Direction FocusDirection {
get => SuperView?.FocusDirection ?? focusDirection;
set {
if (SuperView != null)
SuperView.FocusDirection = value;
else
focusDirection = value;
}
}
///
/// Points to the current driver in use by the view, it is a convenience property
/// for simplifying the development of new views.
///
public static ConsoleDriver Driver { get { return Application.Driver; } }
static IList empty = new List (0).AsReadOnly ();
// This is null, and allocated on demand.
List subviews;
///
/// This returns a list of the subviews contained by this view.
///
/// The subviews.
public IList Subviews => subviews == null ? empty : subviews.AsReadOnly ();
// Internally, we use InternalSubviews rather than subviews, as we do not expect us
// to make the same mistakes our users make when they poke at the Subviews.
internal IList InternalSubviews => subviews ?? empty;
internal Rect NeedDisplay { get; private set; } = Rect.Empty;
// The frame for the object. Superview relative.
Rect frame;
///
/// Gets or sets an identifier for the view;
///
/// The identifier.
/// The id should be unique across all Views that share a SuperView.
public ustring Id { get; set; } = "";
///
/// Returns a value indicating if this View is currently on Top (Active)
///
public bool IsCurrentTop {
get {
return Application.Current == this;
}
}
///
/// Gets or sets a value indicating whether this wants mouse position reports.
///
/// true if want mouse position reports; otherwise, false.
public virtual bool WantMousePositionReports { get; set; } = false;
///
/// Gets or sets a value indicating whether this want continuous button pressed event.
///
public virtual bool WantContinuousButtonPressed { get; set; } = false;
///
/// Gets or sets the frame for the view. The frame is relative to the view's container ().
///
/// The frame.
///
///
/// Change the Frame when using the layout style to move or resize views.
///
///
/// Altering the Frame of a view will trigger the redrawing of the
/// view as well as the redrawing of the affected regions of the .
///
///
public virtual Rect Frame {
get => frame;
set {
if (SuperView != null) {
SuperView.SetNeedsDisplay (frame);
SuperView.SetNeedsDisplay (value);
}
frame = value;
SetNeedsLayout ();
SetNeedsDisplay (frame);
}
}
///
/// Gets an enumerator that enumerates the subviews in this view.
///
/// The enumerator.
public IEnumerator GetEnumerator ()
{
foreach (var v in InternalSubviews)
yield return v;
}
LayoutStyle layoutStyle;
///
/// Controls how the View's is computed during the LayoutSubviews method, if the style is set to ,
/// LayoutSubviews does not change the . If the style is the is updated using
/// the , , , and properties.
///
/// The layout style.
public LayoutStyle LayoutStyle {
get => layoutStyle;
set {
layoutStyle = value;
SetNeedsLayout ();
}
}
///
/// The bounds represent the View-relative rectangle used for this view; the area inside of the view.
///
/// The bounds.
///
///
/// Updates to the Bounds update the ,
/// and has the same side effects as updating the .
///
///
/// Because coordinates are relative to the upper-left corner of the ,
/// the coordinates of the upper-left corner of the rectangle returned by this property are (0,0).
/// Use this property to obtain the size and coordinates of the client area of the
/// control for tasks such as drawing on the surface of the control.
///
///
public Rect Bounds {
get => new Rect (Point.Empty, Frame.Size);
set {
Frame = new Rect (frame.Location, value.Size);
}
}
Pos x, y;
///
/// Gets or sets the X position for the view (the column). Only used whe is .
///
/// The X Position.
///
/// If is changing this property has no effect and its value is indeterminate.
///
public Pos X {
get => x;
set {
x = value;
SetNeedsLayout ();
}
}
///
/// Gets or sets the Y position for the view (the row). Only used whe is .
///
/// The y position (line).
///
/// If is changing this property has no effect and its value is indeterminate.
///
public Pos Y {
get => y;
set {
y = value;
SetNeedsLayout ();
}
}
Dim width, height;
///
/// Gets or sets the width of the view. Only used whe is .
///
/// The width.
///
/// If is changing this property has no effect and its value is indeterminate.
///
public Dim Width {
get => width;
set {
width = value;
SetNeedsLayout ();
}
}
///
/// Gets or sets the height of the view. Only used whe is .
///
/// The height.
/// If is changing this property has no effect and its value is indeterminate.
public Dim Height {
get => height;
set {
height = value;
SetNeedsLayout ();
}
}
///
/// Returns the container for this view, or null if this view has not been added to a container.
///
/// The super view.
public View SuperView => container;
///
/// Initializes a new instance of a class with the absolute
/// dimensions specified in the frame parameter.
///
/// The region covered by this view.
///
/// This constructor intitalize a View with a of . Use to
/// initialize a View with of
///
public View (Rect frame)
{
this.Frame = frame;
CanFocus = false;
LayoutStyle = LayoutStyle.Absolute;
}
///
/// Initializes a new instance of class.
///
///
/// Use , , , and properties to dynamically control the size and location of the view.
///
///
/// This constructor intitalize a View with a of .
/// Use , , , and properties to dynamically control the size and location of the view.
///
public View ()
{
CanFocus = false;
LayoutStyle = LayoutStyle.Computed;
x = Pos.At (0);
y = Pos.At (0);
Height = 0;
Width = 0;
}
///
/// Sets a flag indicating this view needs to be redisplayed because its state has changed.
///
public void SetNeedsDisplay ()
{
SetNeedsDisplay (Bounds);
}
internal bool layoutNeeded = true;
internal void SetNeedsLayout ()
{
if (layoutNeeded)
return;
layoutNeeded = true;
if (SuperView == null)
return;
SuperView.SetNeedsLayout ();
}
///
/// Flags the view-relative region on this View as needing to be repainted.
///
/// The view-relative region that must be flagged for repaint.
public void SetNeedsDisplay (Rect region)
{
if (NeedDisplay == null || NeedDisplay.IsEmpty)
NeedDisplay = region;
else {
var x = Math.Min (NeedDisplay.X, region.X);
var y = Math.Min (NeedDisplay.Y, region.Y);
var w = Math.Max (NeedDisplay.Width, region.Width);
var h = Math.Max (NeedDisplay.Height, region.Height);
NeedDisplay = new Rect (x, y, w, h);
}
if (container != null)
container.ChildNeedsDisplay ();
if (subviews == null)
return;
foreach (var view in subviews)
if (view.Frame.IntersectsWith (region)) {
var childRegion = Rect.Intersect (view.Frame, region);
childRegion.X -= view.Frame.X;
childRegion.Y -= view.Frame.Y;
view.SetNeedsDisplay (childRegion);
}
}
internal bool childNeedsDisplay;
///
/// Indicates that any child views (in the list) need to be repainted.
///
public void ChildNeedsDisplay ()
{
childNeedsDisplay = true;
if (container != null)
container.ChildNeedsDisplay ();
}
///
/// Adds a subview (child) to this view.
///
///
/// The Views that have been added to this view can be retrieved via the property. See also
///
public virtual void Add (View view)
{
if (view == null)
return;
if (subviews == null)
subviews = new List ();
subviews.Add (view);
view.container = this;
if (view.CanFocus)
CanFocus = true;
SetNeedsLayout ();
SetNeedsDisplay ();
}
///
/// Adds the specified views (children) to the view.
///
/// Array of one or more views (can be optional parameter).
///
/// The Views that have been added to this view can be retrieved via the property. See also
///
public void Add (params View [] views)
{
if (views == null)
return;
foreach (var view in views)
Add (view);
}
///
/// Removes all subviews (children) added via or from this View.
///
public virtual void RemoveAll ()
{
if (subviews == null)
return;
while (subviews.Count > 0) {
Remove (subviews [0]);
}
}
///
/// Removes a subview added via or from this View.
///
///
///
public virtual void Remove (View view)
{
if (view == null || subviews == null)
return;
SetNeedsLayout ();
SetNeedsDisplay ();
var touched = view.Frame;
subviews.Remove (view);
view.container = null;
if (subviews.Count < 1)
this.CanFocus = false;
foreach (var v in subviews) {
if (v.Frame.IntersectsWith (touched))
view.SetNeedsDisplay ();
}
}
void PerformActionForSubview (View subview, Action action)
{
if (subviews.Contains (subview)) {
action (subview);
}
SetNeedsDisplay ();
subview.SetNeedsDisplay ();
}
///
/// Brings the specified subview to the front so it is drawn on top of any other views.
///
/// The subview to send to the front
///
/// .
///
public void BringSubviewToFront (View subview)
{
PerformActionForSubview (subview, x => {
subviews.Remove (x);
subviews.Add (x);
});
}
///
/// Sends the specified subview to the front so it is the first view drawn
///
/// The subview to send to the front
///
/// .
///
public void SendSubviewToBack (View subview)
{
PerformActionForSubview (subview, x => {
subviews.Remove (x);
subviews.Insert (0, subview);
});
}
///
/// Moves the subview backwards in the hierarchy, only one step
///
/// The subview to send backwards
///
/// If you want to send the view all the way to the back use SendSubviewToBack.
///
public void SendSubviewBackwards (View subview)
{
PerformActionForSubview (subview, x => {
var idx = subviews.IndexOf (x);
if (idx > 0) {
subviews.Remove (x);
subviews.Insert (idx - 1, x);
}
});
}
///
/// Moves the subview backwards in the hierarchy, only one step
///
/// The subview to send backwards
///
/// If you want to send the view all the way to the back use SendSubviewToBack.
///
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);
}
});
}
///
/// Clears the view region with the current color.
///
///
///
/// This clears the entire region used by this view.
///
///
public void Clear ()
{
var h = Frame.Height;
var w = Frame.Width;
for (int line = 0; line < h; line++) {
Move (0, line);
for (int col = 0; col < w; col++)
Driver.AddRune (' ');
}
}
///
/// Clears the specified region with the current color.
///
///
///
/// The screen-relative region to clear.
public void Clear (Rect regionScreen)
{
var h = regionScreen.Height;
var w = regionScreen.Width;
for (int line = regionScreen.Y; line < regionScreen.Y + h; line++) {
Driver.Move (regionScreen.X, line);
for (int col = 0; col < w; col++)
Driver.AddRune (' ');
}
}
///
/// Converts a view-relative (col,row) position to a screen-relative position (col,row). The values are optionally clamped to the screen dimensions.
///
/// View-relative column.
/// View-relative row.
/// Absolute column; screen-relative.
/// Absolute row; screen-relative.
/// Whether to clip the result of the ViewToScreen method, if set to true, the rcol, rrow values are clamped to the screen (terminal) dimensions (0..TerminalDim-1).
internal void ViewToScreen (int col, int row, out int rcol, out int rrow, bool clipped = true)
{
// Computes the real row, col relative to the screen.
rrow = row + frame.Y;
rcol = col + frame.X;
var ccontainer = container;
while (ccontainer != null) {
rrow += ccontainer.frame.Y;
rcol += ccontainer.frame.X;
ccontainer = ccontainer.container;
}
// The following ensures that the cursor is always in the screen boundaries.
if (clipped) {
rrow = Math.Max (0, Math.Min (rrow, Driver.Rows - 1));
rcol = Math.Max (0, Math.Min (rcol, Driver.Cols - 1));
}
}
///
/// Converts a point from screen-relative coordinates to view-relative coordinates.
///
/// The mapped point.
/// X screen-coordinate point.
/// Y screen-coordinate point.
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);
}
}
///
/// Converts a region in view-relative coordinates to screen-relative coordinates.
///
internal Rect ViewToScreen (Rect region)
{
ViewToScreen (region.X, region.Y, out var x, out var y, clipped: false);
return new Rect (x, y, region.Width, region.Height);
}
// Clips a rectangle in screen coordinates to the dimensions currently available on the screen
internal Rect ScreenClip (Rect regionScreen)
{
var x = regionScreen.X < 0 ? 0 : regionScreen.X;
var y = regionScreen.Y < 0 ? 0 : regionScreen.Y;
var w = regionScreen.X + regionScreen.Width >= Driver.Cols ? Driver.Cols - regionScreen.X : regionScreen.Width;
var h = regionScreen.Y + regionScreen.Height >= Driver.Rows ? Driver.Rows - regionScreen.Y : regionScreen.Height;
return new Rect (x, y, w, h);
}
///
/// Sets the 's clip region to the current View's .
///
/// The existing driver's clip region, which can be then re-eapplied by setting .Clip ().
///
/// is View-relative.
///
public Rect ClipToBounds ()
{
return SetClip (Bounds);
}
///
/// Sets the clip region to the specified view-relative region.
///
/// The previous screen-relative clip region.
/// View-relative clip region.
public Rect SetClip (Rect region)
{
var previous = Driver.Clip;
Driver.Clip = Rect.Intersect (previous, ViewToScreen (region));
return previous;
}
///
/// Draws a frame in the current view, clipped by the boundary of this view
///
/// View-relative region for the frame to be drawn.
/// The padding to add around the outside of the drawn frame.
/// If set to true it fill will the contents.
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;
}
///
/// Utility function to draw strings that contain a hotkey.
///
/// String to display, the underscoore before a letter flags the next letter as the hotkey.
/// Hot color.
/// Normal color.
///
/// The hotkey is any character following an underscore ('_') character.
public void DrawHotString (ustring text, Attribute hotColor, Attribute normalColor)
{
Driver.SetAttribute (normalColor);
foreach (var rune in text) {
if (rune == '_') {
Driver.SetAttribute (hotColor);
continue;
}
Driver.AddRune (rune);
Driver.SetAttribute (normalColor);
}
}
///
/// Utility function to draw strings that contains a hotkey using a and the "focused" state.
///
/// String to display, the underscoore before a letter flags the next letter as the hotkey.
/// If set to true this uses the focused colors from the color scheme, otherwise the regular ones.
/// The color scheme to use.
public void DrawHotString (ustring text, bool focused, ColorScheme scheme)
{
if (focused)
DrawHotString (text, scheme.HotFocus, scheme.Focus);
else
DrawHotString (text, scheme.HotNormal, scheme.Normal);
}
///
/// This moves the cursor to the specified column and row in the view.
///
/// The move.
/// Col (view-relative).
/// Row (view-relative).
public void Move (int col, int row)
{
ViewToScreen (col, row, out var rcol, out var rrow);
Driver.Move (rcol, rrow);
}
///
/// Positions the cursor in the right position based on the currently focused view in the chain.
///
/// Views that are focusable should override to ensure
/// the cursor is placed in a location that makes sense. Unix terminals do not have
/// a way of hiding the cursor, so it can be distracting to have the cursor left at
/// the last focused view. Views should make sure that they place the cursor
/// in a visually sensible place.
public virtual void PositionCursor ()
{
if (focused != null)
focused.PositionCursor ();
else
Move (frame.X, frame.Y);
}
///
public override bool HasFocus {
get {
return base.HasFocus;
}
internal set {
if (base.HasFocus != value)
if (value)
OnEnter ();
else
OnLeave ();
SetNeedsDisplay ();
base.HasFocus = value;
// Remove focus down the chain of subviews if focus is removed
if (!value && focused != null) {
focused.OnLeave ();
focused.HasFocus = false;
focused = null;
}
}
}
///
/// Defines the event arguments for
///
public class FocusEventArgs : EventArgs {
///
/// Constructs.
///
public FocusEventArgs () { }
///
/// Indicates if the current focus event has already been processed and the driver should stop notifying any other event subscriber.
/// Its important to set this value to true specially when updating any View's layout from inside the subscriber method.
///
public bool Handled { get; set; }
}
///
public override bool OnEnter ()
{
FocusEventArgs args = new FocusEventArgs ();
Enter?.Invoke (args);
if (args.Handled)
return true;
if (base.OnEnter ())
return true;
return false;
}
///
public override bool OnLeave ()
{
FocusEventArgs args = new FocusEventArgs ();
Leave?.Invoke (args);
if (args.Handled)
return true;
if (base.OnLeave ())
return true;
return false;
}
///
/// Returns the currently focused view inside this view, or null if nothing is focused.
///
/// The focused.
public View Focused => focused;
///
/// Returns the most focused view in the chain of subviews (the leaf view that has the focus).
///
/// The most focused.
public View MostFocused {
get {
if (Focused == null)
return null;
var most = Focused.MostFocused;
if (most != null)
return most;
return Focused;
}
}
///
/// The color scheme for this view, if it is not defined, it returns the 's
/// color scheme.
///
public ColorScheme ColorScheme {
get {
if (colorScheme == null)
return SuperView?.ColorScheme;
return colorScheme;
}
set {
colorScheme = value;
}
}
ColorScheme colorScheme;
///
/// Displays the specified character in the specified column and row of the View.
///
/// Column (view-relative).
/// Row (view-relative).
/// Ch.
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);
}
///
/// Removes the and the setting on this view.
///
protected void ClearNeedsDisplay ()
{
NeedDisplay = Rect.Empty;
childNeedsDisplay = false;
}
///
/// Redraws this view and its subviews; only redraws the views that have been flagged for a re-display.
///
/// The bounds (view-relative region) to redraw.
///
///
/// Always use (view-relative) when calling , NOT (superview-relative).
///
///
/// Views should set the color that they want to use on entry, as otherwise this will inherit
/// the last color that was set globaly on the driver.
///
///
/// Overrides of must ensure they do not set Driver.Clip to a clip region
/// larger than the region parameter.
///
///
public virtual void Redraw (Rect bounds)
{
var clipRect = new Rect (Point.Empty, frame.Size);
// Invoke DrawContentEvent
OnDrawContent (bounds);
if (subviews != null) {
foreach (var view in subviews) {
if (view.NeedDisplay != null && (!view.NeedDisplay.IsEmpty || view.childNeedsDisplay)) {
if (view.Frame.IntersectsWith (clipRect) && view.Frame.IntersectsWith (bounds)) {
// FIXED: optimize this by computing the intersection of region and view.Bounds
if (view.layoutNeeded)
view.LayoutSubviews ();
Application.CurrentView = view;
// Clip the sub-view
var savedClip = ClipToBounds ();
// Draw the subview
// Use the view's bounds (view-relative; Location will always be (0,0) because
view.Redraw (view.Bounds);
// Undo the clip
Driver.Clip = savedClip;
}
view.NeedDisplay = Rect.Empty;
view.childNeedsDisplay = false;
}
}
}
ClearNeedsDisplay ();
}
///
/// Event invoked when the content area of the View is to be drawn.
///
///
///
/// Will be invoked before any subviews added with have been drawn.
///
///
/// Rect provides the view-relative rectangle describing the currently visible viewport into the .
///
///
public event Action DrawContent;
///
/// Enables overrides to draw infinitely scrolled content and/or a background behind added controls.
///
/// The view-relative rectangle describing the currently visible viewport into the
///
/// This method will be called before any subviews added with have been drawn.
///
public virtual void OnDrawContent (Rect viewport)
{
DrawContent?.Invoke (viewport);
}
///
/// Causes the specified subview to have focus.
///
/// View.
public void SetFocus (View view)
{
if (view == null)
return;
//Console.WriteLine ($"Request to focus {view}");
if (!view.CanFocus)
return;
if (focused == view)
return;
// Make sure that this view is a subview
View c;
for (c = view.container; c != null; c = c.container)
if (c == this)
break;
if (c == null)
throw new ArgumentException ("the specified view is not part of the hierarchy of this view");
if (focused != null)
focused.HasFocus = false;
focused = view;
focused.HasFocus = true;
focused.EnsureFocus ();
// Send focus upwards
SuperView?.SetFocus (this);
}
///
/// Defines the event arguments for
///
public class KeyEventEventArgs : EventArgs {
///
/// Constructs.
///
///
public KeyEventEventArgs (KeyEvent ke) => KeyEvent = ke;
///
/// The for the event.
///
public KeyEvent KeyEvent { get; set; }
///
/// Indicates if the current Key event has already been processed and the driver should stop notifying any other event subscriber.
/// Its important to set this value to true specially when updating any View's layout from inside the subscriber method.
///
public bool Handled { get; set; } = false;
}
///
/// Invoked when a character key is pressed and occurs after the key up event.
///
public event Action KeyPress;
///
public override bool ProcessKey (KeyEvent keyEvent)
{
KeyEventEventArgs args = new KeyEventEventArgs (keyEvent);
KeyPress?.Invoke (args);
if (args.Handled)
return true;
if (Focused?.ProcessKey (keyEvent) == true)
return true;
return false;
}
///
public override bool ProcessHotKey (KeyEvent keyEvent)
{
KeyEventEventArgs args = new KeyEventEventArgs (keyEvent);
KeyPress?.Invoke (args);
if (args.Handled)
return true;
if (subviews == null || subviews.Count == 0)
return false;
foreach (var view in subviews)
if (view.SuperView.IsCurrentTop && view.ProcessHotKey (keyEvent))
return true;
return false;
}
///
public override bool ProcessColdKey (KeyEvent keyEvent)
{
KeyEventEventArgs args = new KeyEventEventArgs (keyEvent);
KeyPress?.Invoke (args);
if (args.Handled)
return true;
if (subviews == null || subviews.Count == 0)
return false;
foreach (var view in subviews)
if (view.SuperView.IsCurrentTop && view.ProcessColdKey (keyEvent))
return true;
return false;
}
///
/// Invoked when a key is pressed
///
public event Action KeyDown;
/// Contains the details about the key that produced the event.
public override bool OnKeyDown (KeyEvent keyEvent)
{
KeyEventEventArgs args = new KeyEventEventArgs (keyEvent);
KeyDown?.Invoke (args);
if (args.Handled)
return true;
if (subviews == null || subviews.Count == 0)
return false;
foreach (var view in subviews)
if (view.SuperView.IsCurrentTop && view.OnKeyDown (keyEvent))
return true;
return false;
}
///
/// Invoked when a key is released
///
public event Action KeyUp;
/// Contains the details about the key that produced the event.
public override bool OnKeyUp (KeyEvent keyEvent)
{
KeyEventEventArgs args = new KeyEventEventArgs (keyEvent);
KeyUp?.Invoke (args);
if (args.Handled)
return true;
if (subviews == null || subviews.Count == 0)
return false;
foreach (var view in subviews)
if (view.SuperView.IsCurrentTop && view.OnKeyUp (keyEvent))
return true;
return false;
}
///
/// Finds the first view in the hierarchy that wants to get the focus if nothing is currently focused, otherwise, it does nothing.
///
public void EnsureFocus ()
{
if (focused == null)
if (FocusDirection == Direction.Forward)
FocusFirst ();
else
FocusLast ();
}
///
/// Focuses the first focusable subview if one exists.
///
public void FocusFirst ()
{
if (subviews == null) {
SuperView?.SetFocus (this);
return;
}
foreach (var view in subviews) {
if (view.CanFocus) {
SetFocus (view);
return;
}
}
}
///
/// Focuses the last focusable subview if one exists.
///
public void FocusLast ()
{
if (subviews == null) {
SuperView?.SetFocus (this);
return;
}
for (int i = subviews.Count; i > 0;) {
i--;
View v = subviews [i];
if (v.CanFocus) {
SetFocus (v);
return;
}
}
}
///
/// Focuses the previous view.
///
/// true, if previous was focused, false otherwise.
public bool FocusPrev ()
{
FocusDirection = Direction.Backward;
if (subviews == null || subviews.Count == 0)
return false;
if (focused == null) {
FocusLast ();
return focused != null;
}
int focused_idx = -1;
for (int i = subviews.Count; i > 0;) {
i--;
View w = subviews [i];
if (w.HasFocus) {
if (w.FocusPrev ())
return true;
focused_idx = i;
continue;
}
if (w.CanFocus && focused_idx != -1) {
focused.HasFocus = false;
if (w != null && w.CanFocus)
w.FocusLast ();
SetFocus (w);
return true;
}
}
if (focused != null) {
focused.HasFocus = false;
focused = null;
}
return false;
}
///
/// Focuses the next view.
///
/// true, if next was focused, false otherwise.
public bool FocusNext ()
{
FocusDirection = Direction.Forward;
if (subviews == null || subviews.Count == 0)
return false;
if (focused == null) {
FocusFirst ();
return focused != null;
}
int n = subviews.Count;
int focused_idx = -1;
for (int i = 0; i < n; i++) {
View w = subviews [i];
if (w.HasFocus) {
if (w.FocusNext ())
return true;
focused_idx = i;
continue;
}
if (w.CanFocus && focused_idx != -1) {
focused.HasFocus = false;
if (w != null && w.CanFocus)
w.FocusFirst ();
SetFocus (w);
return true;
}
}
if (focused != null) {
focused.HasFocus = false;
focused = null;
}
return false;
}
///
/// Sets the View's to the relative coordinates if its container, given the for its container.
///
/// The screen-relative frame for the host.
///
/// Reminder: is superview-relative; is view-relative.
///
internal void SetRelativeLayout (Rect hostFrame)
{
int w, h, _x, _y;
if (x is Pos.PosCenter) {
if (width == null)
w = hostFrame.Width;
else
w = width.Anchor (hostFrame.Width);
_x = x.Anchor (hostFrame.Width - w);
} else {
if (x == null)
_x = 0;
else
_x = x.Anchor (hostFrame.Width);
if (width == null)
w = hostFrame.Width;
else
w = width.Anchor (hostFrame.Width - _x);
}
if (y is Pos.PosCenter) {
if (height == null)
h = hostFrame.Height;
else
h = height.Anchor (hostFrame.Height);
_y = y.Anchor (hostFrame.Height - h);
} else {
if (y == null)
_y = 0;
else
_y = y.Anchor (hostFrame.Height);
if (height == null)
h = hostFrame.Height;
else
h = height.Anchor (hostFrame.Height - _y);
}
Frame = new Rect (_x, _y, w, h);
}
// https://en.wikipedia.org/wiki/Topological_sorting
List TopologicalSort (HashSet nodes, HashSet<(View From, View To)> edges)
{
var result = new List ();
// Set of all nodes with no incoming edges
var S = new HashSet (nodes.Where (n => edges.All (e => e.To.Equals (n) == false)));
while (S.Any ()) {
// remove a node n from S
var n = S.First ();
S.Remove (n);
// add n to tail of L
if (n != this?.SuperView)
result.Add (n);
// for each node m with an edge e from n to m do
foreach (var e in edges.Where (e => e.From.Equals (n)).ToArray ()) {
var m = e.To;
// remove edge e from the graph
edges.Remove (e);
// if m has no other incoming edges then
if (edges.All (me => me.To.Equals (m) == false) && m != this?.SuperView) {
// insert m into S
S.Add (m);
}
}
}
// if graph has edges then
if (edges.Any ()) {
// return error (graph has at least one cycle)
return null;
} else {
// return L (a topologically sorted order)
return result;
}
}
///
/// Event arguments for the event.
///
public class LayoutEventArgs : EventArgs {
///
/// The view-relative bounds of the before it was laid out.
///
public Rect OldBounds { get; set; }
}
///
/// Fired after the Views's method has completed.
///
///
/// Subscribe to this event to perform tasks when the has been resized or the layout has otherwise changed.
///
public event Action LayoutComplete;
///
/// Raises the event. Called from after all sub-views have been laid out.
///
internal virtual void OnLayoutComplete (LayoutEventArgs args)
{
LayoutComplete?.Invoke (args);
}
///
/// 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.
///
///
/// Calls (which raises the event) before it returns.
///
public virtual void LayoutSubviews ()
{
if (!layoutNeeded)
return;
Rect oldBounds = Bounds;
// Sort out the dependencies of the X, Y, Width, Height properties
var nodes = new HashSet ();
var edges = new HashSet<(View, View)> ();
foreach (var v in InternalSubviews) {
nodes.Add (v);
if (v.LayoutStyle == LayoutStyle.Computed) {
if (v.X is Pos.PosView vX)
edges.Add ((vX.Target, v));
if (v.Y is Pos.PosView vY)
edges.Add ((vY.Target, v));
if (v.Width is Dim.DimView vWidth)
edges.Add ((vWidth.Target, v));
if (v.Height is Dim.DimView vHeight)
edges.Add ((vHeight.Target, v));
}
}
var ordered = TopologicalSort (nodes, edges);
if (ordered == null)
throw new Exception ("There is a recursive cycle in the relative Pos/Dim in the views of " + this);
foreach (var v in ordered) {
if (v.LayoutStyle == LayoutStyle.Computed)
v.SetRelativeLayout (Frame);
v.LayoutSubviews ();
v.layoutNeeded = false;
}
if (SuperView == Application.Top && layoutNeeded && ordered.Count == 0 && LayoutStyle == LayoutStyle.Computed) {
SetRelativeLayout (Frame);
}
layoutNeeded = false;
OnLayoutComplete (new LayoutEventArgs () { OldBounds = oldBounds });
}
///
/// Pretty prints the View
///
///
public override string ToString ()
{
return $"{GetType ().Name}({Id})({Frame})";
}
///
/// Specifies the event arguments for
///
public class MouseEventEventArgs : EventArgs {
///
/// Constructs.
///
///
public MouseEventEventArgs (MouseEvent me) => MouseEvent = me;
///
/// The for the event.
///
public MouseEvent MouseEvent { get; set; }
///
/// Indicates if the current mouse event has already been processed and the driver should stop notifying any other event subscriber.
/// Its important to set this value to true specially when updating any View's layout from inside the subscriber method.
///
public bool Handled { get; set; }
}
///
public override bool OnMouseEnter (MouseEvent mouseEvent)
{
MouseEventEventArgs args = new MouseEventEventArgs (mouseEvent);
MouseEnter?.Invoke (args);
if (args.Handled)
return true;
if (base.OnMouseEnter (mouseEvent))
return true;
return false;
}
///
public override bool OnMouseLeave (MouseEvent mouseEvent)
{
MouseEventEventArgs args = new MouseEventEventArgs (mouseEvent);
MouseLeave?.Invoke (args);
if (args.Handled)
return true;
if (base.OnMouseLeave (mouseEvent))
return true;
return false;
}
///
/// Method invoked when a mouse event is generated
///
///
/// true, if the event was handled, false otherwise.
public virtual bool OnMouseEvent (MouseEvent mouseEvent)
{
MouseEventEventArgs args = new MouseEventEventArgs (mouseEvent);
MouseClick?.Invoke (args);
if (args.Handled)
return true;
if (MouseEvent (mouseEvent))
return true;
return false;
}
}
}