1
1
var strPluginName = "Symbol Instance Locator";
2
+ var uiButtons = [];
2
3
3
4
var onRun = function(context) {
4
5
var selection = context.selection;
@@ -24,21 +25,54 @@ var onRun = function(context) {
24
25
}
25
26
26
27
if (symbolInstances.length > 0) {
27
- var alertWindow = COSAlertWindow.new();
28
-
29
- alertWindow.setIcon(NSImage.alloc().initByReferencingFile(context.plugin.urlForResourceNamed("icon.png").path()));
30
- alertWindow.setMessageText(strPluginName);
31
-
32
- alertWindow.addTextLabelWithValue(symbolMaster.name() + " has " + symbolInstances.length + " instance(s).");
33
-
34
- alertWindow.addTextLabelWithValue("Select an instance to navigate to it's location.");
35
-
36
- var instanceItemHeight = 60,
37
- instanceItemWidth = 286,
28
+ var instancePanelWidth = 350,
29
+ instancePanelHeight = 492,
30
+ instancePanelTitle = 44,
31
+ instancePanelContentHeight = instancePanelHeight-instancePanelTitle;
32
+
33
+ var instancePanel = NSPanel.alloc().init();
34
+ instancePanel.setFrame_display(NSMakeRect(0,0,instancePanelWidth,instancePanelHeight),true);
35
+ instancePanel.setStyleMask(NSTexturedBackgroundWindowMask | NSTitledWindowMask | NSClosableWindowMask | NSFullSizeContentViewWindowMask);
36
+ instancePanel.setBackgroundColor(NSColor.controlColor());
37
+ instancePanel.setLevel(NSFloatingWindowLevel);
38
+ instancePanel.standardWindowButton(NSWindowMiniaturizeButton).setHidden(true);
39
+ instancePanel.standardWindowButton(NSWindowZoomButton).setHidden(true);
40
+ instancePanel.makeKeyAndOrderFront(null);
41
+ instancePanel.center();
42
+ instancePanel.title = strPluginName;
43
+
44
+ COScript.currentCOScript().setShouldKeepAround_(true);
45
+
46
+ var closeButton = instancePanel.standardWindowButton(NSWindowCloseButton);
47
+ closeButton.setCOSJSTargetFunction(function(sender) {
48
+ instancePanel.close();
49
+ threadDictionary.removeObjectForKey(identifier);
50
+ COScript.currentCOScript().setShouldKeepAround_(false);
51
+ });
52
+
53
+ var threadDictionary = NSThread.mainThread().threadDictionary(),
54
+ identifier = "com.sonburn.sketchplugins.symbol-instance-locator";
55
+
56
+ if (threadDictionary[identifier]) return;
57
+
58
+ threadDictionary[identifier] = instancePanel;
59
+
60
+ var instancePanelContent = NSView.alloc().initWithFrame(NSMakeRect(0,0,instancePanelWidth,instancePanelContentHeight));
61
+ instancePanelContent.setFlipped(1);
62
+
63
+ var matchText = createBoldDescription(symbolMaster.name() + " has " + symbolInstances.length + " instance(s).",12,NSMakeRect(8,12,instancePanelWidth-16,16));
64
+ instancePanelContent.addSubview(matchText);
65
+
66
+ var selectText = createDescription("Select an instance below to navigate to it's location...",12,NSMakeRect(8,34,instancePanelWidth-16,16));
67
+ instancePanelContent.addSubview(selectText);
68
+
69
+ var gutterWidth = 15,
70
+ instanceItemHeight = 96,
71
+ instanceItemWidth = instancePanelWidth - gutterWidth,
38
72
instanceFrameInnerHeight = instanceItemHeight * (symbolInstances.length),
39
- instanceFrameMaxHeight = 300 ,
73
+ instanceFrameMaxHeight = 384 ,
40
74
instanceFrameHeight = (instanceFrameInnerHeight < instanceFrameMaxHeight) ? instanceFrameInnerHeight : instanceFrameMaxHeight,
41
- instanceFrame = NSScrollView.alloc().initWithFrame(NSMakeRect(0,0,300 ,instanceFrameHeight)),
75
+ instanceFrame = NSScrollView.alloc().initWithFrame(NSMakeRect(0,64,instancePanelWidth ,instanceFrameHeight)),
42
76
instanceFrameSize = instanceFrame.contentSize(),
43
77
instanceFrameInner = NSView.alloc().initWithFrame(NSMakeRect(0,0,instanceFrameSize.width,instanceFrameInnerHeight)),
44
78
count = 0;
@@ -52,11 +86,8 @@ var onRun = function(context) {
52
86
count++;
53
87
}
54
88
55
- alertWindow.addAccessoryView(instanceFrame);
56
-
57
- alertWindow.addButtonWithTitle("Close");
58
-
59
- alertWindow.runModal();
89
+ instancePanelContent.addSubview(instanceFrame);
90
+ instancePanel.contentView().addSubview(instancePanelContent);
60
91
} else {
61
92
displayDialog(symbolMaster.name() + " has no instances.",strPluginName);
62
93
}
@@ -71,16 +102,19 @@ function displayDialog(message,title) {
71
102
72
103
function createListItem(instance,frame) {
73
104
var listItem = NSView.alloc().initWithFrame(frame),
74
- rightColWidth = 120,
75
- leftColWidth = frame.size.width-rightColWidth;
105
+ rightColWidth = 140,
106
+ leftColWidth = frame.size.width-rightColWidth,
107
+ leftPad = 8;
76
108
77
109
listItem.setFlipped(1);
78
- listItem.addSubview(createTextLabel("Page Name",NSMakeRect(4,2,leftColWidth,14)));
79
- listItem.addSubview(createTextField(instance.parentPage().name(),NSMakeRect(4,15,leftColWidth,18)));
80
- listItem.addSubview(createTextLabel("Instance Name",NSMakeRect(4,29,leftColWidth,14)));
81
- listItem.addSubview(createTextField(instance.name(),NSMakeRect(4,42,leftColWidth,18)));
110
+ listItem.addSubview(createTextLabel("Page",NSMakeRect(leftPad,6,leftColWidth,14)));
111
+ listItem.addSubview(createTextField(instance.parentPage().name(),NSMakeRect(leftPad,18,leftColWidth,18)));
112
+ listItem.addSubview(createTextLabel("Artboard",NSMakeRect(leftPad,34,leftColWidth,14)));
113
+ listItem.addSubview(createTextField((instance.parentArtboard()) ? instance.parentArtboard().name() : "None",NSMakeRect(leftPad,46,leftColWidth,18)));
114
+ listItem.addSubview(createTextLabel("Instance",NSMakeRect(leftPad,62,leftColWidth,14)));
115
+ listItem.addSubview(createTextField(instance.name(),NSMakeRect(leftPad,74,leftColWidth,18)));
82
116
listItem.addSubview(createImageArea(instance,NSMakeRect(leftColWidth,0,rightColWidth,frame.size.height)));
83
- listItem.addSubview(createDivider(NSMakeRect(0,59 ,frame.size.width,1)));
117
+ listItem.addSubview(createDivider(NSMakeRect(0,frame.size.height-1 ,frame.size.width,1)));
84
118
listItem.addSubview(createTargetArea(instance,NSMakeRect(0,0,frame.size.width,frame.size.height)));
85
119
86
120
return listItem;
@@ -90,7 +124,7 @@ function createTextLabel(string,frame) {
90
124
var textLabel = NSTextField.alloc().initWithFrame(frame);
91
125
92
126
textLabel.setStringValue(string);
93
- textLabel.setFont(NSFont.boldSystemFontOfSize(10 ));
127
+ textLabel.setFont(NSFont.systemFontOfSize(9 ));
94
128
textLabel.setTextColor(NSColor.colorWithCalibratedRed_green_blue_alpha(0/255,0/255,0/255,0.4));
95
129
textLabel.setBezeled(0);
96
130
textLabel.setEditable(0);
@@ -105,6 +139,7 @@ function createTextField(string,frame) {
105
139
textField.setFont(NSFont.systemFontOfSize(11));
106
140
textField.setBezeled(0);
107
141
textField.setEditable(0);
142
+ textField.setSelectable(1);
108
143
109
144
return textField;
110
145
}
@@ -124,7 +159,7 @@ function createImageArea(instance,frame) {
124
159
imageArea.setTitle("");
125
160
imageArea.setBordered(0);
126
161
imageArea.setWantsLayer(1);
127
- imageArea.layer().setBackgroundColor(CGColorCreateGenericRGB(241 /255,241 /255,241 /255,1.0));
162
+ imageArea.layer().setBackgroundColor(CGColorCreateGenericRGB(248 /255,248 /255,248 /255,1.0));
128
163
129
164
var exportRequest = MSExportRequest.exportRequestsFromExportableLayer_inRect_useIDForName_(
130
165
instance,
@@ -154,10 +189,19 @@ function createImageArea(instance,frame) {
154
189
function createTargetArea(instance,frame) {
155
190
var targetArea = NSButton.alloc().initWithFrame(frame);
156
191
192
+ uiButtons.push(targetArea);
193
+
194
+ targetArea.addCursorRect_cursor(targetArea.frame(),NSCursor.pointingHandCursor());
157
195
targetArea.setTransparent(1);
158
196
targetArea.setAction("callAction:");
159
197
targetArea.setCOSJSTargetFunction(function(sender) {
160
- NSApp.stopModalWithCode(NSOKButton);
198
+ for (var i = 0; i < uiButtons.length; i++) {
199
+ if (uiButtons[i].layer()) uiButtons[i].layer().setBorderWidth(0);
200
+ }
201
+
202
+ sender.setWantsLayer(1);
203
+ sender.layer().setBorderWidth(2);
204
+ sender.layer().setBorderColor(CGColorCreateGenericRGB(0,0,1,1));
161
205
162
206
var rect = (instance.parentArtboard()) ? instance.parentArtboard().rect() : instance.absoluteRect().rect();
163
207
@@ -169,3 +213,33 @@ function createTargetArea(instance,frame) {
169
213
170
214
return targetArea;
171
215
}
216
+
217
+ function createDescription(text,size,frame,alpha) {
218
+ var label = NSTextField.alloc().initWithFrame(frame),
219
+ alpha = (alpha) ? alpha : 1.0;
220
+
221
+ label.setStringValue(text);
222
+ label.setFont(NSFont.systemFontOfSize(size));
223
+ label.setTextColor(NSColor.colorWithCalibratedRed_green_blue_alpha(0/255,0/255,0/255,alpha));
224
+ label.setBezeled(false);
225
+ label.setDrawsBackground(false);
226
+ label.setEditable(false);
227
+ label.setSelectable(false);
228
+
229
+ return label;
230
+ }
231
+
232
+ function createBoldDescription(text,size,frame,alpha) {
233
+ var label = NSTextField.alloc().initWithFrame(frame),
234
+ alpha = (alpha) ? alpha : 1.0;
235
+
236
+ label.setStringValue(text);
237
+ label.setFont(NSFont.boldSystemFontOfSize(size));
238
+ label.setTextColor(NSColor.colorWithCalibratedRed_green_blue_alpha(0/255,0/255,0/255,alpha));
239
+ label.setBezeled(false);
240
+ label.setDrawsBackground(false);
241
+ label.setEditable(false);
242
+ label.setSelectable(false);
243
+
244
+ return label;
245
+ }
0 commit comments