Add FileSystem Rust module.
This commit is contained in:
parent
4f3aebe64f
commit
1546eb1028
@ -5,10 +5,16 @@ import QtQuick.Layouts
|
||||
import clide.module 1.0
|
||||
|
||||
SplitView {
|
||||
id: root
|
||||
Layout.fillHeight: true
|
||||
Layout.fillWidth: true
|
||||
orientation: Qt.Vertical
|
||||
|
||||
// The path to the file to show in the text editor.
|
||||
// This is updated by a signal caught within ClideProjectView.
|
||||
// Initialized by the Default trait for the Rust QML singleton FileSystem.
|
||||
required property string filePath;
|
||||
|
||||
// Customized handle to drag between the Editor and the Console.
|
||||
handle: Rectangle {
|
||||
border.color: SplitHandle.pressed ? RustColors.pressed : SplitHandle.hovered ? RustColors.hovered : RustColors.gutter
|
||||
@ -102,6 +108,7 @@ SplitView {
|
||||
// selectionColor: RustColors.editor_highlight
|
||||
textFormat: Qt.AutoText
|
||||
wrapMode: TextArea.Wrap
|
||||
text: FileSystem.readFile(root.filePath)
|
||||
|
||||
background: Rectangle {
|
||||
color: RustColors.editor_background
|
||||
|
||||
@ -51,5 +51,7 @@ SplitView {
|
||||
}
|
||||
}
|
||||
ClideEditor {
|
||||
// Initialize using the Default trait in Rust QML singleton FileSystem.
|
||||
filePath: FileSystem.filePath
|
||||
}
|
||||
}
|
||||
|
||||
53
src/filesystem.rs
Normal file
53
src/filesystem.rs
Normal file
@ -0,0 +1,53 @@
|
||||
#[cxx_qt::bridge]
|
||||
pub mod qobject {
|
||||
unsafe extern "C++" {
|
||||
// Import Qt Types from C++
|
||||
include!("cxx-qt-lib/qstring.h");
|
||||
type QString = cxx_qt_lib::QString;
|
||||
include!("cxx-qt-lib/qmodelindex.h");
|
||||
type QModelIndex = cxx_qt_lib::QModelIndex;
|
||||
}
|
||||
|
||||
unsafe extern "RustQt" {
|
||||
// Export QML Types from Rust
|
||||
#[qobject]
|
||||
#[qml_element]
|
||||
#[qml_singleton]
|
||||
#[qproperty(QString, file_path, cxx_name = "filePath")]
|
||||
type FileSystem = super::FileSystemImpl;
|
||||
|
||||
#[qinvokable]
|
||||
#[cxx_name = "readFile"]
|
||||
fn read_file(self: &FileSystem, path: &QString) -> QString;
|
||||
}
|
||||
}
|
||||
|
||||
use cxx_qt_lib::{QModelIndex, QString};
|
||||
use std::fs;
|
||||
pub struct FileSystemImpl {
|
||||
file_path: QString,
|
||||
model_index: QModelIndex,
|
||||
}
|
||||
|
||||
// Default is explicit to make the editor open this source file initially.
|
||||
impl Default for FileSystemImpl {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
file_path: QString::from(file!()),
|
||||
model_index: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl qobject::FileSystem {
|
||||
fn read_file(&self, path: &QString) -> QString {
|
||||
if path.is_empty() {
|
||||
return QString::default();
|
||||
}
|
||||
// TODO: What if the file is binary or something horrible?
|
||||
QString::from(
|
||||
fs::read_to_string(path.to_string())
|
||||
.expect(format!("Failed to read file {}", path).as_str()),
|
||||
)
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user