This commit is contained in:
2024-07-08 22:46:35 +02:00
parent 02f44c49d2
commit 27254d817a
56249 changed files with 808097 additions and 1 deletions

View File

@ -0,0 +1,338 @@
/**
* Prefs Library
*
* @author Javad Rahmatzadeh <j.rahmatzadeh@gmail.com>
* @copyright 2020-2024
* @license GPL-3.0-only
*/
/**
* prefs widget for showing prefs window
*/
export class Prefs
{
/**
* Current shell version
*
* @type {number|null}
*/
#shellVersion = null;
/**
* Instance of PrefsKeys
*
* @type {PrefsKeys|null}
*/
#prefsKeys = null;
/**
* Instance of Gtk.Builder
*
* @type {Gtk.Builder|null}
*/
#builder = null;
/**
* Instance of Gio.Settings
*
* @type {Settings|null}
*/
#settings = null;
/**
* Instance of Gdk
*
* @type {Gdk|null}
*/
#gdk = null;
/**
* All available profile names
*
* @type {Array}
*/
#profiles = [
'default',
'minimal',
'superminimal',
];
/**
* class constructor
*
* @param {Object} dependencies
* 'Builder' instance of Gtk::Builder
* 'Settings' instance of Gio::Settings
* 'Gdk' reference to Gdk
* 'GLib' reference to GLib
* @param {PrefsKeys.PrefsKeys} prefsKeys instance of PrefsKeys
* @param {number} shellVersion float in major.minor format
*/
constructor(dependencies, prefsKeys, shellVersion)
{
this.#settings = dependencies['Settings'] || null;
this.#builder = dependencies['Builder'] || null;
this.#gdk = dependencies['Gdk'] || null;
this.#prefsKeys = prefsKeys;
this.#shellVersion = shellVersion;
}
/**
* fill prefs window
*
* @param {string} UIFolderPath folder path to ui folder
* @param {string} gettextDomain gettext domain
*
* @returns {void}
*/
fillPrefsWindow(window, UIFolderPath, gettextDomain)
{
// changing the order here can change the elements order in ui
let uiFilenames = [
'profile',
'visibility',
'icons',
'behavior',
'customize',
];
this.#builder.set_translation_domain(gettextDomain);
for (let uiFilename of uiFilenames) {
this.#builder.add_from_file(`${UIFolderPath}/adw/${uiFilename}.ui`);
}
for (let uiFilename of uiFilenames) {
let page = this.#builder.get_object(uiFilename);
window.add(page);
}
this.#setValues();
this.#guessProfile();
this.#onlyShowSupportedRows();
this.#registerAllSignals(window);
this.#setWindowSize(window);
window.search_enabled = true;
}
/**
* set window size
*
* @param {Adw.PreferencesWindow} window prefs window
*
* @returns {void}
*/
#setWindowSize(window)
{
let [pmWidth, pmHeight, pmScale] = this.#getPrimaryMonitorInfo();
let sizeTolerance = 50;
let width = 600;
let height = 650;
if (
(pmWidth / pmScale) - sizeTolerance >= width &&
(pmHeight / pmScale) - sizeTolerance >= height
) {
window.set_default_size(width, height);
}
}
/**
* get primary monitor info
*
* @returns {Array} [width, height, scale]
*/
#getPrimaryMonitorInfo()
{
let display = this.#gdk.Display.get_default();
let pm = display.get_monitors().get_item(0);
if (!pm) {
return [700, 500, 1];
}
let geo = pm.get_geometry();
let scale = pm.get_scale_factor();
return [geo.width, geo.height, scale];
}
/**
* register all signals
*
* @param {Adw.PreferencesWindow} window prefs dialog
*
* @returns {void}
*/
#registerAllSignals(window)
{
this.#registerKeySignals();
this.#registerProfileSignals();
}
/**
* register signals of all prefs keys
*
* @returns {void}
*/
#registerKeySignals()
{
// all available keys
for (let [, key] of Object.entries(this.#prefsKeys.keys)) {
switch (key.widgetType) {
case 'GtkSwitch':
this.#builder.get_object(key.widgetId).connect('state-set', (w) => {
this.#settings.set_boolean(key.name, w.get_active());
this.#guessProfile();
});
break;
case 'AdwActionRow':
this.#builder.get_object(key.widgetId).connect('notify::selected-item', (w) => {
let index = w.get_selected();
let value = (index in key.maps) ? key.maps[index] : index;
this.#settings.set_int(key.name, value);
this.#guessProfile();
});
break;
}
}
}
/**
* register profile signals
*
* @returns {void}
*/
#registerProfileSignals()
{
for (let profile of this.#profiles) {
let widget = this.#builder.get_object(`profile_${profile}`);
if (!widget) {
break;
}
widget.connect('clicked', (w) => {
this.#setValues(profile);
});
}
}
/**
* can check all current values and guess the profile based on the values
*
* @returns {void}
*/
#guessProfile()
{
let totalCount = 0;
let matchCount = {};
for (let profile of this.#profiles) {
matchCount[profile] = 0;
}
for (let [, key] of Object.entries(this.#prefsKeys.keys)) {
if (!key.supported) {
continue;
}
let value;
switch (key.widgetType) {
case 'GtkSwitch':
value = this.#builder.get_object(key.widgetId).get_active();
break;
case 'AdwActionRow':
value = this.#builder.get_object(key.widgetId).get_selected();
break;
default:
value = '';
continue;
}
for (let profile of this.#profiles) {
if (key.profiles[profile] === value) {
matchCount[profile]++;
}
}
totalCount++;
}
let currentProfile = 'custom';
for (let profile of this.#profiles) {
if (matchCount[profile] === totalCount) {
currentProfile = profile;
break;
}
}
let widget = this.#builder.get_object(`profile_${currentProfile}`);
if (widget) {
widget.set_active(true);
}
}
/**
* set values for all elements
*
* @param {string} profile profile name or null for get it from gsettings
*
* @returns {void}
*/
#setValues(profile)
{
for (let [, key] of Object.entries(this.#prefsKeys.keys)) {
let widget = this.#builder.get_object(key.widgetId);
switch (key.widgetType) {
case 'GtkSwitch':
let value
= (profile)
? key.profiles[profile]
: this.#settings.get_boolean(key.name);
widget.set_active(value);
break;
case 'AdwActionRow':
let index
= (profile)
? key.profiles[profile]
: this.#settings.get_int(key.name);
for (let k in key.maps) {
if (key.maps[k] === index) {
index = k;
break;
}
}
widget.set_selected(index);
break;
}
}
}
/**
* apply all supported keys to the elements
*
* @returns {void}
*/
#onlyShowSupportedRows()
{
for (let [, key] of Object.entries(this.#prefsKeys.keys)) {
let row = this.#builder.get_object(`${key.id}_row`);
let visible = key.supported;
row.visible = visible;
}
}
};

View File

@ -0,0 +1,858 @@
/**
* PrefsKeys Library
*
* @author Javad Rahmatzadeh <j.rahmatzadeh@gmail.com>
* @copyright 2020-2024
* @license GPL-3.0-only
*/
/**
* prefs keys
*/
export class PrefsKeys
{
/**
* Current shell version
*
* @type {number|null}
*/
#shellVersion = null;
/**
* class constructor
*
* @param {number} shellVersion float in major.minor format
*/
constructor(shellVersion)
{
this.#shellVersion = shellVersion;
/**
* holds all keys generated by this.#setKey()
*
* @member {Object}
*/
this.keys = {};
this.#setDefaults();
}
/**
* set all default keys
*
* @returns {void}
*/
#setDefaults()
{
this.#setKey(
'visibility',
'panel',
'GtkSwitch',
true,
{
default: true,
minimal: false,
superminimal: false,
}
);
this.#setKey(
'visibility',
'panel-in-overview',
'GtkSwitch',
true,
{
default: true,
minimal: true,
superminimal: false,
}
);
this.#setKey(
'visibility',
'activities-button',
'GtkSwitch',
true,
{
default: true,
minimal: true,
superminimal: true,
}
);
this.#setKey(
'visibility',
'clock-menu',
'GtkSwitch',
true,
{
default: true,
minimal: true,
superminimal: true,
}
);
this.#setKey(
'visibility',
'keyboard-layout',
'GtkSwitch',
true,
{
default: true,
minimal: true,
superminimal: true,
}
);
this.#setKey(
'visibility',
'accessibility-menu',
'GtkSwitch',
true,
{
default: true,
minimal: false,
superminimal: true,
}
);
this.#setKey(
'visibility',
'quick-settings',
'GtkSwitch',
this.#shellVersion >= 43,
{
default: true,
minimal: true,
superminimal: true,
}
);
this.#setKey(
'visibility',
'screen-sharing-indicator',
'GtkSwitch',
this.#shellVersion >= 43,
{
default: true,
minimal: true,
superminimal: true,
}
);
this.#setKey(
'visibility',
'screen-recording-indicator',
'GtkSwitch',
this.#shellVersion >= 43,
{
default: true,
minimal: true,
superminimal: true,
}
);
this.#setKey(
'visibility',
'search',
'GtkSwitch',
true,
{
default: true,
minimal: false,
superminimal: false,
}
);
this.#setKey(
'visibility',
'dash',
'GtkSwitch',
true,
{
default: true,
minimal: true,
superminimal: false,
}
);
this.#setKey(
'visibility',
'dash-separator',
'GtkSwitch',
true,
{
default: true,
minimal: true,
superminimal: true,
}
);
this.#setKey(
'visibility',
'dash-app-running',
'GtkSwitch',
true,
{
default: true,
minimal: true,
superminimal: true,
}
);
this.#setKey(
'visibility',
'osd',
'GtkSwitch',
true,
{
default: true,
minimal: true,
superminimal: false,
}
);
this.#setKey(
'visibility',
'workspace-popup',
'GtkSwitch',
true,
{
default: true,
minimal: true,
superminimal: false,
}
);
this.#setKey(
'visibility',
'workspace',
'GtkSwitch',
true,
{
default: true,
minimal: false,
superminimal: false,
}
);
this.#setKey(
'visibility',
'background-menu',
'GtkSwitch',
true,
{
default: true,
minimal: true,
superminimal: false,
}
);
this.#setKey(
'visibility',
'show-apps-button',
'GtkSwitch',
true,
{
default: true,
minimal: false,
superminimal: true,
}
);
this.#setKey(
'visibility',
'workspaces-in-app-grid',
'GtkSwitch',
true,
{
default: true,
minimal: false,
superminimal: false,
}
);
this.#setKey(
'visibility',
'window-preview-caption',
'GtkSwitch',
true,
{
default: true,
minimal: true,
superminimal: false,
}
);
this.#setKey(
'visibility',
'window-preview-close-button',
'GtkSwitch',
true,
{
default: true,
minimal: true,
superminimal: false,
}
);
this.#setKey(
'visibility',
'ripple-box',
'GtkSwitch',
true,
{
default: true,
minimal: false,
superminimal: false,
}
);
this.#setKey(
'visibility',
'world-clock',
'GtkSwitch',
true,
{
default: true,
minimal: true,
superminimal: true,
}
);
this.#setKey(
'visibility',
'weather',
'GtkSwitch',
true,
{
default: true,
minimal: true,
superminimal: true,
}
);
this.#setKey(
'visibility',
'calendar',
'GtkSwitch',
true,
{
default: true,
minimal: true,
superminimal: true,
}
);
this.#setKey(
'visibility',
'events-button',
'GtkSwitch',
true,
{
default: true,
minimal: true,
superminimal: true,
}
);
this.#setKey(
'visibility',
'window-menu-take-screenshot-button',
'GtkSwitch',
true,
{
default: true,
minimal: true,
superminimal: true,
}
);
this.#setKey(
'icons',
'panel-notification-icon',
'GtkSwitch',
true,
{
default: true,
minimal: true,
superminimal: true,
}
);
this.#setKey(
'icons',
'power-icon',
'GtkSwitch',
true,
{
default: true,
minimal: true,
superminimal: true,
}
);
this.#setKey(
'icons',
'window-picker-icon',
'GtkSwitch',
true,
{
default: true,
minimal: false,
superminimal: false,
}
);
this.#setKey(
'behavior',
'type-to-search',
'GtkSwitch',
true,
{
default: true,
minimal: true,
superminimal: true,
}
);
this.#setKey(
'behavior',
'window-demands-attention-focus',
'GtkSwitch',
true,
{
default: false,
minimal: true,
superminimal: true,
}
);
this.#setKey(
'behavior',
'window-maximized-on-create',
'GtkSwitch',
true,
{
default: false,
minimal: false,
superminimal: false,
}
);
this.#setKey(
'behavior',
'workspace-switcher-should-show',
'GtkSwitch',
true,
{
default: false,
minimal: false,
superminimal: false,
}
);
this.#setKey(
'behavior',
'startup-status',
'AdwActionRow',
true,
{
default: 1,
minimal: 0,
superminimal: 0,
}
);
this.#setKey(
'behavior',
'workspace-wrap-around',
'GtkSwitch',
true,
{
default: false,
minimal: false,
superminimal: false,
}
);
this.#setKey(
'behavior',
'workspace-peek',
'GtkSwitch',
true,
{
default: true,
minimal: true,
superminimal: true,
}
);
this.#setKey(
'behavior',
'overlay-key',
'GtkSwitch',
true,
{
default: true,
minimal: true,
superminimal: true,
}
);
this.#setKey(
'behavior',
'double-super-to-appgrid',
'GtkSwitch',
true,
{
default: true,
minimal: true,
superminimal: false,
}
);
this.#setKey(
'behavior',
'switcher-popup-delay',
'GtkSwitch',
true,
{
default: true,
minimal: true,
superminimal: true,
}
);
this.#setKey(
'customize',
'controls-manager-spacing-size',
'AdwActionRow',
true,
{
default: 0,
minimal: 0,
superminimal: 22,
}
);
this.#setKey(
'customize',
'workspace-background-corner-size',
'AdwActionRow',
true,
{
default: 0,
minimal: 0,
superminimal: 15,
}
);
this.#setKey(
'customize',
'top-panel-position',
'AdwActionRow',
true,
{
default: 0,
minimal: 0,
superminimal: 0,
}
);
this.#setKey(
'customize',
'clock-menu-position',
'AdwActionRow',
true,
{
default: 0,
minimal: 0,
superminimal: 0,
}
);
this.#setKey(
'customize',
'clock-menu-position-offset',
'AdwActionRow',
true,
{
default: 0,
minimal: 0,
superminimal: 0,
}
);
this.#setKey(
'customize',
'workspace-switcher-size',
'AdwActionRow',
true,
{
default: 0,
minimal: 0,
superminimal: 0,
}
);
this.#setKey(
'customize',
'animation',
'AdwActionRow',
true,
{
default: 1,
minimal: 1,
superminimal: 1,
}
);
this.#setKey(
'customize',
'dash-icon-size',
'AdwActionRow',
true,
{
default: 0,
minimal: 1,
superminimal: 0,
},
{
'1': 16,
'2': 22,
'3': 24,
'4': 32,
'5': 40,
'6': 48,
'7': 56,
'8': 64,
}
);
this.#setKey(
'customize',
'notification-banner-position',
'AdwActionRow',
true,
{
default: 1,
minimal: 1,
superminimal: 1,
}
);
this.#setKey(
'customize',
'panel-size',
'AdwActionRow',
true,
{
default: 0,
minimal: 0,
superminimal: 0,
}
);
this.#setKey(
'customize',
'panel-button-padding-size',
'AdwActionRow',
true,
{
default: 0,
minimal: 0,
superminimal: 0,
}
);
this.#setKey(
'customize',
'panel-indicator-padding-size',
'AdwActionRow',
true,
{
default: 0,
minimal: 0,
superminimal: 0,
}
);
this.#setKey(
'customize',
'panel-icon-size',
'AdwActionRow',
true,
{
default: 0,
minimal: 0,
superminimal: 0,
}
);
this.#setKey(
'customize',
'osd-position',
'AdwActionRow',
true,
{
default: 0,
minimal: 0,
superminimal: 0,
}
);
this.#setKey(
'customize',
'looking-glass-width',
'AdwActionRow',
true,
{
default: 0,
minimal: 0,
superminimal: 0,
}
);
this.#setKey(
'customize',
'looking-glass-height',
'AdwActionRow',
true,
{
default: 0,
minimal: 0,
superminimal: 0,
}
);
this.#setKey(
'customize',
'alt-tab-window-preview-size',
'AdwActionRow',
true,
{
default: 0,
minimal: 0,
superminimal: 0,
},
{
'0': 0,
'1': 32,
'2': 64,
'3': 128,
'4': 256,
'5': 512,
}
);
this.#setKey(
'customize',
'alt-tab-small-icon-size',
'AdwActionRow',
true,
{
default: 0,
minimal: 0,
superminimal: 0,
},
{
'0': 0,
'1': 32,
'2': 64,
'3': 128,
'4': 256,
'5': 512,
}
);
this.#setKey(
'customize',
'alt-tab-icon-size',
'AdwActionRow',
true,
{
default: 0,
minimal: 0,
superminimal: 0,
},
{
'0': 0,
'1': 32,
'2': 64,
'3': 128,
'4': 256,
'5': 512,
}
);
this.#setKey(
'override',
'theme',
'GtkSwitch',
true,
{
default: false,
minimal: true,
superminimal: true,
}
);
}
/**
* set key
*
* @param {string} category possible values:
* - visibility
* - icons
* - behavior
* - customize
* - override
* @param {string} name should be the same as gsettings key name
* @param {string} widgetType gtk widget type like 'GtkSwitch'.
* @param {boolean} supported whether supported in the current shell
* @param {Object} profiles values for each profile. for example:
* {default: true, minimal: false}
* @param {Object} [maps] for example for combobox you can specify
* if the index is 1 use 32 as value:
* {1 : 32}
*
* @returns {Object} key object that has been set
*/
#setKey(category, name, widgetType, supported, profiles, maps)
{
let id = name.replace(/-/g, '_');
let widgetName = widgetType.toLowerCase().replace('gtk', '');
let widgetId = (widgetType === 'AdwActionRow') ? `${id}_row` : `${id}_${widgetName}`;
if (maps === undefined) {
maps = {};
}
this.keys[id] = {
category,
widgetType,
name,
id,
widgetId,
supported,
profiles,
maps,
}
return this.keys[id];
}
/**
* delete key
*
* @param {string} id key id
*
* @returns {void}
*/
deleteKey(id)
{
delete(this.keys[id]);
}
};