@@ -71,75 +71,94 @@ MainWindow::~MainWindow()
7171
7272QIcon MainWindow::findIcon (const QString &icon_name)
7373{
74- static QIcon defaultIcon;
75- static bool defaultIconLoaded = false ;
74+ static const QRegularExpression re (R"( \.(png|svg|xpm)$)" );
75+ static const QStringList extensions {" .png" , " .svg" , " .xpm" };
76+ static const QStringList searchPaths {
77+ QDir::homePath () + " /.local/share/icons/" ,
78+ " /usr/share/pixmaps/" ,
79+ " /usr/local/share/icons/" ,
80+ " /usr/share/icons/" ,
81+ " /usr/share/icons/hicolor/scalable/apps/" ,
82+ " /usr/share/icons/hicolor/48x48/apps/" ,
83+ " /usr/share/icons/Adwaita/48x48/legacy/"
84+ };
85+
86+ // Initialize default icon once
87+ static const QIcon defaultIcon = []() {
88+ // First try themed icon
89+ QIcon icon = QIcon::fromTheme (" utilities-terminal" );
90+ if (!icon.isNull ()) {
91+ return icon;
92+ }
7693
77- if (icon_name.isEmpty ()) {
78- if (!defaultIconLoaded) {
79- defaultIcon = findIcon (" utilities-terminal" );
80- defaultIconLoaded = true ;
94+ // Search in paths with extensions
95+ for (const auto &path : searchPaths) {
96+ for (const auto &ext : extensions) {
97+ QString fullPath = path + " utilities-terminal" + ext;
98+ if (QFile::exists (fullPath)) {
99+ icon = QIcon (fullPath);
100+ if (!icon.isNull ()) {
101+ return icon;
102+ }
103+ }
104+ }
81105 }
106+ return QIcon ();
107+ }();
108+
109+ // Return default for empty or default icon name
110+ if (icon_name.isEmpty () || icon_name == " utilities-terminal" ) {
82111 return defaultIcon;
83112 }
84113
85- // Check if the icon name is an absolute path and exists
114+ // Use absolute path directly if it exists
86115 if (QFileInfo (icon_name).isAbsolute () && QFile::exists (icon_name)) {
87116 return QIcon (icon_name);
88117 }
89118
90- // Prepare regular expression to strip extension
91- static const QRegularExpression re (R"( \.(png|svg|xpm)$)" );
92- QString name_noext = icon_name;
93- name_noext.remove (re);
119+ // Try themed icon first
120+ QString nameNoExt = icon_name;
121+ nameNoExt.remove (re);
94122
95- // Set icon theme if specified
96123 if (!icon_theme.isEmpty ()) {
97124 QIcon::setThemeName (icon_theme);
98125 }
99126
100- // Return the themed icon if available
101- QIcon themedIcon = QIcon::fromTheme (name_noext);
127+ QIcon themedIcon = QIcon::fromTheme (nameNoExt);
102128 if (!themedIcon.isNull ()) {
103129 return themedIcon;
104130 }
105131
106- // Define common search paths for icons
107- QStringList search_paths {QDir::homePath () + " /.local/share/icons/" ,
108- " /usr/share/pixmaps/" ,
109- " /usr/local/share/icons/" ,
110- " /usr/share/icons/" ,
111- " /usr/share/icons/hicolor/scalable/apps/" ,
112- " /usr/share/icons/hicolor/48x48/apps/" ,
113- " /usr/share/icons/Adwaita/48x48/legacy/" };
114-
115- // Optimization: search first for the full icon_name with the specified extension
116- auto it = std::find_if (search_paths.cbegin (), search_paths.cend (),
117- [&](const QString &path) { return QFile::exists (path + icon_name); });
118- if (it != search_paths.cend ()) {
119- return QIcon (*it + icon_name);
120- }
121-
122- // Search for the icon without extension in the specified paths
123- for (const QString &path : search_paths) {
124- for (const QString &ext : {" .png" , " .svg" , " .xpm" }) {
125- QString file = path + name_noext + ext;
126- if (QFile::exists (file)) {
127- return QIcon (file);
132+ // Search in all paths
133+ const auto searchInPaths = [&](const QString &name) -> QIcon {
134+ for (const auto &path : searchPaths) {
135+ QString fullPath = path + name;
136+ if (QFile::exists (fullPath)) {
137+ QIcon icon (fullPath);
138+ if (!icon.isNull ()) {
139+ return icon;
140+ }
128141 }
129142 }
143+ return QIcon ();
144+ };
145+
146+ // Try original name first
147+ QIcon icon = searchInPaths (icon_name);
148+ if (!icon.isNull ()) {
149+ return icon;
130150 }
131151
132- // If the icon is "utilities-terminal" and not found, return the default icon if it's already loaded
133- if (icon_name == " utilities-terminal " ) {
134- if (!defaultIconLoaded) {
135- defaultIcon = QIcon ();
136- defaultIconLoaded = true ;
152+ // Try with each extension
153+ for ( const auto &ext : extensions ) {
154+ icon = searchInPaths (nameNoExt + ext);
155+ if (!icon. isNull ()) {
156+ return icon ;
137157 }
138- return defaultIcon;
139158 }
140159
141- // If the icon is not "utilities-terminal", try to load the default icon
142- return findIcon ( " utilities-terminal " ) ;
160+ // Fall back to the default icon if nothing else was found.
161+ return defaultIcon ;
143162}
144163
145164// Strip %f, %F, %U, etc. if exec expects a file name since it's called without an argument from this launcher.
0 commit comments