Skip to content

Commit

Permalink
#852: Added calls to menu items for main and context, based on whethe…
Browse files Browse the repository at this point in the history
…r 'lockable'
  • Loading branch information
Matthew Mulholland committed Jun 27, 2019
1 parent fc2596d commit b8f5da9
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/main/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ class AppMenu {
{
label: 'Guess Column Properties',
accelerator: 'Shift+CmdOrCtrl+G',
lockable: true,
click: function () {
webContents().send('guessColumnProperties')
}
Expand Down
25 changes: 22 additions & 3 deletions src/main/menuUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ export function disableAllFromLabels(menuLabel, subMenuLabels) {
}
}

export function disableEnableBasedOnAttributeAndConditionFromLabels(menuLabels, attribute, condition) {
for (const nextLabel of menuLabels) {
let menu = getMenu(nextLabel)
menu.submenu.items.forEach(function (x) {
if (typeof x.label !== 'undefined' && x[attribute]) {
x.enabled = condition
}
})
}
}

export function enableAllSubMenuItemsFromMenuLabel (menuLabel) {
let menu = getMenu(menuLabel)
enableAllSubMenuItemsFromMenuObject(menu)
Expand Down Expand Up @@ -59,11 +70,19 @@ export function getSubMenuLabelsFromMenu (menuLabel) {
}

export function disableOpenFileItems() {
applyFnsToLabelsFromMenuLabel('File', ['Open Excel Sheet...', 'Open', 'Open Data Package'], disableAllSubMenuItemsFromMenuObject, disableMenuItem)
disableMenuItems('File', ['Open Excel Sheet...', 'Open', 'Open Data Package'])
}

export function disableMenuItems(menuLabel, subMenuLabels) {
applyFnsToLabelsFromMenuLabel(menuLabel, subMenuLabels, disableAllSubMenuItemsFromMenuObject, disableMenuItem)
}

export function enableOpenFileItems() {
applyFnsToLabelsFromMenuLabel('File', ['Open Excel Sheet...', 'Open', 'Open Data Package'], enableAllSubMenuItemsFromMenuObject, enableMenuItem)
enableMenuItems('File', ['Open Excel Sheet...', 'Open', 'Open Data Package'])
}

export function enableMenuItems(menuLabel, subMenuLabels) {
applyFnsToLabelsFromMenuLabel(menuLabel, subMenuLabels, enableAllSubMenuItemsFromMenuObject, enableMenuItem)
}

export function applyFnsToLabelsFromMenuLabel(menuLabel, labels, subMenuFn, menuItemFn) {
Expand Down Expand Up @@ -103,7 +122,7 @@ export function getSubMenusAndItemsFromMenu (menu) {
// console.log(`skipping `, next.type)
}
}
return { subMenusOnly, menuItemsOnly }
return [ subMenusOnly, menuItemsOnly ]
}

export function getSubMenuFromMenu (menuLabel, subMenuLabel) {
Expand Down
8 changes: 4 additions & 4 deletions src/main/rendererToMain.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { ipcMain as ipc, dialog } from 'electron'
import { ipcMain as ipc, dialog, Menu } from 'electron'
import { showErrors } from './errors.js'
import {
getMenu,
getSubMenuFromMenu,
clickLabelsOnMenu,
disableAllSubMenuItemsFromMenuObject,
enableSubMenuItemsFromMenuObject,
enableAllSubMenuItemsFromMenuLabel
enableAllSubMenuItemsFromMenuLabel,
disableEnableBasedOnAttributeAndConditionFromLabels
} from './menuUtils.js'
import { focusMainWindow, closeSecondaryWindow } from './windows.js'
import { loadPackageJson, loadResourceDataFromPackageUrl } from './url.js'
Expand All @@ -31,8 +32,7 @@ ipc.on('hasLockedActiveTable', (event, arg) => {
let lockedSubMenu = getSubMenuFromMenu('Tools', 'Lock Column Properties')
lockedSubMenu.checked = arg
// for locked table (ie: lock is enabled), value is true, so any menu 'enabled': set to false
let guessSubMenu = getSubMenuFromMenu('Tools', 'Guess Column Properties')
guessSubMenu.enabled = !arg
disableEnableBasedOnAttributeAndConditionFromLabels(['Edit', 'Tools'], 'lockable', !arg)
})

ipc.on('showErrorsWindow', (event, arg) => {
Expand Down
4 changes: 4 additions & 0 deletions src/renderer/components/Home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,9 @@ import {
loadData,
getWindow
} from '../index.js'
import {
disableEnableContextMenu
} from '../menu.js'
import {
HotRegister,
getColumnCount,
Expand Down Expand Up @@ -457,6 +460,7 @@ export default {
this.$subscribeTo(allTableLocks$, async function(allTablesLocks) {
self.isActiveTabLocked = _.includes(allTablesLocks, self.currentHotId)
disableEnableContextMenu(self.isActiveTabLocked)
ipc.send('hasLockedActiveTable', self.isActiveTabLocked)
})
// request may be coming from another page - get focus first
Expand Down
11 changes: 10 additions & 1 deletion src/renderer/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { insertRowAbove, insertRowBelow, insertColumnBefore, insertColumnAfter,
import { sharedMenus } from '@/sharedWithMain.js'
import _ from 'lodash'
import { remote } from 'electron'
import { getMenu } from '../main/menuUtils'
const Menu = remote.Menu
const MenuItem = remote.MenuItem

Expand All @@ -10,7 +11,7 @@ function buildMenuItems(options, clickFn) {
return nextMenu
}

let menu = new Menu()
var menu = new Menu()

menu.append(new MenuItem({ type: 'separator' }))
menu.append(buildMenuItems(sharedMenus.insertRowAbove, insertRowAbove))
Expand All @@ -22,6 +23,14 @@ menu.append(new MenuItem({ type: 'separator' }))
menu.append(buildMenuItems(sharedMenus.removeRows, removeRows))
menu.append(buildMenuItems(sharedMenus.removeColumns, removeColumns))

export function disableEnableContextMenu(isLocked) {
menu.items.forEach(function (x) {
if (typeof x.label !== 'undefined' && x['lockable']) {
x.enabled = !isLocked
}
})
}

export {
menu, sharedMenus
}

0 comments on commit b8f5da9

Please sign in to comment.