Skip to content

Commit f8c4a21

Browse files
author
Jason Burns
committed
Update to make the result window a floating panel, highlight for
current selection, and artboard name.
1 parent a17c2a6 commit f8c4a21

File tree

4 files changed

+108
-33
lines changed

4 files changed

+108
-33
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ To find your plugins directory...
3939

4040
# Changelog
4141

42+
* **1.2** - Update to make the result window a floating panel, highlight for current selection, and artboard name.
4243
* **1.1** - Code optimizations and dialog frame will now be smaller if a small set of results are returned.
4344
* **1.0** - Found instances now display image of the instance, page where the instance reside, and instance name.
4445
* **0.3** - Converted list of instances to NSButtons, which when selected, will now navigate user to location of instance.

Symbol Instance Locator.sketchplugin/Contents/Sketch/manifest.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"commands" : [
44
{
55
"name" : "Symbol Instance Locator",
6-
"shortcut": "cmd option shift l",
6+
"shortcut" : "cmd option shift l",
77
"identifier" : "instanceLocator",
88
"description" : "Locate instances of the selected symbol or instance.",
99
"script" : "script.cocoascript",
@@ -18,7 +18,7 @@
1818
"isRoot" : true
1919
},
2020
"identifier" : "com.sonburn.sketchplugins.symbol-instance-locator",
21-
"version" : "1.1",
21+
"version" : "1.2",
2222
"description" : "Locate all instances of a selected symbol or instance.",
2323
"authorEmail" : "[email protected]",
2424
"name" : "Symbol Instance Locator",

Symbol Instance Locator.sketchplugin/Contents/Sketch/script.cocoascript

Lines changed: 102 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var strPluginName = "Symbol Instance Locator";
2+
var uiButtons = [];
23

34
var onRun = function(context) {
45
var selection = context.selection;
@@ -24,21 +25,54 @@ var onRun = function(context) {
2425
}
2526

2627
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,
3872
instanceFrameInnerHeight = instanceItemHeight * (symbolInstances.length),
39-
instanceFrameMaxHeight = 300,
73+
instanceFrameMaxHeight = 384,
4074
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)),
4276
instanceFrameSize = instanceFrame.contentSize(),
4377
instanceFrameInner = NSView.alloc().initWithFrame(NSMakeRect(0,0,instanceFrameSize.width,instanceFrameInnerHeight)),
4478
count = 0;
@@ -52,11 +86,8 @@ var onRun = function(context) {
5286
count++;
5387
}
5488

55-
alertWindow.addAccessoryView(instanceFrame);
56-
57-
alertWindow.addButtonWithTitle("Close");
58-
59-
alertWindow.runModal();
89+
instancePanelContent.addSubview(instanceFrame);
90+
instancePanel.contentView().addSubview(instancePanelContent);
6091
} else {
6192
displayDialog(symbolMaster.name() + " has no instances.",strPluginName);
6293
}
@@ -71,16 +102,19 @@ function displayDialog(message,title) {
71102

72103
function createListItem(instance,frame) {
73104
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;
76108

77109
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)));
82116
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)));
84118
listItem.addSubview(createTargetArea(instance,NSMakeRect(0,0,frame.size.width,frame.size.height)));
85119

86120
return listItem;
@@ -90,7 +124,7 @@ function createTextLabel(string,frame) {
90124
var textLabel = NSTextField.alloc().initWithFrame(frame);
91125

92126
textLabel.setStringValue(string);
93-
textLabel.setFont(NSFont.boldSystemFontOfSize(10));
127+
textLabel.setFont(NSFont.systemFontOfSize(9));
94128
textLabel.setTextColor(NSColor.colorWithCalibratedRed_green_blue_alpha(0/255,0/255,0/255,0.4));
95129
textLabel.setBezeled(0);
96130
textLabel.setEditable(0);
@@ -105,6 +139,7 @@ function createTextField(string,frame) {
105139
textField.setFont(NSFont.systemFontOfSize(11));
106140
textField.setBezeled(0);
107141
textField.setEditable(0);
142+
textField.setSelectable(1);
108143

109144
return textField;
110145
}
@@ -124,7 +159,7 @@ function createImageArea(instance,frame) {
124159
imageArea.setTitle("");
125160
imageArea.setBordered(0);
126161
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));
128163

129164
var exportRequest = MSExportRequest.exportRequestsFromExportableLayer_inRect_useIDForName_(
130165
instance,
@@ -154,10 +189,19 @@ function createImageArea(instance,frame) {
154189
function createTargetArea(instance,frame) {
155190
var targetArea = NSButton.alloc().initWithFrame(frame);
156191

192+
uiButtons.push(targetArea);
193+
194+
targetArea.addCursorRect_cursor(targetArea.frame(),NSCursor.pointingHandCursor());
157195
targetArea.setTransparent(1);
158196
targetArea.setAction("callAction:");
159197
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));
161205

162206
var rect = (instance.parentArtboard()) ? instance.parentArtboard().rect() : instance.absoluteRect().rect();
163207

@@ -169,3 +213,33 @@ function createTargetArea(instance,frame) {
169213

170214
return targetArea;
171215
}
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+
}

appcast.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
<description>Locate all instances of a selected symbol or instance.</description>
77
<language>en</language>
88
<item>
9-
<title>Version 1.1</title>
9+
<title>Version 1.2</title>
1010
<description>
1111
<![CDATA[
1212
<ul>
13-
<li>Code optimizations and dialog frame will now be smaller if a small set of results are returned.</li>
13+
<li>Update to make the result window a floating panel, highlight for current selection, and artboard name.</li>
1414
</ul>
1515
]]>
1616
</description>
17-
<enclosure url="https://github.com/sonburn/symbol-instance-locator/archive/master.zip" sparkle:version="1.1" />
17+
<enclosure url="https://github.com/sonburn/symbol-instance-locator/archive/master.zip" sparkle:version="1.2" />
1818
</item>
1919
</channel>
2020
</rss>

0 commit comments

Comments
 (0)