File size: 2,287 Bytes
e3ea2f2
07bbbbf
e3ea2f2
 
 
6667126
07bbbbf
 
 
e3ea2f2
 
 
 
 
 
 
 
07bbbbf
 
 
 
 
 
 
e3ea2f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
07bbbbf
 
e3ea2f2
07bbbbf
e3ea2f2
 
 
 
 
 
 
6667126
 
 
e3ea2f2
 
 
6667126
e3ea2f2
07bbbbf
 
 
 
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
using System.Linq;
using System.Windows.Controls;
using System.Windows.Input;
using mdict.Services;
using Mdx.Storage;
using CefSharp;

namespace mdict.Views
{
    public class SearchResult
    {
        public string Key => KeyIndex.Key;
        public string DisplayText => $"{Key} - {Dictionary.Name}";
        public KeyIndex KeyIndex { get; set; }
        public DictionaryItem Dictionary { get; set; }
    }

    public partial class SearchView : UserControl
    {
        public SearchView()
        {
            InitializeComponent();
        }

        private void SearchInput_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                var query = SearchInput.Text.Trim();
                if (string.IsNullOrWhiteSpace(query)) return;

                var results = LibraryManager.Instance.Search(query);
                
                var viewResults = results.Select(r => new SearchResult 
                { 
                    Dictionary = r.Dictionary, 
                    KeyIndex = r.Index 
                }).ToList();

                ResultList.ItemsSource = viewResults;
                
                if (viewResults.Any())
                {
                    ResultList.SelectedIndex = 0;
                }
            }
        }

        private void ResultList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ResultList.SelectedItem is SearchResult item)
            {
                PlaceholderText.Visibility = System.Windows.Visibility.Collapsed;
                ContentBrowser.Visibility = System.Windows.Visibility.Visible;

                try
                {
                    string content = item.Dictionary.Reader.GetHtml(item.KeyIndex);
                    
                    // Use LoadHtml from CefSharp
                    // Providing a dummy URL is often helpful for resolving relative links or just identifying the source
                    ContentBrowser.LoadHtml(content, "http://mdict/" + item.Key);
                }
                catch (System.Exception ex)
                {
                    ContentBrowser.LoadHtml($"<h1>Error</h1><p>{ex.Message}</p>", "http://error/");
                }
            }
        }
    }
}