Last active
May 28, 2019 01:40
-
-
Save KevinGutowski/24c0f09c90d4c1bd862c20c163e56e79 to your computer and use it in GitHub Desktop.
TableView start
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// MochaJSDelegate.js | |
// MochaJSDelegate | |
// | |
// Created by Matt Curtis | |
// Copyright (c) 2015. All rights reserved. | |
// | |
function MochaJSDelegate(selectorHandlerDict){ | |
var uniqueClassName = "MochaJSDelegate_DynamicClass_" + NSUUID.UUID().UUIDString(); | |
var delegateClassDesc = MOClassDescription.allocateDescriptionForClassWithName_superclass_(uniqueClassName, NSObject); | |
delegateClassDesc.registerClass(); | |
// Handler storage | |
var handlers = {}; | |
// Define interface | |
this.setHandlerForSelector = function(selectorString, func){ | |
var handlerHasBeenSet = (selectorString in handlers); | |
var selector = NSSelectorFromString(selectorString); | |
handlers[selectorString] = func; | |
if(!handlerHasBeenSet){ | |
/* | |
For some reason, Mocha acts weird about arguments: | |
https://github.com/logancollins/Mocha/issues/28 | |
We have to basically create a dynamic handler with a likewise dynamic number of predefined arguments. | |
*/ | |
var dynamicHandler = function(){ | |
var functionToCall = handlers[selectorString]; | |
if(!functionToCall) return; | |
return functionToCall.apply(delegateClassDesc, arguments); | |
}; | |
var args = [], regex = /:/g; | |
while(regex.exec(selectorString)) args.push("arg"+args.length); | |
var dynamicFunction = eval("(function("+args.join(",")+"){ return dynamicHandler.apply(this, arguments); })"); | |
delegateClassDesc.addInstanceMethodWithSelector_function_(selector, dynamicFunction); | |
} | |
}; | |
this.removeHandlerForSelector = function(selectorString){ | |
delete handlers[selectorString]; | |
}; | |
this.getHandlerForSelector = function(selectorString){ | |
return handlers[selectorString]; | |
}; | |
this.getAllHandlers = function(){ | |
return handlers; | |
}; | |
this.getClass = function(){ | |
return NSClassFromString(uniqueClassName); | |
}; | |
this.getClassInstance = function(){ | |
return NSClassFromString(uniqueClassName).new(); | |
}; | |
// Conveience | |
if(typeof selectorHandlerDict == "object"){ | |
for(var selectorString in selectorHandlerDict){ | |
this.setHandlerForSelector(selectorString, selectorHandlerDict[selectorString]); | |
} | |
} | |
}; | |
export default MochaJSDelegate; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sketch from 'sketch' | |
import MochaJSDelegate from './MochaJSDelegate' | |
let threadIdentifier = "com.tableviewdemo" | |
export default function(context) { | |
context.document.showMessage("It's alive 🙌") | |
COScript.currentCOScript().setShouldKeepAround_(true) | |
runPanel() | |
} | |
function runPanel() { | |
let threadDictionary = NSThread.mainThread().threadDictionary() | |
// If there is already a panel, prevent the plugin from running again | |
if (threadDictionary[threadIdentifier]) { | |
closePanel(threadDictionary[threadIdentifier], threadDictionary, threadIdentifier) | |
} else { | |
threadDictionary.panelOpen = true | |
setupPanel(threadDictionary, threadIdentifier) | |
} | |
} | |
function setupPanel(threadDictionary, threadIdentifier) { | |
var panelWidth = 312 | |
var panelHeight = 210 | |
let panel = NSPanel.alloc().init() | |
panel.setFrame_display(NSMakeRect(0, 0, panelWidth, panelHeight), true) | |
panel.setStyleMask(NSTexturedBackgroundWindowMask | NSTitledWindowMask | NSClosableWindowMask) | |
panel.title = "Tableview Demo 2" | |
panel.center() | |
panel.makeKeyAndOrderFront(null) | |
panel.setLevel(NSFloatingWindowLevel) | |
panel.standardWindowButton(NSWindowMiniaturizeButton).setHidden(true) | |
panel.standardWindowButton(NSWindowZoomButton).setHidden(true) | |
setupTableView(panel) | |
threadDictionary[threadIdentifier] = panel | |
} | |
function setupTableView(panel) { | |
let scrollView = NSScrollView.alloc().initWithFrame(panel.contentView().bounds()) | |
scrollView.setBorderType(NSBezelBorder) | |
let tableView = NSTableView.alloc().initWithFrame(panel.contentView().bounds()) | |
let tCol | |
const numberOfColumns = 1 | |
for (let i=0; i<numberOfColumns; i++) { | |
let columnKey = 'key' + (i+1) | |
tCol = NSTableColumn.alloc().initWithIdentifier(columnKey) | |
tCol.setWidth(100.0) | |
let columnHeadTitle = 'Column' + (i+1) | |
tCol.headerCell().setStringValue(columnHeadTitle) | |
tableView.addTableColumn(tCol) | |
} | |
tableView.setUsesAlternatingRowBackgroundColors(true) | |
tableView.setGridStyleMask(NSTableViewSolidVerticalGridLineMask) | |
tableView.setGridColor(NSColor.redColor()) | |
tableView.setRowHeight(23.0) | |
tableView.setSelectionHighlightStyle(NSTableViewSelectionHighlightStyleRegular) | |
tableView.setAutoresizesSubviews(true) | |
scrollView.setHasVerticalScroller(true) | |
scrollView.setHasHorizontalScroller(true) | |
scrollView.setAutoresizesSubviews(true) | |
scrollView.setAutoresizingMask(NSViewWidthSizable|NSViewHeightSizable) | |
scrollView.setDocumentView(tableView) | |
tableView.setDataSource(getDataSource()) | |
panel.contentView().addSubview(scrollView) | |
} | |
function getDataSource() { | |
console.log("Attempting to get dataSource") | |
let dataSource = new MochaJSDelegate({ | |
"numberOfRowsInTableView:": (function(tableView){ | |
console.log('numberOfRowsInTableView') | |
return 3 | |
}) | |
}) | |
console.log(dataSource) | |
dataSource.setHandlerForSelector( | |
"tableView:objectValueForTableColumn:row:", function(tableView,column,row) { | |
console.log('setValue') | |
return "test" | |
} | |
) | |
return dataSource.getClassInstance() | |
} | |
function closePanel(panel, threadDictionary, threadIdentifier) { | |
panel.close() | |
//Remove the reference to the panel | |
threadDictionary.removeObjectForKey(threadIdentifier) | |
threadDictionary.panelOpen = false | |
//Stop this script | |
COScript.currentCOScript().setShouldKeepAround_(false) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment