commands running from electron

This commit is contained in:
Jan Prochazka
2021-03-15 20:48:43 +01:00
parent 21feb3a042
commit d4a35fb414
8 changed files with 102 additions and 29 deletions

View File

@@ -27,6 +27,8 @@ autoUpdater.logger = log;
// TODO - create settings for this
// appUpdater.channel = 'beta';
let commands = {};
function hideSplash() {
if (splashWindow) {
splashWindow.destroy();
@@ -35,31 +37,40 @@ function hideSplash() {
mainWindow.show();
}
function commandItem(id) {
const command = commands[id];
return {
id,
label: command ? command.menuName || command.toolbarName || command.name : id,
accelerator: command ? command.keyText : undefined,
enabled: command ? command.enabled : false,
click() {
mainWindow.webContents.executeJavaScript(`dbgate_runCommand('${id}')`);
},
};
}
function buildMenu() {
const template = [
{
label: 'File',
submenu: [
{
label: 'Connect to database',
click() {
mainWindow.webContents.executeJavaScript(`dbgate_createNewConnection()`);
},
},
commandItem('new.connection'),
{
label: 'Open file',
click() {
mainWindow.webContents.executeJavaScript(`dbgate_openFile()`);
},
},
{
label: 'Save',
click() {
mainWindow.webContents.executeJavaScript(`dbgate_tabCommand('save')`);
},
accelerator: 'Ctrl+S',
id: 'save',
},
commandItem('group.save'),
// {
// label: 'Save',
// click() {
// mainWindow.webContents.executeJavaScript(`dbgate_tabCommand('save')`);
// },
// accelerator: 'Ctrl+S',
// id: 'save',
// },
{
label: 'Save As',
click() {
@@ -157,10 +168,22 @@ function buildMenu() {
return Menu.buildFromTemplate(template);
}
ipcMain.on('update-menu', async (event, arg) => {
const commands = await mainWindow.webContents.executeJavaScript(`dbgate_getCurrentTabCommands()`);
mainMenu.getMenuItemById('save').enabled = !!commands.save;
mainMenu.getMenuItemById('saveAs').enabled = !!commands.saveAs;
ipcMain.on('update-commands', async (event, arg) => {
commands = JSON.parse(arg);
for (const key of Object.keys(commands)) {
const menu = mainMenu.getMenuItemById(key);
if (!menu) continue;
const command = commands[key];
// rebuild menu
if (menu.label != command.text || menu.accelerator != command.keyText) {
mainMenu = buildMenu();
mainWindow.setMenu(mainMenu);
return;
}
menu.enabled = command.enabled;
}
});
function createWindow() {

View File

@@ -1,7 +1,7 @@
<script lang="ts" context="module">
import { commands } from '../stores';
import { get } from 'svelte/store';
export function handleCommandKeyDown(e) {
let keyText = '';
if (e.ctrlKey) keyText += 'Ctrl+';
@@ -15,6 +15,7 @@
const command: any = Object.values(commandsValue).find(
(x: any) =>
x.enabled &&
!x.isGroupCommand &&
x.keyText &&
x.keyText
.toLowerCase()

View File

@@ -44,8 +44,8 @@
'text'
);
$: filteredItems = (parentCommand ? parentCommand.getSubCommands() : sortedComands).filter(x =>
filterName(filter, x.text)
$: filteredItems = (parentCommand ? parentCommand.getSubCommands() : sortedComands).filter(
x => !x.isGroupCommand && filterName(filter, x.text)
);
function handleCommand(command) {

View File

@@ -1,5 +1,6 @@
import { tick } from 'svelte';
import { commands } from '../stores';
import { GlobalCommand } from './registerCommand';
let isInvalidated = false;
@@ -12,17 +13,23 @@ export default async function invalidateCommands() {
commands.update(dct => {
let res = null;
for (const key of Object.keys(dct)) {
const command = dct[key];
for (const command of Object.values(dct) as GlobalCommand[]) {
if (command.isGroupCommand) continue;
const { testEnabled } = command;
let enabled = command.enabled;
if (testEnabled) enabled = testEnabled();
if (enabled != command.enabled) {
if (!res) res = { ...dct };
res[key] = {
...command,
enabled,
};
res[command.id].enabled = enabled;
}
}
if (res) {
const values = Object.values(res) as GlobalCommand[];
// test enabled for group commands
for (const command of values) {
if (!command.isGroupCommand) continue;
const groupSources = values.filter(x => x.group == command.group && !x.isGroupCommand && x.enabled);
command.enabled = groupSources.length > 0;
}
}
return res || dct;

View File

@@ -7,10 +7,12 @@ export interface SubCommand {
export interface GlobalCommand {
id: string;
category: string;
category: string; // null for group commands
isGroupCommand?: boolean;
name: string;
text?: string /* category: name */;
keyText?: string;
group?: string;
getSubCommands?: () => SubCommand[];
onClick?: Function;
testEnabled?: () => boolean;
@@ -20,6 +22,7 @@ export interface GlobalCommand {
enabled?: boolean;
showDisabled?: boolean;
toolbarName?: string;
menuName?: string;
toolbarOrder?: number;
disableHandleKeyText?: string;
}
@@ -46,3 +49,4 @@ export default function registerCommand(command: GlobalCommand) {
// });
// }
}

View File

@@ -5,7 +5,7 @@ import { ThemeDefinition } from 'dbgate-types';
import ConnectionModal from '../modals/ConnectionModal.svelte';
import { showModal } from '../modals/modalTools';
import newQuery from '../query/newQuery';
import saveTabFile, { saveTabEnabledStore } from '../utility/saveTabFile';
import saveTabFile from '../utility/saveTabFile';
import openNewTab from '../utility/openNewTab';
function themeCommand(theme: ThemeDefinition) {
@@ -109,6 +109,15 @@ registerCommand({
},
});
registerCommand({
id: 'group.save',
category: null,
isGroupCommand: true,
name: 'Save',
keyText: 'Ctrl+S',
group: 'save',
});
export function registerFileCommands({
idPrefix,
category,
@@ -122,6 +131,7 @@ export function registerFileCommands({
}) {
registerCommand({
id: idPrefix + '.save',
group: 'save',
category,
name: 'Save',
keyText: 'Ctrl+S',

View File

@@ -16,6 +16,7 @@
registerCommand({
id: 'dataGrid.save',
group: 'save',
category: 'Data grid',
name: 'Save',
keyText: 'Ctrl+S',

View File

@@ -1,6 +1,8 @@
import { writable, derived, readable } from 'svelte/store';
import { ExtensionsDirectory } from 'dbgate-types';
import invalidateCommands from './commands/invalidateCommands';
import getElectron from './utility/getElectron';
import { GlobalCommand } from './commands/registerCommand';
interface TabDefinition {
title: string;
@@ -44,6 +46,8 @@ export const nullStore = readable(null, () => {});
export const currentArchive = writable('default');
export const isFileDragActive = writable(false);
const electron = getElectron();
subscribeCssVariable(selectedWidget, x => (x ? 1 : 0), '--dim-visible-left-panel');
subscribeCssVariable(visibleToolbar, x => (x ? 1 : 0), '--dim-visible-toolbar');
subscribeCssVariable(leftPanelWidth, x => `${x}px`, '--dim-left-panel-width');
@@ -75,3 +79,26 @@ openedTabs.subscribe(value => {
invalidateCommands();
});
export const getOpenedTabs = () => openedTabsValue;
let commandsValue = null;
commands.subscribe(value => {
commandsValue = value;
if (electron) {
const { ipcRenderer } = electron;
ipcRenderer.send('update-commands', JSON.stringify(value));
}
});
export const getCommands = () => commandsValue;
export function runCommand(id) {
const command = commandsValue[id];
if (command) {
if (command.isGroupCommand) {
const values = Object.values(commandsValue) as GlobalCommand[];
const real = values.find(x => x.group == command.group && !x.isGroupCommand && x.enabled);
if (real && real.onClick) real.onClick();
}
command.onClick();
}
}
window['dbgate_runCommand'] = runCommand;