File size: 4,461 Bytes
1c4658f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using System.Collections.ObjectModel;
using System.Diagnostics;
using FastSeek.Core;
using FastSeek.Core.Search;
using Microsoft.UI.Input;
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Input;
using Windows.System;

namespace FastSeek.WinUI;

public sealed partial class MainWindow : Window
{
    private readonly FastSeekEngine _engine = new();
    private readonly ObservableCollection<ResultItem> _items = new();

    public MainWindow()
    {
        this.InitializeComponent();
        ResultsList.ItemsSource = _items;
        ExtendsContentIntoTitleBar = true;
        this.Closed += (_, _) => _engine.Dispose();
        SetSpotlightSizing();
        _ = InitializeEngineAsync();
    }

    private async Task InitializeEngineAsync()
    {
        SearchBox.PlaceholderText = "Indexing NTFS drives...";
        try
        {
            await _engine.InitializeAsync();
            SearchBox.PlaceholderText = "Search files and folders...";
        }
        catch (Exception ex)
        {
            SearchBox.PlaceholderText = "Initialization failed";
            _items.Clear();
            _items.Add(new ResultItem { Kind = "ERR", Name = "Could not initialize index", FullPath = ex.Message, Raw = null });
        }
    }

    private void SetSpotlightSizing()
    {
        var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
        var id = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(hwnd);
        var appWindow = AppWindow.GetFromWindowId(id);
        appWindow.Resize(new Windows.Graphics.SizeInt32(900, 560));
        if (appWindow.Presenter is OverlappedPresenter p)
        {
            p.IsMaximizable = false;
            p.IsMinimizable = true;
            p.IsResizable = true;
        }
    }

    private async void SearchBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
    {
        if (args.Reason != AutoSuggestionBoxTextChangeReason.UserInput) return;
        var query = sender.Text;
        var results = await Task.Run(() => _engine.Search(query, 50));
        RenderResults(results);
    }

    private void RenderResults(List<SearchResult> results)
    {
        _items.Clear();
        foreach (var r in results)
        {
            _items.Add(new ResultItem
            {
                Kind = r.IsDir ? "DIR" : "FILE",
                Name = r.Name,
                FullPath = r.FullPath,
                Raw = r,
            });
        }
        if (_items.Count > 0) ResultsList.SelectedIndex = 0;
    }

    private void SearchBox_KeyDown(object sender, KeyRoutedEventArgs e)
    {
        if (e.Key == VirtualKey.Down)
        {
            if (_items.Count == 0) return;
            var i = Math.Min(ResultsList.SelectedIndex + 1, _items.Count - 1);
            ResultsList.SelectedIndex = i;
            ResultsList.ScrollIntoView(_items[i]);
            e.Handled = true;
        }
        else if (e.Key == VirtualKey.Up)
        {
            if (_items.Count == 0) return;
            var i = Math.Max(ResultsList.SelectedIndex - 1, 0);
            ResultsList.SelectedIndex = i;
            ResultsList.ScrollIntoView(_items[i]);
            e.Handled = true;
        }
        else if (e.Key == VirtualKey.Enter)
        {
            OpenSelected();
            e.Handled = true;
        }
        else if (e.Key == VirtualKey.Escape)
        {
            this.Close();
        }
    }

    private void ResultsList_ItemClick(object sender, ItemClickEventArgs e) => OpenSelected();

    private void OpenSelected()
    {
        if (ResultsList.SelectedItem is not ResultItem item) return;
        try
        {
            Process.Start(new ProcessStartInfo
            {
                FileName = item.FullPath,
                UseShellExecute = true,
            });
        }
        catch
        {
            try
            {
                Process.Start(new ProcessStartInfo
                {
                    FileName = "explorer.exe",
                    Arguments = $"/select,\"{item.FullPath}\"",
                    UseShellExecute = true,
                });
            }
            catch { }
        }
    }

    private sealed class ResultItem
    {
        public required string Kind { get; init; }
        public required string Name { get; init; }
        public required string FullPath { get; init; }
        public SearchResult? Raw { get; init; }
    }
}