From b6241c3186dee5da6ea6a294969ba31ad314de65 Mon Sep 17 00:00:00 2001 From: Christian Dywan Date: Thu, 28 Feb 2019 19:40:28 +0100 Subject: Use a dialog to handle script confirm/ prompt (#279) The WebKit API can't cope with asynchronously updating the result. Fixes: #276 --- core/browser.vala | 41 ++++++++++++++++++++++++++++------------- core/tab.vala | 29 ++++++----------------------- ui/tab.ui | 4 ---- 3 files changed, 34 insertions(+), 40 deletions(-) diff --git a/core/browser.vala b/core/browser.vala index 27096385..73d4e008 100644 --- a/core/browser.vala +++ b/core/browser.vala @@ -625,26 +625,41 @@ namespace Midori { tab.print (new WebKit.PrintOperation (tab)); } - void caret_browsing_activated () { - var settings = CoreSettings.get_default (); - if (!settings.enable_caret_browsing) { - var dialog = new Gtk.Dialog.with_buttons ( - get_settings ().gtk_dialogs_use_header ? null : _("Toggle text cursor navigation"), - this, - get_settings ().gtk_dialogs_use_header ? Gtk.DialogFlags.USE_HEADER_BAR : 0, - Gtk.Stock.CANCEL, Gtk.ResponseType.CANCEL, - _("_Enable Caret Browsing"), Gtk.ResponseType.ACCEPT); - var label = new Gtk.Label (_("Pressing F7 toggles Caret Browsing. When active, a text cursor appears in all websites.")); + internal string? prompt (string title, string message, string confirm, string? text=null) { + var dialog = new Gtk.Dialog.with_buttons ( + get_settings ().gtk_dialogs_use_header ? null : (title ?? message), + this, + get_settings ().gtk_dialogs_use_header ? Gtk.DialogFlags.USE_HEADER_BAR : 0, + Gtk.Stock.CANCEL, Gtk.ResponseType.CANCEL, + confirm, Gtk.ResponseType.ACCEPT); + if (message != null) { + var label = new Gtk.Label (message); label.wrap = true; label.max_width_chars = 33; label.margin = 8; label.show (); dialog.get_content_area ().add (label); - dialog.set_default_response (Gtk.ResponseType.ACCEPT); - if (dialog.run () == Gtk.ResponseType.ACCEPT) { + } + var entry = new Gtk.Entry (); + if (text != null) { + entry.text = text; + entry.show (); + dialog.get_content_area ().add (entry); + } + dialog.set_default_response (Gtk.ResponseType.ACCEPT); + string? result = dialog.run () == Gtk.ResponseType.ACCEPT ? (entry.get_text () ?? "") : null; + dialog.close (); + return result; + } + + void caret_browsing_activated () { + var settings = CoreSettings.get_default (); + if (!settings.enable_caret_browsing) { + if (prompt (_("Toggle text cursor navigation"), + _("Pressing F7 toggles Caret Browsing. When active, a text cursor appears in all websites."), + _("_Enable Caret Browsing")) != null) { settings.enable_caret_browsing = true; } - dialog.close (); } else { settings.enable_caret_browsing = false; } diff --git a/core/tab.vala b/core/tab.vala index fd574827..815c5be5 100644 --- a/core/tab.vala +++ b/core/tab.vala @@ -37,8 +37,6 @@ namespace Midori { [GtkChild] Gtk.Label message; [GtkChild] - Gtk.Entry entry; - [GtkChild] Gtk.Button confirm; construct { @@ -332,37 +330,22 @@ namespace Midori { } public override bool script_dialog (WebKit.ScriptDialog dialog) { - message.label = dialog.get_message (); - switch (dialog.get_dialog_type ()) { case WebKit.ScriptDialogType.ALERT: + message.label = dialog.get_message (); confirm.hide (); + popover.show (); break; case WebKit.ScriptDialogType.CONFIRM: case WebKit.ScriptDialogType.BEFORE_UNLOAD_CONFIRM: - confirm.label = _("_Confirm"); - confirm.visible = true; - popover.closed.connect (() => { - dialog.confirm_set_confirmed (false); - }); - confirm.clicked.connect (() => { - dialog.confirm_set_confirmed (true); - }); + string hostname = new Soup.URI (uri).host; + dialog.confirm_set_confirmed(((Browser)get_toplevel ()).prompt (hostname, dialog.get_message (), _("_Confirm")) != null); break; case WebKit.ScriptDialogType.PROMPT: - entry.placeholder_text = dialog.prompt_get_default_text (); - entry.visible = true; - confirm.label = _("_Confirm"); - confirm.visible = true; - popover.closed.connect (() => { - dialog.prompt_set_text (""); - }); - confirm.clicked.connect (() => { - dialog.prompt_set_text (entry.text); - }); + string hostname = new Soup.URI (uri).host; + dialog.prompt_set_text(((Browser)get_toplevel ()).prompt (hostname, dialog.get_message (), _("_Confirm"), dialog.prompt_get_default_text ())); break; } - popover.show (); return true; } diff --git a/ui/tab.ui b/ui/tab.ui index d63b1291..f691693a 100644 --- a/ui/tab.ui +++ b/ui/tab.ui @@ -14,10 +14,6 @@ yes - - - - no -- cgit v1.2.1