Skip to content

Commit 06172ee

Browse files
author
Zerline
committed
Using booleans ('italic') instead of strings ('text-style').
1 parent 944a945 commit 06172ee

File tree

5 files changed

+1373
-502
lines changed

5 files changed

+1373
-502
lines changed

ipywidgets/widgets/widget_button.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from .widget_core import CoreWidget
1313
from .widget_style import Style
1414
from .trait_types import Color, InstanceDict
15+
from .._version import __jupyter_widgets_controls_version__
1516

1617
from traitlets import Unicode, Bool, CaselessStrEnum, Instance, validate, default
1718
import warnings
@@ -21,14 +22,17 @@
2122
class ButtonStyle(Style, CoreWidget):
2223
"""Button style widget."""
2324
_model_name = Unicode('ButtonStyleModel').tag(sync=True)
25+
_view_name = Unicode('ButtonStyleView').tag(sync=True)
26+
_view_module = Unicode('@jupyter-widgets/controls').tag(sync=True)
27+
_view_module_version = Unicode(__jupyter_widgets_controls_version__).tag(sync=True)
28+
bold = Bool(None, allow_none=True, help="Button text bold.").tag(sync=True)
2429
button_color = Color(None, allow_none=True, help="Color of the button").tag(sync=True)
25-
font_family = Unicode(help="Button text font family.").tag(sync=True)
30+
font_family = Unicode(None, allow_none=True, help="Button text font family.").tag(sync=True)
2631
font_size = Unicode(help="Button text font size.").tag(sync=True)
27-
font_style = Unicode(help="Button text font style.").tag(sync=True)
28-
font_variant = Unicode(help="Button text font variant.").tag(sync=True)
29-
font_weight = Unicode(help="Button text font weight.").tag(sync=True)
32+
font_variant = Unicode(None, allow_none=True, help="Button text font variant.").tag(sync=True)
33+
italic = Bool(None, allow_none=True, help="Button text italic.").tag(sync=True)
3034
text_color = Unicode(help="Button text color.").tag(sync=True)
31-
text_decoration = Unicode(help="Button text decoration.").tag(sync=True)
35+
underline = Bool(None, allow_none=True, help="Button text underline.").tag(sync=True)
3236

3337

3438
@register

packages/base/src/widget_style.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,5 +124,9 @@ export class StyleView extends WidgetView {
124124
}, this);
125125
}
126126

127+
get_traitNames(): string[] {
128+
return this._traitNames;
129+
}
130+
127131
private _traitNames: string[];
128132
}

packages/controls/src/widget_button.ts

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,49 @@
11
// Copyright (c) Jupyter Development Team.
22
// Distributed under the terms of the Modified BSD License.
33

4-
import { DOMWidgetView, StyleModel } from '@jupyter-widgets/base';
4+
import { DOMWidgetView, StyleModel, StyleView } from '@jupyter-widgets/base';
55

66
import { CoreDOMWidgetModel } from './widget_core';
77

88
import { JUPYTER_CONTROLS_VERSION } from './version';
99

10+
export function bold_to_weight(value: boolean): string | null {
11+
if (value == true) return 'bold';
12+
if (value == false) return 'normal';
13+
return null;
14+
}
15+
16+
export function italic_to_style(value: boolean): string | null {
17+
if (value == true) return 'italic';
18+
if (value == false) return 'normal';
19+
return null;
20+
}
21+
22+
export function underline_to_decoration(value: boolean): string | null {
23+
if (value == true) return 'underline';
24+
if (value == false) return 'none';
25+
return null;
26+
}
27+
1028
export class ButtonStyleModel extends StyleModel {
1129
defaults(): Backbone.ObjectHash {
1230
return {
1331
...super.defaults(),
1432
_model_name: 'ButtonStyleModel',
1533
_model_module: '@jupyter-widgets/controls',
16-
_model_module_version: JUPYTER_CONTROLS_VERSION
34+
_model_module_version: JUPYTER_CONTROLS_VERSION,
35+
_view_name: 'ButtonStyleView',
36+
_view_module: '@jupyter-widgets/controls',
37+
_view_module_version: JUPYTER_CONTROLS_VERSION
1738
};
1839
}
1940

2041
public static styleProperties = {
42+
bold: {
43+
selector: '',
44+
attribute: 'font-weight',
45+
default: ''
46+
},
2147
button_color: {
2248
selector: '',
2349
attribute: 'background-color',
@@ -33,7 +59,7 @@ export class ButtonStyleModel extends StyleModel {
3359
attribute: 'font-size',
3460
default: ''
3561
},
36-
font_style: {
62+
italic: {
3763
selector: '',
3864
attribute: 'font-style',
3965
default: ''
@@ -43,24 +69,54 @@ export class ButtonStyleModel extends StyleModel {
4369
attribute: 'font-variant',
4470
default: ''
4571
},
46-
font_weight: {
47-
selector: '',
48-
attribute: 'font-weight',
49-
default: ''
50-
},
5172
text_color: {
5273
selector: '',
5374
attribute: 'color',
5475
default: ''
5576
},
56-
text_decoration: {
77+
underline: {
5778
selector: '',
5879
attribute: 'text-decoration',
5980
default: ''
6081
}
6182
};
6283
}
6384

85+
export class ButtonStyleView extends StyleView {
86+
/**
87+
* Handles when a trait value changes
88+
*/
89+
handleChange(trait: string, value: any): void {
90+
// should be synchronous so that we can measure later.
91+
const parent = this.options.parent as DOMWidgetView;
92+
if (parent) {
93+
const ModelType = this.model.constructor as typeof StyleModel;
94+
const styleProperties = ModelType.styleProperties;
95+
const attribute = styleProperties[trait].attribute;
96+
const selector = styleProperties[trait].selector;
97+
const elements = selector
98+
? parent.el.querySelectorAll<HTMLElement>(selector)
99+
: [parent.el];
100+
let transform = undefined;
101+
if (trait == 'bold') transform = bold_to_weight;
102+
if (trait == 'italic') transform = italic_to_style;
103+
if (trait == 'underline') transform = underline_to_decoration;
104+
if (transform !== undefined) value = transform(value);
105+
if (value === null) {
106+
for (let i = 0; i !== elements.length; ++i) {
107+
elements[i].style.removeProperty(attribute);
108+
}
109+
} else {
110+
for (let i = 0; i !== elements.length; ++i) {
111+
elements[i].style.setProperty(attribute, value);
112+
}
113+
}
114+
} else {
115+
console.warn('Style not applied because a parent view does not exist');
116+
}
117+
}
118+
}
119+
64120
export class ButtonModel extends CoreDOMWidgetModel {
65121
defaults(): Backbone.ObjectHash {
66122
return {

0 commit comments

Comments
 (0)