OSDN Git Service

ファイル読み込み後の再描写はすぐ行わなくても構わない
[fooeditengine/FooEditEngine.git] / UWP / FooEditEngine.UWP / FooTextBox.cs
index 6cb215c..71da92f 100644 (file)
@@ -55,10 +55,11 @@ namespace FooEditEngine.UWP
 #endif
         bool nowCaretMove = false;
         bool nowCompstion = false;
+        bool requestSizeChange = false;
         Document _Document;
         DispatcherTimer timer = new DispatcherTimer();
 
-        const int Interval = 16;
+        const int Interval = 32;
         const int IntervalWhenLostFocus = 160;
 
         /// <summary>
@@ -111,6 +112,9 @@ namespace FooEditEngine.UWP
             this.timer.Tick += this.timer_Tick;
             this.timer.Start();
 
+            this.GettingFocus += FooTextBox_GettingFocus;
+            this.LosingFocus += FooTextBox_LosingFocus;
+
             this.SizeChanged += FooTextBox_SizeChanged;
 
             this.Loaded += FooTextBox_Loaded;
@@ -217,7 +221,13 @@ namespace FooEditEngine.UWP
             var dataPackageView = Clipboard.GetContent();
             if (dataPackageView.Contains(StandardDataFormats.Text))
             {
-                this._Controller.SelectedText = await dataPackageView.GetTextAsync();
+                try
+                {
+                    this._Controller.SelectedText = await dataPackageView.GetTextAsync();
+                }catch(Exception e)
+                {
+                    System.Diagnostics.Debug.WriteLine("past error:" + e.Message);
+                }
             }
         }
 
@@ -304,9 +314,12 @@ namespace FooEditEngine.UWP
         /// <summary>
         /// 再描写する
         /// </summary>
-        public void Refresh()
+        public void Refresh(bool immidately=true)
         {
-            this.Document.RequestRedraw();
+            if(immidately)
+                this.Refresh(this.View.PageBound);
+            else
+                this.Document.RequestRedraw();
         }
 
         /// <summary>
@@ -355,7 +368,7 @@ namespace FooEditEngine.UWP
                 if (this.verticalScrollBar != null)
                     this.verticalScrollBar.Maximum = this.View.LayoutLines.Count;
                 this.IsEnabled = true;
-                this.Refresh(this.View.PageBound);
+                this.Refresh(false);
             }
         }
 
@@ -431,13 +444,22 @@ namespace FooEditEngine.UWP
             }
         }
 
-            /// <inheritdoc/>
-        protected override async void OnGotFocus(RoutedEventArgs e)
+        private void FooTextBox_LosingFocus(UIElement sender, LosingFocusEventArgs args)
         {
-            base.OnGotFocus(e);
+            this.RemoveTextContext();
+            
+            if (this.textServiceManager != null)
+            {
+                this.textServiceManager.InputLanguageChanged -= TextServiceManager_InputLanguageChanged;
+                this.textServiceManager = null;
+            }
 
-            System.Diagnostics.Debug.WriteLine("got focus");
+            System.Diagnostics.Debug.WriteLine("losing focus");
+        }
 
+        private async void FooTextBox_GettingFocus(UIElement sender, GettingFocusEventArgs args)
+        {
+            System.Diagnostics.Debug.WriteLine("getting focus");
             if (this.textServiceManager == null)
             {
                 await Task.Delay(500);
@@ -446,30 +468,34 @@ namespace FooEditEngine.UWP
             }
 
             this.CreateTextContext();
+        }
+
+        /// <inheritdoc/>
+        protected override void OnGotFocus(RoutedEventArgs e)
+        {
+            base.OnGotFocus(e);
+
+            System.Diagnostics.Debug.WriteLine("got focus");
 
             this.View.IsFocused = true;
             this.timer.Interval = new TimeSpan(0, 0, 0, 0, Interval);
-            this.Refresh();
+            this.Refresh(false);
         }
 
         private void TextServiceManager_InputLanguageChanged(CoreTextServicesManager sender, object args)
         {
             System.Diagnostics.Debug.WriteLine("input language changed input script:"+  this.textServiceManager.InputLanguage.Script);
-            this.RemoveTextContext();
-            this.CreateTextContext();
         }
 
