예제 #1
0
        /*
         *  テキストの内容の変化を通知してきた。
         */
        private void EditContext_TextUpdating(CoreTextEditContext sender, CoreTextTextUpdatingEventArgs ev)
        {
            Debug.WriteLine("<<--- TextUpdating:({0},{1})->({2},{3}) [{4}] {5}",
                            ev.Range.StartCaretPosition, ev.Range.EndCaretPosition,
                            ev.NewSelection.StartCaretPosition, ev.NewSelection.EndCaretPosition,
                            ev.Text,
                            ev.Result
                            );

            // カーソルがあると文字の操作に邪魔なので、いったん取り除きます。
            RemoveCursor();

            // 以前の選択位置の文字を末尾から取り除いていきます。
            for (int i = ev.Range.EndCaretPosition - 1; ev.Range.StartCaretPosition <= i; i--)
            {
                EditText.Inlines.RemoveAt(i);
            }

            // 新しいテキストを挿入します。
            for (int i = 0; i < ev.Text.Length; i++)
            {
                // 1文字ごとにRunを作ります。
                Run txt = new Run();
                txt.Text = ev.Text.Substring(i, 1);

                // RunをTextBlockに挿入します。
                EditText.Inlines.Insert(ev.Range.StartCaretPosition + i, txt);
            }

            // アプリ内で持っているテキストの選択位置を更新します。
            Selection = ev.NewSelection;

            // カーソルを挿入します。
            InsertCursor();
        }
        void EditContext_TextUpdating(CoreTextEditContext sender, CoreTextTextUpdatingEventArgs args)
        {
            if (!_compositionStarted) // Only update text  when composition is started.
            {
                args.Result = CoreTextTextUpdatingResult.Failed;
                return;
            }

            CoreTextRange range        = args.Range;
            string        newText      = args.Text;
            CoreTextRange newSelection = args.NewSelection;

            // Modify the internal text store.
            _inputBuffer = _inputBuffer.Substring(0, range.StartCaretPosition) +
                           newText +
                           _inputBuffer.Substring(Math.Min(_inputBuffer.Length, range.EndCaretPosition));

            // You can set the proper font or direction for the updated text based on the language by checking
            // args.InputLanguage.  We will not do that in this sample.

            // Modify the current selection.
            newSelection.EndCaretPosition = newSelection.StartCaretPosition;

            // Update the selection of the edit context. There is no need to notify the system
            // because the system itself changed the selection.
            SetSelection(newSelection);

            //var compStr = _inputBuffer;
            //compStr = compStr.Insert(_selection.StartCaretPosition, "|");
            //Debug.WriteLine("composition text: {0}, cursor pos: {1}", (object)compStr, _selection.StartCaretPosition);
            OnTextComposition(_inputBuffer, _selection.StartCaretPosition);
        }
예제 #3
0
        void SetSelection(CoreTextRange selection)
        {
            // Modify the internal selection.
            _selection = selection;

            //Update the UI to show the new selection.
            UpdateTextUI();
        }
예제 #4
0
        void EditContext_SelectionUpdating(CoreTextEditContext sender, CoreTextSelectionUpdatingEventArgs args)
        {
            // Set the new selection to the value specified by the system.
            CoreTextRange range = args.Selection;

            // Update the selection of the edit context. There is no need to notify the system
            // because the system itself changed the selection.
            SetSelection(range);
        }
예제 #5
0
        // Adjust the active endpoint of the selection in the specified direction.
        void AdjustSelectionEndpoint(int direction)
        {
            CoreTextRange range = _selection;

            if (_extendingLeft)
            {
                range.StartCaretPosition = Math.Max(0, range.StartCaretPosition + direction);
            }
            else
            {
                range.EndCaretPosition = Math.Min(_text.Length, range.EndCaretPosition + direction);
            }

            SetSelectionAndNotify(range);
        }
        void EditContext_SelectionUpdating(CoreTextEditContext sender, CoreTextSelectionUpdatingEventArgs args)
        {
            // Set the new selection to the value specified by the system.
            CoreTextRange range = args.Selection;

            // Update the selection of the edit context. There is no need to notify the system
            // because the system itself changed the selection.
            SetSelection(range);

            //var compStr = _inputBuffer;
            //compStr = compStr.Insert(_selection.StartCaretPosition, "|");
            //Debug.WriteLine("composition text: {0}, cursor pos: {1}", (object)compStr, _selection.StartCaretPosition);

            OnTextComposition(_inputBuffer, _selection.StartCaretPosition);
        }
