diff --git a/mod/ovis-composer/main.js b/mod/ovis-composer/main.js index 71f4df1..4273e77 100644 --- a/mod/ovis-composer/main.js +++ b/mod/ovis-composer/main.js @@ -94,6 +94,7 @@ import { shortPath, stillSameAgent, directSendBlocked, + fieldBackground, blockTargetPx, shouldResize, isManualResize, @@ -208,6 +209,7 @@ export default class Composer extends View { // is picked, where the caret was when it opened, and the history as it was // read then. Each tab has a search field of its own. this.panel = null; + this.panelSearchFocused = false; // The height of the composer block (reported by its root) and the height // to give back when a panel that made the block taller closes. this.blockHeight = null; @@ -233,6 +235,15 @@ export default class Composer extends View { cx.notify(); }); input.on("submit", (event, cx) => this.panelPick(this.panel?.index ?? 0, cx)); + // The border of the search field lights up while it has the keyboard. + input.on("focus", (event, cx) => { + this.panelSearchFocused = true; + cx.notify(); + }); + input.on("blur", (event, cx) => { + this.panelSearchFocused = false; + cx.notify(); + }); } this.panelScroll = VirtualListScrollHandle.new(); this.favorites = storage.loadFavorites(this.store); @@ -702,6 +713,7 @@ export default class Composer extends View { if (!this.panel) return; const { caret } = this.panel; this.panel = null; + this.panelSearchFocused = false; if (this.panelRestorePx !== null) { this.resizeBlock(this.panelRestorePx); this.panelRestorePx = null; @@ -848,6 +860,8 @@ export default class Composer extends View { query, index, input: this.panelInputs[tab], + searchFocused: this.panelSearchFocused, + fieldBackground: fieldBackground(theme(), theme().popover), scroll: this.panelScroll, isFavorite: (text) => isFavorite(this.favorites, text), }; diff --git a/mod/ovis-composer/panel.js b/mod/ovis-composer/panel.js index e032f8a..70f7aab 100644 --- a/mod/ovis-composer/panel.js +++ b/mod/ovis-composer/panel.js @@ -32,6 +32,8 @@ const TEXT_HEIGHT = 34; * query: string, * index: number, * input: object, + * searchFocused: boolean, + * fieldBackground: string, * scroll: object, * isFavorite: (text:string) => boolean, * }} view @@ -83,12 +85,21 @@ function renderTop(t, view, on) { .children([ renderTab(t, "history", `Historia ${counts.history}`, view.tab === "history", on), renderTab(t, "favorites", `Ulubione ${counts.favorites}`, view.tab === "favorites", on), + // The search field: a darker recess with a border that lights up while + // the field has the keyboard (the `.hsearch` of the mockup). h_flex() .id("composer-panel-search") .flex_1() .min_w(0) + .h(30) + .px(10) .items_center() .gap(6) + .rounded(8) + .bg(view.fieldBackground) + .border_1() + .border_color(view.searchFocused ? t.accent : t.input) + .child(div().flex_none().text_size(13).text_color(t.dim).child("⌕")) .child(Input.new(view.input).flex_1().min_w(0).text_size(12.5)) .when(view.query !== "", (el) => el.child( diff --git a/mod/ovis-composer/view.js b/mod/ovis-composer/view.js index 36b0492..748e57c 100644 --- a/mod/ovis-composer/view.js +++ b/mod/ovis-composer/view.js @@ -568,3 +568,44 @@ export function isManualResize({ reportedPx, orderedPx, tolerance = 3 }) { if (!Number.isFinite(reportedPx) || !Number.isFinite(orderedPx)) return false; return Math.abs(reportedPx - orderedPx) > tolerance; } + +// --- colors from the theme ----------------------------------------------------- + +/** + * How light a color is (0 dark .. 1 light), from `#rgb`, `#rrggbb` or + * `#rrggbbaa`; nothing for what is not such a color. + * + * @param {unknown} color + * @returns {number|null} + */ +export function luminance(color) { + if (typeof color !== "string") return null; + let hex = color.trim(); + if (!/^#([0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$/i.test(hex)) return null; + hex = hex.slice(1); + if (hex.length === 3) hex = hex.replace(/./g, "$&$&"); + const channel = (at) => parseInt(hex.slice(at, at + 2), 16) / 255; + return 0.2126 * channel(0) + 0.7152 * channel(2) + 0.0722 * channel(4); +} + +/** + * The background of an input field set into a panel: the darkest color the + * theme names among the usual surfaces, so the field reads as a recess in the + * panel whatever the theme. A theme with none to offer leaves the panel's own. + * + * @param {Record} theme - `theme()` + * @param {string} panelBackground - the background of the panel + * @returns {string} + */ +export function fieldBackground(theme, panelBackground) { + let best = null; + let bestLight = Infinity; + for (const key of ["background", "canvas", "pane", "surface", "chrome"]) { + const light = luminance(theme?.[key]); + if (light !== null && light < bestLight) { + best = theme[key]; + bestLight = light; + } + } + return best ?? panelBackground; +} diff --git a/tests/view.test.mjs b/tests/view.test.mjs index fe72cb6..f2de590 100644 --- a/tests/view.test.mjs +++ b/tests/view.test.mjs @@ -144,3 +144,24 @@ test("isManualResize: a height nobody ordered is the person's drag", () => { assert.equal(isManualResize({ reportedPx: 150, orderedPx: null }), false); assert.equal(isManualResize({ reportedPx: NaN, orderedPx: 200 }), false); }); + +import { luminance, fieldBackground } from "../mod/ovis-composer/view.js"; + +test("luminance: black is darker than grey is darker than white; alpha and rubbish are tolerated", () => { + assert.ok(luminance("#000000") < luminance("#808080")); + assert.ok(luminance("#808080") < luminance("#ffffff")); + assert.equal(luminance("#000000ff"), luminance("#000000")); + assert.equal(luminance("#fff"), luminance("#ffffff")); + assert.equal(luminance("red"), null); + assert.equal(luminance(undefined), null); +}); + +test("fieldBackground: the darkest color the theme has, darker than the panel", () => { + const theme = { popover: "#2a2c31", background: "#101114", canvas: "#16171a", raised: "#202226", chrome: "#1b1c20" }; + assert.equal(fieldBackground(theme, theme.popover), "#101114"); + // a theme where the background is lighter than another color: that one wins + assert.equal(fieldBackground({ ...theme, background: "#3a3c41", canvas: "#0c0d0f" }, theme.popover), "#0c0d0f"); + // nothing valid: the panel's own color + assert.equal(fieldBackground({}, "#2a2c31"), "#2a2c31"); + assert.equal(fieldBackground({ background: "nope" }, "#2a2c31"), "#2a2c31"); +});