Skip to content

Instantly share code, notes, and snippets.

@mnvr
Created August 19, 2023 00:28
Show Gist options
  • Save mnvr/00de3fa69e1bf3c79c3484ff6c506932 to your computer and use it in GitHub Desktop.
Save mnvr/00de3fa69e1bf3c79c3484ff6c506932 to your computer and use it in GitHub Desktop.
// Persist the scope window's position across restarts (by default,
// it always starts centered on the screen on each restart).
//
// Usage
// -----
//
// Open the Stethoscope in your startup file, e.g.
//
// s.scope;
//
// Invoke this file to your SuperCollider startup file, e.g. by
// adding the following line (assuming you save this in your user
// support directory, otherwise change the path accordingly):
//
// (Platform.userAppSupportDir +/+ "startup-save-stethoscope-position.scd").load;
//
// If so, this code will remember the position and size of the scope
// by writing it to a file when the server exits (and the scope is
// visible).
//
// On subsequent launches, it'll then set the server's scope window
// to its last remembered position and size.
(
var archivePath, window, bounds;
archivePath = Platform.userAppSupportDir +/+ "scope-bounds.txt";
// Remember the position of the scope window when the server exits
ServerQuit.add { |server|
var window, bounds;
// Evaluating s.scope causes the scopeWindow to be created if
// it doesn't already exist. We don't want to create a new
// scope at this point, only remember the position of the
// existing scope if there's one.
//
// So instead, we directly access the s.scopeWindow property
// (which is anyways the eventual return value of the
// s.scope function).
window = server.scopeWindow !? { server.scopeWindow.window };
if (window.notNil) {
bounds = window.bounds;
bounds.writeTextArchive(archivePath);
}
};
// And move it back to its last remembered position
window = s.scopeWindow !? { s.scopeWindow.window };
if (window.notNil) {
bounds = Object.readTextArchive(archivePath);
window.bounds = bounds;
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment