Skip to content

Commit

Permalink
🚧 Begin work on settings page for search engines
Browse files Browse the repository at this point in the history
Sync with laptop
  • Loading branch information
trickypr committed Nov 13, 2023
1 parent b2834ca commit ea8bde3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
7 changes: 7 additions & 0 deletions src/content/settings/Settings.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->

<script lang="ts">
import { SEARCH_ENGINE_PREF } from '../shared/search/constants'
import Category from './components/Category.svelte'
import SubCategory from './components/SubCategory.svelte'
import StringPref from './components/pref/StringPref.svelte'
Expand All @@ -21,6 +22,12 @@
</SubCategory>
</Category>

<Category bind:sidebarItems title="Search">
<SubCategory title="Default Search Engine">
<StringPref pref={SEARCH_ENGINE_PREF}>Default search engine</StringPref>
</SubCategory>
</Category>

<Category bind:sidebarItems title="Keybinds">
<SubCategory title="Browser">
<StringPref pref="browser.keybinds.toolbox">
Expand Down
8 changes: 8 additions & 0 deletions src/content/shared/search/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const DEFAULT_SEARCH_ENGINE: (typeof SEARCH_ENGINE_IDS)[number] =
'[email protected]'
export const SEARCH_ENGINE_PREF = 'browser.search.engine.default'

export const SEARCH_ENGINE_IDS = [
'[email protected]',
'[email protected]',
] as const
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,24 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import type { AddonSearchEngine } from 'resource://gre/modules/AddonSearchEngine.sys.mjs'
import { Deferred } from '../Deferred'
import { lazyESModuleGetters } from '../../../shared/TypedImportUtilities'
import { Deferred } from '../../browser/lib/Deferred'
import { lazyESModuleGetters } from '../TypedImportUtilities'
import {
SEARCH_ENGINE_IDS,
SEARCH_ENGINE_PREF,
DEFAULT_SEARCH_ENGINE,
} from './constants'

const lazy = lazyESModuleGetters({
AddonSearchEngine: 'resource://gre/modules/AddonSearchEngine.sys.mjs',
SearchUtils: 'resource://gre/modules/SearchUtils.sys.mjs',
})

const SEARCH_ENGINE_IDS = [
'[email protected]',
'[email protected]',
] as const

class SearchService {
private initPromise: Deferred<void> | undefined

private searchEngines: Deferred<AddonSearchEngine[]> = new Deferred()
private defaultEngine: Deferred<AddonSearchEngine> = new Deferred()

constructor() {}

Expand All @@ -45,7 +47,6 @@ class SearchService {

const locale = lazy.SearchUtils.DEFAULT_TAG
const engines: AddonSearchEngine[] = []

for (const extensionID of SEARCH_ENGINE_IDS) {
const engine = new (lazy.AddonSearchEngine as any)({
isAppProvided: true,
Expand All @@ -55,15 +56,29 @@ class SearchService {

engines.push(engine)
}

this.searchEngines.resolve(engines)

const defaultEngineId = Services.prefs.getStringPref(
SEARCH_ENGINE_PREF,
DEFAULT_SEARCH_ENGINE,
)
const defaultEngine = engines.find(
(engine: any) => engine._extensionID === defaultEngineId,
)!
this.defaultEngine.resolve(defaultEngine)

this.initPromise.resolve()
}

async getSearchEngines() {
await this.init()
return await this.searchEngines.promise
}

async getDefaultEngine() {
await this.init()
return await this.defaultEngine.promise
}
}

export const searchService = new SearchService()

0 comments on commit ea8bde3

Please sign in to comment.