예제 #7
0
        /*
         *  テキストの選択位置の変化を通知してきた。
         */
        private void EditContext_SelectionUpdating(CoreTextEditContext sender, CoreTextSelectionUpdatingEventArgs ev)
        {
            Debug.WriteLine("<<--- SelectionUpdating: cancel:{0} result:{1} ({2},{3})",
                            ev.IsCanceled,
                            ev.Result,
                            ev.Selection.StartCaretPosition, ev.Selection.EndCaretPosition
                            );

            // カーソルがあると文字の操作に邪魔なので、いったん取り除きます。
            RemoveCursor();

            // アプリ内で持っているテキストの選択位置を更新します。
            Selection = ev.Selection;

            // カーソルを挿入します。
            InsertCursor();
        }
예제 #8
0
        // Replace the text in the specified range.
        void ReplaceText(CoreTextRange modifiedRange, string text)
        {
            // Modify the internal text store.
            _text = _text.Substring(0, modifiedRange.StartCaretPosition) +
                    text +
                    _text.Substring(modifiedRange.EndCaretPosition);

            // Move the caret to the end of the replacement text.
            _selection.StartCaretPosition = modifiedRange.StartCaretPosition + text.Length;
            _selection.EndCaretPosition   = _selection.StartCaretPosition;

            // Update the selection of the edit context.  There is no need to notify the system
            // of the selection change because we are going to call NotifyTextChanged soon.
            SetSelection(_selection);

            // Let the CoreTextEditContext know what changed.
            _editContext.NotifyTextChanged(modifiedRange, text.Length, _selection);
        }
예제 #9
0
        void EditContext_TextUpdating(CoreTextEditContext sender, CoreTextTextUpdatingEventArgs args)
        {
            CoreTextRange range        = args.Range;
            string        newText      = args.Text;
            CoreTextRange newSelection = args.NewSelection;

            // Modify the internal text store.
            _text = _text.Substring(0, range.StartCaretPosition) +
                    newText +
                    _text.Substring(Math.Min(_text.Length, range.EndCaretPosition));

            // You can set the proper font or direction for the updated text based on the language by checking
            // args.InputLanguage.  We will not do that in this sample.

            // Modify the current selection.
            newSelection.EndCaretPosition = newSelection.StartCaretPosition;

            // Update the selection of the edit context. There is no need to notify the system
            // because the system itself changed the selection.
            SetSelection(newSelection);
        }
예제 #10
0
        async void EditContext_TextUpdating(CoreTextEditContext sender, CoreTextTextUpdatingEventArgs args)
        {
            CoreTextRange range        = args.Range;
            string        newText      = args.Text;
            CoreTextRange newSelection = args.NewSelection;

            var line = _content[CurrentLine].Text;

            //// Modify the internal text store.
            _content[CurrentLine].Text = line.Substring(0, range.StartCaretPosition) +
                                         newText +
                                         line.Substring(Math.Min(line.Length, range.EndCaretPosition));

            if (_content[CurrentLine].Text.Trim().ToLower().StartsWith("todo:") && _content[CurrentLine] is not TodoTextItem)
            {
                var todoItem = new TodoTextItem()
                {
                    Text = _content[CurrentLine].Text.Trim().Substring(5)
                };
                // convert item to todo
                _content.RemoveAt(CurrentLine);
                _content.Insert(CurrentLine, todoItem);
                newSelection.StartCaretPosition = newSelection.EndCaretPosition = 0;

                // had to add this to make sure it works
                await Task.Delay(2);

                SetSelectionAndNotify(newSelection);
            }
            else
            {
                //// Modify the current selection.
                newSelection.EndCaretPosition = newSelection.StartCaretPosition;

                //// Update the selection of the edit context. There is no need to notify the system
                //// because the system itself changed the selection.
                SetSelection(newSelection);
            }
        }
 // Change the selection without notifying CoreTextEditContext of the new selection.
 void SetSelection(CoreTextRange selection)
 {
     // Modify the internal selection.
     _selection = selection;
 }
