forked from gui-cs/Terminal.Gui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TileView.cs
1149 lines (942 loc) · 31.8 KB
/
TileView.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using NStack;
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>
/// The keyboard key that the user can press to toggle resizing
/// of splitter lines. Mouse drag splitting is always enabled.
/// </summary>
public Key ToggleResizable { get; set; } = Key.CtrlMask | Key.F10;
/// <summary>
/// A single <see cref="ContentView"/> 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="ContentView"/> that is contained in this <see cref="TileView"/>.
/// Add new child views to this member for multiple
/// <see cref="ContentView"/>s within the <see cref="Tile"/>.
/// </summary>
public View ContentView { 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="ContentView"/>. This
/// will appear over the splitter line or border (above the view client area).
/// </summary>
/// <remarks>
/// Title are not rendered for root level tiles
/// <see cref="Border.BorderStyle"/> is <see cref="BorderStyle.None"/>.
///</remarks>
public string Title {
get => _title;
set {
if (!OnTitleChanging (_title, value)) {
var old = _title;
_title = value;
OnTitleChanged (old, _title);
return;
}
_title = value;
}
}
private string _title = string.Empty;
/// <summary>
/// An <see cref="EventArgs"/> which allows passing a cancelable new <see cref="Title"/> value event.
/// </summary>
public class TitleEventArgs : EventArgs {
/// <summary>
/// The new Window Title.
/// </summary>
public ustring NewTitle { get; set; }
/// <summary>
/// The old Window Title.
/// </summary>
public ustring OldTitle { get; set; }
/// <summary>
/// Flag which allows cancelling the Title change.
/// </summary>
public bool Cancel { get; set; }
/// <summary>
/// Initializes a new instance of <see cref="TitleEventArgs"/>
/// </summary>
/// <param name="oldTitle">The <see cref="Title"/> that is/has been replaced.</param>
/// <param name="newTitle">The new <see cref="Title"/> to be replaced.</param>
public TitleEventArgs (ustring oldTitle, ustring newTitle)
{
OldTitle = oldTitle;
NewTitle = newTitle;
}
}
/// <summary>
/// Called before the <see cref="Title"/> changes. Invokes the <see cref="TitleChanging"/> event, which can be cancelled.
/// </summary>
/// <param name="oldTitle">The <see cref="Title"/> that is/has been replaced.</param>
/// <param name="newTitle">The new <see cref="Title"/> to be replaced.</param>
/// <returns><c>true</c> if an event handler cancelled the Title change.</returns>
public virtual bool OnTitleChanging (ustring oldTitle, ustring newTitle)
{
var args = new TitleEventArgs (oldTitle, newTitle);
TitleChanging?.Invoke (args);
return args.Cancel;
}
/// <summary>
/// Event fired when the <see cref="Title"/> is changing. Set <see cref="TitleEventArgs.Cancel"/> to
/// <c>true</c> to cancel the Title change.
/// </summary>
public event Action<TitleEventArgs> TitleChanging;
/// <summary>
/// Called when the <see cref="Title"/> has been changed. Invokes the <see cref="TitleChanged"/> event.
/// </summary>
/// <param name="oldTitle">The <see cref="Title"/> that is/has been replaced.</param>
/// <param name="newTitle">The new <see cref="Title"/> to be replaced.</param>
public virtual void OnTitleChanged (ustring oldTitle, ustring newTitle)
{
var args = new TitleEventArgs (oldTitle, newTitle);
TitleChanged?.Invoke (args);
}
/// <summary>
/// Event fired after the <see cref="Title"/> has been changed.
/// </summary>
public event Action<TitleEventArgs> TitleChanged;
/// <summary>
/// Creates a new instance of the <see cref="Tile"/> class.
/// </summary>
public Tile ()
{
ContentView = new View () { Width = Dim.Fill (), Height = Dim.Fill () };
#if DEBUG_IDISPOSABLE
ContentView.Data = "Tile.ContentView";
#endif
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)
{
RebuildForTileCount (tiles);
IgnoreBorderPropertyOnRedraw = true;
Border = new Border () {
BorderStyle = BorderStyle.None
};
}
/// <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> ();
if (splitterLines != null) {
foreach (var sl in splitterLines) {
sl.Dispose ();
}
}
splitterLines = new List<TileViewLineView> ();
RemoveAll ();
foreach (var tile in tiles) {
tile.ContentView.Dispose ();
tile.ContentView = null;
}
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.ContentView);
tile.TitleChanged += (e) => SetNeedsDisplay ();
}
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>
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].ContentView);
tiles [i].ContentView.Dispose ();
tiles [i].ContentView = null;
// restore old Tile and View
tiles [i] = oldTile;
Add (tiles [i].ContentView);
} 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].ContentView);
tiles [i].ContentView.Dispose ();
tiles [i].ContentView = null;
// restore old Tile and View
tiles [i] = oldTile;
Add (tiles [i].ContentView);
}
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].ContentView;
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 ()) {
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>Attempts to update the <see cref="splitterDistances"/> of line at <paramref name="idx"/>
/// to the new <paramref name="value"/>. Returns false if the new position is not allowed because of
/// <see cref="Tile.MinSize"/>, location of other splitters etc.
/// </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 bool 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}");
}
var fullSpace = orientation == Orientation.Vertical ? Bounds.Width : Bounds.Height;
if (fullSpace != 0 && !IsValidNewSplitterPos (idx, value, fullSpace)) {
return false;
}
splitterDistances [idx] = value;
GetRootTileView ().LayoutSubviews ();
OnSplitterMoved (idx);
return true;
}
/// <inheritdoc/>
public override void Redraw (Rect 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, Border.BorderStyle);
lc.AddLine (new Point (0, 0), bounds.Height - 1, Orientation.Vertical, Border.BorderStyle);
lc.AddLine (new Point (bounds.Width - 1, bounds.Height - 1), -bounds.Width + 1, Orientation.Horizontal, Border.BorderStyle);
lc.AddLine (new Point (bounds.Width - 1, bounds.Height - 1), -bounds.Height + 1, Orientation.Vertical, Border.BorderStyle);
}
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, Border.BorderStyle);
}
}
Driver.SetAttribute (ColorScheme.Normal);
foreach (var p in lc.GenerateImage (bounds)) {
this.AddRune (p.Key.X, p.Key.Y, p.Value);
}
// 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.GetTrimmedTitle ();
for (int i = 0; i < title.Length; i++) {
AddRune (renderAt.X + i, renderAt.Y, title [i]);
}
}
}
/// <summary>
/// Converts of <see cref="Tiles"/> element <paramref name="idx"/>
/// from a regular <see cref="View"/> to a new nested <see cref="TileView"/>
/// the specified <paramref name="numberOfPanels"/>.
/// Returns false if the element already contains a nested view.
/// </summary>
/// <remarks>After successful splitting, the old contents will be moved to the
/// <paramref name="result"/> <see cref="TileView"/>'s first tile.</remarks>
/// <param name="idx">The element of <see cref="Tiles"/> that is to be subdivided.</param>
/// <param name="numberOfPanels">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 numberOfPanels, out TileView result)
{
// when splitting a view into 2 sub views we will need to migrate
// the title too
var tile = tiles [idx];
var title = tile.Title;
View toMove = tile.ContentView;
if (toMove is TileView existing) {
result = existing;
return false;
}
var newContainer = new TileView (numberOfPanels) {
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);
toMove.Dispose ();
toMove = null;
Add (newContainer);
tile.ContentView = newContainer;
var newTileView1 = newContainer.tiles [0].ContentView;
// Add the original content into the first view of the new container
foreach (var childView in childViews) {
newTileView1.Add (childView);
}
// Move the title across too
newContainer.tiles [0].Title = title;
tile.Title = string.Empty;
result = newContainer;
return true;
}
/// <inheritdoc/>
public override bool ProcessHotKey (KeyEvent keyEvent)
{
bool focusMoved = false;
if(keyEvent.Key == ToggleResizable) {
foreach(var l in splitterLines) {
var iniBefore = l.IsInitialized;
l.IsInitialized = false;
l.CanFocus = !l.CanFocus;
l.IsInitialized = iniBefore;
if (l.CanFocus && !focusMoved) {
l.SetFocus ();
focusMoved = true;
}
}
return true;
}
return base.ProcessHotKey (keyEvent);
}
private bool IsValidNewSplitterPos (int idx, Pos value, int fullSpace)
{
int newSize = value.Anchor (fullSpace);
bool isGettingBigger = newSize > splitterDistances [idx].Anchor (fullSpace);
int lastSplitterOrBorder = HasBorder () ? 1 : 0;
int nextSplitterOrBorder = HasBorder () ? fullSpace - 1 : fullSpace;
// Cannot move off screen right
if (newSize >= fullSpace - (HasBorder () ? 1 : 0)) {
if (isGettingBigger) {
return false;
}
}
// Cannot move off screen left
if (newSize < (HasBorder () ? 1 : 0)) {
if (!isGettingBigger) {
return false;
}
}
// Do not allow splitter to move left of the one before
if (idx > 0) {
int posLeft = splitterDistances [idx - 1].Anchor (fullSpace);
if (newSize <= posLeft) {
return false;
}
lastSplitterOrBorder = posLeft;
}
// Do not allow splitter to move right of the one after
if (idx + 1 < splitterDistances.Count) {
int posRight = splitterDistances [idx + 1].Anchor (fullSpace);
if (newSize >= posRight) {
return false;
}
nextSplitterOrBorder = posRight;
}
if (isGettingBigger) {
var spaceForNext = nextSplitterOrBorder - newSize;
// space required for the last line itself
if (idx > 0) {
spaceForNext--;
}
// don't grow if it would take us below min size of right panel
if (spaceForNext < tiles [idx + 1].MinSize) {
return false;
}
} else {
var spaceForLast = newSize - lastSplitterOrBorder;
// space required for the line itself
if (idx > 0) {
spaceForLast--;
}
// don't shrink if it would take us below min size of left panel
if (spaceForLast < tiles [idx].MinSize) {
return false;
}
}
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.ContentView.Visible) {
continue;
}
if (sub.ContentView 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 (v, 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 ()
{
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;
}
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.ContentView.Visible).ToArray ();
var visibleSplitterLines = splitterLines.Where (l => l.Visible).ToArray ();
for (int i = 0; i < visibleTiles.Length; i++) {
var tile = visibleTiles [i];
if (Orientation == Orientation.Vertical) {
tile.ContentView.X = i == 0 ? bounds.X : Pos.Right (visibleSplitterLines [i - 1]);
tile.ContentView.Y = bounds.Y;
tile.ContentView.Height = bounds.Height;
tile.ContentView.Width = GetTileWidthOrHeight (i, Bounds.Width, visibleTiles, visibleSplitterLines);
} else {
tile.ContentView.X = bounds.X;
tile.ContentView.Y = i == 0 ? bounds.Y : Pos.Bottom (visibleSplitterLines [i - 1]);
tile.ContentView.Width = bounds.Width;
tile.ContentView.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].ContentView.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 class TileTitleToRender {
public TileView Parent { get; }
public Tile Tile { get; }
public int Depth { get; }
public TileTitleToRender (TileView parent, Tile tile, int depth)
{
Parent = parent;
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.ContentView.ViewToScreen (0, 0, out var screenCol, out var screenRow);
screenRow--;
return intoCoordinateSpace.ScreenToView (screenCol, screenRow);
}
internal string GetTrimmedTitle ()
{
Dim spaceDim = Tile.ContentView.Width;
var spaceAbs = spaceDim.Anchor (Parent.Bounds.Width);
var title = $" {Tile.Title} ";
if (title.Length > spaceAbs) {
return title.Substring (0, spaceAbs);
}
return title;
}
}
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 = false;
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 (dragPosition != null || CanFocus) {
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 (!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.SetSplitterPos (Idx, Offset (Y, dy));
moveRuneRenderLocation = new Point (mouseEvent.X, 0);
} else {
int dx = mouseEvent.X - dragPosition.Value.X;
Parent.SetSplitterPos (Idx, Offset (X, dx));
moveRuneRenderLocation = new Point (0, Math.Max (1, Math.Min (Bounds.Height - 2, mouseEvent.Y)));
}
Parent.SetNeedsDisplay ();
return true;
}