-
-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathSearchResult.svelte
More file actions
173 lines (157 loc) · 4.66 KB
/
Copy pathSearchResult.svelte
File metadata and controls
173 lines (157 loc) · 4.66 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<script lang="ts">
export let results: string[];
export let resultType: number;
import { appDataDir, join, resolveResource } from '@tauri-apps/api/path';
import { convertFileSrc } from '@tauri-apps/api/tauri';
import { appWindow, LogicalSize } from '@tauri-apps/api/window';
import { afterUpdate } from 'svelte';
import { FALLBACK_ICON_SYMBOL, icons } from '../../../cache';
import CalculationResult from './CalculationResult.svelte';
afterUpdate(async () => {
const height = document.getElementsByClassName('container')[0].clientHeight;
await appWindow.setSize(new LogicalSize(750, height));
if (results.length > 0 && results[0] !== '') {
const firstResult = document.getElementById(results[0]);
firstResult.classList.add('searchResultFocused');
await firstResult.focus();
}
});
async function getIcon(
app_name: string
): Promise<{ icon: string; fallbackIcon: string }> {
let icon = icons.get(app_name);
let fallbackIcon = icons.get(FALLBACK_ICON_SYMBOL);
if (icon && fallbackIcon) {
return { icon, fallbackIcon };
}
if (!fallbackIcon) {
fallbackIcon = convertFileSrc(
await resolveResource('assets/default.svg')
);
icons.set(FALLBACK_ICON_SYMBOL, fallbackIcon);
}
let iconPath: string;
if (
[
'Migration Assistant',
'System Information',
'Calendar',
'System Settings',
'Photo Booth',
'AirPort Utility',
].includes(app_name)
) {
iconPath = await resolveResource(`assets/appIcons/${app_name}.app.png`);
} else {
const appDataDirPath = await appDataDir();
iconPath = await join(appDataDirPath, `appIcons/${app_name}.app.png`);
}
icon = convertFileSrc(iconPath);
icons.set(app_name, icon);
return { icon, fallbackIcon };
}
async function handleKeydown(event) {
if (event.keyCode == 38 || event.keyCode == 40) {
const current = document.activeElement as HTMLElement | null;
const items = [...document.getElementsByClassName('searchResult')] as
| HTMLElement[]
| null;
const currentIndex = items.indexOf(current);
let newIndex;
if (currentIndex === -1) {
newIndex = 0;
} else {
if (event.keyCode === 38)
newIndex = (currentIndex + items.length - 1) % items.length;
else newIndex = (currentIndex + 1) % items.length;
}
if (current !== null && items[newIndex] !== null) {
items[newIndex].classList.add('searchResultFocused');
current.classList.remove('searchResultFocused');
current.blur();
items[newIndex].focus();
}
} else if (event.key == 'Enter') {
const current = document.activeElement as HTMLElement | null;
if (current !== null) {
current.click();
}
} else {
const searchBarInput = document.getElementById(
'searchBarInput'
) as HTMLInputElement | null;
searchBarInput.focus();
}
}
</script>
<svelte:window on:keydown={handleKeydown} />
<div class="searchResults">
{#if results.length > 0 && results[0] !== ' '}
{#if resultType !== 3}
{#each results as result}
<button on:click class="searchResult" id={result}>
{#await getIcon(result.split('/').pop().replace(/.app$/, ''))}
<span class="appIcon" />
{:then { icon, fallbackIcon }}
<img
class="appIcon"
src={icon}
alt=""
on:error={event => {
// @ts-ignore
event.target.src = fallbackIcon;
}}
/>
{/await}
<p class="appName">{result.split('/').pop().replace(/.app$/, '')}</p>
</button>
{/each}
{:else}
<CalculationResult bind:results />
{/if}
{/if}
</div>
<style>
.searchResults {
max-height: 250px;
overflow-y: auto;
overflow-x: hidden;
}
.searchResult {
margin-top: 7px;
margin-left: 12px;
box-sizing: border-box;
display: flex;
flex-direction: row;
align-items: center;
padding: 0px 12px;
width: 726px;
height: 43px;
background: transparent;
border: none;
border-radius: 8px;
flex: none;
order: 1;
flex-grow: 0;
}
:global(.searchResultFocused) {
background: var(--highlight-overlay) !important;
outline: 0 !important;
border-radius: 8px;
}
.appIcon {
display: inline-flex;
width: 24px;
height: 24px;
margin-right: 8px;
}
.appName {
font-family: Helvetica;
font-style: normal;
font-weight: 500;
font-size: 14px;
line-height: 20px;
margin: 0;
color: var(--primary-text-color);
}
</style>