From eb426998af26b5b7e58a2d2e42444561037db20c Mon Sep 17 00:00:00 2001 From: konekoneko Date: Mon, 10 Oct 2016 21:44:37 +0530 Subject: [PATCH] =?utf8?q?Document=E3=81=8B=E3=82=89=E7=9B=B4=E6=8E=A5?= =?utf8?q?=E3=83=AD=E3=83=BC=E3=83=89=E3=81=A7=E3=81=8D=E3=82=8B=E3=82=88?= =?utf8?q?=E3=81=86=E3=81=AB=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- Core/Core.projitems | 1 - Core/Document.cs | 57 ++++------------------- Core/StringBuffer.cs | 3 +- Core/WinFileStream.cs | 92 ------------------------------------- UWP/FooEditEngine.UWP/FooTextBox.cs | 43 +++++++++-------- WPF/FooEditEngine/FooTextBox.cs | 32 +++++++------ Windows/FooEditEngine/FooTextBox.cs | 30 ++++++------ 7 files changed, 72 insertions(+), 186 deletions(-) delete mode 100644 Core/WinFileStream.cs diff --git a/Core/Core.projitems b/Core/Core.projitems index 82b1cdb..1b027ab 100644 --- a/Core/Core.projitems +++ b/Core/Core.projitems @@ -51,6 +51,5 @@ - \ No newline at end of file diff --git a/Core/Document.cs b/Core/Document.cs index cef3067..0db9009 100644 --- a/Core/Document.cs +++ b/Core/Document.cs @@ -224,6 +224,8 @@ namespace FooEditEngine set; } + public event ProgressEventHandler LoadProgress; + /// /// ルーラーやキャレット・行番号などの表示すべきものが変化した場合に呼び出される。ドキュメントの内容が変化した通知を受け取り場合はUpdateを使用してください /// @@ -980,11 +982,14 @@ namespace FooEditEngine /// 読み取り操作は別スレッドで行われます。 /// また、非同期操作中はこのメソッドを実行することはできません。 /// - internal async Task LoadAsync(IStreamReader fs, CancellationTokenSource tokenSource = null) + public async Task LoadAsync(TextReader fs, CancellationTokenSource tokenSource = null) { - if (fs.IsEnd()) + if (fs.Peek() == -1) return; + if (this.LoadProgress != null) + this.LoadProgress(this, new ProgressEventArgs(ProgressState.Start)); + try { this.Clear(); @@ -997,6 +1002,8 @@ namespace FooEditEngine this.FireUpdateEvent = true; //これ以降の操作にだけダーティフラグを適用しないとおかしなことになる this.LayoutLines.IsFrozneDirtyFlag = false; + if (this.LoadProgress != null) + this.LoadProgress(this, new ProgressEventArgs(ProgressState.Complete)); } } @@ -1007,7 +1014,7 @@ namespace FooEditEngine /// キャンセルトークン /// Taskオブジェクト /// 非同期操作中はこのメソッドを実行することはできません - internal async Task SaveAsync(IStreamWriter fs, CancellationTokenSource tokenSource = null) + public async Task SaveAsync(TextWriter fs, CancellationTokenSource tokenSource = null) { try { @@ -1205,50 +1212,6 @@ namespace FooEditEngine } /// - /// IStreamReaderインターフェイス - /// - public interface IStreamReader - { - /// - /// ストリームが空かどうかを返す - /// - bool IsEnd(); - - /// - /// ストリームから行を読み取った物を返す。LoadAsyncを呼び出す場合は必ず実装してください - /// - Task ReadLineAsync(); - /// - /// ストリームから指定した文字数だけ読み取る - /// - /// 書き込み先バッファー - /// 書き込み先バッファーのインデックス - /// 読み取る文字数 - /// 読み取った文字数 - Task ReadAsync(char[] buffer, int index, int count); - } - - /// - /// IStreamWriter - /// - public interface IStreamWriter - { - /// - /// ストリームに書き込む。SaveAsyncを呼び出す場合は必ず実装してください - /// - Task WriteAsync(string str); - - /// - /// 書き込む際に使用する改行コード - /// - string NewLine - { - get; - set; - } - } - - /// /// 検索結果を表す /// public class SearchResult diff --git a/Core/StringBuffer.cs b/Core/StringBuffer.cs index 0f8e755..5e52bc5 100644 --- a/Core/StringBuffer.cs +++ b/Core/StringBuffer.cs @@ -10,6 +10,7 @@ You should have received a copy of the GNU General Public License along with thi */ //#define TEST_ASYNC using System; +using System.IO; using System.Collections.Generic; using System.Globalization; using System.Linq; @@ -192,7 +193,7 @@ namespace FooEditEngine this.Update(this, new DocumentUpdateEventArgs(UpdateType.Replace, index, length, count)); } - internal async Task LoadAsync(IStreamReader fs, CancellationTokenSource tokenSource = null) + internal async Task LoadAsync(TextReader fs, CancellationTokenSource tokenSource = null) { char[] str = new char[1024 * 256]; int readCount; diff --git a/Core/WinFileStream.cs b/Core/WinFileStream.cs deleted file mode 100644 index aa17633..0000000 --- a/Core/WinFileStream.cs +++ /dev/null @@ -1,92 +0,0 @@ -using System; -using System.Text; -using System.IO; -using System.Threading.Tasks; - -namespace FooEditEngine -{ - class WinFileWriter : IStreamWriter - { - StreamWriter sw; - -#if !METRO && !WINDOWS_UWP - public WinFileWriter(string filepath,Encoding enc) - { - this.sw = new StreamWriter(filepath,false, enc); - } -#else - public WinFileWriter(StreamWriter sw) - { - this.sw = sw; - } -#endif - - public Task WriteAsync(string str) - { - return this.sw.WriteAsync(str); - } - - public string NewLine - { - get - { - return this.sw.NewLine; - } - set - { - this.sw.NewLine = value; - } - } - - public void Close() - { -#if !METRO && !WINDOWS_UWP - this.sw.Close(); -#else -#endif - } - } - class WinFileReader : IStreamReader - { - TextReader sr; -#if !METRO && !WINDOWS_UWP - public WinFileReader(string filepath, Encoding enc) - { - this.sr = new StreamReader(filepath, enc); - } - public WinFileReader(TextReader tr) - { - this.sr = tr; - } -#else - public WinFileReader(StreamReader sr) - { - this.sr = sr; - } -#endif - - public bool IsEnd() - { - return this.sr.Peek() == -1; - } - - public Task ReadLineAsync() - { - return this.sr.ReadLineAsync(); - } - - public Task ReadAsync(char[] buffer,int index, int count) - { - return this.sr.ReadAsync(buffer, index, count); - } - - - public void Close() - { -#if !METRO && !WINDOWS_UWP - this.sr.Close(); -#else -#endif - } - } -} diff --git a/UWP/FooEditEngine.UWP/FooTextBox.cs b/UWP/FooEditEngine.UWP/FooTextBox.cs index c4172c1..024258f 100644 --- a/UWP/FooEditEngine.UWP/FooTextBox.cs +++ b/UWP/FooEditEngine.UWP/FooTextBox.cs @@ -331,23 +331,29 @@ namespace FooEditEngine.UWP /// Taskオブジェクト public async Task LoadFileAsync(System.IO.StreamReader sr, System.Threading.CancellationTokenSource token) { - this.IsEnabled = false; - this.View.LayoutLines.IsFrozneDirtyFlag = true; - WinFileReader fs = new WinFileReader(sr); - await this.Document.LoadAsync(fs, token); - this.View.LayoutLines.IsFrozneDirtyFlag = false; - - CoreTextRange modified_range = new CoreTextRange(); - modified_range.StartCaretPosition = 0; - modified_range.EndCaretPosition = 0; - //キャレット位置はロード前と同じにしないと違和感を感じる - if(this.textEditContext != null) - this.textEditContext.NotifyTextChanged(modified_range, this.Document.Length, modified_range); + await this.Document.LoadAsync(sr, token); + } - if (this.verticalScrollBar != null) - this.verticalScrollBar.Maximum = this.View.LayoutLines.Count; - this.View.CalculateLineCountOnScreen(); - this.IsEnabled = true; + private void Document_LoadProgress(object sender, ProgressEventArgs e) + { + if(e.state == ProgressState.Start) + { + this.IsEnabled = false; + } + else if(e.state == ProgressState.Complete) + { + CoreTextRange modified_range = new CoreTextRange(); + modified_range.StartCaretPosition = 0; + modified_range.EndCaretPosition = 0; + //キャレット位置はロード前と同じにしないと違和感を感じる + if (this.textEditContext != null) + this.textEditContext.NotifyTextChanged(modified_range, this.Document.Length, modified_range); + + if (this.verticalScrollBar != null) + this.verticalScrollBar.Maximum = this.View.LayoutLines.Count; + this.View.CalculateLineCountOnScreen(); + this.IsEnabled = true; + } } /// @@ -358,8 +364,7 @@ namespace FooEditEngine.UWP /// Taskオブジェクト public async Task SaveFile(System.IO.StreamWriter sw, System.Threading.CancellationTokenSource token) { - WinFileWriter fs = new WinFileWriter(sw); - await this.Document.SaveAsync(fs, token); + await this.Document.SaveAsync(sw, token); } #region command @@ -1260,6 +1265,7 @@ namespace FooEditEngine.UWP { old_doc.Update -= new DocumentUpdateEventHandler(Document_Update); this._Document.SelectionChanged -= Controller_SelectionChanged; + this._Document.LoadProgress -= Document_LoadProgress; //NotifyTextChanged()を呼び出すと落ちるのでTextConextをごっそり作り替える this.RemoveTextContext(); @@ -1270,6 +1276,7 @@ namespace FooEditEngine.UWP this._Document = value; this._Document.LayoutLines.Render = this.Render; this._Document.Update += new DocumentUpdateEventHandler(Document_Update); + this._Document.LoadProgress += Document_LoadProgress; //初期化が終わっていればすべて存在する if (this.Controller != null && this.View != null) { diff --git a/WPF/FooEditEngine/FooTextBox.cs b/WPF/FooEditEngine/FooTextBox.cs index b51f4dd..297028d 100644 --- a/WPF/FooEditEngine/FooTextBox.cs +++ b/WPF/FooEditEngine/FooTextBox.cs @@ -363,8 +363,7 @@ namespace FooEditEngine.WPF /// Taskオブジェクト public async Task LoadAsync(System.IO.TextReader tr, System.Threading.CancellationTokenSource token) { - WinFileReader fs = new WinFileReader(tr); - await this.LoadAsyncImpl(fs, token); + await this.Document.LoadAsync(tr, token); } /// @@ -376,20 +375,25 @@ namespace FooEditEngine.WPF /// Taskオブジェクト public async Task LoadFileAsync(string filepath, Encoding enc,System.Threading.CancellationTokenSource token) { - WinFileReader fs = new WinFileReader(filepath, enc); - await this.LoadAsyncImpl(fs, token); + var fs = new System.IO.StreamReader(filepath, enc); + await this.Document.LoadAsync(fs, token); fs.Close(); } - async Task LoadAsyncImpl(WinFileReader fs,System.Threading.CancellationTokenSource token) + private void Document_LoadProgress(object sender, ProgressEventArgs e) { - this.IsEnabled = false; - await this.Document.LoadAsync(fs, token); - TextStoreHelper.NotifyTextChanged(this.textStore, 0, 0, this.Document.Length); - if (this.verticalScrollBar != null) - this.verticalScrollBar.Maximum = this.View.LayoutLines.Count; - this.View.CalculateLineCountOnScreen(); - this.IsEnabled = true; + if (e.state == ProgressState.Start) + { + this.IsEnabled = false; + } + else if (e.state == ProgressState.Complete) + { + TextStoreHelper.NotifyTextChanged(this.textStore, 0, 0, this.Document.Length); + if (this.verticalScrollBar != null) + this.verticalScrollBar.Maximum = this.View.LayoutLines.Count; + this.View.CalculateLineCountOnScreen(); + this.IsEnabled = true; + } } /// @@ -402,7 +406,7 @@ namespace FooEditEngine.WPF /// Taskオブジェクト public async Task SaveFile(string filepath, Encoding enc,string newLine, System.Threading.CancellationTokenSource token) { - WinFileWriter fs = new WinFileWriter(filepath, enc); + var fs = new System.IO.StreamWriter(filepath, false , enc); fs.NewLine = newLine; await this.Document.SaveAsync(fs, token); fs.Close(); @@ -1223,6 +1227,7 @@ namespace FooEditEngine.WPF if (this._Document != null) { old_doc.Update -= new DocumentUpdateEventHandler(Document_Update); + old_doc.LoadProgress -= Document_LoadProgress; old_doc.SelectionChanged += new EventHandler(Controller_SelectionChanged); oldLength = old_doc.Length; } @@ -1230,6 +1235,7 @@ namespace FooEditEngine.WPF this._Document = value; this._Document.LayoutLines.Render = this.Render; this._Document.Update += new DocumentUpdateEventHandler(Document_Update); + this._Document.LoadProgress += Document_LoadProgress; //初期化が終わっていればすべて存在する if (this.Controller != null && this.View != null && this.textStore != null) { diff --git a/Windows/FooEditEngine/FooTextBox.cs b/Windows/FooEditEngine/FooTextBox.cs index 9f00539..f15e483 100644 --- a/Windows/FooEditEngine/FooTextBox.cs +++ b/Windows/FooEditEngine/FooTextBox.cs @@ -870,8 +870,7 @@ namespace FooEditEngine.Windows /// Taskオブジェクト public async Task LoadAsync(System.IO.TextReader tr, System.Threading.CancellationTokenSource token) { - WinFileReader fs = new WinFileReader(tr); - await this.LoadAsyncImpl(fs, token); + await this.Document.LoadAsync(tr, token); } /// @@ -883,21 +882,24 @@ namespace FooEditEngine.Windows /// Taskオブジェクト public async Task LoadFileAsync(string filepath, Encoding enc, System.Threading.CancellationTokenSource token) { - WinFileReader fs = new WinFileReader(filepath, enc); - await this.LoadAsyncImpl(fs, token); + var fs = new System.IO.StreamReader(filepath, enc); + await this.Document.LoadAsync(fs, token); fs.Close(); } - async Task LoadAsyncImpl(WinFileReader fs, System.Threading.CancellationTokenSource token) + private void Document_LoadProgress(object sender, ProgressEventArgs e) { - this.Enabled = false; - this.View.LayoutLines.IsFrozneDirtyFlag = true; - await this.Document.LoadAsync(fs, token); - this.View.LayoutLines.IsFrozneDirtyFlag = false; - this.initScrollBars(); - this.OnMouseMove(new MouseEventArgs(MouseButtons.None, 0, MousePosition.X, MousePosition.Y, 0)); - this.View.CalculateLineCountOnScreen(); - this.Enabled = true; + if (e.state == ProgressState.Start) + { + this.Enabled = false; + } + else if (e.state == ProgressState.Complete) + { + this.initScrollBars(); + this.OnMouseMove(new MouseEventArgs(MouseButtons.None, 0, MousePosition.X, MousePosition.Y, 0)); + this.View.CalculateLineCountOnScreen(); + this.Enabled = true; + } } /// @@ -910,7 +912,7 @@ namespace FooEditEngine.Windows /// Taskオブジェクト public async Task SaveFile(string filepath, Encoding enc, string newLine, System.Threading.CancellationTokenSource token) { - WinFileWriter fs = new WinFileWriter(filepath, enc); + var fs = new System.IO.StreamWriter(filepath, false, enc); fs.NewLine = newLine; await this.Document.SaveAsync(fs, token); fs.Close(); -- 2.11.0