public TranslationAdornment(SnapshotSpan span, TranslationRequest request, Size viewportSize)
        {
            Span = span.Snapshot.CreateTrackingSpan(span, SpanTrackingMode.EdgeExclusive);
            Request = request;
            Request.TranslationComplete += request_TranslationComplete;

            InitializeComponent();
            DataContext = this;

            SetMaxSize(viewportSize);

            _menu = spListBox.ContextMenu;
            _menu.KeyUp += (sender, args) =>
            {
                if (args.Key == Key.Left || args.Key == Key.Escape)
                {
                    CloseMenu();
                }
            };
            _menu.Opened += (sender, args) => { _closeMenuRequested = false; };
            _menu.Closed += (sender, args) => { _ignoreItemCommand = !_closeMenuRequested; };

            ItemCommand = new RelayCommand<ItemCommandParameter>(ItemCommandExecute);
            ItemOptionsCommand = new RelayCommand<ItemCommandParameter>(ItemOptionsCommandExecute);
            MenuCommand = new RelayCommand<MenuItem>(MenuCommandExecute);
        }
 public void AddTranslation(SnapshotSpan span, TranslationRequest request)
 {
     var viewportSize = new Size(_view.ViewportWidth, _view.ViewportHeight);
     ClearTranslations();
     var a = new TranslationAdornment(span, request, viewportSize);
     Translations.Add(a);
     a.AdornmentClosed += OnTranslationClosed;
     RenderTranslation(a);
     InvalidateState();
     a.Focus();
     _translationAdornmentFocused = a;
 }
예제 #3
0
        public static void Execute(IWpfTextView view, TranslationRequest request)
        {
            TranslationAdornmentManager manager = null;
            try
            {
                manager = view.Properties.GetProperty<TranslationAdornmentManager>(typeof(TranslationAdornmentManager));
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            if (manager == null) return;

            manager.AddTranslation(view.Selection.SelectedSpans[0], request);
            request.GetTranslationAsync();
        }
        /// <summary>
        /// This function is the callback used to execute a command when the a menu item is clicked.
        /// See the Initialize method to see how the menu item is associated to this function using
        /// the OleMenuCommandService service and the MenuCommand class.
        /// </summary>
        private void TranslateMenu_Clicked(object sender, EventArgs e)
        {
            ClearStatusBar();
            IWpfTextView view = GetActiveTextView();
            ITextSelection selection = view.Selection;
            if (selection != null && !selection.IsEmpty)
            {
                SnapshotSpan span = view.Selection.SelectedSpans[0];
                var selectedText = span.GetText();
                //nothing is selected - taking the entire line
                if (string.IsNullOrEmpty(selectedText))
                {
                    ITextSnapshotLine line = span.Start.GetContainingLine();
                    selectedText = span.Start.GetContainingLine().GetText();
                    view.Selection.Select(new SnapshotSpan(line.Start, line.End), false);
                }
                //still no selection
                if (string.IsNullOrWhiteSpace(selectedText))
                {
                    NotifyNothingToTranslate();
                }
                else
                {
                    var opt = Global.Options;
                    if (opt.BingClientCredential != null && opt.BingClientCredential.IsValid)
                    {
                        ClientCredentialRepository.Current.TryAddOrUpdateCredential(Constants.TranslatorNames.Bing, opt.BingClientCredential);
                    }
                    else
                    {
                        ClientCredentialRepository.Current
                        .TryAddOrUpdateCredential(Constants.TranslatorNames.Bing,
                            new ClientCredential
                            {
                                ClientId = Constants.BingTranslator.TokenClientID,
                                ClientSecret = Constants.BingTranslator.TokenClientSecret
                            });
                    }

                    var request = new TranslationRequest(selectedText, opt.SourceLanguage, opt.TargetLanguage,
                        opt.Translators.ToArray());

                    Connector.Execute(view, request);
                }
            }
            else
            {
                NotifyNothingToTranslate();
            }
        }