Skip to content

Commit

Permalink
Add option to configure launching a web app (closes #265, closes #291)
Browse files Browse the repository at this point in the history
This option replaces an existing `firefoxpwa.openInExistingWindow` preference to allow further configuration how the web app should be launched when it is already opened.

It is now possible to open a new window (default behaviour), open a new tab in the existing window (behavior of previous preference), replace an existing tab, or just focus the window.
  • Loading branch information
filips123 committed Feb 7, 2023
1 parent 14e8473 commit da6af37
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 13 deletions.
21 changes: 17 additions & 4 deletions native/userchrome/profile/chrome/pwa/boot.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,26 @@ function launchSite (siteUrl, siteConfig, isStartup) {
gSystemPrincipal,
];

// Open a web app in an existing window of that web app (if enabled)
// Handle launching a web app when the same web app is already opened
// We have to specify pref directly as we cannot access ChromeLoader yet
if (Services.prefs.getBoolPref('firefoxpwa.openInExistingWindow', false)) {
const launchType = Services.prefs.getIntPref('firefoxpwa.launchType', 0);
if (launchType) {
for (const win of Services.wm.getEnumerator('navigator:browser')) {
if (win.gFFPWASiteConfig?.ulid === siteConfig.ulid) {
win.openTrustedLinkIn(siteUrl, 'tab', {});
return win;
switch (launchType) {
case 1:
// Open a new tab in the existing window
win.openTrustedLinkIn(siteUrl, 'tab', {});
return win;
case 2:
// Open in an existing tab in the existing window
win.openTrustedLinkIn(siteUrl, 'current', {});
return win;
case 3:
// Focus the existing window
win.focus();
return win;
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions native/userchrome/profile/chrome/pwa/chrome.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class ChromeLoader {
static DISTRIBUTION_ABOUT = 'With modifications by the PWAsForFirefox project';

static PREF_LINKS_TARGET = 'firefoxpwa.linksTarget';
static PREF_LAUNCH_TYPE = 'firefoxpwa.launchType';
static PREF_DISPLAY_URL_BAR = 'firefoxpwa.displayUrlBar';
static PREF_AUTOHIDE_MUTE_BUTTON = 'firefoxpwa.autohideMuteButton';
static PREF_SITES_SET_THEME_COLOR = 'firefoxpwa.sitesSetThemeColor';
Expand Down
17 changes: 13 additions & 4 deletions native/userchrome/profile/chrome/pwa/content/browser.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -1822,6 +1822,13 @@ class PwaBrowser {
// 3 - Force links into a new tab
xPref.set(ChromeLoader.PREF_LINKS_TARGET, 1, true);

// Determines what happens when a web app is launched if the same web app is already opened
// 0 - Open web app in a new window (default)
// 1 - Open web app in a new tab
// 2 - Replace the existing tab
// 3 - Focus the existing window
xPref.set(ChromeLoader.PREF_LAUNCH_TYPE, 0, true);

// Determines whether URL bar is displayed always, when out-of-scope or never
// 0 - Display URL bar when out-of-scope (default)
// 1 - Never display URL bar (strongly not recommended)
Expand Down Expand Up @@ -1849,10 +1856,6 @@ class PwaBrowser {
// Determines whether out-of-scope URLs should be opened in a default browser
xPref.set(ChromeLoader.PREF_OPEN_OUT_OF_SCOPE_IN_DEFAULT_BROWSER, false, true);

// Determines whether to open a web app in an existing window of that web app
// Should only be used when the tabs mode is enabled
xPref.set(ChromeLoader.PREF_OPEN_IN_EXISTING_WINDOW, false, true);

// Determines whether the tabs mode is enabled
xPref.set(ChromeLoader.PREF_ENABLE_TABS_MODE, false, true);

Expand All @@ -1872,6 +1875,12 @@ class PwaBrowser {
xPref.set(ChromeLoader.PREF_SHORTCUTS_CLOSE_WINDOW, true, true);
xPref.set(ChromeLoader.PREF_SHORTCUTS_QUIT_APPLICATION, true, true);
xPref.set(ChromeLoader.PREF_SHORTCUTS_PRIVATE_BROWSING, true, true);

// Migration from the old "open in existing window" preference
if (xPref.get(ChromeLoader.PREF_OPEN_IN_EXISTING_WINDOW)) {
xPref.clear(ChromeLoader.PREF_OPEN_IN_EXISTING_WINDOW);
xPref.set(ChromeLoader.PREF_LAUNCH_TYPE, 1);
}
}

disableOnboarding () {
Expand Down
23 changes: 18 additions & 5 deletions native/userchrome/profile/chrome/pwa/content/preferences.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class PwaPreferences {
addPreferenceData () {
Preferences.addAll([
{ id: ChromeLoader.PREF_LINKS_TARGET, type: 'int' },
{ id: ChromeLoader.PREF_LAUNCH_TYPE, type: 'int' },
{ id: ChromeLoader.PREF_DISPLAY_URL_BAR, type: 'int' },
{ id: ChromeLoader.PREF_SITES_SET_THEME_COLOR, type: 'bool' },
{ id: ChromeLoader.PREF_SITES_SET_BACKGROUND_COLOR, type: 'bool' },
Expand All @@ -25,7 +26,6 @@ class PwaPreferences {
{ id: ChromeLoader.PREF_DYNAMIC_WINDOW_ICON, type: 'bool' },
{ id: ChromeLoader.PREF_ALWAYS_USE_NATIVE_WINDOW_CONTROLS, type: 'bool' },
{ id: ChromeLoader.PREF_OPEN_OUT_OF_SCOPE_IN_DEFAULT_BROWSER, type: 'bool' },
{ id: ChromeLoader.PREF_OPEN_IN_EXISTING_WINDOW, type: 'bool' },
{ id: ChromeLoader.PREF_ENABLE_TABS_MODE, type: 'bool' },
{ id: ChromeLoader.PREF_ALLOWED_DOMAINS, type: 'wstring' },
{ id: ChromeLoader.PREF_SHORTCUTS_CLOSE_TAB, type: 'bool' },
Expand Down Expand Up @@ -58,7 +58,6 @@ class PwaPreferences {
<vbox id="uxBox" style="padding-top: 1rem;">
<checkbox preference="${ChromeLoader.PREF_OPEN_OUT_OF_SCOPE_IN_DEFAULT_BROWSER}" label="Open out-of-scope URLs in a default browser (can break some web apps)" />
<checkbox preference="${ChromeLoader.PREF_ENABLE_TABS_MODE}" label="Show browser tabs and enable using multi-tabbed web apps" />
<checkbox id="openInExistingWindowCheckbox" preference="${ChromeLoader.PREF_OPEN_IN_EXISTING_WINDOW}" label="Open a web app in an existing window of that web app" />
</vbox>
<vbox id="linksTargetBox" style="padding-top: 1rem;">
Expand All @@ -75,6 +74,20 @@ class PwaPreferences {
</vbox>
</vbox>
<vbox id="launchTypeBox" style="padding-top: 1rem;">
<label>
<description>When launching a web app that is already opened</description>
</label>
<vbox>
<radiogroup id="launchTypeRadioGroup" preference="${ChromeLoader.PREF_LAUNCH_TYPE}">
<radio value="0" id="launchTypeNewWindow" label="Open web app in a new window" />
<radio value="1" id="launchTypeNewTab" label="Open web app in a new tab" />
<radio value="2" id="launchTypeReplace" label="Replace the existing tab" />
<radio value="3" id="launchTypeFocus" label="Focus the existing window" />
</radiogroup>
</vbox>
</vbox>
<vbox id="displayUrlBarBox" style="padding-top: 1rem;">
<label>
<description>Display the address bar</description>
Expand Down Expand Up @@ -137,7 +150,7 @@ class PwaPreferences {
handleTabsModePreferenceSwitch (onLoad = false) {
function setTabsSectionDisabled(disabled) {
document.querySelectorAll('#mainPrefPane > groupbox:nth-child(8) > *').forEach(elem => elem.disabled = disabled)
document.querySelector('#openInExistingWindowCheckbox').disabled = disabled
document.querySelector('#launchTypeNewTab').disabled = disabled
}

if (xPref.get(ChromeLoader.PREF_ENABLE_TABS_MODE)) {
Expand All @@ -146,10 +159,10 @@ class PwaPreferences {
setTimeout(() => setTabsSectionDisabled(false), 100);
if (!onLoad) xPref.set(ChromeLoader.PREF_LINKS_TARGET, 3);
} else {
// If the tabs mode is disabled, disable the tabs section and open in existing window
// If the tabs mode is disabled, disable the tabs section and reset launch type
setTabsSectionDisabled(true)
setTimeout(() => setTabsSectionDisabled(true), 100);
xPref.set(ChromeLoader.PREF_OPEN_IN_EXISTING_WINDOW, false);
xPref.clear(ChromeLoader.PREF_LAUNCH_TYPE);

if (!onLoad) {
// If out-of-scope URLs in a default browser is enabled, keep the links target unchanged
Expand Down

0 comments on commit da6af37

Please sign in to comment.