WICG / web-app-launch

Web App Launch Handler
Other
75 stars 28 forks source link

Should calling `client.focus()` imply `preventDefault()` #6

Open fallaciousreasoning opened 5 years ago

fallaciousreasoning commented 5 years ago

My intuition here is that it should, as otherwise we open the path for strange behavior (I called client.focus() but the browser still opened a new window!).

This raises the question of whether preventDefault even needs to be part of the spec, as the event already MUST focus a client or show a notification in order to prevent default behavior. This means that a call to preventDefault is either: a) Superfluous, we already know the app intends to preventDefault, they called client.focus() or displayed a notification or b) A no-op, the app called preventDefault but they didn't focus a client/show a notification, so the browser does whatever it normally does.

For example, compare (explicit preventDefault):

self.addEventListener('launch', event => {
    event.waitUntil(async () => {
      // Call to prevent default. Be careful not to forget!
      event.preventDefault();

      const allClients = await clients.matchAll();
      // If there isn't one available, open a window.
      if (allClients.length === 0) {
        const client = clients.openWindow('/');
        client.postMessage(event.files);
        client.focus();
        return;
      }

      const client = allClients[0];
      client.postMessage(event.files);
      client.focus();
    }());
  });

and (with preventDefault implicit)

self.addEventListener('launch', event => {
    event.waitUntil(async () => {
      const allClients = await clients.matchAll();
      // If there isn't one available, open a window.
      if (allClients.length === 0) {
        const client = clients.openWindow('/');
        client.postMessage(event.files);
        client.focus();
        return;
      }

      const client = allClients[0];
      client.postMessage(event.files);
      client.focus();
    }());
  });

@raymeskhoury

raymeskhoury commented 5 years ago

I think you're probably right. Maybe we don't need preventDefault.

fallaciousreasoning commented 5 years ago

Recently, it was raised that it may be difficult to determine which handler has called focus/open/showNotification, so if we continue down this avenue we may need the call to preventDefault after all.