Skip to content

Commit 64bb8b2

Browse files
committed
chore: add webview example
1 parent 94dff80 commit 64bb8b2

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

examples/webview.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// @ts-check
2+
3+
import "@nativescript/macos-node-api";
4+
5+
objc.import("WebKit");
6+
7+
export class ApplicationDelegate extends NSObject {
8+
static ObjCProtocols = [NSApplicationDelegate, NSWindowDelegate];
9+
10+
static {
11+
NativeClass(this);
12+
}
13+
14+
/**
15+
* @param {NSNotification} _notification
16+
*/
17+
applicationDidFinishLaunching(_notification) {
18+
const menu = NSMenu.new();
19+
NSApp.mainMenu = menu;
20+
21+
const appMenuItem = NSMenuItem.new();
22+
menu.addItem(appMenuItem);
23+
24+
const appMenu = NSMenu.new();
25+
appMenuItem.submenu = appMenu;
26+
27+
appMenu.addItemWithTitleActionKeyEquivalent("Quit", "terminate:", "q");
28+
29+
const controller = ViewController.new();
30+
const window = NSWindow.windowWithContentViewController(controller);
31+
32+
window.title = "NativeScript for macOS";
33+
window.delegate = this;
34+
window.styleMask = NSWindowStyleMask.Titled |
35+
NSWindowStyleMask.Closable |
36+
NSWindowStyleMask.Miniaturizable |
37+
NSWindowStyleMask.Resizable;
38+
39+
window.makeKeyAndOrderFront(this);
40+
41+
NSApp.activateIgnoringOtherApps(false);
42+
}
43+
44+
/**
45+
* @param {NSNotification} _notification
46+
*/
47+
windowWillClose(_notification) {
48+
NSApp.terminate(this);
49+
}
50+
}
51+
52+
/**
53+
* @implements {WKUIDelegate}
54+
*/
55+
export class ViewController extends NSViewController {
56+
static ObjCProtocols = [WKUIDelegate];
57+
58+
static {
59+
NativeClass(this);
60+
}
61+
62+
/**
63+
* @type {WKWebView | undefined}
64+
*/
65+
webview;
66+
67+
/**
68+
* @override
69+
*/
70+
loadView() {
71+
const config = WKWebViewConfiguration.new();
72+
const view = WKWebView.alloc().initWithFrameConfiguration(
73+
CGRectZero,
74+
config,
75+
);
76+
view.UIDelegate = this;
77+
this.view = view;
78+
this.webview = view;
79+
}
80+
81+
/**
82+
* @override
83+
*/
84+
viewDidLoad() {
85+
super.viewDidLoad();
86+
87+
this.view.frame = {
88+
origin: { x: 0, y: 0 },
89+
size: { width: 800, height: 600 },
90+
};
91+
92+
const url = NSURL.URLWithString("https://nativescript.org");
93+
const request = NSURLRequest.requestWithURL(url);
94+
if (this.webview) this.webview.loadRequest(request);
95+
}
96+
}
97+
98+
const NSApp = NSApplication.sharedApplication;
99+
100+
NSApp.delegate = ApplicationDelegate.new();
101+
102+
NSApp.setActivationPolicy(NSApplicationActivationPolicy.Regular);
103+
104+
NSApplicationMain(0, null);

0 commit comments

Comments
 (0)