Skip to content

Commit a1554c1

Browse files
Lightning00BladeDevtools-frontend LUCI CQ
authored andcommitted
[Freestyler] Update Toolbar UI with props
This makes the button disabled if the correct criteria is not met. This makes the UX much better as you can't press a button that is disabled + A11Y is improved. Bug: 351752761 Change-Id: Iceac861b1c037cdb85238b523f3184fab82d9676 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/5987811 Reviewed-by: Ergün Erdoğmuş <[email protected]> Auto-Submit: Nikolay Vitkov <[email protected]> Commit-Queue: Nikolay Vitkov <[email protected]>
1 parent e46bf56 commit a1554c1

File tree

1 file changed

+61
-58
lines changed

1 file changed

+61
-58
lines changed

front_end/panels/freestyler/FreestylerPanel.ts

Lines changed: 61 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -113,55 +113,6 @@ function selectedElementFilter(maybeNode: SDK.DOMModel.DOMNode|null): SDK.DOMMod
113113
return null;
114114
}
115115

116-
// TODO(ergunsh): Use the WidgetElement instead of separately creating the toolbar.
117-
function createToolbar(
118-
target: HTMLElement,
119-
{onHistoryClick, onNewAgentClick, onDeleteClick}:
120-
{onHistoryClick: (event: Event) => void, onNewAgentClick: () => void, onDeleteClick: () => void}): void {
121-
const toolbarContainer = target.createChild('div', 'freestyler-toolbar-container');
122-
const leftToolbar = new UI.Toolbar.Toolbar('freestyler-left-toolbar', toolbarContainer);
123-
const rightToolbar = new UI.Toolbar.Toolbar('freestyler-right-toolbar', toolbarContainer);
124-
125-
const clearButton =
126-
new UI.Toolbar.ToolbarButton(i18nString(UIStrings.newChat), 'plus', undefined, 'freestyler.new-chat');
127-
clearButton.addEventListener(UI.Toolbar.ToolbarButton.Events.CLICK, onNewAgentClick);
128-
leftToolbar.appendToolbarItem(clearButton);
129-
leftToolbar.appendSeparator();
130-
const historyButton =
131-
new UI.Toolbar.ToolbarButton(i18nString(UIStrings.history), 'history', undefined, 'freestyler.history');
132-
historyButton.addEventListener(UI.Toolbar.ToolbarButton.Events.CLICK, event => {
133-
onHistoryClick(event.data);
134-
});
135-
leftToolbar.appendToolbarItem(historyButton);
136-
const deleteButton =
137-
new UI.Toolbar.ToolbarButton(i18nString(UIStrings.clearChat), 'bin', undefined, 'freestyler.delete');
138-
deleteButton.addEventListener(UI.Toolbar.ToolbarButton.Events.CLICK, onDeleteClick);
139-
leftToolbar.appendToolbarItem(deleteButton);
140-
141-
const link = UI.XLink.XLink.create(
142-
AI_ASSISTANCE_SEND_FEEDBACK, i18nString(UIStrings.sendFeedback), undefined, undefined,
143-
'freestyler.send-feedback');
144-
link.style.setProperty('display', null);
145-
link.style.setProperty('text-decoration', 'none');
146-
link.style.setProperty('padding', '0 var(--sys-size-3)');
147-
const linkItem = new UI.Toolbar.ToolbarItem(link);
148-
rightToolbar.appendToolbarItem(linkItem);
149-
150-
rightToolbar.appendSeparator();
151-
const helpButton = new UI.Toolbar.ToolbarButton(i18nString(UIStrings.help), 'help', undefined, 'freestyler.help');
152-
helpButton.addEventListener(UI.Toolbar.ToolbarButton.Events.CLICK, () => {
153-
Host.InspectorFrontendHost.InspectorFrontendHostInstance.openInNewTab(AI_ASSISTANCE_HELP);
154-
});
155-
rightToolbar.appendToolbarItem(helpButton);
156-
157-
const settingsButton =
158-
new UI.Toolbar.ToolbarButton(i18nString(UIStrings.settings), 'gear', undefined, 'freestyler.settings');
159-
settingsButton.addEventListener(UI.Toolbar.ToolbarButton.Events.CLICK, () => {
160-
void UI.ViewManager.ViewManager.instance().showView('chrome-ai');
161-
});
162-
rightToolbar.appendToolbarItem(settingsButton);
163-
}
164-
165116
function defaultView(input: FreestylerChatUiProps, output: ViewOutput, target: HTMLElement): void {
166117
// clang-format off
167118
LitHtml.render(html`
@@ -217,6 +168,13 @@ export class FreestylerPanel extends UI.Panel.Panel {
217168
#freestylerEnabledSetting: Common.Settings.Setting<boolean>|undefined;
218169
#changeManager = new ChangeManager();
219170

171+
#newChatButton =
172+
new UI.Toolbar.ToolbarButton(i18nString(UIStrings.newChat), 'plus', undefined, 'freestyler.new-chat');
173+
#historyEntriesButton =
174+
new UI.Toolbar.ToolbarButton(i18nString(UIStrings.history), 'history', undefined, 'freestyler.history');
175+
#deleteHistoryEntryButton =
176+
new UI.Toolbar.ToolbarButton(i18nString(UIStrings.clearChat), 'bin', undefined, 'freestyler.delete');
177+
220178
#agents = new Set<AiAgent<unknown>>();
221179
#currentAgent?: AiAgent<unknown>;
222180

@@ -228,11 +186,7 @@ export class FreestylerPanel extends UI.Panel.Panel {
228186
super(FreestylerPanel.panelName);
229187
this.#freestylerEnabledSetting = this.#getAiAssistanceEnabledSetting();
230188

231-
createToolbar(this.contentElement, {
232-
onNewAgentClick: this.#clearMessages.bind(this),
233-
onHistoryClick: this.#onHistoryClicked.bind(this),
234-
onDeleteClick: this.#onDeleteClicked.bind(this),
235-
});
189+
this.#createToolbar();
236190
this.#toggleSearchElementAction =
237191
UI.ActionRegistry.ActionRegistry.instance().getAction('elements.toggle-element-search');
238192
this.#aidaClient = aidaClient;
@@ -265,6 +219,47 @@ export class FreestylerPanel extends UI.Panel.Panel {
265219
};
266220
}
267221

222+
#createToolbar(): void {
223+
const toolbarContainer = this.contentElement.createChild('div', 'freestyler-toolbar-container');
224+
const leftToolbar = new UI.Toolbar.Toolbar('freestyler-left-toolbar', toolbarContainer);
225+
const rightToolbar = new UI.Toolbar.Toolbar('freestyler-right-toolbar', toolbarContainer);
226+
227+
this.#newChatButton.addEventListener(UI.Toolbar.ToolbarButton.Events.CLICK, this.#clearMessages.bind(this));
228+
leftToolbar.appendToolbarItem(this.#newChatButton);
229+
leftToolbar.appendSeparator();
230+
231+
this.#historyEntriesButton.addEventListener(UI.Toolbar.ToolbarButton.Events.CLICK, event => {
232+
this.#onHistoryClicked(event.data);
233+
});
234+
leftToolbar.appendToolbarItem(this.#historyEntriesButton);
235+
this.#deleteHistoryEntryButton.addEventListener(
236+
UI.Toolbar.ToolbarButton.Events.CLICK, this.#onDeleteClicked.bind(this));
237+
leftToolbar.appendToolbarItem(this.#deleteHistoryEntryButton);
238+
239+
const link = UI.XLink.XLink.create(
240+
AI_ASSISTANCE_SEND_FEEDBACK, i18nString(UIStrings.sendFeedback), undefined, undefined,
241+
'freestyler.send-feedback');
242+
link.style.setProperty('display', null);
243+
link.style.setProperty('text-decoration', 'none');
244+
link.style.setProperty('padding', '0 var(--sys-size-3)');
245+
const linkItem = new UI.Toolbar.ToolbarItem(link);
246+
rightToolbar.appendToolbarItem(linkItem);
247+
248+
rightToolbar.appendSeparator();
249+
const helpButton = new UI.Toolbar.ToolbarButton(i18nString(UIStrings.help), 'help', undefined, 'freestyler.help');
250+
helpButton.addEventListener(UI.Toolbar.ToolbarButton.Events.CLICK, () => {
251+
Host.InspectorFrontendHost.InspectorFrontendHostInstance.openInNewTab(AI_ASSISTANCE_HELP);
252+
});
253+
rightToolbar.appendToolbarItem(helpButton);
254+
255+
const settingsButton =
256+
new UI.Toolbar.ToolbarButton(i18nString(UIStrings.settings), 'gear', undefined, 'freestyler.settings');
257+
settingsButton.addEventListener(UI.Toolbar.ToolbarButton.Events.CLICK, () => {
258+
void UI.ViewManager.ViewManager.instance().showView('chrome-ai');
259+
});
260+
rightToolbar.appendToolbarItem(settingsButton);
261+
}
262+
268263
#getChatUiState(): FreestylerChatUiState {
269264
const config = Common.Settings.Settings.instance().getHostConfig();
270265
const blockedByAge = config.aidaAvailability?.blockedByAge === true;
@@ -293,6 +288,17 @@ export class FreestylerPanel extends UI.Panel.Panel {
293288
}
294289
}
295290

291+
#updateToolbarState(): void {
292+
this.#historyEntriesButton.applyEnabledState([...this.#agents].some(agent => !agent.isEmpty));
293+
this.#deleteHistoryEntryButton.applyEnabledState(Boolean(this.#currentAgent && !this.#currentAgent.isEmpty));
294+
/*
295+
* If there is no agent disable new chat button
296+
* If the agent is empty disable new chat button
297+
*/
298+
const newChatEnabled = this.#currentAgent ? (this.#currentAgent.isEmpty ? false : true) : false;
299+
this.#newChatButton.applyEnabledState(newChatEnabled);
300+
}
301+
296302
#createFreestylerAgent(): FreestylerAgent {
297303
const agent = new FreestylerAgent({
298304
aidaClient: this.#aidaClient,
@@ -471,6 +477,7 @@ export class FreestylerPanel extends UI.Panel.Panel {
471477
};
472478

473479
doUpdate(): void {
480+
this.#updateToolbarState();
474481
this.view(this.#viewProps, this.#viewOutput, this.#contentContainer);
475482
}
476483

@@ -566,10 +573,6 @@ export class FreestylerPanel extends UI.Panel.Panel {
566573
}
567574

568575
#onHistoryClicked(event: Event): void {
569-
if ([...this.#agents].every(agent => agent.isEmpty)) {
570-
return;
571-
}
572-
573576
const contextMenu = new UI.ContextMenu.ContextMenu(event);
574577

575578
for (const agent of [...this.#agents].reverse()) {

0 commit comments

Comments
 (0)