Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: the focus in the sketchbook widget #1978

Merged
merged 1 commit into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions arduino-ide-extension/src/browser/library/library-list-widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { Installable } from '../../common/protocol';
import { ListItemRenderer } from '../widgets/component-list/list-item-renderer';
import { nls } from '@theia/core/lib/common';
import { LibraryFilterRenderer } from '../widgets/component-list/filter-renderer';
import { findChildTheiaButton } from '../utils/dom';

@injectable()
export class LibraryListWidget extends ListWidget<
Expand Down Expand Up @@ -243,17 +244,7 @@ class MessageBoxDialog extends AbstractDialog<MessageBoxDialog.Result> {

protected override onAfterAttach(message: Message): void {
super.onAfterAttach(message);
let buttonToFocus: HTMLButtonElement | undefined = undefined;
for (const child of Array.from(this.controlPanel.children)) {
if (child instanceof HTMLButtonElement) {
if (child.classList.contains('main')) {
buttonToFocus = child;
break;
}
buttonToFocus = child;
}
}
buttonToFocus?.focus();
findChildTheiaButton(this.controlPanel)?.focus();
}
}
export namespace MessageBoxDialog {
Expand Down
37 changes: 37 additions & 0 deletions arduino-ide-extension/src/browser/utils/dom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { notEmpty } from '@theia/core';

/**
* Finds the closest child HTMLButtonElement representing a Theia button.
* A button is a Theia button if it's a `<button>` element and has the `"theia-button"` class.
* If an element has multiple Theia button children, this function prefers `"main"` over `"secondary"` button.
*/
export function findChildTheiaButton(
element: HTMLElement,
recursive = false
): HTMLButtonElement | undefined {
let button: HTMLButtonElement | undefined = undefined;
const children = Array.from(element.children);
for (const child of children) {
if (
child instanceof HTMLButtonElement &&
child.classList.contains('theia-button')
) {
if (child.classList.contains('main')) {
return child;
}
button = child;
}
}
if (!button && recursive) {
button = children
.filter(isHTMLElement)
.map((childElement) => findChildTheiaButton(childElement, true))
.filter(notEmpty)
.shift();
}
return button;
}

function isHTMLElement(element: Element): element is HTMLElement {
return element instanceof HTMLElement;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { BaseWidget } from '@theia/core/lib/browser/widgets/widget';
import { CommandService } from '@theia/core/lib/common/command';
import { SketchbookTreeWidget } from './sketchbook-tree-widget';
import { CreateNew } from '../sketchbook/create-new';
import { findChildTheiaButton } from '../../utils/dom';

@injectable()
export abstract class BaseSketchbookCompositeWidget<
Expand All @@ -18,16 +19,17 @@ export abstract class BaseSketchbookCompositeWidget<
protected readonly commandService: CommandService;

private readonly compositeNode: HTMLElement;
private readonly footerNode: HTMLElement;
private readonly footerRoot: Root;

constructor() {
super();
this.compositeNode = document.createElement('div');
this.compositeNode.classList.add('composite-node');
const footerNode = document.createElement('div');
footerNode.classList.add('footer-node');
this.compositeNode.appendChild(footerNode);
this.footerRoot = createRoot(footerNode);
this.footerNode = document.createElement('div');
this.footerNode.classList.add('footer-node');
this.compositeNode.appendChild(this.footerNode);
this.footerRoot = createRoot(this.footerNode);
this.node.appendChild(this.compositeNode);
this.title.closable = false;
}
Expand All @@ -51,6 +53,7 @@ export abstract class BaseSketchbookCompositeWidget<
super.onActivateRequest(message);
// Sending a resize message is needed because otherwise the tree widget would render empty
this.onResize(Widget.ResizeMessage.UnknownSize);
findChildTheiaButton(this.footerNode, true)?.focus();
}

protected override onResize(message: Widget.ResizeMessage): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,7 @@ export class SketchbookWidget extends BaseWidget {

protected override onActivateRequest(message: Message): void {
super.onActivateRequest(message);

// TODO: focus the active sketchbook
// if (this.editor) {
// this.editor.focus();
// } else {
// }
this.node.focus();
this.sketchbookCompositeWidget.activate();
}

protected override onResize(message: Widget.ResizeMessage): void {
Expand Down