From 5ba880bc7e1a2267997e111638b8b5f205718eab Mon Sep 17 00:00:00 2001 From: Shaun Reed Date: Sat, 19 Apr 2025 12:46:22 -0400 Subject: [PATCH] WIP --- qml/main.qml | 1 - src/gui.rs | 13 ++++++++++++- src/gui/filesystem.rs | 34 +++++++++++++++++++++++++--------- 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/qml/main.qml b/qml/main.qml index cba1e07..5e9c7aa 100644 --- a/qml/main.qml +++ b/qml/main.qml @@ -29,4 +29,3 @@ ApplicationWindow { ClideProjectView { } } - diff --git a/src/gui.rs b/src/gui.rs index 62c6172..477cdb5 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -1,4 +1,6 @@ -use cxx_qt_lib::QString; +use crate::gui::filesystem::FS_STATE; +use crate::gui::filesystem::qobject::FileSystem; +use cxx_qt_lib::{QModelIndex, QQmlEngine, QString}; use std::error::Error; pub mod colors; @@ -12,6 +14,15 @@ pub fn run(root_path: std::path::PathBuf) -> Result<(), Box> { let mut app = QGuiApplication::new(); let mut engine = QQmlApplicationEngine::new(); + // let clide_fs = FileSystemImpl { + // file_path: QString::from(root_path.to_str().unwrap()), + // root_index: QModelIndex::default(), + // }; + unsafe { + let mut fs_state = FS_STATE.take_fs(); + fs_state.file_path = QString::from(root_path.to_str().unwrap()); + }; + if let Some(engine) = engine.as_mut() { engine.add_import_path(&QString::from("qml/")); } diff --git a/src/gui/filesystem.rs b/src/gui/filesystem.rs index 2c39d7f..ae7119d 100644 --- a/src/gui/filesystem.rs +++ b/src/gui/filesystem.rs @@ -10,32 +10,33 @@ pub mod qobject { type QFileSystemModel; } + #[auto_cxx_name] + #[auto_rust_name] 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")] + #[qproperty(QString, file_path] + #[qproperty(QModelIndex, root_index] 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"] fn column_count(self: &FileSystem, _index: &QModelIndex) -> i32; #[qinvokable] - #[cxx_name = "readFile"] fn read_file(self: &FileSystem, path: &QString) -> QString; #[qinvokable] - #[cxx_name = "setDirectory"] fn set_directory(self: Pin<&mut FileSystem>, path: &QString) -> QModelIndex; + + #[qinvokable] + fn take_fs(self: &mut) -> FileSystem; } } @@ -44,6 +45,7 @@ use dirs; use log::warn; use std::fs; use std::io::BufRead; +use std::ptr::replace; use syntect::easy::HighlightFile; use syntect::highlighting::ThemeSet; use syntect::html::{ @@ -51,10 +53,24 @@ use syntect::html::{ }; use syntect::parsing::SyntaxSet; -// TODO: Impleent a provider for QFileSystemModel::setIconProvider for icons. +/// Singleton holding filesystem state. +pub struct FileSystemState { + pub fs: Option, +} + +impl FileSystemState { + pub unsafe fn take_fs(&mut self) -> FileSystemImpl { + let fs = replace(&mut self.fs, None); + fs.unwrap() + } +} + +pub static mut FS_STATE : FileSystemState = FileSystemState { fs: Some(FileSystemImpl::default()) }; + +// TODO: Implement a provider for QFileSystemModel::setIconProvider for icons. pub struct FileSystemImpl { - file_path: QString, - root_index: QModelIndex, + pub file_path: QString, + pub root_index: QModelIndex, } // Default is explicit to make the editor open this source file initially.