Skip to content

Allow customization of default dialogs #2522

Closed

Description

@etiktin

I think it would be cool if we allowed customization of default dialogs (alert, confirm and prompt), by registering a handler. So for example if I want my alerts to have a custom title and icon, I could do something like this:

mainWindow.onAlert = function (msg) {
    var dialog = remote.require('dialog');
    dialog.showMessageBox(
        mainWindow,
        {
            type: 'none',
            icon: './alert.png',
            title: 'Foobar',
            message: msg,
            buttons: ['Click me'],
            noLink: true
        }
    );
}

You could even translate the title, labels etc. (see #2518 for a related issue).

If we will have support for default "confirm navigation" dialog (see #2481), it will be great if we can customize that too.

The handlers should probably always use dialog.showMessageBox, because these dialogs are supposed to be modal and synchronous. So maybe it would make more sense for the handlers to just return the options parameter for dialog.showMessageBox. For example:

mainWindow.onAlert = function (msg) {
    return {
            type: 'none',
            icon: './alert.png',
            title: 'Foobar',
            message: msg,
            buttons: ['Click me'],
            noLink: true
    };
}

To be fair, you could accomplish some of these stuff by hooking the methods or using custom methods. That said, I think it's pretty common to want to customize them and Electron as a framework for building applications should expose an API for that.

What do you think?

Activity

  1. duanyao commented on Aug 18, 2015

    @duanyao

    I think it is even better to be able to intercept dialogs globally, so you don't have to change every site that creates BrowserWindow:

    app.on('dialog-alert', function(ev) {
      //...
    });
  2. etiktin commented on Aug 19, 2015

    @etiktin
    ContributorAuthor

    Some users are running multi-apps through a single app. They basically just open new windows for each app from a single master process (see for example #2415 and #2438). So I think it's better to leave the configuration on the BrowserWindow which will give the option to configure the look differently per window (e.g. they can set different dialog title per window). If you want to set the same options for all windows, you could still do that by wrapping the window creation with code to set up the options.

    The more I think about it, I don't see a reason to treat this like setting an event handler. We just want to allow customization of the modal dialog, which means the options used to create it. So maybe something like this will be better:

    mainWindow.alertDialogOptions = {
        type: 'none',
        icon: './alert.png',
        title: 'Foobar',
        buttons: ['Click me'],
        noLink: true
    };

    When an alert is called, the modal dialog creation code, will check if the developer supplied an options object and if so it will customize the dialog according to it and then display it.
    This approach seems simpler and cleaner to me.

  3. duanyao commented on Aug 20, 2015

    @duanyao

    There are requirements other than just changing the look and feel of dialogs, e.g.:

    • Block dialogs from all or some pages, according to user's setting.
    • Fill authentication dialogs with saved usernames/passwords.

    Additionally, one may want to set the title/icon of dialogs according to the title/favicon of the web page that triggered it.

    I think the event handler approach provides all the flexibility. We may add properties such as browserWindow (maybe window too for iframes) to the event object, so that developer can get all context info about the dialog to be shown.

  4. zcbenz commented on Aug 25, 2015

    @zcbenz
    Contributor

    For overriding the alert box you can simply do it in preload script:

    window.alert = ...;

    That's also how we implemented alert in Electron:
    https://github.com/atom/electron/blob/master/atom/renderer/lib/override.coffee#L78-L88

  5. alexindigo commented on Aug 25, 2015

    @alexindigo

    There is one difference between native and custom alert – custom one is not blocking, although it's better UX in most cases, might be a problem for some.

  6. etiktin commented on Aug 25, 2015

    @etiktin
    ContributorAuthor

    @alexindigo, what do you mean when you say that custom alert doesn't block?
    If you implement it using dialog.showMessageBox(), just don't provide a callback and the call will be synchronous/blocking. For alert it doesn't matter that match, but confirm must be synchronous because you need to return the result to the caller.

  7. alexindigo commented on Aug 25, 2015

    @alexindigo

    @etiktin Hey, sorry for the confusion, I missed that part completely. Thank you for the clarification.

  8. duanyao commented on Aug 26, 2015

    @duanyao

    @zcbenz preload script looks awesome, but how about browsing contexts that are not created by new BrowserWindow(...), e.g. window.open(), <a target="_blank">, <iframe>, etc.?

  9. zcbenz commented on Aug 26, 2015

    @zcbenz
    Contributor

    @duanyao Good question, I can't find a existing way to solve that. One possible solution is allow setting preload script for the whole session, so ever page created for the session will load the preload script:

    var session = xxxx.createSession({preload: '/script.js'});
    var window = new BrowserWindow({session: session});

    How does it sound?

  10. duanyao commented on Aug 26, 2015

    @duanyao

    Session may work.

    Another (more general) solution is "browsing context life circle events", so you can inject different scripts to different pages, at different loading states (e.g. before parsing HTML, ondomcontentloaded, or onload). One example is Geckofx, it has Navigating, Navigated, DocumentCompleted, CreateWindow events, and you can retrieval the URL and call EvaluateScript() to inject scripts in the handlers.

    I noticed that Electron class WebContents has did-start-loading, did-frame-finish-load, new-window events and executeJavaScript(), seems potentially being able to achive this purpose?

  11. zcbenz commented on Aug 26, 2015

    @zcbenz
    Contributor

    I noticed that Electron class WebContents has did-start-loading, did-frame-finish-load, new-window events and executeJavaScript(), seems potentially being able to achive this purpose?

    We still need to add all those events to Session though, otherwise we can still not handle pages opened by window.open.

  12. duanyao commented on Aug 26, 2015

    @duanyao

    otherwise we can still not handle pages opened by window.open.

    Doesn't new-window event report windows created by window.open?

  13. etiktin commented on Aug 26, 2015

    @etiktin
    ContributorAuthor

    Currently there's no support for alert and confirm in iframes. The preload script for Sessions sounds like a good solution for customizing the dialogs, but I think we also need a default preload (kind of like what override.coffee does for the parent window).

    Will this work even when websecurity is set to true and the child window(s) belongs to a different origin?

  14. zcbenz commented on Aug 27, 2015

    @zcbenz
    Contributor

    Will this work even when websecurity is set to true and the child window(s) belongs to a different origin?

    Yeah preload script is loaded regardless of web security, and child windows always have the same session.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions