Skip to content

Commit

Permalink
Custom icons reads from /var and /home; streamlined Adwaita-colors va…
Browse files Browse the repository at this point in the history
…riant pathing
  • Loading branch information
celiopy committed Nov 23, 2024
1 parent 592c0d6 commit 83d88a6
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 70 deletions.
83 changes: 37 additions & 46 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import GObject from 'gi://GObject';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import * as MessageTray from 'resource:///org/gnome/shell/ui/messageTray.js';
import { Extension } from 'resource:///org/gnome/shell/extensions/extension.js';
import { fetchLatestVersion } from './utils.js'; // Adjust path as needed
import { fetchLatestVersion, getVariant } from './utils.js'; // Adjust path as needed

const INTERFACE_SCHEMA = 'org.gnome.desktop.interface';
const ACCENT_COLOR = 'accent-color';
Expand Down Expand Up @@ -116,25 +116,12 @@ export default class AccentColorExtension extends Extension {

async _setIconTheme(color) {
if (color) {
let iconTheme = color === 'blue' ? 'Adwaita' : `Adwaita-${color}`;

const possiblePaths = [
'/var/usrlocal/share/icons',
'/usr/share/icons',
GLib.get_home_dir() + '/.local/share/icons'
];

let themeFound = false;
for (const path of possiblePaths) {
const iconThemePath = GLib.build_filenamev([path, iconTheme]);
if (GLib.file_test(iconThemePath, GLib.FileTest.EXISTS)) {
themeFound = true;
break;
}
}
let iconTheme = `Adwaita-${color}`;

const { found } = getVariant(iconTheme);

if (themeFound) {
await this._loopAndUpdate(color);
if (found) {
await this._loopAndUpdate(iconTheme);
this.settingsSchema.set_string(ICON_THEME, iconTheme);
} else {
this.settingsSchema.set_string(ICON_THEME, 'Adwaita');
Expand All @@ -147,46 +134,50 @@ export default class AccentColorExtension extends Extension {
}
}

async _loopAndUpdate(color) {
async _loopAndUpdate(iconTheme) {
let directories = [GLib.get_home_dir(), '/var'];
let file = Gio.File.new_for_path(GLib.get_home_dir());

// Get the contents of the directory
let enumerator = file.enumerate_children('standard::*,metadata::*',
Gio.FileQueryInfoFlags.NONE,
null);
let info;

// Initialize counters
let fileCount = 0;
let dirCount = 0;

// Iterate through the contents
while ((info = await enumerator.next_file(null)) !== null) {
await this._updateIcon(info, color);

// Count files and directories
if (info.get_file_type() === Gio.FileType.DIRECTORY) {
dirCount++;
// Recursively process directories if needed
} else {
fileCount++;
for (let dirPath of directories) {
// Initialize counters
let fileCount = 0;
let dirCount = 0;

// Get the contents of the directory
let enumerator = file.enumerate_children('standard::*,metadata::*',
Gio.FileQueryInfoFlags.NONE,
null);
let info;


// Iterate through the contents
while ((info = await enumerator.next_file(null)) !== null) {
await this._updateIcon(info, iconTheme, dirPath);

// Count files and directories
if (info.get_file_type() === Gio.FileType.DIRECTORY) {
dirCount++;
// Recursively process directories if needed
} else {
fileCount++;
}
}
}

// Log the counts
console.log(`Files: ${fileCount}, Directories: ${dirCount}`);
// Log the counts
console.log(`Files: ${fileCount}, Directories: ${dirCount}`);
}
}

async _updateIcon(fileInfo, color) {
let file = Gio.File.new_for_path(GLib.get_home_dir());
async _updateIcon(fileInfo, iconTheme, parentDir) {
let file = Gio.File.new_for_path(parentDir);
let childFile = file.get_child(fileInfo.get_name());

try {
let iconValue = fileInfo.get_attribute_string('metadata::custom-icon');

const regex = /Adwaita-(\w+)(?=\/)/;
if (regex.test(iconValue)) {
let newIconValue = iconValue.replace(regex, `Adwaita-${color}`);
let newIconValue = iconValue.replace(regex, iconTheme);
childFile.set_attribute_string(
'metadata::custom-icon',
newIconValue,
Expand Down
29 changes: 6 additions & 23 deletions prefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Gtk from 'gi://Gtk';
import Adw from 'gi://Adw';
import GLib from 'gi://GLib';
import { ExtensionPreferences, gettext as _ } from 'resource:///org/gnome/Shell/Extensions/js/extensions/prefs.js';
import { fetchLatestVersion, downloadZip } from './utils.js'; // Adjust path as needed
import { fetchLatestVersion, downloadZip, getVariant } from './utils.js'; // Adjust path as needed

export default class AccentColorExtensionPrefs extends ExtensionPreferences {
fillPreferencesWindow(window) {
Expand Down Expand Up @@ -79,6 +79,10 @@ export default class AccentColorExtensionPrefs extends ExtensionPreferences {
});

group.add(notificationRow);

const { found, state } = getVariant();
window._settings.set_boolean('notify-about-releases', state === 'user');
downloadButton.set_sensitive(state === 'user');
}

async handleDownload(window, versionLabel, progressBar) {
Expand All @@ -87,8 +91,7 @@ export default class AccentColorExtensionPrefs extends ExtensionPreferences {
const tempZipFile = GLib.get_tmp_dir() + '/adwaita-colors.zip';

// Use metadata.path to get the extension's directory dynamically
const extensionRoot = this.path; // This points to the extension's root directory
const scriptPath = GLib.build_filenamev([extensionRoot, 'install_adwaita_colors.sh']); // Construct the full path to the script
const scriptPath = GLib.build_filenamev([this.path, 'install_adwaita_colors.sh']); // Construct the full path to the script

try {
// Show progress bar and initialize
Expand Down Expand Up @@ -133,11 +136,6 @@ export default class AccentColorExtensionPrefs extends ExtensionPreferences {
versionLabel.set_label(this.getCurrentVersionLabel(window));
}
});
if (status === 0) {
} else {
// progressBar.set_text(_("Failed."));
// logError(new Error("Shell script failed."), 'Download/Extraction failed');
}

// Restore the original accent color after the process is finished
window._settings.set_string('accent-color', savedAccentColor);
Expand Down Expand Up @@ -168,20 +166,5 @@ export default class AccentColorExtensionPrefs extends ExtensionPreferences {
const currentVersion = window._settings.get_string('current-version') || 'NA';
return `<b>Current Version:</b> ${currentVersion}`;
}

showErrorMessage(message) {
const dialog = new Gtk.MessageDialog({
modal: true,
message_type: Gtk.MessageType.ERROR,
buttons: Gtk.ButtonsType.OK,
text: message,
});
dialog.run();
dialog.destroy();
}

showSuccessMessage(message) {
console.log(message); // Replace with your UI message logic
}
}

34 changes: 33 additions & 1 deletion utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Gio from 'gi://Gio';
import GLib from 'gi://GLib';

Gio._promisify(Gio.Subprocess.prototype, 'communicate_utf8_async');

Expand Down Expand Up @@ -85,4 +86,35 @@ async function downloadZip(url, destPath) {
});
}

export { fetchLatestVersion, downloadZip };
// A single function that returns all relevant information for a theme
function getVariant(variant = 'Adwaita-blue') {
const possiblePaths = [
'/var/usrlocal/share/icons',
'/usr/share/icons',
GLib.get_home_dir() + '/.local/share/icons'
];

for (const path of possiblePaths) {
const iconThemePath = GLib.build_filenamev([path, variant]);
if (GLib.file_test(iconThemePath, GLib.FileTest.EXISTS)) {
return {
found: true,
path: iconThemePath,
state: getInstallState(path) // Either 'root' or 'user'
};
}
}

// If not found, return a default value indicating it doesn't exist
return { found: false, path: null, state: null };
}

// Function to get the install state (root or user)
function getInstallState(path) {
if (path.startsWith('/var') || path.startsWith('/usr')) {
return 'root';
}
return 'user';
}

export { fetchLatestVersion, downloadZip, getVariant};

0 comments on commit 83d88a6

Please sign in to comment.