Repository navigation
Expand file tree
/
Copy pathview_controller.js
More file actions
182 lines (142 loc) · 4.56 KB
/
Copy pathview_controller.js
File metadata and controls
182 lines (142 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// @ts-check
import "@nativescript/macos-node-api";
export class ApplicationDelegate extends NSObject {
static ObjCProtocols = [NSApplicationDelegate, NSWindowDelegate];
static {
NativeClass(this);
}
/**
* @param {NSNotification} _notification
*/
applicationDidFinishLaunching(_notification) {
const menu = NSMenu.new();
NSApp.mainMenu = menu;
const appMenuItem = NSMenuItem.new();
menu.addItem(appMenuItem);
const appMenu = NSMenu.new();
appMenuItem.submenu = appMenu;
appMenu.addItemWithTitleActionKeyEquivalent("Quit", "terminate:", "q");
const controller = ViewController.new();
const window = NSWindow.windowWithContentViewController(controller);
window.title = "NativeScript for macOS";
window.delegate = this;
window.styleMask =
NSWindowStyleMask.Titled |
NSWindowStyleMask.Closable |
NSWindowStyleMask.Miniaturizable |
NSWindowStyleMask.Resizable |
NSWindowStyleMask.FullSizeContentView;
window.titlebarAppearsTransparent = true;
window.titleVisibility = NSWindowTitleVisibility.Hidden;
window.backgroundColor = NSColor.colorWithSRGBRedGreenBlueAlpha(
118 / 255,
171 / 255,
235 / 255,
1
);
window.makeKeyAndOrderFront(this);
NSApp.activateIgnoringOtherApps(false);
}
/**
* @param {NSNotification} _notification
*/
windowWillClose(_notification) {
NSApp.terminate(this);
}
}
export class ViewController extends NSViewController {
static ObjCExposedMethods = {
buttonClicked: {
params: [NSView],
returns: interop.types.void,
},
};
static {
NativeClass(this);
}
i = 0;
/**
* @type {NSButton | null}
*/
button = null;
loadView() {
this.view = NSView.new();
}
viewDidLoad() {
super.viewDidLoad();
this.view.frame = {
origin: { x: 0, y: 0 },
size: { width: 500, height: 500 },
};
const label = NSTextField.alloc().initWithFrame({
origin: { x: 0, y: 0 },
size: { width: 390, height: 100 },
});
label.stringValue = "Hello, macOS";
label.isBezeled = false;
label.drawsBackground = false;
label.isEditable = false;
label.isSelectable = false;
label.alignment = NSTextAlignment.Center;
label.translatesAutoresizingMaskIntoConstraints = false;
label.textColor = NSColor.colorWithSRGBRedGreenBlueAlpha(1, 1, 1, 1);
label.font = NSFontManager.sharedFontManager.convertFontToHaveTrait(
NSFont.fontWithNameSize(label.font.fontName, 45),
NSFontTraitMask.Bold
);
label.sizeToFit();
const vstack = NSStackView.alloc().initWithFrame({
origin: { x: 0, y: 0 },
size: { width: 500, height: 500 },
});
vstack.orientation = NSUserInterfaceLayoutOrientation.Vertical;
vstack.alignment = NSLayoutAttribute.CenterX;
vstack.distribution = NSStackViewDistribution.Fill;
vstack.spacing = 40;
vstack.translatesAutoresizingMaskIntoConstraints = false;
const image = NSImage.alloc().initWithContentsOfFile(
new URL("../assets/NativeScript.png", import.meta.url).pathname
);
image.size = { width: 128, height: 128 };
const imageView = NSImageView.imageViewWithImage(image);
const button = NSButton.buttonWithTitleTargetAction(
"Try me!",
this,
"buttonClicked"
);
button.controlSize = NSControlSize.Large;
button.bezelStyle = NSBezelStyle.Rounded;
button.setButtonType(NSButtonType.MomentaryLight);
button.translatesAutoresizingMaskIntoConstraints = false;
this.button = button;
vstack.addViewInGravity(imageView, NSStackViewGravity.Center);
vstack.addViewInGravity(label, NSStackViewGravity.Center);
vstack.addViewInGravity(button, NSStackViewGravity.Center);
this.view.addSubview(vstack);
vstack.centerXAnchor.constraintEqualToAnchor(
this.view.centerXAnchor
).isActive = true;
vstack.centerYAnchor.constraintEqualToAnchor(
this.view.centerYAnchor
).isActive = true;
}
/**
* @param {NSNotification} _sender
*/
buttonClicked(_sender) {
if (this.i == 0) {
const alert = NSAlert.new();
alert.icon = NSImage.imageWithSystemSymbolNameAccessibilityDescription(
"cursorarrow.click",
null
);
alert.messageText = "Clicked for the first time!";
alert.runModal();
}
if (this.button) this.button.title = `Clicked ${++this.i} times`;
}
}
const NSApp = NSApplication.sharedApplication;
NSApp.delegate = ApplicationDelegate.new();
NSApp.setActivationPolicy(NSApplicationActivationPolicy.Regular);
NSApplicationMain(0, null);