Skip to content

Commit 2e23223

Browse files
committed
Expose playing trait for the Play Widget
1 parent af90227 commit 2e23223

File tree

4 files changed

+803
-786
lines changed

4 files changed

+803
-786
lines changed

ipywidgets/widgets/widget_int.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -224,17 +224,18 @@ def _validate_value(self, proposal):
224224
class Play(_BoundedInt):
225225
"""Play/repeat buttons to step through values automatically, and optionally loop.
226226
"""
227-
interval = CInt(100, help="The maximum value for the play control.").tag(sync=True)
228-
step = CInt(1, help="Increment step").tag(sync=True)
229-
disabled = Bool(False, help="Enable or disable user changes").tag(sync=True)
230-
231227
_view_name = Unicode('PlayView').tag(sync=True)
232228
_model_name = Unicode('PlayModel').tag(sync=True)
233229

234-
_playing = Bool(help="Whether the control is currently playing.").tag(sync=True)
235230
_repeat = Bool(help="Whether the control will repeat in a continous loop.").tag(sync=True)
231+
232+
interval = CInt(100, help="The maximum value for the play control.").tag(sync=True)
233+
step = CInt(1, help="Increment step").tag(sync=True)
234+
disabled = Bool(False, help="Enable or disable user changes").tag(sync=True)
235+
playing = Bool(help="Whether the control is currently playing.").tag(sync=True)
236236
show_repeat = Bool(True, help="Show the repeat toggle button in the widget.").tag(sync=True)
237237

238+
238239
class _BoundedIntRange(_IntRange):
239240
max = CInt(100, help="Max value").tag(sync=True)
240241
min = CInt(0, help="Min value").tag(sync=True)

packages/controls/src/widget_int.ts

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -777,8 +777,8 @@ class PlayModel extends BoundedIntModel {
777777
return _.extend(super.defaults(), {
778778
_model_name: 'PlayModel',
779779
_view_name: 'PlayView',
780-
_playing: false,
781780
_repeat: false,
781+
playing: false,
782782
show_repeat: true,
783783
interval: 100,
784784
step: 1,
@@ -790,41 +790,40 @@ class PlayModel extends BoundedIntModel {
790790
}
791791

792792
loop() {
793-
if (this.get('_playing')) {
794-
let next_value = this.get('value') + this.get('step');
795-
if (next_value <= this.get('max')) {
796-
this.set('value', next_value);
793+
if (!this.get('playing')) {
794+
return;
795+
}
796+
let next_value = this.get('value') + this.get('step');
797+
if (next_value <= this.get('max')) {
798+
this.set('value', next_value);
799+
this.schedule_next();
800+
} else {
801+
if (this.get('_repeat')) {
802+
this.set('value', this.get('min'));
797803
this.schedule_next();
798804
} else {
799-
if(this.get('_repeat')) {
800-
this.set('value', this.get('min'));
801-
this.schedule_next();
802-
} else {
803-
this.set('_playing', false);
804-
}
805+
this.set('playing', false);
805806
}
806-
this.save_changes();
807807
}
808+
this.save_changes();
808809
}
809810

810811
schedule_next() {
811812
window.setTimeout(this.loop.bind(this), this.get('interval'));
812813
}
813814

814815
stop() {
815-
this.set('_playing', false);
816816
this.set('value', this.get('min'));
817817
this.save_changes();
818818
}
819819

820820
pause() {
821-
this.set('_playing', false);
821+
this.set('playing', false);
822822
this.save_changes();
823823
}
824824

825825
play() {
826-
this.set('_playing', true);
827-
if (this.get('value') == this.get('max')) {
826+
if (this.get('value') === this.get('max')) {
828827
// if the value is at the end, reset if first, and then schedule the next
829828
this.set('value', this.get('min'));
830829
this.schedule_next();
@@ -834,6 +833,7 @@ class PlayModel extends BoundedIntModel {
834833
// loop will call save_changes in this case
835834
this.loop();
836835
}
836+
this.save_changes();
837837
}
838838

839839
repeat() {
@@ -878,15 +878,15 @@ class PlayView extends DOMWidgetView {
878878
repeatIcon.className = 'fa fa-retweet';
879879
this.repeatButton.appendChild(repeatIcon);
880880

881-
this.playButton.onclick = this.model.play.bind(this.model);
881+
this.playButton.onclick = this.onPlayClicked.bind(this);
882882
this.pauseButton.onclick = this.model.pause.bind(this.model);
883883
this.stopButton.onclick = this.model.stop.bind(this.model);
884884
this.repeatButton.onclick = this.model.repeat.bind(this.model);
885885

886-
this.listenTo(this.model, 'change:_playing', this.update_playing);
886+
this.listenTo(this.model, 'change:playing', this.onPlayingChanged);
887887
this.listenTo(this.model, 'change:_repeat', this.update_repeat);
888888
this.listenTo(this.model, 'change:show_repeat', this.update_repeat);
889-
this.update_playing();
889+
this.updatePlaying();
890890
this.update_repeat();
891891
this.update();
892892
}
@@ -897,11 +897,27 @@ class PlayView extends DOMWidgetView {
897897
this.pauseButton.disabled = disabled;
898898
this.stopButton.disabled = disabled;
899899
this.repeatButton.disabled = disabled;
900-
this.update_playing();
900+
this.updatePlaying();
901+
}
902+
903+
onPlayClicked() {
904+
const playing = this.model.get('playing');
905+
this.model.set('playing', !playing);
906+
}
907+
908+
onPlayingChanged() {
909+
this.updatePlaying();
910+
const previous = this.model.previous('playing');
911+
const current = this.model.get('playing');
912+
if (!previous && current) {
913+
this.model.play();
914+
} else {
915+
this.model.pause();
916+
}
901917
}
902918

903-
update_playing() {
904-
let playing = this.model.get('_playing');
919+
updatePlaying() {
920+
let playing = this.model.get('playing');
905921
let disabled = this.model.get('disabled');
906922
if (playing) {
907923
if (!disabled) {

0 commit comments

Comments
 (0)