Skip to content

Commit 731d2f7

Browse files
authored
radioButton.js: Modernize and clean up unused object (#13567)
Port the radio button to GObject. Removes a use of CinnamonGenericContainer. Nothing else in here seems to be used in either Cinnamon or any of the spices. This removes the radio group but this could be modernized and brought back if there is an actual call for it.
1 parent 13eb3ef commit 731d2f7

5 files changed

Lines changed: 47 additions & 198 deletions

File tree

data/theme/cinnamon-sass/widgets/_menus.scss

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ $menuitem_border_radius: $base_border_radius * 1;
2929
background: none;
3030
}
3131

32-
.check-box {
32+
.check-box,
33+
.radiobutton {
3334
StBoxLayout { spacing: 0; }
3435
}
3536
}

data/theme/cinnamon-sass/widgets/_switch-check.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ $check_width: 20px;
3434
// radio
3535

3636
.radiobutton {
37-
CinnamonGenericContainer { spacing: 0.8em; }
37+
StBoxLayout { spacing: 0.8em; }
3838

3939
StBin {
4040
width: $check_width;

js/ui/popupMenu.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -513,15 +513,16 @@ var PopupMenuItem = class PopupMenuItem extends PopupBaseMenuItem {
513513
this._icon = null;
514514
break;
515515
case OrnamentType.DOT:
516-
if ((this._ornament.child)&&(!(this._ornament.child._delegate instanceof RadioButton.RadioBox))) {
516+
if ((this._ornament.child) && (!(this._ornament.child._delegate instanceof RadioButton.RadioButton))) {
517517
this._ornament.child.destroy();
518518
this._ornament.child = null;
519519
}
520520
if (!this._ornament.child) {
521-
let radioOrn = new RadioButton.RadioBox(state);
522-
this._ornament.child = radioOrn.actor;
521+
let radioOrn = new RadioButton.RadioButton();
522+
switchOrn.set_checked(state);
523+
this._ornament.child = radioOrn;
523524
} else {
524-
this._ornament.child._delegate.setToggleState(state);
525+
this._ornament.child.set_checked(state);
525526
}
526527
this._icon = null;
527528
break;
@@ -1158,15 +1159,16 @@ var PopupIndicatorMenuItem = class PopupIndicatorMenuItem extends PopupBaseMenuI
11581159
this._icon = null;
11591160
break;
11601161
case OrnamentType.DOT:
1161-
if ((this._ornament.child)&&(!(this._ornament.child._delegate instanceof RadioButton.RadioBox))) {
1162+
if ((this._ornament.child) && (!(this._ornament.child._delegate instanceof RadioButton.RadioButton))) {
11621163
this._ornament.child.destroy();
11631164
this._ornament.child = null;
11641165
}
11651166
if (!this._ornament.child) {
1166-
let radioOrn = new RadioButton.RadioBox(state);
1167-
this._ornament.child = radioOrn.actor;
1167+
let radioOrn = new RadioButton.RadioButton();
1168+
radioOrn.set_checked(state);
1169+
this._ornament.child = radioOrn;
11681170
} else {
1169-
this._ornament.child._delegate.setToggleState(state);
1171+
this._ornament.child.set_checked(state);
11701172
}
11711173
this._icon = null;
11721174
break;

js/ui/radioButton.js

Lines changed: 28 additions & 183 deletions
Original file line numberDiff line numberDiff line change
@@ -1,196 +1,41 @@
11
const Clutter = imports.gi.Clutter;
2+
const GObject = imports.gi.GObject;
23
const Pango = imports.gi.Pango;
3-
const Cinnamon = imports.gi.Cinnamon;
44
const St = imports.gi.St;
5-
const Signals = imports.signals;
65

7-
const Lang = imports.lang;
8-
9-
function RadioButtonContainer() {
10-
this._init();
11-
}
12-
RadioButtonContainer.prototype = {
13-
_init: function() {
14-
this.actor = new Cinnamon.GenericContainer({ y_align: St.Align.MIDDLE });
15-
this.actor.connect('get-preferred-width',
16-
Lang.bind(this, this._getPreferredWidth));
17-
this.actor.connect('get-preferred-height',
18-
Lang.bind(this, this._getPreferredHeight));
19-
this.actor.connect('allocate',
20-
Lang.bind(this, this._allocate));
21-
this.actor.connect('style-changed', Lang.bind(this,
22-
function() {
23-
let node = this.actor.get_theme_node();
24-
this._spacing = node.get_length('spacing');
25-
}));
26-
this.actor.request_mode = Clutter.RequestMode.HEIGHT_FOR_WIDTH;
6+
var RadioButton = GObject.registerClass(
7+
class RadioButton extends St.Button {
8+
_init(label) {
9+
let container = new St.BoxLayout();
10+
super._init({
11+
style_class: 'radiobutton',
12+
important: true,
13+
child: container,
14+
button_mask: St.ButtonMask.ONE,
15+
toggle_mode: true,
16+
can_focus: true,
17+
x_fill: true,
18+
y_fill: true,
19+
});
2720

2821
this._box = new St.Bin();
29-
this.actor.add_actor(this._box);
30-
31-
this.label = new St.Label();
32-
this.label.clutter_text.set_line_wrap(false);
33-
this.label.clutter_text.set_ellipsize(Pango.EllipsizeMode.NONE);
34-
this.actor.add_actor(this.label);
35-
36-
this._spacing = 0;
37-
},
38-
39-
_getPreferredWidth: function(actor, forHeight, alloc) {
40-
let [minWidth, natWidth] = this._box.get_preferred_width(forHeight);
41-
42-
alloc.min_size = minWidth + this._spacing;
43-
alloc.natural_size = natWidth + this._spacing;
44-
},
45-
46-
_getPreferredHeight: function(actor, forWidth, alloc) {
47-
let [minBoxHeight, natBoxHeight] =
48-
this._box.get_preferred_height(-1);
49-
let [minLabelHeight, natLabelHeight] =
50-
this.label.get_preferred_height(-1);
51-
52-
alloc.min_size = Math.max(minBoxHeight, 2 * minLabelHeight);
53-
alloc.natural_size = Math.max(natBoxHeight, 2 * natLabelHeight);
54-
},
55-
56-
_allocate: function(actor, box, flags) {
57-
let availWidth = box.x2 - box.x1;
58-
let availHeight = box.y2 - box.y1;
59-
60-
let childBox = new Clutter.ActorBox();
61-
let [minBoxWidth, natBoxWidth] =
62-
this._box.get_preferred_width(-1);
63-
let [minBoxHeight, natBoxHeight] =
64-
this._box.get_preferred_height(-1);
65-
childBox.x1 = box.x1;
66-
childBox.x2 = box.x1 + natBoxWidth;
67-
childBox.y1 = box.y1;
68-
childBox.y2 = box.y1 + natBoxHeight;
69-
this._box.allocate(childBox, flags);
70-
71-
childBox.x1 = box.x1 + natBoxWidth + this._spacing;
72-
childBox.x2 = availWidth - childBox.x1;
73-
childBox.y1 = box.y1;
74-
childBox.y2 = box.y2;
75-
this.label.allocate(childBox, flags);
76-
}
77-
};
78-
79-
function RadioBox(state) {
80-
this._init(state);
81-
}
82-
83-
RadioBox.prototype = {
84-
_init: function(state) {
85-
this.actor = new St.Button({ style_class: 'radiobutton',
86-
button_mask: St.ButtonMask.ONE,
87-
toggle_mode: true,
88-
can_focus: true,
89-
x_fill: true,
90-
y_fill: true,
91-
y_align: St.Align.MIDDLE });
92-
93-
this.actor._delegate = this;
94-
this.actor.checked = state;
95-
this._container = new St.Bin();
96-
this.actor.set_child(this._container);
97-
},
98-
99-
setToggleState: function(state) {
100-
this.actor.checked = state;
101-
},
102-
103-
toggle: function() {
104-
this.setToggleState(!this.actor.checked);
105-
},
22+
this._box.set_y_align(Clutter.ActorAlign.START);
23+
container.add_child(this._box);
10624

107-
destroy: function() {
108-
this.actor.destroy();
109-
}
110-
};
111-
112-
function RadioButton(label) {
113-
this._init(label);
114-
}
115-
116-
RadioButton.prototype = {
117-
__proto__: RadioBox.prototype,
118-
119-
_init: function(label) {
120-
RadioBox.prototype._init.call(this, false);
121-
this._container.destroy();
122-
this._container = new RadioButtonContainer();
123-
this.actor.set_child(this._container.actor);
25+
this._label = new St.Label({ y_align: Clutter.ActorAlign.CENTER });
26+
this._label.clutter_text.set_line_wrap(true);
27+
this._label.clutter_text.set_ellipsize(Pango.EllipsizeMode.NONE);
28+
container.add_child(this._label);
12429

12530
if (label)
12631
this.setLabel(label);
127-
},
128-
129-
setLabel: function(label) {
130-
this._container.label.set_text(label);
131-
},
132-
133-
getLabelActor: function() {
134-
return this._container.label;
13532
}
136-
};
137-
138-
function RadioButtonGroup() {
139-
this._init();
140-
}
141-
142-
RadioButtonGroup.prototype = {
143-
_init: function() {
144-
this.actor = new St.BoxLayout({ vertical: true, width: 250 });
145-
this._buttons = [];
146-
this._activeId = null;
147-
},
14833

149-
addButton: function(buttonId, label) {
150-
this.radioButton = new RadioButton(label);
151-
this.radioButton.actor.connect("clicked",
152-
Lang.bind(this, function(actor) {
153-
this.buttonClicked(actor, buttonId);
154-
}));
155-
156-
this._buttons.push({ id: buttonId, button: this.radioButton });
157-
this.actor.add(this.radioButton.actor, { x_fill: true, y_fill: false, y_align: St.Align.MIDDLE });
158-
},
159-
160-
radioChanged: function(actor) {
161-
162-
},
163-
164-
buttonClicked: function(actor, buttonId) {
165-
for (const button of this._buttons) {
166-
if (buttonId !== button['id'] && button['button'].actor.checked) {
167-
button['button'].actor.checked = false;
168-
}
169-
else if (buttonId === button['id'] && !button['button'].actor.checked) {
170-
button['button'].actor.checked = true;
171-
}
172-
}
173-
174-
// Only trigger real changes to radio selection.
175-
if (buttonId !== this._activeId) {
176-
this._activeId = buttonId;
177-
this.emit('radio-changed', this._activeId);
178-
}
179-
},
180-
181-
setActive: function(buttonId) {
182-
for (const button of this._buttons) {
183-
button['button'].actor.checked = buttonId === button['id'];
184-
}
185-
186-
if (this._activeId != buttonId) {
187-
this._activeId = buttonId;
188-
this.emit('radio-changed', this._activeId);
189-
}
190-
},
34+
setLabel(label) {
35+
this._label.set_text(label);
36+
}
19137

192-
getActive: function() {
193-
return this._activeId;
194-
}
195-
}
196-
Signals.addSignalMethods(RadioButtonGroup.prototype);
38+
getLabelActor() {
39+
return this._label;
40+
}
41+
});

js/ui/windowMenu.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,17 @@ var MnemonicLeftOrnamentedMenuItem = class MnemonicLeftOrnamentedMenuItem extend
9696
this._icon = null;
9797
break;
9898
case PopupMenu.OrnamentType.DOT:
99-
if ((this._ornament.child)&&(!(this._ornament.child._delegate instanceof RadioButton.RadioBox))) {
99+
if ((this._ornament.child) && (!(this._ornament.child._delegate instanceof RadioButton.RadioButton))) {
100100
this._ornament.child.destroy();
101101
this._ornament.child = null;
102102
}
103103
if (!this._ornament.child) {
104-
let radioOrn = new RadioButton.RadioBox(state);
105-
this._ornament.child = radioOrn.actor;
106-
radioOrn.actor.reactive = false;
104+
let radioOrn = new RadioButton.RadioButton();
105+
radioOrn.set_checked(state);
106+
this._ornament.child = radioOrn;
107+
radioOrn.reactive = false;
107108
} else {
108-
this._ornament.child._delegate.setToggleState(state);
109+
this._ornament.child.set_checked(state);
109110
}
110111
this._icon = null;
111112
break;

0 commit comments

Comments
 (0)