OSDN Git Service

Documentから直接ロードできるようにした
authorkonekoneko <test2214@hotmail.co.jp>
Mon, 10 Oct 2016 16:14:37 +0000 (21:44 +0530)
committerkonekoneko <test2214@hotmail.co.jp>
Mon, 10 Oct 2016 16:14:37 +0000 (21:44 +0530)
Core/Core.projitems
Core/Document.cs
Core/StringBuffer.cs
Core/WinFileStream.cs [deleted file]
UWP/FooEditEngine.UWP/FooTextBox.cs
WPF/FooEditEngine/FooTextBox.cs
Windows/FooEditEngine/FooTextBox.cs

index 82b1cdb..1b027ab 100644 (file)
@@ -51,6 +51,5 @@
     <Compile Include="$(MSBuildThisFileDirectory)Util.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)ViewBase.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)WatchDogPattern.cs" />
-    <Compile Include="$(MSBuildThisFileDirectory)WinFileStream.cs" />
   </ItemGroup>
 </Project>
\ No newline at end of file
index cef3067..0db9009 100644 (file)
@@ -224,6 +224,8 @@ namespace FooEditEngine
             set;
         }
 
+        public event ProgressEventHandler LoadProgress;
+
         /// <summary>
         /// ルーラーやキャレット・行番号などの表示すべきものが変化した場合に呼び出される。ドキュメントの内容が変化した通知を受け取り場合はUpdateを使用してください
         /// </summary>
@@ -980,11 +982,14 @@ namespace FooEditEngine
         /// 読み取り操作は別スレッドで行われます。
         /// また、非同期操作中はこのメソッドを実行することはできません。
         /// </remarks>
-        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
         /// <param name="tokenSource">キャンセルトークン</param>
         /// <returns>Taskオブジェクト</returns>
         /// <remarks>非同期操作中はこのメソッドを実行することはできません</remarks>
-        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
     }
 
     /// <summary>
-    /// IStreamReaderインターフェイス
-    /// </summary>
-    public interface IStreamReader
-    {
-        /// <summary>
-        /// ストリームが空かどうかを返す
-        /// </summary>
-        bool IsEnd();
-
-        /// <summary>
-        /// ストリームから行を読み取った物を返す。LoadAsyncを呼び出す場合は必ず実装してください
-        /// </summary>
-        Task<string> ReadLineAsync();
-        /// <summary>
-        /// ストリームから指定した文字数だけ読み取る
-        /// </summary>
-        /// <param name="buffer">書き込み先バッファー</param>
-        /// <param name="index">書き込み先バッファーのインデックス</param>
-        /// <param name="count">読み取る文字数</param>
-        /// <returns>読み取った文字数</returns>
-        Task<int> ReadAsync(char[] buffer, int index, int count);
-    }
-
-    /// <summary>
-    /// IStreamWriter
-    /// </summary>
-    public interface IStreamWriter
-    {
-        /// <summary>
-        /// ストリームに書き込む。SaveAsyncを呼び出す場合は必ず実装してください
-        /// </summary>
-        Task WriteAsync(string str);
-
-        /// <summary>
-        /// 書き込む際に使用する改行コード
-        /// </summary>
-        string NewLine
-        {
-            get;
-            set;
-        }
-    }
-
-    /// <summary>
     /// 検索結果を表す
     /// </summary>
     public class SearchResult
index 0f8e755..5e52bc5 100644 (file)
@@ -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 (file)
index aa17633..0000000
+++ /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<string> ReadLineAsync()
-        {
-            return this.sr.ReadLineAsync();
-        }
-
-        public Task<int> 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
-        }
-    }
-}
index c4172c1..024258f 100644 (file)
@@ -331,23 +331,29 @@ namespace FooEditEngine.UWP
         /// <returns>Taskオブジェクト</returns>
         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;
+            }
         }
 
         /// <summary>
@@ -358,8 +364,7 @@ namespace FooEditEngine.UWP
         /// <returns>Taskオブジェクト</returns>
         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)
             {
index b51f4dd..297028d 100644 (file)
@@ -363,8 +363,7 @@ namespace FooEditEngine.WPF
         /// <returns>Taskオブジェクト</returns>
         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);
         }
 
         /// <summary>
@@ -376,20 +375,25 @@ namespace FooEditEngine.WPF
         /// <returns>Taskオブジェクト</returns>
         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;
+            }
         }
 
         /// <summary>
@@ -402,7 +406,7 @@ namespace FooEditEngine.WPF
         /// <returns>Taskオブジェクト</returns>
         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)
             {
index 9f00539..f15e483 100644 (file)
@@ -870,8 +870,7 @@ namespace FooEditEngine.Windows
         /// <returns>Taskオブジェクト</returns>
         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);
         }
 
         /// <summary>
@@ -883,21 +882,24 @@ namespace FooEditEngine.Windows
         /// <returns>Taskオブジェクト</returns>
         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;
+            }
         }
 
         /// <summary>
@@ -910,7 +912,7 @@ namespace FooEditEngine.Windows
         /// <returns>Taskオブジェクト</returns>
         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();