Exemplo n.º 1
0
        private async void on_textChange(object sender, TextChangedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(textBox.Text))
            {
                lbSuggestion.Visibility = Visibility.Collapsed;
                return;
            }
            string typedText = textBox.Text;
            List <GoogleSuggestion> suggestionList = new List <GoogleSuggestion>();

            suggestionList.Clear();
            SearchSuggestionsAPI searchSuggestionsAPI = new SearchSuggestionsAPI();

            suggestionList = await searchSuggestionsAPI.GetSearchSuggestions(typedText);

            if (suggestionList.Count > 0)
            {
                lbSuggestion.ItemsSource = suggestionList.Select(x => x.ToString()).ToList();
                lbSuggestion.Visibility  = Visibility.Visible;
            }
            else if (typedText.Equals(""))
            {
                lbSuggestion.ItemsSource = null;
                lbSuggestion.Visibility  = Visibility.Collapsed;
            }
            else
            {
                lbSuggestion.ItemsSource = null;
                lbSuggestion.Visibility  = Visibility.Collapsed;
            }
        }
Exemplo n.º 2
0
        private Task <List <GoogleSuggestion> > giveSuggestions(string m_toComplete)
        {
            List <string>        ans = new List <string>();
            SearchSuggestionsAPI s   = new SearchSuggestionsAPI();
            var res = s.GetSearchSuggestions(m_toComplete);


            return(res);
        }
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            txt = this.Template.FindName("PART_textbox", this) as TextBox;
            if (txt == null)
            {
                return;
            }

            richtxt = this.Template.FindName("FlowDoc", this) as RichTextBox;
            if (richtxt == null)
            {
                return;
            }

            var border = this.Template.FindName("PART_richborder", this) as Border;

            if (border == null)
            {
                return;
            }



            var popup = Template.FindName("PART_popup", this) as Popup;

            if (popup == null)
            {
                return;
            }

            var listbox = Template.FindName("PART_listbox", this) as ListBox;

            if (listbox == null)
            {
                return;
            }

            var search = new SearchSuggestionsAPI();

            Task <List <GoogleSuggestion> > task;

            border.PreviewMouseDown += (oo, ee) =>
            {
                border.Visibility = Visibility.Hidden;
                txt.Visibility    = Visibility.Visible;
                txt.Focus();
                ee.Handled = true;
                IsFocused  = true;
            };
            var arrows = new List <Key>()
            {
                Key.Up, Key.Down
            };

            txt.PreviewKeyUp += async(oo, ee) =>
            {
                cts?.Cancel();
                if (arrows.Contains(ee.Key))
                {
                    ee.Handled = true;
                    if (ee.Key == Key.Down && listbox.SelectedIndex < listbox.Items.Count - 1)
                    {
                        listbox.SelectedIndex += 1;
                    }
                    else if (ee.Key == Key.Up && listbox.SelectedIndex > 0)
                    {
                        listbox.SelectedIndex -= 1;
                    }

                    return;
                }


                if (ee.Key == Key.Back || ee.Key == Key.Tab)
                {
                    return;
                }
                var localcts = new CancellationTokenSource();
                cts = localcts;
                try
                {
                    FilteredItems.Clear();
                    if (History.Any(i => i.StartsWith(txt.Text)))
                    {
                        popup.IsOpen = History.Count(i => i.StartsWith(txt.Text)) != 1;
                        History.Where(i => i.StartsWith(txt.Text)).ToList().ForEach(i =>
                        {
                            if (FilteredItems.Count >= 7)
                            {
                                return;
                            }
                            FilteredItems.Add(i);
                        });
                        var length = txt.SelectionStart;
                        popup.IsOpen = History.Count(i => i.StartsWith(txt.Text)) != 1 && FilteredItems.Count > 0;
                        txt.Text     = History.First(i => i.StartsWith(txt.Text));
                        txt.Select(length, txt.Text.Length - length);
                    }
                    else
                    {
                        popup.IsOpen = false;
                    }
                    await Task.Delay(500, cts.Token);

                    task = search.GetSearchSuggestions(txt.Text, localcts.Token);
                    var list = await task;
                    if (localcts.IsCancellationRequested)
                    {
                        return;
                    }
                    popup.IsOpen = (list.Count > 0 || FilteredItems.Count > 0);
                    if (FilteredItems.Count == 0 && list.First().Phrase.StartsWith(txt.Text) && FilteredItems.Count == 0)
                    {
                        var length = txt.SelectionStart;
                        txt.Text = list.First().Phrase;
                        txt.Select(length, txt.Text.Length - length);
                    }
                    foreach (var suggestion in list)
                    {
                        if (FilteredItems.Count >= 7)
                        {
                            continue;
                        }
                        if (FilteredItems.Contains(suggestion.Phrase))
                        {
                            continue;
                        }
                        FilteredItems.Add(suggestion.Phrase);
                    }
                }
                catch
                {
                    //ignore
                }
            };
            listbox.SelectionChanged += (oo, ee) =>
            {
                var t = listbox.SelectedItem as string;
                if (t == null)
                {
                    return;
                }
                Text           = t;
                txt.CaretIndex = t.Length;
            };
            listbox.PreviewMouseUp += (oo, ee) =>
            {
                popup.IsOpen = false;
                OnReturnPress();
            };

            listbox.PreviewKeyDown += (oo, ee) =>
            {
                if (ee.Key != Key.Return)
                {
                    return;
                }
                popup.IsOpen = false;
                OnReturnPress();
            };

            txt.PreviewKeyDown += (oo, ee) =>
            {
                if (ee.Key != Key.Return)
                {
                    return;
                }
                popup.IsOpen = false;
                OnReturnPress();
            };

            txt.LostKeyboardFocus += (sender, args) =>
            {
                txt.Visibility    = Visibility.Hidden;
                border.Visibility = Visibility.Visible;
                IsFocused         = false;
            };

            listbox.ItemsSource = FilteredItems;
            ApplyText(Text);
        }