Skip to content

Commit

Permalink
Merge pull request #1829 from googlefonts/keep-focus-if-possible
Browse files Browse the repository at this point in the history
Keep focus if possible
  • Loading branch information
justvanrossum authored Nov 28, 2024
2 parents 60f2ac7 + bcadb23 commit ccc8830
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog for Fontra

## 2024-11-28

- Keep the focus on the canvas when clicking icon buttons and (some) list cell buttons [PR 1829](https://github.com/googlefonts/fontra/pull/1829)

## 2024-11-27

- Add 'Add background image' menu to context menu [PR 1827](https://github.com/googlefonts/fontra/pull/1827)
Expand Down
13 changes: 13 additions & 0 deletions src/fontra/client/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -670,3 +670,16 @@ export function colorizeImage(inputImage, color) {
});
});
}

export class FocusKeeper {
get save() {
// Return a bound method that can be used as an event handler
return (event) => {
this._focusedElement = findNestedActiveElement();
};
}

restore() {
this._focusedElement?.focus();
}
}
4 changes: 4 additions & 0 deletions src/fontra/client/web-components/icon-button.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { InlineSVG } from "./inline-svg.js";
import * as html from "/core/html-utils.js";
import { UnlitElement } from "/core/html-utils.js";
import { FocusKeeper } from "/core/utils.js";

export class IconButton extends UnlitElement {
static styles = `
Expand Down Expand Up @@ -67,11 +68,14 @@ export class IconButton extends UnlitElement {
}

render() {
const focus = new FocusKeeper();
this._button = html.button(
{
onmousedown: focus.save,
onclick: (event) => {
this._buttonOnClick?.(event);
event.stopImmediatePropagation();
focus.restore();
},
disabled: this._buttonDisabled,
},
Expand Down
11 changes: 10 additions & 1 deletion src/fontra/views/editor/panel-designspace-navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
boolInt,
enumerate,
escapeHTMLCharacters,
FocusKeeper,
objectsEqual,
range,
rgbaToCSS,
Expand Down Expand Up @@ -1344,11 +1345,13 @@ function makeIconCellFactory(
switchValue = null
) {
return (item, colDesc) => {
const focus = new FocusKeeper();
const value = item[colDesc.key];
const clickSymbol = triggerOnDoubleClick ? "ondblclick" : "onclick";
const iconElement = html.createDomElement("inline-svg", {
src: iconPaths[boolInt(value)],
style: "width: 1.2em; height: 1.2em;",
onmousedown: focus.save,
ondblclick: (event) => {
event.stopImmediatePropagation();
},
Expand All @@ -1361,6 +1364,7 @@ function makeIconCellFactory(
if (!selectItem) {
event.stopImmediatePropagation();
}
focus.restore();
},
});
item[controllerKey].addKeyListener(colDesc.key, (event) => {
Expand Down Expand Up @@ -1467,11 +1471,16 @@ function cellColorStyle(color) {
}

function makeClickableIconHeader(iconPath, onClick) {
const focus = new FocusKeeper();
return html.div(
{
class: "clickable-icon-header",
style: "height: 1.2em; width: 1.2em;",
onclick: onClick,
onmousedown: focus.save,
onclick: (event) => {
onClick(event);
focus.restore();
},
},
[
html.createDomElement("inline-svg", {
Expand Down

0 comments on commit ccc8830

Please sign in to comment.