Files
likang 1fde96c05e
CI / typecheck + test + build (push) Has been cancelled
打通vibemake
2026-11-05 16:21:37 +08:00

7135 lines
312 KiB
TypeScript

// SPDX-License-Identifier: GPL-3.0-or-later
import { projectSnapshotHost } from '@ziroeda/common/project_snapshot.js';
import { useProjectSnapshot } from '@ziroeda/common/use_project_snapshot.js';
// Copyright (C) 2026 ZiroEDA and contributors.
// Portions derived from KiCad, copyright The KiCad Developers. See NOTICE.md.
import type { HOSTED_LIBRARY } from '@ziroeda/common/libraries/library_manager.js';
import { Reporter } from '@ziroeda/common/reporter.js';
import type { INCREMENT_ANNOTATIONS_VALUES } from './tools/sch_editor_control.js';
import { ANNOTATE_ALGO_T, ANNOTATE_ORDER_T, ANNOTATE_SCOPE_T } from './sch_reference_list.js';
import { SYMBOL_FILTER } from './sch_sheet_path.js';
import { PANEL_EMBEDDED_FILES } from '@ziroeda/common/dialogs/panel_embedded_files.js';
import { EMBEDDED_FILES } from '@ziroeda/common/embedded_files.js';
import { SCH_ACTIONS } from './tools/sch_actions.js';
import { SCH_LAYER_ID } from '@ziroeda/common/layer_id.js';
import { toCss } from '@ziroeda/common/gal/color4d.js';
import { NET_NAVIGATOR_ITEM_DATA } from './net_navigator.js';
import {
GetClipboardText,
SetClipboardFromPaste,
SetClipboardFromText,
} from '@ziroeda/common/clipboard.js';
import { defaultUnitsToggle } from '@ziroeda/common/settings/app_settings_units.js';
import { SCH_SELECTION_TOOL } from './tools/sch_selection_tool.js';
import { SCH_DRAWING_TOOLS } from './tools/sch_drawing_tools.js';
import { ACTIONS } from '@ziroeda/common/tool/actions.js';
import type { TOOL_ACTION } from '@ziroeda/common/tool/tool_action.js';
import type { SCH_TEXT } from './sch_text.js';
import type { SCH_TEXTBOX } from './sch_textbox.js';
import type { SCH_SHEET_PIN } from './sch_sheet_pin.js';
import type { SCH_LABEL_BASE } from './sch_label.js';
import { SCH_COMMIT } from './sch_commit.js';
import type { SCH_FIELD } from './sch_field.js';
import type { SCH_SHEET } from './sch_sheet.js';
import type { SCH_SYMBOL } from './sch_symbol.js';
import type { SCH_TABLE } from './sch_table.js';
import type { SCH_TABLECELL } from './sch_tablecell.js';
import type { FILEDLG_HOOK, SHEET_PROPERTIES_RESULT } from './sch_edit_frame.js';
import { FiledlgImportSheetContentsControls } from './widgets/sch_design_block_pane_ui.js';
import type { SCH_BITMAP } from './sch_bitmap.js';
import type { SCH_SHAPE } from './sch_shape.js';
import {
DIALOG_WIRE_BUS_PROPERTIES,
DialogWireBusProperties,
type WIRE_BUS_DIALOG_VALUES,
} from './dialogs/dialog_wire_bus_properties.js';
import type { SCH_ITEM } from './sch_item.js';
import {
DIALOG_JUNCTION_PROPS,
DialogJunctionProps,
type JUNCTION_DIALOG_VALUES,
} from './dialogs/dialog_junction_props.js';
import type { SCH_JUNCTION } from './sch_junction.js';
import { type ChooserFilter, WxFileDialog, wxFileDialogGetPath } from '@ziroeda/wx/filedlg.js';
import { WxTextEntryDialog } from '@ziroeda/wx/textdlg.js';
import {
type FILEDLG_HOOK_NEW_LIBRARY,
FileDlgHookNewLibraryControls,
type wxFileDialogCustomizeHook,
} from '@ziroeda/common/widgets/filedlg_hook_new_library.js';
import { WX_TEXT_ENTRY_DIALOG } from '@ziroeda/common/dialogs/dialog_text_entry.js';
import {
wxCANCEL,
wxFD_FILE_MUST_EXIST,
wxFD_OPEN,
wxFD_SAVE,
wxICON_WARNING,
wxOK,
} from '@ziroeda/wx/defs.js';
import { wxID_CANCEL, wxID_OK } from '@ziroeda/wx/menu.js';
import {
DisplayErrorMessage as DbDisplayErrorMessage,
handleUnsavedChanges,
IsOK as DbIsOK,
type KICAD_MESSAGE_DIALOG_ARG,
OKOrCancelDialog as DbOKOrCancelDialog,
ShowKicadMessageDialog,
type UnsavedChangesResult,
} from '@ziroeda/common/confirm.js';
import { DialogDesignBlockProperties } from '@ziroeda/common/dialogs/dialog_design_block_properties_ui.js';
import type { DESIGN_BLOCK } from '@ziroeda/common/design_block.js';
import {
GetUseGlobalTable,
MakeFileDlgHookNewLibrary,
} from '@ziroeda/common/widgets/filedlg_hook_new_library.js';
import { PATHS } from '@ziroeda/common/paths.js';
import { KiCadDesignBlockLibPathExtension } from '@ziroeda/common/wildcards_and_files_ext.js';
import { SCH_DESIGN_BLOCK_PANE } from './widgets/sch_design_block_pane.js';
import { SchDesignBlockPane } from './widgets/sch_design_block_pane_ui.js';
import { SchDesignBlockPreviewWidget } from './widgets/sch_design_block_preview_widget.js';
import { SCH_DESIGN_BLOCK_CONTROL } from './tools/sch_design_block_control.js';
import { eeschemaKifaceSettings } from './eeschema_settings.js';
import { UnsavedChangesDialog } from '@ziroeda/common/dialogs/dialog_unsaved_changes.js';
import { EdaListDialog } from '@ziroeda/common/dialogs/eda_list_dialog.js';
import { LIB_ID } from '@ziroeda/common/lib_id.js';
import { FIELD_T, GetCanonicalFieldName } from '@ziroeda/common/template_fieldnames.js';
import type { PICKED_SYMBOL } from './sch_screen.js';
import type { SYMBOL_LIBRARY_FILTER } from './symbol_library_common.js';
import type { SchScriptApi } from './sch_script_api.js';
import { GetAssociatedDocument } from '@ziroeda/common/eda_doc.js';
import * as KIPLATFORM_UI from '@ziroeda/kiplatform/ui.js';
import type { STATUS_TEXT_POPUP } from '@ziroeda/common/status_popup.js';
import {
ensureFileExtension,
iuToMM,
KICAD_SCHEMATIC_FILE_EXTENSION,
mmToIU,
RPT_SEVERITY_ACTION,
RPT_SEVERITY_ERROR,
type ReportLine,
SCH_IU_PER_MM,
type WksSheet,
} from '@ziroeda/common';
import { resolveActiveSheet, readSheetRef, writeSheetRefText } from '@ziroeda/common';
import { Fragment, useEffect, useMemo, useRef, useState, useCallback } from 'react';
import { FRAME_T } from '@ziroeda/common/frame_type.js';
import type { KIWAY } from '@ziroeda/common/kiway.js';
import { SCH_EDIT_FRAME } from './sch_edit_frame.js';
import { parse } from '@ziroeda/sexpr';
import {
applySchematicPatch,
diffSchematic,
schematicPatchIsEmpty,
type SchematicPatch,
} from './browser/sch_diff.js';
import type {
PeerRole,
PresenceInfo,
ProjectSyncTransport,
} from './browser/project_sync_transport.js';
import { ReadOnlyNotice } from '@ziroeda/common/widgets/wx_infobar.js';
import {
type ArcEditMode,
incrementArcEditMode,
directiveNetclassAssignments,
ruleAreaNetclassAssignments,
type LabelSpin,
readSchematic,
serializeSchematic,
readSymbolLib,
serializeSymbolLib,
type ProjectStep,
type ProjectEdit,
computeNetlist,
withCleanup,
refId,
instanceKey,
getSheetPageNumber,
getRootPageNumber,
getPageSettings,
selectionHasGroup,
defaultSelectionFilter,
type SelectionFilterOptions,
annotateSymbols,
defaultAnnotateOptions,
type AnnotateSheet,
type AnnotateOptions,
buildSheetTree,
repairPageNumbersOnLoad,
findRootFile,
makeImage,
ProjectHistory,
type Schematic,
type SchImage,
type LibSymbol,
type SchSymbol,
type EditCommand,
type LabelShape,
type PastePayload,
type SheetTreeNode,
} from './index.js';
import type { PendingLabel } from './sch_draw_panel.js';
import {
DIALOG_LABEL_PROPERTIES,
DialogLabelProperties,
type LABEL_DIALOG_VALUES,
} from './dialogs/dialog_label_properties.js';
import type { GRID_TEXT_BUTTON_HOST } from '@ziroeda/common/widgets/grid_text_button_helpers.js';
import {
DIALOG_TEXT_PROPERTIES,
DialogTextProperties,
type TEXT_DIALOG_VALUES,
} from './dialogs/dialog_text_properties.js';
import {
DIALOG_SYMBOL_PROPERTIES,
DialogSymbolProperties,
type SYMBOL_DIALOG_VALUES,
} from './dialogs/dialog_symbol_properties.js';
import { DIALOG_ERC, DialogErc, type DIALOG_ERC_WINDOW } from './dialogs/dialog_erc.js';
import type { PickedSymbol, SymbolChooserResult } from './picksymbol.js';
import { repairSourceLibs } from './browser/repair_source.js';
import {
findRescues,
rescueDocumentCommand,
rescueLibraryFileName,
rescueLibraryNickname,
rescuedDefinition,
type RescueCandidate,
} from './project_rescue.js';
import type { RescueInstance } from './project_rescue.js';
import {
legacyCacheFileNames,
readLegacySymbolLibrary,
} from './sch_io/kicad_legacy/sch_io_kicad_legacy_lib_cache.js';
import { legacySchLibs, SymbolLibAdapter } from './project_sch.js';
import {
legacyLibrarySymbols,
legacyRootFile,
readLegacyProject,
} from './sch_io/kicad_legacy/sch_io_kicad_legacy.js';
import {
projectSymLibTable,
projectSymLibTablePath,
serializeSymLibTable,
} from './project_sym_lib_table.js';
import {
projectFpLibTablePath,
serializeFpLibTable,
type FpLibRow,
} from '@ziroeda/common/fp_lib_table.js';
import { Toolbar } from '@ziroeda/common/tool/action_toolbar.js';
import { kicadSchematicWildcard } from '@ziroeda/common/wildcards_and_files_ext.js';
import { RIGHT_TOOLBAR_COMMANDS, SCH_DEFAULT_TOOLBARS } from './toolbars_sch_editor.js';
import { MenuBar, ContextMenu, type Menu } from '@ziroeda/wx/menu_ui.js';
import { buildMenus } from './menubar.js';
import {
CONFIRMATION_CAPTION,
LOAD_REPAIRED_MESSAGE,
revertPromptMessage,
savedFileMessage,
} from './files-io.js';
import {
MessageDialogOk,
MessageDialogYesNo,
MessageDialogYesNoCancel,
} from '@ziroeda/common/dialogs/dialog_message.js';
import { Pgm } from '@ziroeda/common/pgm_base.js';
import {
LIBRARY_TABLE,
LIBRARY_TABLE_SCOPE,
LIBRARY_TABLE_TYPE,
} from '@ziroeda/common/libraries/library_table.js';
import { jsonFileWildcard, reportFileWildcard } from '@ziroeda/common/wildcards_and_files_ext.js';
import { KICAD_T } from '@ziroeda/core/typeinfo.js';
import { fetchHostedSymbolFile } from './libraries/symbol_library_adapter.js';
import { type SchErcLine, schErcLines } from './sch_script_api.js';
import { INFO_CAPTION } from '@ziroeda/common/confirm_types.js';
import { dispatchMenuHotkey, focusBlocksHotkey } from '@ziroeda/common/tool/action_menu_hotkeys.js';
import { wasBrowserSuppressed, type FocusLike } from '@ziroeda/common/browser_hotkeys.js';
import { DialogAssignNetclass } from '@ziroeda/common/dialogs/dialog_assign_netclass.js';
import { showHotkeyList } from '@ziroeda/common/hotkeys_basic.js';
import {
DIALOG_TABLECELL_PROPERTIES,
DialogTableCellProperties,
type TABLECELL_DIALOG_VALUES,
} from './dialogs/dialog_tablecell_properties.js';
import {
SchNavigateTool,
firstInstanceOf,
flattenHierarchy,
parentPath,
type SheetRef,
} from './tools/sch_navigate_tool.js';
import { SchFindDialogHost } from './dialogs/dialog_sch_find.js';
import { DIALOG_INCREMENT_ANNOTATIONS_BASE } from './dialogs/dialog_increment_annotations_base.js';
import {
DialogGlobalEditTextAndGraphics,
DIALOG_GLOBAL_EDIT_TEXT_AND_GRAPHICS,
type GLOBAL_EDIT_VALUES,
} from './dialogs/dialog_global_edit_text_and_graphics.js';
import { DIALOG_CHANGE_SYMBOLS, DialogChangeSymbols } from './dialogs/dialog_change_symbols.js';
import type { DIALOG_CHANGE_SYMBOLS_MODE } from './tools/sch_edit_tool.js';
import {
DIALOG_EDIT_SYMBOLS_LIBID,
DialogEditSymbolsLibId,
} from './dialogs/dialog_edit_symbols_libid.js';
import { SingleChoiceDialog } from '@ziroeda/common/dialogs/dialog_single_choice.js';
import { DialogAnnotate, type AnnotateRun } from './dialogs/dialog_annotate.js';
import type { SCH_LINE } from './sch_line.js';
import {
DIALOG_LINE_PROPERTIES,
DialogLineProperties,
type LINE_DIALOG_VALUES,
} from './dialogs/dialog_line_properties.js';
import {
DIALOG_EESCHEMA_PAGE_SETTINGS,
DialogEeschemaPageSettings,
} from './dialogs/dialog_eeschema_page_settings.js';
// `DIALOG_PASTE_SPECIAL` is a `common/dialogs/` dialog upstream, built by
// eeschema AND pcbnew, so it is one module here too rather than a copy under
// this editor's own `dialogs/`. `SCH_EDITOR_CONTROL::Paste` supplies the two
// things that differ: the mode it opens on, and no `aDefaultRef`.
import {
DialogPasteSpecial,
type PasteSpecialMode,
} from '@ziroeda/common/dialogs/dialog_paste_special.js';
import {
DIALOG_SHEET_PROPERTIES,
DialogSheetProperties,
type SHEET_DIALOG_VALUES,
} from './dialogs/dialog_sheet_properties.js';
import { useKiDialog } from '@ziroeda/common/kidialog.js';
import type { PROJECT_FILE } from '@ziroeda/common/project/project_file.js';
import type { JsonValue } from '@ziroeda/common/settings/json_settings_internals.js';
import { BUS_ALIAS } from './bus_alias.js';
import {
DIALOG_SHAPE_PROPERTIES,
DialogShapeProperties,
type SHAPE_DIALOG_VALUES,
} from './dialogs/dialog_shape_properties.js';
import {
DIALOG_IMAGE_PROPERTIES,
DialogImageProperties,
} from './dialogs/dialog_image_properties.js';
import {
DIALOG_FIELD_PROPERTIES,
DialogFieldProperties,
type FIELD_DIALOG_VALUES,
} from './dialogs/dialog_field_properties.js';
import {
DIALOG_SHEET_PIN_PROPERTIES,
DialogSheetPinProperties,
type SHEET_PIN_DIALOG_VALUES,
} from './dialogs/dialog_sheet_pin_properties.js';
import {
DialogSchematicSetup,
defaultSchematicSetup,
type SchematicSetup,
} from './dialogs/dialog_schematic_setup.js';
import { LoadProjectSettings } from './eeschema_config.js';
import { SelectionFilterPanel } from './widgets/panel_sch_selection_filter_ui.js';
import { gridSizeToIU } from './eeschema_settings.js';
import {
findProjectPro,
readSchematicSetup,
readSchematicSetupText,
writeEquivalenceFilesText,
writeSchematicSetupText,
} from './project_settings.js';
import { resolveEffectiveNetClass } from './schematic_settings.js';
import { netClassHumanReadableName } from '@ziroeda/common/project/net_settings.js';
import type { PdfNetInfo } from './sch_plotter.js';
import type { Netlist } from './connectivity/nets.js';
import { DEFAULT_WIRE_WIDTH } from './sch_painter.js';
import { computeNetClassOverrides } from './net_overrides.js';
import {
REFDES_TRACKER,
buildPageRefsMap,
intersheetRefsText,
type IntersheetRefsConfig,
type IntersheetSheet,
} from './index.js';
import { schematicTextVarResolver } from './schematic.js';
import { ResolveShownText, type TextVarResolverFn } from '@ziroeda/common/common.js';
import { DialogExportNetlist } from './dialogs/dialog_export_netlist.js';
import {
DIALOG_SYMBOL_FIELDS_TABLE,
type DIALOG_SYMBOL_FIELDS_TABLE_WINDOW,
DialogSymbolFieldsTable,
} from './dialogs/dialog_symbol_fields_table.js';
import { DialogPrint } from './printing/dialog_print.js';
import { SCH_PRINTOUT } from './printing/sch_printout.js';
import { wxPrinter } from '@ziroeda/wx/printer.js';
import { DialogPlot, type PlotRequest } from './dialogs/dialog_plot_schematic.js';
import {
downloadBlob,
plotPng,
plotSvg,
plotPdf,
plotPdfSheets,
type PdfPlotSheet,
plotDxf,
plotPs,
renderSheetToCanvas,
type PlotOpts,
type PlotSink,
} from './sch_plotter.js';
import { BUILTIN_THEMES } from './sch_render_settings.js';
import { ProgressDialog, nextPaint } from '@ziroeda/common/widgets/wx_progress_reporters.js';
import type { ProgressSnapshot } from '@ziroeda/common/widgets/progress_reporter_snapshot.js';
import { ShowAboutDialog } from '@ziroeda/common/dialog_about/AboutDialog_main.js';
import { ABOUT_TITLES } from '@ziroeda/common/eda_base_frame_about_titles.js';
import type { PrefsPageId } from '@ziroeda/common/frame_type.js';
import type { EESCHEMA_APP } from './browser/eeschema_app.js';
import {
fastGridActionForKey,
fastGridIndex,
gridFeedback,
type FastGridAction,
} from '@ziroeda/common/settings/grid_settings_ui.js';
import { useHotkeyCyclePopup } from '@ziroeda/common/dialogs/hotkey_cycle_popup_ui.js';
import type { RenderOpts } from './sch_render_settings.js';
import type { InputPrefs } from '@ziroeda/common/view/wx_view_controls.js';
import { LiveSchPropertiesPanel } from './widgets/sch_properties_panel_ui.js';
import { SchSearchPane } from './widgets/sch_search_pane.js';
import { NetNavigatorPanel } from './widgets/net_navigator_panel.js';
import { DIALOG_UPDATE_FROM_PCB } from './dialogs/dialog_update_from_pcb.js';
import { setHeadlessPcbNetlistProvider } from './browser/headless_pcb_netlist.js';
import type { DIALOG_SYNC_SHEET_PINS } from './sync_sheet_pin/dialog_sync_sheet_pins.js';
import { DialogSyncSheetPins } from './sync_sheet_pin/dialog_sync_sheet_pins_ui.js';
import {
DIALOG_TABLE_PROPERTIES,
DialogTableProperties,
type SCH_TABLE_DIALOG_VALUES,
} from './dialogs/dialog_table_properties.js';
import { DialogImportGfx, type IMPORT_GFX_RESULT } from './import_gfx/dialog_import_gfx_sch.js';
import { GRAPHICS_IMPORTER_SCH } from './import_gfx/graphics_importer_sch.js';
import { KISTATUSBAR_FIELDS, KiStatusBar } from '@ziroeda/common/widgets/kistatusbar.js';
import { MsgPanel, type MsgPanelItem } from '@ziroeda/common/widgets/msgpanel_ui.js';
import {
gridMsg,
messageTextFromValue,
type StatusUnits,
unitsMsg,
} from '@ziroeda/common/widgets/kistatusbar_format.js';
import { formatTitle, useDocumentTitle } from '@ziroeda/common/use_document_title.js';
import { useLiveState } from '@ziroeda/common/use_live_state.js';
import { withSaveEnablement } from '@ziroeda/common/save_enablement.js';
import {
fileBaseName,
pathHumanReadable,
SCH_FRAME_NAME,
schFrameTitle,
} from './sch_edit_frame.js';
import {
SCH_BOTTOM_DOCK,
SCH_LEFT_PANE_ADD_ORDER,
schDockPosFrom,
schLeftDockLayout,
schPaneGrows,
schSelectionFilterShown,
type SchDockPos,
type SchLeftPane,
} from './sch_edit_frame.js';
import { DockSash } from '@ziroeda/wx/wx_aui_sash.js';
import { loadOutlineFontsFor } from '@ziroeda/common/font/outline_fonts.js';
import { useUnsavedGuard } from '@ziroeda/common/use_unsaved_guard.js';
import '@ziroeda/common/widgets/shell.css';
import { busJunctionIds as busJunctionIdsOf } from './connectivity/bus.js';
import {
LIVE_SCHEMATIC_MIRROR,
liveScreensToRecords,
loadRecordsDetached,
} from './sch_record_bridge.js';
import { createSchDrawPanel } from './browser/sch_canvas.js';
import type { SCH_DRAW_PANEL } from './sch_draw_panel.js';
import { loadBitmapFontImage } from '@ziroeda/common/gal/gal_window.js';
import { type SCH_SCREEN, SCH_SCREENS } from './sch_screen.js';
import type { ACTION_MENU } from '@ziroeda/common/tool/action_menu.js';
import { actionMenuItems } from '@ziroeda/common/tool/action_menu_popup.js';
import { wxMenuEvent, wxMenuEventType } from '@ziroeda/wx/menu.js';
import { MEMORY_FILESYSTEM, wxMountFileSystem } from '@ziroeda/wx/filefn.js';
import { schToolbarAction } from './toolbars_sch_editor.js';
import { symbolLibraryUri } from './cross-probing.js';
// What KiCad writes for File > New Schematic: an empty sheet on A4 paper.
// Launching the editor without a project starts here (no bundled demo).
/** A PICKED_SYMBOL as the chooser's history rows take it. */
function pickedToChooser(aPicked: PICKED_SYMBOL): PickedSymbol {
return {
libId: aPicked.LibId.Format(),
unit: aPicked.Unit,
fields: aPicked.Fields.map(([id, value]) => [GetCanonicalFieldName(id), value]),
};
}
/** The chooser's answer as PickSymbolFromLibrary returns it (picksymbol.cpp). */
function chooserToPicked(aResult: SymbolChooserResult): PICKED_SYMBOL {
const fields: [FIELD_T, string][] = [];
for (const [name, value] of aResult.fields) {
const id = [
FIELD_T.REFERENCE,
FIELD_T.VALUE,
FIELD_T.FOOTPRINT,
FIELD_T.DATASHEET,
FIELD_T.DESCRIPTION,
].find((f) => GetCanonicalFieldName(f) === name);
if (id !== undefined) fields.push([id, value]);
}
const libId = new LIB_ID();
libId.Parse(aResult.symbol.libId);
return {
LibId: libId,
Unit: aResult.unit,
Convert: 1,
KeepSymbol: aResult.keepSymbol,
PlaceAllUnits: aResult.placeAllUnits,
Fields: fields,
};
}
const EMPTY_SCH =
'(kicad_sch (version 20231120) (generator "ziroeda") (paper "A4")\n (lib_symbols)\n)\n';
/** The Attributes submenu's rows: SCH_EDIT_TOOL::SetAttribute's five actions. */
const ATTRIBUTE_IDS: Record<string, TOOL_ACTION> = {
attrSim: SCH_ACTIONS.setExcludeFromSim,
attrBom: SCH_ACTIONS.setExcludeFromBOM,
attrBoard: SCH_ACTIONS.setExcludeFromBoard,
attrPosFiles: SCH_ACTIONS.setExcludeFromPosFiles,
attrDnp: SCH_ACTIONS.setDNP,
};
const SETTINGS_TOGGLES = new Set([
'toggleHiddenPins',
'toggleHiddenFields',
'crosshairSmall',
'crosshairFull',
'crosshair45',
'lineModeFree',
'lineMode90',
'lineMode45',
'annotateAuto',
]);
/** ERC_TESTER::TestDuplicateSheetNames, the guard the highlight tools run
* before picking (sheet names compare case-insensitively upstream). */
/** The dialog's option names onto ANNOTATE_SCOPE_T / ANNOTATE_ORDER_T / ANNOTATE_ALGO_T. */
const ANNOTATE_SCOPE_OF: Record<AnnotateOptions['scope'], ANNOTATE_SCOPE_T> = {
all: ANNOTATE_SCOPE_T.ANNOTATE_ALL,
current_sheet: ANNOTATE_SCOPE_T.ANNOTATE_CURRENT_SHEET,
selection: ANNOTATE_SCOPE_T.ANNOTATE_SELECTION,
};
const ANNOTATE_ORDER_OF: Record<AnnotateOptions['order'], ANNOTATE_ORDER_T> = {
x: ANNOTATE_ORDER_T.SORT_BY_X_POSITION,
y: ANNOTATE_ORDER_T.SORT_BY_Y_POSITION,
unsorted: ANNOTATE_ORDER_T.UNSORTED,
};
const ANNOTATE_ALGO_OF: Record<AnnotateOptions['algo'], ANNOTATE_ALGO_T> = {
incremental: ANNOTATE_ALGO_T.INCREMENTAL_BY_REF,
sheet_100: ANNOTATE_ALGO_T.SHEET_NUMBER_X_100,
sheet_1000: ANNOTATE_ALGO_T.SHEET_NUMBER_X_1000,
};
// The "Current Tool" status-bar field (EDA_DRAW_FRAME::DisplayToolMsg):
// TOOLS_HOLDER::PushTool shows the active action's FriendlyName; the idle
// selection tool reads "Select item(s)". Names from sch_actions.cpp /
// actions.cpp FriendlyName().
const SCH_TOOL_MSGS: Record<string, string> = {
select: 'Select item(s)',
selectLasso: 'Select item(s)',
highlightNet: 'Highlight Nets',
placeSymbol: 'Place Symbols',
placePower: 'Place Power Symbols',
drawWire: 'Draw Wires',
drawBus: 'Draw Buses',
busEntry: 'Place Wire to Bus Entries',
noConnect: 'Place/Remove No Connect Flags',
junction: 'Place Junctions',
placeLabel: 'Place Net Labels',
placeClassLabel: 'Place Directive Labels',
placeGlobalLabel: 'Place Global Labels',
placeHierLabel: 'Place Hierarchical Labels',
drawSheet: 'Draw Hierarchical Sheets',
sheetPin: 'Place Pins from Sheet',
placeText: 'Draw Text',
textBox: 'Draw Text Boxes',
table: 'Draw Tables',
rectangle: 'Draw Rectangles',
circle: 'Draw Circles',
arc: 'Draw Arcs',
bezier: 'Draw Bezier Curve',
lines: 'Draw Lines',
image: 'Place Images',
delete: 'Interactive Delete Tool',
zoomTool: 'Zoom to Selection Area',
};
/** A file picked from disk for a project open. */
/**
* Below this many footprint libraries the served index is a bundled stub, not a
* library table, ERC's footprint-link test stands down rather than reporting
* every standard KiCad library as missing.
*/
const MIN_FOOTPRINT_LIBS = 10;
/**
* The symbol libraries as KiCad has them when DIALOG_ERC runs: the global table (the hosted
* libraries, each its download folder) and the project's sym-lib-table in the program's
* LIBRARY_MANAGER, every placed symbol of a hosted library fetched into its folder, and each
* library a placed symbol names (re)loaded - what PGM_BASE's table load and the adapter's
* preload give ERC_TESTER::TestLibSymbolIssues upstream.
*/
async function prepareSymbolLibraries(
aFrame: SCH_EDIT_FRAME,
aProjectFiles: readonly { name: string; text: string }[],
aLoadIndex: () => Promise<readonly { name: string }[]>,
): Promise<void> {
let hosted: string[] = [];
try {
hosted = (await aLoadIndex()).map((e) => e.name);
} catch {
// No hosted set: only the project's own libraries are configured.
}
const manager = Pgm().GetLibraryManager();
const tablePath = projectSymLibTablePath(aProjectFiles);
const tableFile = aProjectFiles.find((f) => f.name === tablePath);
const projectTable = tableFile
? LIBRARY_TABLE.FromFile(
`${aFrame.Prj().GetProjectPath()}/${tablePath}`,
tableFile.text,
LIBRARY_TABLE_SCOPE.PROJECT,
LIBRARY_TABLE_TYPE.SYMBOL,
)
: LIBRARY_TABLE.Empty(LIBRARY_TABLE_SCOPE.PROJECT, LIBRARY_TABLE_TYPE.SYMBOL);
manager.SetTable(LIBRARY_TABLE_TYPE.SYMBOL, LIBRARY_TABLE_SCOPE.PROJECT, projectTable);
const used = new Set<string>();
const fetches: Promise<unknown>[] = [];
const hostedSet = new Set(hosted);
const screens = new SCH_SCREENS(aFrame.Schematic().Root());
for (let screen = screens.GetFirst(); screen; screen = screens.GetNext()) {
for (const item of screen.Items().OfType(KICAD_T.SCH_SYMBOL_T)) {
const libId = (item as SCH_SYMBOL).GetLibId();
const nickname = libId.GetLibNickname();
used.add(nickname);
// A project row of the same nickname shadows the global one.
if (hostedSet.has(nickname) && !projectTable.HasRow(nickname))
fetches.push(fetchHostedSymbolFile(libId));
}
}
await Promise.all(fetches);
const adapter = SymbolLibAdapter(aFrame.Prj());
for (const nickname of used) adapter.LoadOne(nickname);
}
export interface PickedFile {
name: string;
text: string;
/** Binary payload for non-text files (plot outputs: PNG/PDF, …). When set,
* `text` is empty and the file is stored/downloaded from these bytes. */
bytes?: Uint8Array;
}
const DEFAULT_FILE = 'untitled.kicad_sch';
// The chooser's "Recently Used" group persists across dialog openings for the
// session (sch_drawing_tools.cpp s_SymbolHistoryList / s_PowerHistoryList).
const sSymbolHistoryList: PickedSymbol[] = [];
/** A token's value from a resolver, or '' when it does not answer. */
function resolveToken(aResolver: TextVarResolverFn | undefined, aName: string): string {
const token = { value: aName };
return aResolver?.(token) ? token.value : '';
}
/** DIALOG_IMPORT_GFX_SCH's importer: a fresh GRAPHICS_IMPORTER_SCH for each import. */
const newSchGraphicsImporter = (): GRAPHICS_IMPORTER_SCH => new GRAPHICS_IMPORTER_SCH();
export function SchematicEditor({
app,
onExitToHome,
onShowPcb,
hasBoard,
onEditSymbolInEditor,
readOnlyNotice,
readOnly,
readBoardNetlist,
autosaveActive,
onShowSymbolEditor,
onShowFootprintEditor,
onShowCalculator,
initialProject,
initialFile,
onProjectChange,
onPersistFiles,
onSaveFiles,
onRevert,
onOutputFile,
registerAutosaveFlush,
openNonce,
shown = true,
extraSheetFiles,
projectName,
rootPro,
kiway,
registerScriptApi,
}: {
/** Lets a host script this window while it is shown (the AI pane): read the
* sheet on screen and run one undoable command on it. Returns the
* unregister function. */
registerScriptApi?: (api: SchScriptApi) => () => void;
/** What the program gives this window (`EESCHEMA_APP`, `eeschema_app.ts`):
* settings, the app's dialogs and canvas, the hosted libraries, the other
* KIWAY players. Built by designer's `useEeschemaApp()`. */
app: EESCHEMA_APP;
onExitToHome: () => void;
onShowPcb?: () => void;
/** Whether the project has a board, which Tools > Update PCB from Schematic
* (F8) needs here: upstream would create one, this editor does not. */
hasBoard?: boolean;
/** SCH_EDIT_TOOL's Edit with Symbol Editor (Ctrl+E): hand the placement's
* symbol to the symbol editor and switch to it. */
onEditSymbolInEditor?: (req: {
/** `LoadSymbolFromSchematic( symbol )`: the live placement. */
fromSchematic?: SCH_SYMBOL;
/** `LoadSymbol( symbol->GetLibId(), symbol->GetUnit(), symbol->GetBodyStyle() )`. */
library?: { libId: string; unit: number; bodyStyle: number };
}) => void;
/** Tools > Update Schematic from PCB with no board editor running: the project's board read
* and formatted as MAIL_PCB_GET_NETLIST's payload (`formatPcbNetlist`), what an off-screen
* PCB_EDIT_FRAME answers upstream; null when there is no board or it cannot be read.
* Asynchronous because the host loads the board reader on use, keeping it out of the entry
* chunk. */
readBoardNetlist?: () => Promise<string | null>;
/** A strip to show above the canvas, e.g. "this demo is not being saved". */
readOnlyNotice?: JSX.Element | null;
/**
* `screen->IsReadOnly()` — the `[Read Only]` half of the frame title
* (sch_edit_frame.cpp:1849-1850). A browser has no per-file writable bit;
* the condition that stands in for one here is the demo project, which is
* exactly what {@link readOnlyNotice} already announces above the canvas.
*/
readOnly?: boolean;
/**
* Whether edits reach storage at all. False for a bare `.kicad_sch` opened
* without a project, or when IndexedDB is unavailable — in which case nothing
* is written until Save, and leaving the page is worth a prompt.
*/
autosaveActive?: boolean;
/** Open the Symbol Editor (the top toolbar's `symbolEditor` button). */
onShowSymbolEditor?: () => void;
/** Open the Footprint Editor (the top toolbar's `footprintEditor` button). */
onShowFootprintEditor?: () => void;
/** Open the Calculator Tools (Tools menu). */
onShowCalculator?: () => void;
initialProject?: PickedFile[] | null;
initialFile?: string | null;
/**
* Hand edited sheets to the app, which writes them locally and decides
* whether the account hears about them.
*
* `push` is false for a change that arrived from another peer: it is still
* saved on this machine, and still must not be committed to the cloud from
* here. Absent means true, so every existing caller keeps its behaviour.
*/
onProjectChange?: (files: PickedFile[], opts?: { push?: boolean }) => void;
/** Persist project files immediately (no debounce), used for the drawing-sheet
* reference in .kicad_pro so it survives a "go back and reopen". */
onPersistFiles?: (files: PickedFile[]) => void;
/**
* The EXPLICIT Save. Writes the files and then records a Local History point
* (`LOCAL_HISTORY::CommitSnapshot`, which upstream runs from the same place a
* save does). Distinct from `onPersistFiles` because that one is also used
* for incidental writes — the drawing-sheet reference, sheet bookkeeping —
* and none of those is a point a user chose to be able to come back to.
*/
onSaveFiles?: (files: PickedFile[]) => Promise<void>;
/**
* ACTIONS::revert's restore half — put the project back to its newest save
* point and reload the editors. Resolves false when there is no point to go
* back to, so the command can say so rather than appear to do nothing.
*/
onRevert?: () => Promise<boolean>;
/** Write a generated output file (plot / export) into the project file
* manager instead of the browser download folder. When absent, outputs fall
* back to a browser download. */
onOutputFile?: (name: string, bytes: Uint8Array, mime: string) => void;
/** Register a flush the host calls before leaving/reopening, so a pending
* autosave is written out first (the "edit → home → reopen" case). */
registerAutosaveFlush?: (fn: (() => void) | null) => void;
/**
* Bumped by the host once per deliberate project open, and by nothing else.
*
* `initialProject` is a LIVE prop — the host mirrors this editor's own
* autosaved sheets back into it — so it is not, and must not be, what decides
* that the project should be re-opened. See the effect that reads this.
*/
openNonce?: number;
/**
* Whether this frame is the one on screen. A hidden frame keeps what it has
* and opens the project when next shown; see the same prop on PcbEditor for
* why. Defaults to shown, for a frame that has no host.
*/
shown?: boolean;
/** `.kicad_wks` saved into the project this session (Drawing Sheet Editor →
* Save to Project), offered as extra Page Settings drawing-sheet choices. */
extraSheetFiles?: PickedFile[];
/** Project name shown as "<project>, Schematic Editor" in the menu bar. */
projectName?: string;
/** Basename of the active project's .kicad_pro (no extension). When a folder
* holds several projects, this pins which one's root sheet to load, so the
* editor matches the launcher tree instead of guessing the first/last pro. */
rootPro?: string;
/**
* The program's KIWAY: the editor's SCH_EDIT_FRAME registers as FRAME_SCH's
* player on it, so the board's cross-probe mail reaches `KiwayMailIn`, and
* sends its own through it.
*/
kiway?: KIWAY;
}): JSX.Element {
const {
PreferencesDialog,
HomeLink,
OpenFileDialog,
SaveAsDialog,
DialogSymLibTable,
FootprintChooserFrame,
useToolbarEntries,
loadFootprintIndex,
loadFootprint,
loadFootprintLibraries,
loadIndex,
loadSymbol,
symbolsBase,
libraryUri,
preloadSchematicLibraries,
remapEvent,
applyHotkeyOverrides,
useProjectSync,
useAuth,
PresencePanel,
AssignFootprints,
DialogSymbolChooser,
DialogRescueEach,
SymbolChooserFrame,
} = app;
const [error, setError] = useState<string | null>(null);
const initial = useMemo<Schematic | null>(() => {
try {
return { ...readSchematic(parse(EMPTY_SCH)), fileName: DEFAULT_FILE };
} catch (e) {
setError(e instanceof Error ? e.message : String(e));
return null;
}
}, []);
// Unsaved-changes flag ('*' in the title until the autosave hand-off; Save
// greys when clean), same affordance as the PCB editor / KiCad's title.
const [dirty, setDirty] = useState(false);
const dirtySkipRef = useRef(true);
/**
* Edits made since the last time anything was written.
*
* Not the same as `dirty`, which is the title's asterisk and clears on a
* timer whether or not a write actually happened — fine as a flash, useless
* as "is there work to lose". This one only clears on a save.
*/
const [unsaved, setUnsaved] = useState(false);
// Live, not plain `useState`: the undo step is folded in a `setDoc` updater,
// and a plain updater is replayed by React whenever it replays a render —
// which popped the stack twice and left the document as it was. See the hook.
const [doc, setDoc, docRef] = useLiveState<Schematic | null>(initial);
// Multi-sheet project: every parsed document by basename, and the root file.
// `doc` is always the currently-shown sheet; it is written back into `docs`
// when switching. The undo stack is NOT here — there is one for the whole
// project, on the frame, exactly as `EDA_BASE_FRAME` holds it.
const project = useRef<{ docs: Map<string, Schematic>; root: string }>({
docs: new Map(initial ? [[DEFAULT_FILE, initial]] : []),
root: DEFAULT_FILE,
});
const [currentFile, setCurrentFile] = useState<string>(DEFAULT_FILE);
// The active sheet *instance* (KiCad SCH_SHEET_PATH). Distinct from currentFile
// so two instances of one shared document highlight/navigate independently.
const [currentPath, setCurrentPath] = useState<string>('/');
// KiCad's "Load Schematic" progress: non-null while parsing/saving a project
// (a plain message, or a snapshot with the per-sheet parse gauge).
const [loading, setLoading] = useState<string | ProgressSnapshot | null>(null);
const [selection, setSelection] = useState<ReadonlySet<string>>(new Set());
// Live cross-tab presence/selection sync (designer/src/sync/). Broadcast-only
// for now — does not apply remote model changes yet.
const syncTransport = useRef<ProjectSyncTransport | null>(null);
const [syncPeers, setSyncPeers] = useState<PresenceInfo[]>([]);
// Real identity to announce (see PcbEditor.tsx's own copy of this comment
// and docs/proposals/multiplayer-architecture.md requirement 5).
const { session } = useAuth();
const myDisplayName = session?.user.email ?? null;
/** The tab's one connection, owned by ProjectSyncProvider rather than by
* this editor — see the comment on the subscription effect below. */
const sharedSync = useProjectSync();
// This tab's own role in the live session — see PcbEditor.tsx's own copy
// of this comment and eeschema/project_sync_transport.ts's
// PeerRole. Read by runCommand/applySheetDocument to refuse a local edit
// from a viewer; mirrored into a ref for the same reason PcbEditor.tsx's
// is. No interactive-gesture-start guard here the way PcbEditor.tsx's
// beginMove has one: a schematic move isn't funnelled through one
// function the way a PCB drag is (SchematicCanvas.tsx sets `modeRef`
// directly at several call sites), so a viewer can still visually start
// dragging a symbol here — it just will not commit on drop, the same
// "reads fine without the extra polish" tradeoff PcbEditor.tsx's own
// nine non-drag guarded commands already accept.
const [myRole, setMyRole] = useState<PeerRole>('editor');
const myRoleRef = useRef<PeerRole>('editor');
myRoleRef.current = myRole;
// Marks this tab as applying a change that arrived from a peer rather
// than one it originated itself — checked (and always reset again
// immediately after, unlike PcbEditor.tsx's copy, since there is only
// one remote-apply call site here and no cross-render gap to bridge) so
// that call does not itself get refused by the viewer guard above.
const applyingRemoteRef = useRef(false);
const [presencePanelOpen, setPresencePanelOpen] = useState(false);
// Other peers' last-known cursor world position, keyed by peerId. Cleared
// per-peer on their next 'presence' drop (see the presence handler below).
/**
* What each peer currently has selected, by item uuid.
*
* Stored unresolved, as uuids, exactly as they arrived. `refId` already
* makes a schematic id BE the item's uuid (`uuid ?? kind:idx:index`), so
* unlike the board -- whose ids are positional and have to be translated
* per peer -- nothing has to be mapped here. What still has to be checked
* is the sheet: a uuid selected on another sheet means nothing on this
* one, which is why every read of this filters through presence.
*/
// A remote sheet-text update waiting to be applied (see the effect near
// applySheetDocument below — it needs sheetInstanceRefs/applySheetDocument,
// both defined later in this component, hence the queue rather than
// applying inline here).
const [pendingRemoteChange, setPendingRemoteChange] = useState<
| { sheetPath: string; text: string; patch?: undefined }
| { sheetPath: string; patch: SchematicPatch; text?: undefined }
| null
>(null);
/**
* The sheet state this tab last broadcast, and which sheet it was — the
* base `diffSchematic` measures the next edit against.
*
* Keyed by path because switching sheets replaces `doc` wholesale: diffing
* sheet B against sheet A would describe every item on both as changed,
* which is not just wasteful but wrong. A switch therefore falls back to
* whole text once, and patches resume from there.
*/
const prevSyncedDoc = useRef<{ path: string; doc: Schematic } | null>(null);
// Last text this tab is responsible for having produced on the active
// sheet — set both when broadcasting a local edit and when applying a
// remote one, so applying a remote change doesn't immediately echo it
// straight back out (see the broadcast effect below).
const lastKnownText = useRef<string | null>(null);
useEffect(() => {
// The connection belongs to the tab, not to this editor: both editors
// stay mounted, so owning one here made the tab its own peer. See
// designer/src/sync/ProjectSyncProvider.tsx.
const transport = sharedSync;
if (!transport) return undefined;
syncTransport.current = transport;
const unsubscribe = transport.onMessage((payload, fromPeerId) => {
if (payload.kind === 'presence') {
setSyncPeers(payload.peers);
} else if (payload.kind === 'selection' || payload.kind === 'cursor') {
// Peers' cursors and selections were drawn by the record canvas, which is gone; they come
// back as an overlay on KiCad's canvas. Until then they are received and not shown.
} else if (payload.kind === 'model-changed') {
setPendingRemoteChange({ sheetPath: payload.sheetPath, text: payload.text });
} else if (payload.kind === 'sheet-patch') {
setPendingRemoteChange({ sheetPath: payload.sheetPath, patch: payload.patch });
} else if (payload.kind === 'self-role') {
// Locally synthesized, not peer-authored — see PcbEditor.tsx's own
// copy of this branch and the payload's own doc comment.
setMyRole(payload.role);
} else if (payload.kind === 'role-assign') {
if (payload.toPeerId !== transport.peerId) return; // addressed to someone else
syncTransport.current?.setRole(payload.role);
}
});
return () => {
// Unsubscribe only. Disconnecting is the provider's job -- this editor
// stays mounted and hidden when the user switches to the board, and
// tearing the tab's connection down here would take presence with it.
unsubscribe();
syncTransport.current = null;
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [sharedSync]);
// This editor owns the sheet half of presence, because it is the only
// thing that knows which sheet is open. `shown` -- the prop the host
// already passes to say which editor is in front -- is in the deps, so
// arriving back from the board re-announces the sheet the provider could
// not name when it announced the view.
useEffect(() => {
if (!shown) return;
sharedSync?.updatePresence('schematic', currentPath);
}, [currentPath, shown, sharedSync]);
useEffect(() => {
// Only ids that are really uuids. `refId` falls back to `kind:idx:index`
// for an item with no uuid of its own, and that names a position in THIS
// tab's arrays -- sent as-is it would land on whatever item happened to
// sit at that index on the receiver, which is worse than sending nothing.
const refs = [...selection].filter((id) => !id.includes(':idx:'));
syncTransport.current?.publish({ kind: 'selection', refs });
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [selection]);
// Broadcast the active sheet's edits (designer/src/sync/), debounced so a
// run of small edits collapses into one message instead of one per commit.
// Skipped when the change we're seeing is one we just applied FROM a
// remote peer (lastKnownText already matches it) — otherwise applying a
// remote change would immediately echo it straight back out.
useEffect(() => {
if (!doc) return undefined;
const timer = setTimeout(() => {
let text: string;
try {
text = serializeSchematic(doc);
} catch {
return;
}
// Recorded even when this turns out to be our own echo, so the next
// real edit diffs against what the group actually has rather than
// against whatever this tab last sent.
const base = prevSyncedDoc.current?.path === currentPath ? prevSyncedDoc.current.doc : null;
prevSyncedDoc.current = { path: currentPath, doc };
if (text === lastKnownText.current) return;
lastKnownText.current = text;
// The fast path: a handful of changed items instead of the whole sheet,
// `lib_symbols` cache and title block. Null means this edit touched the
// retained AST (page settings, embedded files) and cannot be described
// as items — see sch_diff.ts.
const patch = base ? diffSchematic(base, doc) : null;
if (patch && !schematicPatchIsEmpty(patch)) {
syncTransport.current?.publish({ kind: 'sheet-patch', sheetPath: currentPath, patch });
return;
}
syncTransport.current?.publish({ kind: 'model-changed', sheetPath: currentPath, text });
}, 400);
return () => clearTimeout(timer);
}, [doc, currentPath]);
/**
* The selection a right-click made just to have something to aim the menu at
* — `SELECTION::SetIsHover`.
*
* `SCH_SELECTION_TOOL::Main`'s right-click branch only picks an item when
* nothing is selected yet, and marks what it picked as a hover:
*
* if( m_selection.Empty() )
* {
* ClearSelection();
* SelectPoint( evt->Position(), { SCH_LOCATE_ANY_T }, nullptr, &selCancelled );
* m_selection.SetIsHover( true );
* }
*
* A hover selection is disposable: every action that acts on one clears it
* when it finishes (`if( selection.IsHover() ) … selectionClear`), and the
* point editor's handles never come up on it — which is why right-clicking a
* sheet cold shows the menu with no resize grips, while right-clicking one
* that was already selected leaves the grips it already had.
*
* Held as the set itself rather than a flag, so it stops applying the moment
* the selection becomes anything else.
*/
const selectionRef = useRef<ReadonlySet<string>>(selection);
selectionRef.current = selection;
const history = useRef(new ProjectHistory());
const [activeTool, setActiveTool] = useState('select');
/** The current tool, for callbacks that must not re-run when it changes. */
const activeToolRef = useRef(activeTool);
activeToolRef.current = activeTool;
const [placeLib, setPlaceLibOnly] = useState<LibSymbol | null>(null);
// A ready-built symbol on the cursor instead of one made from the library's
// defaults: Place Next Symbol Unit attaches a copy of an existing placement.
const [placeInstance, setPlaceInstance] = useState<SchSymbol | null>(null);
// Every other placement path starts (or abandons) a library placement through
// this, which drops the copy, so it can never outlive its own run.
const setPlaceLib = useCallback((lib: LibSymbol | null) => {
setPlaceLibOnly(lib);
setPlaceInstance(null);
// `addSymbol`'s first line, when the symbol goes ON the cursor:
//
// m_toolMgr->RunAction( ACTIONS::selectionClear );
// m_selectionTool->AddItemToSel( aSymbol );
//
// This is NOT what unhighlights the previous symbol when you open the
// chooser — the click branch already cleared it before the dialog went up
// (see `chooserOpen` below), so by the time a pick gets here there is
// nothing left to clear. It is the operative one on the paths that attach
// a symbol WITHOUT a chooser: Place Next Symbol Unit, and the repeated
// copies of "Place all units" / KeepSymbol.
//
// Only on attach: `setPlaceLib( null )` is the abandon path, and Escape's
// own cleanup() clears the selection there.
if (lib) setSelection(new Set());
}, []);
// Unit attached to the cursor, and the chooser's checkbox state driving the
// after-placement continuation (KeepSymbol / PlaceAllUnits stepping).
// Read by the after-placement continuation, which must see the library that
// is on the cursor now rather than the one its closure was built with.
const placeLibRef = useRef<LibSymbol | null>(null);
placeLibRef.current = placeLib;
const placeFlags = useRef({ keepSymbol: true, placeAllUnits: false, unitCount: 1 });
const [pendingLabel, setPendingLabel] = useState<PendingLabel | null>(null);
// SCH_DRAWING_TOOLS' m_last* members: the next label starts from whatever the
// previous one was given. Upstream's initial values: Input, RIGHT, no bold /
// italic / auto-rotate.
const lastLabel = useRef({
shape: 'input' as LabelShape,
bold: false,
italic: false,
spin: 'right' as LabelSpin,
autoRotate: false,
face: '',
});
/** DIALOG_TEXT_PROPERTIES while it is up for the live tools. */
const [labelDialog, setLabelDialog] = useState<{
dlg: DIALOG_LABEL_PROPERTIES;
shown: LABEL_DIALOG_VALUES;
resolve: (aId: number) => void;
} | null>(null);
const [fieldDialog, setFieldDialog] = useState<{
dlg: DIALOG_FIELD_PROPERTIES;
shown: FIELD_DIALOG_VALUES;
field: SCH_FIELD;
commit: SCH_COMMIT;
resolve: (aId: number) => void;
} | null>(null);
const [sheetDialog, setSheetDialog] = useState<{
dlg: DIALOG_SHEET_PROPERTIES;
shown: SHEET_DIALOG_VALUES;
resolve: (aId: number) => void;
} | null>(null);
// KIDIALOG for the live tools' warnings with a "Do not show again" box.
const kiDialog = useKiDialog();
const [symbolDialog, setSymbolDialog] = useState<{
dlg: DIALOG_SYMBOL_PROPERTIES;
shown: SYMBOL_DIALOG_VALUES;
resolve: (aId: number) => void;
} | null>(null);
const [changeSymbolsDialog, setChangeSymbolsDialog] = useState<{
dlg: DIALOG_CHANGE_SYMBOLS;
resolve: (aId: number) => void;
} | null>(null);
// FRAME_SYMBOL_CHOOSER, opened quasi-modal by a dialog's browse button.
const [symbolChooser, setSymbolChooser] = useState<{
preselect: string;
resolve: (aLibId: string | null) => void;
} | null>(null);
const [tableDialog, setTableDialog] = useState<{
dlg: DIALOG_TABLE_PROPERTIES;
shown: SCH_TABLE_DIALOG_VALUES;
isNew: boolean;
resolve: (aId: number) => void;
} | null>(null);
const [cellDialog, setCellDialog] = useState<{
dlg: DIALOG_TABLECELL_PROPERTIES;
shown: TABLECELL_DIALOG_VALUES;
resolve: (aId: number) => void;
} | null>(null);
const [textDialog, setTextDialog] = useState<{
dlg: DIALOG_TEXT_PROPERTIES;
shown: TEXT_DIALOG_VALUES;
resolve: (aId: number) => void;
} | null>(null);
/**
* The rectangle a table was dragged out over, awaiting confirmation.
*
* `DrawTable` derives the row and column counts from the drag —
*
* int colCount = std::max( 1, requestedSize.x / ( fontSize * 15 ) );
* int rowCount = std::max( 1, requestedSize.y / ( fontSize * 2 ) );
*
* — and then shows DIALOG_TABLE_PROPERTIES over the result; only OK commits.
* Asking for the counts up front, which is what this used to do, is a
* different gesture and gives no preview of what you are about to get.
*/
/**
* The open DIALOG_TABLE_PROPERTIES. A table drawn just now is held here
* rather than in the document, because Cancel throws it away —
* `else { delete table; }` — so it must not be committed first.
*/
// The image riding the cursor, built once when the file is chosen and
// re-placed each frame (SCH_DRAWING_TOOLS::PlaceImage keeps one SCH_BITMAP
// and moves it), so its identity — and the renderer's decode of it — survives.
const [pendingImage, setPendingImage] = useState<SchImage | null>(null);
// Keyboard-initiated grabbed move (SCH_MOVE_TOOL): M leaves connected wires
// behind, G drags them along. A fresh nonce restarts the grab.
// Assign Netclass: the patterns the selection produced, awaiting a class.
// DIALOG_ASSIGN_NETCLASS for SCH_EDITOR_CONTROL::AssignNetclass: the net-name patterns, the
// candidates and the previewer the tool hands over.
const [netclassDialog, setNetclassDialog] = useState<{
netNames: readonly string[];
candidates: readonly string[];
previewer: (aNetNames: readonly string[]) => void;
resolve: (aRetval: number) => void;
} | null>(null);
// SCH_MOVE_TOOL::Main's four modes. Break and Slice split the selected
// segment first and then run exactly this drag, which is why they are a grab
// kind rather than an edit of their own.
// Right-click selection context menu (SCH_SELECTION_TOOL's TOOL_MENU):
// client-space position plus the hit-tested item, or null when closed.
// Editing an existing label's text/shape (DIALOG_LABEL_PROPERTIES).
// Editing a hierarchical sheet's name/file (DIALOG_SHEET_PROPERTIES).
// Sheet Properties (DIALOG_SHEET_PROPERTIES); the dialog reads the sheet
// itself out of the document, so only which one is open is state.
// Shape Properties (DIALOG_SHAPE_PROPERTIES). A graphic polyline lives in
// `lines`, every other shape in `graphics`, so the target says which.
/** DIALOG_SHAPE_PROPERTIES while it is up for the live tools. */
const [shapeDialog, setShapeDialog] = useState<{
dlg: DIALOG_SHAPE_PROPERTIES;
title?: string;
shown: SHAPE_DIALOG_VALUES;
resolve: (aId: number) => void;
} | null>(null);
// Image Properties (DIALOG_IMAGE_PROPERTIES over PANEL_IMAGE_EDITOR).
/** DIALOG_IMAGE_PROPERTIES while it is up for the live tools. */
const [imageDialog, setImageDialog] = useState<{
dlg: DIALOG_IMAGE_PROPERTIES;
shown: ReturnType<DIALOG_IMAGE_PROPERTIES['TransferDataToWindow']>;
resolve: (aId: number) => void;
} | null>(null);
// Field Properties (DIALOG_FIELD_PROPERTIES): which symbol, which field.
// Sheet Pin Properties (DIALOG_SHEET_PIN_PROPERTIES).
/** DIALOG_SHEET_PIN_PROPERTIES while it is up for the live tools. */
const [sheetPinDialog, setSheetPinDialog] = useState<{
dlg: DIALOG_SHEET_PIN_PROPERTIES;
shown: SHEET_PIN_DIALOG_VALUES;
resolve: (aId: number) => void;
} | null>(null);
// Unfold from Bus leaves the wire tool drawing away from the new entry
// (SCH_LINE_WIRE_BUS_TOOL continues into its drawing loop).
// Editing a wire/bus stroke (DIALOG_WIRE_BUS_PROPERTIES) or a junction's
// diameter (DIALOG_JUNCTION_PROPS).
// A bus entry opens the same DIALOG_WIRE_BUS_PROPERTIES a wire does: upstream
// groups SCH_BUS_WIRE_ENTRY_T with SCH_LINE_T and SCH_JUNCTION_T in
// SCH_EDIT_TOOL::Properties.
/** DIALOG_WIRE_BUS_PROPERTIES while it is up for the live tools. */
const [wireBusDialog, setWireBusDialog] = useState<{
dlg: DIALOG_WIRE_BUS_PROPERTIES;
shown: WIRE_BUS_DIALOG_VALUES;
resolve: (aId: number) => void;
} | null>(null);
/** DIALOG_JUNCTION_PROPS while it is up for the live tools. */
const [junctionDialog, setJunctionDialog] = useState<{
dlg: DIALOG_JUNCTION_PROPS;
shown: JUNCTION_DIALOG_VALUES;
resolve: (aId: number) => void;
} | null>(null);
/** DIALOG_LINE_PROPERTIES while it is up for the live tools (graphic lines). */
const [lineDialog, setLineDialog] = useState<{
dlg: DIALOG_LINE_PROPERTIES;
shown: LINE_DIALOG_VALUES;
resolve: (aId: number) => void;
} | null>(null);
const imageInputRef = useRef<HTMLInputElement>(null);
// The AUI panes shown (m_auimgr, which upstream's window owns too): aui.show_schematic_hierarchy
// and aui.show_properties default true, aui.show_search and aui.show_net_nav_panel false
// (eeschema_settings.cpp:246-247, 318-319, 297-301). [data]
const [localToggles, setLocalToggles] = useState<Set<string>>(
new Set(['showHierarchy', 'showProperties']),
);
// The frame's user units (UNITS_PROVIDER::GetUserUnits), read back after every units action.
const [frameUnits, setFrameUnits] = useState<StatusUnits>(
defaultUnitsToggle('eeschema') === 'unitsMm' ? 'mm' : 'mils',
);
const syncFrameUnits = useCallback(() => {
const u = schFrameRef.current?.GetUserUnits();
if (u === 'in' || u === 'mils' || u === 'mm') setFrameUnits(u);
}, []);
// Collapsed nodes in the Schematic Hierarchy tree (HIERARCHY_TREE twisties),
// keyed by SheetTreeNode.path; a node not in the set is expanded.
const [collapsedSheets, setCollapsedSheets] = useState<Set<string>>(new Set());
// Left dock sizing (KiCad's default AUI perspective for the Properties /
// Net Navigator / Schematic Hierarchy / Selection Filter column: bestw=300,
// and PropertiesManager's minw=240 is the binding constraint on the whole
// column). Height is per stacked pane; Selection Filter (prop=0 in KiCad's
// perspective) never grows, so it's excluded from panelHeights.
const [leftDockWidth, setLeftDockWidth] = useState(300);
const [panelHeights, setPanelHeights] = useState<Record<string, number>>({});
// `dock_pos` for the left column, which is state and not a table: wxAUI
// renumbers the SHOWN panes on every Update, so the pane opened first ends
// up at 0 and the next one keeps its (larger) `Position()` and docks below
// it. See `schLeftDockLayout` and `qa/probes/aui_dock_pos_probe.cpp`. It
// starts at what `AddPane` leaves behind, so a frame that opens with several
// panes already shown gets them in `Position()` order.
//
// A ref rather than state: it is written during the render that lays the
// column out, and every write is accompanied by the toggle change that
// caused it, so there is nothing extra to re-render for.
//
// It starts from the stored perspective, not from the `Position()` table:
// `RestoreAuiLayout()` runs before any pane is shown, so upstream's column
// resumes wherever the last session left it. See `schDockPosFrom`.
const dockPosRef = useRef<SchDockPos>(schDockPosFrom(app.settings.eeschema.window.left_dock_pos));
// The numbers the last laid-out render produced, persisted after it.
const dockPosSaveRef = useRef<SchDockPos>(dockPosRef.current);
// `SCH_EDIT_FRAME::SaveSettings` writes `m_auimgr.SavePerspective()`, which
// carries every pane's `dock_pos`, so the renumbering wxAUI did during the
// session outlives it. Ours is written after the render that produced it
// rather than during, because a settings commit notifies subscribers.
//
// No dependency array: the value is a ref, so there is nothing React could
// key on, and the comparison below makes the pass a no-op whenever the column
// did not move.
useEffect(() => {
const next = dockPosSaveRef.current;
const stored = app.settings.eeschema.window.left_dock_pos;
if (SCH_LEFT_PANE_ADD_ORDER.every((pane) => stored[pane] === next[pane])) return;
app.settings.updateEeschema((s) => {
s.window.left_dock_pos = { ...next };
});
});
// Drags the pane immediately above the sash (KiCad's HIERARCHY_TREE /
// PROPERTIES_PANEL / NET_NAVIGATOR sashes); the pane below it keeps filling
// the rest via flex:1, same chain KiCad's wxAUI splitters produce.
const startPanelResize = (key: string, e: React.MouseEvent): void => {
e.preventDefault();
const paneEl = (e.currentTarget as HTMLElement).previousElementSibling as HTMLElement | null;
const startY = e.clientY;
const startH = panelHeights[key] ?? paneEl?.getBoundingClientRect().height ?? 200;
const onMove = (ev: MouseEvent): void =>
setPanelHeights((p) => ({ ...p, [key]: Math.max(60, startH + ev.clientY - startY) }));
const onUp = (): void => {
document.removeEventListener('mousemove', onMove);
document.removeEventListener('mouseup', onUp);
document.body.style.cursor = '';
};
document.addEventListener('mousemove', onMove);
document.addEventListener('mouseup', onUp);
document.body.style.cursor = 'row-resize';
};
/**
* The sash above the BOTTOM dock. `startPanelResize` grows the pane before
* the sash; this one is the mirror image — the pane is *after* the sash, so
* dragging down shrinks it. The floor is the pane's own
* `.MinSize( 180, 60 )`, not a number chosen here.
*/
const startBottomDockResize = (e: React.MouseEvent): void => {
e.preventDefault();
const paneEl = (e.currentTarget as HTMLElement).nextElementSibling as HTMLElement | null;
const startY = e.clientY;
const startH =
panelHeights.search ?? paneEl?.getBoundingClientRect().height ?? SCH_BOTTOM_DOCK.bestHeight;
const onMove = (ev: MouseEvent): void =>
setPanelHeights((p) => ({
...p,
search: Math.max(SCH_BOTTOM_DOCK.minHeight, startH - (ev.clientY - startY)),
}));
const onUp = (): void => {
document.removeEventListener('mousemove', onMove);
document.removeEventListener('mouseup', onUp);
document.body.style.cursor = '';
};
document.addEventListener('mousemove', onMove);
document.addEventListener('mouseup', onUp);
document.body.style.cursor = 'row-resize';
};
/** The dock opens at the pane's `.BestSize( 180, 100 )` height. */
const bottomDockStyle: React.CSSProperties = {
height: panelHeights.search ?? SCH_BOTTOM_DOCK.bestHeight,
};
const [prefsOpen, setPrefsOpen] = useState(false);
const [aboutOpen, setAboutOpen] = useState(false);
/**
* `ShowPreferences( aStartPage, aStartParentPage )`'s first argument, for the
* callers that name a page — `COMMON_TOOLS::GridProperties` is the only one
* so far (`common/tool/common_tools.cpp:609-634`). Undefined means the book
* opens where it always did.
*/
const [prefsPage, setPrefsPage] = useState<PrefsPageId | undefined>(undefined);
const openPrefs = useCallback((page?: PrefsPageId) => {
setPrefsPage(page);
setPrefsOpen(true);
}, []);
const common = app.useCommonSettings();
const es = app.useEeschemaSettings();
/**
* `EDA_BASE_FRAME::RecreateToolbars` (`common/eda_base_frame.cpp:1728-1843`):
* the frame asks `GetToolbarConfig( loc, m_CustomToolbars )` for each bar and
* never reads `DefaultToolbarConfig` itself, which is what lets Preferences >
* Toolbars change what is drawn.
*/
const schTopBar = useToolbarEntries('eeschema', 'TOP_MAIN', SCH_DEFAULT_TOOLBARS);
const schLeftBar = useToolbarEntries('eeschema', 'LEFT', SCH_DEFAULT_TOOLBARS);
const schRightBar = useToolbarEntries('eeschema', 'RIGHT', SCH_DEFAULT_TOOLBARS);
const theme = app.useSchematicTheme();
// The displayed toggle set: local toggles plus the settings-derived ones
// (Preferences and the left toolbar drive the same EESCHEMA_SETTINGS keys).
const toggles = useMemo(() => {
const t = new Set(localToggles);
// cond.Units( EDA_UNITS::... ): the units group checks the frame's units.
t.add(frameUnits === 'in' ? 'unitsInches' : frameUnits === 'mils' ? 'unitsMils' : 'unitsMm');
if (es.window.grid.show) t.add('toggleGrid');
if (es.window.grid.overrides_enabled) t.add('toggleGridOverrides');
if (es.appearance.show_hidden_pins) t.add('toggleHiddenPins');
if (es.appearance.show_hidden_fields) t.add('toggleHiddenFields');
t.add(
es.window.cursor.crosshair === '45'
? 'crosshair45'
: es.window.cursor.crosshair === 'small'
? 'crosshairSmall'
: 'crosshairFull',
);
t.add(
es.drawing.line_mode === 0
? 'lineModeFree'
: es.drawing.line_mode === 2
? 'lineMode45'
: 'lineMode90',
);
if (es.annotation.automatic) t.add('annotateAuto');
return t;
}, [localToggles, es, frameUnits]);
const togglesRef = useRef(toggles);
togglesRef.current = toggles;
// ToggleNetNavigator's tail: once the pane is shown, the tree rebuilt around its selection - on
// the live schematic, which is built first if nothing has asked for it yet.
const netNavigatorShown = toggles.has('showNetNavigator');
// biome-ignore lint/correctness/useExhaustiveDependencies: runs when the pane opens; the frame and the sync are refs
useEffect(() => {
const frame = schFrameRef.current;
if (!netNavigatorShown || !frame) return;
syncLiveRef.current();
const navigator = frame.GetNetNavigator();
const selection = navigator.GetSelection();
const data = selection.IsOk() ? navigator.GetItemData(selection) : null;
frame.RefreshNetNavigator(data instanceof NET_NAVIGATOR_ITEM_DATA ? data : null);
}, [netNavigatorShown]);
/** HIERARCHY_PANE::UpdateNetHighlight's marks: the sheet paths carrying the net, and the colour. */
const [hierarchyNetMark, setHierarchyNetMark] = useState<{
paths: ReadonlySet<string>;
color: string;
}>({ paths: new Set(), color: '' });
// Selection Filter (SCH_SELECTION_FILTER_OPTIONS): gates which item types,
// and locked items, the selection accepts.
const [selFilter, setSelFilter] = useState<SelectionFilterOptions>(defaultSelectionFilter);
// ACTIONS::toggleUnits / the imperial-unit pair, and the display's device
// pixel ratio: both feed the live status panes, so they are resolved before
// the readout that writes them.
const units: StatusUnits = frameUnits;
const dpr = typeof window !== 'undefined' ? window.devicePixelRatio || 1 : 1;
// HOTKEY_CYCLE_POPUP, this frame's one instance (EDA_DRAW_FRAME::m_hotkeyPopup).
// Its expiry hands the keyboard back with `m_drawFrame->GetCanvas()->SetFocus()`
// (common/dialogs/hotkey_cycle_popup.cpp:48).
const appRef = useRef<HTMLDivElement>(null);
const hotkeyPopup = useHotkeyCyclePopup(() => appRef.current?.querySelector('canvas')?.focus());
/**
* `SCH_EDITOR_CONTROL::GridFeedback`
* (eeschema/tools/sch_editor_control.cpp:3360-3382), bound to
* `EVENTS::GridChangedByKeyEvent` (`:3550`) - which `COMMON_TOOLS::
* OnGridChanged` posts only for the HOTKEY paths, never for the grid combo
* or the menu (common/tool/common_tools.cpp:562-564).
*
* Held in a ref because the keydown listener is installed once, while the
* labels depend on the frame's live units.
*/
const gridFeedbackRef = useRef<() => void>(() => {});
gridFeedbackRef.current = () => {
// The settings manager is read here rather than the render-time `es`,
// because this runs immediately after `updateEeschema` has moved the index
// and must see the grid the keystroke just chose - as upstream does, where
// `OnGridChanged` assigns `last_size_idx` before posting the event.
const grid = app.settings.eeschema.window.grid;
gridFeedback(hotkeyPopup, {
hotkeyFeedback: app.settings.common.input.hotkey_feedback,
grids: grid.sizes,
lastSizeIdx: grid.last_size_idx,
units,
iuPerMM: SCH_IU_PER_MM,
});
};
// The symbol whose properties dialog is open (its refId), or null.
// Items parsed from the clipboard, attached to the cursor until dropped.
const [pastePending, setPastePendingOnly] = useState<PastePayload | null>(null);
/**
* Attaching a paste clears the selection, so the items it came from go dark.
*
* Ctrl+D is not its own operation upstream — it IS a paste:
*
* int SCH_EDITOR_CONTROL::Duplicate( const TOOL_EVENT& aEvent )
* {
* doCopy( true ); // Use the local clipboard
* Paste( aEvent );
* }
*
* (sch_editor_control.cpp:1797-1803), and the paste path clears the selection
* before it takes the pasted items into it. So the moment you duplicate, the
* original stops being selected and the new copy is what is highlighted.
* Ours left the original lit and only moved the selection across on the drop.
*
* Every paste path goes through this one setter — Ctrl+V, Duplicate, the
* repeat-item and drag-drop paths — so the rule is stated once here rather
* than at seven call sites. `null` is the abandon/finish path and leaves the
* selection alone: `onPasteDone` sets it to the items just dropped.
*/
const setPastePending = useCallback((payload: PastePayload | null) => {
setPastePendingOnly(payload);
if (payload) setSelection(new Set());
}, []);
/** File > Import > Graphics (Ctrl+Shift+F): the open DIALOG_IMPORT_GFX_SCH. */
const [importGfx, setImportGfx] = useState<{
result: IMPORT_GFX_RESULT;
resolve: (aRetval: number) => void;
} | null>(null);
/** `m_ercDialog`: the frame's DIALOG_ERC, and whether its window is shown. */
const [ercDialog, setErcDialog] = useState<{ dialog: DIALOG_ERC; shown: boolean } | null>(null);
const ercDialogRef = useRef(ercDialog);
ercDialogRef.current = ercDialog;
/** DIALOG_ERC's "Delete exclusions too?" and its Save Report File dialog, while open. */
const [ercYesNoCancel, setErcYesNoCancel] = useState<{
resolve: (aResult: 'yes' | 'no' | 'cancel') => void;
} | null>(null);
/** WX_TEXT_ENTRY_DIALOG( this, wxEmptyString, _( "Exclusion Comment" ), aInitial, true ). */
const [ercComment, setErcComment] = useState<{
initial: string;
resolve: (aText: string | null) => void;
} | null>(null);
const [ercSaveReport, setErcSaveReport] = useState<{
defaultName: string;
resolve: (aPath: string | null) => void;
} | null>(null);
/** `m_symbolFieldsTableDialog`: the frame's DIALOG_SYMBOL_FIELDS_TABLE, and whether it shows. */
const [fieldsTable, setFieldsTable] = useState<{
dialog: DIALOG_SYMBOL_FIELDS_TABLE;
shown: boolean;
} | null>(null);
const fieldsTableRef = useRef(fieldsTable);
fieldsTableRef.current = fieldsTable;
/** DIALOG_SYMBOL_FIELDS_TABLE's sub-dialogs, while one is open. */
const [fieldsTextEntry, setFieldsTextEntry] = useState<{
message: string;
caption: string;
value: string;
resolve: (aText: string | null) => void;
} | null>(null);
const [fieldsList, setFieldsList] = useState<{
caption: string;
label: string;
items: readonly string[];
resolve: (aPicked: string | null) => void;
} | null>(null);
const [fieldsUnsaved, setFieldsUnsaved] = useState<{
message: string;
resolve: (aResult: UnsavedChangesResult) => void;
} | null>(null);
/** `new DIALOG_SYMBOL_FIELDS_TABLE( this )`, as GetSymbolFieldsTableDialog asks for one. */
const fieldsTableFactoryRef = useRef<() => DIALOG_SYMBOL_FIELDS_TABLE | null>(() => null);
/** `new DIALOG_ERC( this )`, as SCH_EDIT_FRAME::GetErcDialog asks for one. */
const ercDialogFactoryRef = useRef<() => DIALOG_ERC | null>(() => null);
/** Tools > Update Schematic from PCB: the footprints read for this run. */
const [updateFromPcbDialog, setUpdateFromPcbDialog] = useState<{
resolve: (aRet: number) => void;
} | null>(null);
/** DIALOG_SYNC_SHEET_PINS while it is open (SCH_DRAWING_TOOLS::m_dialogSyncSheetPin). */
const [syncDialog, setSyncDialog] = useState<DIALOG_SYNC_SHEET_PINS | null>(null);
const [fileName, setFileName] = useState<string | null>(null);
/**
* `SCH_ACTIONS::importSheet`'s file picker.
*
* Deliberately not the Open dialog: Open *replaces* the document, while
* importing brings another schematic's contents *into* this one, so the two
* must not share a picker.
*/
const libById = useMemo<Map<string, LibSymbol>>(
() => new Map((doc?.libSymbols ?? []).map((l) => [l.libId, l])),
[doc?.libSymbols],
);
// The same map for the stable callbacks, which are built once and would
// otherwise capture the first render's.
// Read by the live frame's dialog router, which outlives a render.
const readBoardNetlistRef = useRef(readBoardNetlist);
readBoardNetlistRef.current = readBoardNetlist;
const libByIdRef = useRef(libById);
libByIdRef.current = libById;
// Connectivity: compute the netlist, then brighten the net the Highlight-Net tool
// picked (not the selection, KiCad keeps those separate). The renderer matches
// wire/junction/pin ids against this set.
// Project-scoped Schematic Setup values (SCHEMATIC_SETTINGS working state);
// hydrated from .kicad_pro on project load, committed via commitSetup.
const [setup, setSetup] = useState<SchematicSetup>(defaultSchematicSetup);
// Bus Alias Definitions feed group-bus expansion in the netlist.
const busAliases = useMemo(
() => new Map(setup.busAliases.filter((a) => a.name).map((a) => [a.name, a.members])),
[setup.busAliases],
);
// Connectivity runs one task behind the document. Rebuilding the graph is the
// most expensive thing an edit triggers, and nothing about the *geometry* the
// user just changed depends on it, so the edit is painted first and the nets
// are rebuilt immediately afterwards, with the previous result left on screen
// for that one frame rather than blanking. Everything derived from the graph
// (net colours, netclass widths) keys off `connDoc` so it stays
// self-consistent; a wire drawn this frame simply has no override yet.
//
// This does not widen any window a caller could observe: a handler that edits
// the document already cannot see a rebuilt netlist, because React has not
// re-rendered at that point either.
const [connDoc, setConnDoc] = useState<Schematic | null>(doc);
useEffect(() => {
if (connDoc === doc) return;
// setTimeout, not requestAnimationFrame, rAF never fires while the tab is
// hidden, and connectivity must keep up with edits made off-screen.
const t = setTimeout(() => setConnDoc(doc), 0);
return () => clearTimeout(t);
}, [doc, connDoc]);
const netlist = useMemo(
() => (connDoc ? computeNetlist(connDoc, libById, { busAliases }) : null),
[connDoc, libById, busAliases],
);
const schFrameRef = useRef<SCH_EDIT_FRAME | null>(null);
/** `DIALOG_DESIGN_BLOCK_PROPERTIES`, open: `ShowModal() == wxID_OK`. */
const [dbProperties, setDbProperties] = useState<{
designBlock: DESIGN_BLOCK;
disableName: boolean;
resolve: (aOk: boolean) => void;
} | null>(null);
/**
* `m_designBlocksPane` (sch_edit_frame.cpp:257): `new SCH_DESIGN_BLOCK_PANE( this, nullptr,
* m_designBlockHistoryList )`, its modals answered by this window, its placement options
* `eeschema.json`'s `design_block_chooser.*`.
*/
const [designBlocksPane] = useState(
() =>
new SCH_DESIGN_BLOCK_PANE(
{
DesignBlockLibs: () => schFrameRef.current!.Prj().DesignBlockLibs(),
GetLibraryManager: () => Pgm().GetLibraryManager(),
NormalizePath: (aPath) => aPath,
ShowInfoBarError: (aMsg) => schFrameRef.current?.ShowInfoBarError(aMsg),
SetStatusText: (aMsg) => schFrameRef.current?.SetStatusText(aMsg),
},
{
IsOK: (aMessage) => DbIsOK(aMessage),
OKOrCancel: async (aMessage, aDetail, aOKLabel) =>
(await DbOKOrCancelDialog('Confirmation', aMessage, aDetail, aOKLabel)) === wxID_OK,
ConfirmOverwriteLibrary: async (aMessage) =>
(await ShowKicadMessageDialog({
message: aMessage,
caption: 'Warning',
style: wxOK | wxCANCEL | wxICON_WARNING,
okLabel: 'Overwrite',
})) === wxID_OK,
DisplayError: (aMessage, aDetail) => DbDisplayErrorMessage(aMessage, aDetail ?? ''),
GetTextFromUser: (aMessage, aTitle) =>
new Promise<string | null>((resolve) =>
setTextEntryRequest({ message: aMessage, caption: aTitle, value: '', resolve }),
),
// `LibraryFileBrowser( …, false, …, FILEDLG_HOOK_NEW_LIBRARY )`. Web delta, as the symbol
// and footprint editors': a library is named, not placed - the account's Design Blocks
// folder for the global table, the project for the project's.
NewLibraryBrowser: async (aTitle) => {
const frame = schFrameRef.current!;
const projectOpen = frame.Prj().GetProjectPath() !== '';
const hook = MakeFileDlgHookNewLibrary(!projectOpen);
const name = await frame.AskLibraryName(aTitle, '', projectOpen ? hook : undefined);
if (!name) return null;
const global = !projectOpen || GetUseGlobalTable(hook);
const place = global
? PATHS.GetDefaultUserDesignBlocksPath()
: frame.Prj().GetProjectPath().replace(/\/+$/, '');
return { path: `${place}/${name}.${KiCadDesignBlockLibPathExtension}`, global };
},
DesignBlockProperties: (aDesignBlock, aDisableName) =>
new Promise<boolean>((resolve) =>
setDbProperties((prev) => {
prev?.resolve(false);
return { designBlock: aDesignBlock, disableName: aDisableName, resolve };
}),
),
},
[],
() => {
const panel = eeschemaKifaceSettings().m_DesignBlockChooserPanel;
return {
repeated_placement: panel.repeated_placement,
place_as_group: panel.place_as_group,
place_as_sheet: panel.place_as_sheet,
keep_annotations: panel.keep_annotations,
};
},
(aOptions) => {
Object.assign(eeschemaKifaceSettings().m_DesignBlockChooserPanel, aOptions);
},
),
);
/** `m_DesignBlockChooserPanel.sort_mode` / `sash_pos_v`, `m_LibTree.open_libs`, the dock's width. */
const [dbSortMode, setDbSortMode] = useState(0);
const [dbSashPosV, setDbSashPosV] = useState(-1);
const [dbOpenLibs, setDbOpenLibs] = useState<readonly string[]>([]);
// defaultDesignBlocksPaneInfo: BestSize( 300, 200 ), MinSize( 240, 60 ). [data]
const [dbPaneWidth, setDbPaneWidth] = useState(300);
useEffect(() => {
const frame = schFrameRef.current;
if (!frame) return;
frame.m_designBlocksPane = designBlocksPane;
return () => {
frame.m_designBlocksPane = null;
};
}, [designBlocksPane]);
/**
* `KIWAY::CommonSettingsChanged` after a Preferences OK: SCH_BASE_FRAME::CommonSettingsChanged
* looks the colour theme up again and hands it to the canvas's painter. The settings
* subscription behind `theme` is our version of that call.
*/
// biome-ignore lint/correctness/useExhaustiveDependencies: `theme` is the trigger - the frame reads the settings itself
useEffect(() => {
schFrameRef.current?.CommonSettingsChanged();
}, [theme]);
/** `wxTextEntryDialog` for the live tools while it is up. */
/** A library's name being asked for (SCH_BASE_FRAME::AskLibraryName), with its hook. */
const [libraryNameRequest, setLibraryNameRequest] = useState<{
title: string;
name: string;
hook?: wxFileDialogCustomizeHook;
resolve: (aName: string | null) => void;
} | null>(null);
const [textEntryRequest, setTextEntryRequest] = useState<{
message: string;
caption: string;
value: string;
resolve: (aText: string | null) => void;
} | null>(null);
/** `wxFileDialog` (open) for the live tools while it is up. */
const [fileDialogRequest, setFileDialogRequest] = useState<{
title: string;
defaultDir: string;
filters: readonly ChooserFilter[];
/** `dlg.SetCustomizeHook( hook )`: the controls drawn under the chooser. */
hook?: FILEDLG_HOOK;
resolve: (aPath: string | null) => void;
} | null>(null);
/** PickSymbolFromLibrary's DIALOG_SYMBOL_CHOOSER while it is up. */
const [chooserRequest, setChooserRequest] = useState<{
filter: SYMBOL_LIBRARY_FILTER | null;
history: readonly PICKED_SYMBOL[];
placed: readonly PICKED_SYMBOL[];
showFootprints: boolean;
resolve: (aPicked: PICKED_SYMBOL | null) => void;
} | null>(null);
// MAIL_ASSIGN_FOOTPRINTS and MAIL_SCH_SAVE, filled in below once the
// project edit and the save they call exist.
const saveProjectRef = useRef<() => boolean>(() => false);
// TRANSITIONAL (S2-5b, gone at S7): the frame's live SCHEMATIC, rebuilt from the window's
// records when one changed (sch_record_bridge.ts).
const syncLiveRef = useRef<() => boolean>(() => false);
const editLiveRef = useRef<NonNullable<SchScriptApi['editLive']>>(async () => null);
const liveFilesRef = useRef<{
rawFiles: readonly PickedFile[];
projectName?: string;
rootPro?: string;
}>({ rawFiles: [] });
const modalAnnotateRef = useRef<(aMessage: string) => void>(() => {});
/**
* `SaveProject()` arriving in the same tick as the assignment it saves: the
* assignment's step folds on the next render, so it is marked to be written
* then rather than left to the debounced save.
*/
const saveRequestedRef = useRef(false);
const assignPendingRef = useRef(false);
// What a grid's text-button cell opens (GRID_CELL_FPID / URL / PATH_EDITOR): the frame's own
// file dialog and document opener.
const gridTextButtonHost: GRID_TEXT_BUTTON_HOST = {
// `Kiway().Player( FRAME_FOOTPRINT_CHOOSER )`, sent MAIL_SYMBOL_NETLIST: KiwayMailIn reads the
// pins (one per distinct number) from the first line and the filters from the second.
ChooseFootprint: (aPreselect, aNetlist) =>
new Promise((resolve) => {
const [pinLine = '', filterLine = ''] = aNetlist.split('\r');
const pinNames = new Set(
pinLine === '' ? [] : pinLine.split('\t').map((pin) => pin.split(' ')[0]!),
);
setFpChooser({
current: aPreselect,
commit: resolve,
cancel: () => resolve(null),
fpFilters: filterLine === '' ? [] : filterLine.split(' '),
pinCount: pinNames.size,
});
}),
OpenFile: (aTitle, aDefaultDir, aWildcard) =>
schFrameRef.current!.ShowFileDialog(
aTitle,
aDefaultDir,
'',
aWildcard,
wxFD_OPEN | wxFD_FILE_MUST_EXIST,
),
OpenDocument: (aUrl) =>
GetAssociatedDocument(aUrl, (aToken) => schFrameRef.current!.Prj().TextVarResolver(aToken)),
};
if (!schFrameRef.current) {
schFrameRef.current = new SCH_EDIT_FRAME({
// `DIALOG_xxx( this, … ).ShowModal()` for the live tools: each dialog KiCad names, by its C++
// class. One not ported yet is cancelled, as with no window, and says so in the console.
// The AUI half of ToggleSearch / ToggleSchematicHierarchy / ToggleNetNavigator /
// ToggleProperties: the window's panes are its local toggles.
// `m_netNavigator->IsShownOnScreen()`.
isNetNavigatorShown: () => togglesRef.current.has('showNetNavigator'),
// HIERARCHY_PANE::UpdateNetHighlight( aNetName ) (hierarchy_pane.cpp:850): the sheets the
// net runs on, their rows drawn in LAYER_BRIGHTENED.
hierarchyUpdateNetHighlight: (aNetName) => {
const frame = schFrameRef.current!;
const sheetsWithNet = new Set<string>();
if (aNetName !== '') {
for (const sg of frame.Schematic().ConnectionGraph().GetAllSubgraphs(aNetName))
if (sg.GetSheet().Last()) sheetsWithNet.add(sg.GetSheet().PathAsString());
}
const color = frame.GetRenderSettings()?.GetLayerColor(SCH_LAYER_ID.LAYER_BRIGHTENED);
setHierarchyNetMark({ paths: sheetsWithNet, color: color ? toCss(color) : '' });
},
togglePane: (aPane) => {
const id = {
Search: 'showSearch',
SchematicHierarchy: 'showHierarchy',
NetNavigator: 'showNetNavigator',
Properties: 'showProperties',
DesignBlocks: 'showDesignBlocks',
}[aPane as string];
if (!id) return;
setLocalToggles((prev) => {
const next = new Set(prev);
if (next.has(id)) next.delete(id);
else next.add(id);
return next;
});
},
showModal: (aDialog, _aItems, aArg) => {
if (aDialog === 'KICAD_MESSAGE_DIALOG')
return ShowKicadMessageDialog(aArg as KICAD_MESSAGE_DIALOG_ARG);
if (aDialog === 'DIALOG_WIRE_BUS_PROPERTIES') {
const items = _aItems as SCH_ITEM[];
const dlg = new DIALOG_WIRE_BUS_PROPERTIES(schFrameRef.current!, items);
return new Promise<number>((resolve) =>
setWireBusDialog({ dlg, shown: dlg.TransferDataToWindow(), resolve }),
);
}
if (aDialog === 'DIALOG_SHAPE_PROPERTIES') {
const shape = _aItems[0] as SCH_SHAPE;
const dlg = new DIALOG_SHAPE_PROPERTIES(schFrameRef.current!, shape);
return new Promise<number>((resolve) =>
setShapeDialog({
dlg,
// SCH_EDIT_TOOL names a rule area's dialog itself (dlg.SetTitle()).
title: typeof aArg === 'string' ? aArg : undefined,
shown: dlg.TransferDataToWindow(),
resolve,
}),
);
}
if (aDialog === 'DIALOG_IMAGE_PROPERTIES') {
const dlg = new DIALOG_IMAGE_PROPERTIES(schFrameRef.current!, _aItems[0] as SCH_BITMAP);
return new Promise<number>((resolve) =>
setImageDialog({ dlg, shown: dlg.TransferDataToWindow(), resolve }),
);
}
if (aDialog === 'DIALOG_SHEET_PIN_PROPERTIES') {
const dlg = new DIALOG_SHEET_PIN_PROPERTIES(
schFrameRef.current!,
_aItems[0] as SCH_SHEET_PIN,
);
return new Promise<number>((resolve) =>
setSheetPinDialog({ dlg, shown: dlg.TransferDataToWindow(), resolve }),
);
}
if (aDialog === 'DIALOG_TEXT_PROPERTIES') {
const dlg = new DIALOG_TEXT_PROPERTIES(
schFrameRef.current!,
_aItems[0] as SCH_TEXT | SCH_TEXTBOX,
);
return new Promise<number>((resolve) =>
setTextDialog({ dlg, shown: dlg.TransferDataToWindow(), resolve }),
);
}
if (aDialog === 'DIALOG_LABEL_PROPERTIES') {
// `DIALOG_LABEL_PROPERTIES dlg( m_frame, label, aNew )`, plus `SetLabelList` for a new
// label: Properties passes false, createNewLabel { isNew, labelList }.
const arg = aArg as boolean | { isNew: boolean; labelList: SCH_LABEL_BASE[] } | undefined;
const isNew = typeof arg === 'object' ? arg.isNew : !!arg;
const dlg = new DIALOG_LABEL_PROPERTIES(
schFrameRef.current!,
_aItems[0] as SCH_LABEL_BASE,
isNew,
gridTextButtonHost,
);
if (typeof arg === 'object') dlg.SetLabelList(arg.labelList);
return new Promise<number>((resolve) =>
setLabelDialog({ dlg, shown: dlg.TransferDataToWindow(), resolve }),
);
}
if (aDialog === 'DIALOG_FIELD_PROPERTIES') {
// `DIALOG_FIELD_PROPERTIES dlg( m_frame, caption, aField )`; editFieldText hands its commit
// for UpdateField.
const { caption, commit } = aArg as { caption: string; commit: SCH_COMMIT };
const field = _aItems[0] as SCH_FIELD;
const dlg = new DIALOG_FIELD_PROPERTIES(
schFrameRef.current!,
caption,
field,
gridTextButtonHost,
);
return new Promise<number>((resolve) =>
setFieldDialog({ dlg, shown: dlg.TransferDataToWindow(), field, commit, resolve }),
);
}
if (aDialog === 'DIALOG_SHEET_PROPERTIES') {
// EditSheetProperties: `DIALOG_SHEET_PROPERTIES dlg( this, aSheet, aIsUndoable, … )`.
const { result, sourceSheetFilename } = aArg as {
result: SHEET_PROPERTIES_RESULT;
sourceSheetFilename: string | null;
};
const dlg = new DIALOG_SHEET_PROPERTIES(
schFrameRef.current!,
_aItems[0] as SCH_SHEET,
result,
sourceSheetFilename,
gridTextButtonHost,
kiDialog.ask,
);
return new Promise<number>((resolve) =>
setSheetDialog({ dlg, shown: dlg.TransferDataToWindow(), resolve }),
);
}
if (aDialog === 'DIALOG_SYMBOL_PROPERTIES') {
const dlg = new DIALOG_SYMBOL_PROPERTIES(
schFrameRef.current!,
_aItems[0] as SCH_SYMBOL,
gridTextButtonHost,
);
return new Promise<number>((resolve) =>
setSymbolDialog({ dlg, shown: dlg.TransferDataToWindow(), resolve }),
);
}
if (aDialog === 'DIALOG_CHANGE_SYMBOLS') {
// `DIALOG_CHANGE_SYMBOLS dlg( m_frame, selectedSymbol, mode ); dlg.ShowQuasiModal()`.
const dlg = new DIALOG_CHANGE_SYMBOLS(
schFrameRef.current!,
(_aItems[0] as SCH_SYMBOL | undefined) ?? null,
aArg as DIALOG_CHANGE_SYMBOLS_MODE,
(aPreselect) =>
new Promise((resolve) => setSymbolChooser({ preselect: aPreselect, resolve })),
);
return new Promise<number>((resolve) => {
setChangeSymbolsDialog({ dlg, resolve });
void dlg.TransferDataToWindow();
});
}
if (aDialog === 'DIALOG_EDIT_SYMBOLS_LIBID') {
// InvokeDialogEditSymbolsLibId: `dlg.ShowQuasiModal(); return dlg.IsSchematicModified()`.
const dlg = new DIALOG_EDIT_SYMBOLS_LIBID(schFrameRef.current!, {
pickSymbolFromLibrary: (aPreselect) =>
new Promise((resolve) => setSymbolChooser({ preselect: aPreselect, resolve })),
singleChoice: (aMessage, aCaption, aChoices) =>
new Promise((resolve) =>
setLibIdChoice({
message: aMessage,
caption: aCaption,
choices: aChoices,
resolve,
}),
),
});
return new Promise<number>((resolve) => setLibIdDialog({ dlg, resolve }));
}
if (aDialog === 'DIALOG_TABLE_PROPERTIES') {
const table = _aItems[0] as SCH_TABLE;
const dlg = new DIALOG_TABLE_PROPERTIES(schFrameRef.current!, table);
return new Promise<number>((resolve) =>
setTableDialog({
dlg,
shown: dlg.TransferDataToWindow(),
isNew: table.IsNew(),
resolve,
}),
);
}
if (aDialog === 'DIALOG_TABLECELL_PROPERTIES') {
// `dlg.ShowQuasiModal(); dlg.GetReturnValue()`: the cell dialog answers its return value.
const dlg = new DIALOG_TABLECELL_PROPERTIES(
schFrameRef.current!,
_aItems as SCH_TABLECELL[],
);
return new Promise<number>((resolve) =>
setCellDialog({ dlg, shown: dlg.TransferDataToWindow(), resolve }),
);
}
if (aDialog === 'DIALOG_JUNCTION_PROPS') {
const junctions = _aItems as SCH_JUNCTION[];
const dlg = new DIALOG_JUNCTION_PROPS(schFrameRef.current!, junctions);
return new Promise<number>((resolve) =>
setJunctionDialog({ dlg, shown: dlg.TransferDataToWindow(), resolve }),
);
}
if (aDialog === 'DIALOG_LINE_PROPERTIES') {
const dlg = new DIALOG_LINE_PROPERTIES(schFrameRef.current!, _aItems as SCH_LINE[]);
return new Promise<number>((resolve) =>
setLineDialog({ dlg, shown: dlg.TransferDataToWindow(), resolve }),
);
}
if (aDialog === 'DIALOG_GLOBAL_EDIT_TEXT_AND_GRAPHICS') {
// `DIALOG_GLOBAL_EDIT_TEXT_AND_GRAPHICS dlg( m_frame ); dlg.ShowModal();`
const dlg = new DIALOG_GLOBAL_EDIT_TEXT_AND_GRAPHICS(schFrameRef.current!);
return new Promise<number>((resolve) =>
setGlobalEditDialog({ dlg, shown: dlg.TransferDataToWindow(), resolve }),
);
}
if (aDialog === 'DIALOG_SYNC_SHEET_PINS') {
// `m_dialogSyncSheetPin->Show( true )`: modeless, so the tool goes on at once.
setSyncDialog(aArg as DIALOG_SYNC_SHEET_PINS);
return wxID_OK;
}
if (aDialog === 'DIALOG_PASTE_SPECIAL') {
const dlg = aArg as { pasteMode: PasteSpecialMode };
return new Promise<number>((resolve) => setPasteSpecialDialog({ dlg, resolve }));
}
if (aDialog === 'DIALOG_INCREMENT_ANNOTATIONS_BASE') {
const values = aArg as INCREMENT_ANNOTATIONS_VALUES;
return new Promise<number>((resolve) => setIncrementDialog({ values, resolve }));
}
if (aDialog === 'DIALOG_UPDATE_FROM_PCB') {
// `DIALOG_UPDATE_FROM_PCB dlg( m_frame ); dlg.ShowQuasiModal();` The board is read first,
// so the dialog's dry runs get FetchNetlistFromPCB's answer synchronously.
return (async () => {
const netlist = (await readBoardNetlistRef.current?.()) ?? null;
setHeadlessPcbNetlistProvider(() => netlist);
return new Promise<number>((resolve) => setUpdateFromPcbDialog({ resolve }));
})();
}
if (aDialog === 'DIALOG_ASSIGN_NETCLASS') {
const arg = aArg as {
netNames: readonly string[];
candidates: readonly string[];
previewer: (aNetNames: readonly string[]) => void;
};
return new Promise<number>((resolve) => setNetclassDialog({ ...arg, resolve }));
}
if (aDialog === 'DIALOG_IMPORT_GFX_SCH') {
return new Promise<number>((resolve) =>
setImportGfx({ result: aArg as IMPORT_GFX_RESULT, resolve }),
);
}
if (aDialog === 'wxMessageBox')
return ShowKicadMessageDialog(aArg as KICAD_MESSAGE_DIALOG_ARG);
if (aDialog === 'DIALOG_EESCHEMA_PAGE_SETTINGS') {
const dlg = new DIALOG_EESCHEMA_PAGE_SETTINGS(schFrameRef.current!);
return new Promise<number>((resolve) => setPageDialog({ dlg, resolve }));
}
console.warn(`${aDialog} is not ported to the live model yet`);
return wxID_CANCEL;
},
textEntry: (aMessage, aCaption, aValue) =>
new Promise((resolve) =>
setTextEntryRequest({ message: aMessage, caption: aCaption, value: aValue, resolve }),
),
askLibraryName: (aTitle, aDefaultName, aHook) =>
new Promise((resolve) =>
setLibraryNameRequest({
title: aTitle,
name: aDefaultName,
...(aHook ? { hook: aHook } : {}),
resolve,
}),
),
fileDialog: (aTitle, aDefaultDir, _aDefaultFile, aWildcard, aStyle, aCustomizeHook) => {
// The project's picker opens files; naming a new one to write is not ported yet.
if (aStyle & wxFD_SAVE) {
console.warn(`wxFileDialog(wxFD_SAVE) '${aTitle}' is not ported yet`);
return null;
}
return new Promise((resolve) =>
setFileDialogRequest({
title: aTitle,
defaultDir: aDefaultDir,
filters: typeof aWildcard === 'string' ? [] : aWildcard,
...(aCustomizeHook ? { hook: aCustomizeHook } : {}),
resolve,
}),
);
},
pickSymbol: (aFilter, aHistory, aPlaced, aShowFootprints) =>
new Promise((resolve) =>
setChooserRequest({
filter: aFilter,
history: aHistory,
placed: aPlaced,
showFootprints: aShowFootprints,
resolve,
}),
),
crossProbingSettings: () => app.settings.eeschema.cross_probing,
saveProject: () => saveProjectRef.current(),
syncLiveSchematic: () => syncLiveRef.current(),
modalAnnotate: (aMessage) => modalAnnotateRef.current(aMessage),
symbolLibraryUri: (aNickname) => symbolLibraryUri(liveFilesRef.current.rawFiles)(aNickname),
liveModified: () => liveModifiedRef.current(),
liveSheetChanged: () => liveSheetChangedRef.current(),
ercDialog: () => ercDialogFactoryRef.current(),
symbolFieldsTableDialog: () => fieldsTableFactoryRef.current(),
});
}
// `KIWAY::Player()` stores the frame it created as FRAME_SCH's player, and
// the frame's close tells KIWAY it is gone (`PlayerDidClose`).
useEffect(() => {
const frame = schFrameRef.current!;
if (!kiway) return;
frame.SetKiway(kiway);
kiway.SetPlayerFrame(FRAME_T.FRAME_SCH, frame);
return () => {
kiway.PlayerDidClose(FRAME_T.FRAME_SCH, frame);
frame.SetKiway(null);
};
}, [kiway]);
/** Tools > Update PCB from Schematic (F8): `SCH_EDIT_FRAME::OnUpdatePCB`. */
const onUpdatePcb = useMemo(
() => (hasBoard && kiway ? () => schFrameRef.current!.OnUpdatePCB() : undefined),
[hasBoard, kiway],
);
// The live document for stable callbacks is `docRef`, kept by `setDoc`.
// Which file that document is, for the same reason: an undo step is applied
// inside a `setDoc` updater, where the state value of `currentFile` may be a
// render behind.
const currentFileRef = useRef(currentFile);
currentFileRef.current = currentFile;
/**
* `grid.GetGrid().x` for the rules that measure in grid squares — right now
* only `SCH_SELECTION_TOOL::Main`'s right-click test. A ref because
* `gridSizeIU` is derived far below and these callbacks are built once.
*/
const gridSizeIURef = useRef(0);
/** A live TOOL_ACTION run on the frame's TOOL_MANAGER, for a hotkey the canvas did not take. */
const runLiveAction = useCallback((aAction: TOOL_ACTION) => {
const frame = schFrameRef.current;
frame?.GetToolManager()?.RunAction(aAction);
// wxEVT_IDLE follows a menu command as it follows a canvas event: what the action wrote into
// the frame's settings (COMMON_TOOLS' grid) is stored there.
frame?.OnIdle();
}, []);
/** ACTION_CONDITIONS::checkCondition of a live action, on the live selection. */
const liveChecked = useCallback((aAction: TOOL_ACTION): boolean => {
const mgr = schFrameRef.current?.GetToolManager();
const selection = mgr?.GetTool(SCH_SELECTION_TOOL)?.GetSelection();
if (!mgr || !selection) return false;
return mgr.GetActionManager().GetCondition(aAction)?.checkCondition(selection) ?? false;
}, []);
/**
* Fold a history step back into the project.
*
* The step names every document it changed. The one on screen becomes the new
* `doc`; the rest are written into the project and queued for the host, so a
* single Ctrl+Z that reverts an edit spanning three sheets really does put all
* three back — `PutDataInPreviousState` adding each item to the screen its own
* picker names.
*
* Called from inside a `setDoc` updater, so it must not call `setState`: the
* project's documents live in a ref, and the files are persisted after the
* render.
*/
const foldStep = useCallback(
(step: ProjectStep | null, fallback: Schematic, persist = false): Schematic => {
if (!step) return fallback;
const here = currentFileRef.current;
const changed: PickedFile[] = [];
for (const [file, next] of step.docs) {
// The open sheet is written to disk by the ordinary (debounced) save, so
// it is reported only when the caller asked to persist right now.
if (file === here && !persist && !saveRequestedRef.current) continue;
if (file !== here) project.current.docs.set(file, next);
try {
changed.push({ name: file, text: serializeSchematic(next) });
} catch {
/* skip a bad sheet */
}
}
if (changed.length) {
pendingProjectChange.current.push(...changed);
if (!applyingRemoteRef.current) pendingIsMine.current = true;
}
if (persist || saveRequestedRef.current) pendingPersist.current = true;
// The one status whose undo navigates; see `EditCommand.pageSettings`.
if (step.showSheet && step.showSheet !== here) pendingShowSheet.current = step.showSheet;
return step.docs.get(here) ?? fallback;
},
[],
);
/** Files an edit or undo folded back, flushed to the host after the render. */
const pendingProjectChange = useRef<PickedFile[]>([]);
/**
* Whether anything in the queued batch is MY work.
*
* A peer's edit is written to this machine -- it has to be, or a reload shows
* a sheet older than the one on screen -- but it must not be pushed to the
* cloud from here. Every peer holding the same change would otherwise race
* the version compare-and-swap to commit bytes they did not author, N pushes
* for one edit. The author's own push is what puts it there.
*
* Sticky across a batch and cleared with it: one local keystroke in the same
* frame as a remote patch still has to reach the account.
*/
const pendingIsMine = useRef(false);
/** …and whether the caller wanted them written now rather than on the timer. */
const pendingPersist = useRef(false);
/**
* `ClearUndoRedoList` asked for by an edit that has just been queued.
*
* The Project Rescue Helper is the one operation that drops the whole undo
* history when it finishes; it cannot do it at the call site, because the
* entry it is dropping has not been pushed yet — `runProject` folds inside a
* `setDoc` updater, which React runs at the next render.
*/
const pendingClearHistory = useRef(false);
useEffect(() => {
saveRequestedRef.current = false;
assignPendingRef.current = false;
if (pendingClearHistory.current) {
pendingClearHistory.current = false;
history.current.clear();
}
if (pendingProjectChange.current.length === 0) return;
const files = pendingProjectChange.current;
const persist = pendingPersist.current;
const mine = pendingIsMine.current;
pendingProjectChange.current = [];
pendingPersist.current = false;
pendingIsMine.current = false;
onProjectChange?.(files, { push: mine });
if (persist) onPersistFiles?.(files);
});
/** A sheet an undo asked to be shown (page settings), navigated to after. */
const pendingShowSheet = useRef<string | null>(null);
/**
* The project's live documents as a history step must see them, from inside a
* `setDoc` updater — the open sheet's edits are in `d`, not yet in the map.
*/
const docsWith = (d: Schematic): Map<string, Schematic> =>
new Map(project.current.docs).set(currentFileRef.current, d);
/**
* Run one edit as a SINGLE undo entry, over whichever sheets it touches.
*
* This is `SCH_COMMIT::Push`: the commit has staged each modified item with
* the screen it belongs to, and pushes one `PICKED_ITEMS_LIST` onto the
* frame's undo list. An edit confined to the open sheet is not a different
* case, just a one-file one.
*/
const runProject = useCallback(
(edit: ProjectEdit, persist = false): void => {
// The one choke point every edit funnels through, whichever sheets it
// touches — see PcbEditor.tsx's own copy of this guard and
// eeschema/project_sync_transport.ts's PeerRole.
// applyingRemoteRef is what lets a remote update still land on a
// Viewer's own tab while refusing a local edit.
if (!applyingRemoteRef.current && myRoleRef.current === 'viewer') return;
setDoc((d) => {
if (!d) return d;
const staged = new Map<string, EditCommand>();
for (const [file, cmd] of edit) staged.set(file, withCleanup(cmd, libById));
const docs = docsWith(d);
// Undo is over MY operations. A peer's edit still lands on the
// document -- that is the whole point of it arriving -- but it does not
// become an entry on my stack and does not clear my redo. See
// `ProjectHistory.applyUnrecorded`.
const step = applyingRemoteRef.current
? history.current.applyUnrecorded(docs, staged)
: history.current.execute(docs, staged);
return foldStep(step, d, persist);
});
},
[libById, foldStep],
);
/** The overwhelmingly common case: an edit on the sheet you are looking at. */
const runCommand = useCallback(
(cmd: EditCommand) => runProject(new Map([[currentFileRef.current, cmd]])),
[runProject],
);
const runCommandRef = useRef(runCommand);
runCommandRef.current = runCommand;
const runProjectRef = useRef(runProject);
runProjectRef.current = runProject;
// biome-ignore lint/correctness/useExhaustiveDependencies: everything it reads goes through refs or stable callbacks
useEffect(() => {
// Registered for as long as the frame exists, shown or not: the AI
// works on the schematic while the board is on screen, and back.
if (!registerScriptApi) return;
return registerScriptApi({
doc: () => docRef.current,
runCommand: (cmd) => runCommandRef.current(cmd),
annotatePlacement: (sym, lib) => annotatePlacementRef.current(sym, lib),
loadSymbol: (library, name) => app.loadSymbol(library, name),
erc: () => ercLinesRef.current(),
symbolIndex: () => app.loadIndex(),
footprintIndex: () => app.loadFootprintIndex(),
snapshot: () => snapshotRef.current(),
undo: () => undoRef.current(),
docs: () => {
const docs = new Map(project.current.docs);
if (docRef.current) docs.set(currentFileRef.current, docRef.current);
return docs;
},
currentFile: () => currentFileRef.current,
runCommandOn: (file, cmd) => runProjectRef.current(new Map([[file, cmd]])),
editLive: (aEdit) => editLiveRef.current(aEdit),
readLive: (aRead) => (syncLiveRef.current() ? aRead(schFrameRef.current!) : null),
});
}, [registerScriptApi]);
const undo = useCallback(
() => setDoc((d) => (d ? foldStep(history.current.undo(docsWith(d)), d) : d)),
[foldStep],
);
const undoRef = useRef(undo);
undoRef.current = undo;
const redo = useCallback(
() => setDoc((d) => (d ? foldStep(history.current.redo(docsWith(d)), d) : d)),
[foldStep],
);
// The schematic hierarchy (SCH_SHEET_LIST): rebuilt from the live documents so
// sheet edits (adding/renaming sheets) reflect immediately.
const sheetTree = useMemo<SheetTreeNode | null>(() => {
if (!doc) return null;
const docs = new Map(project.current.docs);
docs.set(currentFile, doc);
return buildSheetTree(docs, project.current.root);
}, [doc, currentFile]);
// Depth-first hierarchy order (virtual page numbers) + Back/Forward history
// (SCH_NAVIGATE_TOOL). Sheet edits prune dead history entries (CleanHistory).
const flatSheets = useMemo<SheetRef[]>(
() => (sheetTree ? flattenHierarchy(sheetTree) : []),
[sheetTree],
);
// The same DFS with each instance's sheet name and human-readable path
// (SCH_SHEET_PATH::PathHumanReadable), the title block's ${SHEETNAME} /
// ${SHEETPATH} context for the screen and for printed pages.
const sheetInstanceRefs = useMemo<
{ file: string; path: string; name: string; namePath: string }[]
>(() => {
const refs: { file: string; path: string; name: string; namePath: string }[] = [];
const walk = (n: SheetTreeNode, parentNames: string): void => {
const namePath = n.path === '/' ? '/' : `${parentNames}${n.name}/`;
refs.push({ file: n.file, path: n.path, name: n.name, namePath });
for (const c of n.children) walk(c, namePath);
};
if (sheetTree) walk(sheetTree, '/');
return refs;
}, [sheetTree]);
const navTool = useRef(new SchNavigateTool());
// UpdateHierarchyNavigator's CleanHistory (sch_edit_frame.cpp:1398): when the hierarchy
// changes, not on every sheet change - it puts the cursor at the end, which would undo Back.
// flatSheets is rebuilt on each change of sheet; the paths it holds are what matter.
const sheetPathsKey = flatSheets.map((s) => s.path).join('\n');
useEffect(() => {
navTool.current.cleanHistory(new Set(sheetPathsKey.split('\n')));
}, [sheetPathsKey]);
// Live documents with the on-screen sheet's edits folded in.
const liveDocs = useCallback((): Map<string, Schematic> => {
const docs = new Map(project.current.docs);
if (doc) docs.set(currentFile, doc);
return docs;
}, [doc, currentFile]);
// ----- Choose Symbol dialog (DIALOG_SYMBOL_CHOOSER) ----------------------------
/**
* Dismissed by Cancel, and reopened by the next click.
*
* `PickSymbolFromLibrary` returning an invalid LIB_ID is a `continue`
* (sch_drawing_tools.cpp:416-419): back to the top of `Wait()` with no
* `PopTool` and no `break`, so the TOOL STAYS ACTIVE and the chooser comes
* back on the next click, which is the click branch that opened it in the
* first place (:371-375). Ours called `setActiveTool('select')`, dropping the
* tool the moment the dialog closed.
*
* It cannot simply be derived from "the tool is active and nothing is on the
* cursor", because that is true again the instant Cancel returns and the
* dialog would reopen forever. Upstream is event-driven and so is this.
*/
/**
* The Selection Filter's close box, which holds only until its visibility is
* derived again.
*
* The pane has no visibility control of its own - upstream's own comment,
* "Don't give the selection filter its own visibility controls; instead show
* it if anything else is visible" - but its pane info still asks for
* `.CloseButton( true )` (eeschema_settings.cpp:120). Closing it hides the
* pane, and the next `updateSelectionFilterVisbility` writes the derived
* answer back over it (sch_edit_frame.cpp:2817-2831). That runs whenever a
* pane opens or closes, which is what clears the latch here.
*/
const [selectionFilterClosed, setSelectionFilterClosed] = useState(false);
const [chooserDismissed, setChooserDismissed] = useState(false);
const chooserOpen =
(activeTool === 'placeSymbol' || activeTool === 'placePower') && !placeLib && !chooserDismissed;
/**
* The selection goes dark as the chooser OPENS, not when you pick from it.
*
* `selectionClear` is the first statement inside the click branch's
* `if( !symbol )`, ahead of the whole already-placed scan and of
* `PickSymbolFromLibrary` itself (sch_drawing_tools.cpp:375-377):
*
* if( !symbol )
* {
* m_toolMgr->RunAction( ACTIONS::selectionClear );
* ...
* PICKED_SYMBOL sel = m_frame->PickSymbolFromLibrary( ... );
*
* so the symbol placed a moment ago is unhighlighted the instant the dialog
* appears — which is also the instant `A` primes the tool, since the prime
* IS that click. Ours held the highlight through the whole chooser session.
*/
useEffect(() => {
if (chooserOpen) setSelection(new Set());
}, [chooserOpen]);
/**
* Activating the tool primes it, and a primed event IS a click here.
* `PrimeTool` posts `TOOL_EVENT( TC_MOUSE, TA_PRIME, BUT_LEFT )`
* (tool_manager.cpp:414-430); `TA_PRIME` is 0x800001 and carries
* `TA_MOUSE_CLICK`'s 0x0001 bit, and `IsClick()` tests exactly that bit
* (tool_event.cpp:212-215). With `input.immediate_actions` set, which is its
* default (common_settings.cpp:251-252), the tool therefore opens its chooser
* on activation without the user clicking anything.
*/
useEffect(() => {
setChooserDismissed(false);
}, [activeTool]);
/** `updateSelectionFilterVisbility` runs on every pane show/hide. */
const selFilterInputs = `${toggles.has('showNetNavigator')}|${toggles.has('showHierarchy')}|${toggles.has('showProperties')}`;
useEffect(() => {
setSelectionFilterClosed(false);
}, [selFilterInputs]);
// The stored page number of the sheet instance at `path`
// (SCH_SHEET_PATH::GetPageNumber): the root sheet from the document-level
// sheet_instances, a sub-sheet from its object's instances in the parent doc.
const pageNumberOf = useCallback(
(path: string): string => {
const docs = liveDocs();
const rootDoc = docs.get(project.current.root);
if (path === '/') return rootDoc ? getRootPageNumber(rootDoc) : '';
const rootUuid = rootDoc?.uuid;
if (!rootUuid) return '';
const chain = path.split('/').filter(Boolean);
const ownUuid = chain[chain.length - 1];
const parent = flatSheets.find((s) => s.path === (parentPath(path) ?? '/'));
const parentDoc = docs.get(parent?.file ?? project.current.root);
const sheet = parentDoc?.sheets.find((s) => s.uuid === ownUuid);
return sheet ? getSheetPageNumber(sheet, instanceKey(rootUuid, chain)) : '';
},
[liveDocs, flatSheets],
);
// Annotate Schematic (SCH_EDIT_FRAME::AnnotateSymbols) dialog.
const [annotateOpen, setAnnotateOpen] = useState(false);
// SCH_ACTIONS::incrementAnnotations, a small dialog of its own.
// DIALOG_INCREMENT_ANNOTATIONS for the live SCH_EDITOR_CONTROL::IncrementAnnotations: the
// values object the tool reads back, and the answer it waits on.
const [incrementDialog, setIncrementDialog] = useState<{
values: INCREMENT_ANNOTATIONS_VALUES;
resolve: (aRetval: number) => void;
} | null>(null);
// DIALOG_GLOBAL_EDIT_TEXT_AND_GRAPHICS on the live schematic (SCH_EDIT_TOOL::GlobalEdit).
const [globalEditDialog, setGlobalEditDialog] = useState<{
dlg: DIALOG_GLOBAL_EDIT_TEXT_AND_GRAPHICS;
shown: GLOBAL_EDIT_VALUES;
resolve: (aRetval: number) => void;
} | null>(null);
// DIALOG_EDIT_SYMBOLS_LIBID (Bulk Edit Symbol Library Links), and the wxSingleChoiceDialog its
// Map Orphans asks when a part name is found in more than one library.
const [libIdDialog, setLibIdDialog] = useState<{
dlg: DIALOG_EDIT_SYMBOLS_LIBID;
resolve: (aRetval: number) => void;
} | null>(null);
const [libIdChoice, setLibIdChoice] = useState<{
message: string;
caption: string;
choices: readonly string[];
resolve: (aPick: string | null) => void;
} | null>(null);
// DIALOG_CHANGE_SYMBOLS, in whichever of its two modes was asked for.
/**
* The symbol DIALOG_CHANGE_SYMBOLS was opened ON, which it seeds all three
* match entries from — `m_symbol` is its second constructor argument and
* `TransferDataToWindow` (:146-152) fills reference, value and library id
* from it. Null when it is opened from the Tools menu, and then upstream
* hides the "selected symbol(s)" radio outright.
*/
// Page Settings (DIALOG_PAGES_SETTINGS), Print (DIALOG_PRINT) and Plot
// (DIALOG_PLOT_SCHEMATIC) dialogs, open flags.
// DIALOG_EESCHEMA_PAGE_SETTINGS for the live SCH_EDITOR_CONTROL::PageSetup.
const [pageDialog, setPageDialog] = useState<{
dlg: DIALOG_EESCHEMA_PAGE_SETTINGS;
resolve: (aRetval: number) => void;
} | null>(null);
// Raw project files (kept for the .kicad_pro drawing-sheet reference and the
// project's .kicad_wks files); reseeded whenever a project is (re)opened.
const [rawFiles, setRawFiles] = useState<PickedFile[]>(() => initialProject ?? []);
// In-session Page Settings override of the drawing sheet: `name` '' = built-in
// default. Persisted to .kicad_pro (schematic.page_layout_descr_file) on OK;
// otherwise the sheet is resolved from the project like KiCad does.
const [sheetOverride, setSheetOverride] = useState<{
name: string;
sheet: WksSheet | null;
} | null>(null);
// Project files plus any .kicad_wks saved this session (the .kicad_pro
// reference lives in rawFiles; the sheets themselves may come from either).
const allFiles = useMemo(
() => (extraSheetFiles?.length ? [...rawFiles, ...extraSheetFiles] : rawFiles),
[rawFiles, extraSheetFiles],
);
// The drawing sheet to draw (override else the project reference) and its
// file name for `SetWksFileName` in the Page Settings dialog.
const activeSheet = useMemo(
() => (sheetOverride ? sheetOverride.sheet : resolveActiveSheet(allFiles)),
[allFiles, sheetOverride],
);
const sheetRefName = sheetOverride ? sheetOverride.name : readSheetRef(rawFiles);
// WX_INFOBAR message posted by a tool (null = hidden).
const [infoBar, setInfoBar] = useState<string | null>(null);
/**
* `SetStatusText( msg, 0 )` — field 0 of the status bar. wx leaves whatever
* was written there until something writes over it, so this is state rather
* than a transient toast. The highlight message shares the field and takes
* precedence while a net is actually highlighted.
*/
const [statusText, setStatusText] = useState<string>('');
// `EDA_BASE_FRAME::SetStatusText` from the frame: field 0 is state (the net highlight shares
// it); zoom, coordinates and deltas (SCH_BASE_FRAME::UpdateStatusBar on every event) are
// written straight to their panes, so the pointer's motion re-renders nothing.
const statusPaneRefs = useRef<Record<string, HTMLSpanElement | null>>({});
useEffect(() => {
const frame = schFrameRef.current!;
frame.SetStatusTextSink((aText, aField) => {
if (aField === 0) {
setStatusText(aText);
return;
}
const name = KISTATUSBAR_FIELDS[aField];
const el = name && name !== 'grid' && name !== 'units' ? statusPaneRefs.current[name] : null;
if (el) el.textContent = aText;
});
return () => frame.SetStatusTextSink(null);
}, []);
/** ACTIONS::revert's IsOK(), while it is up. */
const [revertPrompt, setRevertPrompt] = useState<{
file: string;
onYes: () => void;
onNo: () => void;
} | null>(null);
const [printOpen, setPrintOpen] = useState(false);
const [plotOpen, setPlotOpen] = useState(false);
// Paste Special (DIALOG_PASTE_SPECIAL): pick the PASTE_MODE before pasting.
// DIALOG_PASTE_SPECIAL for the live SCH_EDITOR_CONTROL::Paste: its { pasteMode } and answer.
const [pasteSpecialDialog, setPasteSpecialDialog] = useState<{
dlg: { pasteMode: PasteSpecialMode };
resolve: (aRetval: number) => void;
} | null>(null);
// Schematic Setup (DIALOG_SCHEMATIC_SETUP): project-scoped settings, incl. the
// ERC severities + pin-conflict map that the ERC checker reads. (The setup
// state itself is declared above the netlist memo, which consumes it.)
const [setupOpen, setSetupOpen] = useState(false);
// Export Netlist (DIALOG_EXPORT_NETLIST) dialog.
const [netlistOpen, setNetlistOpen] = useState(false);
// Assign Footprints (CVPCB_MAINFRAME).
const [assignFpOpen, setAssignFpOpen] = useState(false);
// The sheets of THIS design, in hierarchy order, cvpcb is handed the
// current schematic's netlist, so sibling projects sharing the folder (and
// sheets reached twice) must not add rows.
const assignFpFiles = useMemo(() => {
const seen = new Set<string>();
const out: string[] = [];
for (const s of sheetInstanceRefs) {
if (seen.has(s.file)) continue;
seen.add(s.file);
out.push(s.file);
}
return out.length > 0 ? out : [currentFile];
}, [sheetInstanceRefs, currentFile]);
// The project's own `.pretty` libraries (the fp-lib-table's project scope),
// which Assign Footprints lists ahead of the global libraries. The table
// itself comes along: it holds the library nicknames the FPIDs are written
// with ("Footprints:…" for `${KIPRJMOD}/footprints.pretty`).
const projectFootprintFiles = useMemo(
() =>
rawFiles
.filter(
(f) =>
/\.kicad_mod$/i.test(f.name) ||
/(^|\/)fp-lib-table$/i.test(f.name) ||
// The `.equ` footprint association files, which automatic
// association reads, and the `.kicad_pro` that lists them at
// `cvpcb.equivalence_files`.
/\.equ$/i.test(f.name) ||
/\.kicad_pro$/i.test(f.name),
)
.map((f) => ({ name: f.name, text: f.text })),
[rawFiles],
);
// Manage Footprint Libraries: write the project's `fp-lib-table` (creating it
// next to the `.kicad_pro` when the project has none) and keep it in memory,
// so a library registered here resolves immediately.
const saveProjectFpLibTable = useCallback(
(rows: FpLibRow[], libraryFiles: { name: string; text: string }[] = []) => {
const name = projectFpLibTablePath(rawFiles);
const text = serializeFpLibTable(rows);
// The table, and the libraries "Add Existing" brought in off the computer.
const written = [{ name, text }, ...libraryFiles];
setRawFiles((prev) => [
...prev.filter((f) => !written.some((w) => w.name === f.name)),
...written,
]);
onPersistFiles?.(written);
},
[rawFiles, onPersistFiles],
);
// Manage Footprint Association Files' OK: `cvpcb.equivalence_files` into the
// project's `.kicad_pro` (upstream's `SaveProject()`,
// dialog_config_equfiles.cpp:116), plus any `.equ` file Add brought in from
// outside the project — which has to be written too, because
// `buildEquivalenceList` re-reads the reference on every press.
const saveProjectEquFiles = useCallback(
(files: readonly string[], newFiles: readonly { name: string; text: string }[]) => {
const pro = findProjectPro(rawFiles);
const written: { name: string; text: string }[] = [...newFiles];
if (pro) {
const text = writeEquivalenceFilesText(pro.text, files);
if (text !== null) written.push({ name: pro.name, text });
}
if (written.length === 0) return;
setRawFiles((prev) => {
const byName = new Map(written.map((f) => [f.name, f.text]));
const updated = prev.map((f) =>
byName.has(f.name) ? { ...f, text: byName.get(f.name) as string } : f,
);
const known = new Set(prev.map((f) => f.name));
return [...updated, ...written.filter((f) => !known.has(f.name))];
});
onPersistFiles?.(written);
},
[rawFiles, onPersistFiles],
);
// Manage Symbol Libraries: the same for the project's `sym-lib-table`. A row
// written here is what makes the library exist — SYMBOL_LIB_TABLE resolves a
// LIB_ID's nickname through this table, so nothing else can register one.
const saveProjectSymLibTable = useCallback(
(rows: FpLibRow[], libraryFiles: { name: string; text: string }[] = []) => {
const name = projectSymLibTablePath(rawFiles);
const text = serializeSymLibTable(rows);
// The table, and the libraries "Add Existing" brought in off the computer.
const written = [{ name, text }, ...libraryFiles];
setRawFiles((prev) => [
...prev.filter((f) => !written.some((w) => w.name === f.name)),
...written,
]);
onPersistFiles?.(written);
// The table decides what ERC can resolve, so drop the cached library set.
},
[rawFiles, onPersistFiles],
);
const [symLibTableOpen, setSymLibTableOpen] = useState(false);
/** The hosted libraries, the global table's stand-in in the dialog.
* Fetched when it first opens; the ERC cache is not reused because a run
* merges the project's own rows into it. */
const [hostedSymbolLibs, setHostedSymbolLibs] = useState<readonly HOSTED_LIBRARY[]>([]);
useEffect(() => {
if (!symLibTableOpen || hostedSymbolLibs.length > 0) return;
void loadIndex()
.then((index) => setHostedSymbolLibs(index.map(({ name, descr }) => ({ name, descr }))))
.catch(() => setHostedSymbolLibs([]));
}, [symLibTableOpen, hostedSymbolLibs.length, loadIndex]);
// The Annotation Messages the last Annotate / Clear Annotation run produced;
// the dialog stays open showing them (WX_HTML_REPORT_PANEL).
const [annotateMessages, setAnnotateMessages] = useState<readonly ReportLine[]>([]);
// The drawing sheet the Page Settings dialog chose (name '' = the built-in default): adopted
// for the view and persisted into .kicad_pro (schematic.page_layout_descr_file), like KiCad.
const adoptDrawingSheet = useCallback(
(sheet: WksSheet | null, sheetName: string) => {
setSheetOverride({ name: sheetName, sheet });
setRawFiles((prev) => {
const pro = prev.find((f) => /\.kicad_pro$/i.test(f.name));
if (!pro) return prev;
const updated = writeSheetRefText(pro.text, sheetName);
if (updated === null || updated === pro.text) return prev;
const changed = { name: pro.name, text: updated };
// Persist the reference now (not via the debounced autosave) so a
// reopen straight after picking the sheet reads it back.
onPersistFiles?.([changed]);
return prev.map((f) => (f.name === pro.name ? changed : f));
});
},
[onPersistFiles],
);
// A base file name for a printed/plotted output (KiCad names plots after the
// sheet file): the current sheet's name without extension, else the title.
const outputBaseName = useCallback((): string => {
const base = currentFile !== DEFAULT_FILE ? currentFile : (fileName ?? '');
const noExt = base.replace(/\.kicad_sch$/i, '');
return noExt || doc?.titleBlock?.title || 'schematic';
}, [currentFile, fileName, doc]);
// Commit a new SchematicSetup: into the live project's PROJECT_FILE - SCHEMATIC_SETTINGS,
// ERC_SETTINGS, NET_SETTINGS and the text variables all nest there - then
// SETTINGS_MANAGER::SaveProject, as KiCad saves after DIALOG_SCHEMATIC_SETUP. The file is what
// the window's own copy and its `setup` are re-read from (the project mount's write listener),
// so the live settings are the only writer. The panels still edit the `.kicad_pro` shape, which
// is the interchange until each becomes its own live panel. Used by the Schematic Setup dialog's
// OK and by the dialogs that write single settings back (Annotate, BOM presets, net classes).
const commitSetup = useCallback((next: SchematicSetup) => {
const frame = schFrameRef.current;
if (!frame || !syncLiveRef.current()) {
console.warn('Schematic Setup: no live project to write the settings to');
return;
}
const projectFile = frame.Prj().GetProjectFile() as PROJECT_FILE;
const updated = writeSchematicSetupText(JSON.stringify(projectFile.SaveToJson()), next);
if (updated === null) return;
projectFile.LoadFromFile(JSON.parse(updated) as JsonValue);
// SCHEMATIC::SetBusAliases, as PANEL_SETUP_BUSES::TransferDataFromWindow ends.
frame.Schematic().SetBusAliases(
[...projectFile.m_BusAliases].map(([name, members]) => {
const alias = new BUS_ALIAS();
alias.SetName(name);
alias.SetMembers(members);
return alias;
}),
);
Pgm().GetSettingsManager().SaveProject();
}, []);
/** DIALOG_SCHEMATIC_SETUP's TransferDataToWindow: the live project's settings, as the panels edit them. */
const liveSetup = useCallback((): SchematicSetup => {
const frame = schFrameRef.current;
if (!frame || !syncLiveRef.current()) return setup;
const projectFile = frame.Prj().GetProjectFile() as PROJECT_FILE;
return {
...readSchematicSetupText(JSON.stringify(projectFile.SaveToJson())),
embeddedFiles: setup.embeddedFiles,
};
}, [setup]);
/** The dialog's starting values, taken once as it opens. */
const [setupValue, setSetupValue] = useState<SchematicSetup | null>(null);
// biome-ignore lint/correctness/useExhaustiveDependencies: opening is the trigger; liveSetup reads the project then
useEffect(() => {
setSetupValue(setupOpen ? liveSetup() : null);
}, [setupOpen]);
// Per-item netclass render fallbacks (wire colour/width/style, junction
// clamp) for the current sheet, reuses the connectivity memo; undefined
// when no class carries a visual parameter.
// Keyed on connDoc alongside the graph it resolves against, the ids in the
// override maps are only meaningful for the document the netlist was built
// from. An item added since simply has no override for a frame.
const netOverrides = useMemo(
() =>
connDoc
? computeNetClassOverrides(connDoc, libById, setup, netlist, [
// Netclass directive labels assign to whatever net they sit on.
...directiveNetclassAssignments(connDoc, netlist),
// ...and a rule area assigns to every net it encloses, from the
// directives attached to its border. `GetNetclassesForDriver`
// concatenates the two sources the same way.
...ruleAreaNetclassAssignments(connDoc, libById, netlist),
])
: undefined,
[connDoc, libById, setup, netlist],
);
// `${VAR}` resolver for a document: project text variables (Schematic Setup
// > Text Variables) + the sheet's title block + sheet/file tokens, per
// PROJECT / TITLE_BLOCK / SCHEMATIC TextVarResolver.
const resolverForDoc = useCallback(
(d: Schematic, file: string, path = '/') => {
const ps = getPageSettings(d);
return schematicTextVarResolver({
textVars: Object.fromEntries(
setup.textVars.filter((v) => v.name).map((v) => [v.name, v.value]),
),
titleBlock: {
title: ps.title,
date: ps.date,
rev: ps.rev,
company: ps.company,
comments: ps.comments,
},
sheetName: path === '/' ? 'Root' : (path.split('/').filter(Boolean).pop() ?? 'Root'),
sheetPath: path,
fileName: file,
...(projectName ? { projectName } : {}),
});
},
[setup.textVars, projectName],
);
const resolveTextVar = useMemo(
() => (doc ? resolverForDoc(doc, currentFile, currentPath) : undefined),
[doc, resolverForDoc, currentFile, currentPath],
);
/** Library symbols of every sheet taking part, for unit counts. */
const hierarchyLibs = useCallback(
(sheets: readonly AnnotateSheet[]): Map<string, LibSymbol> => {
const libs = new Map(libById);
for (const s of sheets)
for (const l of s.doc.libSymbols) if (!libs.has(l.libId)) libs.set(l.libId, l);
return libs;
},
[libById],
);
/** Apply one sheet's new symbol list, on its own undo history when off-screen. */
/**
* Run an edit against any sheet of the project, not just the open one.
*
* The open sheet goes through the ordinary undo path so Ctrl+Z reaches it;
* another sheet gets its own history and is serialized straight into the
* `changed` list for the caller to persist. Extracted from
* `applySheetSymbols` when Sync Sheet Pins needed the same thing for pins and
* labels — the mechanism was never about symbols.
*/
const applySheetCommand = useCallback(
(file: string, cmd: EditCommand): void => {
if (file === currentFile || project.current.docs.has(file))
runProject(new Map([[file, cmd]]));
},
[currentFile, runProject],
);
/** The same, for an edit that replaces a whole sheet document. */
const applySheetDocument = useCallback(
(file: string, next: Schematic, label: string): void => {
const cmd: EditCommand = {
label,
apply: () => next,
invert: (before: Schematic) => ({
label,
apply: () => before,
invert: (b: Schematic) => ({ label, apply: () => b, invert: () => cmd }),
}),
};
applySheetCommand(file, cmd);
},
[applySheetCommand],
);
// Apply a queued remote sheet-text update (designer/src/sync/). Reuses
// applySheetDocument, the same primitive Increment Annotations/Sync Sheet
// Pins use to replace a whole sheet document — so this rides the ordinary
// undo path when it lands on the open sheet (a bad remote update is a
// Ctrl+Z away) and gets its own history entry otherwise, exactly like any
// other cross-sheet edit. Not a merge: last update to arrive wins.
useEffect(() => {
if (!pendingRemoteChange) return;
const { sheetPath } = pendingRemoteChange;
setPendingRemoteChange(null);
const target = sheetInstanceRefs.find((r) => r.path === sheetPath);
if (!target) return; // not (yet) part of this tab's loaded hierarchy
let next: Schematic;
if (pendingRemoteChange.patch) {
// A patch is only meaningful against the sheet it was diffed from, so
// it needs this tab's current copy of that sheet to splice into.
const current =
target.file === currentFile ? docRef.current : project.current.docs.get(target.file);
if (!current) return; // the sheet is named but not loaded here
next = applySchematicPatch(current, pendingRemoteChange.patch);
} else {
try {
next = { ...readSchematic(parse(pendingRemoteChange.text)), fileName: target.file };
} catch {
return; // malformed text mid-broadcast; wait for the next update
}
}
if (target.file === currentFile) {
// Echo suppression works off text either way, so a patch has to be
// serialized here — the alternative is re-broadcasting what we just
// received. Cheaper than it looks: this runs once per received edit,
// not once per frame, and only for the sheet on screen.
try {
lastKnownText.current = serializeSchematic(next);
} catch {
lastKnownText.current = null; // unserializable: fall back to sending text next time
}
}
applyingRemoteRef.current = true;
try {
applySheetDocument(target.file, next, 'Remote update');
} finally {
applyingRemoteRef.current = false;
}
}, [pendingRemoteChange, sheetInstanceRefs, currentFile, applySheetDocument]);
/**
* SCH_EDIT_TOOL's Edit with Symbol Editor. Hands the placement's symbol over
* in library form: the fields come back out of schematic space, and the unit
* and body style the editor opens on are the placement's.
*/
/**
* `SCH_ACTIONS::editLibSymbolWithLibEdit`. Both Edit Symbol and Edit Library
* Symbol run ONE handler upstream — `SCH_EDITOR_CONTROL::EditWithSymbolEditor`
* (sch_editor_control.cpp:2886, both actions bound at :3553) — and open the
* same SYMBOL_EDIT_FRAME. They differ only in what they seed it with:
*
* editWithLibEdit LoadSymbolFromSchematic( symbol )
* editLibSymbolWithLibEdit LoadSymbol( symbol->GetLibId(),
* symbol->GetUnit(),
* symbol->GetBodyStyle() )
*
* The first opens this placement's own copy, the second the LIBRARY part, so
* an edit there reaches every use of it. This one used to call
* `onShowSymbolEditor()` with no arguments at all, which opened an empty
* editor — the symbol was never seeded.
*/
const openSymbolEditorOn = useCallback(
(id: string, target: 'schematic' | 'library'): void => {
const d = docRef.current;
if (!d || !onEditSymbolInEditor) return;
// The placement on the live model, by its uuid: what SCH_EDITOR_CONTROL hands over.
const placed = d.symbols.find((sy, i) => refId('symbol', sy.uuid, i) === id);
const live =
placed?.uuid && syncLiveRef.current()
? schFrameRef.current!.Schematic().Hierarchy().ResolveItem(placed.uuid, null, true)
: null;
const symbol = live?.Type() === KICAD_T.SCH_SYMBOL_T ? (live as SCH_SYMBOL) : null;
if (!symbol || symbol.IsMissingLibSymbol()) {
// "Symbols with broken library symbol links cannot be edited." (sch_editor_control.cpp)
setInfoBar('Symbols with broken library symbol links cannot be edited.');
return;
}
if (target === 'schematic') onEditSymbolInEditor({ fromSchematic: symbol });
else
onEditSymbolInEditor({
library: {
libId: symbol.GetLibId().Format(),
unit: symbol.GetUnit(),
bodyStyle: symbol.GetBodyStyle(),
},
});
},
[onEditSymbolInEditor],
);
const editSymbolInEditor = useCallback(
(id: string): void => openSymbolEditorOn(id, 'schematic'),
[openSymbolEditorOn],
);
/**
* Tools > Rescue Symbols — `SCH_EDITOR_CONTROL::RescueSymbols`.
*
* Always the symbol-library-table rescuer: `LEGACY_RESCUER` is chosen only
* when `HasNoFullyDefinedLibIds()`, a KiCad 4 schematic whose symbols carry a
* bare name with no nickname, and nothing we can open is one.
*
* `aRunningOnDemand` is true here and always will be: the other caller is the
* prompt at load time, which sits inside the legacy `.sch` branch
* (`files-io.cpp:616-621`). That is what makes the "nothing to rescue"
* message appear at all — the automatic call passes false and stays silent.
*/
const [rescueCandidates, setRescueCandidates] = useState<readonly RescueCandidate[] | null>(null);
const [rescueMessage, setRescueMessage] = useState<string | null>(null);
/**
* `DisplayInfoMessage` after a load that `RepairPageNumbers` changed
* (files-io.cpp:451-458). One box, one string, raised by the loader alone.
*/
const [loadRepairedMessage, setLoadRepairedMessage] = useState<string | null>(null);
/** `aRunningOnDemand`: true from the Tools menu, false for the load prompt. */
const [rescueOnDemand, setRescueOnDemand] = useState(true);
/** Set by a legacy project load; the prompt runs once the sheet is on screen. */
const pendingRescuePrompt = useRef(false);
/**
* The project's `<project>-cache.lib`, read — `PROJECT_SCH::LegacySchLibs`
* (`@ziroeda/eeschema/project_sch.ts`'s `legacySchLibs`).
*
* `LoadAllLibraries` adds it whatever the schematic's format
* (`legacy_symbol_library.cpp:589-600`, "add the special cache library"), and
* the lazy load happens the first time anything asks — which for a modern
* project is the Project Rescue Helper and nothing else.
*
* A cache that will not parse leaves the map empty and the rescue runs
* without it, which is what upstream does with the `IO_ERROR`: `AddLibrary`
* throws, `LoadAllLibraries` catches it, logs "Symbol library '%s' failed to
* load." and carries on with the libraries it did get.
*/
const legacyCache = useCallback(
(): Map<string, LibSymbol> => legacySchLibs(rawFiles, project.current.root),
[rawFiles],
);
const runRescueSymbols = useCallback(
async (onDemand = true) => {
setRescueOnDemand(onDemand);
const docs = liveDocs();
const symbols = [...docs.values()].flatMap((d) => d.symbols);
// `SchGetLibSymbol( symbol_id, SymbolLibAdapter( … ) )`: the library the id
// names, never the sheet's own cached copy — the cache is the other half of
// the comparison, so it cannot also stand in for the library.
const libs = await repairSourceLibs(
symbols.map((sym) => sym.libId),
loadSymbol,
new Map(),
);
const found = findRescues(symbols, {
cache: legacyCache(),
lib: (id) => libs.get(id) ?? null,
schematicFileName: project.current.root,
});
if (found.length === 0) {
// "if( aRunningOnDemand )" — the automatic call says nothing when there
// is nothing to say, because the user did not ask for it.
if (onDemand) setRescueMessage('This project has nothing to rescue.');
return;
}
setRescueCandidates(found);
},
[liveDocs, legacyCache, loadSymbol],
);
/**
* The load-time prompt — `files-io.cpp:616-621`:
*
* if( ( !cfg || !cfg->m_RescueNeverShow ) && !cacheExists )
* editor->RescueSymbolLibTableProject( false );
*
* Deferred to an effect because it needs the sheet that was just opened, and
* `loadProject` puts that on screen with `setDoc`.
*/
useEffect(() => {
if (!pendingRescuePrompt.current || !doc) return;
pendingRescuePrompt.current = false;
void runRescueSymbols(false);
}, [doc, runRescueSymbols]);
/** "Instances of this symbol" — every placement of one id, over the hierarchy. */
const rescueInstances = useCallback(
(requestedId: string): RescueInstance[] => {
const out: RescueInstance[] = [];
for (const sheet of flatSheets) {
const d = sheet.file === currentFile ? doc : project.current.docs.get(sheet.file);
for (const sym of d?.symbols ?? []) {
if (sym.libId !== requestedId) continue;
out.push({
reference: sym.fields.find((f) => f.key === 'Reference')?.value ?? '',
value: sym.fields.find((f) => f.key === 'Value')?.value ?? '',
});
}
}
return out;
},
[flatSheets, currentFile, doc],
);
/**
* OK on the dialog — `DoRescues` then `WriteRescueLibrary`, and then
* `m_frame->ClearUndoRedoList()` (`sch_editor_control.cpp:582`).
*
* The undo list goes because the rescue has written a library file; undoing
* the schematic afterwards would leave it pointing at symbol ids the rescue
* library still defines, which is not a state the project was ever in.
*/
const applyRescues = useCallback(
(chosen: readonly RescueCandidate[]) => {
setRescueCandidates(null);
if (chosen.length === 0) {
// "No symbols were rescued." — upstream says this even on Cancel,
// because a mis-click there should not look like it did something.
setRescueMessage('No symbols were rescued.');
return;
}
// The library file itself. `OpenRescueLibrary` copies any existing rescue
// library in first so previous rescues are not lost.
const libFile = rescueLibraryFileName(project.current.root);
const nickname = rescueLibraryNickname(project.current.root);
const existing = rawFiles.find((f) => f.name === libFile);
const kept: LibSymbol[] = existing ? readSymbolLib(parse(existing.text)) : [];
const minted = chosen
.map(rescuedDefinition)
.filter((d): d is LibSymbol => d !== null)
.filter((d) => !kept.some((k) => k.libId === d.libId));
const text = serializeSymbolLib([...kept, ...minted]);
setRawFiles((prev) =>
prev.some((f) => f.name === libFile)
? prev.map((f) => (f.name === libFile ? { ...f, text } : f))
: [...prev, { name: libFile, text }],
);
onPersistFiles?.([{ name: libFile, text }]);
// "If the rescue library already exists in the symbol library table no
// need save it to add it to the table."
const rows = projectSymLibTable(rawFiles);
if (!rows.some((r) => r.name === nickname)) {
saveProjectSymLibTable([
...rows,
{
name: nickname,
type: 'KiCad',
// `wxS( "${KIPRJMOD}/" ) + fn.GetFullName()` — a path relative to
// the project, so the row survives the folder being moved.
uri: `\${KIPRJMOD}/${libFile}`,
options: '',
descr: '',
},
]);
}
// The schematic half: one edit over every sheet that places a rescued id.
const edit = new Map<string, EditCommand>();
for (const [file, d] of liveDocs()) {
if (!d.symbols.some((sym) => chosen.some((c) => c.requestedId === sym.libId))) continue;
edit.set(file, rescueDocumentCommand(chosen));
}
if (edit.size > 0) runProject(edit);
// Queued, not immediate: `runProject` folds inside a `setDoc` updater, so
// the entry it pushes does not exist yet.
pendingClearHistory.current = true;
},
[rawFiles, onPersistFiles, saveProjectSymLibTable, liveDocs, runProject],
);
/**
* Give a symbol being placed its reference, when KiCad would.
*
* `sch_drawing_tools.cpp`, after the symbol is added and inside the same
* commit:
*
* if( cfg->m_AnnotatePanel.automatic || newReference.AlwaysAnnotate() )
* refs.ReannotateByOptions( … );
*
* so the "Annotate Automatically" toggle is not the only gate:
* `SCH_REFERENCE::AlwaysAnnotate` is true for a power symbol or a reference
* beginning with '#', and those are numbered whatever the toggle says.
*
* The numbering itself goes through the same pass the Annotate dialog uses,
* rather than a second "find the next free number" of its own, so the sort
* order, the algorithm, the start number and the designator tracker all apply
* exactly as they do there. The symbol is annotated *before* it is placed:
* KiCad annotates after adding but within one COMMIT, and building it
* annotated is how that comes out as a single undo step here.
*/
const annotatePlacement = useCallback(
(sym: SchSymbol, lib: LibSymbol): SchSymbol => {
const d = docRef.current;
if (!d) return sym;
const reference = sym.fields.find((f) => f.key === 'Reference')?.value ?? '';
const alwaysAnnotate = lib.isPower === true || reference.startsWith('#');
if (!es.annotation.automatic && !alwaysAnnotate) return sym;
const tracker = new REFDES_TRACKER();
tracker.Deserialize(setup.usedDesignators);
tracker.SetReuseRefDes(setup.annotation.allowReuse);
// Annotate it in a document that already holds it, scoped to it alone, so
// every existing reference on the sheet is seen as taken.
const staged: Schematic = { ...d, symbols: [...d.symbols, sym] };
const libs = new Map(
hierarchyLibs([{ file: currentFile, doc: staged, sheetNumber: 1, scope: 'full' }]),
);
if (!libs.has(lib.libId)) libs.set(lib.libId, lib);
const index = staged.symbols.length - 1;
const only = new Set([refId('symbol', sym.uuid, index)]);
const annotated = annotateSymbols(
staged,
libs,
{
...defaultAnnotateOptions(),
scope: 'selection',
// The same project settings DIALOG_ANNOTATE seeds itself from.
order: setup.annotation.sortOrder,
algo:
setup.annotation.numbering === 'sheetX100'
? 'sheet_100'
: setup.annotation.numbering === 'sheetX1000'
? 'sheet_1000'
: 'incremental',
startNumber: setup.annotation.firstFreeAfter,
resetExisting: false,
// SYMBOL_FILTER_ALL: the placement path numbers power symbols too,
// which is how a freshly placed GND becomes #PWR01 rather than
// staying #PWR? and colliding with the next one.
includePower: true,
tracker,
},
only,
);
return annotated[index] ?? sym;
},
[setup, es.annotation.automatic, hierarchyLibs, currentFile],
);
const annotatePlacementRef = useRef(annotatePlacement);
annotatePlacementRef.current = annotatePlacement;
/** Every live screen, handed back to the window's records after a multi-sheet live edit. */
const adoptAllLiveScreens = useCallback((): void => {
const frame = schFrameRef.current;
if (!frame) return;
const all: SCH_SCREEN[] = [];
const screens = new SCH_SCREENS(frame.Schematic().Root());
for (let sc = screens.GetFirst(); sc; sc = screens.GetNext()) all.push(sc);
adoptLiveScreensRef.current(all);
}, []);
// DIALOG_ANNOTATE::OnAnnotateClick (dialog_annotate.cpp:245-264): one SCH_COMMIT over the
// live schematic, SCH_EDIT_FRAME::AnnotateSymbols reporting into the message panel, pushed as
// one undo step. The REFDES_TRACKER is the live SCHEMATIC_SETTINGS' (loaded from
// schematic.used_designators); the project settings still write the file, so its new state
// goes back there.
const runAnnotate = useCallback(
(opts: AnnotateRun) => {
const frame = schFrameRef.current;
if (!frame) return;
const commit = new SCH_COMMIT(frame);
const reporter = new Reporter();
frame.AnnotateSymbols(
commit,
ANNOTATE_SCOPE_OF[opts.scope],
ANNOTATE_ORDER_OF[opts.order],
ANNOTATE_ALGO_OF[opts.algo],
opts.recursive,
opts.startNumber,
opts.resetExisting,
opts.resetExisting && opts.regroupUnits === true,
true,
reporter,
SYMBOL_FILTER.SYMBOL_FILTER_NON_POWER,
);
commit.Push('Annotate');
adoptAllLiveScreens();
setAnnotateMessages([...reporter.lines]);
const usedDesignators = frame.Schematic().Settings().m_refDesTracker?.Serialize() ?? '';
if (usedDesignators !== setup.usedDesignators) commitSetup({ ...setup, usedDesignators });
},
[adoptAllLiveScreens, setup, commitSetup],
);
// DIALOG_ANNOTATE::OnClearAnnotationClick (:266-272): SCH_EDIT_FRAME::DeleteAnnotation.
const runClearAnnotation = useCallback(
(scope: AnnotateOptions['scope'], recursive: boolean) => {
const frame = schFrameRef.current;
if (!frame) return;
const reporter = new Reporter();
frame.DeleteAnnotation(ANNOTATE_SCOPE_OF[scope], recursive, reporter);
adoptAllLiveScreens();
setAnnotateMessages([...reporter.lines]);
},
[adoptAllLiveScreens],
);
// Drawing defaults shared by every output (screen, print, plot), derived
// from Schematic Setup > Formatting the way SCH_RENDER_SETTINGS is seeded
// from SCHEMATIC_SETTINGS upstream (eeschema_config.cpp).
/**
* `SetLayer( busLine ? LAYER_BUS_JUNCTION : LAYER_JUNCTION )`
* (`connection_graph.cpp:1451-1454`), for the junctions on this sheet.
*
* Cheap enough to keep current on every document change: one segment index
* over the buses, then one lookup per junction.
*/
const busJunctionIds = useMemo(() => (doc ? busJunctionIdsOf(doc) : new Set<string>()), [doc]);
const drawingDefaults = useMemo(() => LoadProjectSettings(setup), [setup]);
// Inter-sheet references (SCHEMATIC::RecomputeIntersheetRefs): resolved
// global-label text -> virtual pages across the hierarchy, plus each virtual
// page's page-number string, rebuilt when the hierarchy or settings change.
const intersheetRefsBase = useMemo(() => {
if (!setup.formatting.intersheetRefsShow) return undefined;
const docs = liveDocs();
const sheets: IntersheetSheet[] = [];
const virtualPageToPages = new Map<number, string>();
sheetInstanceRefs.forEach((s, i) => {
const sch = docs.get(s.file);
const page = pageNumberOf(s.path) || String(i + 1);
virtualPageToPages.set(i + 1, page);
if (sch) {
const resolver = resolverForDoc(sch, s.file, s.path);
sheets.push({
sch,
virtualPage: i + 1,
pageString: page,
resolve: (t) => ResolveShownText(t, resolver),
});
}
});
// No hierarchy yet (fresh document): the on-screen sheet is page 1.
if (sheets.length === 0 && doc) {
virtualPageToPages.set(1, pageNumberOf('/') || '1');
const resolver = resolverForDoc(doc, currentFile);
sheets.push({
sch: doc,
virtualPage: 1,
pageString: '1',
resolve: (t) => ResolveShownText(t, resolver),
});
}
return { pageRefsMap: buildPageRefsMap(sheets), virtualPageToPages };
}, [setup, liveDocs, sheetInstanceRefs, pageNumberOf, doc, currentFile, resolverForDoc]);
// ${INTERSHEET_REFS} resolver for the sheet shown as `currentVirtualPage`
// (SCH_GLOBALLABEL::ResolveTextVar reads CurrentSheet()'s virtual page).
const intersheetRefsFor = useCallback(
(currentVirtualPage: number): RenderOpts['intersheetRefs'] => {
if (!intersheetRefsBase) return undefined;
const cfg: IntersheetRefsConfig = {
pageRefsMap: intersheetRefsBase.pageRefsMap,
virtualPageToPages: intersheetRefsBase.virtualPageToPages,
currentVirtualPage,
listOwnPage: setup.formatting.intersheetRefsOwnPage,
formatShort: setup.formatting.intersheetRefsAbbreviated,
prefix: setup.formatting.intersheetRefsPrefix,
suffix: setup.formatting.intersheetRefsSuffix,
};
return { text: (resolvedLabel) => intersheetRefsText(resolvedLabel, cfg) };
},
[intersheetRefsBase, setup],
);
// The on-screen sheet's resolver (CurrentSheet().GetVirtualPageNumber()).
const intersheetRefs = useMemo(() => {
const idx = sheetInstanceRefs.findIndex((s) => s.path === currentPath);
return intersheetRefsFor(idx === -1 ? 1 : idx + 1);
}, [intersheetRefsFor, sheetInstanceRefs, currentPath]);
// Print (DIALOG_PRINT::TransferDataFromWindow): SavePrintOptions has run in the dialog, so
// SCH_PRINTOUT reads them from eeconfig(); wxPrinter draws its pages into the browser's print
// window on the paper TransferDataToWindow takes from the current screen's page settings.
const doPrint = useCallback(() => {
const frame = schFrameRef.current;
const pageInfo = frame?.GetScreen()?.GetPageSettings();
if (!frame || !pageInfo) return;
new wxPrinter().Print(new SCH_PRINTOUT(frame, 'Print Schematic'), {
x: pageInfo.GetWidthMils() / 1000,
y: pageInfo.GetHeightMils() / 1000,
});
setPrintOpen(false);
}, []);
// Plot (DIALOG_PLOT_SCHEMATIC / SCH_PLOTTER::Plot): write the chosen format
// into the project's output directory. "Plot All Pages" (the upstream OK
// button) plots every sheet file, "Plot Current Page" (wxID_APPLY) just this
// one. Each written file is reported to the dialog's Output Messages panel
// the way SCH_PLOTTER reports "Plotted to '<path>'.".
const doPlot = useCallback(
({
format,
opts,
allPages,
themeId,
outputDir,
pdfMetadata,
pdfPropertyPopups,
pdfHierarchicalLinks,
openAfter,
report,
}: PlotRequest) => {
const plotTheme = themeId && BUILTIN_THEMES[themeId] ? BUILTIN_THEMES[themeId]!.theme : theme;
const o: PlotOpts = {
...opts,
...drawingDefaults,
...(activeSheet ? { sheet: activeSheet } : {}),
pdfPropertyPopups,
pdfHierarchicalLinks,
};
// "Open file after plot": open the tab now, in the click gesture, so the
// browser doesn't block it, the sink navigates it once the file (which
// for PNG/PDF is produced asynchronously) is ready. Single page only.
const preview = openAfter && !allPages ? window.open('', '_blank') : null;
// Netclass visuals, text variables and intersheet refs all resolve per
// sheet, so the options are built per sheet even when the pages end up in
// one document.
const optsFor = (d: Schematic, name: string, file: string): PlotOpts => {
const nov = computeNetClassOverrides(
d,
new Map(d.libSymbols.map((l) => [l.libId, l])),
setup,
);
const resolve = resolverForDoc(d, name);
// The PDF popups' "Net" / "Resolved netclass" lines and a bus's
// members, from this sheet's connectivity (SCH_CONNECTION::Name,
// GetEffectiveNetClass()->GetHumanReadableName()). Computed lazily:
// the other formats never ask.
const docLib = new Map(d.libSymbols.map((l) => [l.libId, l]));
let nl: Netlist | null = null;
const netOf = (id: string): PdfNetInfo | undefined => {
nl ??= computeNetlist(d, docLib);
const code = nl.netByItem.get(id);
const net = code !== undefined ? nl.nets.find((n) => n.code === code) : undefined;
if (!net) return undefined;
const members = nl.buses.find((b) => b.items.includes(id))?.members;
return {
net: net.name,
netclass: netClassHumanReadableName(
resolveEffectiveNetClass(net.name, setup.netClasses),
),
...(members ? { members } : {}),
// SCH_LINE::GetPenWidth: the netclass width, else the stroke's,
// else the default wire width the plot renderer strokes with.
penWidth: nov?.lines.get(id)?.widthIU ?? DEFAULT_WIRE_WIDTH,
};
};
const od: PlotOpts = {
...o,
...(nov ? { netOverrides: nov } : {}),
resolveTextVar: resolve,
netOf,
libById: docLib,
...((): Partial<PlotOpts> => {
const idx = sheetInstanceRefs.findIndex((s) => s.file === file);
const r = intersheetRefsFor(idx === -1 ? 1 : idx + 1);
return r ? { intersheetRefs: r } : {};
})(),
// "Generate metadata from AUTHOR & SUBJECT variables": the same text
// variables SCH_PLOTTER resolves before writing the PDF.
...(pdfMetadata
? {
pdfMetadata: {
title: d.titleBlock?.title || name,
// `m_schematic->ResolveTextVar( &sheet, &msg, 0 )` with the bare token.
author: resolveToken(resolve, 'AUTHOR'),
subject: resolveToken(resolve, 'SUBJECT'),
},
}
: {}),
};
return od;
};
// Every plot is downloaded through the App's onOutputFile (a run of several as one zip),
// and "Open file after plot" navigates the pre-opened preview tab to it.
const makeSink = (): PlotSink => {
return (blob, filename) => {
const path = outputDir ? `${outputDir}/${filename}` : filename;
if (onOutputFile) {
void blob.arrayBuffer().then((buf) => {
onOutputFile(path, new Uint8Array(buf), blob.type);
report(`Plotted to '${path}'.`, RPT_SEVERITY_ACTION);
});
} else {
downloadBlob(blob, filename);
report(`Plotted to '${filename}'.`, RPT_SEVERITY_ACTION);
}
if (preview) {
// Browsers render PDF/SVG/PNG inline but can't display PostScript or
// DXF, those are text, so re-wrap them as text/plain to show the
// file content in the tab instead of triggering a download.
const viewable = blob.type === 'application/pdf' || blob.type.startsWith('image/');
const shown = viewable ? blob : new Blob([blob], { type: 'text/plain' });
preview.location.href = URL.createObjectURL(shown);
}
};
};
const one = (d: Schematic, name: string, file: string): void => {
const od = optsFor(d, name, file);
const sink = makeSink();
// The async back-ends are fired and forgotten, so a failure had nowhere
// to go: the dialog has a report panel and it stayed empty. A raster
// plot of a big sheet is the realistic case — the canvas has a size
// limit and exceeding it throws.
const failed = (e: unknown): void =>
report(`Plot failed: ${e instanceof Error ? e.message : String(e)}`, RPT_SEVERITY_ERROR);
if (format === 'svg') plotSvg(d, plotTheme, od, name, sink);
else if (format === 'png') void plotPng(d, plotTheme, od, name, sink).catch(failed);
else if (format === 'dxf') plotDxf(d, plotTheme, od, name, sink);
else if (format === 'ps') plotPs(d, plotTheme, od, name, sink);
else void plotPdf(d, plotTheme, od, name, sink).catch(failed);
};
const run = (): void => {
if (allPages) {
const sheets = [...liveDocs()];
// SCH_PLOTTER::Plot: nothing to write is an error, not a silent no-op.
if (sheets.length === 0) report('No sheets to plot.', RPT_SEVERITY_ERROR);
else if (format === 'pdf') {
// createPDFFile opens one file and pages through the SHEET LIST —
// one page per sheet instance, in hierarchy order, each with its
// own page number and sheet name (StartPlot / StartPage's
// arguments) and its parent's, so the document's outline nests
// the way the hierarchy does. Every other format has no page
// after the first, which is why only this one is gathered.
const refs = sheetInstanceRefs;
const docs = liveDocs();
const pageOf = (path: string): string =>
pageNumberOf(path) || String(refs.findIndex((r) => r.path === path) + 1);
const pages: PdfPlotSheet[] = refs.flatMap((s, i) => {
const d = docs.get(s.file);
if (!d) return [];
const parentPath = s.path === '/' ? null : s.path.replace(/[^/]+\/$/, '');
const parent = refs.find((r) => r.path === parentPath);
const r = intersheetRefsFor(i + 1);
// `findSelf().GetPageNumber()` for each sheet symbol on this page:
// the instance at this path plus the sheet's uuid.
const childPages = new Map(
d.sheets.flatMap((sh) =>
sh.uuid ? [[sh.uuid, pageOf(`${s.path}${sh.uuid}/`)] as const] : [],
),
);
return [
{
sch: d,
childPages,
opts: {
...optsFor(d, s.file.replace(/\.kicad_sch$/i, '') || outputBaseName(), s.file),
pageNumber: pageOf(s.path),
sheetNumber: i + 1,
sheetCount: refs.length,
...(s.path !== '/' ? { sheetName: s.name } : {}),
sheetPath: s.namePath,
...(r ? { intersheetRefs: r } : {}),
},
...(parent
? {
parent: {
pageNumber: pageOf(parent.path),
sheetName: parent.path === '/' ? '' : parent.name,
},
}
: {}),
},
];
});
void plotPdfSheets(
pages.length > 0
? pages
: sheets.map(([file, d]) => ({
sch: d,
opts: optsFor(d, file.replace(/\.kicad_sch$/i, '') || outputBaseName(), file),
})),
plotTheme,
outputBaseName(),
makeSink(),
).catch((e) =>
report(
`Plot failed: ${e instanceof Error ? e.message : String(e)}`,
RPT_SEVERITY_ERROR,
),
);
} else {
for (const [file, d] of sheets)
one(d, file.replace(/\.kicad_sch$/i, '') || outputBaseName(), file);
}
} else if (doc) one(doc, outputBaseName(), currentFile);
else report('No sheets to plot.', RPT_SEVERITY_ERROR);
};
// A faced text plots from the same glyphs it is drawn with, which
// means its face has to be here first: a sheet never shown on screen
// has not asked for its fonts yet.
void loadOutlineFontsFor(allPages ? [...liveDocs().values()] : doc ? [doc] : []).then(run);
// The dialog stays open after plotting (like DIALOG_PLOT_SCHEMATIC) so the
// Output Messages panel is visible; only the Close button dismisses it.
},
[
doc,
theme,
outputBaseName,
liveDocs,
activeSheet,
drawingDefaults,
setup,
resolverForDoc,
intersheetRefsFor,
sheetInstanceRefs,
pageNumberOf,
currentFile,
onOutputFile,
],
);
// Load a schematic from raw .kicad_sch text: parse (lossless), fresh history,
// clear transient state, and fit the view. Embedded lib_symbols render as-is.
const resetTransient = useCallback(() => {
setSelection(new Set());
setPendingLabel(null);
setActiveTool('select');
setPlaceLib(null);
setPastePending(null);
}, []);
/**
* Drop the ERC run. Deliberately *not* part of `resetTransient`: a run spans
* the whole hierarchy and its markers live on the sheet each fault belongs to
* (upstream keeps them on that sheet's SCH_SCREEN), so entering another sheet
* must leave the list alone — DIALOG_ERC is modeless and navigating to a
* marker on another sheet is the *point* of clicking its row. Only loading a
* different schematic or project invalidates the run.
*/
const resetErc = useCallback(() => {
// The outgoing project's ERC dialog goes with it (SCH_INSPECTION_TOOL::Reset).
schFrameRef.current?.CloseErcDialog();
}, []);
const loadText = useCallback(
async (text: string, name?: string) => {
setLoading(`Loading ${name ?? 'untitled.kicad_sch'}...`);
await nextPaint();
try {
const next = { ...readSchematic(parse(text)), fileName: name ?? 'untitled.kicad_sch' };
const file = name ?? 'untitled.kicad_sch';
project.current = { docs: new Map([[file, next]]), root: file };
history.current.clear();
setCurrentFile(file);
setCurrentPath('/');
navTool.current.resetHistory('/');
setDoc(next);
resetTransient();
resetErc();
if (name) setFileName(name);
setError(null);
// `SCH_EDIT_FRAME::OpenProjectFiles`' trailing CallAfter (files-io.cpp:857-864):
// the libraries are paid for now, in the background, so nothing waits
// for them later. See ./preload.ts.
preloadSchematicLibraries([next]);
} catch (e) {
setError(e instanceof Error ? e.message : String(e));
} finally {
setLoading(null);
}
},
[resetTransient, resetErc, preloadSchematicLibraries],
);
// Open a whole KiCad project: parse every .kicad_sch, find the root (the
// .kicad_pro's schematic, else the sheet nothing references), and show it.
const loadProject = useCallback(
async (files: PickedFile[], startFile?: string) => {
// The reporter comes up before anything is read; its message is empty
// (an 80-space reservation) until the loader's first Report().
setLoading({ message: '' });
await nextPaint(); // paint the dialog before the (synchronous) sheet parse
try {
const docs = new Map<string, Schematic>();
const problems: string[] = [];
let proName: string | undefined;
// When a folder bundles several projects, the launcher pins the active
// one via rootPro; load that project's .kicad_pro so the editor's root
// sheet matches the tree instead of guessing the first pro found.
const wantPro = rootPro ? `${rootPro}.kicad_pro`.toLowerCase() : null;
// Parse sheet by sheet with a per-sheet gauge (KiCad's "Loading
// Schematic" progress dialog), yielding a paint between sheets so the
// bar advances even though each parse is synchronous.
const sheets = files.filter((f) =>
/\.kicad_sch$/i.test(f.name.split('/').pop()!.split('\\').pop()!),
);
let parsed = 0;
for (const f of files) {
const base = f.name.replace(/\\/g, '/');
if (/\.kicad_pro$/i.test(base)) {
// Prefer the active project's .kicad_pro (rootPro) so the editor and
// the launcher tree open the same root sheet. Absent that, fall back
// to the FIRST .kicad_pro (matching projectNameOf and the tree root);
// picking a later one would open a different root than the tree shows
// and the two would then edit different sheets and diverge.
if (wantPro && base.toLowerCase() === wantPro) proName = base;
else proName ??= base;
continue;
}
if (!/\.kicad_sch$/i.test(base)) continue;
// `Loading %s...` (sch_io_kicad_sexpr.cpp:324) per sheet; the gauge
// is the sheet count, one phase per file as KiCad's loader adds them.
setLoading({ message: `Loading ${base}...`, value: parsed / sheets.length });
// Unconditional: `Report()` is followed by `KeepRefreshing()` before
// the read (sch_io_kicad_sexpr.cpp:324-327), so the name is on screen
// while the sheet parses — for one sheet as much as for twelve. Skipping
// the yield for a single sheet batched the message with the close, and
// the dialog showed its blank reservation for the whole read.
await nextPaint();
try {
docs.set(base, { ...readSchematic(parse(f.text)), fileName: base });
} catch (e) {
problems.push(`${base}: ${e instanceof Error ? e.message : String(e)}`);
}
parsed++;
}
/**
* A KiCad 4/5 project, converted on the way in — `SCH_IO_MGR::SCH_LEGACY`.
*
* Only when the selection holds no `.kicad_sch` at all: a folder with
* both is one that has already been upgraded, and the old files beside
* it are what KiCad leaves behind rather than what it opens.
*/
let legacy = false;
if (docs.size === 0) {
const sch = new Map<string, string>();
const libs = new Map<string, string>();
for (const f of files) {
const base = f.name.split('/').pop()!.split('\\').pop()!;
if (/\.sch$/i.test(base)) sch.set(base, f.text);
else if (/\.lib$/i.test(base)) libs.set(base, f.text);
}
const rootSch = legacyRootFile(sch, proName?.replace(/\.kicad_pro$/i, ''));
if (rootSch) {
legacy = true;
setLoading(`Loading ${rootSch}...`);
await nextPaint();
const converted = readLegacyProject({
files: sch,
rootFile: rootSch,
projectName: (proName ?? rootSch).replace(/\.[^.]*$/, ''),
// `UpdateSymbolLinks` fills a screen's `lib_symbols` from the
// resolved library, and it is that which a `.kicad_sch` carries.
libSymbols: legacyLibrarySymbols(libs, readLegacySymbolLibrary),
});
for (const [name, d] of converted.docs) docs.set(name, d);
problems.push(...converted.problems);
// The one thing `never_show_rescue_dialog` gates:
//
// if( ( !cfg || !cfg->m_RescueNeverShow ) && !cacheExists )
// editor->RescueSymbolLibTableProject( false );
// (files-io.cpp:616-621)
//
// A project whose cache library is still there needs no prompt —
// the symbols it was drawn with are all present.
const cacheNames = legacyCacheFileNames(proName ?? rootSch);
const cacheExists = files.some((f) =>
cacheNames.includes(f.name.replace(/\\/g, '/').split('/').pop() ?? ''),
);
if (!app.settings.eeschema.system.never_show_rescue_dialog && !cacheExists) {
pendingRescuePrompt.current = true;
}
}
}
if (docs.size === 0) {
setError(problems[0] ?? 'No .kicad_sch files in the selection');
return;
}
const root = findRootFile(docs, proName);
// Page numbers are made whole before anything reads the hierarchy
// (files-io.cpp:439-446): a blank or duplicated page is reassigned
// and, when that happened, the user is told the file needs saving.
const pageRepair = repairPageNumbersOnLoad(docs, root);
for (const [name, d] of pageRepair.docs) docs.set(name, d);
if (pageRepair.repaired) setLoadRepairedMessage(LOAD_REPAIRED_MESSAGE);
project.current = { docs, root };
// Home-page tree clicks land on the clicked sheet, else the root.
const startBase = startFile?.replace(/\\/g, '/');
const start = startBase && docs.has(startBase) ? startBase : root;
const wantRoot = proName?.replace(/\.kicad_pro$/i, '.kicad_sch');
if (wantRoot && !docs.has(wantRoot) && start === root)
problems.push(
`root schematic ${wantRoot} is not in the selection, opened ${root} instead`,
);
history.current.clear();
setCurrentFile(start);
// A sub-sheet opened from the project tree: the root opens and MAIL_SCH_NAVIGATE_TO_SHEET
// changes to the first instance of that file (project_tree_item.cpp:274-299), so the
// sheet path is that instance's, not the root's - the hierarchy highlights the sheet shown.
const startPath =
start === root ? '/' : (firstInstanceOf(buildSheetTree(docs, root), start) ?? '/');
setCurrentPath(startPath);
// The root opened first and changeSheet pushed the sheet: Back returns to the root.
navTool.current.resetHistory('/');
navTool.current.pushToHistory(startPath);
setDoc(docs.get(start)!);
resetTransient();
resetErc();
setFileName(start);
setError(problems.length ? `Some sheets failed to load: ${problems.join('; ')}` : null);
// The other CallAfter, `SCH_EDIT_FRAME::LoadProject`
// (sch_edit_frame.cpp:1492-1499). Every sheet, not just the one being
// shown: `PreloadLibraries` is hierarchy-wide because the library table
// is, and entering a sub-sheet must not start a fresh wait.
preloadSchematicLibraries(docs.values());
} catch (e) {
// Each *sheet* is already caught individually and reported through
// `problems`. This is everything around them — reading the .kicad_pro,
// picking the root, building the hierarchy — where a throw escaped an
// async handler: the overlay cleared, no project loaded, and the error
// bar stayed empty, so opening a project appeared to do nothing at all.
setError(`Could not open this project: ${e instanceof Error ? e.message : String(e)}`);
} finally {
setLoading(null);
}
},
[resetTransient, resetErc, rootPro, app, preloadSchematicLibraries],
);
/**
* A project handed over from the home page's Open Project picker.
*
* Keyed on `openNonce` — the host telling us it OPENED something — and not on
* the identity of the `initialProject` array. That array is now live: the
* host mirrors our own autosaved sheets back into it so a reopen, or a
* remount, sees the session's work instead of the file it started from. When
* the reload was keyed on identity, every unrelated change to it re-ran
* `loadProject` and reverted the canvas to the opened file — a plot output
* file, a Ctrl+S in the board editor, a reopen from the tree — and the 900 ms
* autosave below then wrote that revert over the good copy in storage.
*
* `loadProject` throws away `project.current`, the undo histories and the
* view, so it must run when a project is opened and at no other time. That is
* KiCad's own shape: `OpenProjectFiles` is called by an action.
*/
const openedKey = useRef<string | null>(null);
const projectRef = useRef(initialProject);
projectRef.current = initialProject;
/**
* The project's NON-SHEET content: the `.kicad_pro`, the library tables, the
* drawing sheet. What `rawFiles` and Schematic Setup are read from.
*
* Keyed on content rather than on the array, which now changes identity every
* time autosave mirrors a sheet back. Re-deriving Setup from the prop on each
* of those ticks would undo a Schematic Setup the user had just changed: the
* dialog persists straight to storage and does not refresh the prop, so what
* came back would be the `.kicad_pro` as it was opened.
*/
const projectMetaKey = useMemo(
() =>
(initialProject ?? [])
.filter((f) => !/\.kicad_sch$/i.test(f.name))
.map((f) => `${f.name} ${f.text}`)
.join(''),
[initialProject],
);
useEffect(() => {
const files = projectRef.current;
// rootPro is part of the key so switching the active project (same folder,
// different .kicad_pro) reloads with the newly-pinned root sheet.
const key = `${openNonce ?? 0} ${rootPro ?? ''}`;
// Only a frame on screen opens; a hidden one takes the open when shown.
if (shown && openedKey.current !== key) {
const first = openedKey.current === null;
openedKey.current = key;
if (files && files.length > 0) void loadProject(files, initialFile ?? undefined);
// An open with no project is a NEW schematic (`is_new` in files-io.cpp:
// "Create Schematic"). The frame outlives the manager now, so without
// this the launcher pressed with no project open would show whatever
// project this frame held last. The very first open needs nothing: the
// frame was born on the blank sheet.
else if (!first) void loadText(EMPTY_SCH);
// Drop any in-session sheet override for the freshly opened project.
setSheetOverride(null);
}
// Reseed the raw files (drawing-sheet reference + .kicad_wks choices) and
// hydrate the Schematic Setup from the project's .kicad_pro (SCHEMATIC/ERC/
// NET_SETTINGS live in the project file, like KiCad's project load). Both
// read the project rather than the document, so they follow the prop.
setRawFiles(files ?? []);
setSetup(readSchematicSetup(files ?? [], rootPro ?? undefined));
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [projectMetaKey, rootPro, openNonce, shown]);
const snapshotHost = useRef(projectSnapshotHost()).current;
useProjectSnapshot(
'schematic-safety',
{ flush: () => {}, isBusy: () => !(schFrameRef.current?.ToolStackIsEmpty() ?? true) },
unsaved ? doc : undefined,
);
// Serialize the project's sheets (current sheet + resident others) for autosave.
const serializeSheets = useCallback((): PickedFile[] => {
if (!doc) return [];
const docs = new Map(project.current.docs);
docs.set(currentFile, doc);
const files: PickedFile[] = [];
for (const [file, d] of docs) {
try {
files.push({ name: file, text: serializeSchematic(d) });
} catch (error) {
if (snapshotHost) throw error;
/* skip a bad sheet */
}
}
return files;
}, [doc, currentFile, snapshotHost]);
// Autosave: once edits settle, hand the sheets up (App debounces the write to
// IndexedDB). Fires on sheet switch/load too, re-saving identical content.
useEffect(() => {
if (!doc || !onProjectChange) return;
const t = setTimeout(() => {
const files = serializeSheets();
if (files.length) onProjectChange(files);
}, 900);
return () => clearTimeout(t);
}, [doc, onProjectChange, serializeSheets]);
// Register a flush so the host can force the pending autosave out before the
// project is reopened (the "edit → home → reopen" case).
useEffect(() => {
if (!registerAutosaveFlush) return;
registerAutosaveFlush(() => {
const files = serializeSheets();
if (files.length) onProjectChange?.(files);
if (snapshotHost) setUnsaved(false);
});
return () => registerAutosaveFlush(null);
}, [registerAutosaveFlush, onProjectChange, serializeSheets, snapshotHost]);
// Switch the visible sheet (KiCad's Enter Sheet / hierarchy navigation): stash
// the edited current sheet back into the project, swap in the target document
// and its own undo history.
const switchSheet = useCallback(
(path: string, file: string, pushHistory = true) => {
// Every sheet change lands in the Back/Forward history (changeSheet →
// pushToHistory); Back/Forward themselves move the cursor instead.
if (pushHistory) navTool.current.pushToHistory(path);
// Always record which instance is active (path is unique per instance).
setCurrentPath(path);
// Two instances of the same file share one document, nothing to swap, just
// the active path changed.
if (!doc || file === currentFile) return;
const proj = project.current;
proj.docs.set(currentFile, doc);
const target = proj.docs.get(file);
if (!target) {
setError(`Sheet file not in project: ${file}`);
return;
}
// The undo stack is NOT swapped: there is one for the whole project, and
// stepping into a sheet does not change what Ctrl+Z takes back.
setCurrentFile(file);
setDoc(target);
resetTransient();
},
[doc, currentFile, resetTransient],
);
/**
* The page-settings undo's navigation, run after the step has been folded in.
*
* SetCurrentSheet( undoSheet );
* DisplayCurrentSheet();
* (`schematic_undo_redo.cpp:357-358`)
*
* Only that one status asks for it; see `EditCommand.pageSettings`. If the
* sheet has since been removed from the hierarchy there is nothing to show,
* and the restored settings simply stay where they landed.
*/
useEffect(() => {
const file = pendingShowSheet.current;
if (!file) return;
pendingShowSheet.current = null;
const target = flatSheets.find((sh) => sh.file === file);
if (target) switchSheet(target.path, file);
});
const openFile = useCallback(
(file: File) => {
if (!/\.kicad_sch$/i.test(file.name)) {
setError(`Not a .kicad_sch file: ${file.name}`);
return;
}
file
.text()
.then((t) => void loadText(t, file.name))
.catch((e) => setError(String(e)));
},
[loadText],
);
/**
* `SCH_EDIT_FRAME::OnOpenSchematic` - a `wxFileDialog` on the project
* directory filtered by `FILEEXT::KiCadSchematicFileWildcard`, not the
* operating system's file manager. Ours clicked a hidden
* `<input type="file">`, which cannot see the account's projects at all, so
* a schematic saved to the cloud could not be re-opened from inside the
* editor.
*/
const [openDlgOpen, setOpenDlgOpen] = useState(false);
const promptOpen = useCallback(() => {
if (snapshotHost) snapshotHost.command?.('open');
else setOpenDlgOpen(true);
}, [snapshotHost]);
// Doc edits mark the title dirty; the flag clears after the app's coalesced
// autosave window (1.2 s) has taken the change. Mount / file switches skip.
useEffect(() => {
dirtySkipRef.current = true;
setUnsaved(false);
}, [currentFile]);
// Only when nothing is writing the work down. With a project open, autosave
// plus the flush on page-hide already carry it, and a prompt would be noise
// on every close.
useUnsavedGuard(!autosaveActive && unsaved);
useEffect(() => {
if (dirtySkipRef.current) {
dirtySkipRef.current = false;
return;
}
setDirty(true);
setUnsaved(true);
const id = setTimeout(() => setDirty(false), 1600);
return () => clearTimeout(id);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [doc]);
const save = useCallback(async () => {
// Not inside a setDoc updater. A state updater must be pure — StrictMode
// runs it twice, which here meant persisting twice or firing two downloads
// — and it must not be where the work is decided: the document is read from
// its ref instead.
const d = docRef.current;
if (!d) return;
let text: string;
try {
text = serializeSchematic(d);
} catch (e) {
// The flags stay set. Clearing them first told the user their work was
// safe *because* we were about to write it, which is exactly backwards
// when the write is the thing that failed.
setInfoBar(`Could not save: ${e instanceof Error ? e.message : String(e)}`);
return;
}
if (onSaveFiles && currentFile !== DEFAULT_FILE) {
// Save writes into the project's file manager (cloud storage); a local
// copy can be downloaded from there (or via Save a Copy). This is the
// path that also commits the Local History point.
try {
await onSaveFiles([{ name: currentFile, text }]);
} catch (error) {
setInfoBar(`Could not save: ${error instanceof Error ? error.message : String(error)}`);
return;
}
} else if (onPersistFiles && currentFile !== DEFAULT_FILE) {
onPersistFiles([{ name: currentFile, text }]);
} else {
const url = URL.createObjectURL(new Blob([text], { type: 'application/octet-stream' }));
const a = document.createElement('a');
a.href = url;
a.download =
currentFile !== DEFAULT_FILE
? currentFile
: (fileName ?? `${d.titleBlock?.title ?? 'schematic'}.kicad_sch`);
a.click();
URL.revokeObjectURL(url);
}
// Only now: the asterisk and the leave-prompt both mean "written".
setDirty(false);
setUnsaved(false);
}, [fileName, currentFile, onPersistFiles, onSaveFiles]);
// MAIL_SCH_GET_NETLIST is answered by the frame from its live SCHEMATIC (ReadyToNetlist,
// NETLIST_EXPORTER_KICAD); this keeps that schematic the window's: every sheet's record, the
// open one with the edits its file only gets on the debounced save, opened through
// OpenProjectFiles under the folder pcbnew loads the project from.
// (TRANSITIONAL, W3a): the open project's files on the mounted file system, where
// KiCad would find them (`/<project>/…`), so the live tools that read or write files - Import
// Sheet, Place Image, Export Symbols, Rescue, ChangeSheetFile - see the project. A write lands
// in the project's file store; the explicit Save stays the window's until the switch.
useEffect(() => {
const name = projectName ?? project.current.root.replace(/\.[^.]*$/, '');
if (!name) return;
const enc = new TextEncoder();
const dec = new TextDecoder();
const fs = new MEMORY_FILESYSTEM();
for (const f of rawFiles) fs.Write(f.name, f.bytes ?? enc.encode(f.text));
fs.SetWriteListener((aRel, aData) => {
const text = dec.decode(aData);
if (snapshotHost) snapshotHost.writeFile(aRel, aData);
else onPersistFiles?.([{ name: aRel, text, bytes: aData }]);
// (TRANSITIONAL, with the mirror) the live project wrote its project file
// (SETTINGS_MANAGER::SaveProject): the window's copy and the Schematic Setup read from it
// follow the file, the one thing both models read, so neither writes over the other.
if (/\.kicad_pro$/i.test(aRel)) {
liveMirrorRef.current?.NoteProjectSaved(text);
const next = liveFilesRef.current.rawFiles.map((f) =>
f.name === aRel ? { ...f, text } : f,
);
setRawFiles(next);
setSetup(readSchematicSetup(next, rootPro ?? undefined));
}
});
const unmount = wxMountFileSystem(`/${name}`, fs);
// LIBRARY_MANAGER::LoadProjectTables for the design block table, which the pane lists: the
// project's design-block-lib-table is on the mount now.
Pgm().GetLibraryManager().LoadProjectTables(`/${name}`, [LIBRARY_TABLE_TYPE.DESIGN_BLOCK]);
// PGM_BASE::PreloadDesignBlockLibraries, as a project opens: every row loaded, so the pane
// lists its blocks and a save into one finds it (SaveDesignBlock reads loaded libraries only).
schFrameRef.current?.Prj().DesignBlockLibs().AsyncLoad();
designBlocksPane.RefreshLibs();
return unmount;
}, [rawFiles, projectName, onPersistFiles, rootPro, designBlocksPane, snapshotHost]);
const liveMirrorRef = useRef<LIVE_SCHEMATIC_MIRROR | null>(null);
const inEditLiveRef = useRef(false);
const adoptLiveScreensRef = useRef<(aScreens: Iterable<SCH_SCREEN>) => void>(() => {});
const liveModifiedRef = useRef<() => void>(() => {});
const liveSheetChangedRef = useRef<() => void>(() => {});
const showingLiveRef = useRef(false);
liveFilesRef.current = { rawFiles, projectName, rootPro };
syncLiveRef.current = () => {
liveMirrorRef.current ??= new LIVE_SCHEMATIC_MIRROR(
schFrameRef.current!,
() => {
const docs = new Map(project.current.docs);
if (docRef.current) docs.set(currentFileRef.current, docRef.current);
if (docs.size === 0) return null;
const { rawFiles: files, projectName: name } = liveFilesRef.current;
// A window not yet handed a project (the idle warm-up) opens nothing. Upstream's frame
// starts on an empty SCHEMATIC under the current project (SCH_EDIT_FRAME's ctor); opening
// the placeholder would load "/untitled.kicad_pro" over the project, and its
// LIBRARY_MANAGER::ProjectChanged drops every library the other editors had loaded.
if (!name && files.length === 0) return null;
const root = project.current.root;
return {
dir: `/${name ?? root.replace(/\.[^.]*$/, '')}`,
docs,
root,
files: files
.filter((f) => /\.kicad_(pro|prl)$/i.test(f.name))
.map((f) => ({ name: f.name, text: f.text })),
};
},
() => {
ercDialogRef.current?.dialog.OnSchematicReplaced();
fieldsTableRef.current?.dialog.OnSchematicReplaced();
// A new schematic is a load: files-io.cpp's RefreshNetNavigator() after it.
schFrameRef.current?.RefreshNetNavigator();
},
);
return liveMirrorRef.current.get() !== null;
};
// The script API's ERC: ERC_TESTER on the window's records loaded on their own, as kicad-cli
// loads a schematic, so the editor's screens get no markers.
const ercLinesRef = useRef<() => SchErcLine[]>(() => []);
ercLinesRef.current = () => {
const frame = schFrameRef.current;
const docs = new Map(project.current.docs);
if (docRef.current) docs.set(currentFileRef.current, docRef.current);
if (!frame || docs.size === 0 || !syncLiveRef.current()) return [];
const { rawFiles: files, projectName: name } = liveFilesRef.current;
const root = project.current.root;
const sch = loadRecordsDetached(
{
dir: `/${name ?? root.replace(/\.[^.]*$/, '')}`,
docs,
root,
files: files.filter((f) => /\.kicad_(pro|prl)$/i.test(f.name)),
},
frame.Prj(),
);
return sch ? schErcLines(sch, app.settings.eeschema.erc_dialog.show_all_errors) : [];
};
// DIALOG_ERC's window: what wx gives the dialog - its sub-dialogs, the report file, its
// showing - and the libraries ERC_TESTER reads, loaded before the run that cannot wait.
ercDialogFactoryRef.current = () => {
const frame = schFrameRef.current;
if (!frame) return null;
let dialog: DIALOG_ERC;
const window: DIALOG_ERC_WINDOW = {
// WX_TEXT_ENTRY_DIALOG( this, wxEmptyString, _( "Exclusion Comment" ), aInitial, true )
textEntry: (initial) =>
new Promise<string | null>((resolve) => setErcComment({ initial, resolve })),
askDeleteExclusions: () =>
new Promise<'yes' | 'no' | 'cancel'>((resolve) => setErcYesNoCancel({ resolve })),
saveReport: async (defaultName, write) => {
// wxFileDialog( _( "Save Report File" ), Prj().GetProjectPath(), ... ): the report goes
// to the project's files, as a plot does.
const path = await new Promise<string | null>((resolve) =>
setErcSaveReport({ defaultName, resolve }),
);
if (path === null) return null;
const { projectName: name } = liveFilesRef.current;
const prefix = name ? `/${name}/` : '/';
const file = path.startsWith(prefix)
? path.slice(prefix.length)
: path.slice(path.lastIndexOf('/') + 1);
const text = write(file);
if (text === null) return file;
if (onOutputFile) onOutputFile(file, new TextEncoder().encode(text), 'text/plain');
return file;
},
displayError: (message) => console.error(message),
blockUntilLoaded: async () => {
// The live schematic brought up to the window's edits first: a rebuild after the run
// has begun would put back the markers it is about to delete.
syncLiveRef.current();
await prepareSymbolLibraries(frame, liveFilesRef.current.rawFiles, loadIndex);
// CvPcb's link test reads the footprint libraries (KIWAY::KiFACE( FACE_CVPCB )).
await loadFootprintLibraries();
},
show: (aShow) => setErcDialog({ dialog, shown: aShow }),
raise: () => {},
isShownOnScreen: () => ercDialogRef.current?.shown ?? false,
destroy: () => setErcDialog(null),
projectModified: () => {
frame.Schematic().RecordERCExclusions();
Pgm().GetSettingsManager().SaveProject();
},
ercDialogSettings: () => app.settings.eeschema.erc_dialog,
saveErcDialogSettings: (values) =>
app.settings.updateEeschema((c) => {
c.erc_dialog.crossprobe = values.crossprobe;
c.erc_dialog.scroll_on_crossprobe = values.scroll_on_crossprobe;
c.erc_dialog.show_all_errors = values.show_all_errors;
}),
};
dialog = new DIALOG_ERC(frame, window);
return dialog;
};
// DIALOG_SYMBOL_FIELDS_TABLE's window: its sub-dialogs, the BOM file, its showing and the
// field_editor settings.
fieldsTableFactoryRef.current = () => {
const frame = schFrameRef.current;
if (!frame || !syncLiveRef.current()) return null;
let dialog: DIALOG_SYMBOL_FIELDS_TABLE;
/** A path the dialog names, relative to the project folder (where generated files go). */
const projectRelative = (aPath: string): string => {
const { projectName: name } = liveFilesRef.current;
const prefix = name ? `/${name}/` : '/';
return aPath.startsWith(prefix) ? aPath.slice(prefix.length) : aPath.replace(/^\/+/, '');
};
const window: DIALOG_SYMBOL_FIELDS_TABLE_WINDOW = {
textEntry: (message, caption, value) =>
new Promise<string | null>((resolve) =>
setFieldsTextEntry({ message, caption, value, resolve }),
),
listDialog: (caption, label, items) =>
new Promise<string | null>((resolve) => setFieldsList({ caption, label, items, resolve })),
handleUnsavedChanges: async (message, save) => {
const result = await new Promise<UnsavedChangesResult>((resolve) =>
setFieldsUnsaved({ message, resolve }),
);
return handleUnsavedChanges(result, save);
},
// The BOM is downloaded, as every generated output is (the App's onOutputFile).
writeFile: (path, text) => {
const file = projectRelative(path);
if (onOutputFile) onOutputFile(file, new TextEncoder().encode(text), 'text/csv');
else downloadBlob(new Blob([text], { type: 'text/csv' }), file);
return true;
},
chooseFootprint: (current) =>
new Promise<string | null>((resolve) =>
setFpChooser({ current, commit: resolve, fpFilters: [], cancel: () => resolve(null) }),
),
show: (aShow) => setFieldsTable({ dialog, shown: aShow }),
raise: () => {},
destroy: () => setFieldsTable(null),
fieldEditorSettings: () => app.settings.eeschema.field_editor,
saveFieldEditorSettings: (values) =>
app.settings.updateEeschema((c) => {
c.field_editor = structuredClone(values);
}),
};
dialog = new DIALOG_SYMBOL_FIELDS_TABLE(frame, window);
return dialog;
};
// (TRANSITIONAL, S4-6c/S5): KiCad's own canvas - SCH_DRAW_PANEL on the live model,
// its VIEW and WX_VIEW_CONTROLS owning zoom and pan - over the window's, read only until the
// tools (SCH_SELECTION_TOOL onward) run on it. The live model follows the records.
const glCanvasRef = useRef<HTMLCanvasElement>(null);
const schPanelRef = useRef<SCH_DRAW_PANEL | null>(null);
const [schFontImage, setSchFontImage] = useState<ImageBitmap | null>(null);
const showLiveRef = useRef<() => void>(() => {});
const schShownRef = useRef(false);
showLiveRef.current = () => {
const panel = schPanelRef.current;
const frame = schFrameRef.current;
if (!panel || !frame) return;
// A rebuild of the live model zooms to fit (initScreenZoom); after the first showing, keep
// where the user was.
const view = panel.GetView();
const had = schShownRef.current ? { scale: view.GetScale(), center: view.GetCenter() } : null;
if (!syncLiveRef.current()) return;
schShownRef.current = true;
const file = currentFileRef.current;
const path = frame
.Schematic()
.Hierarchy()
.find((p) => p.LastScreen()?.GetFileName().endsWith(`/${file}`));
showingLiveRef.current = true;
try {
if (path && !path.equals(frame.GetCurrentSheet())) {
// SCH_NAVIGATE_TOOL::changeSheet: DisplayCurrentSheet clears the selection, so nothing
// selected on the sheet left behind comes along.
frame.SetCurrentSheet(path);
frame.DisplayCurrentSheet();
} else panel.DisplaySheet(frame.GetScreen());
} finally {
showingLiveRef.current = false;
}
if (had) {
view.SetScale(had.scale);
view.SetCenter(had.center);
}
panel.ForceRefresh();
};
useEffect(() => {
let cancelled = false;
loadBitmapFontImage().then(
(img) => {
if (!cancelled) setSchFontImage(img);
},
(err: unknown) => console.warn(`Could not use OpenGL: ${(err as Error).message}`),
);
return () => {
cancelled = true;
};
}, []);
// biome-ignore lint/correctness/useExhaustiveDependencies: a changed sheet or record is the trigger; the panel reads them through refs
useEffect(() => {
showLiveRef.current();
}, [doc, currentFile]);
// biome-ignore lint/correctness/useExhaustiveDependencies: the font atlas is the trigger; the rest are refs
useEffect(() => {
const canvas = glCanvasRef.current;
const frame = schFrameRef.current;
if (!canvas || !frame || !schFontImage || schPanelRef.current) return;
const panel = createSchDrawPanel(frame, canvas, schFontImage);
if (!panel) return;
schPanelRef.current = panel;
frame.ActivateGalCanvas();
syncFrameUnits(); // LoadSettings has read system.units
showLiveRef.current();
return () => {
panel.Destroy();
schPanelRef.current = null;
frame.SetCanvas(null);
};
}, [schFontImage]);
// SchScriptApi.editLive: an edit on the live model, its screens written back into the window
// as one undo step (sch_record_bridge.ts liveScreensToRecords; TRANSITIONAL until S7).
editLiveRef.current = async (aEdit) => {
if (!syncLiveRef.current()) return null;
const frame = schFrameRef.current!;
let edit: { result: Iterable<SCH_SCREEN> | null; messages: string[] };
inEditLiveRef.current = true;
try {
edit = await frame.WithoutDialogsAsync(true, () => aEdit(frame));
} catch (e) {
// The edit may have changed the live model before it threw; the window has not changed,
// so the next sync rebuilds the live model from it.
liveMirrorRef.current?.Invalidate();
throw e;
} finally {
inEditLiveRef.current = false;
}
const { result: screens, messages } = edit;
if (!screens) return messages.length ? messages : ['nothing was changed'];
adoptLiveScreensRef.current(screens);
return messages;
};
// The live model's changed screens written back as the window's records (sch_record_bridge.ts
// liveScreensToRecords; TRANSITIONAL until S7, when saving reads the live model). Stored as
// KiCad wrote them, the very objects the mirror adopts: they used to go through runProject,
// whose record cleanup (withCleanup) rebuilt them, so the mirror saw records it had not built
// from and reopened the project - wiping SCH_EDIT_FRAME's undo list after every edit. The
// undo step is the live commit's own (SCH_COMMIT::Push); KiCad's cleanup has already run in it.
adoptLiveScreensRef.current = (screens) => {
// A viewer's tab keeps no edits (runProject's guard, kept for the write-back).
if (myRoleRef.current === 'viewer') return;
const frame = schFrameRef.current!;
const name = liveFilesRef.current.projectName;
const dir = `/${name ?? project.current.root.replace(/\.[^.]*$/, '')}`;
const docs = liveScreensToRecords(frame, screens, dir);
const here = currentFileRef.current;
const changed: PickedFile[] = [];
for (const [file, d] of docs) {
if (file === here) continue;
// Another sheet, or a new sheet's file (initSheetDocument's case): the project holds it,
// and it is reported for the host to write, as foldStep reports a sheet an edit touched.
project.current.docs.set(file, d);
try {
changed.push({ name: file, text: serializeSchematic(d) });
} catch {
/* skip a bad sheet */
}
}
if (changed.length) {
pendingProjectChange.current.push(...changed);
pendingIsMine.current = true;
}
liveMirrorRef.current?.Adopt(docs);
const current = docs.get(here);
if (current) setDoc(current);
};
// (TRANSITIONAL, S5): a tool on the live canvas committed (SCH_EDIT_FRAME::OnModify).
// Its screen - and any sheet file the commit created - become the window's records, so the
// window follows the live model the tools edit.
// (TRANSITIONAL, W2c): a live tool changed sheets (DisplayCurrentSheet). The window
// follows: the record path of that instance is '/' then each sheet below the top-level one,
// by uuid ('/<uuid>/<uuid>/'), and the file is its screen's, relative to the project folder.
liveSheetChangedRef.current = () => {
if (showingLiveRef.current || !schPanelRef.current) return;
const frame = schFrameRef.current!;
const sheetPath = frame.GetCurrentSheet();
const screen = sheetPath.LastScreen();
if (!screen) return;
let path = '/';
for (let i = 1; i < sheetPath.size(); i++) path += `${sheetPath.at(i)!.m_Uuid}/`;
const name = liveFilesRef.current.projectName;
const dir = `/${name ?? project.current.root.replace(/\.[^.]*$/, '')}`;
const absolute = screen.GetFileName().replace(/\\/g, '/');
const file = absolute.startsWith(`${dir}/`) ? absolute.slice(dir.length + 1) : absolute;
if (path !== currentPath || file !== currentFile) switchSheet(path, file);
};
liveModifiedRef.current = () => {
if (inEditLiveRef.current || !schPanelRef.current) return;
const frame = schFrameRef.current!;
const changed = new Set<SCH_SCREEN>();
const current = frame.GetScreen();
if (current) changed.add(current);
for (const screen of liveMirrorRef.current?.TakeCommittedScreens() ?? []) changed.add(screen);
const name = liveFilesRef.current.projectName;
const dir = `/${name ?? project.current.root.replace(/\.[^.]*$/, '')}`;
const screens = new SCH_SCREENS(frame.Schematic().Root());
for (let screen = screens.GetFirst(); screen; screen = screens.GetNext()) {
const file = screen.GetFileName().replace(/\\/g, '/');
const rel = file.startsWith(`${dir}/`) ? file.slice(dir.length + 1) : file;
if (rel !== currentFileRef.current && !project.current.docs.has(rel)) changed.add(screen);
}
adoptLiveScreensRef.current(changed);
};
// ModalAnnotate: the Annotate dialog, which cannot block the mail here; the user annotates
// and updates the board again.
modalAnnotateRef.current = () => setAnnotateOpen(true);
// `SaveProject()`: an assignment from this same mail round is written as it
// folds; with nothing pending, the open sheet is saved now.
saveProjectRef.current = () => {
if (assignPendingRef.current) saveRequestedRef.current = true;
else save();
return true;
};
/**
* `SCH_EDITOR_CONTROL::SaveCurrSheetCopyAs` (eeschema/tools/
* sch_editor_control.cpp:426-442):
*
* SCH_SHEET* curr_sheet = m_frame->GetCurrentSheet().Last();
* wxFileName curr_fn = curr_sheet->GetFileName();
* wxFileDialog dlg( …, curr_fn.GetPath(), curr_fn.GetFullName(),
* FILEEXT::KiCadSchematicFileWildcard(),
* wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
* if( dlg.ShowModal() == wxID_CANCEL ) return false;
* wxString newFilename =
* EnsureFileExtension( dlg.GetPath(), FILEEXT::KiCadSchematicFileExtension );
* m_frame->saveSchematicFile( curr_sheet, newFilename );
*
* Three things this must NOT do, all of them from
* `SCH_EDIT_FRAME::saveSchematicFile` (eeschema/files-io.cpp:989-1081):
*
* - it writes the CURRENT SHEET only, never the hierarchy. The dialog is
* seeded from that sheet's own name, not the project's.
* - it never calls `screen->SetFileName()`, so the editor is NOT retargeted
* at the copy: you go on editing the original, and the title does not
* change.
* - on success it does `screen->SetContentModified( false )` and
* `SetStatusText( "File '%s' saved." )` built from `screen->GetFileName()`
* — the ORIGINAL name, because the screen was never renamed.
*
* That last pair looks like an upstream bug: saving a COPY clears the dirty
* flag on a document that was not written, and then reports the original
* file's name as the one saved. It is mirrored here deliberately rather than
* corrected. The parity target is the installed build including where it is
* odd; the moment we start fixing KiCad's oddities the two stop matching and
* a user who knows KiCad is the one surprised.
*
* The path comes from the same file manager every other Save As uses -
* upstream's is `wxFileDialog( … wxFD_SAVE | wxFD_OVERWRITE_PROMPT )` seeded
* with the current sheet's name. It was a `window.prompt`, which cannot show
* the project, cannot filter and cannot warn about an overwrite. Nothing
* else about the command changes: the seed, the extension rule, what gets
* written, and what is left alone are all upstream's.
*/
const [copyAsOpen, setCopyAsOpen] = useState(false);
// curr_fn.GetFullName() — the current sheet's own file name, which for us is
// the file the editor has open.
const copyAsSeed = currentFile !== DEFAULT_FILE ? currentFile : (fileName ?? DEFAULT_FILE);
/** `curr_fn.GetFullName()` — the leaf, not the project-relative path. */
const basename = (file: string): string => file.split('/').filter(Boolean).pop() ?? file;
/**
* `curr_fn.GetPath()` — the folder the sheet's own file sits in.
*
* A sheet's stored name is project-relative, so a sub-sheet kept in a
* subfolder opens THERE rather than at the project root. With no directory
* part the answer is the project folder itself, which is where a flat
* project's sheets live.
*/
const sheetDirOf = (project: string, file: string): string => {
const parts = file.split('/').filter(Boolean);
parts.pop();
return [`/${project}`, ...parts].join('/');
};
const saveCurrSheetCopyAs = useCallback(() => setCopyAsOpen(true), []);
const saveCurrSheetCopyTo = useCallback(
(picked: string) => {
const d = docRef.current;
if (!d) return;
const seed = copyAsSeed;
const trimmed = picked.split('/').filter(Boolean).pop()?.trim() ?? '';
if (!trimmed) return;
const newFilename = ensureFileExtension(trimmed, KICAD_SCHEMATIC_FILE_EXTENSION);
let text: string;
try {
text = serializeSchematic(d);
} catch (e) {
// saveSchematicFile's catch( IO_ERROR ) -> DisplayError, and success
// stays false, so neither the dirty flag nor the status text is touched.
setError(
`Error saving schematic file '${newFilename}'.\n${e instanceof Error ? e.message : String(e)}`,
);
return;
}
if (onPersistFiles && currentFile !== DEFAULT_FILE) {
onPersistFiles([{ name: newFilename, text }]);
} else {
const url = URL.createObjectURL(new Blob([text], { type: 'application/octet-stream' }));
const a = document.createElement('a');
a.href = url;
a.download = newFilename;
a.click();
URL.revokeObjectURL(url);
}
// screen->SetContentModified( false ), then the status text built from
// screen->GetFileName() — the original, not the copy. See above.
setDirty(false);
setUnsaved(false);
setStatusText(savedFileMessage(seed));
},
[copyAsSeed, currentFile, onPersistFiles],
);
/**
* `SCH_EDITOR_CONTROL::Revert` (eeschema/tools/sch_editor_control.cpp:445-492).
*
* The order is upstream's and each step is load-bearing:
*
* 1. remember the current sheet, and whether it is a subsheet:
* `wasOnSubsheet = ( GetCurrentSheet().Last() != &root )`.
* 2. if it is, navigate to the ROOT FIRST — `Hierarchy().at( 0 )`, with the
* comment "manually pushing root creates a path with empty KIID which
* causes assertions". Upstream deliberately does NOT `wxSafeYield()` here,
* "to avoid repainting the root sheet before the dialog", so the user
* sees the question rather than a flash of a sheet they did not ask for.
* 3. ask. The string is verbatim, and `IsOK` (common/confirm.cpp:278-300) is
* a "Confirmation" dialog with a question icon whose OK/Cancel pair is
* relabelled Yes/No, with wxOK_DEFAULT — so YES is the default button.
* 4. NO returns you to the sheet you were on (this one DOES yield) and
* changes nothing.
* 5. YES marks every screen unmodified BEFORE restoring — "do not prompt the
* user for changes" — then releases and re-opens.
*
* The `%s` is `schematic.GetFileName()`, which is the first top-level sheet's
* file (eeschema/schematic.cpp:524-532), not whichever sheet you are looking
* at — the prompt names the project, and says "(and all sub-sheets)" because
* it discards the whole hierarchy.
*
* What differs here, and it is the persistence model rather than a shortcut:
* upstream re-reads the FILE, because KiCad writes to disk only on Save. We
* autosave, so the file already holds the edits Revert is meant to discard;
* our "last version saved" is the newest Local History save point instead.
* Upstream keeps Revert and Local History's Restore Commit separate; here
* they necessarily meet.
*/
const revert = useCallback(() => {
if (!onRevert) return;
const rootSheet = flatSheets[0];
const wasOnSubsheet = !!rootSheet && currentPath !== rootSheet.path;
const originalSheet = { path: currentPath, file: currentFile };
// Step 2: to the root before asking, and without repainting first.
if (wasOnSubsheet && rootSheet) switchSheet(rootSheet.path, rootSheet.file, false);
setRevertPrompt({
// schematic.GetFileName() — the first top-level sheet's file.
file: rootSheet?.file ?? currentFile,
onNo: () => {
setRevertPrompt(null);
// Step 4: back to where they were.
if (wasOnSubsheet) switchSheet(originalSheet.path, originalSheet.file, false);
},
onYes: () => {
setRevertPrompt(null);
// Step 5. `SetContentModified( false )` on every screen first, so the
// reload does not stop to ask about the very changes being discarded.
setDirty(false);
setUnsaved(false);
void onRevert().then((ok) => {
if (!ok) setInfoBar('There is no saved version to revert to yet.');
});
},
});
}, [onRevert, flatSheets, currentPath, currentFile, switchSheet]);
// ----- copy / cut / paste / duplicate (SCH_EDITOR_CONTROL port) -------------
// Copy writes KiCad's clipboard format (lib_symbols + items as S-expressions),
// so text copied here pastes into desktop KiCad and vice versa. Paste parses
// the clipboard, gives everything fresh UUIDs, re-annotates duplicate
// references, and attaches the items to the cursor until clicked to drop.
// Text-entry focus only: a focused checkbox/radio (e.g. the Selection
// Filter panel) must not swallow editor hotkeys the way a text box does.
const isTyping = (): boolean => {
const el = document.activeElement as HTMLElement | null;
if (!el) return false;
if (el.tagName === 'TEXTAREA' || el.isContentEditable) return true;
return (
el.tagName === 'INPUT' &&
!/^(checkbox|radio|button|range)$/.test((el as HTMLInputElement).type)
);
};
useEffect(() => {
// Editors stay mounted behind display:none, only the visible frame may
// own the document clipboard events (see App's activeView stamp).
const hidden = (): boolean => (document.body.dataset.activeView ?? 'schematic') !== 'schematic';
const liveSelectionEmpty = (): boolean =>
schFrameRef.current?.GetToolManager()?.GetTool(SCH_SELECTION_TOOL)?.GetSelection().Empty() ??
true;
// SCH_EDITOR_CONTROL::Copy / Cut save through SaveClipboard; inside the browser's own
// event, what they saved goes on the system clipboard too (as pcbnew's frame does).
const onCopy = (e: ClipboardEvent): void => {
if (hidden() || isTyping() || liveSelectionEmpty()) return;
runLiveAction(ACTIONS.copy);
const text = GetClipboardText();
if (!text) return;
e.clipboardData?.setData('text/plain', text);
e.preventDefault();
};
const onCut = (e: ClipboardEvent): void => {
if (hidden() || isTyping() || liveSelectionEmpty()) return;
runLiveAction(ACTIONS.cut);
const text = GetClipboardText();
if (!text) return;
e.clipboardData?.setData('text/plain', text);
e.preventDefault();
};
// SCH_EDITOR_CONTROL::Paste reads the clipboard; the event's data becomes it first.
const onPaste = (e: ClipboardEvent): void => {
if (hidden() || isTyping() || !e.clipboardData) return;
e.preventDefault();
void SetClipboardFromPaste(e.clipboardData).then(() => runLiveAction(ACTIONS.paste));
};
document.addEventListener('copy', onCopy);
document.addEventListener('cut', onCut);
document.addEventListener('paste', onPaste);
return () => {
document.removeEventListener('copy', onCopy);
document.removeEventListener('cut', onCut);
document.removeEventListener('paste', onPaste);
};
// biome-ignore lint/correctness/useExhaustiveDependencies: isTyping reads the DOM at the event
}, [runLiveAction]);
// Duplicate (Ctrl+D): SCH_EDITOR_CONTROL::Duplicate on the live tool.
const duplicateSelection = useCallback(() => runLiveAction(ACTIONS.duplicate), [runLiveAction]);
// The script API's picture: the whole sheet through the plot path, in the
// editor's theme. 150 dpi keeps 1.27 mm text legible to a vision model.
const snapshotRef = useRef<() => string>(() => '');
snapshotRef.current = () => {
const d = docRef.current;
if (!d) return '';
const canvas = renderSheetToCanvas(
d,
theme,
{ color: true, drawingSheet: true, background: true },
150,
);
return canvas.toDataURL('image/png').slice('data:image/png;base64,'.length);
};
// Display + input options handed to the canvas, straight from the settings
// (Preferences > Display Options / Grids / Mouse and Touchpad).
const renderOpts = useMemo<RenderOpts>(
() => ({
showHiddenPins: es.appearance.show_hidden_pins,
showHiddenFields: es.appearance.show_hidden_fields,
// `SCH_RENDER_SETTINGS::LoadColors` copies the theme's
// `GetOverrideSchItemColors()` into `m_OverrideItemColors`
// (`sch_render_settings.cpp:75`); the painter then ignores every item's
// own colour. The flag belongs to the THEME, so it comes from whichever
// one is selected — see `overrideItemColorsFor`.
overrideItemColors: app.overrideItemColorsFor(es.appearance.color_theme),
showPageLimits: es.appearance.show_page_limits,
// `eeconfig()->m_Appearance.show_directive_labels` — read per label by
// the painter (sch_painter.cpp:3266), so the flags disappear the moment
// Display Options turns them off, and a selected one stays visible.
showDirectiveLabels: es.appearance.show_directive_labels,
// `eeconfig()->m_Selection.fill_shapes`, read in the shadow pass
// (sch_painter.cpp:2068).
showPinAltIcons: es.appearance.show_pin_alt_icons,
highlightNetclassColors: es.selection.highlight_netclass_colors,
netclassHighlightThicknessMils: es.selection.highlight_netclass_colors_thickness,
netclassHighlightAlpha: es.selection.highlight_netclass_colors_alpha,
fillSelectedShapes: es.selection.fill_shapes,
drawSelectedChildren: es.selection.draw_selected_children,
// `eeconfig()->m_Appearance.mark_sim_exclusions` — the painter reads it
// per symbol (sch_painter.cpp:2696), so the marker disappears the moment
// Display Options turns it off.
markSimExclusions: es.appearance.mark_sim_exclusions,
...(activeSheet ? { drawingSheet: activeSheet } : {}),
// The on-screen title block shows the current instance's real page
// number, sheet count and path (SCH_EDIT_FRAME::SetSheetNumberAndCount).
...((): Partial<RenderOpts> => {
const idx = sheetInstanceRefs.findIndex((s) => s.path === currentPath);
if (idx === -1) return {};
const ref = sheetInstanceRefs[idx]!;
return {
pageNumber: pageNumberOf(currentPath) || String(idx + 1),
sheetNumber: idx + 1,
sheetCount: sheetInstanceRefs.length,
...(ref.path !== '/' ? { sheetName: ref.name } : {}),
sheetPath: ref.namePath,
};
})(),
// Default pen for zero-width strokes = Schematic Setup > Formatting's
// "Default line width" (SCHEMATIC_SETTINGS::m_DefaultLineWidth), mils→IU.
defaultPenIU: mmToIU((setup.formatting.defaultLineWidthMils * 25.4) / 1000),
// Wires and buses resolve separately: SCH_LINE::GetPenWidth reads the
// netclass's wire width on LAYER_WIRE and its *bus* width on LAYER_BUS,
// and eeschema seeds those from Preferences > Editing Options
// (m_Drawing.default_wire_thickness 6 mils, default_bus_thickness 12).
// A bus is meant to read as twice as thick as a wire.
defaultWireIU: mmToIU((es.drawing.default_wire_thickness * 25.4) / 1000),
defaultBusIU: mmToIU((es.drawing.default_bus_thickness * 25.4) / 1000),
// Junction-dot size, dash ratios and label/pin text offsets from
// Schematic Setup > Formatting (SCH_RENDER_SETTINGS seeding).
...drawingDefaults,
// Wire colour/width/style + junction clamp from the resolved netclasses.
...(netOverrides ? { netOverrides } : {}),
// Which junction dots the graph put on LAYER_BUS_JUNCTION. Upstream this
// is a flag CONNECTION_GRAPH writes onto the item; here it travels with
// the other connectivity answers rather than being re-derived by the
// painter.
busJunctionIds,
// ${VAR} expansion in labels/text/fields (GetShownText).
...(resolveTextVar ? { resolveTextVar } : {}),
// ${INTERSHEET_REFS} on global labels (LAYER_INTERSHEET_REFS shown).
...(intersheetRefs ? { intersheetRefs } : {}),
selectionThicknessMils: es.selection.thickness,
highlightThicknessMils: es.selection.highlight_thickness,
grid: {
show: es.window.grid.show,
sizeIU: gridSizeToIU(es.window.grid.sizes[es.window.grid.last_size_idx]?.x ?? '50 mil'),
style: es.window.grid.style,
lineWidthPx: es.window.grid.line_width,
minSpacingPx: es.window.grid.min_spacing,
devicePixelRatio: dpr,
overrides: {
enabled: es.window.grid.overrides_enabled,
...(es.window.grid.overrides.connected.enabled
? { connected: gridSizeToIU(es.window.grid.overrides.connected.size) }
: {}),
...(es.window.grid.overrides.wires.enabled
? { wires: gridSizeToIU(es.window.grid.overrides.wires.size) }
: {}),
...(es.window.grid.overrides.text.enabled
? { text: gridSizeToIU(es.window.grid.overrides.text.size) }
: {}),
...(es.window.grid.overrides.graphics.enabled
? { graphics: gridSizeToIU(es.window.grid.overrides.graphics.size) }
: {}),
},
},
}),
[
es,
activeSheet,
setup,
drawingDefaults,
busJunctionIds,
netOverrides,
resolveTextVar,
intersheetRefs,
sheetInstanceRefs,
currentPath,
pageNumberOf,
dpr,
app,
],
);
const inputPrefs = useMemo<InputPrefs>(
() => ({
zoomSpeed: common.input.zoom_speed,
zoomSpeedAuto: common.input.zoom_speed_auto,
zoomAcceleration: common.input.zoom_acceleration,
centerOnZoom: common.input.center_on_zoom,
reverseZoom: common.input.reverse_scroll_zoom,
scrollModZoom: common.input.scroll_modifier_zoom,
scrollModPanH: common.input.scroll_modifier_pan_h,
scrollModPanV: common.input.scroll_modifier_pan_v,
reverseScrollPanH: common.input.reverse_scroll_pan_h,
horizontalPan: common.input.horizontal_pan,
motionPanModifier: common.input.motion_pan_modifier,
autoPan: common.input.auto_pan,
autoPanAcceleration: common.input.auto_pan_acceleration,
mouseLeft: common.input.mouse_left as InputPrefs['mouseLeft'],
mouseMiddle: common.input.mouse_middle as InputPrefs['mouseMiddle'],
mouseRight: common.input.mouse_right as InputPrefs['mouseRight'],
dragIsMove: es.input.drag_is_move,
autoStartWires: es.drawing.auto_start_wires,
crosshair: es.window.cursor.crosshair,
alwaysShowCrosshair: es.window.cursor.always_show_cursor,
}),
[common, es],
);
// Selecting a placement tool reopens its chooser/dialog (clears any attached item).
const onToolSelect = useCallback((id: string) => {
// Every one of these is an AF_ACTIVATE tool, so picking the one already
// running stops it — see `activateTool`. Checked before the two tools that
// open something, or clicking a lit Image button would reopen the file
// picker instead of putting the tool away.
if (activeToolRef.current === id) {
setActiveTool('select');
setPlaceLib(null);
setPendingLabel(null);
setPendingImage(null);
return;
}
// The Image tool opens a file picker; the image then follows the cursor
// (SCH_ACTIONS::placeImage).
if (id === 'image') {
imageInputRef.current?.click();
return;
}
setActiveTool(id);
setPlaceLib(null);
setPendingLabel(null);
setPendingImage(null);
}, []);
/**
* "Place Pins from Sheet" (`SCH_ACTIONS::importSheetPin`).
*
* The tool imports; it does not ask. `TwoClickPlace` takes the next
* hierarchical label the child sheet has and the parent has no pin for —
*
* SCH_HIERLABEL* label = importHierLabel( sheet );
* if( !label ) { … "No new hierarchical labels found." … break; }
* item = createNewSheetPinFromLabel( sheet, cursorPos, label );
*
* — and `createNewSheetPinFromLabel` copies the label's text *and* shape onto
* the pin, so the two cannot disagree. Ours opened the pin-properties dialog
* and had you type a name, which is the manual gesture upstream only offers
* from the sync dialog, and which lets a pin and its label drift apart.
*/
/**
* SCH_DRAWING_TOOLS::m_statusPopup: a tool's one popup, replaced by the next
* (`std::make_unique` over the old one) and shown beside the cursor for two
* seconds - `Move( GetMousePosition() + wxPoint( 20, 20 ) ); PopupFor( 2000 )`.
*/
const statusPopupRef = useRef<STATUS_TEXT_POPUP | null>(null);
useEffect(() => () => statusPopupRef.current?.Destroy(), []);
// `wxWindow::PopupMenu` for the frame under (TRANSITIONAL, W2): the tools'
// context menu (TOOL_MENU::ShowContextMenu, evaluated against the selection) and any other
// ACTION_MENU a tool puts up, drawn with the menu bar's ContextMenu.
const [toolPopup, setToolPopup] = useState<{
menu: ACTION_MENU;
x: number;
y: number;
onClose: () => void;
} | null>(null);
useEffect(() => {
const frame = schFrameRef.current;
if (!frame) return;
frame.SetPopupMenuPresenter((aMenu, aOnClose) => {
const at = KIPLATFORM_UI.GetMousePosition();
aMenu.OnMenuEvent(new wxMenuEvent(wxMenuEventType.wxEVT_MENU_OPEN, 0, aMenu));
setToolPopup({ menu: aMenu, x: at.x, y: at.y, onClose: aOnClose });
});
return () => frame.SetPopupMenuPresenter(null);
}, []);
/** The active grid step, which the table's cell size is snapped to. */
const gridSizeIU = useMemo(
() => gridSizeToIU(es.window.grid.sizes[es.window.grid.last_size_idx]?.x ?? '50 mil'),
[es.window.grid.sizes, es.window.grid.last_size_idx],
);
gridSizeIURef.current = gridSizeIU;
/** A label was dropped: take the next of a multi-label run, else stop. */
/** The sheet as DIALOG_SHEET_PROPERTIES wants it: its fields as grid rows,
* its border and fill, this instance's page number and its attributes. */
// The image file picker: read the chosen bitmap as base64 and attach it to the cursor.
const onImageFile = useCallback((file: File) => {
const reader = new FileReader();
reader.onload = () => {
const res = String(reader.result);
const comma = res.indexOf(',');
setPendingImage(makeImage({ x: 0, y: 0 }, comma >= 0 ? res.slice(comma + 1) : res));
setActiveTool('image');
};
reader.readAsDataURL(file);
}, []);
const onTopAction = useCallback(
(id: string) => {
if (snapshotHost && id === 'save') {
void snapshotHost.save().catch(() => undefined);
return;
}
// the canvas-editing actions run on the frame's TOOL_MANAGER (TRANSITIONAL, W2).
const galAction = schToolbarAction(id);
const galMgr = schFrameRef.current?.GetToolManager();
if (galAction && galMgr) {
galMgr.RunAction(galAction);
return;
}
// ACTIONS::about — Help > About. The menu sent this id and nothing
// answered it, so the schematic's About did nothing at all.
if (id === 'about') {
setAboutOpen(true);
return;
}
// ACTIONS::listHotKeys — Ctrl+F1 and Help > List Hotkeys.
if (id === 'listHotkeys') {
showHotkeyList();
return;
}
if (id === 'assignNetclass') {
runLiveAction(SCH_ACTIONS.assignNetclass);
return;
}
if (id === 'open') promptOpen();
else if (id === 'save') save();
// SCH_ACTIONS::saveCurrSheetCopyAs
else if (id === 'saveCurrSheetCopyAs') saveCurrSheetCopyAs();
// ACTIONS::revert
else if (id === 'revert') revert();
else if (id === 'erc') runLiveAction(SCH_ACTIONS.runERC);
else if (id === 'manageSymbolLibraries') setSymLibTableOpen(true);
else if (id === 'ercPrevMarker') runLiveAction(SCH_ACTIONS.prevMarker);
else if (id === 'ercNextMarker') runLiveAction(SCH_ACTIONS.nextMarker);
else if (id === 'ercExcludeMarker') runLiveAction(SCH_ACTIONS.excludeMarker);
else if (id === 'syncSheetPins' || id === 'syncAllSheetPins') {
// SCH_DRAWING_TOOLS::SyncSheetsPins / SyncAllSheetsPins on the live tool.
runLiveAction(
id === 'syncSheetPins' ? SCH_ACTIONS.syncSheetPins : SCH_ACTIONS.syncAllSheetsPins,
);
} else if (id === 'importSheet') runLiveAction(SCH_ACTIONS.importSheet);
else if (id === 'importGraphics') runLiveAction(SCH_ACTIONS.importGraphics);
else if (id === 'showPcbNew') onShowPcb?.();
else if (id === 'updatePcbFromSch') onUpdatePcb?.();
else if (id === 'updateSchFromPcb') runLiveAction(ACTIONS.updateSchematicFromPcb);
else if (id === 'symbolEditor') onShowSymbolEditor?.();
else if (id === 'footprintEditor') onShowFootprintEditor?.();
else if (id === 'bom') runLiveAction(SCH_ACTIONS.generateBOM);
else if (id === 'exportNetlist') setNetlistOpen(true);
else if (id === 'editSymbolFields') runLiveAction(SCH_ACTIONS.editSymbolFields);
// COMMON_CONTROL::ShowPlayer( FRAME_SCH_VIEWER ): the browser is a KIWAY player of its own.
else if (id === 'symbolBrowser') runLiveAction(ACTIONS.showSymbolBrowser);
else if (id === 'assignFootprints') setAssignFpOpen(true);
else if (id === 'showCalculator') onShowCalculator?.();
// ACTIONS::selectAll, whose row carries Ctrl+A and dispatches from there.
else if (id === 'selectAll') runLiveAction(ACTIONS.selectAll);
else if (id === 'unselectAll') setSelection(new Set());
// Add to Group / Remove from Group: GROUP_TOOL on the live selection.
else if (id === 'addToGroup') runLiveAction(ACTIONS.addToGroup);
else if (id === 'removeFromGroup') runLiveAction(ACTIONS.removeFromGroup);
else if (id === 'openPreferences') setPrefsOpen(true);
else if (id === 'close') onExitToHome();
// ACTIONS::help has no VibeMake documentation destination yet.
// Tools > Project Manager. One page here, so it lands where Close does.
else if (id === 'showProjectManager') onExitToHome();
else if (id === 'find') runLiveAction(ACTIONS.find);
else if (id === 'findReplace') runLiveAction(ACTIONS.findAndReplace);
else if (id === 'annotate') setAnnotateOpen(true);
else if (id === 'incrementAnnotations') runLiveAction(SCH_ACTIONS.incrementAnnotations);
else if (id === 'globalEditTextAndGraphics') runLiveAction(SCH_ACTIONS.editTextAndGraphics);
else if (id === 'rescueSymbols') void runRescueSymbols(true);
else if (id === 'editSymbolLibraryLinks') runLiveAction(SCH_ACTIONS.editSymbolLibraryLinks);
else if (id === 'schematicSetup') {
// The Embedded Files page lists the sheet's embedded_files section
// (names + embed-fonts flag) fresh from the document on every open,
// read-only until the zstd blobs can be decoded.
// PANEL_EMBEDDED_FILES( book, &m_frame->Schematic() ): the live schematic's files.
const schematic = schFrameRef.current?.Schematic();
if (schematic) {
setSetup((prev) => ({
...prev,
embeddedFiles: PANEL_EMBEDDED_FILES.TransferDataToWindow(schematic.GetEmbeddedFiles()),
}));
}
setSetupOpen(true);
} else if (id === 'pageSettings') runLiveAction(ACTIONS.pageSettings);
else if (id === 'print') setPrintOpen(true);
else if (id === 'plot') setPlotOpen(true);
// SCH_EDIT_TOOL::EditPageNumber asks through TextEntryDialog.
else if (id === 'editPageNumber') runLiveAction(SCH_ACTIONS.editPageNumber);
// Hierarchy navigation (SCH_NAVIGATE_TOOL). Back/Forward move the history
// cursor without pushing; Up and Previous/Next go through changeSheet.
else if (id === 'navBack' || id === 'navFwd') {
const p = id === 'navBack' ? navTool.current.back() : navTool.current.forward();
const target = p !== null ? flatSheets.find((s) => s.path === p) : undefined;
if (target) switchSheet(target.path, target.file, false);
} else if (id === 'navUp') {
const pp = parentPath(currentPath);
const target = pp !== null ? flatSheets.find((s) => s.path === pp) : undefined;
if (target) switchSheet(target.path, target.file);
} else if (id === 'navPrev' || id === 'navNext') {
const idx = flatSheets.findIndex((s) => s.path === currentPath);
const target = idx !== -1 ? flatSheets[idx + (id === 'navNext' ? 1 : -1)] : undefined;
if (target) switchSheet(target.path, target.file);
}
// Menu Cut/Copy re-dispatch the native clipboard events our document
// handlers already implement; Paste reads the async clipboard API (menu
// clicks can't synthesize a trusted paste event).
else if (id === 'cut') document.execCommand('cut');
else if (id === 'copy') document.execCommand('copy');
// ACTIONS::copyAsText: SCH_EDITOR_CONTROL::CopyAsText on the live tool.
else if (id === 'copyAsText') runLiveAction(ACTIONS.copyAsText);
// ACTIONS::pasteSpecial: SCH_EDITOR_CONTROL::Paste asks the dialog itself. A menu row is
// a user gesture, so the system clipboard can be read in first.
else if (id === 'pasteSpecial')
void (navigator.clipboard?.readText() ?? Promise.resolve(''))
.catch(() => '')
.then((text) => {
if (text) SetClipboardFromText(text);
runLiveAction(ACTIONS.pasteSpecial);
});
},
[
undo,
redo,
save,
saveCurrSheetCopyAs,
revert,
promptOpen,
onShowPcb,
onUpdatePcb,
onShowSymbolEditor,
onShowFootprintEditor,
onShowCalculator,
onExitToHome,
flatSheets,
currentPath,
switchSheet,
netlist,
selection,
runRescueSymbols,
runLiveAction,
snapshotHost,
],
);
/**
* A right-toolbar click. Most of its buttons arm a placement tool; the few in
* `RIGHT_TOOLBAR_COMMANDS` run straight away instead, so they go to the same
* dispatcher the menu items use rather than becoming an `activeTool` no tool
* answers to.
*/
const onRightToolbar = useCallback(
(id: string) => {
// the tools run on the frame's TOOL_MANAGER (TRANSITIONAL, W2).
const galAction = schToolbarAction(id);
const galMgr = schFrameRef.current?.GetToolManager();
if (galAction && galMgr) {
// ACTION_TOOLBAR::onToolEvent (action_toolbar.cpp:804-811): the event has no
// position, so a drawing tool does not prime its first point at the toolbar.
const evt = galAction.MakeEvent();
evt.SetHasPosition(false);
galMgr.ProcessEvent(evt);
return;
}
if (RIGHT_TOOLBAR_COMMANDS.has(id)) onTopAction(id);
},
[onTopAction],
);
const onLeftToggle = useCallback(
(id: string) => {
// Not a toggle, and not a button either: the Show Grid button carries a
// right-click menu whose one row is `ACTIONS::gridProperties`
// (`eeschema/toolbars_sch_editor.cpp:71-79`), and upstream runs that row
// through the same TOOL_MANAGER the button goes through, so it arrives
// here. `COMMON_TOOLS::GridProperties` for FRAME_SCH is
// `ShowPreferences( _( "Grids" ), _( "Schematic Editor" ) )`
// (`common/tool/common_tools.cpp:623`).
if (id === 'gridProperties') {
openPrefs('sch-grids');
return;
}
// The Attributes submenu is a set of item edits, not a view setting: it
// acts on the selection (SCH_EDIT_TOOL::SetAttribute).
const attr = ATTRIBUTE_IDS[id];
if (attr) {
runLiveAction(attr);
return;
}
if (SETTINGS_TOGGLES.has(id)) {
app.settings.updateEeschema((s) => {
if (id === 'toggleHiddenPins')
s.appearance.show_hidden_pins = !s.appearance.show_hidden_pins;
else if (id === 'toggleHiddenFields')
s.appearance.show_hidden_fields = !s.appearance.show_hidden_fields;
else if (id === 'crosshairSmall') s.window.cursor.crosshair = 'small';
else if (id === 'crosshairFull') s.window.cursor.crosshair = 'full';
else if (id === 'crosshair45') s.window.cursor.crosshair = '45';
else if (id === 'lineModeFree') s.drawing.line_mode = 0;
else if (id === 'lineMode90') s.drawing.line_mode = 1;
else if (id === 'lineMode45') s.drawing.line_mode = 2;
else if (id === 'annotateAuto') s.annotation.automatic = !s.annotation.automatic;
});
return;
}
// ACTIONS::toggleGrid / toggleGridOverrides: COMMON_TOOLS::ToggleGrid on the frame, which
// sets the window's grid.show and the GAL's grid together and redraws. Through the app's
// settings alone, the canvas - the GAL's - kept its grid until a reload.
if (id === 'toggleGrid' || id === 'toggleGridOverrides') {
runLiveAction(id === 'toggleGrid' ? ACTIONS.toggleGrid : ACTIONS.toggleGridOverrides);
return;
}
// ACTIONS::inchesUnits / milsUnits / millimetersUnits: COMMON_TOOLS::SwitchUnits on the frame.
const unitAction =
id === 'unitsInches'
? ACTIONS.inchesUnits
: id === 'unitsMils'
? ACTIONS.milsUnits
: id === 'unitsMm'
? ACTIONS.millimetersUnits
: null;
if (unitAction) {
runLiveAction(unitAction);
syncFrameUnits();
return;
}
// An AUI pane flips.
setLocalToggles((prev) => {
const next = new Set(prev);
if (next.has(id)) next.delete(id);
else next.add(id);
return next;
});
// eslint-disable-next-line react-hooks/exhaustive-deps
},
[openPrefs, app, runLiveAction, syncFrameUnits],
);
// Menus carry their shortcut as literal text, so a rebinding has to be
// painted back over them (see applyHotkeyOverrides).
const hotkeyOverrides = app.useHotkeyOverrides();
/**
* The tree as the actions *declare* it, before any rebinding is painted on.
*
* Split out because the two consumers want opposite things. What is drawn
* must show the user's own key, so it gets `applyHotkeyOverrides`. What
* *dispatches* must match the defaults, because `remapEvent` has already
* turned a rebound press into the action's default combo - that is the whole
* mechanism, and it is why the key chain "deliberately still matches on the
* defaults". Dispatching against the painted tree would mean translating the
* event to the default and then comparing it with the override, so every
* rebound command would answer to nothing.
*/
// biome-ignore lint/correctness/useExhaustiveDependencies: doc and selection are the triggers: an edit or a selection change re-reads liveChecked's ticks
const menusRaw = useMemo(
() =>
buildMenus(
{
tool: onToolSelect,
action: onTopAction,
toggle: onLeftToggle,
// Preferences > Set Language (menubar.cpp:347-348). The setting is
// COMMON_SETTINGS', shared by every frame, so it is read and written
// through the common store exactly as the other five launchers do.
language: app.settings.common.system.language,
onSelectLanguage: (label: string) =>
app.settings.updateCommon((c) => {
c.system.language = label;
}),
},
{
// CHECK( cond.CurrentTool( ACTIONS::zoomTool ) ): the View entry ticks
// while the tool is running, the same condition the button uses.
zoomTool: activeTool === 'zoomTool',
toggleHiddenPins: es.appearance.show_hidden_pins,
toggleHiddenFields: es.appearance.show_hidden_fields,
showProperties: toggles.has('showProperties'),
showSearch: toggles.has('showSearch'),
showHierarchy: toggles.has('showHierarchy'),
showNetNavigator: toggles.has('showNetNavigator'),
// Each attribute ticks by the check condition SCH_EDIT_TOOL::Init gives its action
// (attribDNPCond and friends), on the live selection.
...Object.fromEntries(
Object.entries(ATTRIBUTE_IDS).map(([id, a]) => [id, liveChecked(a)]),
),
},
),
[
onToolSelect,
onTopAction,
onLeftToggle,
es.appearance.show_hidden_pins,
es.appearance.show_hidden_fields,
toggles,
activeTool,
doc,
selection,
app,
liveChecked,
],
);
const menus = useMemo(
() => applyHotkeyOverrides(menusRaw, hotkeyOverrides),
[menusRaw, hotkeyOverrides, applyHotkeyOverrides],
);
/**
* The tree the chain dispatches off, read through a ref: it is rebuilt on
* every render, and depending on it would tear the listener down and put it
* back on each keystroke's re-render. `useMenuHotkeys` holds one for the
* same reason.
*/
const menusRef = useRef<Menu[]>(menusRaw);
menusRef.current = menusRaw;
// The frame's single key chain, in ACTION_MANAGER::RunHotKey order: the
// context actions this canvas owns, then the menus. See ui/menu_hotkeys.ts.
//
// The dispatch is called from *inside* this listener rather than added
// beside it, which matters more here than anywhere else in the app: the
// event the menus must see is the one `remapEvent` produced, so a user's
// rebinding reaches a menu row exactly as it reaches a tool key.
useEffect(() => {
const onKey = (raw: KeyboardEvent) => {
// Hidden frames must not act on global hotkeys (editors stay mounted
// behind display:none; no stamp = standalone build, always active).
if ((document.body.dataset.activeView ?? 'schematic') !== 'schematic') return;
// `defaultPrevented` means someone already acted on this key - EXCEPT
// when it was our own browser suppressor, which runs in the capture phase
// and cancels every combo the app claims purely to stop the browser.
// Reading that as "handled" is what made every hotkey in the app stop
// working once the dispatcher landed (c4a00590).
if (raw.defaultPrevented && !wasBrowserSuppressed(raw)) return;
// tool_dispatcher.cpp:654-670 - an editable entry takes every key, a
// read-only one keeps Ctrl+C.
const target = raw.target as (FocusLike & { readOnly?: boolean; disabled?: boolean }) | null;
if (focusBlocksHotkey(target, raw)) return;
// The user's rebindings, applied before anything below sees the event: a
// key bound elsewhere arrives spelled as the action's *default* combo, and
// a cleared one arrives as null and stops here. See hotkey_bindings.ts —
// the chain below deliberately still matches on the defaults.
// Read off the manager rather than through a subscription: this effect is
// re-bound on a long dependency list already, and the map has to be the
// live one the moment the key is pressed, not the one this closure was
// built with.
const e = remapEvent(raw, app.settings.hotkeys);
if (!e) return;
if ((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === 'o') {
// Under the project manager eeschema's File menu starts at Save - New
// and Open belong to the launcher (menubar.cpp) - so this key has no
// row to answer from and stays here.
e.preventDefault();
promptOpen();
} else if ((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === 'y') {
// ACTIONS::redo. actions.cpp:292-302 binds Ctrl+Y off macOS and
// Ctrl+Shift+Z on it, so THIS is the platform default and the key the
// row now prints. The old comment had it exactly the wrong way round.
e.preventDefault();
redo();
} else if ((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === 'd') {
e.preventDefault();
duplicateSelection();
} else if (e.key === 'F3') {
// ACTIONS::findNext / findPrevious (F3 / Shift+F3).
e.preventDefault();
runLiveAction(e.shiftKey ? ACTIONS.findPrevious : ACTIONS.findNext);
} else if ((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === 'u' && !e.shiftKey) {
// ACTIONS::toggleUnits (Ctrl+U): COMMON_TOOLS::ToggleUnits, which remembers the last
// imperial unit (m_imperialUnit).
e.preventDefault();
runLiveAction(ACTIONS.toggleUnits);
syncFrameUnits();
} else if ((e.ctrlKey || e.metaKey) && e.key === ' ') {
// ACTIONS::cycleArcEditMode (Ctrl+Space): switch to a different method
// of editing arcs. The point editor reads the same preference, so this
// changes what dragging an arc's points does from the next drag on.
e.preventDefault();
app.settings.updateEeschema((s) => {
s.drawing.arc_edit_mode = incrementArcEditMode(s.drawing.arc_edit_mode as ArcEditMode);
});
} else if (e.key === 'Insert' && !e.altKey && !e.shiftKey && !e.ctrlKey && !e.metaKey) {
// SCH_ACTIONS::repeatDrawItem (Ins; F1 only on macOS, sch_actions.cpp:757-759), on the
// live tool as the canvas would have run it had it kept the focus.
e.preventDefault();
runLiveAction(SCH_ACTIONS.repeatDrawItem);
} else if (e.key === 'F1' && (e.ctrlKey || e.metaKey) && !e.altKey && !e.shiftKey) {
// ACTIONS::listHotKeys is AS_GLOBAL and HotkeyListHost binds Ctrl+F1
// once, above every frame. The arm stays so the bare-F1 zoom below - a
// *different* action that requires no modifiers - is still reached only
// when Ctrl is absent; without it, Ctrl+F1 would fall through and zoom.
e.preventDefault();
} else if ((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === 'e' && !e.shiftKey) {
// SCH_ACTIONS::editWithLibEdit (Ctrl+E) on a single selected symbol.
e.preventDefault();
if (selection.size === 1) {
const id = [...selection][0]!;
editSymbolInEditor(/^(.*):field\d+$/.exec(id)?.[1] ?? id);
}
} else if (e.key === 'Tab' && !e.ctrlKey && !e.metaKey && !e.altKey) {
// SCH_ACTIONS::nextNetItem / previousNetItem (Tab / Shift+Tab): SCH_SELECTION_TOOL's
// SelectNext / SelectPrevious step the frame's Net Navigator, wrapping.
e.preventDefault();
runLiveAction(e.shiftKey ? SCH_ACTIONS.previousNetItem : SCH_ACTIONS.nextNetItem);
} else if ((e.ctrlKey || e.metaKey) && e.shiftKey && e.key.toLowerCase() === 'g') {
// ACTIONS::toggleGridOverrides (Ctrl+Shift+G).
e.preventDefault();
onLeftToggle('toggleGridOverrides');
} else if (e.altKey && fastGridActionForKey(e.key) !== null) {
// ACTIONS::gridFast1 / gridFast2 / gridFastCycle, through the shared
// `COMMON_TOOLS` implementation. This had its own copy that read the
// two indices as 1-BASED — `min(max(v, 1), n) - 1` — where
// `GridPreset` clamps `idx` into `[0, size-1]` untouched, so with the
// stock settings Alt+1 selected 100 mil instead of 50 and Alt+2 50
// instead of 25. See `fastGridIndex`.
const action = fastGridActionForKey(e.key);
e.preventDefault();
app.settings.updateEeschema((st) => {
const idx = fastGridIndex(st.window.grid, action as FastGridAction);
if (idx !== null) st.window.grid.last_size_idx = idx;
});
// GridFast1/2/Cycle all reach `GridPreset( idx, true )`, so they post
// GridChangedByKeyEvent too (common/tool/common_tools.cpp:569-592).
gridFeedbackRef.current();
} else if (e.altKey && e.key === '3') {
// SCH_ACTIONS::selectNode (Alt+3), on the live selection tool.
e.preventDefault();
runLiveAction(SCH_ACTIONS.selectNode);
} else if ((e.ctrlKey || e.metaKey) && e.key === '4') {
// SCH_ACTIONS::selectConnection (Ctrl+4), on the live selection tool.
e.preventDefault();
runLiveAction(SCH_ACTIONS.selectConnection);
} else if (e.altKey && e.key.toLowerCase() === 's') {
// SCH_ACTIONS::swap (Alt+S), on the live edit tool.
e.preventDefault();
runLiveAction(SCH_ACTIONS.swap);
} else if (e.altKey && e.key === 'Backspace') {
// SCH_ACTIONS::leaveSheet (Alt+Backspace), same as Navigate Up.
e.preventDefault();
onTopAction('navUp');
} else if (e.key === 'Escape') {
// Abandoning a Sync Sheet Pins placement puts the rest of the queue
// back and reopens the dialog, rather than leaving it half-placed with
// nothing on screen to say so:
//
// if( m_dialogSyncSheetPin && m_dialogSyncSheetPin->CanPlaceMore() )
// { m_dialogSyncSheetPin->EndPlacement(); m_dialogSyncSheetPin->Show( true ); }
if (pastePending) setPastePending(null);
else if (pendingImage) {
setPendingImage(null);
setActiveTool('select');
} else if (pendingLabel) {
setPendingLabel(null);
setActiveTool('select');
} else if (placeLib || placeInstance) {
// `PlaceSymbol`'s cancel is TWO states, not one
// (sch_drawing_tools.cpp:324-344):
//
// if( symbol ) { cleanup();
// if( keepSymbol ) PostAction( cursorClick ); }
// else { m_frame->PopTool( aEvent ); break; }
//
// The first Escape runs `cleanup()` — drop the symbol, clear the
// selection — and LEAVES THE TOOL RUNNING; only a second one, with
// nothing on the cursor, pops it. Ours collapsed both into a single
// press, which put the tool away while KiCad keeps it armed for the
// next symbol. Two presses to leave the tool is upstream's answer,
// not a wrong count.
setPlaceLib(null);
// cleanup()'s own first line, ACTIONS::selectionClear.
setSelection(new Set());
// `if( keepSymbol ) PostAction( ACTIONS::cursorClick )` re-enters the
// chooser straight away; without it the tool waits, and it is the
// next click that reopens it (the click branch at :371-375).
setChooserDismissed(!placeFlags.current.keepSymbol);
} else {
// Popping the tool and clearing the selection are NOT alternatives.
// TWO tools see this one cancel event, because SCH_SELECTION_TOOL is
// not "the tool you go back to" — it runs the whole time, alongside
// whatever drawing tool is active:
//
// PlaceSymbol, nothing on the cursor:
// m_frame->PopTool( aEvent ); break; (:341-343)
// — pops the tool and never touches the selection.
// SCH_SELECTION_TOOL::Main, same event:
// if( !GetSelection().Empty() ) ClearSelection();
// (sch_selection_tool.cpp:1093-1096)
//
// So one Escape after a drop both puts the tool away AND unhighlights
// the symbol you just placed. Ours had these as `else if` arms of one
// chain, which made it take two presses to get back to a clean sheet.
if (activeTool !== 'select') setActiveTool('select');
// The selection tool's own arms, in its order: it clears the
// selection, and only when there was nothing to clear does the
// Escape fall through to the net highlight.
if (selection.size > 0) setSelection(new Set());
// "<ESC> clears net highlighting" (eeschema input.esc_clears_net_highlight): the
// live editor control's, as the canvas's own Escape reaches it.
else if (app.settings.eeschema.input.esc_clears_net_highlight)
runLiveAction(SCH_ACTIONS.clearHighlight);
}
} else if (!e.ctrlKey && !e.metaKey && !e.altKey) {
// KiCad single-key tool hotkeys (A=symbol, W=wire, …). Skip while
// typing, but a focused checkbox/radio isn't typing.
const tgt = e.target as HTMLElement | null;
const typing =
!!tgt &&
(tgt.tagName === 'TEXTAREA' ||
tgt.tagName === 'SELECT' ||
tgt.isContentEditable ||
(tgt.tagName === 'INPUT' &&
!/^(checkbox|radio|button|range)$/.test((tgt as HTMLInputElement).type)));
if (typing) return;
// R / Shift+R / X / Y, rotate & mirror the selection
// (SCH_ACTIONS::rotateCCW/rotateCW/mirrorH/mirrorV default hotkeys).
const txKey =
e.key.toLowerCase() === 'r'
? e.shiftKey
? 'rotateCW'
: 'rotateCCW'
: e.key.toLowerCase() === 'x'
? 'mirrorH'
: e.key.toLowerCase() === 'y'
? 'mirrorV'
: null;
if (txKey) {
e.preventDefault();
onTopAction(txKey);
return;
}
// ` = Highlight Net (the net under the cursor, not the click tool), ~ = clear
// highlighting: SCH_ACTIONS::highlightNet / clearHighlight, on the live frame.
if (e.key === '`') {
e.preventDefault();
runLiveAction(SCH_ACTIONS.highlightNet);
return;
}
if (e.key === '~') {
e.preventDefault();
runLiveAction(SCH_ACTIONS.clearHighlight);
return;
}
// Shift+Space, cycle the wire/bus line mode free → 90° → 45°
// (SCH_ACTIONS::lineModeNext; SCH_EDITOR_CONTROL::NextLineMode).
if (e.key === ' ' && e.shiftKey) {
e.preventDefault();
app.settings.updateEeschema((s) => {
s.drawing.line_mode = s.drawing.line_mode === 0 ? 1 : s.drawing.line_mode === 1 ? 2 : 0;
});
return;
}
// N / Shift+N, next/previous grid (ACTIONS::gridNext/gridPrev).
if (e.key.toLowerCase() === 'n') {
e.preventDefault();
app.settings.updateEeschema((s) => {
const n = s.window.grid.sizes.length;
if (n > 0)
s.window.grid.last_size_idx =
(s.window.grid.last_size_idx + (e.shiftKey ? n - 1 : 1)) % n;
});
// `OnGridChanged( true )` ends by posting GridChangedByKeyEvent.
gridFeedbackRef.current();
return;
}
// C = Unfold from Bus (SCH_ACTIONS::unfoldBus): SCH_LINE_WIRE_BUS_TOOL::UnfoldBus offers
// the bus under the cursor's members, as the canvas would have run it.
if (e.key.toLowerCase() === 'c') {
e.preventDefault();
runLiveAction(SCH_ACTIONS.unfoldBus);
return;
}
// D = Show Datasheet: SCH_INSPECTION_TOOL::ShowDatasheet on the live tool.
if (e.key.toLowerCase() === 'd') {
e.preventDefault();
runLiveAction(ACTIONS.showDatasheet);
return;
}
// O = Autoplace Fields: SCH_EDIT_TOOL::AutoplaceFields on the live tool.
if (e.key.toLowerCase() === 'o') {
e.preventDefault();
runLiveAction(SCH_ACTIONS.autoplaceFields);
return;
}
// A, P, W, B, Z, Q, J, L, H, S, T and I used to be dispatched here
// out of TOOL_HOTKEYS, and every one of them is also a Place menu row
// carrying the same key. The row is the declaration now; the map stays
// because `ui/hotkeys_inventory.ts` reads it for the Hotkey List.
if (dispatchMenuHotkey(menusRef.current, e, { target })) e.preventDefault();
}
// --- global: every other menu accelerator ---------------------------
// Reached only when no arm above claimed the key, which is
// ACTION_MANAGER::RunHotKey's order: a context action first, the
// AS_GLOBAL ones the menus render second.
else if (dispatchMenuHotkey(menusRef.current, e, { target })) e.preventDefault();
};
window.addEventListener('keydown', onKey);
return () => window.removeEventListener('keydown', onKey);
}, [
undo,
redo,
save,
promptOpen,
selection,
onUpdatePcb,
editSymbolInEditor,
activeTool,
onToolSelect,
onTopAction,
onLeftToggle,
libById,
pendingLabel,
pendingImage,
placeLib,
placeInstance,
pastePending,
duplicateSelection,
app,
remapEvent,
runLiveAction,
syncFrameUnits,
]);
const fmt = (iu: number): string => {
const mm = iuToMM(iu);
if (units === 'mm') return `${mm.toFixed(4)}`;
if (units === 'mils') return `${(mm / 0.0254).toFixed(2)}`;
return `${(mm / 25.4).toFixed(4)}`;
};
/**
* FRAME_FOOTPRINT_CHOOSER, opened by the Footprint field's PG_FPID_EDITOR
* button. `OnEvent`'s wxEVT_BUTTON branch shows it modally on the cell's
* current text and, on OK, writes the picked fpid back through the property
* (pg_editors.cpp:556-586) - so the commit callback is the CELL's, not a
* separate edit path.
*/
const [fpChooser, setFpChooser] = useState<{
current: string;
commit: (picked: string) => void;
/** The symbol's `ki_fp_filters`, split — MAIL_SYMBOL_NETLIST's half. */
fpFilters: readonly string[];
/** Its pin count, the other half. */
pinCount?: number;
/** Closed without a pick. */
cancel?: () => void;
} | null>(null);
// Message-panel rows (EDA_MSG_PANEL): what the frame's SetMsgPanel holds - the live tools'
// SCH_INSPECTION_TOOL::UpdateMessagePanel fills it on every selection change, from the item's
// GetMsgPanelInfo.
const [msgPanelItems, setMsgPanelItems] = useState<MsgPanelItem[]>([]);
useEffect(() => {
const frame = schFrameRef.current!;
frame.SetMsgPanelSink((aItems) => {
const rows = aItems.map((i) => ({ upper: i.GetUpperText(), lower: i.GetLowerText() }));
// The same rows again re-render nothing: UpdateMessagePanel runs on every selection event,
// and a fresh array for an unchanged panel set off a render that raised another one.
setMsgPanelItems((prev) =>
prev.length === rows.length &&
prev.every((r, i) => r.upper === rows[i]!.upper && r.lower === rows[i]!.lower)
? prev
: rows,
);
});
return () => frame.SetMsgPanelSink(null);
}, []);
/**
* The frame title, `SCH_EDIT_FRAME::updateTitle`
* (eeschema/sch_edit_frame.cpp:1819-1862), built by the shared rule rather
* than restated here — see `frame_title.ts`.
*
* The document half is the CURRENT sheet's file, so descending into a
* sub-sheet renames the title; the bracket is that sheet's
* `PathHumanReadable( false, true )`, which is seeded with the ROOT file's
* base name and is therefore suppressed on the root sheet.
*/
const schTitle = useMemo(() => {
const rootBase = sheetTree ? fileBaseName(sheetTree.file) : '';
const here = sheetInstanceRefs.find((r) => r.path === currentPath)?.namePath ?? '/';
const sheetNames = here.split('/').filter(Boolean);
return schFrameTitle({
fileName: doc ? currentFile : null,
sheetPath: rootBase === '' ? '' : pathHumanReadable(rootBase, sheetNames),
modified: dirty,
readOnly,
});
}, [doc, currentFile, sheetTree, sheetInstanceRefs, currentPath, dirty, readOnly]);
// The browser tab. Every other editor claims it; the schematic did not, so
// whichever view rendered last kept the tab's name forever.
useDocumentTitle('schematic', formatTitle(SCH_FRAME_NAME, schTitle.document, dirty));
// A load failure before any document exists is fatal; once a document is open,
// a bad Open just shows a dismissible banner and leaves the current sheet intact.
if (!doc) {
return error ? (
<pre style={{ color: 'crimson', padding: 16 }}>Failed to load schematic: {error}</pre>
) : (
<div className="ze-app sch-theme">
<ProgressDialog title="Load Schematic" label={loading ?? 'Loading schematic...'} />
</div>
);
}
const _title =
currentFile !== DEFAULT_FILE ? currentFile : (fileName ?? doc.titleBlock?.title ?? 'Root');
// Hierarchy-navigation buttons grey out when there's nowhere to go, matching
// KiCad's SCH_NAVIGATE_TOOL enable conditions (CanGoBack/Forward, CanGoUp):
// on a flat/root schematic Navigate Up has no parent to enter, so it disables.
const navDisabled = new Set<string>();
if (!navTool.current.canGoBack()) navDisabled.add('navBack');
if (!navTool.current.canGoForward()) navDisabled.add('navFwd');
if (parentPath(currentPath) === null) navDisabled.add('navUp');
// The toolbar's Group / Ungroup grey out when they can't act (GROUP_TOOL::
// update): Group needs >= 2 selected items, Ungroup needs a group in the
// selection. Add / Remove are right-click-only, gated in the context menu.
if (selection.size < 2) navDisabled.add('group');
if (!selectionHasGroup(doc, selection)) navDisabled.add('ungroup');
const onDrop = (e: React.DragEvent) => {
e.preventDefault();
const files = [...e.dataTransfer.files];
if (files.length > 1) {
// Several files at once = a project drop: load them all as one hierarchy.
Promise.all(files.map(async (f) => ({ name: f.name, text: await f.text() })))
.then(loadProject)
.catch((err) => setError(String(err)));
} else if (files[0]) {
openFile(files[0]);
}
};
return (
<div
ref={appRef}
className="ze-app sch-theme"
onDrop={onDrop}
onDragOver={(e) => e.preventDefault()}
>
{/* HOTKEY_CYCLE_POPUP: a wxSTAY_ON_TOP window over the whole frame. */}
{hotkeyPopup.node}
{syncPeers.length > 0 && (
<button
type="button"
className="ze-presence-badge"
title={syncPeers.map((p) => `${p.view} · ${p.sheetPath ?? '/'}`).join('\n')}
onClick={() => setPresencePanelOpen((v) => !v)}
>
{syncPeers.length === 1 ? '1 other viewer' : `${syncPeers.length} other viewers`}
</button>
)}
{presencePanelOpen && (
<PresencePanel
me={{
peerId: syncTransport.current?.peerId ?? '',
role: myRole,
displayName: myDisplayName,
}}
peers={syncPeers}
onSetRole={(peerId, role) =>
syncTransport.current?.publish({ kind: 'role-assign', toPeerId: peerId, role })
}
onClose={() => setPresencePanelOpen(false)}
/>
)}
{copyAsOpen && (
<SaveAsDialog
title="Save Current Sheet Copy As"
// `wxFileDialog( m_frame, _( "Schematic Files" ), curr_fn.GetPath(),
// curr_fn.GetFullName(), ... )`
// (sch_editor_control.cpp, SaveCurrSheetCopyAs). Both arguments come
// off the sheet's OWN file: it opens in the folder that file already
// sits in, and suggests that file's own name UNCHANGED - upstream
// appends no "_copy", the word is in the command's FriendlyName
// (sch_actions.cpp:1623) and nowhere else.
//
// Ours passed a name and no directory, so it opened at the account
// root listing every project.
initialName={basename(copyAsSeed)}
{...(projectName ? { projectDir: `/${projectName}` } : {})}
{...(projectName ? { initialPath: sheetDirOf(projectName, copyAsSeed) } : {})}
filters={[kicadSchematicWildcard()]}
onDone={(path) => {
setCopyAsOpen(false);
if (path === null) return; // wxID_CANCEL
saveCurrSheetCopyTo(path);
}}
/>
)}
{openDlgOpen && (
<OpenFileDialog
filters={[kicadSchematicWildcard()]}
onDone={(file) => {
setOpenDlgOpen(false);
if (!file) return; // wxID_CANCEL
const leaf = file.path.split('/').filter(Boolean).pop() ?? file.path;
void loadText(file.text, leaf);
}}
/>
)}
<input
ref={imageInputRef}
type="file"
accept="image/*"
style={{ display: 'none' }}
onChange={(e) => {
const f = e.target.files?.[0];
if (f) onImageFile(f);
e.target.value = '';
}}
/>
{error && (
<div className="ze-error-banner" onClick={() => setError(null)} title="Dismiss">
{error}, click to dismiss
</div>
)}
<MenuBar
menus={menus}
leftSlot={<HomeLink onClick={onExitToHome} />}
title={
<>
<b>
{schTitle.modified}
{schTitle.document}
</b>
{schTitle.separator}
{schTitle.frameName}
</>
}
/>
<Toolbar
entries={schTopBar}
app="eeschema"
orientation="horizontal"
disabledIds={withSaveEnablement(navDisabled, dirty)}
// Almost everything up here is a plain action, but the zoom tool is not:
// it is an AF_ACTIVATE tool that keeps running, and its button stays
// checked for as long as it does.
//
// mgr->SetConditions( ACTIONS::zoomTool, CHECK( cond.CurrentTool( ACTIONS::zoomTool ) ) );
//
// Passing the current tool is enough to get that: no other id on this
// toolbar is a tool id, so `zoomTool` is the only one that can match.
activeTool={activeTool}
onActivate={onTopAction}
/>
<div className="ze-body">
{(() => {
// Which panes are on screen. The ORDER they are drawn in is not a
// table: it is `schLeftDockLayout`, one wxAUI Update, run below.
const growShown = {
netNavigator: toggles.has('showNetNavigator') && !!doc,
hierarchy: toggles.has('showHierarchy'),
properties: toggles.has('showProperties'),
};
const paneShown: Record<SchLeftPane, boolean> = {
...growShown,
// Not a toggle of its own: `updateSelectionFilterVisbility` ORs the
// other three panes. See `schSelectionFilterShown`.
// `updateSelectionFilterVisbility` ORs the other three and writes
// the answer every time it runs, so a close only holds until the
// next recompute — which is exactly what the latch below does.
selectionFilter: schSelectionFilterShown(growShown) && !selectionFilterClosed,
};
// One `wxAuiManager::Update()`: sort the shown panes by `dock_pos`,
// draw them in that order, and renumber them for the next time. The
// pane opened FIRST holds the top of the column, which is what
// upstream does and what a fixed order table could not express.
//
// Writing the ref here is safe because the pass is idempotent: a
// second render with the same panes shown produces the same order
// and the same numbers.
const dockLayout = schLeftDockLayout(dockPosRef.current, paneShown);
dockPosRef.current = dockLayout.dockPos;
dockPosSaveRef.current = dockLayout.dockPos;
// Adjacent visible grow panes get a drag sash between them, top pane
// resizes (KiCad's wxAUI sash chain); Selection Filter (prop=0 in
// KiCad's perspective) never grows, so it's never in this list.
// Search is not here either — it is the BOTTOM dock, below the
// canvas, and its sash is its own (`SCH_BOTTOM_DOCK`).
const visibleGrowKeys: string[] = dockLayout.order.filter(schPaneGrows);
const sashAfter = (key: string): JSX.Element | null =>
visibleGrowKeys.indexOf(key) < visibleGrowKeys.length - 1 ? (
<div
className="ze-splitter horizontal"
onMouseDown={(e) => startPanelResize(key, e)}
title="Drag to resize"
/>
) : null;
const heightStyle = (key: string): React.CSSProperties | undefined =>
panelHeights[key] != null ? { flex: `0 0 ${panelHeights[key]}px` } : undefined;
return (
// Search is no longer a term: it does not live in this column.
(paneShown.properties || paneShown.hierarchy || paneShown.netNavigator) && (
<>
<div
className="ze-leftdock sch-leftdock"
// The pane WINDOW is `leftDockWidth` wide; wxAUI's 1px pane
// border sits outside it on both sides ([px] the tree's own
// frame runs x=67..366 with #292929 at 66 and 367), so the
// column is the window plus two borders.
style={{ width: `calc(${leftDockWidth}px + 2 * var(--aui-pane-border-size))` }}
>
{/* The docked panes, in the order the wxAUI Update above
sorted them into — see `panes.ts`. Only the ORDER is
data; each pane's contents stay inline. */}
{dockLayout.order.map((paneKey) => (
<Fragment key={paneKey}>
{paneKey === 'netNavigator' && paneShown.netNavigator && (
<>
<div className="ze-panel grow" style={heightStyle('netNavigator')}>
<div className="ze-panel-header">
<span>Net Navigator</span>
{/* `.CloseButton( true )` on every one of these
palettes. Closing a pane is the same state the
View > Panels check item drives, which is why it
goes through the same toggle. */}
<button
type="button"
className="ze-pane-close"
onClick={() => onLeftToggle('showNetNavigator')}
title="Close"
>
⊠
</button>
</div>
<div className="ze-panel-body">
<NetNavigatorPanel frame={schFrameRef.current!} />
</div>
</div>
{sashAfter('netNavigator')}
</>
)}
{paneKey === 'hierarchy' && paneShown.hierarchy && (
<>
<div className="ze-panel grow" style={heightStyle('hierarchy')}>
<div className="ze-panel-header">
<span>Schematic Hierarchy</span>
{/* `.CloseButton( true )` on every one of these
palettes. Closing a pane is the same state the
View > Panels check item drives, which is why it
goes through the same toggle. */}
<button
type="button"
className="ze-pane-close"
onClick={() => onLeftToggle('showHierarchy')}
title="Close"
>
⊠
</button>
</div>
{/* HIERARCHY_TREE fills the pane: `sizer->Add( m_tree,
1, wxEXPAND, wxBORDER_NONE )` (hierarchy_pane.cpp:57)
— no inset, so the control's own frame is the
pane's edge. */}
<div className="ze-panel-body ze-hiertree">
{sheetTree &&
renderSheetNode(
sheetTree,
0,
currentPath,
switchSheet,
collapsedSheets,
setCollapsedSheets,
hierarchyNetMark,
)}
</div>
</div>
{sashAfter('hierarchy')}
</>
)}
{paneKey === 'properties' && paneShown.properties && (
<>
<div className="ze-panel grow" style={heightStyle('properties')}>
<div className="ze-panel-header">
<span>Properties</span>
{/* `.CloseButton( true )` on every one of these
palettes. Closing a pane is the same state the
View > Panels check item drives, which is why it
goes through the same toggle. */}
<button
type="button"
className="ze-pane-close"
onClick={() => onLeftToggle('showProperties')}
title="Close"
>
⊠
</button>
</div>
<div className="ze-panel-body">
{/* The empty and multi-selection captions are
PROPERTIES_PANEL's own (properties_panel.cpp:
196-210), so the panel renders them rather
than the frame swapping in a placeholder. */}
{/* SCH_PROPERTIES_PANEL on the live model. */}
<LiveSchPropertiesPanel frame={schFrameRef.current!} units={units} />
</div>
</div>
{sashAfter('properties')}
</>
)}
{/* `PANEL_SCH_SELECTION_FILTER`, the same widget the Symbol
Editor builds (`widgets/panel_sch_selection_filter_ui.tsx`). What was
here was a private copy that (a) rendered a visible
"Locked items" row — `m_cbLockedItems->Hide()`,
`panel_sch_selection_filter_base.cpp:28`, hides it in
BOTH frames — and (b) put "All items" on a row of its
own above the grid, which shifted every pair one cell
left of upstream's `wxGBPosition`s. */}
{paneKey === 'selectionFilter' && paneShown.selectionFilter && (
<SelectionFilterPanel
frame="FRAME_SCH"
filter={selFilter}
onChange={setSelFilter}
onClose={() => setSelectionFilterClosed(true)}
/>
)}
</Fragment>
))}
</div>
{/* wxAUI's sash: a sibling of the dock, 5px of sash colour
between the pane border and the left toolbar — the same
`<DockSash>` pcbnew's Properties dock carries, and for the
same reason: it is the sash, not a rule, that separates a
dock from the toolbar beside it. The clamps are the
Properties pane's MinSize (240) and the width past which
the canvas suffers. */}
<DockSash
edge="right"
width={leftDockWidth}
min={240}
max={800}
onResize={setLeftDockWidth}
/>
</>
)
);
})()}
<Toolbar
entries={schLeftBar}
app="eeschema"
orientation="vertical"
side="left"
toggled={toggles}
onActivate={onLeftToggle}
/>
{/* The centre pane and the docks of LAYER 0 around it. Only the Search
pane is in that layer here, `.Bottom()` with no `.Layer()` call
(sch_edit_frame.cpp:290-292), and wxAUI nests docks outward by
layer — so it is as wide as the canvas, with the left dock (layer 3)
and both toolbars (layer 2) running full height past it and the
message panel (layer 6) below the lot. See `SCH_BOTTOM_DOCK`. */}
<div className="ze-canvas-col">
{/* `CreateInfoBar()` puts WX_INFOBAR in its own AUI pane ABOVE the canvas. These strips
were the wrap's children, and the wrap's <canvas> is `position: absolute; inset: 0`,
so the GL canvas painted over them the moment the sheet drew - the demo banner
vanished as the schematic appeared. In the column they take their own height and
the wrap, flex: 1, gets the rest. */}
{readOnlyNotice}
{myRole === 'viewer' && (
<ReadOnlyNotice message="You have view-only access to this project. Ask the owner for edit access to make changes." />
)}
{/* WX_INFOBAR: the strip a tool posts an error into, dismissed with
its ✕ or by the next successful action. */}
{infoBar && (
<div className="ze-infobar">
{infoBar}
<span
className="x"
title="Close"
onClick={() => setInfoBar(null)}
style={{ marginLeft: 'auto', cursor: 'default' }}
>
✕
</span>
</div>
)}
<div className="ze-canvas-wrap">
<canvas
ref={glCanvasRef}
// biome-ignore lint/a11y/noNoninteractiveTabindex: the canvas takes the keys, as the wxGLCanvas does
tabIndex={0}
style={{
position: 'absolute',
inset: 0,
outline: 'none',
width: '100%',
height: '100%',
}}
/>
{toolPopup && (
<ContextMenu
x={toolPopup.x}
y={toolPopup.y}
items={actionMenuItems(toolPopup.menu)}
onClose={() => {
const close = toolPopup.onClose;
setToolPopup(null);
// wx runs the chosen row while PopupMenu is still open and returns after, so
// the close half runs once the row's handler has.
queueMicrotask(close);
}}
/>
)}
{updateFromPcbDialog && schFrameRef.current && (
<DIALOG_UPDATE_FROM_PCB
frame={schFrameRef.current}
// The commit touched every sheet the board names; hand them back to the window.
onUpdated={adoptAllLiveScreens}
onClose={() => {
setHeadlessPcbNetlistProvider(() => null);
setUpdateFromPcbDialog(null);
updateFromPcbDialog.resolve(wxID_CANCEL);
}}
/>
)}
{/* Hidden, not closed, while a placement queue is running: upstream
calls Hide() and Show(true) around the placement tool. */}
{syncDialog && (
<DialogSyncSheetPins
dlg={syncDialog}
onClose={() => {
// The dialog's OnClose; the tool forgets it as its unique_ptr would.
syncDialog.Hide();
const tool = schFrameRef.current?.GetToolManager()?.GetTool(SCH_DRAWING_TOOLS);
if (tool) tool.m_dialogSyncSheetPin = null;
setSyncDialog(null);
}}
/>
)}
{symLibTableOpen && (
<DialogSymLibTable
projectFiles={rawFiles}
globalLibraries={hostedSymbolLibs}
globalBase={symbolsBase()}
onSave={(rows, files) => {
saveProjectSymLibTable(rows, files);
setSymLibTableOpen(false);
}}
onClose={() => setSymLibTableOpen(false)}
/>
)}
{rescueCandidates && (
<DialogRescueEach
candidates={rescueCandidates}
instancesOf={rescueInstances}
// `aAskShowAgain = !aRunningOnDemand`, and ours is always on
// demand: the automatic caller lives in the legacy `.sch`
// branch of the loader, which we never take.
// `aAskShowAgain = !aRunningOnDemand`: the button is offered
// only by the prompt the user did not ask for.
askShowAgain={!rescueOnDemand}
inputPrefs={inputPrefs}
onOk={applyRescues}
onCancel={() => {
// `OnCancelClick` clears the chosen candidates, and
// `RescueProject` then reports that nothing was rescued.
setRescueCandidates(null);
setRescueMessage('No symbols were rescued.');
}}
onNeverShowAgain={() => {
setRescueCandidates(null);
app.settings.updateEeschema((st) => {
st.system.never_show_rescue_dialog = true;
});
}}
/>
)}
{rescueMessage && (
<MessageDialogOk
caption="Project Rescue Helper"
message={rescueMessage}
onClose={() => setRescueMessage(null)}
/>
)}
{loadRepairedMessage && (
<MessageDialogOk
caption={INFO_CAPTION}
message={loadRepairedMessage}
onClose={() => setLoadRepairedMessage(null)}
/>
)}
{ercDialog?.shown && <DialogErc dialog={ercDialog.dialog} />}
{fieldsTable?.shown && <DialogSymbolFieldsTable dialog={fieldsTable.dialog} />}
{fieldsTextEntry && (
<WX_TEXT_ENTRY_DIALOG
label={fieldsTextEntry.message}
caption={fieldsTextEntry.caption}
defaultValue={fieldsTextEntry.value}
onResult={(text) => {
fieldsTextEntry.resolve(text);
setFieldsTextEntry(null);
}}
/>
)}
{fieldsList && (
<EdaListDialog
title={fieldsList.caption}
headers={['Presets']}
rows={fieldsList.items.map((name) => ({ value: name, cells: [name] }))}
listLabel={fieldsList.label}
onResult={(picked) => {
fieldsList.resolve(picked);
setFieldsList(null);
}}
/>
)}
{fieldsUnsaved && (
<UnsavedChangesDialog
message={fieldsUnsaved.message}
onResult={(result) => {
fieldsUnsaved.resolve(result);
setFieldsUnsaved(null);
}}
/>
)}
{ercComment && (
<WX_TEXT_ENTRY_DIALOG
label=""
caption="Exclusion Comment"
defaultValue={ercComment.initial}
extraWidth
onResult={(text) => {
ercComment.resolve(text);
setErcComment(null);
}}
/>
)}
{ercYesNoCancel && (
<MessageDialogYesNoCancel
caption="Delete All Markers"
message="Delete exclusions too?"
icon="question"
defaultButton="yes"
labels={{ yes: 'Errors and Warnings Only', no: 'Errors, Warnings and Exclusions' }}
onResult={(r) => {
ercYesNoCancel.resolve(r);
setErcYesNoCancel(null);
}}
/>
)}
{ercSaveReport && (
// DIALOG_ERC::OnSaveReport (dialog_erc.cpp:1157-1159): wxFileDialog(
// _( "Save Report File" ), Prj().GetProjectPath(), fn.GetFullName(),
// ReportFileWildcard() | JsonFileWildcard(), wxFD_SAVE | wxFD_OVERWRITE_PROMPT ).
<SaveAsDialog
title="Save Report File"
initialName={ercSaveReport.defaultName}
filters={[reportFileWildcard(), jsonFileWildcard()]}
{...(projectName
? { projectDir: `/${projectName}`, initialPath: `/${projectName}` }
: {})}
onDone={(path) => {
ercSaveReport.resolve(path);
setErcSaveReport(null);
}}
/>
)}
{/* DIALOG_SCH_FIND, modeless: SCH_BASE_FRAME::ShowFindReplaceDialog builds it. */}
<SchFindDialogHost frame={schFrameRef.current!} frameType="FRAME_SCH" />
{annotateOpen && (
<DialogAnnotate
settings={app.settings}
// Sort order, numbering method and start number are project
// settings (SCHEMATIC_SETTINGS), seed from Schematic Setup >
// Annotation like DIALOG_ANNOTATE::TransferDataToWindow.
initial={{
order: setup.annotation.sortOrder,
algo:
setup.annotation.numbering === 'sheetX100'
? 'sheet_100'
: setup.annotation.numbering === 'sheetX1000'
? 'sheet_1000'
: 'incremental',
startNumber: setup.annotation.firstFreeAfter,
}}
messages={annotateMessages}
onAnnotate={runAnnotate}
onClear={runClearAnnotation}
onClose={(s) => {
// ~DIALOG_ANNOTATE: write changed settings back to the project.
const numbering =
s.algo === 'sheet_100'
? 'sheetX100'
: s.algo === 'sheet_1000'
? 'sheetX1000'
: 'firstFree';
if (
s.order !== setup.annotation.sortOrder ||
numbering !== setup.annotation.numbering ||
s.startNumber !== setup.annotation.firstFreeAfter
) {
commitSetup({
...setup,
annotation: {
...setup.annotation,
sortOrder: s.order,
numbering,
firstFreeAfter: s.startNumber,
},
});
}
// OnClose destroys the dialog, so its messages go with it.
setAnnotateMessages([]);
setAnnotateOpen(false);
}}
/>
)}
{libIdDialog && (
<DialogEditSymbolsLibId
dlg={libIdDialog.dlg}
onClose={(aResult) => {
setLibIdDialog(null);
libIdDialog.resolve(aResult);
}}
/>
)}
{libIdChoice && (
<SingleChoiceDialog
caption={libIdChoice.caption}
message={libIdChoice.message}
choices={libIdChoice.choices.map((c) => ({ value: c, label: c }))}
onResult={(aPick) => {
setLibIdChoice(null);
libIdChoice.resolve(aPick);
}}
/>
)}
{changeSymbolsDialog && (
<DialogChangeSymbols
dlg={changeSymbolsDialog.dlg}
onClose={() => {
setChangeSymbolsDialog(null);
changeSymbolsDialog.resolve(wxID_CANCEL);
}}
/>
)}
{symbolChooser && (
<SymbolChooserFrame
preselect={symbolChooser.preselect}
historyList={sSymbolHistoryList}
onOk={(libId) => {
setSymbolChooser(null);
symbolChooser.resolve(libId);
}}
onCancel={() => {
setSymbolChooser(null);
symbolChooser.resolve(null);
}}
/>
)}
{globalEditDialog && (
<DialogGlobalEditTextAndGraphics
dlg={globalEditDialog.dlg}
initial={globalEditDialog.shown}
units={units}
onClose={() => {
setGlobalEditDialog(null);
globalEditDialog.resolve(wxID_OK);
}}
/>
)}
{incrementDialog && (
<DIALOG_INCREMENT_ANNOTATIONS_BASE
values={incrementDialog.values}
onOk={() => {
setIncrementDialog(null);
incrementDialog.resolve(wxID_OK);
// The tool's commit can touch every sheet; once it has run (after this answer's
// microtasks), hand every live screen back to the window's records.
setTimeout(adoptAllLiveScreens, 0);
}}
onCancel={() => {
setIncrementDialog(null);
incrementDialog.resolve(wxID_CANCEL);
}}
/>
)}
{pageDialog && (
// DIALOG_EESCHEMA_PAGE_SETTINGS, not DIALOG_PAGES_SETTINGS: the
// base class hides the sheet tallies and all fourteen "Export to
// other sheets" boxes (dialog_page_settings.cpp:169-185) and this
// subclass is the only thing that shows them
// (dialog_eeschema_page_settings.cpp:87-102). pcbnew and
// pl_editor open the base class and get neither.
<DialogEeschemaPageSettings
units={units}
value={pageDialog.dlg.TransferDataToWindow()}
sheetCount={pageDialog.dlg.SheetTallies().count}
sheetNumber={pageDialog.dlg.SheetTallies().number}
wksFileName={sheetRefName}
sheet={activeSheet}
projectDir={projectName ? `/${projectName}` : null}
stored={{
paper: es.page_settings.export_paper,
date: es.page_settings.export_date,
rev: es.page_settings.export_revision,
title: es.page_settings.export_title,
company: es.page_settings.export_company,
comments: es.page_settings.export_comments,
}}
onStoreExports={(next) =>
app.settings.updateEeschema((cfg) => {
cfg.page_settings.export_paper = next.paper;
cfg.page_settings.export_revision = next.rev;
cfg.page_settings.export_date = next.date;
cfg.page_settings.export_title = next.title;
cfg.page_settings.export_company = next.company;
cfg.page_settings.export_comments = [...next.comments];
})
}
onOk={(next, exports, drawingSheet, drawingSheetName) => {
pageDialog.dlg.TransferDataFromWindow(next, exports);
// Exported fields changed other screens; the window's records follow them
// until saving reads the live model.
const exported =
exports.paper ||
exports.date ||
exports.rev ||
exports.title ||
exports.company ||
exports.comments.some(Boolean);
const frame = schFrameRef.current;
if (exported && frame) {
const all: SCH_SCREEN[] = [];
const screens = new SCH_SCREENS(frame.Schematic().Root());
for (let sc = screens.GetFirst(); sc; sc = screens.GetNext()) all.push(sc);
adoptLiveScreensRef.current(all);
}
adoptDrawingSheet(drawingSheet, drawingSheetName);
setPageDialog(null);
pageDialog.resolve(wxID_OK);
}}
onCancel={() => {
setPageDialog(null);
pageDialog.resolve(wxID_CANCEL);
}}
/>
)}
{printOpen && (
<DialogPrint
settings={app.settings}
onPrint={doPrint}
themeId={es.appearance.color_theme}
onClose={() => setPrintOpen(false)}
/>
)}
{pasteSpecialDialog && (
<DialogPasteSpecial
mode={pasteSpecialDialog.dlg.pasteMode}
onOk={(chosen: PasteSpecialMode) => {
pasteSpecialDialog.dlg.pasteMode = chosen;
setPasteSpecialDialog(null);
pasteSpecialDialog.resolve(wxID_OK);
}}
onCancel={() => {
setPasteSpecialDialog(null);
pasteSpecialDialog.resolve(wxID_CANCEL);
}}
/>
)}
{plotOpen && (
<DialogPlot
settings={app.settings}
themeId={es.appearance.color_theme}
onPlot={doPlot}
onClose={() => setPlotOpen(false)}
/>
)}
{setupOpen && setupValue && (
<DialogSchematicSetup
value={setupValue}
onOk={(next) => {
commitSetup(next);
// The Embedded Files page edits the schematic itself (EMBEDDED_FILES lives in
// the .kicad_sch, not the project file). Compressing an added file needs the
// zstd codec, which only a board load has initialised so far.
const frame = schFrameRef.current;
if (frame) {
void EMBEDDED_FILES.InitCodec().then(() => {
if (
PANEL_EMBEDDED_FILES.TransferDataFromWindow(
next.embeddedFiles,
frame.Schematic().GetEmbeddedFiles(),
)
)
frame.OnModify();
});
}
setSetupOpen(false);
}}
onCancel={() => setSetupOpen(false)}
onExportEmbedded={(files) => {
// onExportFiles: write every embedded file out, here as
// downloads; pending rows export their picked bytes directly.
const embedded = schFrameRef.current?.Schematic().GetEmbeddedFiles();
if (!embedded) return;
void EMBEDDED_FILES.InitCodec().then(() => {
for (const f of files) {
const file = f.pendingBytes ? null : embedded.GetEmbeddedFile(f.name);
if (file && file.decompressedData.length === 0)
EMBEDDED_FILES.DecompressAndDecode(file);
const bytes = f.pendingBytes ?? file?.decompressedData;
if (bytes) downloadBlob(new Blob([bytes.slice().buffer]), f.name);
}
});
}}
/>
)}
{netlistOpen && doc && (
<DialogExportNetlist
doc={doc}
libById={libById}
baseName={outputBaseName()}
{...(projectName ? { projectName } : {})}
onOutputFile={onOutputFile}
onClose={() => setNetlistOpen(false)}
/>
)}
{/* Assign Footprints (cvpcb): assignments apply as Footprint field
edits through the same per-sheet pathway as the fields table. */}
{assignFpOpen && (
<AssignFootprints
docs={liveDocs()}
// The netlist CVPCB works on is this design's sheets, in
// hierarchy order, not every .kicad_sch in the project folder.
files={assignFpFiles}
projectFootprints={projectFootprintFiles}
kiway={kiway}
onSaveLibTable={saveProjectFpLibTable}
onSaveEquFiles={saveProjectEquFiles}
onClose={() => setAssignFpOpen(false)}
/>
)}
</div>
{toggles.has('showSearch') && doc && (
<>
{/* The sash sits ABOVE the pane here, so dragging it down has to
SHRINK the pane below rather than grow the one above —
`startPanelResize`'s inverse. */}
<div
className="ze-splitter horizontal"
onMouseDown={startBottomDockResize}
title="Drag to resize"
/>
<div className="ze-bottomdock sch-bottomdock" style={bottomDockStyle}>
<div className="ze-panel">
<div className="ze-panel-header">Search</div>
<div className="ze-panel-body">
{schFrameRef.current && (
<SchSearchPane
frame={schFrameRef.current}
schematic={schFrameRef.current.Schematic()}
units={units}
menuState={{
selectionZoom: app.settings.common.search_pane.selection_zoom,
searchHiddenFields: app.settings.common.search_pane.search_hidden_fields,
searchMetadata: app.settings.common.search_pane.search_metadata,
}}
onMenuStateChange={(next) =>
app.settings.updateCommon((c) => {
c.search_pane.selection_zoom = next.selectionZoom;
c.search_pane.search_hidden_fields = next.searchHiddenFields;
c.search_pane.search_metadata = next.searchMetadata;
})
}
/>
)}
</div>
</div>
</div>
</>
)}
</div>
<Toolbar
entries={schRightBar}
app="eeschema"
orientation="vertical"
side="right"
activeTool={activeTool}
onActivate={onRightToolbar}
/>
{/* The Design Blocks pane: Right().Layer( 3 ), outside the right toolbar
(defaultDesignBlocksPaneInfo, eeschema_settings.cpp:132-151). */}
{toggles.has('showDesignBlocks') && (
<>
<DockSash
edge="left"
width={dbPaneWidth}
min={240}
max={800}
onResize={setDbPaneWidth}
/>
<SchDesignBlockPane
pane={designBlocksPane}
libs={schFrameRef.current!.Prj().DesignBlockLibs()}
manager={Pgm().GetLibraryManager()}
pinned={schFrameRef.current!.Prj().GetProjectFile().m_PinnedDesignBlockLibs}
width={dbPaneWidth}
onClose={() => onLeftToggle('showDesignBlocks')}
onChosen={() => runLiveAction(SCH_ACTIONS.placeDesignBlock)}
renderPreview={(aDesignBlock) => (
<SchDesignBlockPreviewWidget designBlock={aDesignBlock} theme={theme} />
)}
sortMode={dbSortMode}
sashPosV={dbSashPosV}
onSortModeChanged={setDbSortMode}
onSashPosVChanged={setDbSashPosV}
openLibs={dbOpenLibs}
onToggleLibrary={(aNode, aOpen) =>
setDbOpenLibs((prev) =>
aOpen
? [...prev.filter((n) => n !== aNode.libNickname), aNode.libNickname]
: prev.filter((n) => n !== aNode.libNickname),
)
}
// LIB_TREE::onItemContextMenu with SCH_DESIGN_BLOCK_CONTROL as the adapter's
// context menu tool: its menu, evaluated for the row the tree selected.
onItemContextMenu={(aNode) => {
const frame = schFrameRef.current!;
// The row under the pointer is the current one, as the tree selected it.
designBlocksPane.SetCurrentTreeNode(aNode);
const tool = frame.GetToolManager()!.GetTool(SCH_DESIGN_BLOCK_CONTROL);
if (!tool) return;
const menu = tool.GetToolMenu().GetMenu();
menu.Evaluate(frame.GetCurrentSelection());
menu.UpdateAll();
menu.ClearDirty();
frame.PopupMenu(menu, () => {});
}}
/>
</>
)}
</div>
{/* EDA_DRAW_FRAME hosts a message panel above the 8-field status bar:
a single selected item's GetMsgPanelInfo rows; anything else clears
it (SCH_INSPECTION_TOOL::UpdateMessagePanel). */}
<MsgPanel items={msgPanelItems} testId="sch-message-panel" />
{/* KISTATUSBAR's 8 fields (eda_draw_frame.cpp): message (grows), the
net-highlight text lands here (UpdateNetHighlightStatus) | Z zoom |
absolute X/Y | relative dx/dy/dist | grid | units | current-tool
(grows) | constraint (eeschema never writes it). */}
<KiStatusBar
testIds={{ message: 'sch-status-msg', tool: 'sch-tool-msg' }}
fields={{
message: statusText,
zoom: (
<span
ref={(el) => {
statusPaneRefs.current.zoom = el;
}}
/>
),
coords: (
<span
ref={(el) => {
statusPaneRefs.current.coords = el;
}}
/>
),
deltas: (
<span
ref={(el) => {
statusPaneRefs.current.deltas = el;
}}
/>
),
grid: gridMsg(messageTextFromValue(iuToMM(renderOpts.grid.sizeIU), units, SCH_IU_PER_MM)),
units: unitsMsg(units),
tool: SCH_TOOL_MSGS[activeTool] ?? '',
}}
/>
{revertPrompt && (
/* IsOK (common/confirm.cpp:278-300): a KICAD_MESSAGE_DIALOG captioned
"Confirmation" with wxICON_QUESTION, whose OK/Cancel pair is
relabelled "&Yes"/"&No", and wxOK_DEFAULT makes YES the default. */
<MessageDialogYesNo
caption={CONFIRMATION_CAPTION}
message={revertPromptMessage(revertPrompt.file)}
icon="question"
defaultButton="yes"
onResult={(r) => (r === 'yes' ? revertPrompt.onYes() : revertPrompt.onNo())}
/>
)}
{libraryNameRequest && (
<WxTextEntryDialog
caption={libraryNameRequest.title}
message="Library name:"
value={libraryNameRequest.name}
{...(libraryNameRequest.hook &&
(libraryNameRequest.hook as Partial<FILEDLG_HOOK_NEW_LIBRARY>).kind === 'new_library'
? {
extra: (
<FileDlgHookNewLibraryControls
hook={libraryNameRequest.hook as FILEDLG_HOOK_NEW_LIBRARY}
/>
),
}
: {})}
onCancel={() => {
libraryNameRequest.resolve(null);
setLibraryNameRequest(null);
}}
onConfirm={(aName) => {
libraryNameRequest.resolve(aName);
setLibraryNameRequest(null);
}}
/>
)}
{dbProperties && (
<DialogDesignBlockProperties
designBlock={dbProperties.designBlock}
disableName={dbProperties.disableName}
onClose={(ok) => {
setDbProperties(null);
dbProperties.resolve(ok);
}}
showError={(m) => DbDisplayErrorMessage(m)}
/>
)}
{textEntryRequest && (
<WxTextEntryDialog
caption={textEntryRequest.caption}
message={textEntryRequest.message}
value={textEntryRequest.value}
onCancel={() => {
setTextEntryRequest(null);
textEntryRequest.resolve(null);
}}
onConfirm={(text) => {
setTextEntryRequest(null);
textEntryRequest.resolve(text);
}}
/>
)}
{fileDialogRequest && (
<WxFileDialog
title={fileDialogRequest.title}
filters={fileDialogRequest.filters}
initialPath={fileDialogRequest.defaultDir || undefined}
projectDir={project.current.root ? `/${project.current.root.replace(/\/.*$/, '')}` : null}
extra={
fileDialogRequest.hook &&
'kind' in fileDialogRequest.hook &&
fileDialogRequest.hook.kind === 'import_sheet_contents' ? (
<FiledlgImportSheetContentsControls hook={fileDialogRequest.hook} />
) : undefined
}
onDone={(file) => {
setFileDialogRequest(null);
fileDialogRequest.resolve(file ? wxFileDialogGetPath(file) : null);
}}
/>
)}
{/* DIALOG_SYMBOL_CHOOSER, opened by PickSymbolFromLibrary for the live tools. */}
{chooserRequest && (
<DialogSymbolChooser
powerFilter={chooserRequest.filter?.GetFilterPowerSymbols() ?? false}
showFootprints={chooserRequest.showFootprints}
historyList={chooserRequest.history.map(pickedToChooser)}
alreadyPlaced={chooserRequest.placed.map(pickedToChooser)}
onOk={(result) => {
setChooserRequest(null);
chooserRequest.resolve(result ? chooserToPicked(result) : null);
}}
onCancel={() => {
setChooserRequest(null);
chooserRequest.resolve(null);
}}
/>
)}
{aboutOpen && (
<ShowAboutDialog title={ABOUT_TITLES.schematic} onClose={() => setAboutOpen(false)} />
)}
{prefsOpen && (
<PreferencesDialog initialPage={prefsPage} onClose={() => setPrefsOpen(false)} />
)}
{/* DIALOG_SYMBOL_PROPERTIES on a live symbol (SCH_EDIT_TOOL::Properties). */}
{symbolDialog && (
<DialogSymbolProperties
dlg={symbolDialog.dlg}
initial={symbolDialog.shown}
onClose={(aRetval) => {
setSymbolDialog(null);
symbolDialog.resolve(aRetval);
}}
onCancel={() => {
setSymbolDialog(null);
symbolDialog.resolve(wxID_CANCEL);
}}
/>
)}
{/* DIALOG_WIRE_BUS_PROPERTIES on the live wires, buses and bus entries. */}
{wireBusDialog && (
<DialogWireBusProperties
dlg={wireBusDialog.dlg}
initial={wireBusDialog.shown}
units={units}
onOk={(values) => {
setWireBusDialog(null);
wireBusDialog.dlg.TransferDataFromWindow(values);
wireBusDialog.resolve(wxID_OK);
}}
onCancel={() => {
setWireBusDialog(null);
wireBusDialog.resolve(wxID_CANCEL);
}}
/>
)}
{/* DIALOG_LINE_PROPERTIES on the live graphic lines. */}
{lineDialog && (
<DialogLineProperties
dlg={lineDialog.dlg}
initial={lineDialog.shown}
units={units}
onOk={(values) => {
setLineDialog(null);
lineDialog.dlg.TransferDataFromWindow(values);
lineDialog.resolve(wxID_OK);
}}
onCancel={() => {
setLineDialog(null);
lineDialog.resolve(wxID_CANCEL);
}}
/>
)}
{/* Assign Netclass (DIALOG_ASSIGN_NETCLASS). */}
{netclassDialog && (
<DialogAssignNetclass
frame="schematic"
netNames={new Set(netclassDialog.netNames)}
candidateNetNames={netclassDialog.candidates}
// netSettings->GetNetclasses(): a std::map, so by name, and without Default.
netClasses={[
...(schFrameRef.current!.Prj().GetProjectFile() as PROJECT_FILE).m_NetSettings
.GetNetclasses()
.keys(),
]
.filter((n) => n !== 'Default')
.sort()}
onPreview={netclassDialog.previewer}
onCancel={() => {
setNetclassDialog(null);
netclassDialog.resolve(wxID_CANCEL);
}}
onOk={(pattern, netClass) => {
// DIALOG_ASSIGN_NETCLASS::TransferDataFromWindow: the one pattern into the live
// project's NET_SETTINGS, then the project written, as Schematic Setup's OK writes it.
const projectFile = schFrameRef.current!.Prj().GetProjectFile() as PROJECT_FILE;
projectFile.m_NetSettings.SetNetclassPatternAssignment(pattern, netClass);
Pgm().GetSettingsManager().SaveProject();
setNetclassDialog(null);
netclassDialog.resolve(wxID_OK);
}}
/>
)}
{/* The read-only hotkey list (DIALOG_LIST_HOTKEYS, Ctrl+F1). */}
{/* DIALOG_TABLECELL_PROPERTIES on live cells (SCH_EDIT_TOOL::Properties). */}
{cellDialog && (
<DialogTableCellProperties
dlg={cellDialog.dlg}
initial={cellDialog.shown}
units={units}
onClose={(aRetval) => {
setCellDialog(null);
cellDialog.resolve(aRetval);
}}
/>
)}
{/* DIALOG_JUNCTION_PROPS on the live junctions (E / double-click on a junction). */}
{junctionDialog && (
<DialogJunctionProps
dlg={junctionDialog.dlg}
initial={junctionDialog.shown}
units={units}
onOk={(values) => {
setJunctionDialog(null);
junctionDialog.dlg.TransferDataFromWindow(values);
junctionDialog.resolve(wxID_OK);
}}
onCancel={() => {
setJunctionDialog(null);
junctionDialog.resolve(wxID_CANCEL);
}}
/>
)}
{/* DIALOG_SHEET_PROPERTIES on a live sheet (SCH_EDIT_FRAME::EditSheetProperties). */}
{sheetDialog && (
<DialogSheetProperties
dlg={sheetDialog.dlg}
initial={sheetDialog.shown}
units={units}
onOk={(values) => {
void sheetDialog.dlg.TransferDataFromWindow(values).then((ok) => {
if (!ok) return;
setSheetDialog(null);
sheetDialog.resolve(wxID_OK);
});
}}
onCancel={() => {
setSheetDialog(null);
sheetDialog.resolve(wxID_CANCEL);
}}
/>
)}
{kiDialog.node}
{/* FRAME_FOOTPRINT_CHOOSER. Upstream reaches it through
`Kiway().Player( FRAME_FOOTPRINT_CHOOSER, true, m_frame )` from
PG_FPID_EDITOR, and the same frame is what Symbol Properties'
GRID_CELL_FPID_EDITOR opens - one chooser, two callers. */}
{fpChooser && (
<FootprintChooserFrame
preselect={fpChooser.current}
fpFilters={fpChooser.fpFilters}
{...(fpChooser.pinCount === undefined ? {} : { pinCount: fpChooser.pinCount })}
onOk={(libId) => {
fpChooser.commit(libId);
setFpChooser(null);
}}
onCancel={() => {
fpChooser.cancel?.();
setFpChooser(null);
}}
/>
)}
{/* DIALOG_SHEET_PIN_PROPERTIES on a live sheet pin. */}
{sheetPinDialog && (
<DialogSheetPinProperties
dlg={sheetPinDialog.dlg}
initial={sheetPinDialog.shown}
units={units}
onOk={(values) => {
setSheetPinDialog(null);
sheetPinDialog.dlg.TransferDataFromWindow(values);
sheetPinDialog.resolve(wxID_OK);
}}
onCancel={() => {
setSheetPinDialog(null);
sheetPinDialog.resolve(wxID_CANCEL);
}}
/>
)}
{fieldDialog && (
<DialogFieldProperties
dlg={fieldDialog.dlg}
initial={fieldDialog.shown}
// Every UNIT_BINDER in the dialog reads its units off the frame.
units={units}
onOk={(values) => {
if (!fieldDialog.dlg.TransferDataFromWindow(values)) return;
// `dlg.UpdateField( &commit, aField, &m_frame->GetCurrentSheet() )`, on the caller's
// commit.
fieldDialog.dlg.UpdateField(
fieldDialog.commit,
fieldDialog.field,
schFrameRef.current!.GetCurrentSheet(),
);
setFieldDialog(null);
fieldDialog.resolve(wxID_OK);
}}
onCancel={() => {
setFieldDialog(null);
fieldDialog.resolve(wxID_CANCEL);
}}
/>
)}
{/* DIALOG_IMAGE_PROPERTIES on a live bitmap. */}
{imageDialog && (
<DialogImageProperties
posX={imageDialog.shown.posX}
posY={imageDialog.shown.posY}
units={units}
scale={imageDialog.shown.scale}
data={imageDialog.shown.data}
ppi={imageDialog.shown.ppi}
pixelSize={imageDialog.shown.pixelSize}
onOk={(r) => {
setImageDialog(null);
imageDialog.dlg.TransferDataFromWindow(r);
imageDialog.resolve(wxID_OK);
}}
onCancel={() => {
setImageDialog(null);
imageDialog.resolve(wxID_CANCEL);
}}
/>
)}
{/* DIALOG_SHAPE_PROPERTIES on a live shape. */}
{shapeDialog && (
<DialogShapeProperties
dlg={shapeDialog.dlg}
initial={shapeDialog.shown}
title={shapeDialog.title}
units={units}
onOk={(values) => {
setShapeDialog(null);
shapeDialog.dlg.TransferDataFromWindow(values);
shapeDialog.resolve(wxID_OK);
}}
onCancel={() => {
setShapeDialog(null);
shapeDialog.resolve(wxID_CANCEL);
}}
/>
)}
{/* DIALOG_TEXT_PROPERTIES on a live text or text box. */}
{labelDialog && (
<DialogLabelProperties
dlg={labelDialog.dlg}
initial={labelDialog.shown}
units={units}
onOk={(values) => {
if (!labelDialog.dlg.TransferDataFromWindow(values)) return;
setLabelDialog(null);
labelDialog.resolve(wxID_OK);
}}
onCancel={() => {
setLabelDialog(null);
labelDialog.resolve(wxID_CANCEL);
}}
/>
)}
{textDialog && (
<DialogTextProperties
dlg={textDialog.dlg}
units={units}
initial={textDialog.shown}
onOk={(values) => {
if (!textDialog.dlg.TransferDataFromWindow(values)) return;
setTextDialog(null);
textDialog.resolve(wxID_OK);
}}
onCancel={() => {
setTextDialog(null);
textDialog.resolve(wxID_CANCEL);
}}
/>
)}
{/* DIALOG_IMPORT_GFX_SCH for SCH_DRAWING_TOOLS::ImportGraphics: OK fills its result. */}
{importGfx && (
<DialogImportGfx
importer={newSchGraphicsImporter}
filenameOverride={importGfx.result.filenameOverride}
onCancel={() => {
setImportGfx(null);
importGfx.resolve(wxID_CANCEL);
}}
onOk={(items, interactive) => {
importGfx.result.items = items;
importGfx.result.interactive = interactive;
setImportGfx(null);
importGfx.resolve(wxID_OK);
}}
/>
)}
{/* DIALOG_TABLE_PROPERTIES on a live table (DrawTable, Properties, EditTable). */}
{tableDialog && (
<DialogTableProperties
dlg={tableDialog.dlg}
initial={tableDialog.shown}
units={units}
isNew={tableDialog.isNew}
onOk={(values) => {
if (!tableDialog.dlg.TransferDataFromWindow(values)) return;
setTableDialog(null);
tableDialog.resolve(wxID_OK);
}}
onCancel={() => {
setTableDialog(null);
tableDialog.resolve(wxID_CANCEL);
}}
/>
)}
{/* WX_PROGRESS_REPORTER( this, _( "Load Schematic" ), 1, PR_CAN_ABORT )
(files-io.cpp:179), over the frame — which is already up, menus,
toolbars and grid, with its blank sheet, exactly as SCH_EDIT_FRAME
is before OpenProjectFiles runs. */}
<ProgressDialog title="Load Schematic" label={loading} />
</div>
);
}
/** One row of the hierarchy tree; children indent one level (KiCad's navigator). */
/**
* One row of the hierarchy tree (HIERARCHY_TREE): ancestor columns carry a
* dotted guide line for every level whose parent still has siblings below it,
* and this node's own column elbows into its row - straight through for a
* middle child, cut off halfway down for the last one - matching KiCad's
* wxTreeCtrl connector lines.
*/
/**
* One row of HIERARCHY_TREE and, below it, its subtree.
*
* On GTK a wxTreeCtrl is wxGenericTreeCtrl, and it paints every row with the
* same arithmetic (src/generic/treectlg.cpp, `PaintLevel` :2765 and
* `PaintItem` :2556), so the row is laid out from those numbers rather than
* from flex gaps. `qa/probes/hierarchy_tree_probe.cpp` builds the tree with
* HIERARCHY_TREE's style word and KiCad's two images and reads the same
* positions back off wx. With `m_indent` 15 and `m_spacing` 18, an item at
* `level` (1 for the top-level sheet — `wxTR_HIDE_ROOT` hides level 0):
*
* x = level * 15 the button's centre and the trunk
* item X = x + 18 the 16px icon
* text = X + 16 + 4 MARGIN_BETWEEN_IMAGE_AND_TEXT
* guide = (x > 15 ? x - 15 : x) .. X the dotted lead-in, at y_mid
* trunk = x, from y_mid + 5 down to the last child's y_mid
* selection = X + 17 .. text end + 3 the band leaves the icon alone
*
* The control paints from y = 2 and each row is `GetLineHeight()` = 24 tall
* (font extent 18 + 4 + 2, the probe's number), the icon centred at +4 and
* the text centred in the row.
*
* The CSS carries the same numbers as `--x`, `--X` per level; the classes are
* `ze-hier-*` and nothing else in the app shares them.
*
* Which row is selected: `UpdateHierarchySelection` (hierarchy_pane.cpp:145)
* bolds the current sheet and `SetFocusedItem`s it, which in the generic tree
* is `SelectItem` — so the current sheet is the selected one, and a selected
* item shows its SELECTED image, `tree_sel` (the blue dot), where every other
* row shows `tree_nosel`. wxGenericTreeCtrl has no hover state.
*/
function renderSheetNode(
node: SheetTreeNode,
depth: number,
currentPath: string,
onOpen: (path: string, file: string) => void,
collapsedPaths: ReadonlySet<string>,
setCollapsedPaths: (updater: (prev: Set<string>) => Set<string>) => void,
netMark: { paths: ReadonlySet<string>; color: string },
isLast = true,
isFirst = true,
): JSX.Element {
const hasChildren = node.children.length > 0;
const collapsed = collapsedPaths.has(node.path);
const active = node.path === currentPath;
const level = depth + 1;
const toggle = (e: React.MouseEvent): void => {
e.stopPropagation();
setCollapsedPaths((prev) => {
const next = new Set(prev);
if (next.has(node.path)) next.delete(node.path);
else next.add(node.path);
return next;
});
};
return (
<div
key={node.path}
className={`ze-hier-node${isLast ? ' last' : ''}${isFirst ? ' first' : ''}`}
style={{ '--lvl': level } as React.CSSProperties}
>
<div className="ze-hier-row" onClick={() => onOpen(node.path, node.file)} title={node.file}>
{/* The dotted lead-in: from the parent's trunk (or, for a top-level
sheet, from its own button) to the icon. */}
<span className="ze-hier-guide" />
{hasChildren && (
<span
className={`ze-hier-button${collapsed ? '' : ' open'}`}
onClick={toggle}
title={collapsed ? 'Expand' : 'Collapse'}
/>
)}
<span className={`ze-hier-icon${active ? ' sel' : ''}`} />
<span
className={`ze-hier-label${active ? ' sel' : ''}`}
// `SetItemTextColour( id, mark ? markText : wxNullColour )`. wxGenericTreeCtrl's
// PaintItem draws a SELECTED row in wxSYS_COLOUR_HIGHLIGHTTEXT whatever its own colour,
// so the mark shows on every sheet but the current one.
style={!active && netMark.paths.has(node.path) ? { color: netMark.color } : undefined}
>
{node.name}
{node.page && ` (page ${node.page})`}
</span>
</div>
{!collapsed && hasChildren && (
<div className="ze-hier-children">
{node.children.map((c, i) =>
renderSheetNode(
c,
depth + 1,
currentPath,
onOpen,
collapsedPaths,
setCollapsedPaths,
netMark,
i === node.children.length - 1,
i === 0,
),
)}
</div>
)}
</div>
);
}