From facdf3ca44c170aec9466b7e309a209de0c7f643 Mon Sep 17 00:00:00 2001 From: Jaakko Keränen Date: Sat, 19 Dec 2020 07:33:34 +0200 Subject: Opening unrecognized schemes in default browser This works if the OS supports the scheme as built-in or via some handler application. IssueID #103 IssueID #84 --- src/ui/documentwidget.c | 36 ++++++++++++++++++++++-------------- src/ui/util.c | 5 +++++ src/ui/util.h | 1 + 3 files changed, 28 insertions(+), 14 deletions(-) (limited to 'src/ui') diff --git a/src/ui/documentwidget.c b/src/ui/documentwidget.c index 22fa29f8..3f3e453a 100644 --- a/src/ui/documentwidget.c +++ b/src/ui/documentwidget.c @@ -370,7 +370,7 @@ static void addVisible_DocumentWidget_(void *context, const iGmRun *run) { if (run->audioId) { pushBack_PtrArray(&d->visiblePlayers, run); } - if (run->linkId && linkFlags_GmDocument(d->doc, run->linkId) & supportedProtocol_GmLinkFlag) { + if (run->linkId) { pushBack_PtrArray(&d->visibleLinks, run); } } @@ -2147,13 +2147,10 @@ static iBool processEvent_DocumentWidget_(iDocumentWidget *d, const SDL_Event *e const iGmRun *run = i.ptr; if (run->flags & decoration_GmRunFlag && visibleLinkOrdinal_DocumentWidget_(d, run->linkId) == ord) { - const int kmods = keyMods_Sym(SDL_GetModState()); postCommandf_App("open newtab:%d url:%s", - ((kmods & KMOD_PRIMARY) && (kmods & KMOD_SHIFT)) ? 1 - : (kmods & KMOD_PRIMARY) ? 2 - : 0, + openTabMode_Sym(SDL_GetModState()), cstr_String(absoluteUrl_String( - d->mod.url, linkUrl_GmDocument(d->doc, run->linkId)))); + d->mod.url, linkUrl_GmDocument(d->doc, run->linkId)))); iChangeFlags(d->flags, showLinkNumbers_DocumentWidgetFlag, iFalse); invalidateVisibleLinks_DocumentWidget_(d); refresh_Widget(d); @@ -2315,7 +2312,7 @@ static iBool processEvent_DocumentWidget_(iDocumentWidget *d, const SDL_Event *e &(iMenuItem){ "Open Link in Default Browser", 0, 0, - format_CStr("!open url:%s", cstr_String(linkUrl)) }); + format_CStr("!open default:1 url:%s", cstr_String(linkUrl)) }); } if (willUseProxy_App(scheme)) { pushBackN_Array( @@ -2437,10 +2434,10 @@ static iBool processEvent_DocumentWidget_(iDocumentWidget *d, const SDL_Event *e setFocus_Widget(NULL); if (d->hoverLink) { const iGmLinkId linkId = d->hoverLink->linkId; + const int linkFlags = linkFlags_GmDocument(d->doc, linkId); iAssert(linkId); /* Media links are opened inline by default. */ - if (isMediaLink_GmDocument(d->doc, linkId)) { - const int linkFlags = linkFlags_GmDocument(d->doc, linkId); + if (isMediaLink_GmDocument(d->doc, linkId)) { if (linkFlags & content_GmLinkFlag && linkFlags & permanent_GmLinkFlag) { /* We have the content and it cannot be dismissed, so nothing further to do. */ @@ -2490,15 +2487,26 @@ static iBool processEvent_DocumentWidget_(iDocumentWidget *d, const SDL_Event *e } refresh_Widget(w); } - else { - const int kmods = keyMods_Sym(SDL_GetModState()); + else if (linkFlags & supportedProtocol_GmLinkFlag) { postCommandf_App("open newtab:%d url:%s", - ((kmods & KMOD_PRIMARY) && (kmods & KMOD_SHIFT)) ? 1 - : (kmods & KMOD_PRIMARY) ? 2 - : 0, + openTabMode_Sym(SDL_GetModState()), cstr_String(absoluteUrl_String( d->mod.url, linkUrl_GmDocument(d->doc, linkId)))); } + else { + const iString *url = absoluteUrl_String( + d->mod.url, linkUrl_GmDocument(d->doc, linkId)); + makeQuestion_Widget( + uiTextCaution_ColorEscape "OPEN LINK", + format_CStr( + "Open this link in the default browser?\n" uiTextAction_ColorEscape + "%s", + cstr_String(url)), + (const char *[]){ "Cancel", uiTextCaution_ColorEscape "Open Link" }, + (const char *[]){ + "cancel", format_CStr("!open default:1 url:%s", cstr_String(url)) }, + 2); + } } if (d->selectMark.start) { d->selectMark = iNullRange; diff --git a/src/ui/util.c b/src/ui/util.c index b6378055..855f0fb3 100644 --- a/src/ui/util.c +++ b/src/ui/util.c @@ -135,6 +135,11 @@ int keyMods_Sym(int kmods) { return kmods; } +int openTabMode_Sym(int kmods) { + const int km = keyMods_Sym(kmods); + return ((km & KMOD_PRIMARY) && (km & KMOD_SHIFT)) ? 1 : (km & KMOD_PRIMARY) ? 2 : 0; +} + iRangei intersect_Rangei(iRangei a, iRangei b) { if (a.end < b.start || a.start > b.end) { return (iRangei){ 0, 0 }; diff --git a/src/ui/util.h b/src/ui/util.h index c9f464ad..a280fedb 100644 --- a/src/ui/util.h +++ b/src/ui/util.h @@ -52,6 +52,7 @@ iBool isMod_Sym (int key); int normalizedMod_Sym (int key); int keyMods_Sym (int kmods); /* shift, alt, control, or gui */ void toString_Sym (int key, int kmods, iString *str); +int openTabMode_Sym (int kmods); iRangei intersect_Rangei (iRangei a, iRangei b); iRangei union_Rangei (iRangei a, iRangei b); -- cgit v1.2.3