Skip to content

Instantly share code, notes, and snippets.

@jamshark70
Created August 25, 2021 09:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamshark70/e295d6f034f8f62b2e01800925a0aae3 to your computer and use it in GitHub Desktop.
Save jamshark70/e295d6f034f8f62b2e01800925a0aae3 to your computer and use it in GitHub Desktop.
SuperCollider startup file code for a hot-key accessible utility window
// 2015-2021 H. James Harkins
StartUp.add({
var e = Environment.make {
~cmds = (
$q: "Query all nodes" -> { Server.default.queryAllNodes },
$s: "Browse SynthDescLib" -> { SynthDescLib.global.browse },
$b: "Class browser" -> { Object.browse },
// commenting out b/c you don't have this file
// $f: "Audition soundfiles" -> { (Platform.userAppSupportDir +/+ "scd/sample-audition.scd").loadPath },
$d: "Dismiss" -> { ~win.visible = false },
$k: "Bookmarks" -> { ~openBookmark.() }
);
~makeWin = {
~win = Window("util", Rect(800, 679, ~cmds.size * 30 + 10, 70));
~win.layout = HLayout(*
~cmds.keys.asArray.sort.collect { |key|
Button().fixedWidth_(20)
.states_([[key]])
.action_(inEnvir { ~win.close; ~cmds[key].value.value; })
.toolTip_(~cmds[key].key)
.keyDownAction_(false);
}
);
~win.view.keyDownAction = inEnvir { |view, char|
if(~cmds[char].notNil) {
~win.close; //.visible = false;
~cmds[char].value.value;
};
};
~win.front;
~bkShutdownFunc = nil;
Library.put(\ddwBookmarksDirty, false);
~openBookmark = {
var file, line, book = Library.at(\ddwBookmarks),
win, list, openBtn, addBtn, removeBtn, cancelBtn, lastClick = 0;
if(book.isNil) {
file = File(Platform.userConfigDir +/+ "ddwBookmarks.txt", "r");
if(file.isOpen) {
while {
line = file.getLine;
line.notNil
} {
book = book.add(line);
};
} {
"Could not read bookmarks file".warn;
book = Array.new;
};
};
if(~bkShutdownFunc.isNil) {
~bkShutdownFunc = {
var file;
if(Library.at(\ddwBookmarksDirty) ?? { false }) {
file = File(Platform.userConfigDir +/+ "ddwBookmarks.txt", "w");
if(file.isOpen) {
protect {
Library.at(\ddwBookmarks).do { |path|
file << path << "\n";
};
} { file.close };
} {
"Could not write bookmarks file".warn;
};
};
};
ShutDown.add(~bkShutdownFunc);
};
win = Window("Bookmarks",
Rect.aboutPoint(Window.screenBounds.center, 200, 150));
win.layout = HLayout(
list = ListView(),
VLayout(
nil,
openBtn = Button().fixedWidth_(80),
addBtn = Button().fixedWidth_(80),
removeBtn = Button().fixedWidth_(80),
cancelBtn = Button().fixedWidth_(80),
nil
)
);
list.items_(book.collect(_.basename)).value_(0)
.enterKeyAction_({ |view|
openBtn.doAction
})
.mouseDownAction_({ |view|
if(AppClock.seconds - lastClick < 0.3) {
openBtn.doAction
} {
lastClick = AppClock.seconds;
};
});
openBtn.states_([["Open"]])
.action_({
book[list.value].openDocument;
win.close;
});
addBtn.states_([["Add"]])
.action_({
var new = 'Document'.asClass.current.path, base, i;
if(new.isNil) {
"Document is unsaved; can't bookmark".warn;
} {
if(book.size == 0) {
book = [new];
i = 0;
} {
base = new.basename;
i = book.detectIndex { |path| base < path.basename };
if(i.notNil) {
book = book.insert(i, new);
} {
book = book.add(new);
i = book.size - 1;
};
};
};
Library.put(\ddwBookmarks, book);
Library.put(\ddwBookmarksDirty, true);
list.items_(book.collect(_.basename)).value_(i);
});
removeBtn.states_([["Remove"]])
.action_({
var saveItem;
book.removeAt(list.value);
saveItem = book[list.value];
Library.put(\ddwBookmarks, book);
Library.put(\ddwBookmarksDirty, true);
list.items_(book.collect(_.basename))
.value_(book.indexOfEqual(saveItem) ?? { book.size - 1 })
});
cancelBtn.states_([["Exit"]])
.action_({ win.close });
win.front;
};
};
};
{
'Document'.asClass.globalKeyDownAction = 'Document'.asClass.globalKeyDownAction.addFunc({ |doc, char, modifiers, ascii|
if(ascii == 27 and: { modifiers.bitAnd(262144) != 0 }) {
~makeWin.value; // ~win.front;
};
}.inEnvir(e));
}.defer(3.0);
Library.put(\utilityEnvir, e);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment