Skip to content

Commit

Permalink
Add sb-atom-sonic-pi-view, add test_toggle commands, start to prepare…
Browse files Browse the repository at this point in the history
… for possible Sonic Pi tutorial integration, and tweak other things

Tweak other things. E.g. add more brackets and semi colons to make it look more organised.
  • Loading branch information
SunderB committed Feb 28, 2018
1 parent a70c5b7 commit 42fc3b9
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 33 deletions.
31 changes: 31 additions & 0 deletions lib/sb-atom-sonic-pi-view.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use babel';

export default class sbAtomSonicPiView {

constructor(serializedState) {
console.log('creating element');
// Create root element
this.element = document.createElement('div');
this.element.classList.add('sb-atom-sonic-pi');

// Create message element
const message = document.createElement('div');
message.textContent = 'The sb-atom-sonic-pi package is Alive! It\'s ALIVE!';
message.classList.add('message');
this.element.appendChild(message);
}

// Returns an object that can be retrieved when package is activated
serialize() {}

// Tear down any state and detach
destroy() {
console.log('removing element');
this.element.remove();
}

getElement() {
return this.element;
}

}
80 changes: 55 additions & 25 deletions lib/sb-atom-sonic-pi.coffee
Original file line number Diff line number Diff line change
@@ -1,48 +1,78 @@
{CompositeDisposable} = require 'atom'
osc = require 'node-osc'
provider = require './sb-atom-sonic-pi-autocomplete'
'use babel';

{CompositeDisposable} = require 'atom';
osc = require 'node-osc';
provider = require './sb-atom-sonic-pi-autocomplete';
sbAtomSonicPiView = require './sb-atom-sonic-pi-view';

module.exports = sbAtomSonicPi =
config:
sendMessagesToGUI:
title: 'Send Messages To GUI'
type: 'boolean'
title: 'Send Messages To GUI',
type: 'boolean',
default: false
subscriptions: null
provide: -> provider
sbAtomSonicPiView: null,
subscriptions: null,
modalPanel: null,
provide: -> provider;

activate: (state) ->
@subscriptions = new CompositeDisposable
@sbAtomSonicPiView = new sbAtomSonicPiView(state.sbAtomSonicPiViewState);
@modalPanel = atom.workspace.addModalPanel(
item: this.sbAtomSonicPiView.getElement(),
visible: false
);

@subscriptions = new CompositeDisposable;
@subscriptions.add(atom.commands.add 'atom-workspace',
'sb-atom-sonic-pi:play-file': => @play('getText'),
'sb-atom-sonic-pi:save-and-play-file':=> @saveAndPlay(),
'sb-atom-sonic-pi:play-selection': => @play('getSelectedText'),
'sb-atom-sonic-pi:stop': => @stop())
'sb-atom-sonic-pi:stop': => @stop(),
'sb-atom-sonic-pi:toggle-tutorial': => @toggleTutorial(),
'sb-atom-sonic-pi:test_toggle': => @test_toggle()
);

deactivate: ->
@subscriptions.dispose()
@modalPanel.destroy();
@subscriptions.dispose();
@sbAtomSonicPiView.destroy();

serialize: ->
return sbAtomSonicPiViewState: this.sbAtomSonicPiView.serialize();

play: (selector) ->
editor = atom.workspace.getActiveTextEditor()
source = editor[selector]()
@send '/run-code', 'atom', source
atom.notifications.addSuccess "Sent source code to Sonic Pi. :)"
editor = atom.workspace.getActiveTextEditor();
source = editor[selector]();
@send('/run-code', 'atom', source);
atom.notifications.addSuccess("Sent source code to Sonic Pi. :)");

saveAndPlay: ->
editor = atom.workspace.getActiveTextEditor()
editor = atom.workspace.getActiveTextEditor();
editor.save();
fullPath = editor.getPath()
title = editor.getTitle()
@send '/run-code', 'atom', "run_file \"" + fullPath + "\""
atom.notifications.addSuccess "Saved file and told Sonic Pi to start playing. :)"
fullPath = editor.getPath().replace(/\\/g,"/");
title = editor.getTitle();
@send('/run-code', 'atom', "run_file \"" + fullPath + "\"");
atom.notifications.addSuccess("Saved file and told Sonic Pi to start playing. :)");

stop: ->
@send '/stop-all-jobs'
atom.notifications.addInfo "Told Sonic Pi to stop playing."
@send('/stop-all-jobs');
atom.notifications.addInfo("Told Sonic Pi to stop playing.");

