mirror of
https://github.com/AvengeMedia/DankMaterialShell.git
synced 2026-01-24 21:42:51 -05:00
feat: Update Notepad to use Metadata (READ ME)
- Potential breaking changes; SAVE your notes before updating - The new system will store files locally to free up Session data - Session metadata will be saved in JSON via: `notepad-session.json` - Local files will be saved in: `/.local/state/DankMaterialShell/notepad-files`
This commit is contained in:
7
.gitignore
vendored
7
.gitignore
vendored
@@ -63,6 +63,11 @@ CLAUDE-temp.md
|
|||||||
niri-colors.generated.kdl
|
niri-colors.generated.kdl
|
||||||
ghostty-colors.generated.conf
|
ghostty-colors.generated.conf
|
||||||
|
|
||||||
|
# Notepad files (should be in ~/.local/state/DankMaterialShell/)
|
||||||
|
untitled-*.txt
|
||||||
|
file:*
|
||||||
|
notepad-files/
|
||||||
|
|
||||||
result
|
result
|
||||||
|
|
||||||
# If you prefer the allow list template instead of the deny list, see community template:
|
# If you prefer the allow list template instead of the deny list, see community template:
|
||||||
@@ -96,4 +101,4 @@ go.work.sum
|
|||||||
|
|
||||||
# Editor/IDE
|
# Editor/IDE
|
||||||
# .idea/
|
# .idea/
|
||||||
# .vscode/
|
# .vscode/
|
||||||
@@ -44,12 +44,6 @@ Singleton {
|
|||||||
property int wallpaperCyclingInterval: 300 // seconds (5 minutes)
|
property int wallpaperCyclingInterval: 300 // seconds (5 minutes)
|
||||||
property string wallpaperCyclingTime: "06:00" // HH:mm format
|
property string wallpaperCyclingTime: "06:00" // HH:mm format
|
||||||
property string lastBrightnessDevice: ""
|
property string lastBrightnessDevice: ""
|
||||||
property string notepadContent: ""
|
|
||||||
property string notepadCurrentFileName: ""
|
|
||||||
property string notepadCurrentFileUrl: ""
|
|
||||||
property string notepadLastSavedContent: ""
|
|
||||||
property var notepadTabs: []
|
|
||||||
property int notepadCurrentTabIndex: 0
|
|
||||||
|
|
||||||
Component.onCompleted: {
|
Component.onCompleted: {
|
||||||
loadSettings()
|
loadSettings()
|
||||||
@@ -104,45 +98,11 @@ Singleton {
|
|||||||
wallpaperCyclingInterval = settings.wallpaperCyclingInterval !== undefined ? settings.wallpaperCyclingInterval : 300
|
wallpaperCyclingInterval = settings.wallpaperCyclingInterval !== undefined ? settings.wallpaperCyclingInterval : 300
|
||||||
wallpaperCyclingTime = settings.wallpaperCyclingTime !== undefined ? settings.wallpaperCyclingTime : "06:00"
|
wallpaperCyclingTime = settings.wallpaperCyclingTime !== undefined ? settings.wallpaperCyclingTime : "06:00"
|
||||||
lastBrightnessDevice = settings.lastBrightnessDevice !== undefined ? settings.lastBrightnessDevice : ""
|
lastBrightnessDevice = settings.lastBrightnessDevice !== undefined ? settings.lastBrightnessDevice : ""
|
||||||
notepadContent = settings.notepadContent !== undefined ? settings.notepadContent : ""
|
|
||||||
|
|
||||||
// Generate system themes but don't override user's theme choice
|
// Generate system themes but don't override user's theme choice
|
||||||
if (typeof Theme !== "undefined") {
|
if (typeof Theme !== "undefined") {
|
||||||
Theme.generateSystemThemesFromCurrentTheme()
|
Theme.generateSystemThemesFromCurrentTheme()
|
||||||
}
|
}
|
||||||
notepadCurrentFileName = settings.notepadCurrentFileName !== undefined ? settings.notepadCurrentFileName : ""
|
|
||||||
notepadCurrentFileUrl = settings.notepadCurrentFileUrl !== undefined ? settings.notepadCurrentFileUrl : ""
|
|
||||||
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) {
|
||||||
|
|
||||||
@@ -178,13 +138,7 @@ Singleton {
|
|||||||
"wallpaperCyclingMode": wallpaperCyclingMode,
|
"wallpaperCyclingMode": wallpaperCyclingMode,
|
||||||
"wallpaperCyclingInterval": wallpaperCyclingInterval,
|
"wallpaperCyclingInterval": wallpaperCyclingInterval,
|
||||||
"wallpaperCyclingTime": wallpaperCyclingTime,
|
"wallpaperCyclingTime": wallpaperCyclingTime,
|
||||||
"lastBrightnessDevice": lastBrightnessDevice,
|
"lastBrightnessDevice": lastBrightnessDevice
|
||||||
"notepadContent": notepadContent,
|
|
||||||
"notepadCurrentFileName": notepadCurrentFileName,
|
|
||||||
"notepadCurrentFileUrl": notepadCurrentFileUrl,
|
|
||||||
"notepadLastSavedContent": notepadLastSavedContent,
|
|
||||||
"notepadTabs": notepadTabs,
|
|
||||||
"notepadCurrentTabIndex": notepadCurrentTabIndex
|
|
||||||
}, null, 2))
|
}, null, 2))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -594,4 +548,4 @@ Singleton {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -18,79 +18,95 @@ PanelWindow {
|
|||||||
property bool isVisible: false
|
property bool isVisible: false
|
||||||
property bool fileDialogOpen: false
|
property bool fileDialogOpen: false
|
||||||
property string currentFileName: ""
|
property string currentFileName: ""
|
||||||
property bool hasUnsavedChanges: false
|
|
||||||
property url currentFileUrl
|
property url currentFileUrl
|
||||||
property var targetScreen: null
|
property var targetScreen: null
|
||||||
property var modelData: null
|
property var modelData: null
|
||||||
property bool confirmationDialogOpen: false
|
property bool confirmationDialogOpen: false
|
||||||
property string pendingAction: ""
|
property string pendingAction: ""
|
||||||
|
property url pendingFileUrl
|
||||||
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 var currentTab: NotepadStorageService.tabs.length > NotepadStorageService.currentTabIndex ? NotepadStorageService.tabs[NotepadStorageService.currentTabIndex] : null
|
||||||
property int nextTabId: Date.now()
|
property string currentContent: ""
|
||||||
|
property string lastSavedContent: ""
|
||||||
|
property bool contentLoaded: false
|
||||||
|
|
||||||
function hasFileChanges() {
|
function hasUnsavedChanges() {
|
||||||
if (!currentTab) return false
|
if (!currentTab || !contentLoaded) {
|
||||||
return currentTab.content !== currentTab.lastSavedContent
|
return false
|
||||||
}
|
|
||||||
|
|
||||||
function getCurrentTabData() {
|
|
||||||
return currentTab || {
|
|
||||||
id: 0,
|
|
||||||
title: "Untitled",
|
|
||||||
content: "",
|
|
||||||
fileName: "",
|
|
||||||
fileUrl: "",
|
|
||||||
lastSavedContent: "",
|
|
||||||
hasUnsavedChanges: false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// For temporary files, show unsaved if there's any content that hasn't been hard-saved
|
||||||
|
if (currentTab.isTemporary) {
|
||||||
|
return textArea.text.length > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// For non-temporary files, show unsaved if content differs from last saved state
|
||||||
|
return textArea.text !== lastSavedContent
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateCurrentTab(properties, saveImmediately = false) {
|
function hasUnsavedTemporaryContent() {
|
||||||
|
return hasUnsavedChanges()
|
||||||
|
}
|
||||||
|
|
||||||
|
function hasUnsavedChangesForTab(tab) {
|
||||||
|
if (!tab) return false
|
||||||
|
|
||||||
|
// Only the currently active tab can show real unsaved status
|
||||||
|
if (tab.id === currentTab?.id) {
|
||||||
|
return hasUnsavedChanges()
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
function loadCurrentTabContent() {
|
||||||
if (!currentTab) return
|
if (!currentTab) return
|
||||||
|
|
||||||
var tabs = [...SessionData.notepadTabs]
|
contentLoaded = false
|
||||||
var tabIndex = SessionData.notepadCurrentTabIndex
|
NotepadStorageService.loadTabContent(
|
||||||
|
NotepadStorageService.currentTabIndex,
|
||||||
if (tabIndex >= 0 && tabIndex < tabs.length) {
|
(content) => {
|
||||||
var updatedTab = Object.assign({}, tabs[tabIndex])
|
currentContent = content
|
||||||
Object.assign(updatedTab, properties)
|
lastSavedContent = content
|
||||||
tabs[tabIndex] = updatedTab
|
textArea.text = content
|
||||||
SessionData.notepadTabs = tabs
|
contentLoaded = true
|
||||||
|
|
||||||
if (saveImmediately) {
|
|
||||||
SessionData.saveSettings()
|
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function saveCurrentTabContent() {
|
||||||
|
if (!currentTab || !contentLoaded) return
|
||||||
|
|
||||||
|
NotepadStorageService.saveTabContent(
|
||||||
|
NotepadStorageService.currentTabIndex,
|
||||||
|
textArea.text
|
||||||
|
)
|
||||||
|
currentContent = textArea.text
|
||||||
|
lastSavedContent = textArea.text
|
||||||
|
}
|
||||||
|
|
||||||
|
function autoSaveToSession() {
|
||||||
|
if (!currentTab || !contentLoaded) return
|
||||||
|
|
||||||
|
currentContent = textArea.text
|
||||||
|
saveCurrentTabContent()
|
||||||
}
|
}
|
||||||
|
|
||||||
function createNewTab() {
|
function createNewTab() {
|
||||||
var newTab = {
|
performCreateNewTab()
|
||||||
id: ++nextTabId,
|
}
|
||||||
title: "Untitled",
|
|
||||||
content: "",
|
function performCreateNewTab() {
|
||||||
fileName: "",
|
NotepadStorageService.createNewTab()
|
||||||
fileUrl: "",
|
|
||||||
lastSavedContent: "",
|
|
||||||
hasUnsavedChanges: false
|
|
||||||
}
|
|
||||||
|
|
||||||
var tabs = [...SessionData.notepadTabs]
|
|
||||||
tabs.push(newTab)
|
|
||||||
SessionData.notepadTabs = tabs
|
|
||||||
SessionData.notepadCurrentTabIndex = tabs.length - 1
|
|
||||||
|
|
||||||
textArea.text = ""
|
textArea.text = ""
|
||||||
|
currentContent = ""
|
||||||
|
lastSavedContent = ""
|
||||||
|
contentLoaded = true
|
||||||
textArea.forceActiveFocus()
|
textArea.forceActiveFocus()
|
||||||
|
|
||||||
deferredSaveTimer.restart()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function closeTab(tabIndex) {
|
function closeTab(tabIndex) {
|
||||||
var tabToClose = SessionData.notepadTabs[tabIndex]
|
if (tabIndex === NotepadStorageService.currentTabIndex && hasUnsavedChanges()) {
|
||||||
var hasChanges = tabToClose && tabToClose.content !== tabToClose.lastSavedContent
|
|
||||||
|
|
||||||
if (hasChanges) {
|
|
||||||
root.pendingAction = "close_tab_" + tabIndex
|
root.pendingAction = "close_tab_" + tabIndex
|
||||||
root.confirmationDialogOpen = true
|
root.confirmationDialogOpen = true
|
||||||
confirmationDialog.open()
|
confirmationDialog.open()
|
||||||
@@ -100,54 +116,27 @@ PanelWindow {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function performCloseTab(tabIndex) {
|
function performCloseTab(tabIndex) {
|
||||||
var tabs = [...SessionData.notepadTabs]
|
NotepadStorageService.closeTab(tabIndex)
|
||||||
|
|
||||||
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(() => {
|
Qt.callLater(() => {
|
||||||
if (currentTab) {
|
loadCurrentTabContent()
|
||||||
textArea.text = currentTab.content
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
deferredSaveTimer.restart()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function switchToTab(tabIndex) {
|
function switchToTab(tabIndex) {
|
||||||
if (tabIndex < 0 || tabIndex >= SessionData.notepadTabs.length) return
|
if (tabIndex < 0 || tabIndex >= NotepadStorageService.tabs.length) return
|
||||||
|
|
||||||
SessionData.notepadCurrentTabIndex = tabIndex
|
if (contentLoaded) {
|
||||||
|
autoSaveToSession()
|
||||||
|
}
|
||||||
|
|
||||||
|
NotepadStorageService.switchToTab(tabIndex)
|
||||||
Qt.callLater(() => {
|
Qt.callLater(() => {
|
||||||
|
loadCurrentTabContent()
|
||||||
if (currentTab) {
|
if (currentTab) {
|
||||||
textArea.text = currentTab.content
|
root.currentFileName = currentTab.fileName || ""
|
||||||
root.currentFileName = currentTab.fileName
|
root.currentFileUrl = currentTab.fileUrl || ""
|
||||||
root.currentFileUrl = currentTab.fileUrl
|
|
||||||
root.lastSavedFileContent = currentTab.lastSavedContent
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
deferredSaveTimer.restart()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function show() {
|
function show() {
|
||||||
@@ -303,18 +292,17 @@ PanelWindow {
|
|||||||
spacing: Theme.spacingXS
|
spacing: Theme.spacingXS
|
||||||
|
|
||||||
Repeater {
|
Repeater {
|
||||||
model: SessionData.notepadTabs
|
model: NotepadStorageService.tabs
|
||||||
|
|
||||||
delegate: Rectangle {
|
delegate: Rectangle {
|
||||||
required property int index
|
required property int index
|
||||||
required property var modelData
|
required property var modelData
|
||||||
|
|
||||||
readonly property bool isActive: SessionData.notepadCurrentTabIndex === index
|
readonly property bool isActive: NotepadStorageService.currentTabIndex === index
|
||||||
readonly property bool tabHasChanges: modelData.content !== modelData.lastSavedContent
|
|
||||||
readonly property bool isHovered: tabMouseArea.containsMouse && !closeMouseArea.containsMouse
|
readonly property bool isHovered: tabMouseArea.containsMouse && !closeMouseArea.containsMouse
|
||||||
readonly property real calculatedWidth: {
|
readonly property real calculatedWidth: {
|
||||||
const textWidth = tabText.paintedWidth || 100
|
const textWidth = tabText.paintedWidth || 100
|
||||||
const closeButtonWidth = SessionData.notepadTabs.length > 1 ? 20 : 0
|
const closeButtonWidth = NotepadStorageService.tabs.length > 1 ? 20 : 0
|
||||||
const spacing = Theme.spacingXS
|
const spacing = Theme.spacingXS
|
||||||
const padding = Theme.spacingM * 2
|
const padding = Theme.spacingM * 2
|
||||||
return Math.max(120, Math.min(200, textWidth + closeButtonWidth + spacing + padding))
|
return Math.max(120, Math.min(200, textWidth + closeButtonWidth + spacing + padding))
|
||||||
@@ -344,7 +332,13 @@ PanelWindow {
|
|||||||
|
|
||||||
StyledText {
|
StyledText {
|
||||||
id: tabText
|
id: tabText
|
||||||
text: (tabHasChanges ? "● " : "") + (modelData.title || "Untitled")
|
text: {
|
||||||
|
var prefix = ""
|
||||||
|
if (hasUnsavedChangesForTab(modelData)) {
|
||||||
|
prefix = "● "
|
||||||
|
}
|
||||||
|
return prefix + (modelData.title || "Untitled")
|
||||||
|
}
|
||||||
font.pixelSize: Theme.fontSizeSmall
|
font.pixelSize: Theme.fontSizeSmall
|
||||||
color: isActive ? Theme.primary : Theme.surfaceText
|
color: isActive ? Theme.primary : Theme.surfaceText
|
||||||
font.weight: isActive ? Font.Medium : Font.Normal
|
font.weight: isActive ? Font.Medium : Font.Normal
|
||||||
@@ -359,7 +353,7 @@ PanelWindow {
|
|||||||
height: 20
|
height: 20
|
||||||
radius: 10
|
radius: 10
|
||||||
color: closeMouseArea.containsMouse ? Theme.surfaceTextHover : "transparent"
|
color: closeMouseArea.containsMouse ? Theme.surfaceTextHover : "transparent"
|
||||||
visible: SessionData.notepadTabs.length > 1
|
visible: NotepadStorageService.tabs.length > 1
|
||||||
anchors.verticalCenter: parent.verticalCenter
|
anchors.verticalCenter: parent.verticalCenter
|
||||||
|
|
||||||
DankIcon {
|
DankIcon {
|
||||||
@@ -432,6 +426,7 @@ PanelWindow {
|
|||||||
focus: root.isVisible
|
focus: root.isVisible
|
||||||
activeFocusOnTab: true
|
activeFocusOnTab: true
|
||||||
textFormat: TextEdit.PlainText
|
textFormat: TextEdit.PlainText
|
||||||
|
inputMethodHints: Qt.ImhNoPredictiveText | Qt.ImhNoAutoUppercase
|
||||||
persistentSelection: true
|
persistentSelection: true
|
||||||
tabStopDistance: 40
|
tabStopDistance: 40
|
||||||
leftPadding: Theme.spacingM
|
leftPadding: Theme.spacingM
|
||||||
@@ -440,32 +435,35 @@ PanelWindow {
|
|||||||
bottomPadding: Theme.spacingM
|
bottomPadding: Theme.spacingM
|
||||||
|
|
||||||
Component.onCompleted: {
|
Component.onCompleted: {
|
||||||
|
loadCurrentTabContent()
|
||||||
if (currentTab) {
|
if (currentTab) {
|
||||||
text = currentTab.content
|
root.currentFileName = currentTab.fileName || ""
|
||||||
root.currentFileName = currentTab.fileName
|
root.currentFileUrl = currentTab.fileUrl || ""
|
||||||
root.currentFileUrl = currentTab.fileUrl
|
|
||||||
root.lastSavedFileContent = currentTab.lastSavedContent
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Connections {
|
Connections {
|
||||||
target: root
|
target: NotepadStorageService
|
||||||
function onCurrentTabChanged() {
|
function onCurrentTabIndexChanged() {
|
||||||
if (currentTab && textArea.text !== currentTab.content) {
|
loadCurrentTabContent()
|
||||||
textArea.text = currentTab.content
|
if (currentTab) {
|
||||||
root.currentFileName = currentTab.fileName
|
root.currentFileName = currentTab.fileName || ""
|
||||||
root.currentFileUrl = currentTab.fileUrl
|
root.currentFileUrl = currentTab.fileUrl || ""
|
||||||
root.lastSavedFileContent = currentTab.lastSavedContent
|
}
|
||||||
|
}
|
||||||
|
function onTabsChanged() {
|
||||||
|
if (NotepadStorageService.tabs.length > 0 && !contentLoaded) {
|
||||||
|
loadCurrentTabContent()
|
||||||
|
if (currentTab) {
|
||||||
|
root.currentFileName = currentTab.fileName || ""
|
||||||
|
root.currentFileUrl = currentTab.fileUrl || ""
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onTextChanged: {
|
onTextChanged: {
|
||||||
if (currentTab && text !== currentTab.content) {
|
if (contentLoaded && text !== lastSavedContent) {
|
||||||
updateCurrentTab({
|
|
||||||
content: text,
|
|
||||||
hasUnsavedChanges: true
|
|
||||||
})
|
|
||||||
autoSaveTimer.restart()
|
autoSaveTimer.restart()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -480,16 +478,19 @@ PanelWindow {
|
|||||||
switch (event.key) {
|
switch (event.key) {
|
||||||
case Qt.Key_S:
|
case Qt.Key_S:
|
||||||
event.accepted = true
|
event.accepted = true
|
||||||
if (currentTab && currentTab.fileUrl) {
|
if (currentTab && !currentTab.isTemporary && currentTab.filePath) {
|
||||||
saveToFile(currentTab.fileUrl)
|
// For non-temporary tabs, save directly to the original file
|
||||||
|
var fileUrl = "file://" + currentTab.filePath
|
||||||
|
saveToFile(fileUrl)
|
||||||
} else {
|
} else {
|
||||||
|
// For temporary tabs or new files, open save dialog
|
||||||
root.fileDialogOpen = true
|
root.fileDialogOpen = true
|
||||||
saveBrowser.open()
|
saveBrowser.open()
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
case Qt.Key_O:
|
case Qt.Key_O:
|
||||||
event.accepted = true
|
event.accepted = true
|
||||||
if (hasFileChanges()) {
|
if (hasUnsavedChanges()) {
|
||||||
root.pendingAction = "open"
|
root.pendingAction = "open"
|
||||||
root.confirmationDialogOpen = true
|
root.confirmationDialogOpen = true
|
||||||
confirmationDialog.open()
|
confirmationDialog.open()
|
||||||
@@ -500,7 +501,7 @@ PanelWindow {
|
|||||||
break
|
break
|
||||||
case Qt.Key_N:
|
case Qt.Key_N:
|
||||||
event.accepted = true
|
event.accepted = true
|
||||||
if (hasFileChanges()) {
|
if (hasUnsavedChanges()) {
|
||||||
root.pendingAction = "new"
|
root.pendingAction = "new"
|
||||||
root.confirmationDialogOpen = true
|
root.confirmationDialogOpen = true
|
||||||
confirmationDialog.open()
|
confirmationDialog.open()
|
||||||
@@ -538,7 +539,7 @@ PanelWindow {
|
|||||||
iconName: "save"
|
iconName: "save"
|
||||||
iconSize: Theme.iconSize - 2
|
iconSize: Theme.iconSize - 2
|
||||||
iconColor: Theme.primary
|
iconColor: Theme.primary
|
||||||
enabled: currentTab && (hasFileChanges() || currentTab.content.length > 0)
|
enabled: currentTab && (hasUnsavedChanges() || textArea.text.length > 0)
|
||||||
onClicked: {
|
onClicked: {
|
||||||
root.fileDialogOpen = true
|
root.fileDialogOpen = true
|
||||||
saveBrowser.open()
|
saveBrowser.open()
|
||||||
@@ -559,7 +560,7 @@ PanelWindow {
|
|||||||
iconSize: Theme.iconSize - 2
|
iconSize: Theme.iconSize - 2
|
||||||
iconColor: Theme.secondary
|
iconColor: Theme.secondary
|
||||||
onClicked: {
|
onClicked: {
|
||||||
if (hasFileChanges()) {
|
if (hasUnsavedChanges()) {
|
||||||
root.pendingAction = "open"
|
root.pendingAction = "open"
|
||||||
root.confirmationDialogOpen = true
|
root.confirmationDialogOpen = true
|
||||||
confirmationDialog.open()
|
confirmationDialog.open()
|
||||||
@@ -599,7 +600,7 @@ PanelWindow {
|
|||||||
spacing: Theme.spacingL
|
spacing: Theme.spacingL
|
||||||
|
|
||||||
StyledText {
|
StyledText {
|
||||||
text: currentTab && currentTab.content.length > 0 ? qsTr("%1 characters").arg(currentTab.content.length) : qsTr("Empty")
|
text: textArea.text.length > 0 ? qsTr("%1 characters").arg(textArea.text.length) : qsTr("Empty")
|
||||||
font.pixelSize: Theme.fontSizeSmall
|
font.pixelSize: Theme.fontSizeSmall
|
||||||
color: Theme.surfaceTextMedium
|
color: Theme.surfaceTextMedium
|
||||||
}
|
}
|
||||||
@@ -608,14 +609,38 @@ 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: currentTab && currentTab.content.length > 0
|
visible: textArea.text.length > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
StyledText {
|
StyledText {
|
||||||
text: autoSaveTimer.running ? qsTr("Auto-saving...") : (hasFileChanges() ? qsTr("Unsaved changes") : qsTr("Auto-saved"))
|
text: {
|
||||||
|
if (autoSaveTimer.running) {
|
||||||
|
return qsTr("Auto-saving...")
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hasUnsavedChanges()) {
|
||||||
|
if (currentTab && currentTab.isTemporary) {
|
||||||
|
return qsTr("Unsaved note...")
|
||||||
|
} else {
|
||||||
|
return qsTr("Unsaved changes")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return qsTr("Saved")
|
||||||
|
}
|
||||||
|
}
|
||||||
font.pixelSize: Theme.fontSizeSmall
|
font.pixelSize: Theme.fontSizeSmall
|
||||||
color: hasFileChanges() ? Theme.warning : (autoSaveTimer.running ? Theme.primary : Theme.surfaceTextMedium)
|
color: {
|
||||||
opacity: currentTab && currentTab.content.length > 0 ? 1 : 0
|
if (autoSaveTimer.running) {
|
||||||
|
return Theme.primary
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hasUnsavedChanges()) {
|
||||||
|
return Theme.warning
|
||||||
|
} else {
|
||||||
|
return Theme.success
|
||||||
|
}
|
||||||
|
}
|
||||||
|
opacity: textArea.text.length > 0 ? 1 : 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -627,30 +652,18 @@ PanelWindow {
|
|||||||
interval: 2000
|
interval: 2000
|
||||||
repeat: false
|
repeat: false
|
||||||
onTriggered: {
|
onTriggered: {
|
||||||
if (currentTab) {
|
autoSaveToSession()
|
||||||
updateCurrentTab({
|
|
||||||
hasUnsavedChanges: false
|
|
||||||
}, true)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Timer {
|
|
||||||
id: deferredSaveTimer
|
|
||||||
interval: 500
|
|
||||||
repeat: false
|
|
||||||
onTriggered: {
|
|
||||||
SessionData.saveSettings()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
property string pendingSaveContent: ""
|
property string pendingSaveContent: ""
|
||||||
|
|
||||||
function saveToFile(fileUrl) {
|
function saveToFile(fileUrl) {
|
||||||
if (!currentTab) return
|
if (!currentTab) return
|
||||||
|
|
||||||
const content = currentTab.content
|
var content = textArea.text
|
||||||
const filePath = fileUrl.toString().replace(/^file:\/\//, '')
|
var filePath = fileUrl.toString().replace(/^file:\/\//, '')
|
||||||
|
|
||||||
saveFileView.path = ""
|
saveFileView.path = ""
|
||||||
pendingSaveContent = content
|
pendingSaveContent = content
|
||||||
@@ -660,8 +673,22 @@ PanelWindow {
|
|||||||
Qt.callLater(() => {
|
Qt.callLater(() => {
|
||||||
saveFileView.setText(pendingSaveContent)
|
saveFileView.setText(pendingSaveContent)
|
||||||
})
|
})
|
||||||
} function loadFromFile(fileUrl) {
|
}
|
||||||
|
|
||||||
|
function loadFromFile(fileUrl) {
|
||||||
|
if (hasUnsavedTemporaryContent()) {
|
||||||
|
root.pendingFileUrl = fileUrl
|
||||||
|
root.pendingAction = "load_file"
|
||||||
|
root.confirmationDialogOpen = true
|
||||||
|
confirmationDialog.open()
|
||||||
|
} else {
|
||||||
|
performLoadFromFile(fileUrl)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function performLoadFromFile(fileUrl) {
|
||||||
const filePath = fileUrl.toString().replace(/^file:\/\//, '')
|
const filePath = fileUrl.toString().replace(/^file:\/\//, '')
|
||||||
|
const fileName = filePath.split('/').pop()
|
||||||
|
|
||||||
loadFileView.path = ""
|
loadFileView.path = ""
|
||||||
loadFileView.path = filePath
|
loadFileView.path = filePath
|
||||||
@@ -669,19 +696,25 @@ PanelWindow {
|
|||||||
// Wait for the file to be loaded before reading
|
// Wait for the file to be loaded before reading
|
||||||
if (loadFileView.waitForJob()) {
|
if (loadFileView.waitForJob()) {
|
||||||
Qt.callLater(() => {
|
Qt.callLater(() => {
|
||||||
const content = loadFileView.text()
|
var content = loadFileView.text()
|
||||||
if (currentTab && content !== undefined && content !== null) {
|
if (currentTab && content !== undefined && content !== null) {
|
||||||
updateCurrentTab({
|
|
||||||
content: content,
|
|
||||||
hasUnsavedChanges: false,
|
|
||||||
lastSavedContent: content
|
|
||||||
}, true)
|
|
||||||
textArea.text = content
|
textArea.text = content
|
||||||
|
currentContent = content
|
||||||
|
lastSavedContent = content
|
||||||
|
contentLoaded = true
|
||||||
root.lastSavedFileContent = content
|
root.lastSavedFileContent = content
|
||||||
|
|
||||||
|
NotepadStorageService.updateTabMetadata(NotepadStorageService.currentTabIndex, {
|
||||||
|
title: fileName,
|
||||||
|
filePath: filePath,
|
||||||
|
isTemporary: false
|
||||||
|
})
|
||||||
|
|
||||||
|
root.currentFileName = fileName
|
||||||
|
root.currentFileUrl = fileUrl
|
||||||
|
saveCurrentTabContent()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
} else {
|
|
||||||
console.warn("Notepad: Failed to load file - waitForJob returned false")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -694,17 +727,16 @@ PanelWindow {
|
|||||||
|
|
||||||
onSaved: {
|
onSaved: {
|
||||||
if (currentTab && saveFileView.path && pendingSaveContent) {
|
if (currentTab && saveFileView.path && pendingSaveContent) {
|
||||||
updateCurrentTab({
|
NotepadStorageService.updateTabMetadata(NotepadStorageService.currentTabIndex, {
|
||||||
hasUnsavedChanges: false,
|
hasUnsavedChanges: false,
|
||||||
lastSavedContent: pendingSaveContent
|
lastSavedContent: pendingSaveContent
|
||||||
}, true)
|
})
|
||||||
root.lastSavedFileContent = pendingSaveContent
|
root.lastSavedFileContent = pendingSaveContent
|
||||||
pendingSaveContent = ""
|
pendingSaveContent = ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onSaveFailed: (error) => {
|
onSaveFailed: (error) => {
|
||||||
console.warn("Notepad: Failed to save file:", error, "Path:", saveFileView.path)
|
|
||||||
pendingSaveContent = ""
|
pendingSaveContent = ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -717,7 +749,6 @@ PanelWindow {
|
|||||||
printErrors: true
|
printErrors: true
|
||||||
|
|
||||||
onLoadFailed: (error) => {
|
onLoadFailed: (error) => {
|
||||||
console.warn("Notepad: Failed to load file:", error)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -730,7 +761,16 @@ PanelWindow {
|
|||||||
fileExtensions: ["*.txt", "*.md", "*.*"]
|
fileExtensions: ["*.txt", "*.md", "*.*"]
|
||||||
allowStacking: true
|
allowStacking: true
|
||||||
saveMode: true
|
saveMode: true
|
||||||
defaultFileName: (currentTab && currentTab.fileName) || "note.txt"
|
defaultFileName: {
|
||||||
|
if (currentTab && currentTab.title && currentTab.title !== "Untitled") {
|
||||||
|
return currentTab.title
|
||||||
|
} else if (currentTab && !currentTab.isTemporary && currentTab.filePath) {
|
||||||
|
// Extract filename from path for non-temporary files
|
||||||
|
return currentTab.filePath.split('/').pop()
|
||||||
|
} else {
|
||||||
|
return "note.txt"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
WlrLayershell.layer: WlrLayershell.Overlay
|
WlrLayershell.layer: WlrLayershell.Overlay
|
||||||
|
|
||||||
@@ -742,15 +782,14 @@ PanelWindow {
|
|||||||
|
|
||||||
root.currentFileName = fileName
|
root.currentFileName = fileName
|
||||||
root.currentFileUrl = fileUrl
|
root.currentFileUrl = fileUrl
|
||||||
|
|
||||||
if (currentTab) {
|
if (currentTab) {
|
||||||
updateCurrentTab({
|
NotepadStorageService.saveTabAs(
|
||||||
title: fileName,
|
NotepadStorageService.currentTabIndex,
|
||||||
fileName: fileName,
|
cleanPath
|
||||||
fileUrl: fileUrl
|
)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
saveToFile(fileUrl)
|
saveToFile(fileUrl)
|
||||||
|
|
||||||
if (root.pendingAction === "new") {
|
if (root.pendingAction === "new") {
|
||||||
@@ -798,14 +837,6 @@ 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()
|
||||||
}
|
}
|
||||||
@@ -863,7 +894,9 @@ PanelWindow {
|
|||||||
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_") ?
|
root.pendingAction.startsWith("close_tab_") ?
|
||||||
qsTr("You have unsaved changes. Save before closing this tab?") :
|
qsTr("You have unsaved changes. Save before closing this tab?") :
|
||||||
qsTr("You have unsaved changes. Save before opening a file?")
|
root.pendingAction === "load_file" || root.pendingAction === "open" ?
|
||||||
|
qsTr("You have unsaved changes. Save before opening a file?") :
|
||||||
|
qsTr("You have unsaved changes. Save before continuing?")
|
||||||
font.pixelSize: Theme.fontSizeMedium
|
font.pixelSize: Theme.fontSizeMedium
|
||||||
color: Theme.surfaceTextMedium
|
color: Theme.surfaceTextMedium
|
||||||
width: parent.width
|
width: parent.width
|
||||||
@@ -921,11 +954,14 @@ PanelWindow {
|
|||||||
} else if (root.pendingAction === "open") {
|
} else if (root.pendingAction === "open") {
|
||||||
root.fileDialogOpen = true
|
root.fileDialogOpen = true
|
||||||
loadBrowser.open()
|
loadBrowser.open()
|
||||||
|
} else if (root.pendingAction === "load_file") {
|
||||||
|
performLoadFromFile(root.pendingFileUrl)
|
||||||
} else if (root.pendingAction.startsWith("close_tab_")) {
|
} else if (root.pendingAction.startsWith("close_tab_")) {
|
||||||
var tabIndex = parseInt(root.pendingAction.split("_")[2])
|
var tabIndex = parseInt(root.pendingAction.split("_")[2])
|
||||||
performCloseTab(tabIndex)
|
performCloseTab(tabIndex)
|
||||||
}
|
}
|
||||||
root.pendingAction = ""
|
root.pendingAction = ""
|
||||||
|
root.pendingFileUrl = ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import QtQuick
|
import QtQuick
|
||||||
import qs.Common
|
import qs.Common
|
||||||
|
import qs.Services
|
||||||
import qs.Widgets
|
import qs.Widgets
|
||||||
|
|
||||||
Rectangle {
|
Rectangle {
|
||||||
@@ -45,7 +46,7 @@ Rectangle {
|
|||||||
anchors.top: parent.top
|
anchors.top: parent.top
|
||||||
anchors.rightMargin: SettingsData.topBarNoBackground ? 0 : 4
|
anchors.rightMargin: SettingsData.topBarNoBackground ? 0 : 4
|
||||||
anchors.topMargin: SettingsData.topBarNoBackground ? 0 : 4
|
anchors.topMargin: SettingsData.topBarNoBackground ? 0 : 4
|
||||||
visible: SessionData.notepadContent.length > 0
|
visible: NotepadStorageService.tabs && NotepadStorageService.tabs.length > 0
|
||||||
opacity: 0.8
|
opacity: 0.8
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,4 +69,4 @@ Rectangle {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
395
Services/NotepadStorageService.qml
Normal file
395
Services/NotepadStorageService.qml
Normal file
@@ -0,0 +1,395 @@
|
|||||||
|
import QtQuick
|
||||||
|
import QtCore
|
||||||
|
import Quickshell
|
||||||
|
import Quickshell.Io
|
||||||
|
pragma Singleton
|
||||||
|
pragma ComponentBehavior: Bound
|
||||||
|
|
||||||
|
Singleton {
|
||||||
|
id: root
|
||||||
|
|
||||||
|
readonly property string baseDir: StandardPaths.writableLocation(StandardPaths.GenericStateLocation) + "/DankMaterialShell"
|
||||||
|
readonly property string filesDir: baseDir + "/notepad-files"
|
||||||
|
readonly property string metadataPath: baseDir + "/notepad-session.json"
|
||||||
|
|
||||||
|
property var tabs: []
|
||||||
|
property int currentTabIndex: 0
|
||||||
|
property var tabsBeingCreated: ({})
|
||||||
|
|
||||||
|
Component.onCompleted: {
|
||||||
|
ensureDirectoryProcess.running = true
|
||||||
|
}
|
||||||
|
|
||||||
|
Process {
|
||||||
|
id: ensureDirectoryProcess
|
||||||
|
command: ["mkdir", "-p", root.filesDir]
|
||||||
|
onExited: loadMetadata()
|
||||||
|
}
|
||||||
|
|
||||||
|
FileView {
|
||||||
|
id: metadataFile
|
||||||
|
path: root.metadataPath
|
||||||
|
blockWrites: true
|
||||||
|
atomicWrites: true
|
||||||
|
|
||||||
|
onLoaded: {
|
||||||
|
try {
|
||||||
|
var data = JSON.parse(text())
|
||||||
|
root.tabs = data.tabs || []
|
||||||
|
root.currentTabIndex = data.currentTabIndex || 0
|
||||||
|
validateTabs()
|
||||||
|
} catch(e) {
|
||||||
|
console.warn("Failed to parse notepad metadata:", e)
|
||||||
|
createDefaultTab()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
onLoadFailed: {
|
||||||
|
createDefaultTab()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function loadMetadata() {
|
||||||
|
metadataFile.path = ""
|
||||||
|
metadataFile.path = root.metadataPath
|
||||||
|
}
|
||||||
|
|
||||||
|
function createDefaultTab() {
|
||||||
|
var id = Date.now()
|
||||||
|
var filePath = "notepad-files/untitled-" + id + ".txt"
|
||||||
|
var fullPath = baseDir + "/" + filePath
|
||||||
|
|
||||||
|
var newTabsBeingCreated = Object.assign({}, tabsBeingCreated)
|
||||||
|
newTabsBeingCreated[id] = true
|
||||||
|
tabsBeingCreated = newTabsBeingCreated
|
||||||
|
|
||||||
|
createEmptyFile(fullPath, function() {
|
||||||
|
root.tabs = [{
|
||||||
|
id: id,
|
||||||
|
title: "Untitled",
|
||||||
|
filePath: filePath,
|
||||||
|
isTemporary: true,
|
||||||
|
lastModified: new Date().toISOString(),
|
||||||
|
cursorPosition: 0,
|
||||||
|
scrollPosition: 0
|
||||||
|
}]
|
||||||
|
root.currentTabIndex = 0
|
||||||
|
|
||||||
|
var updatedTabsBeingCreated = Object.assign({}, tabsBeingCreated)
|
||||||
|
delete updatedTabsBeingCreated[id]
|
||||||
|
tabsBeingCreated = updatedTabsBeingCreated
|
||||||
|
saveMetadata()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function saveMetadata() {
|
||||||
|
var metadata = {
|
||||||
|
version: 1,
|
||||||
|
currentTabIndex: currentTabIndex,
|
||||||
|
tabs: tabs
|
||||||
|
}
|
||||||
|
metadataFile.setText(JSON.stringify(metadata, null, 2))
|
||||||
|
}
|
||||||
|
|
||||||
|
function loadTabContent(tabIndex, callback) {
|
||||||
|
if (tabIndex < 0 || tabIndex >= tabs.length) {
|
||||||
|
callback("")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var tab = tabs[tabIndex]
|
||||||
|
var fullPath = tab.isTemporary
|
||||||
|
? baseDir + "/" + tab.filePath
|
||||||
|
: tab.filePath
|
||||||
|
|
||||||
|
if (tabsBeingCreated[tab.id]) {
|
||||||
|
Qt.callLater(() => {
|
||||||
|
loadTabContent(tabIndex, callback)
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var loader = tabFileLoaderComponent.createObject(root, {
|
||||||
|
path: fullPath,
|
||||||
|
callback: callback
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function saveTabContent(tabIndex, content) {
|
||||||
|
if (tabIndex < 0 || tabIndex >= tabs.length) return
|
||||||
|
|
||||||
|
var tab = tabs[tabIndex]
|
||||||
|
var fullPath = tab.isTemporary
|
||||||
|
? baseDir + "/" + tab.filePath
|
||||||
|
: tab.filePath
|
||||||
|
|
||||||
|
var saver = tabFileSaverComponent.createObject(root, {
|
||||||
|
path: fullPath,
|
||||||
|
content: content,
|
||||||
|
tabIndex: tabIndex
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function createNewTab() {
|
||||||
|
var id = Date.now()
|
||||||
|
var filePath = "notepad-files/untitled-" + id + ".txt"
|
||||||
|
var fullPath = baseDir + "/" + filePath
|
||||||
|
|
||||||
|
var newTab = {
|
||||||
|
id: id,
|
||||||
|
title: "Untitled",
|
||||||
|
filePath: filePath,
|
||||||
|
isTemporary: true,
|
||||||
|
lastModified: new Date().toISOString(),
|
||||||
|
cursorPosition: 0,
|
||||||
|
scrollPosition: 0
|
||||||
|
}
|
||||||
|
|
||||||
|
var newTabsBeingCreated = Object.assign({}, tabsBeingCreated)
|
||||||
|
newTabsBeingCreated[id] = true
|
||||||
|
tabsBeingCreated = newTabsBeingCreated
|
||||||
|
createEmptyFile(fullPath, function() {
|
||||||
|
var newTabs = tabs.slice()
|
||||||
|
newTabs.push(newTab)
|
||||||
|
tabs = newTabs
|
||||||
|
currentTabIndex = tabs.length - 1
|
||||||
|
|
||||||
|
var updatedTabsBeingCreated = Object.assign({}, tabsBeingCreated)
|
||||||
|
delete updatedTabsBeingCreated[id]
|
||||||
|
tabsBeingCreated = updatedTabsBeingCreated
|
||||||
|
saveMetadata()
|
||||||
|
})
|
||||||
|
|
||||||
|
return newTab
|
||||||
|
}
|
||||||
|
|
||||||
|
function closeTab(tabIndex) {
|
||||||
|
if (tabIndex < 0 || tabIndex >= tabs.length) return
|
||||||
|
|
||||||
|
var newTabs = tabs.slice()
|
||||||
|
|
||||||
|
if (newTabs.length <= 1) {
|
||||||
|
var id = Date.now()
|
||||||
|
var filePath = "notepad-files/untitled-" + id + ".txt"
|
||||||
|
|
||||||
|
var newTabsBeingCreated = Object.assign({}, tabsBeingCreated)
|
||||||
|
newTabsBeingCreated[id] = true
|
||||||
|
tabsBeingCreated = newTabsBeingCreated
|
||||||
|
createEmptyFile(baseDir + "/" + filePath, function() {
|
||||||
|
newTabs[0] = {
|
||||||
|
id: id,
|
||||||
|
title: "Untitled",
|
||||||
|
filePath: filePath,
|
||||||
|
isTemporary: true,
|
||||||
|
lastModified: new Date().toISOString(),
|
||||||
|
cursorPosition: 0,
|
||||||
|
scrollPosition: 0
|
||||||
|
}
|
||||||
|
currentTabIndex = 0
|
||||||
|
tabs = newTabs
|
||||||
|
|
||||||
|
var updatedTabsBeingCreated = Object.assign({}, tabsBeingCreated)
|
||||||
|
delete updatedTabsBeingCreated[id]
|
||||||
|
tabsBeingCreated = updatedTabsBeingCreated
|
||||||
|
saveMetadata()
|
||||||
|
})
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
var tabToDelete = newTabs[tabIndex]
|
||||||
|
if (tabToDelete && tabToDelete.isTemporary) {
|
||||||
|
deleteFile(baseDir + "/" + tabToDelete.filePath)
|
||||||
|
}
|
||||||
|
|
||||||
|
newTabs.splice(tabIndex, 1)
|
||||||
|
if (currentTabIndex >= newTabs.length) {
|
||||||
|
currentTabIndex = newTabs.length - 1
|
||||||
|
} else if (currentTabIndex > tabIndex) {
|
||||||
|
currentTabIndex -= 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tabs = newTabs
|
||||||
|
saveMetadata()
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function switchToTab(tabIndex) {
|
||||||
|
if (tabIndex < 0 || tabIndex >= tabs.length) return
|
||||||
|
|
||||||
|
currentTabIndex = tabIndex
|
||||||
|
saveMetadata()
|
||||||
|
}
|
||||||
|
|
||||||
|
function saveTabAs(tabIndex, userPath) {
|
||||||
|
if (tabIndex < 0 || tabIndex >= tabs.length) return
|
||||||
|
|
||||||
|
var tab = tabs[tabIndex]
|
||||||
|
var fileName = userPath.split('/').pop()
|
||||||
|
|
||||||
|
if (tab.isTemporary) {
|
||||||
|
var tempPath = baseDir + "/" + tab.filePath
|
||||||
|
copyFile(tempPath, userPath)
|
||||||
|
deleteFile(tempPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
var newTabs = tabs.slice()
|
||||||
|
newTabs[tabIndex] = Object.assign({}, tab, {
|
||||||
|
title: fileName,
|
||||||
|
filePath: userPath,
|
||||||
|
isTemporary: false,
|
||||||
|
lastModified: new Date().toISOString()
|
||||||
|
})
|
||||||
|
tabs = newTabs
|
||||||
|
saveMetadata()
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateTabMetadata(tabIndex, properties) {
|
||||||
|
if (tabIndex < 0 || tabIndex >= tabs.length) return
|
||||||
|
|
||||||
|
var newTabs = tabs.slice()
|
||||||
|
var updatedTab = Object.assign({}, newTabs[tabIndex], properties)
|
||||||
|
updatedTab.lastModified = new Date().toISOString()
|
||||||
|
newTabs[tabIndex] = updatedTab
|
||||||
|
tabs = newTabs
|
||||||
|
saveMetadata()
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function validateTabs() {
|
||||||
|
var validTabs = []
|
||||||
|
for (var i = 0; i < tabs.length; i++) {
|
||||||
|
var tab = tabs[i]
|
||||||
|
validTabs.push(tab)
|
||||||
|
}
|
||||||
|
tabs = validTabs
|
||||||
|
|
||||||
|
if (tabs.length === 0) {
|
||||||
|
createDefaultTab()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Component {
|
||||||
|
id: tabFileLoaderComponent
|
||||||
|
FileView {
|
||||||
|
property var callback
|
||||||
|
blockLoading: true
|
||||||
|
preload: true
|
||||||
|
|
||||||
|
onLoaded: {
|
||||||
|
callback(text())
|
||||||
|
destroy()
|
||||||
|
}
|
||||||
|
|
||||||
|
onLoadFailed: {
|
||||||
|
callback("")
|
||||||
|
destroy()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Component {
|
||||||
|
id: tabFileSaverComponent
|
||||||
|
FileView {
|
||||||
|
property string content
|
||||||
|
property int tabIndex
|
||||||
|
property var creationCallback
|
||||||
|
|
||||||
|
blockWrites: false
|
||||||
|
atomicWrites: true
|
||||||
|
|
||||||
|
Component.onCompleted: setText(content)
|
||||||
|
|
||||||
|
onSaved: {
|
||||||
|
if (tabIndex >= 0) {
|
||||||
|
updateTabMetadata(tabIndex, {})
|
||||||
|
}
|
||||||
|
if (creationCallback) {
|
||||||
|
creationCallback()
|
||||||
|
}
|
||||||
|
destroy()
|
||||||
|
}
|
||||||
|
|
||||||
|
onSaveFailed: {
|
||||||
|
console.error("Failed to save tab content")
|
||||||
|
if (creationCallback) {
|
||||||
|
creationCallback()
|
||||||
|
}
|
||||||
|
destroy()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function createEmptyFile(path, callback) {
|
||||||
|
// Ensure path is a local file path, not a URL
|
||||||
|
var cleanPath = path.toString()
|
||||||
|
if (cleanPath.startsWith("file://")) {
|
||||||
|
cleanPath = cleanPath.substring(7)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate the cleaned path is absolute and in the right location
|
||||||
|
if (!cleanPath.startsWith("/")) {
|
||||||
|
cleanPath = baseDir + "/" + cleanPath
|
||||||
|
}
|
||||||
|
|
||||||
|
var creator = fileCreatorComponent.createObject(root, {
|
||||||
|
filePath: cleanPath,
|
||||||
|
creationCallback: callback
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function copyFile(source, destination) {
|
||||||
|
copyProcess.source = source
|
||||||
|
copyProcess.destination = destination
|
||||||
|
copyProcess.running = true
|
||||||
|
}
|
||||||
|
|
||||||
|
function deleteFile(path) {
|
||||||
|
deleteProcess.filePath = path
|
||||||
|
deleteProcess.running = true
|
||||||
|
}
|
||||||
|
|
||||||
|
Component {
|
||||||
|
id: fileCreatorComponent
|
||||||
|
QtObject {
|
||||||
|
property string filePath
|
||||||
|
property var creationCallback
|
||||||
|
|
||||||
|
Component.onCompleted: {
|
||||||
|
var touchProcess = touchProcessComponent.createObject(this, {
|
||||||
|
filePath: filePath,
|
||||||
|
callback: creationCallback
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Component {
|
||||||
|
id: touchProcessComponent
|
||||||
|
Process {
|
||||||
|
property string filePath
|
||||||
|
property var callback
|
||||||
|
command: ["touch", filePath]
|
||||||
|
|
||||||
|
Component.onCompleted: running = true
|
||||||
|
|
||||||
|
onExited: (exitCode) => {
|
||||||
|
if (callback) callback()
|
||||||
|
destroy()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Process {
|
||||||
|
id: copyProcess
|
||||||
|
property string source
|
||||||
|
property string destination
|
||||||
|
command: ["cp", source, destination]
|
||||||
|
}
|
||||||
|
|
||||||
|
Process {
|
||||||
|
id: deleteProcess
|
||||||
|
property string filePath
|
||||||
|
command: ["rm", "-f", filePath]
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user