|
| 1 | +// Copyright 2025 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import * as i18n from '../../core/i18n/i18n.js'; |
| 6 | +import * as Workspace from '../../models/workspace/workspace.js'; |
| 7 | +import type * as TextEditor from '../../ui/components/text_editor/text_editor.js'; |
| 8 | +import * as SourceFrame from '../../ui/legacy/components/source_frame/source_frame.js'; |
| 9 | +import * as UI from '../../ui/legacy/legacy.js'; |
| 10 | + |
| 11 | +import {Plugin} from './Plugin.js'; |
| 12 | + |
| 13 | +const UIStrings = { |
| 14 | + /** |
| 15 | + *@description Infobar text announcing that the file contents have been changed by AI |
| 16 | + */ |
| 17 | + aiContentWarning: 'This file contains AI-generated content', |
| 18 | +} as const; |
| 19 | +const str_ = i18n.i18n.registerUIStrings('panels/sources/AiWarningInfobarPlugin.ts', UIStrings); |
| 20 | +const i18nString = i18n.i18n.getLocalizedString.bind(undefined, str_); |
| 21 | + |
| 22 | +export class AiWarningInfobarPlugin extends Plugin { |
| 23 | + #editor: TextEditor.TextEditor.TextEditor|undefined = undefined; |
| 24 | + #aiWarningInfobar: UI.Infobar.Infobar|null = null; |
| 25 | + |
| 26 | + constructor(uiSourceCode: Workspace.UISourceCode.UISourceCode) { |
| 27 | + super(uiSourceCode); |
| 28 | + this.uiSourceCode.addEventListener( |
| 29 | + Workspace.UISourceCode.Events.WorkingCopyCommitted, this.#onWorkingCopyCommitted, this); |
| 30 | + } |
| 31 | + |
| 32 | + override dispose(): void { |
| 33 | + this.#aiWarningInfobar?.dispose(); |
| 34 | + this.#aiWarningInfobar = null; |
| 35 | + this.uiSourceCode.removeEventListener( |
| 36 | + Workspace.UISourceCode.Events.WorkingCopyCommitted, this.#onWorkingCopyCommitted, this); |
| 37 | + super.dispose(); |
| 38 | + } |
| 39 | + |
| 40 | + static override accepts(uiSourceCode: Workspace.UISourceCode.UISourceCode): boolean { |
| 41 | + return uiSourceCode.contentType().hasScripts() || uiSourceCode.contentType().hasStyleSheets(); |
| 42 | + } |
| 43 | + |
| 44 | + override editorInitialized(editor: TextEditor.TextEditor.TextEditor): void { |
| 45 | + this.#editor = editor; |
| 46 | + if (this.uiSourceCode.containsAiChanges()) { |
| 47 | + this.#showAiWarningInfobar(); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + #onWorkingCopyCommitted(): void { |
| 52 | + if (!this.uiSourceCode.containsAiChanges()) { |
| 53 | + this.#aiWarningInfobar?.dispose(); |
| 54 | + this.#aiWarningInfobar = null; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + #showAiWarningInfobar(): void { |
| 59 | + const infobar = new UI.Infobar.Infobar( |
| 60 | + UI.Infobar.Type.WARNING, i18nString(UIStrings.aiContentWarning), undefined, undefined, |
| 61 | + 'contains-ai-content-warning'); |
| 62 | + this.#aiWarningInfobar = infobar; |
| 63 | + infobar.setCloseCallback(() => this.removeInfobar(this.#aiWarningInfobar)); |
| 64 | + this.attachInfobar(this.#aiWarningInfobar); |
| 65 | + } |
| 66 | + |
| 67 | + attachInfobar(bar: UI.Infobar.Infobar): void { |
| 68 | + if (this.#editor) { |
| 69 | + this.#editor.dispatch({effects: SourceFrame.SourceFrame.addInfobar.of(bar)}); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + removeInfobar(bar: UI.Infobar.Infobar|null): void { |
| 74 | + if (this.#editor && bar) { |
| 75 | + this.#editor.dispatch({effects: SourceFrame.SourceFrame.removeInfobar.of(bar)}); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments