Skip to content

Commit a8ee050

Browse files
committed
Add unsaved-asterisk to tab header
1 parent 824a25c commit a8ee050

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

MathInterpreter/MathTab.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ namespace MathInterpreter
1515
public class MathTab : TabItem
1616
{
1717
private string _filePath;
18+
19+
private bool _saved = true;
1820
private bool _savedBefore;
1921

2022
private readonly MathParser _parser = new MathParser();
2123
private readonly TextEditor _editor = new TextEditor();
22-
24+
2325
public MathTab() : this("Untitled")
2426
{
2527
}
@@ -30,6 +32,7 @@ public MathTab(string header)
3032
Content = _editor;
3133

3234
_editor.FontSize = Properties.Settings.Default.EditorFontSize;
35+
_editor.TextChanged += (sender, args) => AddSaveHeader();
3336
}
3437

3538
public void Run()
@@ -68,6 +71,7 @@ public void Save()
6871
return;
6972
}
7073

74+
RemoveSaveHeader();
7175
File.WriteAllText(_filePath, _editor.Text);
7276
}
7377

@@ -83,9 +87,30 @@ public void SaveAs()
8387
_savedBefore = true;
8488
_filePath = saveFile.FileName;
8589

90+
RemoveSaveHeader();
8691
File.WriteAllText(_filePath, _editor.Text);
8792
}
88-
93+
94+
public void AddSaveHeader()
95+
{
96+
if (!_saved)
97+
return;
98+
99+
_saved = false;
100+
Header += "*";
101+
}
102+
103+
public void RemoveSaveHeader()
104+
{
105+
if (_saved)
106+
return;
107+
108+
_saved = true;
109+
110+
var header = Header.ToString();
111+
Header = header.Substring(0, header.Length - 1);
112+
}
113+
89114
public static MathTab Open()
90115
{
91116
var openFile = new OpenFileDialog {Filter = "Mathos Parser File (*.mpf)|*.mpf|Any File (*.*)|*.*", CheckFileExists = true};
@@ -96,6 +121,7 @@ public static MathTab Open()
96121
var tab = new MathTab(Path.GetFileName(openFile.FileName)) {_savedBefore = true, _filePath = openFile.FileName};
97122

98123
tab._editor.Text = File.ReadAllText(openFile.FileName);
124+
tab.RemoveSaveHeader();
99125

100126
return tab;
101127
}

0 commit comments

Comments
 (0)