Create gio settings only once

This commit is contained in:
Rafostar
2020-11-04 15:05:16 +01:00
parent 8296f40382
commit de7a850cc2
6 changed files with 39 additions and 31 deletions

View File

@@ -4,8 +4,10 @@ const { Widget } = imports.clapper_src.widget;
const Debug = imports.clapper_src.debug; const Debug = imports.clapper_src.debug;
const Menu = imports.clapper_src.menu; const Menu = imports.clapper_src.menu;
const Misc = imports.clapper_src.misc; const Misc = imports.clapper_src.misc;
const Shared = imports.clapper_src.shared;
let { debug } = Debug; let { debug } = Debug;
let { settings } = Shared;
const APP_NAME = 'Clapper'; const APP_NAME = 'Clapper';
const APP_ID = 'com.github.rafostar.Clapper'; const APP_ID = 'com.github.rafostar.Clapper';
@@ -47,7 +49,7 @@ class ClapperApp extends Gtk.Application
let clapperWidget = new Widget(); let clapperWidget = new Widget();
window.set_child(clapperWidget); window.set_child(clapperWidget);
let size = clapperWidget.player.settings.get_string('window-size'); let size = settings.get_string('window-size');
try { try {
size = JSON.parse(size); size = JSON.parse(size);
} }

View File

