From a6bf21c501162a06509d3a9c691f38296549d1e8 Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Tue, 31 May 2016 23:48:16 +0200 Subject: [PATCH] Change bar position to topCenter, fix Constructor.io search on empty query, let ESC close the search bar, fix result selection --- src/app.js | 10 +++++-- src/components/app/component.tag | 12 ++++---- src/components/search-box/component.tag | 19 ++++++------ src/components/search-results/component.tag | 7 ++++- src/search/constructor-io.js | 33 ++++++++++++++++----- 5 files changed, 56 insertions(+), 25 deletions(-) diff --git a/src/app.js b/src/app.js index c1317d7..b0a85d7 100644 --- a/src/app.js +++ b/src/app.js @@ -26,6 +26,7 @@ let bar = menubar({ icon: path.join(__dirname, "..", "assets", "icon.png"), "preload-window": true, "always-on-top": true, + "window-position": "topCenter", width: 700, height: baseHeight }); @@ -42,7 +43,8 @@ bar.on("ready", () => { bar.on("after-show", () => { bar.window.setAlwaysOnTop(true); - bar.window.focusOnWebView(); + bar.window.setResizable(false); + bar.window.focus(); bar.window.webContents.send("focusSearch"); }) @@ -61,7 +63,11 @@ ipcMain.on("resize", (event, newSize) => { /* Temporary workaround until maxogden/menubar#125 is resolved */ bar.window.setSize(bar.getOption("width"), bar.getOption("height")); -}) +}); + +ipcMain.on("closeSearch", (event) => { + bar.hideWindow(); +}); Promise.try(() => { return mkdirpAsync(appDirectories.userData()); diff --git a/src/components/app/component.tag b/src/components/app/component.tag index 7df4232..787c244 100644 --- a/src/components/app/component.tag +++ b/src/components/app/component.tag @@ -8,6 +8,7 @@ app script. const Promise = require("bluebird"); + const {ipcRenderer} = require("electron"); const rfr = require("rfr"); const search = rfr("lib/search/constructor-io")("CD06z4gVeqSXRiDL2ZNK"); @@ -37,15 +38,14 @@ app /* Sometimes, search results may come back out of order. Here we check whether * the lastQuery is still the same as when we initially made the request. */ if (lastQuery === query) { - this.results = results.packages.map((pkg) => { - return { - name: pkg.value, - description: pkg.data.description - } - }); + this.results = results.packages; this.update(); } }); + }); + + searchBox.on("cancel", () => { + ipcRenderer.send("closeSearch"); }) }); diff --git a/src/components/search-box/component.tag b/src/components/search-box/component.tag index bde7441..0f527c1 100644 --- a/src/components/search-box/component.tag +++ b/src/components/search-box/component.tag @@ -19,16 +19,17 @@ search-box case "Escape": this.trigger("cancel"); break; + default: + let searchInput = this.root.querySelector(".search"); + + if (searchInput.value !== lastKnownQuery) { + lastKnownQuery = searchInput.value; + this.trigger("queryChanged", searchInput.value); + } + + return true; + break; } - - let searchInput = this.root.querySelector(".search"); - - if (searchInput.value !== lastKnownQuery) { - lastKnownQuery = searchInput.value; - this.trigger("queryChanged", searchInput.value); - } - - return true; } }) diff --git a/src/components/search-results/component.tag b/src/components/search-results/component.tag index f47deee..930b37f 100644 --- a/src/components/search-results/component.tag +++ b/src/components/search-results/component.tag @@ -4,6 +4,8 @@ search-results .description {result.description} script. + let lastResults; + Object.assign(this, { currentSelection: 0, moveSelectionDown: function() { @@ -22,7 +24,10 @@ search-results this.on("update", () => { /* Reset the cursor/selection state... */ - this.currentSelection = 0; + if (lastResults !== opts.results) { + lastResults = opts.results; + this.currentSelection = 0; + } }); style(scoped, type="scss"). diff --git a/src/search/constructor-io.js b/src/search/constructor-io.js index 8bc1ab0..c68f410 100644 --- a/src/search/constructor-io.js +++ b/src/search/constructor-io.js @@ -19,15 +19,34 @@ module.exports = function(autocompleteKey) { return `https://ac.cnstrc.com/autocomplete/${encodeURIComponent(query)}?${createQueryString(query, options)}`; } - return function doSearch(query, options) { + return function doSearch(query, options = {}) { return Promise.try(() => { - return bhttp.get(createUrl(query, options)); - }).then((response) => { - if (response.statusCode === 200) { - return response.body; + if (query.trim() !== "") { + return Promise.try(() => { + return bhttp.get(createUrl(query, options)); + }).then((response) => { + if (response.statusCode === 200) { + console.log(response.body); + return { + packages: response.body.sections.packages.map((pkg) => { + return { + name: pkg.value, + description: pkg.data.description + } + }), + suggestions: response.body.sections.standard + }; + } else { + throw new Error(`Non-200 status code encountered: ${response.statusCode}`) + } + }); } else { - throw new Error(`Non-200 status code encountered: ${response.statusCode}`) + return { + packages: [], + suggestions: [] + } } - }); + }) + } } \ No newline at end of file