Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 23 additions & 38 deletions UICatalog/Scenarios/Notepad.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Terminal.Gui;
using static UICatalog.Scenario;

namespace UICatalog.Scenarios {

[ScenarioMetadata (Name: "Notepad", Description: "Multi tab text editor uising the TabView control.")]
[ScenarioMetadata (Name: "Notepad", Description: "Multi-tab text editor uising the TabView control.")]
[ScenarioCategory ("Controls"), ScenarioCategory ("TabView")]
public class Notepad : Scenario {

TabView tabView;
Label lblStatus;

private int numbeOfNewTabs = 1;

public override void Setup ()
// Don't create a Window, just return the top-level view
public override void Init (Toplevel top, ColorScheme colorScheme)
{
Win.Title = this.GetName ();
Win.Y = 1; // menu
Win.Height = Dim.Fill (1); // status bar
Top.LayoutSubviews ();
Application.Init ();

Top = top;
if (Top == null) {
Top = Application.Top;
}
Top.ColorScheme = Colors.Base;
}

public override void Setup ()
{
var menu = new MenuBar (new MenuBarItem [] {
new MenuBarItem ("_File", new MenuItem [] {
new MenuItem ("_New", "", () => New()),
new MenuItem ("_Open", "", () => Open()),
new MenuItem ("_Save", "", () => Save()),
new MenuItem ("_Save As", "", () => SaveAs()),
new MenuItem ("Save _As", "", () => SaveAs()),
new MenuItem ("_Close", "", () => Close()),
new MenuItem ("_Quit", "", () => Quit()),
})
Expand All @@ -39,16 +38,17 @@ public override void Setup ()

tabView = new TabView () {
X = 0,
Y = 0,
Y = 1,
Width = Dim.Fill (),
Height = Dim.Fill (1),
};

tabView.Style.ShowBorder = false;
tabView.Style.ShowBorder = true;
tabView.ApplyStyleChanges ();

Win.Add (tabView);
Top.Add (tabView);

var lenStatusItem = new StatusItem (Key.CharMask, "Len: ", null);
var statusBar = new StatusBar (new StatusItem [] {
new StatusItem(Key.CtrlMask | Key.Q, "~^Q~ Quit", () => Quit()),

Expand All @@ -58,26 +58,16 @@ public override void Setup ()

new StatusItem(Key.CtrlMask | Key.S, "~^S~ Save", () => Save()),
new StatusItem(Key.CtrlMask | Key.W, "~^W~ Close", () => Close()),
lenStatusItem,
});

Win.Add (lblStatus = new Label ("Len:") {
Y = Pos.Bottom (tabView),
Width = Dim.Fill (),
TextAlignment = TextAlignment.Right
});

tabView.SelectedTabChanged += (s, e) => UpdateStatus (e.NewTab);
tabView.SelectedTabChanged += (s, e) => lenStatusItem.Title = $"Len:{(e.NewTab?.View?.Text?.Length ?? 0)}";

Top.Add (statusBar);

New ();
}

private void UpdateStatus (TabView.Tab newTab)
{
lblStatus.Text = $"Len:{(newTab?.View?.Text?.Length ?? 0)}";
}

private void New ()
{
Open ("", null, $"new {numbeOfNewTabs++}");
Expand Down Expand Up @@ -109,12 +99,10 @@ private void Close ()
// close and dispose the tab
tabView.RemoveTab (tab);
tab.View.Dispose ();

}

private void Open ()
{

var open = new OpenDialog ("Open", "Open a file") { AllowsMultipleSelection = true };

Application.Run (open);
Expand All @@ -130,7 +118,6 @@ private void Open ()
Open (File.ReadAllText (path), new FileInfo (path), Path.GetFileName (path));
}
}

}

/// <summary>
Expand All @@ -140,7 +127,6 @@ private void Open ()
/// <param name="fileInfo">File that was read or null if a new blank document</param>
private void Open (string initialText, FileInfo fileInfo, string tabName)
{

var textView = new TextView () {
X = 0,
Y = 0,
Expand Down Expand Up @@ -188,7 +174,7 @@ public void Save ()
}

tab.Save ();

tabView.SetNeedsDisplay ();
}

public bool SaveAs ()
Expand All @@ -207,14 +193,13 @@ public bool SaveAs ()
}

tab.File = new FileInfo (fd.FilePath.ToString ());
tab.Text = fd.FileName.ToString ();
tab.Save ();

return true;
}

private class OpenedFile : TabView.Tab {


public FileInfo File { get; set; }

/// <summary>
Expand Down