简体   繁体   English

Electron 应用程序:如何制作 dialog.showOpenDialog 模态

[英]Electron app: How to make dialog.showOpenDialog modal

In my small Electron app I have a couple of buttons to allow the user to browse for folders to use for the processing the app does.在我的小型 Electron 应用程序中,我有几个按钮允许用户浏览文件夹以用于处理应用程序。 The call to open the dialog to do this passes the ID of the main browser window so that the dialog will be modal and this initially appeared to work.打开对话框的调用会传递主浏览器 window 的 ID,这样对话框将是模态的,这最初似乎可以工作。 The buttons in the app main screen appear to be disabled.应用程序主屏幕中的按钮似乎已禁用。 However, if the disabled buttons are clicked, when the dialog is closed those clicks are executed.但是,如果单击禁用的按钮,则在关闭对话框时会执行这些单击。 This true of all the buttons in the main screen.主屏幕中的所有按钮都是如此。 If I click on the disabled "Cancel" button while the dialog is showing the app closes when the dialog is closed.如果我在对话框显示应用程序关闭时单击禁用的“取消”按钮,则对话框关闭时。

Seems to me that one should not be able to switch back to the parent of a modal dialog and "store" clicks.在我看来,一个人不应该能够切换回模式对话框的父级并“存储”点击。

The dialog.showOpenDialog call is made in the renderer process, is this possibly the issue? dialog.showOpenDialog 调用是在渲染器进程中进行的,这可能是问题吗?

Sid席德

In the renderer process you need to use the browser window reference, not the ID. 在渲染器过程中,您需要使用浏览器窗口引用,而不是ID。 You can get the reference in the renderer process by using: remote.getCurrentWindow() . 您可以使用以下方法在渲染器过程中获取引用: remote.getCurrentWindow() You can make the call as follows. 您可以按以下方式拨打电话。 The example is specifically for opening multiple files, should be configured as needed of course: 该示例专门用于打开多个文件,当然应该根据需要进行配置:

const { remote } = window.require('electron')
const dialog = remote.dialog

dialog.showOpenDialog(
  remote.getCurrentWindow(),
  { properties: ['openFile', 'multiSelections'] }, 
  (filePaths?) => {
      // do your thing
    }
  )

Not sure what was going on previously, I now cannot reproduce the problem, so I am going to mark this for closure. 不知道以前发生了什么,我现在无法重现该问题,因此我将其标记为关闭。

Sorry for the noise, Sid 对不起,Sid

You don't need electron/remote to achieve this, in fact, electron/remote is deprecated.您不需要电子/远程来实现这一点,事实上,电子/远程已被弃用。 The key is to provide the browser window's reference to the showOpenDialog API (in the main process).关键是提供浏览器窗口对 showOpenDialog API 的引用(在主进程中)。 This is a general API pattern in window managers: in order to have a modal window, you have to specify a parent window.这是 window 管理器中的通用 API 模式:为了具有模态 window,您必须指定父 Z05B8C74CBD96FBFF24207。 Code sample:代码示例:

const ipcApi = {
  'select-file': async () => {
    // It's important to pass the window handler in showOpenDialog
    // in order to make the dialog modal
    const browserWindow = BrowserWindow.getFocusedWindow()
    if (browserWindow) {
      return dialog.showOpenDialog(browserWindow, { properties: ['openFile'] })
    }
    return
  },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 修复电子模块远程和 dialog.showOpenDialog - fix for electron modules remote & dialog.showOpenDialog 电子dialog.showOpenDialog()过滤器不起作用 - Electron dialog.showOpenDialog() Filters not working Electron - dialog.showOpenDialog 如何打开目录选择对话框并在 Windows 上显示“灰色”文件 - Electron - dialog.showOpenDialog how to open directory selection dialog box and show "grayed out" files on windows 如何制作jQuery对话框模态? - How to make jQuery dialog modal? 如何使 Material UI 模态和对话框可滚动? - How to make Material UI modal and dialog scrollable? Electron 19 showOpenDialog“无法读取未定义的属性(读取‘showOpenDialog’)” - Electron 19 showOpenDialog "Cannot read properties of undefined (reading 'showOpenDialog')" 如何创建以模式对话框开头的团队应用程序 - How to create a Teams app that starts with a modal dialog ShowOpenDialog 不适用于最新版本的电子 js - ShowOpenDialog not working on recent versions of electron-js 如何只单击模态外部而不单击对话框/内容? - How to make click only on modal exterior and not on dialog/content? 如何在引导程序中的页面加载时打开模态对话框 - How to make modal dialog open at page load in bootstrap
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM