Files
likang 7e01147566
CI / typecheck + test + build (push) Has been cancelled
Initial commit: import VibeMake PCB Studio
2026-11-03 11:20:08 +08:00

224 lines
8.9 KiB
TypeScript

// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (C) 2026 ZiroEDA and contributors.
// Portions derived from KiCad, copyright The KiCad Developers. See NOTICE.md.
import { wxCANCEL, wxCENTER, wxICON_WARNING, wxOK, wxOK_DEFAULT } from '@ziroeda/wx/defs.js';
import { wxID_CANCEL, wxID_OK } from '@ziroeda/wx/menu.js';
/**
* The "Save Changes?" question, and what each answer means. Counterpart:
* `common/confirm.cpp` (`UnsavedChangesDialog` / `HandleUnsavedChanges`).
*
* KiCad asks this once, from `common/`, and every frame that can hold unsaved
* work calls it: eeschema, pcbnew, the symbol and footprint editors, cvpcb.
* The wording, the button labels, the button order and which button is default
* are therefore the same everywhere, and so is the rule that turns the answer
* into "may I close?". That rule lives here rather than in the React dialog so
* it can be tested without a DOM, and so the dialog stays a rendering of it.
*
* The three answers are not symmetric and the difference is the whole point of
* having three: only *cancel* aborts the close. "Discard Changes" closes and
* loses the work — deliberately — and "Save" closes only if the save succeeds,
* which is why the caller hands in a function returning whether it did.
* A two-way "discard?" prompt, which is what several of our dialogs grew
* instead, offers no way to keep the work at all: the user who hit Esc by
* mistake can only lose it or go back and save by hand.
*/
/** The button the user pressed: `wxID_YES` / `wxID_NO` / `wxID_CANCEL`. */
export type UnsavedChangesResult = 'save' | 'discard' | 'cancel';
/** The dialog's window title. */
export const UNSAVED_CHANGES_TITLE = 'Save Changes?';
/** `SetExtendedMessage`, the grey sub-line under the question. */
export const UNSAVED_CHANGES_EXTENDED =
"If you don't save, all your changes will be permanently lost.";
/** `SetYesNoLabels( _( "&Save" ), _( "&Discard Changes" ) )`. */
export const UNSAVED_CHANGES_SAVE_LABEL = 'Save';
export const UNSAVED_CHANGES_DISCARD_LABEL = 'Discard Changes';
export const UNSAVED_CHANGES_CANCEL_LABEL = 'Cancel';
/**
* `ConfirmRevertDialog` (`common/confirm.cpp:137-145`), the other question
* `common/` asks on behalf of every frame that can revert: eeschema, pcbnew and
* the symbol and footprint editors all call this one function, so the wording
* and the two button labels are the same in all four.
*
* KICAD_MESSAGE_DIALOG_BASE dlg( parent, aMessage, wxEmptyString,
* wxOK | wxCANCEL | wxOK_DEFAULT | … );
* dlg.SetExtendedMessage( _( "Your current changes will be permanently lost." ) );
* dlg.SetOKCancelLabels( _( "&Revert" ), _( "&Cancel" ) );
*
* Unlike the unsaved-changes question this one is genuinely two-way: there is
* nothing left to save, so OK throws the edits away and Cancel keeps them.
* `wxOK_DEFAULT` puts the focus on Revert.
*/
export const CONFIRM_REVERT_EXTENDED = 'Your current changes will be permanently lost.';
/** `SetOKCancelLabels( _( "&Revert" ), _( "&Cancel" ) )`. */
export const CONFIRM_REVERT_OK_LABEL = 'Revert';
export const CONFIRM_REVERT_CANCEL_LABEL = 'Cancel';
/**
* The caller's `aMessage`. Both editors that revert a library item format the
* same sentence — `FOOTPRINT_EDIT_FRAME::RevertFootprint`
* (`pcbnew/footprint_libraries_utils.cpp:1195`) and `SYMBOL_EDIT_FRAME`'s
* counterpart — so it belongs here rather than in either of them.
*/
export function confirmRevertMessage(itemName: string): string {
return `Revert '${itemName}' to last version saved?`;
}
/**
* `ConfirmRevertDialog( aParent, aMessage )` (common/confirm.cpp:137): the question above, with its
* extended message and Revert / Cancel; true is Revert.
*/
export async function ConfirmRevertDialog(aMessage: string): Promise<boolean> {
return (
(await ShowKicadMessageDialog({
message: aMessage,
caption: '',
style: wxOK | wxCANCEL | wxOK_DEFAULT | wxICON_WARNING | wxCENTER,
extended: CONFIRM_REVERT_EXTENDED,
okLabel: CONFIRM_REVERT_OK_LABEL,
cancelLabel: CONFIRM_REVERT_CANCEL_LABEL,
})) === wxID_OK
);
}
/**
* `HandleUnsavedChanges`: run the answer and report whether the caller may go
* on with whatever it was doing (closing, usually).
*
* case wxID_YES: return aSaveFunction();
* case wxID_NO: return true; // proceed without saving
* default:
* case wxID_CANCEL: return false;
*
* `save` returns whether the save succeeded; a failed save keeps the window
* open, because the alternative is throwing the work away on the user's behalf.
*/
export function handleUnsavedChanges(result: UnsavedChangesResult, save: () => boolean): boolean {
switch (result) {
case 'save':
return save();
case 'discard':
return true;
default:
return false;
}
}
/** What shows the error box: installed by the app at startup (`confirm_ui.tsx`). */
type ErrorPresenter = (aText: string, aExtraInfo: string) => void;
let s_errorPresenter: ErrorPresenter = (aText, aExtraInfo) =>
// No window yet (a test, a worker): the message still goes somewhere.
console.error(aExtraInfo ? `${aText}\n${aExtraInfo}` : aText);
/** Install the modal that `DisplayErrorMessage` shows. */
export function SetErrorPresenter(aPresenter: ErrorPresenter): void {
s_errorPresenter = aPresenter;
}
/**
* `DisplayErrorMessage( aParent, aText, aExtraInfo )` (common/confirm.cpp):
* the modal error box, caption "Error", one OK button.
*/
export function DisplayErrorMessage(aText: string, aExtraInfo = ''): void {
s_errorPresenter(aText, aExtraInfo);
}
/** What shows the Yes/No question: installed by the app at startup (`confirm_ui.tsx`). */
type QuestionPresenter = (aMessage: string) => Promise<boolean>;
// No window (a test, a worker): nothing can answer, and IsOK's default is
// the destructive-safe one.
let s_questionPresenter: QuestionPresenter = () => Promise.resolve(false);
/** Install the modal that `IsOK` shows. */
export function SetQuestionPresenter(aPresenter: QuestionPresenter): void {
s_questionPresenter = aPresenter;
}
/**
* `IsOK( aParent, aMessage )` (common/confirm.cpp:278-300): a modal "Confirmation"
* question with the question icon and Yes/No buttons - the OK/Cancel dialog
* relabelled, so Yes is the default and Esc is No. Resolves true on Yes.
*/
export function IsOK(aMessage: string): Promise<boolean> {
return s_questionPresenter(aMessage);
}
/** What shows the information box: installed by the app at startup (`confirm_ui.tsx`). */
type InfoPresenter = (aMessage: string, aExtraInfo: string) => Promise<void>;
let s_infoPresenter: InfoPresenter = (aMessage, aExtraInfo) => {
// No window: `wxLogTrace( traceConfirm, "%s %s", aMessage, aExtraInfo )`.
console.info(aExtraInfo ? `${aMessage} ${aExtraInfo}` : aMessage);
return Promise.resolve();
};
/** Install the modal that `DisplayInfoMessage` shows. */
export function SetInfoPresenter(aPresenter: InfoPresenter): void {
s_infoPresenter = aPresenter;
}
/**
* `DisplayInfoMessage( aParent, aMessage, aExtraInfo )` (common/confirm.cpp:249):
* the modal information box, caption "Information", one OK button.
*/
export function DisplayInfoMessage(aMessage: string, aExtraInfo = ''): Promise<void> {
return s_infoPresenter(aMessage, aExtraInfo);
}
/**
* A `KICAD_MESSAGE_DIALOG` (confirm.h: wxRichMessageDialog, the native message box on GTK) as a
* tool builds it: the message, the caption, the wx style word, `SetExtendedMessage`, and
* `SetOKCancelLabels`.
*/
export interface KICAD_MESSAGE_DIALOG_ARG {
message: string;
caption: string;
style: number;
extended?: string;
okLabel?: string;
cancelLabel?: string;
}
type MessageDialogPresenter = (aArg: KICAD_MESSAGE_DIALOG_ARG) => Promise<number>;
let s_messageDialogPresenter: MessageDialogPresenter = async () => wxID_CANCEL;
/** Install the modal that `ShowKicadMessageDialog` shows. */
export function SetMessageDialogPresenter(aPresenter: MessageDialogPresenter): void {
s_messageDialogPresenter = aPresenter;
}
/** `KICAD_MESSAGE_DIALOG( … ).ShowModal()`: wxID_OK or wxID_CANCEL. */
export function ShowKicadMessageDialog(aArg: KICAD_MESSAGE_DIALOG_ARG): Promise<number> {
return s_messageDialogPresenter(aArg);
}
/**
* `OKOrCancelDialog( aParent, aWarning, aMessage, aDetailedMessage, aOKLabel, aCancelLabel )`
* (common/confirm.cpp:169): a warning box whose caption is `aWarning`, OK the default; wxID_OK or
* wxID_CANCEL. The "Apply to all" checkbox is not here; no caller in the port passes it.
*/
export function OKOrCancelDialog(
aWarning: string,
aMessage: string,
aDetailedMessage = '',
aOKLabel = '',
aCancelLabel = '',
): Promise<number> {
return ShowKicadMessageDialog({
message: aMessage,
caption: aWarning,
style: wxOK | wxCANCEL | wxOK_DEFAULT | wxICON_WARNING | wxCENTER,
...(aDetailedMessage ? { extended: aDetailedMessage } : {}),
okLabel: aOKLabel === '' ? 'OK' : aOKLabel,
cancelLabel: aCancelLabel === '' ? 'Cancel' : aCancelLabel,
});
}