@@ -2,8 +2,10 @@ const { Gdk, Gio, GLib, GObject, Gst, GstPlayer, Gtk } = imports.gi;
const ByteArray = imports.byteArray; const ByteArray = imports.byteArray;
const { PlayerBase } = imports.clapper_src.playerBase; const { PlayerBase } = imports.clapper_src.playerBase;
const Debug = imports.clapper_src.debug; const Debug = imports.clapper_src.debug;
const Shared = imports.clapper_src.shared;
let { debug } = Debug; let { debug } = Debug;
let { settings } = Shared;
var Player = GObject.registerClass( var Player = GObject.registerClass(
class ClapperPlayer extends PlayerBase class ClapperPlayer extends PlayerBase
@@ -205,8 +207,8 @@ class ClapperPlayer extends PlayerBase
let { controls } = this.widget.get_ancestor(Gtk.Grid); let { controls } = this.widget.get_ancestor(Gtk.Grid);
let max = controls.positionAdjustment.get_upper(); let max = controls.positionAdjustment.get_upper();
let seekingValue = this.settings.get_int('seeking-value'); let seekingValue = settings.get_int('seeking-value');
let seekingUnit = this.settings.get_string('seeking-unit'); let seekingUnit = settings.get_string('seeking-unit');
switch(seekingUnit) { switch(seekingUnit) {
case 'minute': case 'minute':
@@ -351,9 +353,9 @@ class ClapperPlayer extends PlayerBase
if(!this.doneStartup) { if(!this.doneStartup) {
this.doneStartup = true; this.doneStartup = true;
if(this.settings.get_string('volume-initial') === 'custom') if(settings.get_string('volume-initial') === 'custom')
this.set_volume(this.settings.get_int('volume-value') / 100); this.set_volume(settings.get_int('volume-value') / 100);
if(this.settings.get_boolean('fullscreen-auto')) { if(settings.get_boolean('fullscreen-auto')) {
let root = player.widget.get_root(); let root = player.widget.get_root();
let clapperWidget = root.get_child(); let clapperWidget = root.get_child();
if(!clapperWidget.fullscreenMode) { if(!clapperWidget.fullscreenMode) {
@@ -617,7 +619,7 @@ class ClapperPlayer extends PlayerBase
if(!clapperWidget.fullscreenMode && !clapperWidget.floatingMode) { if(!clapperWidget.fullscreenMode && !clapperWidget.floatingMode) {
let size = window.get_size(); let size = window.get_size();
if(size[0] > 0 && size[1] > 0) { if(size[0] > 0 && size[1] > 0) {
this.settings.set_string('window-size', JSON.stringify(size)); settings.set_string('window-size', JSON.stringify(size));
debug(`saved window dimensions: ${size[0]}x${size[1]}`); debug(`saved window dimensions: ${size[0]}x${size[1]}`);
} }
} }

View File

@@ -1,5 +1,6 @@
const { Gio, GLib, GObject, Gst, GstPlayer, Gtk } = imports.gi; const { Gio, GLib, GObject, Gst, GstPlayer, Gtk } = imports.gi;
const Debug = imports.clapper_src.debug; const Debug = imports.clapper_src.debug;
const Shared = imports.clapper_src.shared;
/* PlayFlags are not exported through GI */ /* PlayFlags are not exported through GI */
Gst.PlayFlags = { Gst.PlayFlags = {
@@ -19,6 +20,7 @@ Gst.PlayFlags = {
}; };
let { debug } = Debug; let { debug } = Debug;
let { settings } = Shared;
var PlayerBase = GObject.registerClass( var PlayerBase = GObject.registerClass(
class ClapperPlayerBase extends GstPlayer.Player class ClapperPlayerBase extends GstPlayer.Player
@@ -62,17 +64,13 @@ class ClapperPlayerBase extends GstPlayer.Player
this.widget.hexpand = true; this.widget.hexpand = true;
this.widget.set_opacity(0); this.widget.set_opacity(0);
this.settings = new Gio.Settings({
schema_id: 'com.github.rafostar.Clapper'
});
this.visualization_enabled = false; this.visualization_enabled = false;
this.set_all_plugins_ranks(); this.set_all_plugins_ranks();
this.set_initial_config(); this.set_initial_config();
this.set_and_bind_settings(); this.set_and_bind_settings();
this.settings.connect('changed', this._onSettingsKeyChanged.bind(this)); settings.connect('changed', this._onSettingsKeyChanged.bind(this));
} }
set_and_bind_settings() set_and_bind_settings()
@@ -82,7 +80,7 @@ class ClapperPlayerBase extends GstPlayer.Player
]; ];
for(let key of settingsToSet) for(let key of settingsToSet)
this._onSettingsKeyChanged(this.settings, key); this._onSettingsKeyChanged(settings, key);
//let flag = Gio.SettingsBindFlags.GET; //let flag = Gio.SettingsBindFlags.GET;
} }
@@ -124,13 +122,13 @@ class ClapperPlayerBase extends GstPlayer.Player
/* Set empty plugin list if someone messed it externally */ /* Set empty plugin list if someone messed it externally */
try { try {
data = JSON.parse(this.settings.get_string('plugin-ranking')); data = JSON.parse(settings.get_string('plugin-ranking'));
if(!Array.isArray(data)) if(!Array.isArray(data))
throw new Error('plugin ranking data is not an array!'); throw new Error('plugin ranking data is not an array!');
} }
catch(err) { catch(err) {
debug(err); debug(err);
this.settings.set_string('plugin-ranking', "[]"); settings.set_string('plugin-ranking', "[]");
} }
for(let plugin of data) { for(let plugin of data) {

View File

@@ -1,5 +1,8 @@
const { GObject, Gtk, Pango } = imports.gi; const { GObject, Gtk, Pango } = imports.gi;
const PrefsBase = imports.clapper_src.prefsBase; const PrefsBase = imports.clapper_src.prefsBase;
const Shared = imports.clapper_src.shared;
let { settings } = Shared;
let GeneralPage = GObject.registerClass( let GeneralPage = GObject.registerClass(
class ClapperGeneralPage extends PrefsBase.Grid class ClapperGeneralPage extends PrefsBase.Grid
@@ -145,7 +148,7 @@ class ClapperGStreamerPage extends PrefsBase.Grid
removeButton.connect('clicked', this._onRemoveButtonClicked.bind(this, listStore)); removeButton.connect('clicked', this._onRemoveButtonClicked.bind(this, listStore));
treeSelection.connect('changed', this._onTreeSelectionChanged.bind(this, removeButton)); treeSelection.connect('changed', this._onTreeSelectionChanged.bind(this, removeButton));
this.settingsChangedSignal = this.settings.connect( this.settingsChangedSignal = settings.connect(
'changed::plugin-ranking', this.refreshListStore.bind(this, listStore) 'changed::plugin-ranking', this.refreshListStore.bind(this, listStore)
); );
@@ -154,7 +157,7 @@ class ClapperGStreamerPage extends PrefsBase.Grid
refreshListStore(listStore) refreshListStore(listStore)
{ {
let data = JSON.parse(this.settings.get_string('plugin-ranking')); let data = JSON.parse(settings.get_string('plugin-ranking'));
listStore.clear(); listStore.clear();
for(let plugin of data) { for(let plugin of data) {
@@ -171,9 +174,9 @@ class ClapperGStreamerPage extends PrefsBase.Grid
updatePlugin(index, prop, value) updatePlugin(index, prop, value)
{ {
let data = JSON.parse(this.settings.get_string('plugin-ranking')); let data = JSON.parse(settings.get_string('plugin-ranking'));
data[index][prop] = value; data[index][prop] = value;
this.settings.set_string('plugin-ranking', JSON.stringify(data)); settings.set_string('plugin-ranking', JSON.stringify(data));
} }
_onTreeSelectionChanged(removeButton, treeSelection) _onTreeSelectionChanged(removeButton, treeSelection)
@@ -190,13 +193,13 @@ class ClapperGStreamerPage extends PrefsBase.Grid
_onAddButtonClicked(listStore, button) _onAddButtonClicked(listStore, button)
{ {
let data = JSON.parse(this.settings.get_string('plugin-ranking')); let data = JSON.parse(settings.get_string('plugin-ranking'));
data.push({ data.push({
apply: false, apply: false,
name: '', name: '',
rank: 0, rank: 0,
}); });
this.settings.set_string('plugin-ranking', JSON.stringify(data)); settings.set_string('plugin-ranking', JSON.stringify(data));
} }
_onRemoveButtonClicked(listStore, button) _onRemoveButtonClicked(listStore, button)
@@ -204,9 +207,9 @@ class ClapperGStreamerPage extends PrefsBase.Grid
if(this.activeIndex < 0) if(this.activeIndex < 0)
return; return;
let data = JSON.parse(this.settings.get_string('plugin-ranking')); let data = JSON.parse(settings.get_string('plugin-ranking'));
data.splice(this.activeIndex, 1); data.splice(this.activeIndex, 1);
this.settings.set_string('plugin-ranking', JSON.stringify(data)); settings.set_string('plugin-ranking', JSON.stringify(data));
} }
_onApplyCellEdited(cell, path) _onApplyCellEdited(cell, path)
@@ -235,7 +238,7 @@ class ClapperGStreamerPage extends PrefsBase.Grid
{ {
super._onClose('gstreamer'); super._onClose('gstreamer');
this.settings.disconnect(this.settingsChangedSignal); settings.disconnect(this.settingsChangedSignal);
this.settingsChangedSignal = null; this.settingsChangedSignal = null;
} }
}); });

View File

@@ -1,7 +1,9 @@
const { Gio, GObject, Gtk } = imports.gi; const { Gio, GObject, Gtk } = imports.gi;
const Debug = imports.clapper_src.debug; const Debug = imports.clapper_src.debug;
const Shared = imports.clapper_src.shared;
let { debug } = Debug; let { debug } = Debug;
let { settings } = Shared;
var Notebook = GObject.registerClass( var Notebook = GObject.registerClass(
class ClapperPrefsNotebook extends Gtk.Notebook class ClapperPrefsNotebook extends Gtk.Notebook
@@ -68,11 +70,7 @@ class ClapperPrefsGrid extends Gtk.Grid
column_spacing: 20, column_spacing: 20,
}); });
this.settings = new Gio.Settings({
schema_id: 'com.github.rafostar.Clapper'
});
this.flag = Gio.SettingsBindFlags.DEFAULT; this.flag = Gio.SettingsBindFlags.DEFAULT;
this.gridIndex = 0; this.gridIndex = 0;
this.widgetDefaults = { this.widgetDefaults = {
width_request: 160, width_request: 160,
@@ -159,7 +157,7 @@ class ClapperPrefsGrid extends Gtk.Grid
for(let entry of entries) for(let entry of entries)
comboBox.append(entry[0], entry[1]); comboBox.append(entry[0], entry[1]);
this.settings.bind(setting, comboBox, 'active-id', this.flag); settings.bind(setting, comboBox, 'active-id', this.flag);
return comboBox; return comboBox;
} }
@@ -169,7 +167,7 @@ class ClapperPrefsGrid extends Gtk.Grid
let spinButton = new Gtk.SpinButton(this.widgetDefaults); let spinButton = new Gtk.SpinButton(this.widgetDefaults);
spinButton.set_range(min, max); spinButton.set_range(min, max);
spinButton.set_increments(1, 2); spinButton.set_increments(1, 2);
this.settings.bind(setting, spinButton, 'value', this.flag); settings.bind(setting, spinButton, 'value', this.flag);
return spinButton; return spinButton;
} }
@@ -179,7 +177,7 @@ class ClapperPrefsGrid extends Gtk.Grid
let checkButton = new Gtk.CheckButton({ let checkButton = new Gtk.CheckButton({
label: text || null, label: text || null,
}); });
this.settings.bind(setting, checkButton, 'active', this.flag); settings.bind(setting, checkButton, 'active', this.flag);
return checkButton; return checkButton;
} }

5
clapper_src/shared.js Normal file
View File

@@ -0,0 +1,5 @@
const { Gio } = imports.gi;
var settings = new Gio.Settings({
schema_id: 'com.github.rafostar.Clapper'
});