23#include <QApplication>
26#include <QJsonDocument>
27#include <QKeySequence>
31 #include <emscripten/emscripten.h>
41 ShortcutSetting shortcut(
const QString& key,
const QString& label, QAction* action,
42 const QKeySequence& defaultShortcut)
48 .defaultValue = QVariant::fromValue(defaultShortcut),
62 globalThis.__siliconShortcutCaptures = globalThis.__siliconShortcutCaptures ||
new Map();
63 const id = globalThis.__siliconNextShortcutCaptureId ?? 0;
64 globalThis.__siliconNextShortcutCaptureId =
id + 1;
65 globalThis.__siliconShortcutCaptures.set(
id, {});
75 const captures = globalThis.__siliconShortcutCaptures;
76 const capture = captures.get($0);
77 for (
const [type, handler] of capture.listeners || [])
78 document.removeEventListener(type, handler,
true);
80 cancelAnimationFrame(capture.frame);
91 const captures = globalThis.__siliconShortcutCaptures;
92 const capture = captures.get($0);
93 const shortcuts = JSON.parse(UTF8ToString($1));
95 shortcuts.map(sequence => sequence.trim()
97 .map(part => part.trim().toLowerCase())
100 const isEditableTarget = target => target instanceof HTMLInputElement || target
101 instanceof HTMLTextAreaElement || target
102 instanceof HTMLSelectElement || target?.isContentEditable;
104 const normalizedKeyToken = token => ({
119 const eventMatchesShortcut = (event, shortcut) =>
121 if (shortcut.length === 0)
124 const target =
event.composedPath()[0];
125 const key =
event.key.toLowerCase();
126 const keyToken = normalizedKeyToken(shortcut[shortcut.length - 1]);
127 const wantsCtrl = shortcut.includes(
'ctrl') || shortcut.includes(
'control');
128 const wantsMeta = shortcut.includes(
'meta');
129 const wantsAlt = shortcut.includes(
'alt');
130 const wantsShift = shortcut.includes(
'shift');
131 const usesCommandModifier = wantsCtrl || wantsMeta || wantsAlt;
133 if (!usesCommandModifier && isEditableTarget(target))
136 if (wantsCtrl || wantsMeta) {
137 if (wantsCtrl && !(event.ctrlKey || event.metaKey))
139 if (wantsMeta && !event.metaKey)
141 }
else if (event.ctrlKey || event.metaKey) {
144 if (wantsAlt !== event.altKey)
146 if (wantsShift !== event.shiftKey)
149 return key === keyToken ||
event.code.toLowerCase() === keyToken;
152 if (capture.listeners)
158 const qtHost =
event =>
event.composedPath().find(
159 node => node.id ===
'qt-shadow-container' && node.shadowRoot);
160 const rememberQt =
event => {
161 const host = qtHost(event);
165 capture.window =
event.composedPath().find(
166 node => node.classList?.contains(
'qt-window'));
169 const scheduleFocus = () => {
170 if (capture.frame || !capture.owned)
174 capture.frame = requestAnimationFrame(() => {
176 if (!capture.owned || !document.hasFocus()
177 || document.activeElement !== document.body)
179 const root = capture.host?.shadowRoot;
180 if (!capture.host?.isConnected || !root || root.activeElement)
182 const visible = element => element.getClientRects().length > 0;
186 const selector =
'.qt-window-focus-helper, .hidden-visually-read-by-screen-reader';
187 const local = capture.window?.querySelectorAll(selector) || [];
188 const target = Array.from(local).find(visible)
189 || Array.from(root.querySelectorAll(selector)).find(visible);
190 target?.focus({preventScroll:
true});
193 const onFocusIn =
event => {
194 capture.owned = rememberQt(event);
196 const onPointerDown =
event => {
197 capture.owned = rememberQt(event);
200 const onFocusOut =
event => {
201 if (capture.owned && qtHost(event))
204 const onKeyDown =
event => {
205 if (!rememberQt(event))
208 capture.owned =
event.key !==
'Tab';
209 if (!capture.owned || event.isComposing)
212 if (capture.sequences.some(
213 shortcut => eventMatchesShortcut(event, shortcut)))
214 event.preventDefault();
216 capture.listeners = ([
217 [
'keydown', onKeyDown], [
'focusin', onFocusIn],
218 [
'focusout', onFocusOut], [
'pointerdown', onPointerDown]
220 for (
const [type, handler] of capture.listeners)
221 document.addEventListener(type, handler,
true);
224 shortcutsJson.constData());
236 QJsonArray shortcutSequences;
238 if (!shortcut.action)
241 for (
const QKeySequence& sequence : shortcut.action->shortcuts()) {
242 if (sequence.isEmpty())
245 for (
int index = 0; index < sequence.count(); ++index)
246 shortcutSequences.append(
247 QKeySequence(sequence[index]).toString(QKeySequence::PortableText));
251 const QByteArray shortcutsJson =
252 QJsonDocument(shortcutSequences).toJson(QJsonDocument::Compact);
254 if (!browserShortcuts)
255 browserShortcuts = std::make_unique<BrowserShortcutCapture>();
256 browserShortcuts->update(shortcutsJson);
263 shortcut(QStringLiteral(
"keybindings/new"), tr(
"New"),
project.newProject,
264 QKeySequence(QKeySequence::New)),
265 shortcut(QStringLiteral(
"keybindings/open"), tr(
"Open"),
project.open,
266 QKeySequence(QKeySequence::Open)),
267 shortcut(QStringLiteral(
"keybindings/save"), tr(
"Save"),
project.save,
268 QKeySequence(QKeySequence::Save)),
269 shortcut(QStringLiteral(
"keybindings/exportImage"), tr(
"Export"),
270 project.exportImage, QKeySequence(QKeySequence::Print)),
271 shortcut(QStringLiteral(
"keybindings/exit"), tr(
"Exit"),
project.exit,
272 QKeySequence(QKeySequence::Quit)),
273 shortcut(QStringLiteral(
"keybindings/undo"), tr(
"Undo"),
edit.undo,
274 QKeySequence(QKeySequence::Undo)),
275 shortcut(QStringLiteral(
"keybindings/redo"), tr(
"Redo"),
edit.redo,
276 QKeySequence(QKeySequence::Redo)),
277 shortcut(QStringLiteral(
"keybindings/cut"), tr(
"Cut"),
edit.cut,
278 QKeySequence(QKeySequence::Cut)),
279 shortcut(QStringLiteral(
"keybindings/copy"), tr(
"Copy"),
edit.copy,
280 QKeySequence(QKeySequence::Copy)),
281 shortcut(QStringLiteral(
"keybindings/paste"), tr(
"Paste"),
edit.paste,
282 QKeySequence(QKeySequence::Paste)),
283 shortcut(QStringLiteral(
"keybindings/selectAll"), tr(
"Select All"),
284 circuit.selectAll, QKeySequence(Qt::ControlModifier | Qt::Key_A)),
285 shortcut(QStringLiteral(
"keybindings/rotate"), tr(
"Rotate"),
circuit.rotate,
286 QKeySequence(Qt::AltModifier | Qt::Key_R)),
287 shortcut(QStringLiteral(
"keybindings/autoPlace"), tr(
"Auto place"),
288 circuit.autoPlace, QKeySequence(Qt::AltModifier | Qt::Key_L)),
289 shortcut(QStringLiteral(
"keybindings/centerView"), tr(
"Center view"),
290 circuit.centerView, QKeySequence(Qt::AltModifier | Qt::Key_C)),
291 shortcut(QStringLiteral(
"keybindings/delete"), tr(
"Delete"),
edit.remove,
292 QKeySequence(QKeySequence::Delete)),
293 shortcut(QStringLiteral(
"keybindings/normalMode"), tr(
"Normal mode"),
294 circuit.setNormalMode, QKeySequence()),
295 shortcut(QStringLiteral(
"keybindings/panMode"), tr(
"Pan mode"),
circuit.setPanMode,
297 shortcut(QStringLiteral(
"keybindings/wireCreationMode"), tr(
"Wire creation mode"),
298 circuit.setWireCreationMode, QKeySequence(Qt::AltModifier | Qt::Key_W)),
299 shortcut(QStringLiteral(
"keybindings/simulationMode"), tr(
"Simulation mode"),
301 QKeySequence(Qt::AltModifier | Qt::ControlModifier | Qt::Key_S)),
302 shortcut(QStringLiteral(
"keybindings/componentPlacingMode"),
303 tr(
"Component placing mode"),
circuit.setComponentPlacingMode,
304 QKeySequence(Qt::AltModifier | Qt::Key_A)),
305 shortcut(QStringLiteral(
"keybindings/cancelInteraction"),
306 tr(
"Cancel current interaction"),
circuit.cancelInteraction,
307 QKeySequence(Qt::Key_Escape)),
308 shortcut(QStringLiteral(
"keybindings/toggleTrace"), tr(
"Waveform trace"),
309 waveform.toggleTrace, QKeySequence()),
310 shortcut(QStringLiteral(
"keybindings/settings"), tr(
"Settings"),
project.settings,
329 shortcut.action->setShortcut(
332 shortcut.action->setShortcutContext(Qt::ApplicationShortcut);
333 if (!
window.actions().contains(shortcut.action))
334 window.addAction(shortcut.action);
347 settingsWindow->setAttribute(Qt::WA_DeleteOnClose);
348 connect(settingsWindow, &QDialog::finished,
this,
350 settingsWindow->open();
353 settingsWindow.exec();
static void setMaxSimulationSteps(uint64_t value)
static void setMaxTransitionsPerDeltaCycle(int value)
void update(const QByteArray &shortcutsJson)
~BrowserShortcutCapture()
Dialog for editing persisted application settings and shortcuts.
QSettings wrapper configured to read and write SILICON TOML settings files.
static void apply(QApplication &app, theme::Mode mode)
WaveformActions waveform
Commands owned by the waveform viewer.
~WindowActions() override
CircuitActions circuit
Commands owned by the circuit editor.
void syncWasmShortcutCapture()
ProjectActions project
Commands owned by the project and application shell.
void applyStoredSettings()
EditActions edit
Commands owned by the active editor, whatever its kind.
QVector< ShortcutSetting > shortcutSettings() const
QVariant value(const QSettings &settings, const Definition &setting)
Reads a setting value, falling back to its declared default when unset.
SILICON::ui::theme::Mode themeModeFromText(const QString &value)
Converts a persisted theme string into a theme engine mode.
CommonSettingsValues readCommonSettings(const QSettings &settings)
Reads the common UI and simulation settings into a typed snapshot.
Snapshot of the common application settings used at runtime.
int maxTransitionsPerDeltaCycle
Shortcut row shown in the settings dialog.