Add basic FileSystem view.

This commit is contained in:
2025-03-30 21:38:57 -04:00
parent b62dce631f
commit bdf942371c
8 changed files with 80 additions and 64 deletions

View File

@@ -25,6 +25,9 @@ pub mod qobject {
#[qproperty(QColor, editor_highlighted_text)]
#[qproperty(QColor, editor_highlight)]
#[qproperty(QColor, gutter)]
#[qproperty(QColor, explorer_hovered)]
#[qproperty(QColor, explorer_text)]
#[qproperty(QColor, explorer_text_selected)]
#[qproperty(QColor, explorer_background)]
#[qproperty(QColor, explorer_folder)]
#[qproperty(QColor, explorer_folder_open)]
@@ -51,6 +54,9 @@ pub struct RustColorsImpl {
editor_highlighted_text: QColor,
editor_highlight: QColor,
gutter: QColor,
explorer_hovered: QColor,
explorer_text: QColor,
explorer_text_selected: QColor,
explorer_background: QColor,
explorer_folder: QColor,
explorer_folder_open: QColor,
@@ -75,7 +81,10 @@ impl Default for RustColorsImpl {
editor_highlighted_text: QColor::try_from("#ccced3").unwrap(),
editor_highlight: QColor::try_from("#ccced3").unwrap(),
gutter: QColor::try_from("#1e1f22").unwrap(),
explorer_background: QColor::try_from("#3c3f41").unwrap(),
explorer_hovered: QColor::try_from("#4c5053").unwrap(),
explorer_text: QColor::try_from("#3b3b3b").unwrap(),
explorer_text_selected: QColor::try_from("#8b8b8b").unwrap(),
explorer_background: QColor::try_from("#676c70").unwrap(),
explorer_folder: QColor::try_from("#FFF").unwrap(),
explorer_folder_open: QColor::try_from("#FFF").unwrap(),
}

View File

@@ -6,29 +6,36 @@ pub mod qobject {
type QString = cxx_qt_lib::QString;
include!("cxx-qt-lib/qmodelindex.h");
type QModelIndex = cxx_qt_lib::QModelIndex;
include!(<QtGui/QFileSystemModel>);
type QFileSystemModel;
}
unsafe extern "RustQt" {
// Export QML Types from Rust
#[qobject]
#[base = QFileSystemModel]
#[qml_element]
#[qml_singleton]
#[qproperty(QString, file_path, cxx_name = "filePath")]
#[qproperty(QModelIndex, root_index, cxx_name = "rootIndex")]
type FileSystem = super::FileSystemImpl;
#[inherit]
#[cxx_name = "setRootPath"]
fn set_root_path(self: Pin<&mut FileSystem>, path: &QString) -> QModelIndex;
#[qinvokable]
#[cxx_override]
#[cxx_name = "columnCount"]
pub fn column_count(self: &FileSystem, index: &QModelIndex) -> i32;
fn column_count(self: &FileSystem, _index: &QModelIndex) -> i32;
#[qinvokable]
#[cxx_name = "readFile"]
fn read_file(self: &FileSystem, path: &QString) -> QString;
// TODO: Remove if unused in QML.
#[qinvokable]
#[cxx_name = "setInitialDirectory"]
fn set_initial_directory(self: &FileSystem, path: &QString);
#[cxx_name = "setDirectory"]
fn set_directory(self: Pin<&mut FileSystem>, path: &QString) -> QModelIndex;
}
}
@@ -36,6 +43,7 @@ use cxx_qt_lib::{QModelIndex, QString};
use dirs;
use std::fs;
// TODO: Impleent a provider for QFileSystemModel::setIconProvider for icons.
pub struct FileSystemImpl {
file_path: QString,
root_index: QModelIndex,
@@ -64,21 +72,27 @@ impl qobject::FileSystem {
}
// There will never be more than one column.
pub fn column_count(&self, _index: &QModelIndex) -> i32 {
fn column_count(&self, _index: &QModelIndex) -> i32 {
1
}
fn set_initial_directory(&self, path: &QString) {
fn set_directory(self: std::pin::Pin<&mut Self>, path: &QString) -> QModelIndex {
if !path.is_empty()
&& fs::metadata(path.to_string())
.expect(format!("Failed to get metadata for file {}", path).as_str())
.is_file()
.expect(format!("Failed to get metadata for path {}", path).as_str())
.is_dir()
{
// Open the file
// setRootPa
self.set_root_path(path)
} else {
// If the initial directory can't be opened, attempt to find the home directory.
// dirs::home_dir()
self.set_root_path(&QString::from(
dirs::home_dir()
.expect("Failed to get home directory")
.as_path()
.to_str()
.unwrap()
.to_string(),
))
}
}
}