码迷,mamicode.com
首页 > 系统相关 > 详细

08.《Electron 跨平台开发实战》- chapter08-深入集成(shell模块)、动态启动菜单项

时间:2020-06-16 18:13:06      阅读:67      评论:0      收藏:0      [点我收藏+]

标签:efault   log   打开   开发实战   menu   not show   containe   orb   turn   

在渲染进程(UI界面)中使用shell模块

//在文件管理器中打开
const { ..., shell } = require(‘electron‘);

const showFile = () => {
    if (!filePath) { return alert(‘This file has not been saved to the file system.‘); }
    shell.showItemInFolder(filePath);
};

//使用默认程序打开
const openInDefaultApplication = () => {
    if (!filePath) { return alert(‘This file has not been saved to the file system.‘); }
    //electron-9.x 已经不存在了
    shell.openItem(fullPath)
};

在应用菜单中使用 shell 模块

  • applicationMenu.js
 { type: ‘separator‘ },
        {
          label: ‘Show File‘,
          enabled: hasFilePath,
          click(item, focusedWindow) {
            if (!focusedWindow) {
              return dialog.showErrorBox(
                ‘Cannot Show File\‘s Location‘,
                ‘There is currently no active document show.‘
              );
            }
            focusedWindow.webContents.send(‘show-file‘);
          },
        },
        {
          label: ‘Open in Default Application‘,
          enabled: hasFilePath,
          click(item, focusedWindow) {
            if (!focusedWindow) {
              return dialog.showErrorBox(
                ‘Cannot Open File in Default Application‘,
                ‘There is currently no active document to open.‘
              );
            }
            focusedWindow.webContents.send(‘open-in-default‘);
          },
        },
  • renderer.js
ipcRenderer.on(‘show-file‘, showFile);
ipcRenderer.on(‘open-in-default‘, openInDefaultApplication);

在上下文菜单中使用 shell 模块

  • renderer.js

//上下文菜单
const markdownContextMenu = Menu.buildFromTemplate([
...
    {
        label: ‘Show File in Folder‘,
        click: showFile,
        enabled: !!filePath
      },
      {
        label: ‘Open in Default‘,
        click: openInDefaultApplication,
        enabled: !!filePath
      },
    { type: ‘separator‘ },
   ...
]);

markdownView.addEventListener(‘contextmenu‘, (event) => {
    event.preventDefault();
    markdownContextMenu.popup();
});

禁用菜单项

08.《Electron 跨平台开发实战》- chapter08-深入集成(shell模块)、动态启动菜单项

标签:efault   log   打开   开发实战   menu   not show   containe   orb   turn   

原文地址:https://www.cnblogs.com/easy5weikai/p/13143293.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!