mirror of
https://github.com/AvengeMedia/DankMaterialShell.git
synced 2025-12-06 05:25:41 -05:00
feat: Add tabs to Notepad
This commit is contained in:
@@ -48,6 +48,8 @@ Singleton {
|
|||||||
property string notepadCurrentFileName: ""
|
property string notepadCurrentFileName: ""
|
||||||
property string notepadCurrentFileUrl: ""
|
property string notepadCurrentFileUrl: ""
|
||||||
property string notepadLastSavedContent: ""
|
property string notepadLastSavedContent: ""
|
||||||
|
property var notepadTabs: []
|
||||||
|
property int notepadCurrentTabIndex: 0
|
||||||
|
|
||||||
Component.onCompleted: {
|
Component.onCompleted: {
|
||||||
loadSettings()
|
loadSettings()
|
||||||
@@ -111,6 +113,36 @@ Singleton {
|
|||||||
notepadCurrentFileName = settings.notepadCurrentFileName !== undefined ? settings.notepadCurrentFileName : ""
|
notepadCurrentFileName = settings.notepadCurrentFileName !== undefined ? settings.notepadCurrentFileName : ""
|
||||||
notepadCurrentFileUrl = settings.notepadCurrentFileUrl !== undefined ? settings.notepadCurrentFileUrl : ""
|
notepadCurrentFileUrl = settings.notepadCurrentFileUrl !== undefined ? settings.notepadCurrentFileUrl : ""
|
||||||
notepadLastSavedContent = settings.notepadLastSavedContent !== undefined ? settings.notepadLastSavedContent : ""
|
notepadLastSavedContent = settings.notepadLastSavedContent !== undefined ? settings.notepadLastSavedContent : ""
|
||||||
|
notepadTabs = settings.notepadTabs !== undefined ? settings.notepadTabs : []
|
||||||
|
notepadCurrentTabIndex = settings.notepadCurrentTabIndex !== undefined ? settings.notepadCurrentTabIndex : 0
|
||||||
|
|
||||||
|
// Migrate legacy single notepad to tabs if needed
|
||||||
|
if (notepadTabs.length === 0 && (notepadContent || notepadCurrentFileName)) {
|
||||||
|
notepadTabs = [{
|
||||||
|
id: Date.now(),
|
||||||
|
title: notepadCurrentFileName || "Untitled",
|
||||||
|
content: notepadContent,
|
||||||
|
fileName: notepadCurrentFileName,
|
||||||
|
fileUrl: notepadCurrentFileUrl,
|
||||||
|
lastSavedContent: notepadLastSavedContent,
|
||||||
|
hasUnsavedChanges: false
|
||||||
|
}]
|
||||||
|
notepadCurrentTabIndex = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure at least one tab exists
|
||||||
|
if (notepadTabs.length === 0) {
|
||||||
|
notepadTabs = [{
|
||||||
|
id: Date.now(),
|
||||||
|
title: "Untitled",
|
||||||
|
content: "",
|
||||||
|
fileName: "",
|
||||||
|
fileUrl: "",
|
||||||
|
lastSavedContent: "",
|
||||||
|
hasUnsavedChanges: false
|
||||||
|
}]
|
||||||
|
notepadCurrentTabIndex = 0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
||||||
@@ -150,7 +182,9 @@ Singleton {
|
|||||||
"notepadContent": notepadContent,
|
"notepadContent": notepadContent,
|
||||||
"notepadCurrentFileName": notepadCurrentFileName,
|
"notepadCurrentFileName": notepadCurrentFileName,
|
||||||
"notepadCurrentFileUrl": notepadCurrentFileUrl,
|
"notepadCurrentFileUrl": notepadCurrentFileUrl,
|
||||||
"notepadLastSavedContent": notepadLastSavedContent
|
"notepadLastSavedContent": notepadLastSavedContent,
|
||||||
|
"notepadTabs": notepadTabs,
|
||||||
|
"notepadCurrentTabIndex": notepadCurrentTabIndex
|
||||||
}, null, 2))
|
}, null, 2))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -26,12 +26,128 @@ PanelWindow {
|
|||||||
property string pendingAction: ""
|
property string pendingAction: ""
|
||||||
property string lastSavedFileContent: ""
|
property string lastSavedFileContent: ""
|
||||||
property bool expandedWidth: false
|
property bool expandedWidth: false
|
||||||
|
property var currentTab: SessionData.notepadTabs.length > SessionData.notepadCurrentTabIndex ? SessionData.notepadTabs[SessionData.notepadCurrentTabIndex] : null
|
||||||
|
property int nextTabId: Date.now()
|
||||||
|
|
||||||
function hasFileChanges() {
|
function hasFileChanges() {
|
||||||
if (!root.currentFileUrl.toString()) {
|
if (!currentTab) return false
|
||||||
return root.hasUnsavedChanges || SessionData.notepadContent.length > 0
|
return currentTab.content !== currentTab.lastSavedContent
|
||||||
|
}
|
||||||
|
|
||||||
|
function getCurrentTabData() {
|
||||||
|
return currentTab || {
|
||||||
|
id: 0,
|
||||||
|
title: "Untitled",
|
||||||
|
content: "",
|
||||||
|
fileName: "",
|
||||||
|
fileUrl: "",
|
||||||
|
lastSavedContent: "",
|
||||||
|
hasUnsavedChanges: false
|
||||||
}
|
}
|
||||||
return SessionData.notepadContent !== root.lastSavedFileContent
|
}
|
||||||
|
|
||||||
|
function updateCurrentTab(properties, saveImmediately = false) {
|
||||||
|
if (!currentTab) return
|
||||||
|
|
||||||
|
var tabs = [...SessionData.notepadTabs]
|
||||||
|
var tabIndex = SessionData.notepadCurrentTabIndex
|
||||||
|
|
||||||
|
if (tabIndex >= 0 && tabIndex < tabs.length) {
|
||||||
|
var updatedTab = Object.assign({}, tabs[tabIndex])
|
||||||
|
Object.assign(updatedTab, properties)
|
||||||
|
tabs[tabIndex] = updatedTab
|
||||||
|
SessionData.notepadTabs = tabs
|
||||||
|
|
||||||
|
if (saveImmediately) {
|
||||||
|
SessionData.saveSettings()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function createNewTab() {
|
||||||
|
var newTab = {
|
||||||
|
id: ++nextTabId,
|
||||||
|
title: "Untitled",
|
||||||
|
content: "",
|
||||||
|
fileName: "",
|
||||||
|
fileUrl: "",
|
||||||
|
lastSavedContent: "",
|
||||||
|
hasUnsavedChanges: false
|
||||||
|
}
|
||||||
|
|
||||||
|
var tabs = [...SessionData.notepadTabs]
|
||||||
|
tabs.push(newTab)
|
||||||
|
SessionData.notepadTabs = tabs
|
||||||
|
SessionData.notepadCurrentTabIndex = tabs.length - 1
|
||||||
|
|
||||||
|
textArea.text = ""
|
||||||
|
textArea.forceActiveFocus()
|
||||||
|
|
||||||
|
deferredSaveTimer.restart()
|
||||||
|
}
|
||||||
|
|
||||||
|
function closeTab(tabIndex) {
|
||||||
|
var tabToClose = SessionData.notepadTabs[tabIndex]
|
||||||
|
var hasChanges = tabToClose && tabToClose.content !== tabToClose.lastSavedContent
|
||||||
|
|
||||||
|
if (hasChanges) {
|
||||||
|
root.pendingAction = "close_tab_" + tabIndex
|
||||||
|
root.confirmationDialogOpen = true
|
||||||
|
confirmationDialog.open()
|
||||||
|
} else {
|
||||||
|
performCloseTab(tabIndex)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function performCloseTab(tabIndex) {
|
||||||
|
var tabs = [...SessionData.notepadTabs]
|
||||||
|
|
||||||
|
if (tabs.length <= 1) {
|
||||||
|
tabs[0] = {
|
||||||
|
id: ++nextTabId,
|
||||||
|
title: "Untitled",
|
||||||
|
content: "",
|
||||||
|
fileName: "",
|
||||||
|
fileUrl: "",
|
||||||
|
lastSavedContent: "",
|
||||||
|
hasUnsavedChanges: false
|
||||||
|
}
|
||||||
|
SessionData.notepadCurrentTabIndex = 0
|
||||||
|
} else {
|
||||||
|
tabs.splice(tabIndex, 1)
|
||||||
|
if (SessionData.notepadCurrentTabIndex >= tabs.length) {
|
||||||
|
SessionData.notepadCurrentTabIndex = tabs.length - 1
|
||||||
|
} else if (SessionData.notepadCurrentTabIndex > tabIndex) {
|
||||||
|
SessionData.notepadCurrentTabIndex -= 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SessionData.notepadTabs = tabs
|
||||||
|
|
||||||
|
Qt.callLater(() => {
|
||||||
|
if (currentTab) {
|
||||||
|
textArea.text = currentTab.content
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
deferredSaveTimer.restart()
|
||||||
|
}
|
||||||
|
|
||||||
|
function switchToTab(tabIndex) {
|
||||||
|
if (tabIndex < 0 || tabIndex >= SessionData.notepadTabs.length) return
|
||||||
|
|
||||||
|
SessionData.notepadCurrentTabIndex = tabIndex
|
||||||
|
|
||||||
|
Qt.callLater(() => {
|
||||||
|
if (currentTab) {
|
||||||
|
textArea.text = currentTab.content
|
||||||
|
root.currentFileName = currentTab.fileName
|
||||||
|
root.currentFileUrl = currentTab.fileUrl
|
||||||
|
root.lastSavedFileContent = currentTab.lastSavedContent
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
deferredSaveTimer.restart()
|
||||||
}
|
}
|
||||||
|
|
||||||
function show() {
|
function show() {
|
||||||
@@ -119,57 +235,165 @@ PanelWindow {
|
|||||||
spacing: Theme.spacingM
|
spacing: Theme.spacingM
|
||||||
|
|
||||||
// Header
|
// Header
|
||||||
Row {
|
Column {
|
||||||
width: parent.width
|
width: parent.width
|
||||||
height: 40
|
spacing: Theme.spacingXS
|
||||||
|
|
||||||
|
// Title row
|
||||||
|
Row {
|
||||||
|
width: parent.width
|
||||||
|
height: 32
|
||||||
|
|
||||||
Column {
|
Column {
|
||||||
width: parent.width - buttonRow.width
|
width: parent.width - buttonRow.width
|
||||||
spacing: Theme.spacingXS
|
spacing: Theme.spacingXS
|
||||||
anchors.verticalCenter: parent.verticalCenter
|
anchors.verticalCenter: parent.verticalCenter
|
||||||
|
|
||||||
StyledText {
|
StyledText {
|
||||||
text: qsTr("Notepad")
|
text: qsTr("Notepad")
|
||||||
font.pixelSize: Theme.fontSizeLarge
|
font.pixelSize: Theme.fontSizeLarge
|
||||||
color: Theme.surfaceText
|
color: Theme.surfaceText
|
||||||
font.weight: Font.Medium
|
font.weight: Font.Medium
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
StyledText {
|
Row {
|
||||||
text: (hasFileChanges() ? "● " : "") + (root.currentFileName || qsTr("Untitled"))
|
id: buttonRow
|
||||||
font.pixelSize: Theme.fontSizeSmall
|
spacing: Theme.spacingXS
|
||||||
color: hasFileChanges() ? Theme.primary : Theme.surfaceTextMedium
|
|
||||||
visible: root.currentFileName !== "" || hasFileChanges()
|
DankActionButton {
|
||||||
elide: Text.ElideMiddle
|
id: expandButton
|
||||||
maximumLineCount: 1
|
iconName: root.expandedWidth ? "unfold_less" : "unfold_more"
|
||||||
width: parent.width - Theme.spacingM
|
iconSize: Theme.iconSize - 4
|
||||||
|
iconColor: Theme.surfaceText
|
||||||
|
onClicked: root.expandedWidth = !root.expandedWidth
|
||||||
|
|
||||||
|
transform: Rotation {
|
||||||
|
angle: 90
|
||||||
|
origin.x: expandButton.width / 2
|
||||||
|
origin.y: expandButton.height / 2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
DankActionButton {
|
||||||
|
id: closeButton
|
||||||
|
iconName: "close"
|
||||||
|
iconSize: Theme.iconSize - 4
|
||||||
|
iconColor: Theme.surfaceText
|
||||||
|
onClicked: root.hide()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Tab bar
|
||||||
Row {
|
Row {
|
||||||
id: buttonRow
|
width: parent.width
|
||||||
|
height: 36
|
||||||
spacing: Theme.spacingXS
|
spacing: Theme.spacingXS
|
||||||
|
|
||||||
DankActionButton {
|
ScrollView {
|
||||||
id: expandButton
|
width: parent.width - newTabButton.width - Theme.spacingXS
|
||||||
iconName: root.expandedWidth ? "unfold_less" : "unfold_more"
|
height: parent.height
|
||||||
iconSize: Theme.iconSize - 4
|
clip: true
|
||||||
iconColor: Theme.surfaceText
|
|
||||||
onClicked: root.expandedWidth = !root.expandedWidth
|
|
||||||
|
|
||||||
transform: Rotation {
|
ScrollBar.horizontal.visible: false
|
||||||
angle: 90
|
ScrollBar.vertical.visible: false
|
||||||
origin.x: expandButton.width / 2
|
|
||||||
origin.y: expandButton.height / 2
|
Row {
|
||||||
|
spacing: Theme.spacingXXS
|
||||||
|
|
||||||
|
Repeater {
|
||||||
|
model: SessionData.notepadTabs
|
||||||
|
|
||||||
|
delegate: Rectangle {
|
||||||
|
required property int index
|
||||||
|
required property var modelData
|
||||||
|
|
||||||
|
readonly property bool isActive: SessionData.notepadCurrentTabIndex === index
|
||||||
|
readonly property bool tabHasChanges: modelData.content !== modelData.lastSavedContent
|
||||||
|
readonly property bool isHovered: tabMouseArea.containsMouse && !closeMouseArea.containsMouse
|
||||||
|
|
||||||
|
width: Math.max(120, Math.min(200, tabContent.implicitWidth + Theme.spacingM * 2))
|
||||||
|
height: 32
|
||||||
|
radius: Theme.cornerRadius
|
||||||
|
color: isActive ? Theme.primaryPressed : isHovered ? Theme.primaryHoverLight : "transparent"
|
||||||
|
border.width: isActive ? 0 : 1
|
||||||
|
border.color: Theme.outlineMedium
|
||||||
|
|
||||||
|
MouseArea {
|
||||||
|
id: tabMouseArea
|
||||||
|
anchors.fill: parent
|
||||||
|
hoverEnabled: true
|
||||||
|
cursorShape: Qt.PointingHandCursor
|
||||||
|
acceptedButtons: Qt.LeftButton
|
||||||
|
|
||||||
|
onClicked: switchToTab(index)
|
||||||
|
}
|
||||||
|
|
||||||
|
Row {
|
||||||
|
id: tabContent
|
||||||
|
anchors.centerIn: parent
|
||||||
|
spacing: Theme.spacingXS
|
||||||
|
|
||||||
|
StyledText {
|
||||||
|
text: (tabHasChanges ? "● " : "") + (modelData.title || "Untitled")
|
||||||
|
font.pixelSize: Theme.fontSizeSmall
|
||||||
|
color: isActive ? Theme.primary : Theme.surfaceText
|
||||||
|
font.weight: isActive ? Font.Medium : Font.Normal
|
||||||
|
elide: Text.ElideMiddle
|
||||||
|
maximumLineCount: 1
|
||||||
|
anchors.verticalCenter: parent.verticalCenter
|
||||||
|
}
|
||||||
|
|
||||||
|
Rectangle {
|
||||||
|
id: tabCloseButton
|
||||||
|
width: 20
|
||||||
|
height: 20
|
||||||
|
radius: 10
|
||||||
|
color: closeMouseArea.containsMouse ? Theme.surfaceTextHover : "transparent"
|
||||||
|
visible: SessionData.notepadTabs.length > 1
|
||||||
|
anchors.verticalCenter: parent.verticalCenter
|
||||||
|
|
||||||
|
DankIcon {
|
||||||
|
name: "close"
|
||||||
|
size: 14
|
||||||
|
color: Theme.surfaceTextMedium
|
||||||
|
anchors.centerIn: parent
|
||||||
|
}
|
||||||
|
|
||||||
|
MouseArea {
|
||||||
|
id: closeMouseArea
|
||||||
|
anchors.fill: parent
|
||||||
|
hoverEnabled: true
|
||||||
|
cursorShape: Qt.PointingHandCursor
|
||||||
|
z: 100
|
||||||
|
|
||||||
|
onClicked: {
|
||||||
|
closeTab(index)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Behavior on color {
|
||||||
|
ColorAnimation {
|
||||||
|
duration: Theme.shortDuration
|
||||||
|
easing.type: Theme.standardEasing
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
DankActionButton {
|
DankActionButton {
|
||||||
id: closeButton
|
id: newTabButton
|
||||||
iconName: "close"
|
width: 32
|
||||||
|
height: 32
|
||||||
|
iconName: "add"
|
||||||
iconSize: Theme.iconSize - 4
|
iconSize: Theme.iconSize - 4
|
||||||
iconColor: Theme.surfaceText
|
iconColor: Theme.surfaceText
|
||||||
onClicked: root.hide()
|
onClicked: createNewTab()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -177,7 +401,7 @@ PanelWindow {
|
|||||||
// Text area
|
// Text area
|
||||||
StyledRect {
|
StyledRect {
|
||||||
width: parent.width
|
width: parent.width
|
||||||
height: parent.height - 140
|
height: parent.height - 180
|
||||||
color: Theme.surface
|
color: Theme.surface
|
||||||
border.color: Theme.outlineMedium
|
border.color: Theme.outlineMedium
|
||||||
border.width: 1
|
border.width: 1
|
||||||
@@ -208,26 +432,33 @@ PanelWindow {
|
|||||||
bottomPadding: Theme.spacingM
|
bottomPadding: Theme.spacingM
|
||||||
|
|
||||||
Component.onCompleted: {
|
Component.onCompleted: {
|
||||||
text = SessionData.notepadContent
|
if (currentTab) {
|
||||||
root.currentFileName = SessionData.notepadCurrentFileName
|
text = currentTab.content
|
||||||
root.currentFileUrl = SessionData.notepadCurrentFileUrl
|
root.currentFileName = currentTab.fileName
|
||||||
root.lastSavedFileContent = SessionData.notepadLastSavedContent
|
root.currentFileUrl = currentTab.fileUrl
|
||||||
|
root.lastSavedFileContent = currentTab.lastSavedContent
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Connections {
|
Connections {
|
||||||
target: SessionData
|
target: root
|
||||||
function onNotepadContentChanged() {
|
function onCurrentTabChanged() {
|
||||||
if (textArea.text !== SessionData.notepadContent) {
|
if (currentTab && textArea.text !== currentTab.content) {
|
||||||
textArea.text = SessionData.notepadContent
|
textArea.text = currentTab.content
|
||||||
|
root.currentFileName = currentTab.fileName
|
||||||
|
root.currentFileUrl = currentTab.fileUrl
|
||||||
|
root.lastSavedFileContent = currentTab.lastSavedContent
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onTextChanged: {
|
onTextChanged: {
|
||||||
if (text !== SessionData.notepadContent) {
|
if (currentTab && text !== currentTab.content) {
|
||||||
SessionData.notepadContent = text
|
updateCurrentTab({
|
||||||
root.hasUnsavedChanges = true
|
content: text,
|
||||||
saveTimer.restart()
|
hasUnsavedChanges: true
|
||||||
|
})
|
||||||
|
autoSaveTimer.restart()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -241,8 +472,8 @@ PanelWindow {
|
|||||||
switch (event.key) {
|
switch (event.key) {
|
||||||
case Qt.Key_S:
|
case Qt.Key_S:
|
||||||
event.accepted = true
|
event.accepted = true
|
||||||
if (root.currentFileUrl.toString()) {
|
if (currentTab && currentTab.fileUrl) {
|
||||||
saveToFile(root.currentFileUrl)
|
saveToFile(currentTab.fileUrl)
|
||||||
} else {
|
} else {
|
||||||
root.fileDialogOpen = true
|
root.fileDialogOpen = true
|
||||||
saveBrowser.open()
|
saveBrowser.open()
|
||||||
@@ -266,16 +497,7 @@ PanelWindow {
|
|||||||
root.confirmationDialogOpen = true
|
root.confirmationDialogOpen = true
|
||||||
confirmationDialog.open()
|
confirmationDialog.open()
|
||||||
} else {
|
} else {
|
||||||
textArea.text = ""
|
createNewTab()
|
||||||
SessionData.notepadContent = ""
|
|
||||||
root.currentFileName = ""
|
|
||||||
root.currentFileUrl = ""
|
|
||||||
root.hasUnsavedChanges = false
|
|
||||||
root.lastSavedFileContent = ""
|
|
||||||
SessionData.notepadCurrentFileName = ""
|
|
||||||
SessionData.notepadCurrentFileUrl = ""
|
|
||||||
SessionData.notepadLastSavedContent = ""
|
|
||||||
SessionData.saveSettings()
|
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
case Qt.Key_A:
|
case Qt.Key_A:
|
||||||
@@ -308,7 +530,7 @@ PanelWindow {
|
|||||||
iconName: "save"
|
iconName: "save"
|
||||||
iconSize: Theme.iconSize - 2
|
iconSize: Theme.iconSize - 2
|
||||||
iconColor: Theme.primary
|
iconColor: Theme.primary
|
||||||
enabled: hasFileChanges() || SessionData.notepadContent.length > 0
|
enabled: currentTab && (hasFileChanges() || currentTab.content.length > 0)
|
||||||
onClicked: {
|
onClicked: {
|
||||||
root.fileDialogOpen = true
|
root.fileDialogOpen = true
|
||||||
saveBrowser.open()
|
saveBrowser.open()
|
||||||
@@ -353,24 +575,7 @@ PanelWindow {
|
|||||||
iconName: "note_add"
|
iconName: "note_add"
|
||||||
iconSize: Theme.iconSize - 2
|
iconSize: Theme.iconSize - 2
|
||||||
iconColor: Theme.surfaceText
|
iconColor: Theme.surfaceText
|
||||||
onClicked: {
|
onClicked: createNewTab()
|
||||||
if (hasFileChanges()) {
|
|
||||||
root.pendingAction = "new"
|
|
||||||
root.confirmationDialogOpen = true
|
|
||||||
confirmationDialog.open()
|
|
||||||
} else {
|
|
||||||
textArea.text = ""
|
|
||||||
SessionData.notepadContent = ""
|
|
||||||
root.currentFileName = ""
|
|
||||||
root.currentFileUrl = ""
|
|
||||||
root.hasUnsavedChanges = false
|
|
||||||
root.lastSavedFileContent = ""
|
|
||||||
SessionData.notepadCurrentFileName = ""
|
|
||||||
SessionData.notepadCurrentFileUrl = ""
|
|
||||||
SessionData.notepadLastSavedContent = ""
|
|
||||||
SessionData.saveSettings()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
StyledText {
|
StyledText {
|
||||||
anchors.verticalCenter: parent.verticalCenter
|
anchors.verticalCenter: parent.verticalCenter
|
||||||
@@ -386,7 +591,7 @@ PanelWindow {
|
|||||||
spacing: Theme.spacingL
|
spacing: Theme.spacingL
|
||||||
|
|
||||||
StyledText {
|
StyledText {
|
||||||
text: SessionData.notepadContent.length > 0 ? qsTr("%1 characters").arg(SessionData.notepadContent.length) : qsTr("Empty")
|
text: currentTab && currentTab.content.length > 0 ? qsTr("%1 characters").arg(currentTab.content.length) : qsTr("Empty")
|
||||||
font.pixelSize: Theme.fontSizeSmall
|
font.pixelSize: Theme.fontSizeSmall
|
||||||
color: Theme.surfaceTextMedium
|
color: Theme.surfaceTextMedium
|
||||||
}
|
}
|
||||||
@@ -395,14 +600,14 @@ PanelWindow {
|
|||||||
text: qsTr("Lines: %1").arg(textArea.lineCount)
|
text: qsTr("Lines: %1").arg(textArea.lineCount)
|
||||||
font.pixelSize: Theme.fontSizeSmall
|
font.pixelSize: Theme.fontSizeSmall
|
||||||
color: Theme.surfaceTextMedium
|
color: Theme.surfaceTextMedium
|
||||||
visible: SessionData.notepadContent.length > 0
|
visible: currentTab && currentTab.content.length > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
StyledText {
|
StyledText {
|
||||||
text: saveTimer.running ? qsTr("Auto-saving...") : (hasFileChanges() ? qsTr("Unsaved changes") : qsTr("Auto-saved"))
|
text: autoSaveTimer.running ? qsTr("Auto-saving...") : (hasFileChanges() ? qsTr("Unsaved changes") : qsTr("Auto-saved"))
|
||||||
font.pixelSize: Theme.fontSizeSmall
|
font.pixelSize: Theme.fontSizeSmall
|
||||||
color: hasFileChanges() ? Theme.warning : (saveTimer.running ? Theme.primary : Theme.surfaceTextMedium)
|
color: hasFileChanges() ? Theme.warning : (autoSaveTimer.running ? Theme.primary : Theme.surfaceTextMedium)
|
||||||
opacity: SessionData.notepadContent.length > 0 ? 1 : 0
|
opacity: currentTab && currentTab.content.length > 0 ? 1 : 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -410,19 +615,32 @@ PanelWindow {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Timer {
|
Timer {
|
||||||
id: saveTimer
|
id: autoSaveTimer
|
||||||
interval: 1000
|
interval: 2000
|
||||||
|
repeat: false
|
||||||
|
onTriggered: {
|
||||||
|
if (currentTab) {
|
||||||
|
updateCurrentTab({
|
||||||
|
hasUnsavedChanges: false
|
||||||
|
}, true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Timer {
|
||||||
|
id: deferredSaveTimer
|
||||||
|
interval: 500
|
||||||
repeat: false
|
repeat: false
|
||||||
onTriggered: {
|
onTriggered: {
|
||||||
SessionData.saveSettings()
|
SessionData.saveSettings()
|
||||||
root.hasUnsavedChanges = false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// File save/load functionality
|
// File save/load functionality
|
||||||
function saveToFile(fileUrl) {
|
function saveToFile(fileUrl) {
|
||||||
const content = SessionData.notepadContent
|
if (!currentTab) return
|
||||||
|
|
||||||
|
const content = currentTab.content
|
||||||
const cleanPath = fileUrl.toString().replace(/^file:\/\//, '')
|
const cleanPath = fileUrl.toString().replace(/^file:\/\//, '')
|
||||||
const escapedContent = content.replace(/'/g, "'\\''")
|
const escapedContent = content.replace(/'/g, "'\\''")
|
||||||
saveProcess.command = ["sh", "-c", "printf '%s' '" + escapedContent + "' > '" + cleanPath + "'"]
|
saveProcess.command = ["sh", "-c", "printf '%s' '" + escapedContent + "' > '" + cleanPath + "'"]
|
||||||
@@ -440,13 +658,12 @@ PanelWindow {
|
|||||||
id: saveProcess
|
id: saveProcess
|
||||||
|
|
||||||
onExited: (exitCode) => {
|
onExited: (exitCode) => {
|
||||||
if (exitCode === 0) {
|
if (exitCode === 0 && currentTab) {
|
||||||
root.hasUnsavedChanges = false
|
updateCurrentTab({
|
||||||
root.lastSavedFileContent = SessionData.notepadContent
|
hasUnsavedChanges: false,
|
||||||
SessionData.notepadCurrentFileName = root.currentFileName
|
lastSavedContent: currentTab.content
|
||||||
SessionData.notepadCurrentFileUrl = root.currentFileUrl.toString()
|
}, true)
|
||||||
SessionData.notepadLastSavedContent = root.lastSavedFileContent
|
root.lastSavedFileContent = currentTab.content
|
||||||
SessionData.saveSettings()
|
|
||||||
} else {
|
} else {
|
||||||
console.warn("Notepad: Failed to save file, exit code:", exitCode)
|
console.warn("Notepad: Failed to save file, exit code:", exitCode)
|
||||||
}
|
}
|
||||||
@@ -458,13 +675,15 @@ PanelWindow {
|
|||||||
|
|
||||||
stdout: StdioCollector {
|
stdout: StdioCollector {
|
||||||
onStreamFinished: {
|
onStreamFinished: {
|
||||||
SessionData.notepadContent = text
|
if (currentTab) {
|
||||||
root.hasUnsavedChanges = false
|
updateCurrentTab({
|
||||||
root.lastSavedFileContent = text
|
content: text,
|
||||||
SessionData.notepadCurrentFileName = root.currentFileName
|
hasUnsavedChanges: false,
|
||||||
SessionData.notepadCurrentFileUrl = root.currentFileUrl.toString()
|
lastSavedContent: text
|
||||||
SessionData.notepadLastSavedContent = text
|
}, true)
|
||||||
SessionData.saveSettings()
|
textArea.text = text
|
||||||
|
root.lastSavedFileContent = text
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -484,7 +703,7 @@ PanelWindow {
|
|||||||
fileExtensions: ["*.txt", "*.md", "*.*"]
|
fileExtensions: ["*.txt", "*.md", "*.*"]
|
||||||
allowStacking: true
|
allowStacking: true
|
||||||
saveMode: true
|
saveMode: true
|
||||||
defaultFileName: root.currentFileName || "note.txt"
|
defaultFileName: (currentTab && currentTab.fileName) || "note.txt"
|
||||||
|
|
||||||
WlrLayershell.layer: WlrLayershell.Overlay
|
WlrLayershell.layer: WlrLayershell.Overlay
|
||||||
|
|
||||||
@@ -497,27 +716,31 @@ PanelWindow {
|
|||||||
root.currentFileName = fileName
|
root.currentFileName = fileName
|
||||||
root.currentFileUrl = fileUrl
|
root.currentFileUrl = fileUrl
|
||||||
|
|
||||||
|
if (currentTab) {
|
||||||
|
updateCurrentTab({
|
||||||
|
title: fileName,
|
||||||
|
fileName: fileName,
|
||||||
|
fileUrl: fileUrl
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
saveToFile(fileUrl)
|
saveToFile(fileUrl)
|
||||||
|
|
||||||
// Handle pending action after save
|
// Handle pending action after save
|
||||||
if (root.pendingAction === "new") {
|
if (root.pendingAction === "new") {
|
||||||
Qt.callLater(() => {
|
Qt.callLater(() => {
|
||||||
textArea.text = ""
|
createNewTab()
|
||||||
SessionData.notepadContent = ""
|
|
||||||
root.currentFileName = ""
|
|
||||||
root.currentFileUrl = ""
|
|
||||||
root.hasUnsavedChanges = false
|
|
||||||
root.lastSavedFileContent = ""
|
|
||||||
SessionData.notepadCurrentFileName = ""
|
|
||||||
SessionData.notepadCurrentFileUrl = ""
|
|
||||||
SessionData.notepadLastSavedContent = ""
|
|
||||||
SessionData.saveSettings()
|
|
||||||
})
|
})
|
||||||
} else if (root.pendingAction === "open") {
|
} else if (root.pendingAction === "open") {
|
||||||
Qt.callLater(() => {
|
Qt.callLater(() => {
|
||||||
root.fileDialogOpen = true
|
root.fileDialogOpen = true
|
||||||
loadBrowser.open()
|
loadBrowser.open()
|
||||||
})
|
})
|
||||||
|
} else if (root.pendingAction.startsWith("close_tab_")) {
|
||||||
|
Qt.callLater(() => {
|
||||||
|
var tabIndex = parseInt(root.pendingAction.split("_")[2])
|
||||||
|
performCloseTab(tabIndex)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
root.pendingAction = ""
|
root.pendingAction = ""
|
||||||
|
|
||||||
@@ -549,6 +772,14 @@ PanelWindow {
|
|||||||
root.currentFileName = fileName
|
root.currentFileName = fileName
|
||||||
root.currentFileUrl = fileUrl
|
root.currentFileUrl = fileUrl
|
||||||
|
|
||||||
|
if (currentTab) {
|
||||||
|
updateCurrentTab({
|
||||||
|
title: fileName,
|
||||||
|
fileName: fileName,
|
||||||
|
fileUrl: fileUrl
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
loadFromFile(fileUrl)
|
loadFromFile(fileUrl)
|
||||||
close()
|
close()
|
||||||
}
|
}
|
||||||
@@ -604,6 +835,8 @@ PanelWindow {
|
|||||||
StyledText {
|
StyledText {
|
||||||
text: root.pendingAction === "new" ?
|
text: root.pendingAction === "new" ?
|
||||||
qsTr("You have unsaved changes. Save before creating a new file?") :
|
qsTr("You have unsaved changes. Save before creating a new file?") :
|
||||||
|
root.pendingAction.startsWith("close_tab_") ?
|
||||||
|
qsTr("You have unsaved changes. Save before closing this tab?") :
|
||||||
qsTr("You have unsaved changes. Save before opening a file?")
|
qsTr("You have unsaved changes. Save before opening a file?")
|
||||||
font.pixelSize: Theme.fontSizeMedium
|
font.pixelSize: Theme.fontSizeMedium
|
||||||
color: Theme.surfaceTextMedium
|
color: Theme.surfaceTextMedium
|
||||||
@@ -658,19 +891,13 @@ PanelWindow {
|
|||||||
confirmationDialog.close()
|
confirmationDialog.close()
|
||||||
root.confirmationDialogOpen = false
|
root.confirmationDialogOpen = false
|
||||||
if (root.pendingAction === "new") {
|
if (root.pendingAction === "new") {
|
||||||
textArea.text = ""
|
createNewTab()
|
||||||
SessionData.notepadContent = ""
|
|
||||||
root.currentFileName = ""
|
|
||||||
root.currentFileUrl = ""
|
|
||||||
root.hasUnsavedChanges = false
|
|
||||||
root.lastSavedFileContent = ""
|
|
||||||
SessionData.notepadCurrentFileName = ""
|
|
||||||
SessionData.notepadCurrentFileUrl = ""
|
|
||||||
SessionData.notepadLastSavedContent = ""
|
|
||||||
SessionData.saveSettings()
|
|
||||||
} else if (root.pendingAction === "open") {
|
} else if (root.pendingAction === "open") {
|
||||||
root.fileDialogOpen = true
|
root.fileDialogOpen = true
|
||||||
loadBrowser.open()
|
loadBrowser.open()
|
||||||
|
} else if (root.pendingAction.startsWith("close_tab_")) {
|
||||||
|
var tabIndex = parseInt(root.pendingAction.split("_")[2])
|
||||||
|
performCloseTab(tabIndex)
|
||||||
}
|
}
|
||||||
root.pendingAction = ""
|
root.pendingAction = ""
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user