Skip to content

Commit 39c167e

Browse files
authored
Merge pull request #196 from martinRenou/toolbar_collapsed_state
Add collapsed state to the toolbar
2 parents 46d83db + 8ffea99 commit 39c167e

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

ipympl/backend_nbagg.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ class Toolbar(DOMWidget, NavigationToolbar2WebAgg):
9696
button_style = CaselessStrEnum(
9797
values=['primary', 'success', 'info', 'warning', 'danger', ''], default_value='',
9898
help="""Use a predefined styling for the button.""").tag(sync=True)
99+
collapsed = Bool(True).tag(sync=True)
99100

100101
_current_action = Enum(values=['pan', 'zoom', ''], default_value='').tag(sync=True)
101102

js/src/toolbar_widget.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class ToolbarModel extends widgets.DOMWidgetModel {
1717
toolitems: [],
1818
orientation: 'vertical',
1919
button_style: '',
20+
collapsed: true,
2021
_current_action: '',
2122
};
2223
}
@@ -42,7 +43,10 @@ class ToolbarView extends widgets.DOMWidgetView {
4243
this.toggle_button.setAttribute('href', '#');
4344
this.toggle_button.setAttribute('title', 'Toggle Interaction');
4445
this.toggle_button.style.outline = 'none';
45-
this.toggle_button.addEventListener('click', this.toggle_interaction.bind(this));
46+
this.toggle_button.addEventListener('click', () => {
47+
this.model.set('collapsed', !this.model.get('collapsed'));
48+
this.model.save_changes();
49+
});
4650

4751
const icon = document.createElement('i');
4852
icon.classList = 'center fa fa-bars';
@@ -141,15 +145,14 @@ class ToolbarView extends widgets.DOMWidgetView {
141145
}
142146
}
143147

144-
toggle_interaction() {
145-
// Toggle the interactivity of the figure.
146-
const visible = this.toolbar.style.display !== 'none';
147-
this.toolbar.style.display = visible ? 'none' : '';
148+
update_collapsed() {
149+
this.toolbar.style.display = this.model.get('collapsed') ? '' : 'none';
148150
}
149151

150152
model_events() {
151153
this.model.on('change:orientation', this.update_orientation.bind(this));
152154
this.model.on_some_change(['button_style', '_current_action'], this.set_buttons_style.bind(this));
155+
this.model.on('change:collapsed', this.update_collapsed.bind(this));
153156
}
154157

155158
update_orientation() {

0 commit comments

Comments
 (0)