Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Commit 20eff6d

Browse files
authored
Implement before unload prompt (#3508)
1 parent 6b2659e commit 20eff6d

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

app/src/common/shared/org/mozilla/vrbrowser/browser/PromptDelegate.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.mozilla.vrbrowser.ui.widgets.prompts.ConfirmPromptWidget;
2626
import org.mozilla.vrbrowser.ui.widgets.prompts.PromptWidget;
2727
import org.mozilla.vrbrowser.ui.widgets.prompts.TextPromptWidget;
28+
import org.mozilla.vrbrowser.utils.StringUtils;
2829
import org.mozilla.vrbrowser.utils.UrlUtils;
2930

3031
import java.util.ArrayList;
@@ -303,6 +304,41 @@ public void dismiss() {
303304
});
304305
}
305306

307+
@Nullable
308+
@Override
309+
public GeckoResult<PromptResponse> onBeforeUnloadPrompt(@NonNull GeckoSession session, @NonNull BeforeUnloadPrompt prompt) {
310+
final GeckoResult<PromptResponse> result = new GeckoResult<>();
311+
312+
mPrompt = new ConfirmPromptWidget(mContext);
313+
mPrompt.getPlacement().parentHandle = mAttachedWindow.getHandle();
314+
mPrompt.getPlacement().parentAnchorY = 0.0f;
315+
mPrompt.getPlacement().translationY = WidgetPlacement.unitFromMeters(mContext, R.dimen.js_prompt_y_distance);
316+
String message = mContext.getString(R.string.before_unload_prompt_message);
317+
if (!StringUtils.isEmpty(prompt.title)) {
318+
message = prompt.title;
319+
}
320+
mPrompt.setTitle(mContext.getString(R.string.before_unload_prompt_title));
321+
mPrompt.setMessage(message);
322+
((ConfirmPromptWidget)mPrompt).setButtons(new String[] {
323+
mContext.getResources().getText(R.string.before_unload_prompt_leave).toString(),
324+
mContext.getResources().getText(R.string.before_unload_prompt_stay).toString()
325+
});
326+
mPrompt.setPromptDelegate(new ConfirmPromptWidget.ConfirmPromptDelegate() {
327+
@Override
328+
public void confirm(int index) {
329+
result.complete(prompt.confirm(index == 0 ? AllowOrDeny.ALLOW : AllowOrDeny.DENY));
330+
}
331+
332+
@Override
333+
public void dismiss() {
334+
result.complete(prompt.dismiss());
335+
}
336+
});
337+
mPrompt.show(UIWidget.REQUEST_FOCUS);
338+
339+
return result;
340+
}
341+
306342
// WindowWidget.WindowListener
307343

308344
@Override

app/src/common/shared/org/mozilla/vrbrowser/browser/engine/Session.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,6 +1470,16 @@ public GeckoResult<PromptResponse> onFilePrompt(@NonNull GeckoSession aSession,
14701470
return GeckoResult.fromValue(filePrompt.dismiss());
14711471
}
14721472

1473+
@Nullable
1474+
@Override
1475+
public GeckoResult<PromptResponse> onBeforeUnloadPrompt(@NonNull GeckoSession aSession, @NonNull BeforeUnloadPrompt prompt) {
1476+
if (mPromptDelegate != null) {
1477+
return mPromptDelegate.onBeforeUnloadPrompt(aSession, prompt);
1478+
}
1479+
return GeckoResult.fromValue(prompt.dismiss());
1480+
}
1481+
1482+
14731483
// MediaDelegate
14741484

14751485
@Override

app/src/main/res/values/strings.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,6 +1411,16 @@
14111411
'%1$s' will be replace at runtime with the app's name. -->
14121412
<string name="not_entitled_message">%1$s does not have permission to run on this device and will now exit.</string>
14131413

1414+
1415+
<!-- This string is displayed in the title of an Before Unload prompt, which asks for confirmation to stay or leave the current page. -->
1416+
<string name="before_unload_prompt_title">Leave page?</string>
1417+
<!-- This string is displayed in the message of an Before Unload prompt, which asks for confirmation to stay or leave the current page. -->
1418+
<string name="before_unload_prompt_message">This page is asking to confirm that you want to leave. Data you have entered may not be saved.</string>
1419+
<!-- This string is displayed in a button of the Before Unload prompt. Clicking it cancels the navigation and stays on the same page. -->
1420+
<string name="before_unload_prompt_stay">Stay on Page</string>
1421+
<!-- This string is displayed in a button of the Before Unload prompt. Clicking it confirms the navigation and leaves the page. -->
1422+
<string name="before_unload_prompt_leave">Leave Page</string>
1423+
14141424
<!-- This string is displayed in a button that when pressed allows a user to view a page that has been blocked from being displayed.-->
14151425
<string name="performance_unblock_page">Unblock Page</string>
14161426
<!-- This string is displayed as the title of a dialog displayed when poor web page performance has been detected. -->

0 commit comments

Comments
 (0)