예제 #12
0
        void CoreWindow_KeyDown(CoreWindow sender, KeyEventArgs args)
        {
            // Do not process keyboard input if the custom edit control does not
            // have focus.
            if (!_internalFocus)
            {
                return;
            }

            // This holds the range we intend to operate on, or which we intend
            // to become the new selection. Start with the current selection.
            CoreTextRange range = _selection;

            // For the purpose of this sample, we will support only the left and right
            // arrow keys and the backspace key. A more complete text edit control
            // would also handle keys like Home, End, and Delete, as well as
            // hotkeys like Ctrl+V to paste.
            //
            // Note that this sample does not properly handle surrogate pairs
            // nor does it handle grapheme clusters.

            switch (args.VirtualKey)
            {
            // Backspace
            case VirtualKey.Back:
                if (range.StartCaretPosition == 0 && _content[CurrentLine] is TodoTextItem todoItem)
                {
                    // convert todo item to a regular item
                    _content[CurrentLine] = new TextItem()
                    {
                        Text = "todo" + todoItem.Text
                    };

                    range.StartCaretPosition = range.EndCaretPosition = 4;
                    SetSelectionAndNotify(range);
                }
                else if (range.StartCaretPosition == 0 && CurrentLine > 0)
                {
                    CurrentLine--;
                    range.StartCaretPosition = _content[CurrentLine].Text.Length;
                    range.EndCaretPosition   = range.StartCaretPosition;

                    _content[CurrentLine].Text += _content[CurrentLine + 1].Text;

                    _content.RemoveAt(CurrentLine + 1);

                    SetSelectionAndNotify(range);
                }
                else
                {
                    range.StartCaretPosition = Math.Max(0, range.StartCaretPosition - 1);
                    ReplaceText(range, "");
                }
                break;

            // Left arrow
            case VirtualKey.Left:
                // There was no selection. Move the caret left one code unit if possible.
                if (range.StartCaretPosition == 0 && CurrentLine > 0)
                {
                    // move to previous line
                    CurrentLine--;
                    range.StartCaretPosition = _content[CurrentLine].Text.Length;
                    range.EndCaretPosition   = range.StartCaretPosition;
                }
                else
                {
                    range.StartCaretPosition = Math.Max(0, range.StartCaretPosition - 1);
                    range.EndCaretPosition   = range.StartCaretPosition;
                }
                SetSelectionAndNotify(range);
                break;

            // Right arrow
            case VirtualKey.Right:
                // There was no selection. Move the caret right one code unit if possible.
                if (range.StartCaretPosition == _content[CurrentLine].Text.Length && CurrentLine < _content.Count - 1)
                {
                    // move to next line
                    CurrentLine++;
                    range.StartCaretPosition = 0;
                    range.EndCaretPosition   = range.StartCaretPosition;
                }
                else
                {
                    range.StartCaretPosition = Math.Min(_content[CurrentLine].Text.Length, range.StartCaretPosition + 1);
                    range.EndCaretPosition   = range.StartCaretPosition;
                }
                SetSelectionAndNotify(range);
                break;

            case VirtualKey.Down:

                if (CurrentLine < _content.Count - 1)
                {
                    //if (_content[CurrentLine] is TodoTextItem item)
                    //{
                    //    item.Update();
                    //}

                    CurrentLine++;
                    range.StartCaretPosition = Math.Min(_content[CurrentLine].Text.Length, range.StartCaretPosition);
                    range.EndCaretPosition   = range.StartCaretPosition;
                    SetSelectionAndNotify(range);
                }

                break;

            case VirtualKey.Up:

                if (CurrentLine > 0)
                {
                    //if (_content[CurrentLine] is TodoTextItem item)
                    //{
                    //    item.Update();
                    //}

                    CurrentLine--;
                    range.StartCaretPosition = Math.Min(_content[CurrentLine].Text.Length, range.StartCaretPosition);
                    range.EndCaretPosition   = range.StartCaretPosition;
                    SetSelectionAndNotify(range);
                }

                break;

            case VirtualKey.Enter:

                var line = _content[CurrentLine].Text;

                var left  = line.Substring(0, _selection.StartCaretPosition);
                var right = line.Substring(_selection.EndCaretPosition);

                _content[CurrentLine].Text = left;

                _content.Insert(CurrentLine + 1, new TextItem()
                {
                    Text = right
                });

                if (_content[CurrentLine] is TodoTextItem item)
                {
                    item.Update();
                }

                CurrentLine++;

                range.StartCaretPosition = 0;
                range.EndCaretPosition   = range.StartCaretPosition;
                SetSelectionAndNotify(range);

                break;
            }
        }
예제 #13
0
 // Change the selection and notify CoreTextEditContext of the new selection.
 void SetSelectionAndNotify(CoreTextRange selection)
 {
     SetSelection(selection);
     _editContext.NotifySelectionChanged(_selection);
 }
예제 #14
0
 public static bool IsEmpty(this CoreTextRange range)
 {
     return(range.StartCaretPosition == range.EndCaretPosition);
 }
