Get file explorer icon based on file type.

Fixes #15
This commit is contained in:
2026-02-07 18:01:31 -05:00
parent 6f2a655497
commit aa42ec6072
6 changed files with 44 additions and 19 deletions

View File

@@ -3,6 +3,7 @@
// SPDX-License-Identifier: GNU General Public License v3.0 or later
use cxx_qt_lib::{QModelIndex, QString};
use devicons::FileIcon;
use dirs;
use log::warn;
use std::fs;
@@ -52,6 +53,9 @@ pub mod qobject {
#[qinvokable]
#[cxx_name = "setDirectory"]
fn set_directory(self: Pin<&mut FileSystem>, path: &QString) -> QModelIndex;
#[qinvokable]
fn icon(self: Pin<&mut FileSystem>, path: &QString) -> QString;
}
}
@@ -139,4 +143,15 @@ impl qobject::FileSystem {
self.set_root_path(&QString::from(homedir))
}
}
fn icon(self: std::pin::Pin<&mut Self>, path: &QString) -> QString {
let str = path.to_string();
if Path::new(&str).is_dir() {
// Ensures directories are given a folder icon and not mistakenly resolved to a language.
// For example, a directory named `cpp` would otherwise return a C++ icon.
return QString::from(FileIcon::from("dir/").to_string())
}
let icon = FileIcon::from(str);
QString::from(icon.to_string())
}
}