diff --git a/build.rs b/build.rs index 83e59a6..5328c17 100644 --- a/build.rs +++ b/build.rs @@ -10,7 +10,7 @@ fn main() { .qt_module("Network") .qml_module(QmlModule { uri: "clide.module", - rust_files: &["src/main.rs"], + rust_files: &["src/line_count.rs"], qml_files: &["qml/main.qml", "qml/ProjectView/ClideProjectView.qml", "qml/Editor/ClideEditor.qml", diff --git a/qml/Editor/ClideEditor.qml b/qml/Editor/ClideEditor.qml index c91bd04..a527508 100644 --- a/qml/Editor/ClideEditor.qml +++ b/qml/Editor/ClideEditor.qml @@ -22,7 +22,6 @@ SplitView { } } } - RowLayout { // We use a flickable to synchronize the position of the editor and // the line numbers. This is necessary because the line numbers can @@ -41,14 +40,16 @@ SplitView { Column { anchors.fill: parent + topPadding: 6 Repeater { id: repeatedLineNumbers + // Each line number in the gutter. delegate: Item { required property int index - height: Math.ceil(fontMetrics.lineSpacing) + height: 17 width: parent.width Label { @@ -71,12 +72,7 @@ SplitView { width: 1 } } - model: LineCount { - id: lineCountModel - - // This count sets the max line numbers shown in the gutter. - count: areaText.lineCount - } + model: areaText.lineCount } } } @@ -107,10 +103,6 @@ SplitView { textFormat: Qt.AutoText wrapMode: TextArea.Wrap - onTextChanged: { - console.log("Updated line count: " + areaText.lineCount) - } - background: Rectangle { color: "#2b2b2b" } diff --git a/src/line_count.rs b/src/line_count.rs new file mode 100644 index 0000000..a66834e --- /dev/null +++ b/src/line_count.rs @@ -0,0 +1,103 @@ +// TODO: Header + +#[cxx_qt::bridge] +pub mod qobject { + unsafe extern "C++" { + include!("cxx-qt-lib/qvariant.h"); + type QVariant = cxx_qt_lib::QVariant; + include!(); + type QModelIndex = cxx_qt_lib::QModelIndex; + type QAbstractListModel; + } + + unsafe extern "RustQt" { + #[qobject] + #[base = QAbstractListModel] + type AbstractListModel = super::AbstractListModelRust; + + #[qobject] + #[base = AbstractListModel] + #[qml_element] + #[qproperty(i32, count)] + type LineCount = super::LineCountRust; + } + + unsafe extern "RustQt" { + #[cxx_name = "beginInsertRows"] + #[inherit] + fn beginInsertRows(self: Pin<&mut LineCount>, parent: &QModelIndex, first: i32, last: i32); + + #[cxx_name = "endInsertRows"] + #[inherit] + fn endInsertRows(self: Pin<&mut LineCount>); + + #[cxx_name = "beginRemoveRows"] + #[inherit] + fn beginRemoveRows(self: Pin<&mut LineCount>, parent: &QModelIndex, first: i32, last: i32); + + #[cxx_name = "endRemoveRows"] + #[inherit] + fn endRemoveRows(self: Pin<&mut LineCount>); + + #[qinvokable] + fn set_line_count(self: Pin<&mut LineCount>, line_count: i32); + + #[qinvokable] + #[cxx_override] + fn data(self: &LineCount, index: &QModelIndex, role: i32) -> QVariant; + + #[qinvokable] + #[cxx_override] + #[cxx_name = "rowCount"] + fn row_count(self: &LineCount, _parent: &QModelIndex) -> i32; + } +} + +use cxx_qt::CxxQtType; +use cxx_qt_lib::{QModelIndex, QVariant}; + +impl qobject::LineCount { + pub fn set_line_count(mut self: std::pin::Pin<&mut Self>, line_count: i32) { + let current_count = self.as_mut().rust_mut().count; + if line_count < 0 || current_count == line_count { + log::warn!( + "Can't set line count: {}; Current count: {}", + line_count, + current_count + ); + return; + } + if current_count < line_count { + self.as_mut() + .beginInsertRows(&QModelIndex::default(), current_count, line_count - 1); + self.as_mut().endInsertRows(); + } else if current_count > line_count { + self.as_mut() + .beginRemoveRows(&QModelIndex::default(), line_count, current_count - 1); + self.as_mut().endRemoveRows(); + } + self.as_mut().rust_mut().count = line_count; + log::warn!( + "Line count changed from {} to {}", + current_count, + line_count + ); + } + + pub fn row_count(self: &Self, _parent: &QModelIndex) -> i32 { + *self.count() + } + + pub fn data(self: &Self, _index: &QModelIndex, _role: i32) -> QVariant { + QVariant::default() + } +} + +/// A struct which inherits from QAbstractListModel +#[derive(Default)] +pub struct AbstractListModelRust {} + +#[derive(Default)] +pub struct LineCountRust { + pub count: i32, +} diff --git a/src/main.rs b/src/main.rs index 4239053..cdbdc57 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,104 +1,6 @@ // TODO: Header -#[cxx_qt::bridge] -pub mod qobject { - unsafe extern "C++" { - include!("cxx-qt-lib/qvariant.h"); - type QVariant = cxx_qt_lib::QVariant; - include!(); - type QModelIndex = cxx_qt_lib::QModelIndex; - type QAbstractListModel; - } - - unsafe extern "RustQt" { - #[qobject] - #[base = QAbstractListModel] - type AbstractListModel = super::AbstractListModelRust; - - #[qobject] - #[base = AbstractListModel] - #[qml_element] - #[qproperty(i32, count)] - type LineCount = super::LineCountRust; - - #[cxx_name = "beginInsertRows"] - #[inherit] - fn beginInsertRows(self: Pin<&mut LineCount>, parent: &QModelIndex, first: i32, last: i32); - - #[cxx_name = "endInsertRows"] - #[inherit] - fn endInsertRows(self: Pin<&mut LineCount>); - - #[cxx_name = "beginRemoveRows"] - #[inherit] - fn beginRemoveRows(self: Pin<&mut LineCount>, parent: &QModelIndex, first: i32, last: i32); - - #[cxx_name = "endRemoveRows"] - #[inherit] - fn endRemoveRows(self: Pin<&mut LineCount>); - - #[qinvokable] - pub fn set_line_count(self: Pin<&mut LineCount>, line_count: i32); - - #[qinvokable] - #[cxx_override] - fn data(self: &LineCount, index: &QModelIndex, role: i32) -> QVariant; - - #[qinvokable] - #[cxx_override] - #[cxx_name = "rowCount"] - fn row_count(self: &LineCount, _parent: &QModelIndex) -> i32; - } -} - -use cxx_qt::CxxQtType; -use cxx_qt_lib::{QModelIndex, QVariant}; - -impl qobject::LineCount { - pub fn set_line_count(mut self: std::pin::Pin<&mut Self>, line_count: i32) { - let current_count = self.as_mut().rust_mut().count; - if line_count < 0 || current_count == line_count { - log::warn!( - "Can't set line count: {}; Current count: {}", - line_count, - current_count - ); - return; - } - if current_count < line_count { - self.as_mut() - .beginInsertRows(&QModelIndex::default(), current_count, line_count - 1); - self.as_mut().endInsertRows(); - } else if current_count > line_count { - self.as_mut() - .beginRemoveRows(&QModelIndex::default(), line_count, current_count - 1); - self.as_mut().endRemoveRows(); - } - self.as_mut().rust_mut().count = line_count; - log::warn!( - "Line count changed from {} to {}", - current_count, - line_count - ); - } - - pub fn row_count(self: &Self, _parent: &QModelIndex) -> i32 { - *self.count() - } - - pub fn data(self: &Self, _index: &QModelIndex, _role: i32) -> QVariant { - QVariant::default() - } -} - -/// A struct which inherits from QAbstractListModel -#[derive(Default)] -pub struct AbstractListModelRust {} - -#[derive(Default)] -pub struct LineCountRust { - pub count: i32, -} +pub mod line_count; fn main() { use cxx_qt_lib::{QGuiApplication, QQmlApplicationEngine, QUrl};