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 // TODO - create settings for this
// appUpdater.channel = 'beta'; // appUpdater.channel = 'beta';
let commands = {};
function hideSplash() { function hideSplash() {
if (splashWindow) { if (splashWindow) {
splashWindow.destroy(); splashWindow.destroy();
@@ -35,31 +37,40 @@ function hideSplash() {
mainWindow.show(); 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() { function buildMenu() {
const template = [ const template = [
{ {
label: 'File', label: 'File',
submenu: [ submenu: [
{ commandItem('new.connection'),
label: 'Connect to database',
click() {
mainWindow.webContents.executeJavaScript(`dbgate_createNewConnection()`);
},
},
{ {
label: 'Open file', label: 'Open file',
click() { click() {
mainWindow.webContents.executeJavaScript(`dbgate_openFile()`); mainWindow.webContents.executeJavaScript(`dbgate_openFile()`);
}, },
}, },
{ commandItem('group.save'),
label: 'Save', // {
click() { // label: 'Save',
mainWindow.webContents.executeJavaScript(`dbgate_tabCommand('save')`); // click() {
}, // mainWindow.webContents.executeJavaScript(`dbgate_tabCommand('save')`);
accelerator: 'Ctrl+S', // },
id: 'save', // accelerator: 'Ctrl+S',
}, // id: 'save',
// },
{ {
label: 'Save As', label: 'Save As',
click() { click() {
@@ -157,10 +168,22 @@ function buildMenu() {
return Menu.buildFromTemplate(template); return Menu.buildFromTemplate(template);
} }
ipcMain.on('update-menu', async (event, arg) => { ipcMain.on('update-commands', async (event, arg) => {
const commands = await mainWindow.webContents.executeJavaScript(`dbgate_getCurrentTabCommands()`); commands = JSON.parse(arg);
mainMenu.getMenuItemById('save').enabled = !!commands.save; for (const key of Object.keys(commands)) {
mainMenu.getMenuItemById('saveAs').enabled = !!commands.saveAs; 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() { function createWindow() {

View File

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

View File

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

View File

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

View File

@@ -7,10 +7,12 @@ export interface SubCommand {
export interface GlobalCommand { export interface GlobalCommand {
id: string; id: string;
category: string; category: string; // null for group commands
isGroupCommand?: boolean;
name: string; name: string;
text?: string /* category: name */; text?: string /* category: name */;
keyText?: string; keyText?: string;
group?: string;
getSubCommands?: () => SubCommand[]; getSubCommands?: () => SubCommand[];
onClick?: Function; onClick?: Function;
testEnabled?: () => boolean; testEnabled?: () => boolean;
@@ -20,6 +22,7 @@ export interface GlobalCommand {
enabled?: boolean; enabled?: boolean;
showDisabled?: boolean; showDisabled?: boolean;
toolbarName?: string; toolbarName?: string;
menuName?: string;
toolbarOrder?: number; toolbarOrder?: number;
disableHandleKeyText?: string; 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 ConnectionModal from '../modals/ConnectionModal.svelte';
import { showModal } from '../modals/modalTools'; import { showModal } from '../modals/modalTools';
import newQuery from '../query/newQuery'; import newQuery from '../query/newQuery';
import saveTabFile, { saveTabEnabledStore } from '../utility/saveTabFile'; import saveTabFile from '../utility/saveTabFile';
import openNewTab from '../utility/openNewTab'; import openNewTab from '../utility/openNewTab';
function themeCommand(theme: ThemeDefinition) { 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({ export function registerFileCommands({
idPrefix, idPrefix,
category, category,
@@ -122,6 +131,7 @@ export function registerFileCommands({
}) { }) {
registerCommand({ registerCommand({
id: idPrefix + '.save', id: idPrefix + '.save',
group: 'save',
category, category,
name: 'Save', name: 'Save',
keyText: 'Ctrl+S', keyText: 'Ctrl+S',

View File

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

View File

@@ -1,6 +1,8 @@
import { writable, derived, readable } from 'svelte/store'; import { writable, derived, readable } from 'svelte/store';
import { ExtensionsDirectory } from 'dbgate-types'; import { ExtensionsDirectory } from 'dbgate-types';
import invalidateCommands from './commands/invalidateCommands'; import invalidateCommands from './commands/invalidateCommands';
import getElectron from './utility/getElectron';
import { GlobalCommand } from './commands/registerCommand';
interface TabDefinition { interface TabDefinition {
title: string; title: string;
@@ -44,6 +46,8 @@ export const nullStore = readable(null, () => {});
export const currentArchive = writable('default'); export const currentArchive = writable('default');
export const isFileDragActive = writable(false); export const isFileDragActive = writable(false);
const electron = getElectron();
subscribeCssVariable(selectedWidget, x => (x ? 1 : 0), '--dim-visible-left-panel'); subscribeCssVariable(selectedWidget, x => (x ? 1 : 0), '--dim-visible-left-panel');
subscribeCssVariable(visibleToolbar, x => (x ? 1 : 0), '--dim-visible-toolbar'); subscribeCssVariable(visibleToolbar, x => (x ? 1 : 0), '--dim-visible-toolbar');
subscribeCssVariable(leftPanelWidth, x => `${x}px`, '--dim-left-panel-width'); subscribeCssVariable(leftPanelWidth, x => `${x}px`, '--dim-left-panel-width');
@@ -75,3 +79,26 @@ openedTabs.subscribe(value => {
invalidateCommands(); invalidateCommands();
}); });
export const getOpenedTabs = () => openedTabsValue; 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;