-
Notifications
You must be signed in to change notification settings - Fork 41
/
Button_mac.js
183 lines (169 loc) · 6.12 KB
/
Button_mac.js
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
183
module.exports = (function() {
if(global.__TINT.Button) {
return global.__TINT.Button;
}
var util = require('Utilities');
var Container = require('Container');
var $ = process.bridge.objc;
/**
* @class Button
* @description Creates a button for the user to active or indicate status or steps.
* @extends Container
*/
/**
* @new
* @memberof Button
* @description Creates a new Button control.
*/
function Button(properties, options, inherited) {
options = options || {};
options.mouseDownBlocks = true;
this.nativeClass = this.nativeClass || $.NSButton;
this.nativeViewClass = this.nativeViewClass || $.NSButton;
Container.call(this, properties, options, inherited);
this.private.img = null;
this.private.buttonStyle = this.private.buttonType = "normal";
this.native('setButtonType',$.NSMomentaryLightButton);
this.native('setBezelStyle',$.NSTexturedRoundedBezelStyle);
this.native('cell')('setWraps',$.NO);
this.native('setTitle', $(""));
util.setProperties(this, properties, inherited);
}
Button.prototype = Object.create(Container.prototype);
Button.prototype.constructor = Button;
/**
* @member border
* @type {boolean}
* @memberof Button
* @description Gets or sets whether the button has a border around it.
* @default true
*/
util.makePropertyBoolType(Button.prototype, 'border', 'isBordered', 'setBordered');
/**
* @member state
* @type {boolean}
* @memberof Button
* @description Gets or sets whether the button is "on" or "off". This has different
* meanings depending on the type of button, for radio, checkbox and
* toggle type buttons this changes its visual style. This has no meaning
* (visually) for normal buttons. The default is false.
* @default false
*/
util.def(Button.prototype, 'state',
function() { return this.nativeView('state') === $.NSOnState ? true : false; },
function(e) { return this.nativeView('setState', e === true ? $.NSOnState : $.NSOffState); }
);
/**
* @member title
* @type {string}
* @memberof Button
* @description Gets or sets the text label on the button.
*/
util.def(Button.prototype, 'title',
function() { return this.nativeView('title').toString(); },
function(e) {
// Private event, do not rely on it.
this.fireEvent('property-change', ['title', e]);
if(e.toString() === "") {
this.nativeView('cell')('setImagePosition', $.NSImageOnly);
} else {
this.nativeView('cell')('setImagePosition', $.NSImageLeft);
}
this.nativeView('setTitle', $(e));
}
);
/**
* @member type
* @type {string}
* @memberof Button
* @description Gets or sets the type of button, this can be
* "toggle", "checkbox", "radio" or "normal".
*/
util.def(Button.prototype, 'type',
function() { return this.private.buttonType; },
function(type) {
this.private.buttonType = type;
if (type === "toggle") {
this.nativeView('setButtonType',$.NSPushOnPushOffButton);
} else if (type === "checkbox") {
this.nativeView('setButtonType', $.NSSwitchButton);
} else if (type === "radio") {
this.nativeView('setButtonType', $.NSRadioButton);
} else {
this.nativeView('setButtonType',$.NSMomentaryLightButton);
}
// no complement on other systems.
//else if (type == "none") this.nativeView('setButtonType', $.NSMomentaryPushInButton);
}
);
// TODO: Not supported on Win, perhaps investigate differences in behavior and replicate?
util.def(Button.prototype, 'showBorderOnHover',
function() { return this.nativeView('showsBorderOnlyWhileMouseInside') ? true : false; },
function(e) { this.nativeView('setShowsBorderOnlyWhileMouseInside', e ? true : false ); }
);
/**
* @member enabled
* @type {string}
* @memberof Button
* @description Gets or sets the buttons availability and visual presentation to the user.
* When set to false the button is grayed out and does not respond to clicks.
* The default is true.
* @default true
*/
util.def(Button.prototype, 'enabled',
function() { return this.nativeView('isEnabled'); },
function(e) {
// Private event, do not rely on it.
this.fireEvent('property-change', ['enabled', e]);
return this.nativeView('setEnabled',e);
}
);
/**
* @member default
* @type {boolean}
* @memberof Button
* @description Set whether this button is the default button for the window, e.g., what is executed if the
* user presses enter on the window with no focus on a child element..
*/
util.def(Button.prototype, 'default',
function() { return this.private.defaultbutton; },
function(e) {
this.private.defaultbutton = e ? true : false;
if(this.private.defaultbutton) {
this.nativeView('setKeyEquivalent',$('\r'));
this.nativeView('setNeedsDisplay', $.YES);
this.nativeView('setBezelStyle', $.NSRoundedBezelStyle);
} else {
this.nativeView('setKeyEquivalent',$(''));
this.nativeView('setNeedsDisplay', $.YES);
}
}
);
/**
* @member image
* @type {string}
* @memberof Button
* @description Gets or sets the associated image to be displayed on the button. This can be
* a named icon resource for the OS, or a URL to an image (including the app:// schema).
*/
util.def(Button.prototype, 'image',
function() { return this.private.img; },
function(e) {
// Private event, do not rely on it.
this.fireEvent('property-change', ['image', e]);
this.private.img = e;
e = util.makeNSImage(e);
if(e) {
this.nativeView('setImage', e);
if(this.nativeView('title').toString() === "") {
this.nativeView('cell')('setImagePosition', $.NSImageOnly);
} else {
this.nativeView('cell')('setImagePosition', $.NSImageLeft);
}
this.nativeView('cell')('setImageScaling',$.NSImageScaleProportionallyDown);
}
}
);
global.__TINT.Button = Button;
return Button;
})();