Skip to content

Commit

Permalink
Merge pull request #20 from toshaf/multi-webview
Browse files Browse the repository at this point in the history
Permit multiple Webview instances
  • Loading branch information
MoAlyousef authored Jan 5, 2024
2 parents a8bff08 + 88f36f2 commit 96f12cd
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions fltk-webview-sys/webview/webview.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#include <string>

#ifndef WEBVIEW_H
#define WEBVIEW_H

Expand Down Expand Up @@ -715,6 +718,12 @@ inline id operator"" _str(const char *s, std::size_t) {
return objc::msg_send<id>("NSString"_cls, "stringWithUTF8String:"_sel, s);
}

inline std::string unique_name(std::string base) {
static int name_counter = 0;
base += std::to_string(name_counter++);
return base;
}

class cocoa_wkwebview_engine {
public:
cocoa_wkwebview_engine(bool debug, void *window)
Expand Down Expand Up @@ -827,7 +836,7 @@ class cocoa_wkwebview_engine {
// default name in projects created with Xcode, and using the same name
// causes objc_registerClassPair to crash.
auto cls = objc_allocateClassPair((Class) "NSResponder"_cls,
"WebviewAppDelegate", 0);
unique_name("WebviewAppDelegate").c_str(), 0);
class_addProtocol(cls, objc_getProtocol("NSTouchBarProvider"));
class_addMethod(cls, "applicationShouldTerminateAfterLastWindowClosed:"_sel,
(IMP)(+[](id, SEL, id) -> BOOL { return 1; }), "c@:@");
Expand All @@ -850,7 +859,7 @@ class cocoa_wkwebview_engine {
}
id create_script_message_handler() {
auto cls = objc_allocateClassPair((Class) "NSResponder"_cls,
"WebkitScriptMessageHandler", 0);
unique_name("WebkitScriptMessageHandler").c_str(), 0);
class_addProtocol(cls, objc_getProtocol("WKScriptMessageHandler"));
class_addMethod(
cls, "userContentController:didReceiveScriptMessage:"_sel,
Expand All @@ -868,7 +877,7 @@ class cocoa_wkwebview_engine {
}
static id create_webkit_ui_delegate() {
auto cls =
objc_allocateClassPair((Class) "NSObject"_cls, "WebkitUIDelegate", 0);
objc_allocateClassPair((Class) "NSObject"_cls, unique_name("WebkitUIDelegate").c_str(), 0);
class_addProtocol(cls, objc_getProtocol("WKUIDelegate"));
class_addMethod(
cls,
Expand Down Expand Up @@ -2292,4 +2301,4 @@ WEBVIEW_API const webview_version_info_t *webview_version() {

#endif /* WEBVIEW_HEADER */
#endif /* __cplusplus */
#endif /* WEBVIEW_H */
#endif /* WEBVIEW_H */

0 comments on commit 96f12cd

Please sign in to comment.