Skip to content
Draft
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<PackageVersion Include="ReportGenerator" Version="5.5.9" />
<PackageVersion Include="coverlet.collector" Version="10.0.0" />
<PackageVersion Include="GitVersion.MsBuild" Version="6.7.0" />
<PackageVersion Include="Terminal.Gui.Editor" Version="2.1.1-develop.75" />
</ItemGroup>
<ItemGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<PackageVersion Include="Terminal.Gui" Version="2.0.0" />
Expand Down
81 changes: 40 additions & 41 deletions Examples/UICatalog/Scenarios/Notepad.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace UICatalog.Scenarios;
[ScenarioMetadata ("Notepad", "Multi-tab text editor using the Tabs control.")]
[ScenarioCategory ("Controls")]
[ScenarioCategory ("Tabs")]
[ScenarioCategory ("TextView")]
[ScenarioCategory ("TextViewEditor")]
public class Notepad : Scenario
{
private IApplication? _app;
Expand Down Expand Up @@ -271,8 +271,8 @@ private void Open (FileInfo? fileInfo, string tabName)
}

OpenedFile tab = new (this) { Title = tabName, File = fileInfo };
tab.CreateAndAddTextView (fileInfo);
tab.RegisterTextViewEvents ();
tab.CreateAndAddEditor (fileInfo);
tab.RegisterEditorEvents ();

_focusedTabs.Add (tab);
_focusedTabs.Value = tab;
Expand Down Expand Up @@ -344,7 +344,7 @@ private int GetSelectedTextLength ()
{
if (_focusedTabs?.Value is OpenedFile tab)
{
return tab.TextView?.Text.Length ?? 0;
return tab.Editor?.Text.Length ?? 0;
}

return 0;
Expand All @@ -360,7 +360,7 @@ private void Tabs_ValueChanged (object? sender, ValueChangedEventArgs<View?> e)

if (e.NewValue is OpenedFile tab)
{
len = tab.TextView?.Text.Length ?? 0;
len = tab.Editor?.Text.Length ?? 0;
}

LenShortcut.Title = $"Len:{len}";
Expand All @@ -376,16 +376,16 @@ private class OpenedFile (Notepad notepad) : View
public FileInfo? File { get; set; }

/// <summary>Gets whether this tab is a pristine new document — never opened to a file and has no content.</summary>
public bool IsPristine => File is null && string.IsNullOrEmpty (TextView?.Text);
public bool IsPristine => File is null && string.IsNullOrEmpty (Editor?.Text);

public TextView? TextView { get; private set; }
public TextViewEditor? Editor { get; private set; }

/// <summary>The text of the tab the last time it was saved.</summary>
private string? _savedText;

public bool UnsavedChanges => TextView is { } && !string.Equals (_savedText, TextView.Text);
public bool UnsavedChanges => Editor is { } && !string.Equals (_savedText, Editor.Text);

public void CreateAndAddTextView (FileInfo? file)
public void CreateAndAddEditor (FileInfo? file)
{
var initialText = string.Empty;

Expand All @@ -394,25 +394,24 @@ public void CreateAndAddTextView (FileInfo? file)
initialText = System.IO.File.ReadAllText (file.FullName);
}

TextView = new TextView
Editor = new TextViewEditor
{
X = 0,
Y = 0,
Width = Dim.Fill (),
Height = Dim.Fill (),
Text = initialText,
TabKeyAddsTab = false
Text = initialText
};

_savedText = initialText;

Add (TextView);
Add (Editor);
}

/// <summary>Loads a file into an existing tab, replacing its content.</summary>
public void LoadFile (FileInfo file)
{
if (TextView is null)
if (Editor is null)
{
return;
}
Expand All @@ -426,49 +425,49 @@ public void LoadFile (FileInfo file)

// Set _savedText first so the ContentsChanged handler sees matching text (not dirty).
_savedText = text;
TextView.Text = text;
Editor.Text = text;
}

public void RegisterTextViewEvents ()
public void RegisterEditorEvents ()
{
if (TextView is null)
if (Editor is null)
{
return;
}

// when user makes changes rename tab to indicate unsaved
TextView.ContentsChanged += (_, _) =>
{
// if current text doesn't match saved text
bool areDiff = UnsavedChanges;

if (areDiff)
{
if (!Title.EndsWith ('*'))
{
Title = Title + "*";
}
}
else
{
if (Title.EndsWith ('*'))
{
Title = Title.TrimEnd ('*');
}
}

notepad.LenShortcut?.Title = $"Len:{TextView.Text.Length}";
};
Editor.ContentsChanged += (_, _) =>
{
// if current text doesn't match saved text
bool areDiff = UnsavedChanges;

if (areDiff)
{
if (!Title.EndsWith ('*'))
{
Title = Title + "*";
}
}
else
{
if (Title.EndsWith ('*'))
{
Title = Title.TrimEnd ('*');
}
}

notepad.LenShortcut?.Title = $"Len:{Editor.Text.Length}";
};
}

internal void Save ()
{
if (TextView is null || File is null || string.IsNullOrWhiteSpace (File.FullName))
if (Editor is null || File is null || string.IsNullOrWhiteSpace (File.FullName))
{
return;
}

string newText = TextView.Text;
string newText = Editor.Text;

System.IO.File.WriteAllText (File.FullName, newText);
_savedText = newText;
Expand Down
1 change: 1 addition & 0 deletions Examples/UICatalog/UICatalog.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Terminal.Gui\Terminal.Gui.csproj" />
<ProjectReference Include="..\..\Terminal.Gui.TextViewEditor\Terminal.Gui.TextViewEditor.csproj" />
</ItemGroup>
<ItemGroup>
<Using Include="System.Drawing.Rectangle" Alias="Rectangle" />
Expand Down
18 changes: 18 additions & 0 deletions Terminal.Gui.TextViewEditor/Terminal.Gui.TextViewEditor.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AssemblyName>Terminal.Gui.TextViewEditor</AssemblyName>
<TargetFramework>net10.0</TargetFramework>
<LangVersion>14</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>Terminal.Gui.Views</RootNamespace>
<PackageId>Terminal.Gui.TextViewEditor</PackageId>
<Description>Wraps the Terminal.Gui.Editor view with a TextView-compatible API to ease porting.</Description>
<IsPackable>true</IsPackable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Terminal.Gui\Terminal.Gui.csproj" />
<PackageReference Include="Terminal.Gui.Editor" />
</ItemGroup>
</Project>
Loading
Loading