-        /// <inheritdoc/>
+       /// <inheritdoc/>
         protected override void OnLostFocus(RoutedEventArgs e)
         {
             base.OnLostFocus(e);
 
-            this.RemoveTextContext();
-
             System.Diagnostics.Debug.WriteLine("lost focus");
             
             this.View.IsFocused = false;
-            this.Refresh(this.View.PageBound);
+            this.Refresh(false);
             this.timer.Interval = new TimeSpan(0, 0, 0, 0, IntervalWhenLostFocus);
         }
 
@@ -478,6 +504,7 @@ namespace FooEditEngine.UWP
             if(this.textServiceManager != null)
             {
                 this.textEditContext = this.textServiceManager.CreateEditContext();
+                this.textEditContext.InputScope = CoreTextInputScope.Text;
                 this.textEditContext.CompositionStarted += TextEditContext_CompositionStarted;
                 this.textEditContext.CompositionCompleted += TextEditContext_CompositionCompleted;
                 this.textEditContext.LayoutRequested += TextEditContext_LayoutRequested;
@@ -496,6 +523,7 @@ namespace FooEditEngine.UWP
         {
             if(this.textEditContext != null)
             {
+                this.textEditContext.NotifyFocusLeave();
                 this.textEditContext.CompositionStarted -= TextEditContext_CompositionStarted;
                 this.textEditContext.CompositionCompleted -= TextEditContext_CompositionCompleted;
                 this.textEditContext.LayoutRequested -= TextEditContext_LayoutRequested;
@@ -506,7 +534,6 @@ namespace FooEditEngine.UWP
                 this.textEditContext.FormatUpdating -= TextEditContext_FormatUpdating;
                 this.textEditContext.FocusRemoved -= TextEditContext_FocusRemoved;
                 this.textEditContext.NotifyFocusLeaveCompleted -= TextEditContext_NotifyFocusLeaveCompleted;
-                this.textEditContext.NotifyFocusLeave();
                 this.textEditContext = null;
             }
         }
@@ -1100,6 +1127,7 @@ namespace FooEditEngine.UWP
                 //タッチスクリーンで行選択した場合、アンカーインデックスを単語の先頭にしないとバグる
                 this.Document.SelectGrippers.BottomLeft.Enabled = touched;
                 this.Document.SelectLine(this.Controller.SelectionStart, touched);
+                this.Refresh();
             }
             else  if(e.TapCount == 2)   //ダブルタップ
             {
@@ -1220,11 +1248,8 @@ namespace FooEditEngine.UWP
 
         void FooTextBox_SizeChanged(object sender, SizeChangedEventArgs e)
         {
-            if (this.Resize(this.rectangle.ActualWidth, this.rectangle.ActualHeight))
-            {
-                //普通に再描写するとちらつく
-                this.Refresh(this.View.PageBound);
-            }
+            //LostFocusやGotFocusなどと競合するとDirect2Dでエラーが起きるので、timer_tickイベントでサイズ変更を行うことにした
+            this.requestSizeChange = true;
         }
 
         void horizontalScrollBar_Scroll(object sender, ScrollEventArgs e)
@@ -1293,10 +1318,21 @@ namespace FooEditEngine.UWP
 
         void timer_Tick(object sender, object e)
         {
-            if (this.View.LayoutLines.HilightAll() || this.View.LayoutLines.GenerateFolding() || this.Document.IsRequestRedraw)
+            this.timer.Stop();
+            if(this.requestSizeChange)
+            {
+                if (this.Resize(this.rectangle.ActualWidth, this.rectangle.ActualHeight))
+                {
+                    //普通に再描写するとちらつく
+                    this.Refresh(this.View.PageBound);
+                }
+                this.requestSizeChange = false;
+            }
+            else if (this.View.LayoutLines.HilightAll() || this.View.LayoutLines.GenerateFolding() || this.Document.IsRequestRedraw)
             {
                 this.Refresh(this.View.PageBound);
             }
+            this.timer.Start();
         }
 
         private void SetDocument(Document value)