You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

213 lines
6.2 KiB
QML

5 years ago
import QtQuick 2.15
import QtQml 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
5 years ago
import QtQuick.Layouts 1.15
5 years ago
import FishUI 1.0 as FishUI
import Cutefish.TextEditor 1.0
Item {
id: control
property var tabName: document.fileName + (document.modified ? " *" : "")
5 years ago
property alias fileUrl: document.fileUrl
5 years ago
property alias fileName: document.fileName
5 years ago
property bool showLineNumbers: true
5 years ago
property int characterCount: body.text.length
5 years ago
height: ListView.view ? ListView.view.height : 0
width: ListView.view ? ListView.view.width : 0
5 years ago
5 years ago
DocumentHandler {
id: document
document: body.textDocument
cursorPosition: body.cursorPosition
selectionStart: body.selectionStart
selectionEnd: body.selectionEnd
backgroundColor: FishUI.Theme.backgroundColor
enableSyntaxHighlighting: true
theme: FishUI.Theme.darkMode ? "Breeze Dark" : "Breeze Light"
5 years ago
onSearchFound: {
body.select(start, end)
}
onFileSaved: {
root.showPassiveNotification(qsTr("Saved successfully"), 3000)
}
5 years ago
}
ScrollView {
id: _scrollView
anchors.fill: parent
Keys.enabled: true
Keys.forwardTo: body
5 years ago
contentWidth: availableWidth
5 years ago
Flickable {
id: _flickable
5 years ago
FishUI.WheelHandler {
id: wheelHandler
target: _flickable
}
5 years ago
boundsBehavior: Flickable.StopAtBounds
boundsMovement: Flickable.StopAtBounds
5 years ago
ScrollBar.vertical: ScrollBar {
policy: ScrollBar.AlwaysOff
}
5 years ago
TextArea.flickable: TextArea {
id: body
text: document.text
selectByKeyboard: true
selectByMouse: true
persistentSelection: true
textFormat: TextEdit.PlainText
wrapMode: TextEdit.WrapAnywhere
activeFocusOnPress: true
activeFocusOnTab: true
leftPadding: _linesCounter.width + FishUI.Units.smallSpacing
padding: FishUI.Units.smallSpacing
color: FishUI.Theme.textColor
font.family: "Noto Mono"
background: Rectangle {
color: FishUI.Theme.backgroundColor
}
Keys.enabled: true
Keys.onPressed: {
if ((event.modifiers & Qt.ControlModifier) && (event.key === Qt.Key_S)) {
control.save()
event.accepted = true
}
}
5 years ago
Loader {
id: _linesCounter
active: control.showLineNumbers && !document.isRich
5 years ago
asynchronous: true
5 years ago
5 years ago
anchors.left: body.left
anchors.top: body.top
anchors.topMargin: body.topPadding + body.textMargin
5 years ago
5 years ago
height: _flickable.contentHeight
5 years ago
width: active ? 32 : 0
sourceComponent: _linesCounterComponent
}
}
}
}
Component {
id: _linesCounterComponent
ListView {
id: _linesCounterList
model: document.lineCount
5 years ago
clip: true
5 years ago
Binding on currentIndex {
value: document.currentLineIndex
restoreMode: Binding.RestoreBindingOrValue
}
Timer {
id: _lineIndexTimer
interval: 250
onTriggered: _linesCounterList.currentIndex = document.currentLineIndex
}
Connections {
target: document
function onLineCountChanged() {
_lineIndexTimer.restart()
}
}
orientation: ListView.Vertical
interactive: false
snapMode: ListView.NoSnap
boundsBehavior: Flickable.StopAtBounds
boundsMovement :Flickable.StopAtBounds
preferredHighlightBegin: 0
preferredHighlightEnd: width
highlightRangeMode: ListView.StrictlyEnforceRange
highlightMoveDuration: 0
highlightFollowsCurrentItem: false
highlightResizeDuration: 0
highlightMoveVelocity: -1
highlightResizeVelocity: -1
maximumFlickVelocity: 0
delegate: Row {
id: _delegate
readonly property int line : index
5 years ago
// property bool foldable : control.document.isFoldable(line)
5 years ago
width: ListView.view.width
5 years ago
height: document.lineHeight(line)
5 years ago
readonly property bool isCurrentItem: ListView.isCurrentItem
// Connections {
// target: control.body
// function onContentHeightChanged() {
// if (_delegate.isCurrentItem) {
// console.log("Updating line height")
// _delegate.height = control.document.lineHeight(_delegate.line)
// _delegate.foldable = control.document.isFoldable(_delegate.line)
// }
// }
// }
Label {
width: 32
height: parent.height
opacity: isCurrentItem ? 1 : 0.7
color: isCurrentItem ? FishUI.Theme.highlightColor
: FishUI.Theme.textColor
font.pointSize: body.font.pointSize
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
font.family: "Monospace"
text: index + 1
}
}
}
}
function forceActiveFocus() {
body.forceActiveFocus()
}
function goToLine(line) {
if (line > 0 && line <= body.lineCount) {
body.cursorPosition = document.goToLine(line - 1)
body.forceActiveFocus()
}
}
function save() {
document.saveAs(document.fileUrl)
}
5 years ago
}