Reuse old redio buttons

When a media is changed, normally one would expect to create new radio buttons with video/audio/subtitle tracks names corresponding to current video, but this is inefficient. Destroying objects just to create similiar ones again does take a long time and might lead to memory leaks. That is why a better and faster approach is to simply edit already available objects to match our expectations instead. This commit does just that for tracks radio buttons.
This commit is contained in:
Rafostar
2020-09-10 19:50:41 +02:00
parent e9ec155e7b
commit b8ed6b32dc

View File

@@ -1,8 +1,11 @@
const { GObject, Gtk } = imports.gi; const { GObject, Gtk } = imports.gi;
const Debug = imports.clapper_src.debug;
const CONTROLS_MARGIN = 4; const CONTROLS_MARGIN = 4;
const CONTROLS_SPACING = 4; const CONTROLS_SPACING = 4;
let { debug } = Debug;
var Controls = GObject.registerClass({ var Controls = GObject.registerClass({
Signals: { Signals: {
'position-seeking-changed': { 'position-seeking-changed': {
@@ -116,6 +119,7 @@ var Controls = GObject.registerClass({
button.osd = this.fullscreenMode; button.osd = this.fullscreenMode;
button.popover.add(button.popoverBox); button.popover.add(button.popoverBox);
button.connect('clicked', this._onPopoverButtonClicked.bind(this, button)); button.connect('clicked', this._onPopoverButtonClicked.bind(this, button));
button.popoverBox.show();
return button; return button;
} }
@@ -123,27 +127,53 @@ var Controls = GObject.registerClass({
addRadioButtons(box, array, activeId) addRadioButtons(box, array, activeId)
{ {
let group = null; let group = null;
let children = box.get_children();
let lastEl = (children.length > array.length)
? children.length
: array.length;
for(let el of array) { for(let i = 0; i < lastEl; i++) {
let radioButton = new Gtk.RadioButton({ if(i >= array.length) {
label: el.label, children[i].hide();
group: group, debug(`hiding unused ${children[i].trackType} radioButton nr: ${i}`);
}); continue;
}
let el = array[i];
let radioButton;
if(i < children.length) {
radioButton = children[i];
debug(`reusing ${el.type} radioButton nr: ${i}`);
}
else {
debug(`creating new ${el.type} radioButton nr: ${i}`);
radioButton = new Gtk.RadioButton({
group: group,
});
radioButton.connect(
'toggled',
this._onTrackRadioButtonToggled.bind(this, radioButton)
);
this.setDefaultWidgetBehaviour(radioButton);
box.add(radioButton);
}
radioButton.label = el.label;
debug(`radioButton label: ${radioButton.label}`);
radioButton.trackType = el.type; radioButton.trackType = el.type;
debug(`radioButton type: ${radioButton.trackType}`);
radioButton.trackId = el.value; radioButton.trackId = el.value;
debug(`radioButton track id: ${radioButton.trackId}`);
if(radioButton.trackId === activeId) if(radioButton.trackId === activeId) {
radioButton.set_active(true); radioButton.set_active(true);
debug(`activated ${el.type} radioButton nr: ${i}`);
}
if(!group) if(!group)
group = radioButton; group = radioButton;
radioButton.connect( radioButton.show();
'toggled', this._onTrackRadioButtonToggled.bind(this, radioButton)
);
this.setDefaultWidgetBehaviour(radioButton);
box.add(radioButton);
} }
box.show_all();
} }
setDefaultWidgetBehaviour(widget) setDefaultWidgetBehaviour(widget)