Skip to content

Commit

Permalink
LibGUI: Make GUI::FileIconProvider::icon_for_executable() a public API
Browse files Browse the repository at this point in the history
This way we can use it instead of icon_for_path() in a bunch of places
and avoid loading the full FileIconProvider configuration.
  • Loading branch information
awesomekling committed Dec 28, 2020
1 parent a49fd0f commit ffa2412
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Applications/FileManager/DirectoryView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ namespace FileManager {

NonnullRefPtr<GUI::Action> LauncherHandler::create_launch_action(Function<void(const LauncherHandler&)> launch_handler)
{
auto icon = GUI::FileIconProvider::icon_for_path(details().executable).bitmap_for_size(16);
auto icon = GUI::FileIconProvider::icon_for_executable(details().executable).bitmap_for_size(16);
return GUI::Action::create(details().name, move(icon), [this, launch_handler = move(launch_handler)](auto&) {
launch_handler(*this);
});
Expand Down
2 changes: 1 addition & 1 deletion Applications/SystemMonitor/ProcessModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ GUI::Variant ProcessModel::data(const GUI::ModelIndex& index, GUI::ModelRole rol
if (role == GUI::ModelRole::Display) {
switch (index.column()) {
case Column::Icon: {
auto icon = GUI::FileIconProvider::icon_for_path(thread.current_state.executable);
auto icon = GUI::FileIconProvider::icon_for_executable(thread.current_state.executable);
if (auto* bitmap = icon.bitmap_for_size(16))
return *bitmap;
return *m_generic_process_icon;
Expand Down
17 changes: 15 additions & 2 deletions Libraries/LibGUI/FileIconProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ static RefPtr<Gfx::Bitmap> s_symlink_emblem_small;
static HashMap<String, Icon> s_filetype_icons;
static HashMap<String, Vector<String>> s_filetype_patterns;

static void initialize_executable_icon_if_needed()
{
static bool initialized = false;
if (initialized)
return;
initialized = true;
s_executable_icon = Icon::default_icon("filetype-executable");
}

static void initialize_if_needed()
{
static bool s_initialized = false;
Expand All @@ -78,9 +87,11 @@ static void initialize_if_needed()
s_file_icon = Icon::default_icon("filetype-unknown");
s_symlink_icon = Icon::default_icon("filetype-symlink");
s_socket_icon = Icon::default_icon("filetype-socket");
s_executable_icon = Icon::default_icon("filetype-executable");

s_filetype_image_icon = Icon::default_icon("filetype-image");

initialize_executable_icon_if_needed();

for (auto& filetype : config->keys("Icons")) {
s_filetype_icons.set(filetype, Icon::default_icon(String::formatted("filetype-{}", filetype)));
s_filetype_patterns.set(filetype, config->read_entry("Icons", filetype).split(','));
Expand Down Expand Up @@ -127,13 +138,15 @@ Icon FileIconProvider::icon_for_path(const String& path)
return icon_for_path(path, stat.st_mode);
}

static Icon icon_for_executable(const String& path)
Icon FileIconProvider::icon_for_executable(const String& path)
{
static HashMap<String, Icon> app_icon_cache;

if (auto it = app_icon_cache.find(path); it != app_icon_cache.end())
return it->value;

initialize_executable_icon_if_needed();

// If the icon for an app isn't in the cache we attempt to load the file as an ELF image and extract
// the serenity_app_icon_* sections which should contain the icons as raw PNG data. In the future it would
// be better if the binary signalled the image format being used or we deduced it, e.g. using magic bytes.
Expand Down
1 change: 1 addition & 0 deletions Libraries/LibGUI/FileIconProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class FileIconProvider {
public:
static Icon icon_for_path(const String&, mode_t);
static Icon icon_for_path(const String&);
static Icon icon_for_executable(const String&);

static Icon filetype_image_icon();
static Icon directory_icon();
Expand Down
2 changes: 1 addition & 1 deletion Libraries/LibGUI/RunningProcessesModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void RunningProcessesModel::update()
Process process;
process.pid = it.value.pid;
process.uid = it.value.uid;
process.icon = FileIconProvider::icon_for_path(it.value.executable).bitmap_for_size(16);
process.icon = FileIconProvider::icon_for_executable(it.value.executable).bitmap_for_size(16);
process.name = it.value.name;
m_processes.append(move(process));
}
Expand Down
2 changes: 1 addition & 1 deletion Services/SystemMenu/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ NonnullRefPtr<GUI::Menu> build_system_menu()
// Then we create and insert all the app menu items into the right place.
int app_identifier = 0;
for (const auto& app : g_apps) {
auto icon = GUI::FileIconProvider::icon_for_path(app.executable).bitmap_for_size(16);
auto icon = GUI::FileIconProvider::icon_for_executable(app.executable).bitmap_for_size(16);

#ifdef SYSTEM_MENU_DEBUG
if (icon)
Expand Down

0 comments on commit ffa2412

Please sign in to comment.