Add SplitView to Notepad

This commit is contained in:
tznind
2023-01-21 09:33:11 +00:00
parent 4f2212d751
commit 5b5f40fd46
2 changed files with 148 additions and 42 deletions

View File

@@ -682,7 +682,7 @@ namespace Terminal.Gui {
/// <summary>
/// <para>
/// Moves <see cref="parent"/> <see cref="SplitView.SplitterDistance"/> to
/// Moves <see cref="Parent"/> <see cref="SplitView.SplitterDistance"/> to
/// <see cref="Pos"/> <paramref name="newValue"/> preserving <see cref="Pos"/> format
/// (absolute / relative) that <paramref name="oldValue"/> had.
/// </para>
@@ -734,6 +734,40 @@ namespace Terminal.Gui {
}
/// <summary>
/// Replaces <see cref="View1"/> with the provided <paramref name="view"/>.
/// The new <paramref name="view"/> will be resized to fill available
/// area and resized on <see cref="SplitterMoved"/>.
/// </summary>
/// <param name="view"></param>
public void SetView1 (View view)
{
if(View1 != null) {
Remove (View1);
}
Add (view);
View1 = view;
LayoutSubviews ();
}
/// <summary>
/// Replaces <see cref="View2"/> with the provided <paramref name="view"/>.
/// The new <paramref name="view"/> will be resized to fill available
/// area and resized on <see cref="SplitterMoved"/>.
/// </summary>
/// <param name="view"></param>
public void SetView2 (View view)
{
if (View2 != null) {
Remove (View2);
}
Add (view);
View2 = view;
LayoutSubviews ();
}
private class ChildSplitterLine {
readonly SplitContainerLineView currentLine;

View File

@@ -1,4 +1,5 @@
using System.IO;
using System;
using System.IO;
using Terminal.Gui;
namespace UICatalog.Scenarios {
@@ -43,7 +44,16 @@ namespace UICatalog.Scenarios {
tabView.Style.ShowBorder = true;
tabView.ApplyStyleChanges ();
Application.Top.Add (tabView);
// Start with only a single view but support splitting to show side by side
var split = new SplitView {
Width = Dim.Fill(),
Height = Dim.Fill(),
};
split.View2.Visible = false;
split.SetView1 (tabView);
split.IntegratedBorder = BorderStyle.None;
Application.Top.Add (split);
var lenStatusItem = new StatusItem (Key.CharMask, "Len: ", null);
var statusBar = new StatusBar (new StatusItem [] {
@@ -80,22 +90,63 @@ namespace UICatalog.Scenarios {
});
} else {
var tv = (TabView)sender;
var t = (OpenedFile)e.Tab;
items = new MenuBarItem (new MenuItem [] {
new MenuItem ($"Save", "", () => Save(e.Tab)),
new MenuItem ($"Close", "", () => Close(e.Tab)),
null,
new MenuItem ($"Split Up", "", () => SplitUp(tv,t)),
new MenuItem ($"Split Down", "", () => SplitDown(tv,t)),
new MenuItem ($"Split Right", "", () => SplitRight(tv,t)),
new MenuItem ($"Split Left", "", () => SplitLeft(tv,t)),
});
}
var contextMenu = new ContextMenu (e.MouseEvent.X + 1, e.MouseEvent.Y + 1, items);
var contextMenu = new ContextMenu (e.MouseEvent.X + 1, e.MouseEvent.Y + 1, items);
contextMenu.Show ();
e.MouseEvent.Handled = true;
}
private void SplitUp (TabView sender, OpenedFile tab)
{
}
private void SplitDown (TabView sender, OpenedFile tab)
{
}
private void SplitLeft (TabView sender, OpenedFile tab)
{
}
private void SplitRight (TabView sender, OpenedFile tab)
{
var split = (SplitView)sender.SuperView;
split.TrySplitView1 (out var sub);
sub.Orientation = Terminal.Gui.Graphs.Orientation.Vertical;
var newTabView = CreateNewTabView ();
tab.CloneTo (newTabView);
sub.SetView2 (newTabView);
}
private TabView CreateNewTabView ()
{
return new TabView () {
X = 0,
Y = 0,
Width = Dim.Fill (),
Height = Dim.Fill (),
};
}
private void New ()
{
Open ("", null, $"new {numbeOfNewTabs++}");
Open (null, $"new {numbeOfNewTabs++}");
}
private void Close ()
@@ -144,7 +195,7 @@ namespace UICatalog.Scenarios {
return;
}
Open (File.ReadAllText (path), new FileInfo (path), Path.GetFileName (path));
Open (new FileInfo (path), Path.GetFileName (path));
}
}
}
@@ -152,42 +203,11 @@ namespace UICatalog.Scenarios {
/// <summary>
/// Creates a new tab with initial text
/// </summary>
/// <param name="initialText"></param>
/// <param name="fileInfo">File that was read or null if a new blank document</param>
private void Open (string initialText, FileInfo fileInfo, string tabName)
private void Open (FileInfo fileInfo, string tabName)
{
var textView = new TextView () {
X = 0,
Y = 0,
Width = Dim.Fill (),
Height = Dim.Fill (),
Text = initialText
};
var tab = new OpenedFile (tabName, fileInfo, textView);
var tab = new OpenedFile (tabView, tabName, fileInfo);
tabView.AddTab (tab, true);
// when user makes changes rename tab to indicate unsaved
textView.KeyUp += (k) => {
// if current text doesn't match saved text
var areDiff = tab.UnsavedChanges;
if (areDiff) {
if (!tab.Text.ToString ().EndsWith ('*')) {
tab.Text = tab.Text.ToString () + '*';
tabView.SetNeedsDisplay ();
}
} else {
if (tab.Text.ToString ().EndsWith ('*')) {
tab.Text = tab.Text.ToString ().TrimEnd ('*');
tabView.SetNeedsDisplay ();
}
}
};
}
public void Save ()
@@ -243,12 +263,64 @@ namespace UICatalog.Scenarios {
public bool UnsavedChanges => !string.Equals (SavedText, View.Text.ToString ());
public OpenedFile (string name, FileInfo file, TextView control) : base (name, control)
public OpenedFile (TabView parent, string name, FileInfo file)
: base (name, CreateTextView(file))
{
File = file;
SavedText = control.Text.ToString ();
SavedText = View.Text.ToString ();
RegisterTextViewEvents (parent);
}
private void RegisterTextViewEvents (TabView parent)
{
var textView = (TextView)View;
// when user makes changes rename tab to indicate unsaved
textView.KeyUp += (k) => {
// if current text doesn't match saved text
var areDiff = this.UnsavedChanges;
if (areDiff) {
if (!this.Text.ToString ().EndsWith ('*')) {
this.Text = this.Text.ToString () + '*';
parent.SetNeedsDisplay ();
}
} else {
if (Text.ToString ().EndsWith ('*')) {
Text = Text.ToString ().TrimEnd ('*');
parent.SetNeedsDisplay ();
}
}
};
}
private static View CreateTextView (FileInfo file)
{
string initialText = string.Empty;
if(file != null && file.Exists) {
initialText = System.IO.File.ReadAllText (file.FullName);
}
return new TextView () {
X = 0,
Y = 0,
Width = Dim.Fill (),
Height = Dim.Fill (),
Text = initialText,
AllowsTab = false,
};
}
public OpenedFile CloneTo(TabView other)
{
var newTab = new OpenedFile (other, base.Text.ToString(), File);
other.AddTab (newTab, true);
return newTab;
}
internal void Save ()
{
var newText = View.Text.ToString ();