Skip to content

Commit 0c33fa1

Browse files
authored
Merge pull request #20 from thetrung/master
add support for macOS
2 parents 96054f7 + bb922d2 commit 0c33fa1

File tree

4 files changed

+32
-10
lines changed

4 files changed

+32
-10
lines changed

webview/ext.manifest

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
name: WebViewExternal
22

33
platforms:
4+
x86_64-osx:
5+
context:
6+
frameworks: ["WebKit"]
7+
48
arm64-ios:
59
context:
610
frameworks: ["WebKit"]

webview/src/webview_common.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#if defined(DM_PLATFORM_ANDROID) || defined(DM_PLATFORM_IOS)
1+
#if defined(DM_PLATFORM_ANDROID) || defined(DM_PLATFORM_IOS) || defined (DM_PLATFORM_OSX)
22

33
#include <assert.h>
44
#include <dmsdk/dlib/log.h>
@@ -104,7 +104,7 @@ static int Create(lua_State* L)
104104
info.m_Self = dmScript::Ref(L, LUA_REGISTRYINDEX);
105105
info.m_L = dmScript::GetMainThread(L);
106106

107-
int webview_id = Platform_Create(L, &info);
107+
int webview_id = Platform_Create(L, &info);
108108
lua_pushnumber(L, webview_id);
109109

110110
assert(top + 1 == lua_gettop(L));

webview/src/webview_ios.mm renamed to webview/src/webview_darwin.mm

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#if defined(DM_PLATFORM_IOS)
1+
#if defined(DM_PLATFORM_IOS) || defined(DM_PLATFORM_OSX)
22

33
#include <dmsdk/dlib/array.h>
44
#include <dmsdk/dlib/log.h>
@@ -7,9 +7,13 @@
77
#include <dmsdk/extension/extension.h>
88

99
#import <Foundation/Foundation.h>
10-
#import <UIKit/UIKit.h>
1110
#import <WebKit/WebKit.h>
1211

12+
#if defined(DM_PLATFORM_IOS)
13+
#import <UIKit/UIKit.h>
14+
#endif
15+
16+
1317
#include "webview_common.h"
1418

1519
enum CommandType
@@ -30,8 +34,11 @@
3034
void* m_Data;
3135
const char* m_Url;
3236
};
33-
37+
#if defined(DM_PLATFORM_IOS)
3438
@interface WebViewDelegate : UIViewController <WKNavigationDelegate>
39+
#elif defined(DM_PLATFORM_OSX)
40+
@interface WebViewDelegate : NSViewController <WKNavigationDelegate>
41+
#endif
3542
{
3643
@public int m_WebViewID;
3744
@public int m_RequestID;
@@ -142,7 +149,6 @@ static void QueueCommand(Command* cmd)
142149

143150
namespace dmWebView
144151
{
145-
146152
int Platform_Create(lua_State* L, dmWebView::WebViewInfo* _info)
147153
{
148154
// Find a free slot
@@ -163,12 +169,17 @@ int Platform_Create(lua_State* L, dmWebView::WebViewInfo* _info)
163169
}
164170

165171
g_WebView.m_Info[webview_id] = *_info;
166-
172+
#if defined(DM_PLATFORM_IOS)
167173
UIScreen* screen = [UIScreen mainScreen];
168174

169175
WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
170176
WKWebView *view = [[WKWebView alloc] initWithFrame:screen.bounds configuration:configuration];
177+
#elif defined(DM_PLATFORM_OSX)
178+
NSScreen* screen = [NSScreen mainScreen];
171179

180+
WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
181+
WKWebView *view = [[WKWebView alloc] initWithFrame:screen.visibleFrame configuration:configuration];
182+
#endif
172183
WebViewDelegate* navigationDelegate = [WebViewDelegate alloc];
173184
navigationDelegate->m_WebViewID = webview_id;
174185
navigationDelegate->m_RequestID = 0;
@@ -178,8 +189,11 @@ int Platform_Create(lua_State* L, dmWebView::WebViewInfo* _info)
178189

179190
g_WebView.m_WebViews[webview_id] = view;
180191
g_WebView.m_WebViewDelegates[webview_id] = navigationDelegate;
181-
192+
#if defined(DM_PLATFORM_IOS)
182193
UIView * topView = [[[[UIApplication sharedApplication] keyWindow] subviews] lastObject];
194+
#elif defined(DM_PLATFORM_OSX)
195+
NSView * topView = [[[NSApplication sharedApplication] keyWindow] contentView];
196+
#endif
183197
[topView addSubview:view];
184198
view.hidden = TRUE;
185199

@@ -281,7 +295,11 @@ int Platform_IsVisible(lua_State* L, int webview_id)
281295
int Platform_SetPosition(lua_State* L, int webview_id, int x, int y, int width, int height)
282296
{
283297
CHECK_WEBVIEW_AND_RETURN();
298+
#if defined(DM_PLATFORM_IOS)
284299
CGRect screenRect = [[UIScreen mainScreen] bounds];
300+
#elif defined(DM_PLATFORM_OSX)
301+
CGRect screenRect = [[NSScreen mainScreen] visibleFrame];
302+
#endif
285303
g_WebView.m_WebViews[webview_id].frame = CGRectMake(x, y, width >= 0 ? width : screenRect.size.width, height >= 0 ? height : screenRect.size.height);
286304
return 0;
287305
}
@@ -379,4 +397,4 @@ int Platform_SetPosition(lua_State* L, int webview_id, int x, int y, int width,
379397

380398
} // namespace dmWebView
381399

382-
#endif // DM_PLATFORM_IOS
400+
#endif // DM_PLATFORM_IOS || DM_PLATFORM_OSX

webview/src/webview_null.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#if !defined(DM_PLATFORM_ANDROID) && !defined(DM_PLATFORM_IOS)
1+
#if !defined(DM_PLATFORM_ANDROID) && !defined(DM_PLATFORM_IOS) && !defined(DM_PLATFORM_OSX)
22
extern "C" void WebViewExternal()
33
{
44

0 commit comments

Comments
 (0)