예제 #15
0
 public static int Size(this CoreTextRange range)
 {
     return(range.EndCaretPosition - range.StartCaretPosition);
 }
        // Replace the text in the specified range.
        void ReplaceText(CoreTextRange modifiedRange, string text)
        {
            // Modify the internal text store.
            _text = _text.Substring(0, modifiedRange.StartCaretPosition) +
              text +
              _text.Substring(modifiedRange.EndCaretPosition);

            // Move the caret to the end of the replacement text.
            _selection.StartCaretPosition = modifiedRange.StartCaretPosition + text.Length;
            _selection.EndCaretPosition = _selection.StartCaretPosition;

            // Update the selection of the edit context.  There is no need to notify the system
            // of the selection change because we are going to call NotifyTextChanged soon.
            SetSelection(_selection);

            // Let the CoreTextEditContext know what changed.
            _editContext.NotifyTextChanged(modifiedRange, text.Length, _selection);
        }
        // Change the selection without notifying CoreTextEditContext of the new selection.
        void SetSelection(CoreTextRange selection)
        {
            // Modify the internal selection.
            _selection = selection;

            //Update the UI to show the new selection.
            UpdateTextUI();
        }
예제 #18
0
        void CoreWindow_KeyDown(CoreWindow sender, KeyEventArgs args)
        {
            // Do not process keyboard input if the custom edit control does not
            // have focus.
            if (!_internalFocus)
            {
                return;
            }

            // This holds the range we intend to operate on, or which we intend
            // to become the new selection. Start with the current selection.
            CoreTextRange range = _selection;

            // For the purpose of this sample, we will support only the left and right
            // arrow keys and the backspace key. A more complete text edit control
            // would also handle keys like Home, End, and Delete, as well as
            // hotkeys like Ctrl+V to paste.
            //
            // Note that this sample does not properly handle surrogate pairs
            // nor does it handle grapheme clusters.

            switch (args.VirtualKey)
            {
            // Backspace
            case VirtualKey.Back:
                // If there is a selection, then delete the selection.
                if (HasSelection())
                {
                    // Set the text in the selection to nothing.
                    ReplaceText(range, "");
                }
                else
                {
                    // Delete the character to the left of the caret, if one exists,
                    // by creating a range that encloses the character to the left
                    // of the caret, and setting the contents of that range to nothing.
                    range.StartCaretPosition = Math.Max(0, range.StartCaretPosition - 1);
                    ReplaceText(range, "");
                }
                break;

            // Left arrow
            case VirtualKey.Left:
                // If the shift key is down, then adjust the size of the selection.
                if (_coreWindow.GetKeyState(VirtualKey.Shift).HasFlag(CoreVirtualKeyStates.Down))
                {
                    // If this is the start of a selection, then remember which edge we are adjusting.
                    if (!HasSelection())
                    {
                        _extendingLeft = true;
                    }

                    // Adjust the selection and notify CoreTextEditContext.
                    AdjustSelectionEndpoint(-1);
                }
                else
                {
                    // The shift key is not down. If there was a selection, then snap the
                    // caret at the left edge of the selection.
                    if (HasSelection())
                    {
                        range.EndCaretPosition = range.StartCaretPosition;
                        SetSelectionAndNotify(range);
                    }
                    else
                    {
                        // There was no selection. Move the caret left one code unit if possible.
                        range.StartCaretPosition = Math.Max(0, range.StartCaretPosition - 1);
                        range.EndCaretPosition   = range.StartCaretPosition;
                        SetSelectionAndNotify(range);
                    }
                }
                break;

            // Right arrow
            case VirtualKey.Right:
                // If the shift key is down, then adjust the size of the selection.
                if (_coreWindow.GetKeyState(VirtualKey.Shift).HasFlag(CoreVirtualKeyStates.Down))
                {
                    // If this is the start of a selection, then remember which edge we are adjusting.
                    if (!HasSelection())
                    {
                        _extendingLeft = false;
                    }

                    // Adjust the selection and notify CoreTextEditContext.
                    AdjustSelectionEndpoint(+1);
                }
                else
                {
                    // The shift key is not down. If there was a selection, then snap the
                    // caret at the right edge of the selection.
                    if (HasSelection())
                    {
                        range.StartCaretPosition = range.EndCaretPosition;
                        SetSelectionAndNotify(range);
                    }
                    else
                    {
                        // There was no selection. Move the caret right one code unit if possible.
                        range.StartCaretPosition = Math.Min(_text.Length, range.StartCaretPosition + 1);
                        range.EndCaretPosition   = range.StartCaretPosition;
                        SetSelectionAndNotify(range);
                    }
                }
                break;
            }
        }
 // Change the selection and notify CoreTextEditContext of the new selection.
 void SetSelectionAndNotify(CoreTextRange selection)
 {
     SetSelection(selection);
     _editContext.NotifySelectionChanged(_selection);
 }
예제 #20
0
 public TextChangedEventArgs(CoreTextRange modifiedRange, int newLength, CoreTextRange newSelection)
 {
     ModifiedRange = modifiedRange;
     NewLength     = newLength;
     NewSelection  = newSelection;
 }
 public SelectionChangedEventArgs(CoreTextRange selection)
 {
     Selection = selection;
 }