12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Terminal.Gui.Graphs;
- namespace Terminal.Gui {
- /// <summary>
- /// A <see cref="View"/> consisting of a moveable bar that divides
- /// the display area into resizeable <see cref="Tiles"/>.
- /// </summary>
- public class TileView : View {
- TileView parentTileView;
- /// <summary>
- /// Use this field instead of Border to create an integrated
- /// Border in which lines connect with subviews and splitters
- /// seamlessly
- /// </summary>
- public BorderStyle IntegratedBorder { get; set; }
- /// <summary>
- /// A single <see cref="View"/> presented in a <see cref="TileView"/>. To create
- /// new instances use <see cref="TileView.RebuildForTileCount(int)"/>
- /// or <see cref="TileView.InsertTile(int)"/>.
- /// </summary>
- public class Tile {
- /// <summary>
- /// The <see cref="View"/> that is showing in this <see cref="TileView"/>.
- /// You should add new child views to this member if you want multiple
- /// <see cref="View"/> within the <see cref="Tile"/>.
- /// </summary>
- public View View { get; internal set; }
- /// <summary>
- /// Gets or Sets the minimum size you to allow when splitter resizing along
- /// parent <see cref="TileView.Orientation"/> direction.
- /// </summary>
- public int MinSize { get; set; }
- /// <summary>
- /// The text that should be displayed above the <see cref="View"/>. This
- /// will appear over the splitter line or border (above the view client area).
- /// </summary>
- /// <remarks>
- /// Title are not rendered for root level tiles if there is no
- /// <see cref="TileView.IntegratedBorder"/> to render into.
- ///</remarks>
- public string Title { get; set; }
- /// <summary>
- /// Creates a new instance of the <see cref="Tile"/> class.
- /// </summary>
- internal Tile ()
- {
- View = new View () { Width = Dim.Fill (), Height = Dim.Fill () };
- Title = string.Empty;
- MinSize = 0;
- }
- }
- List<Tile> tiles;
- private List<Pos> splitterDistances;
- private List<TileViewLineView> splitterLines;
- /// <summary>
- /// The sub sections hosted by the view
- /// </summary>
- public IReadOnlyCollection<Tile> Tiles => tiles.AsReadOnly ();
- /// <summary>
- /// The splitter locations. Note that there will be N-1 splitters where
- /// N is the number of <see cref="Tiles"/>.
- /// </summary>
- public IReadOnlyCollection<Pos> SplitterDistances => splitterDistances.AsReadOnly ();
- private Orientation orientation = Orientation.Vertical;
- /// <summary>
- /// Creates a new instance of the <see cref="TileView"/> class with
- /// 2 tiles (i.e. left and right).
- /// </summary>
- public TileView () : this (2)
- {
- }
- /// <summary>
- /// Creates a new instance of the <see cref="TileView"/> class with
- /// <paramref name="tiles"/> number of tiles.
- /// </summary>
- /// <param name="tiles"></param>
- public TileView (int tiles)
- {
- CanFocus = true;
- RebuildForTileCount (tiles);
- }
- /// <summary>
- /// Invoked when any of the <see cref="SplitterDistances"/> is changed.
- /// </summary>
- public event SplitterEventHandler SplitterMoved;
- /// <summary>
- /// Raises the <see cref="SplitterMoved"/> event
- /// </summary>
- protected virtual void OnSplitterMoved (int idx)
- {
- SplitterMoved?.Invoke (this, new SplitterEventArgs (this, idx, splitterDistances [idx]));
- }
- /// <summary>
- /// Scraps all <see cref="Tiles"/> and creates <paramref name="count"/> new tiles
- /// in orientation <see cref="Orientation"/>
- /// </summary>
- /// <param name="count"></param>
- public void RebuildForTileCount (int count)
- {
- tiles = new List<Tile> ();
- splitterDistances = new List<Pos> ();
- splitterLines = new List<TileViewLineView> ();
- RemoveAll ();
- tiles.Clear ();
- splitterDistances.Clear ();
- if (count == 0) {
- return;
- }
- for (int i = 0; i < count; i++) {
- if (i > 0) {
- var currentPos = Pos.Percent ((100 / count) * i);
- splitterDistances.Add (currentPos);
- var line = new TileViewLineView (this, i - 1);
- Add (line);
- splitterLines.Add (line);
- }
- var tile = new Tile ();
- tiles.Add (tile);
- Add (tile.View);
- }
- LayoutSubviews ();
- }
- /// <summary>
- /// Adds a new <see cref="Tile"/> to the collection at <paramref name="idx"/>.
- /// This will also add another splitter line
- /// </summary>
- /// <param name="idx"></param>
- /// <exception cref="NotImplementedException"></exception>
- public Tile InsertTile (int idx)
- {
- var oldTiles = Tiles.ToArray ();
- RebuildForTileCount (oldTiles.Length + 1);
- Tile toReturn = null;
- for(int i=0;i<tiles.Count;i++) {
-
- if(i != idx) {
- var oldTile = oldTiles [i > idx ? i - 1 : i];
- // remove the new empty View
- Remove (tiles [i].View);
-
- // restore old Tile and View
- tiles [i] = oldTile;
- Add (tiles [i].View);
- }
- else
- {
- toReturn = tiles[i];
- }
- }
- SetNeedsDisplay ();
- LayoutSubviews ();
- return toReturn;
- }
- /// <summary>
- /// Removes a <see cref="Tiles"/> at the provided <paramref name="idx"/> from
- /// the view. Returns the removed tile or null if already empty.
- /// </summary>
- /// <param name="idx"></param>
- /// <returns></returns>
- public Tile RemoveTile (int idx)
- {
- var oldTiles = Tiles.ToArray ();
-
- if (idx < 0 || idx >= oldTiles.Length) {
- return null;
- }
-
- var removed = Tiles.ElementAt (idx);
- RebuildForTileCount (oldTiles.Length - 1);
- for (int i = 0; i < tiles.Count; i++) {
- int oldIdx = i >= idx ? i + 1: i;
- var oldTile = oldTiles [oldIdx];
- // remove the new empty View
- Remove (tiles [i].View);
- // restore old Tile and View
- tiles [i] = oldTile;
- Add (tiles [i].View);
-
- }
- SetNeedsDisplay ();
- LayoutSubviews ();
- return removed;
- }
- ///<summary>
- /// Returns the index of the first <see cref="Tile"/> in
- /// <see cref="Tiles"/> which contains <paramref name="toFind"/>.
- ///</summary>
- public int IndexOf(View toFind, bool recursive = false)
- {
- for(int i = 0 ;i < tiles.Count; i++)
- {
- var v = tiles[i].View;
- if(v == toFind)
- {
- return i;
- }
-
- if(v.Subviews.Contains(toFind))
- {
- return i;
- }
- if(recursive)
- {
- if(RecursiveContains(v.Subviews,toFind))
- {
- return i;
- }
- }
- }
- return -1;
- }
- private bool RecursiveContains (IEnumerable<View> haystack, View needle)
- {
- foreach(var v in haystack)
- {
- if(v == needle)
- {
- return true;
- }
-
- if(RecursiveContains(v.Subviews,needle))
- {
- return true;
- }
- }
- return false;
- }
- /// <summary>
- /// Orientation of the dividing line (Horizontal or Vertical).
- /// </summary>
- public Orientation Orientation {
- get { return orientation; }
- set {
- orientation = value;
- LayoutSubviews ();
- }
- }
- /// <inheritdoc/>
- public override void LayoutSubviews ()
- {
- var contentArea = Bounds;
- if (HasBorder ()) {
- // TODO: Bound with Max/Min
- contentArea = new Rect (
- contentArea.X + 1,
- contentArea.Y + 1,
- Math.Max (0, contentArea.Width - 2),
- Math.Max (0, contentArea.Height - 2));
- }
- Setup (contentArea);
- base.LayoutSubviews ();
- }
- /// <summary>
- /// <para>Distance Horizontally or Vertically to the splitter line when
- /// neither view is collapsed.
- /// </para>
- /// <para>Only absolute values (e.g. 10) and percent values (i.e. <see cref="Pos.Percent(float)"/>)
- /// are supported for this property.</para>
- /// </summary>
- public void SetSplitterPos (int idx, Pos value)
- {
- if (!(value is Pos.PosAbsolute) && !(value is Pos.PosFactor)) {
- throw new ArgumentException ($"Only Percent and Absolute values are supported. Passed value was {value.GetType ().Name}");
- }
- splitterDistances [idx] = value;
- GetRootTileView ().LayoutSubviews ();
- OnSplitterMoved (idx);
- }
- /// <inheritdoc/>
- public override bool OnEnter (View view)
- {
- Driver.SetCursorVisibility (CursorVisibility.Invisible);
- return base.OnEnter (view);
- }
- /// <inheritdoc/>
- public override void Redraw (Rect bounds)
- {
- // TODO: We are getting passed stale bounds, does this only happen in TileView
- // or is it a larger problem? This line should not be required right?
- bounds = Bounds;
- Driver.SetAttribute (ColorScheme.Normal);
- Clear ();
- base.Redraw (bounds);
- var lc = new LineCanvas ();
- var allLines = GetAllLineViewsRecursively (this);
- var allTitlesToRender = GetAllTitlesToRenderRecursively(this);
- if (IsRootTileView ()) {
- if (HasBorder ()) {
- lc.AddLine (new Point (0, 0), bounds.Width - 1, Orientation.Horizontal, IntegratedBorder);
- lc.AddLine (new Point (0, 0), bounds.Height - 1, Orientation.Vertical, IntegratedBorder);
- lc.AddLine (new Point (bounds.Width - 1, bounds.Height - 1), -bounds.Width + 1, Orientation.Horizontal, IntegratedBorder);
- lc.AddLine (new Point (bounds.Width - 1, bounds.Height - 1), -bounds.Height + 1, Orientation.Vertical, IntegratedBorder);
- }
- foreach (var line in allLines) {
- bool isRoot = splitterLines.Contains (line);
- line.ViewToScreen (0, 0, out var x1, out var y1);
- var origin = ScreenToView (x1, y1);
- var length = line.Orientation == Orientation.Horizontal ?
- line.Frame.Width - 1 :
- line.Frame.Height - 1;
- if (!isRoot) {
- if (line.Orientation == Orientation.Horizontal) {
- origin.X -= 1;
- } else {
- origin.Y -= 1;
- }
- length += 2;
- }
- lc.AddLine (origin, length, line.Orientation, IntegratedBorder);
- }
- }
- Driver.SetAttribute (ColorScheme.Normal);
- lc.Draw (this, bounds);
- // Redraw the lines so that focus/drag symbol renders
- foreach (var line in allLines) {
- line.DrawSplitterSymbol ();
- }
- // Draw Titles over Border
- foreach(var titleToRender in allTitlesToRender)
- {
- var renderAt = titleToRender.GetLocalCoordinateForTitle(this);
- if(renderAt.Y < 0)
- {
- // If we have no border then root level tiles
- // have nowhere to render their titles.
- continue;
- }
- // TODO: Render with focus color if focused
- var title = titleToRender.Tile.Title;
- for(int i=0;i<title.Length;i++)
- {
- AddRune(renderAt.X+i,renderAt.Y,title[i]);
- }
- }
- }
- /// <summary>
- /// Converts <see cref="Tile.View"/> of <see cref="Tiles"/> element <paramref name="idx"/>
- /// from a regular <see cref="View"/> to a new nested <see cref="TileView"/> with <paramref name="panels"/>
- /// number of panels. Returns false if the element already contains a nested view.
- /// </summary>
- /// <remarks>After successful splitting, the <paramref name="result"/> <see cref="TileView"/>
- /// will contain the previous (replaced) <see cref="Tile.View"/> at element 0.</remarks>
- /// <param name="idx">The element of <see cref="Tiles"/> that is to be subdivided.</param>
- /// <param name="panels">The number of panels that the <see cref="Tile"/> should be split into</param>
- /// <param name="result">The new nested <see cref="TileView"/>.</param>
- /// <returns><see langword="true"/> if a <see cref="View"/> was converted to a new nested
- /// <see cref="TileView"/>. <see langword="false"/> if it was already a nested
- /// <see cref="TileView"/></returns>
- public bool TrySplitTile(int idx, int panels, out TileView result)
- {
- // when splitting a view into 2 sub views we will need to migrate
- // the title too
- var tile = tiles [idx];
- // TODO: migrate the title too right?
- var title = tile.Title;
- View toMove = tile.View;
- if (toMove is TileView existing) {
- result = existing;
- return false;
- }
- var newContainer = new TileView(panels) {
- Width = Dim.Fill (),
- Height = Dim.Fill (),
- parentTileView = this,
- };
-
- // Take everything out of the View we are moving
- var childViews = toMove.Subviews.ToArray();
- toMove.RemoveAll ();
- // Remove the view itself and replace it with the new TileView
- Remove (toMove);
- Add (newContainer);
- tile.View = newContainer;
- var newTileView1 = newContainer.tiles [0].View;
- // Add the original content into the first view of the new container
- foreach (var childView in childViews) {
- newTileView1.Add (childView);
- }
- result = newContainer;
- return true;
- }
- private List<TileViewLineView> GetAllLineViewsRecursively (View v)
- {
- var lines = new List<TileViewLineView> ();
- foreach (var sub in v.Subviews) {
- if (sub is TileViewLineView s) {
- if (s.Visible && s.Parent.GetRootTileView () == this) {
- lines.Add (s);
- }
- } else {
- if(sub.Visible) {
- lines.AddRange (GetAllLineViewsRecursively (sub));
- }
- }
- }
- return lines;
- }
- private List<TileTitleToRender> GetAllTitlesToRenderRecursively (TileView v, int depth = 0)
- {
- var titles = new List<TileTitleToRender> ();
- foreach (var sub in v.Tiles) {
- // Don't render titles for invisible stuff!
- if(!sub.View.Visible)
- {
- continue;
- }
- if(sub.View is TileView subTileView)
- {
- // Panels with sub split tiles in them can never
- // have their Titles rendered. Instead we dive in
- // and pull up their children as titles
- titles.AddRange (GetAllTitlesToRenderRecursively (subTileView,depth+1));
- }
- else
- {
- if(sub.Title.Length > 0)
- {
- titles.Add(new TileTitleToRender(sub,depth));
- }
- }
- }
- return titles;
- }
- /// <summary>
- /// <para>
- /// <see langword="true"/> if <see cref="TileView"/> is nested within a parent <see cref="TileView"/>
- /// e.g. via the <see cref="TrySplitTile"/>. <see langword="false"/> if it is a root level <see cref="TileView"/>.
- /// </para>
- /// </summary>
- /// <remarks>Note that manually adding one <see cref="TileView"/> to another will not result in a parent/child
- /// relationship and both will still be considered 'root' containers. Always use
- /// <see cref="TrySplitTile(int, int, out TileView)"/> if you want to subdivide a <see cref="TileView"/>.</remarks>
- /// <returns></returns>
- public bool IsRootTileView ()
- {
- // TODO: don't want to layout subviews since the parent recursively lays them all out
- return parentTileView == null;
- }
- /// <summary>
- /// Returns the immediate parent <see cref="TileView"/> of this. Note that in case
- /// of deep nesting this might not be the root <see cref="TileView"/>. Returns null
- /// if this instance is not a nested child (created with
- /// <see cref="TrySplitTile(int, int, out TileView)"/>)
- /// </summary>
- /// <remarks>
- /// Use <see cref="IsRootTileView"/> to determine if the returned value is the root.
- /// </remarks>
- /// <returns></returns>
- public TileView GetParentTileView ()
- {
- return this.parentTileView;
- }
- private TileView GetRootTileView ()
- {
- TileView root = this;
- while (root.parentTileView != null) {
- root = root.parentTileView;
- }
- return root;
- }
- private void Setup (Rect bounds)
- {
- if (bounds.IsEmpty || bounds.Height <= 0 || bounds.Width <= 0) {
- return;
- }
- RespectMinimumTileSizes ();
- for (int i = 0; i < splitterLines.Count; i++) {
- var line = splitterLines[i];
- line.Orientation = Orientation;
- line.Width = orientation == Orientation.Vertical
- ? 1 : Dim.Fill ();
- line.Height = orientation == Orientation.Vertical
- ? Dim.Fill () : 1;
- line.LineRune = orientation == Orientation.Vertical ?
- Driver.VLine : Driver.HLine;
- if (orientation == Orientation.Vertical) {
- line.X = splitterDistances [i];
- line.Y = 0;
- }
- else {
- line.Y = splitterDistances [i];
- line.X = 0;
- }
- }
- HideSplittersBasedOnTileVisibility ();
- var visibleTiles = tiles.Where (t => t.View.Visible).ToArray ();
- var visibleSplitterLines = splitterLines.Where (l => l.Visible).ToArray ();
- for (int i = 0; i < visibleTiles.Length; i++) {
- var tile = visibleTiles [i];
- // TODO: Deal with lines being Visibility false
- if (Orientation == Orientation.Vertical) {
- tile.View.X = i == 0 ? bounds.X : Pos.Right (visibleSplitterLines [i - 1]);
- tile.View.Y = bounds.Y;
- tile.View.Height = bounds.Height;
- tile.View.Width = GetTileWidthOrHeight(i, Bounds.Width, visibleTiles,visibleSplitterLines);
- } else {
- tile.View.X = bounds.X;
- tile.View.Y = i == 0 ? 0 : Pos.Bottom (visibleSplitterLines [i - 1]);
- tile.View.Width = bounds.Width;
- tile.View.Height = GetTileWidthOrHeight(i, Bounds.Height, visibleTiles, visibleSplitterLines);
- }
- }
- }
- private void HideSplittersBasedOnTileVisibility ()
- {
- if(splitterLines.Count == 0) {
- return;
- }
- foreach(var line in splitterLines) {
- line.Visible = true;
- }
- for(int i=0;i<tiles.Count;i++) {
- if (!tiles [i].View.Visible) {
- // when a tile is not visible, prefer hiding
- // the splitter on it's left
- var candidate = splitterLines [Math.Max (0, i - 1)];
- // unless that splitter is already hidden
- // e.g. when hiding panels 0 and 1 of a 3 panel
- // container
- if (candidate.Visible) {
- candidate.Visible = false;
- }
- else {
- splitterLines [Math.Min(i,splitterLines.Count-1)].Visible = false;
- }
-
-
- }
- }
- }
- private Dim GetTileWidthOrHeight (int i, int space, Tile[] visibleTiles, TileViewLineView [] visibleSplitterLines)
- {
- // last tile
- if(i + 1 >= visibleTiles.Length)
- {
- return Dim.Fill (HasBorder () ? 1 : 0);
- }
- var nextSplitter = visibleSplitterLines [i];
- var nextSplitterPos = Orientation == Orientation.Vertical ?
- nextSplitter.X : nextSplitter.Y;
- var nextSplitterDistance = nextSplitterPos.Anchor (space);
- var lastSplitter = i >= 1 ? visibleSplitterLines [i-1]:null;
- var lastSplitterPos = Orientation == Orientation.Vertical ?
- lastSplitter?.X : lastSplitter?.Y;
- var lastSplitterDistance = lastSplitterPos?.Anchor (space) ?? 0;
- var distance = nextSplitterDistance - lastSplitterDistance;
- if(i>0) {
- return distance - 1;
- }
- return distance - (HasBorder() ? 1 : 0);
- }
- private void RespectMinimumTileSizes ()
- {
- // if we are not yet initialized then we don't know
- // how big we are and therefore cannot sensibly calculate
- // how big the views will be with a given SplitterDistance
- if (!IsInitialized) {
- return;
- }
- // how much space is there?
- var availableSpace = Orientation == Orientation.Horizontal
- ? this.Bounds.Height
- : this.Bounds.Width;
-
- var fullSpace = availableSpace;
- var lastSplitterLocation = 0;
- for(int i=0;i< splitterDistances.Count; i++) {
- var splitterLocation = splitterDistances [i].Anchor(fullSpace);
- var availableLeft = splitterLocation - lastSplitterLocation;
- // Border steals space
- availableLeft -= HasBorder () && i == 0 ? 1 : 0;
- var availableRight = fullSpace - splitterLocation;
- // Border steals space
- availableRight -= HasBorder () && i == 0 ? 1 : 0;
- // Splitter line steals space
- availableRight--;
- // TODO: Test 3+ panel max/mins because this calculation is probably wrong
- var requiredLeft = tiles [i].MinSize;
- var requiredRight = tiles [i+1].MinSize;
- if (availableLeft < requiredLeft) {
- // There is not enough space for panel on left
- var insteadTake = requiredLeft + (HasBorder() ? 1 :0);
- // Don't take more than the available space in view
- insteadTake = Math.Max(0,Math.Min (fullSpace, insteadTake));
- splitterDistances [i] = insteadTake;
- splitterLocation = insteadTake;
- }
- else if (availableRight < requiredRight) {
- // There is not enough space for panel on right
- var insteadTake = fullSpace - (requiredRight + (HasBorder()?1:0));
- // leave 1 space for the splitter
- insteadTake --;
- insteadTake = Math.Max (0, Math.Min (fullSpace, insteadTake));
- splitterDistances [i] = insteadTake;
- splitterLocation = insteadTake;
- }
- availableSpace -= splitterLocation;
- lastSplitterLocation = splitterLocation;
- }
- }
- private class TileTitleToRender
- {
- public Tile Tile {get;}
- public int Depth {get;}
- public TileTitleToRender(Tile tile, int depth)
- {
- Tile = tile;
- Depth = depth;
- }
-
- /// <summary>
- /// Translates the <see cref="Tile"/> title location from its local
- /// coordinate space <paramref name="intoCoordinateSpace"/>.
- /// </summary>
- public Point GetLocalCoordinateForTitle(TileView intoCoordinateSpace)
- {
- Tile.View.ViewToScreen(0,0, out var screenCol, out var screenRow);
- screenRow--;
- return intoCoordinateSpace.ScreenToView(screenCol,screenRow);
- }
- }
- private class TileViewLineView : LineView {
- public TileView Parent { get; private set; }
- public int Idx { get; }
- Point? dragPosition;
- Pos dragOrignalPos;
- public Point? moveRuneRenderLocation;
- public TileViewLineView (TileView parent, int idx)
- {
- CanFocus = true;
- TabStop = true;
- this.Parent = parent;
- Idx = idx;
- base.AddCommand (Command.Right, () => {
- return MoveSplitter (1, 0);
- });
- base.AddCommand (Command.Left, () => {
- return MoveSplitter (-1, 0);
- });
- base.AddCommand (Command.LineUp, () => {
- return MoveSplitter (0, -1);
- });
- base.AddCommand (Command.LineDown, () => {
- return MoveSplitter (0, 1);
- });
- AddKeyBinding (Key.CursorRight, Command.Right);
- AddKeyBinding (Key.CursorLeft, Command.Left);
- AddKeyBinding (Key.CursorUp, Command.LineUp);
- AddKeyBinding (Key.CursorDown, Command.LineDown);
- }
- public override bool ProcessKey (KeyEvent kb)
- {
- if (!CanFocus || !HasFocus) {
- return base.ProcessKey (kb);
- }
- var result = InvokeKeybindings (kb);
- if (result != null)
- return (bool)result;
- return base.ProcessKey (kb);
- }
- public override void PositionCursor ()
- {
- base.PositionCursor ();
- var location = moveRuneRenderLocation ??
- new Point (Bounds.Width / 2, Bounds.Height / 2);
- Move (location.X, location.Y);
- }
- public override bool OnEnter (View view)
- {
- Driver.SetCursorVisibility (CursorVisibility.Default);
- PositionCursor ();
- return base.OnEnter (view);
- }
- public override void Redraw (Rect bounds)
- {
- base.Redraw (bounds);
- DrawSplitterSymbol ();
- }
- public void DrawSplitterSymbol ()
- {
- if (CanFocus && HasFocus) {
- var location = moveRuneRenderLocation ??
- new Point (Bounds.Width / 2, Bounds.Height / 2);
- AddRune (location.X, location.Y, Driver.Diamond);
- }
- }
- public override bool MouseEvent (MouseEvent mouseEvent)
- {
- if (!CanFocus) {
- return true;
- }
- if (!dragPosition.HasValue && (mouseEvent.Flags == MouseFlags.Button1Pressed)) {
- // Start a Drag
- SetFocus ();
- Application.EnsuresTopOnFront ();
- if (mouseEvent.Flags == MouseFlags.Button1Pressed) {
- dragPosition = new Point (mouseEvent.X, mouseEvent.Y);
- dragOrignalPos = Orientation == Orientation.Horizontal ? Y : X;
- Application.GrabMouse (this);
- if (Orientation == Orientation.Horizontal) {
- } else {
- moveRuneRenderLocation = new Point (0, Math.Max (1, Math.Min (Bounds.Height - 2, mouseEvent.Y)));
- }
- }
- return true;
- } else if (
- dragPosition.HasValue &&
- (mouseEvent.Flags == (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))) {
- // Continue Drag
- // how far has user dragged from original location?
- if (Orientation == Orientation.Horizontal) {
- int dy = mouseEvent.Y - dragPosition.Value.Y;
- Parent.splitterDistances [Idx] = Offset (Y, dy);
- moveRuneRenderLocation = new Point (mouseEvent.X, 0);
- } else {
- int dx = mouseEvent.X - dragPosition.Value.X;
- Parent.splitterDistances [Idx] = Offset (X, dx);
- moveRuneRenderLocation = new Point (0, Math.Max (1, Math.Min (Bounds.Height - 2, mouseEvent.Y)));
- }
- Parent.SetNeedsDisplay ();
- return true;
- }
- if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Released) && dragPosition.HasValue) {
- // End Drag
- Application.UngrabMouse ();
- Driver.UncookMouse ();
- FinalisePosition (
- dragOrignalPos,
- Orientation == Orientation.Horizontal ? Y : X);
- dragPosition = null;
- moveRuneRenderLocation = null;
- }
- return false;
- }
- private bool MoveSplitter (int distanceX, int distanceY)
- {
- if (Orientation == Orientation.Vertical) {
- // Cannot move in this direction
- if (distanceX == 0) {
- return false;
- }
- var oldX = X;
- FinalisePosition (oldX, (Pos)Offset (X, distanceX));
- return true;
- } else {
- // Cannot move in this direction
- if (distanceY == 0) {
- return false;
- }
- var oldY = Y;
- FinalisePosition (oldY, (Pos)Offset (Y, distanceY));
- return true;
- }
- }
- private Pos Offset (Pos pos, int delta)
- {
- var posAbsolute = pos.Anchor (Orientation == Orientation.Horizontal ?
- Parent.Bounds.Height : Parent.Bounds.Width);
- return posAbsolute + delta;
- }
- /// <summary>
- /// <para>
- /// Moves <see cref="Parent"/> <see cref="TileView.SplitterDistances"/> to
- /// <see cref="Pos"/> <paramref name="newValue"/> preserving <see cref="Pos"/> format
- /// (absolute / relative) that <paramref name="oldValue"/> had.
- /// </para>
- /// <remarks>This ensures that if splitter location was e.g. 50% before and you move it
- /// to absolute 5 then you end up with 10% (assuming a parent had 50 width). </remarks>
- /// </summary>
- /// <param name="oldValue"></param>
- /// <param name="newValue"></param>
- private void FinalisePosition (Pos oldValue, Pos newValue)
- {
- if (oldValue is Pos.PosFactor) {
- if (Orientation == Orientation.Horizontal) {
- Parent.SetSplitterPos(Idx, ConvertToPosFactor (newValue, Parent.Bounds.Height));
- } else {
- Parent.SetSplitterPos (Idx, ConvertToPosFactor (newValue, Parent.Bounds.Width));
- }
- } else {
- Parent.SetSplitterPos (Idx, newValue);
- }
- }
- /// <summary>
- /// <para>
- /// Determines the absolute position of <paramref name="p"/> and
- /// returns a <see cref="Pos.PosFactor"/> that describes the percentage of that.
- /// </para>
- /// <para>Effectively turning any <see cref="Pos"/> into a <see cref="Pos.PosFactor"/>
- /// (as if created with <see cref="Pos.Percent(float)"/>)</para>
- /// </summary>
- /// <param name="p">The <see cref="Pos"/> to convert to <see cref="Pos.Percent(float)"/></param>
- /// <param name="parentLength">The Height/Width that <paramref name="p"/> lies within</param>
- /// <returns></returns>
- private Pos ConvertToPosFactor (Pos p, int parentLength)
- {
- // calculate position in the 'middle' of the cell at p distance along parentLength
- float position = p.Anchor (parentLength) + 0.5f;
- return new Pos.PosFactor (position / parentLength);
- }
- }
- private bool HasBorder ()
- {
- return IntegratedBorder != BorderStyle.None;
- }
- }
- /// <summary>
- /// Provides data for <see cref="TileView"/> events.
- /// </summary>
- public class SplitterEventArgs : EventArgs {
- /// <summary>
- /// Creates a new instance of the <see cref="SplitterEventArgs"/> class.
- /// </summary>
- /// <param name="tileView"><see cref="TileView"/> in which splitter is being moved.</param>
- /// <param name="idx">Index of the splitter being moved in <see cref="TileView.SplitterDistances"/>.</param>
- /// <param name="splitterDistance">The new <see cref="Pos"/> of the splitter line.</param>
- public SplitterEventArgs (TileView tileView, int idx, Pos splitterDistance)
- {
- SplitterDistance = splitterDistance;
- TileView = tileView;
- Idx = idx;
- }
- /// <summary>
- /// New position of the splitter line (see <see cref="TileView.SplitterDistances"/>).
- /// </summary>
- public Pos SplitterDistance { get; }
- /// <summary>
- /// Container (sender) of the event.
- /// </summary>
- public TileView TileView { get; }
- /// <summary>
- /// Gets the index of the splitter that is being moved. This can be
- /// used to index <see cref="TileView.SplitterDistances"/>
- /// </summary>
- public int Idx { get; }
- }
- /// <summary>
- /// Represents a method that will handle splitter events.
- /// </summary>
- public delegate void SplitterEventHandler (object sender, SplitterEventArgs e);
- }
|