aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Utilities/Tango.Stubs.UI/ViewModels/CodeTabVM.cs
blob: 6a8d9d37047702f46bf16fe16bf306b65af50998 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Core.Commands;
using Tango.SharedUI;

namespace Tango.Stubs.UI.ViewModels
{
    /// <summary>
    /// Represents a single script editor tab view model;
    /// </summary>
    /// <seealso cref="Tango.SharedUI.ViewModel" />
    public class CodeTabVM : ViewModel
    {
        private String _title;
        /// <summary>
        /// Gets or sets the script title/file name.
        /// </summary>
        public String Title
        {
            get
            {
                return File != null ? Path.GetFileName(File) : _title;
            }
            set
            {
                _title = value;
                RaisePropertyChanged(nameof(Title));
            }
        }

        private String _file;
        /// <summary>
        /// Gets or sets the full script file path.
        /// </summary>
        public String File
        {
            get { return _file; }
            set
            {
                _file = value;
                RaisePropertyChanged(nameof(File));
                RaisePropertyChanged(nameof(Title));
            }
        }

        private String _code;
        /// <summary>
        /// Gets or sets the script code.
        /// </summary>
        public String Code
        {
            get { return _code; }
            set { _code = value; RaisePropertyChanged(nameof(Code)); }
        }

        private bool _isRunning;
        /// <summary>
        /// Gets or sets a value indicating whether this instance is running.
        /// </summary>
        public bool IsRunning
        {
            get { return _isRunning; }
            set { _isRunning = value; RaisePropertyChangedAuto(); }
        }

        private RelayCommand _insertCodeSnippetCommand;
        /// <summary>
        /// Gets or sets the insert snippet command. (Inserts stub snippet to editor)
        /// </summary>
        public RelayCommand InsertSnippetCommand
        {
            get { return _insertCodeSnippetCommand; }
            set { _insertCodeSnippetCommand = value; RaisePropertyChanged(nameof(InsertSnippetCommand)); }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CodeTabVM"/> class.
        /// </summary>
        public CodeTabVM()
        {
            Title = "untitled";
            Code = Properties.Resources.CodeTabTemplate;
        }

        /// <summary>
        /// Returns a <see cref="System.String" /> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String" /> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            return Title;
        }
    }
}
class="w"> // Clear content after tooltip is closed. // We cannot clear is immediately when setting IsOpen=false // because the tooltip uses an animation for closing. if (toolTip != null) toolTip.Content = null; } void completionList_SelectionChanged(object sender, SelectionChangedEventArgs e) { var item = completionList.SelectedItem; if (item == null) return; object description = item.Description; if (description != null) { string descriptionText = description as string; if (descriptionText != null) { toolTip.Content = new TextBlock { Text = descriptionText, TextWrapping = TextWrapping.Wrap }; } else { toolTip.Content = description; } toolTip.IsOpen = true; } else { toolTip.IsOpen = false; } } #endregion void completionList_InsertionRequested(object sender, EventArgs e) { Close(); // The window must close before Complete() is called. // If the Complete callback pushes stacked input handlers, we don't want to pop those when the CC window closes. var item = completionList.SelectedItem; if (item != null) item.Complete(this.TextArea, new AnchorSegment(this.TextArea.Document, this.StartOffset, this.EndOffset - this.StartOffset), e); } void AttachEvents() { this.completionList.InsertionRequested += completionList_InsertionRequested; this.completionList.SelectionChanged += completionList_SelectionChanged; this.TextArea.Caret.PositionChanged += CaretPositionChanged; this.TextArea.MouseWheel += textArea_MouseWheel; this.TextArea.PreviewTextInput += textArea_PreviewTextInput; } /// <inheritdoc/> protected override void DetachEvents() { this.completionList.InsertionRequested -= completionList_InsertionRequested; this.completionList.SelectionChanged -= completionList_SelectionChanged; this.TextArea.Caret.PositionChanged -= CaretPositionChanged; this.TextArea.MouseWheel -= textArea_MouseWheel; this.TextArea.PreviewTextInput -= textArea_PreviewTextInput; base.DetachEvents(); } /// <inheritdoc/> protected override void OnClosed(EventArgs e) { base.OnClosed(e); if (toolTip != null) { toolTip.IsOpen = false; toolTip = null; } } /// <inheritdoc/> protected override void OnKeyDown(KeyEventArgs e) { base.OnKeyDown(e); if (!e.Handled) { completionList.HandleKey(e); } } void textArea_PreviewTextInput(object sender, TextCompositionEventArgs e) { e.Handled = RaiseEventPair(this, PreviewTextInputEvent, TextInputEvent, new TextCompositionEventArgs(e.Device, e.TextComposition)); } void textArea_MouseWheel(object sender, MouseWheelEventArgs e) { e.Handled = RaiseEventPair(GetScrollEventTarget(), PreviewMouseWheelEvent, MouseWheelEvent, new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta)); } UIElement GetScrollEventTarget() { if (completionList == null) return this; return completionList.ScrollViewer ?? completionList.ListBox ?? (UIElement)completionList; } /// <summary> /// Gets/Sets whether the completion window should close automatically. /// The default value is true. /// </summary> public bool CloseAutomatically { get; set; } /// <inheritdoc/> protected override bool CloseOnFocusLost { get { return this.CloseAutomatically; } } /// <summary> /// When this flag is set, code completion closes if the caret moves to the /// beginning of the allowed range. This is useful in Ctrl+Space and "complete when typing", /// but not in dot-completion. /// Has no effect if CloseAutomatically is false. /// </summary> public bool CloseWhenCaretAtBeginning { get; set; } void CaretPositionChanged(object sender, EventArgs e) { int offset = this.TextArea.Caret.Offset; if (offset == this.StartOffset) { if (CloseAutomatically && CloseWhenCaretAtBeginning) { Close(); } else { completionList.SelectItem(string.Empty); } return; } if (offset < this.StartOffset || offset > this.EndOffset) { if (CloseAutomatically) { Close(); } } else { TextDocument document = this.TextArea.Document; if (document != null) { completionList.SelectItem(document.GetText(this.StartOffset, offset - this.StartOffset)); } } } } }