send: (args...) ->
if atom.config.get('sb-atom-sonic-pi.sendMessagesToGUI') == true
sp_gui = new osc.Client('localhost', 4559)
sp_gui.send args..., -> sp_gui.kill()
sp_server = new osc.Client('127.0.0.1', 4557)
sp_server.send args..., -> sp_server.kill()
sp_gui = new osc.Client('localhost', 4559);
sp_gui.send args..., -> sp_gui.kill();
sp_server = new osc.Client('127.0.0.1', 4557);
sp_server.send args..., -> sp_server.kill();

test_toggle: ->
console.log('sb-atom-sonic-pi was toggled!');
return (
if this.modalPanel.isVisible()
console.log('sb-atom-sonic-pi: hiding panel');
this.modalPanel.hide()
else
console.log('sb-atom-sonic-pi: showing panel');
this.modalPanel.show()
)
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
}
}
},
"consumedServices": {
"tree-view": {
"versions": {
"^1.0.0": "consumeTreeView"
}
}
},
"repository": "https://github.com/sunderb/sb-atom-sonic-pi",
"license": "MIT",
"engines": {
Expand Down
16 changes: 10 additions & 6 deletions spec/sb-atom-sonic-pi-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -12,51 +12,55 @@ describe "sbAtomSonicPi", ->
workspaceElement = atom.views.getView(atom.workspace)
activationPromise = atom.packages.activatePackage('sb-atom-sonic-pi')

describe "when the sb-atom-sonic-pi:toggle event is triggered", ->
describe "when the sb-atom-sonic-pi:test_toggle event is triggered", ->
it "hides and shows the modal panel", ->
# Before the activation event the view is not on the DOM, and no panel
# has been created
console.log('sb-atom-sonic-pi: test 1');
expect(workspaceElement.querySelector('.sb-atom-sonic-pi')).not.toExist()

# This is an activation event, triggering it will cause the package to be
# activated.
atom.commands.dispatch workspaceElement, 'sb-atom-sonic-pi:toggle'
atom.commands.dispatch workspaceElement, 'sb-atom-sonic-pi:test_toggle'

waitsForPromise ->
activationPromise

runs ->
console.log('sb-atom-sonic-pi: test 2');
expect(workspaceElement.querySelector('.sb-atom-sonic-pi')).toExist()

sbAtomSonicPiElement = workspaceElement.querySelector('.sb-atom-sonic-pi')
expect(sbAtomSonicPiElement).toExist()

sbAtomSonicPiPanel = atom.workspace.panelForItem(sbAtomSonicPiElement)
expect(sbAtomSonicPiPanel.isVisible()).toBe true
atom.commands.dispatch workspaceElement, 'sb-atom-sonic-pi:toggle'
atom.commands.dispatch workspaceElement, 'sb-atom-sonic-pi:test_toggle'
expect(sbAtomSonicPiPanel.isVisible()).toBe false

it "hides and shows the view", ->
console.log('sb-atom-sonic-pi: next set of tests');
# This test shows you an integration test testing at the view level.

# Attaching the workspaceElement to the DOM is required to allow the
# `toBeVisible()` matchers to work. Anything testing visibility or focus
# requires that the workspaceElement is on the DOM. Tests that attach the
# workspaceElement to the DOM are generally slower than those off DOM.
jasmine.attachToDOM(workspaceElement)

console.log('sb-atom-sonic-pi: test 3');
expect(workspaceElement.querySelector('.sb-atom-sonic-pi')).not.toExist()

# This is an activation event, triggering it causes the package to be
# activated.
atom.commands.dispatch workspaceElement, 'sb-atom-sonic-pi:toggle'
atom.commands.dispatch workspaceElement, 'sb-atom-sonic-pi:test_toggle'

waitsForPromise ->
activationPromise

runs ->
console.log('sb-atom-sonic-pi: test 4');
# Now we can test for view visibility
sbAtomSonicPiElement = workspaceElement.querySelector('.sb-atom-sonic-pi')
expect(sbAtomSonicPiElement).toBeVisible()
atom.commands.dispatch workspaceElement, 'sb-atom-sonic-pi:toggle'
atom.commands.dispatch workspaceElement, 'sb-atom-sonic-pi:test_toggle'
expect(sbAtomSonicPiElement).not.toBeVisible()
4 changes: 2 additions & 2 deletions spec/sb-atom-sonic-pi-view-spec.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
sbAtomSonicView = require '../lib/sb-atom-sonic-pi-view'
sbAtomSonicPiView = require '../lib/sb-atom-sonic-pi-view'

describe "sbAtomSonicView", ->
describe "sbAtomSonicPiView", ->
it "has one valid test", ->
expect("life").toBe "easy"
# I'm not sure why this test is here, but OK...

0 comments on commit 42fc3b9

Please sign in to comment.