private void UpdateCurrentSuggestionSource() { LogTo.Debug("Update started."); var start = DateTime.Now; var htmlCtrls = ContentUtils.GetHtmlCtrls(); if (htmlCtrls == null) { LogTo.Debug("Failed to update current suggestion source because htmlCtrls was null"); return; } var newSuggestionss = BaseSuggestions.Copy(); foreach (var htmlCtrl in htmlCtrls) { var doc = htmlCtrl?.GetDocument(); var text = doc?.body?.innerText; if (string.IsNullOrEmpty(text)) { continue; } var words = SplitIntoWords(text); if (words == null) { return; } newSuggestionss.AddWords(words); } CurrentSuggestions = newSuggestionss; LogTo.Debug("Update took: " + (DateTime.Now - start).TotalMilliseconds.ToString() + "ms"); }
/// <summary> /// Subscribe to keydown and keyup events /// </summary> private void SubscribeToHtmlEvents() { CurrentPopup = null; var htmlCtrls = ContentUtils.GetHtmlCtrls(); if (htmlCtrls == null) { return; } foreach (var ctrl in htmlCtrls) { var doc = ctrl?.GetDocument(); var body = doc?.body as IHTMLElement2; if (body == null) { return; } SubscribeToContentChangedEvents(body); SubscribeToKeyupEvent(body); SubscribeToKeydownEvent(body); } }
private bool SelectPlaceholder(bool forward = true) { var selObj = ContentUtils.GetSelectionObject(); if (selObj != null) { selObj.collapse(!forward); if (selObj.findText("<++>", Flags: forward ? 0 : 1)) { selObj.select(); return(true); } } return(false); }
private void ShowNewAutocompleteWdw(IEnumerable <string> matches, string lastWord) { System.Windows.Application.Current.Dispatcher.BeginInvoke((Action)(() => { try { var wdw = ContentUtils.GetFocusedHtmlWindow() as IHTMLWindow4; var htmlDoc = ((IHTMLWindow2)wdw)?.document; if (wdw == null || htmlDoc == null) { return; } LogTo.Debug("Creating and showing a new Autocomplete popup"); CurrentPopup = wdw.CreatePopup(); CurrentPopup.Show(matches, lastWord); } catch (UnauthorizedAccessException) { } catch (RemotingException) { } })); }
private void HandleKeyUp(HtmlKeyInfo info) { if (!Enabled) { return; } if (info.Key == Keys.None || info.Modifiers != KeyModifiers.None || info.Key == Keys.Escape) { return; } if ((info.Key == Keys.Up) || info.Key == Keys.Right || info.Key == Keys.Down || (info.Key == Keys.J && info.Modifiers == KeyModifiers.Ctrl) || (info.Key == Keys.K && info.Modifiers == KeyModifiers.Ctrl)) { LogTo.Debug("KeyUp: Ignoring because it was an arrow key."); return; } else if (info.Key == Keys.Escape) { LogTo.Debug("KeyUp: Escape pressed, closing popup"); CurrentPopup?.Hide(); return; } var selObj = ContentUtils.GetSelectionObject(); if (selObj == null || !string.IsNullOrEmpty(selObj.text)) { return; } var lastWord = ContentUtils.GetLastPartialWord(selObj); if (lastWord == null || string.IsNullOrEmpty(lastWord)) { CurrentPopup?.Hide(); return; } var matches = CurrentSuggestions.GetWords(lastWord); if (matches == null || !matches.Any()) { CurrentPopup?.Hide(); return; } if (matches.Count() == 1 && matches.First() == lastWord) { CurrentPopup?.Hide(); return; } if (CurrentPopup == null) { ShowNewAutocompleteWdw(matches, lastWord); } else { CurrentPopup?.Show(matches, lastWord); } }
public bool InsertSelectedItem() { try { string word = null; if (_popup == null) { return(false); } var selItem = GetSelectedItem(); if (selItem == null) { return(false); } var selObj = ContentUtils.GetSelectionObject(); if (selObj == null) { return(false); } // Replace the last partial word while (selObj.moveStart("character", -1) == -1) { if (string.IsNullOrEmpty(selObj.text)) { return(false); } char first = selObj.text.First(); if (char.IsWhiteSpace(first)) { selObj.moveStart("character", 1); break; } // Break if word contains punctuation else if (char.IsPunctuation(first)) { break; } } word = selItem.innerText; if (Svc <AutocompleterPlugin> .Plugin.Converter.ContainsKey(word)) { word = Svc <AutocompleterPlugin> .Plugin.Converter[word]; } Hide(); selObj.text = word; if (word.Contains("<++>")) { var sel = ContentUtils.GetSelectionObject(); sel.moveEnd("character", -word.Length); sel.select(); } return(true); } catch (RemotingException) { } catch (UnauthorizedAccessException) { } catch (COMException) { } return(false); }