Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import org.jetbrains.compose.desktop.application.dsl.TargetFormat
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
kotlin("jvm") version "1.7.0"
id("org.jetbrains.compose") version "1.2.0-alpha01-dev755"
kotlin("plugin.serialization") version "1.7.0"
kotlin("jvm") version "1.7.10"
id("org.jetbrains.compose") version "1.2.0-alpha01-dev774"
kotlin("plugin.serialization") version "1.7.10"

}

Expand All @@ -21,10 +21,8 @@ repositories {
dependencies {
implementation(compose.desktop.currentOs)
implementation("org.jetbrains.kotlinx:kotlinx-serialization-cbor:1.4.0")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-hocon:1.4.0")
implementation("org.deeplearning4j:deeplearning4j-core:1.0.0-beta6")

// implementation("com.charleskorn.kaml:kaml:0.47.0")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-properties:1.4.0")
// implementation("org.deeplearning4j:deeplearning4j-core:1.0.0-beta6")
}

tasks.withType<KotlinCompile>() {
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
106 changes: 43 additions & 63 deletions src/main/kotlin/ApplicationState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import kotlinx.serialization.cbor.Cbor
import kotlinx.serialization.decodeFromByteArray
import kotlinx.serialization.encodeToByteArray
import ui.*
import java.io.File
import java.nio.file.Path
import kotlin.io.path.*

Expand All @@ -20,10 +19,8 @@ class ApplicationState {
val settings = Settings()
val pens = mutableStateListOf<Pen>(Pen(Color(0xA00000FF), 2f), Pen(Color.Blue, 1f))

// val theme = mutableStateOf(LightMode())

init {
THEME.value = if (settings.darkmode) DarkMode() else LightMode()
THEME.value = settings.theme
}

private val _windows = mutableStateListOf<PPCWindowState>()
Expand All @@ -36,87 +33,69 @@ class ApplicationState {
)
}

fun loadDocument(docInfo: DocumentInformation): Document? {
return loadDocument(Path.of(docInfo.path))
}
// fun loadDocument(docInfo: DocumentInformation): Document? {
// return loadDocument(docInfo.path)
// }


fun loadDocument(docPath: Path): Document? {
return try {
val file = docPath.readBytes()
Cbor.decodeFromByteArray<Document>(file)
} catch (e: Exception) {
e.printStackTrace()
null
runCatching {
return Cbor.decodeFromByteArray<Document>(docPath.readBytes())
}
return null
}


fun loadDocumentInformation(
path: String = workingDirectoryPath.pathString,
depth: Int = 3,
loadedDoc: LoadedDoc
): List<DocumentInformation> {
val folders = mutableListOf<DocumentInformation>()
File(path).listFiles()?.forEach {
if (it.isDirectory) {
folders.add(
DocumentInformation(
it.name,
DocumentInformationType.Folder,
it.path
path: Path = workingDirectoryPath,
prefix: String = "",
loadedDoc: LoadedDoc? = null
): DirectoryInformation {
val document = DirectoryInformation(path.name, path, prefix)

path.listDirectoryEntries().forEach {
if (it.isDirectory()) {
document.directories.add(loadDocumentInformation(it, "$prefix/${it.name}"))
} else if (it.extension == "ppc") {
document.files.add(
FileInformation(
it.nameWithoutExtension,
it,
"$prefix/${it.name}",
document, // backwards reference
it.fileSize()
)
)
}
}

addChildrenToFolder(folders, depth - 1)

updateLoadedDoc(folders, loadedDoc)
return folders
}
if (loadedDoc != null) updateLoadedDoc(document, loadedDoc)

fun addChildrenToFolder(folders: List<DocumentInformation>, remainingDepth: Int) {
folders.forEach {
if (it.type == DocumentInformationType.Folder) {

File(it.path).listFiles()?.forEach { file ->
if (file.isFile && file.name.endsWith(".ppc")) {
it.children.add(
DocumentInformation(
file.name.substringBefore('.'),
DocumentInformationType.Document,
file.path
)
)
} else if (file.isDirectory) {
it.children.add(DocumentInformation(file.name, DocumentInformationType.Folder, file.path))
}
}
if (remainingDepth > 0) addChildrenToFolder(it.children, remainingDepth - 1)
}
}
return document
}

private fun updateLoadedDoc(folders: List<DocumentInformation>, loadedDoc: LoadedDoc) {
loadedDoc.workbook.value = folders.firstOrNull {
it.path == loadedDoc.workbook.value?.path
}
loadedDoc.folder.value = loadedDoc.workbook.value?.children?.firstOrNull {
it.path == loadedDoc.folder.value?.path
}

// TODO: Rework - cache latest opened
private fun updateLoadedDoc(document: DirectoryInformation, loadedDoc: LoadedDoc) {
loadedDoc.parent.value = document
// loadedDoc.workbook.value = document.directories.firstOrNull()
// loadedDoc.workbook.value = folders.firstOrNull {
// it.path == loadedDoc.workbook.value?.path
// }
// loadedDoc.folder.value = loadedDoc.workbook.value?.children?.firstOrNull {
// it.path == loadedDoc.folder.value?.path
// }
}

fun newFolder(doc: DocumentInformation?, name: String) {
val parentPath = doc?.path ?: workingDirectoryPath.pathString
fun newFolder(path: Path): Boolean {
runCatching {
Path(parentPath, name).createDirectory()
path.createDirectories()
return true
}
return false
}

fun newFile(doc: DocumentInformation?, name: String) {
val parentPath = doc?.path ?: workingDirectoryPath.pathString
fun newFile(doc: FileInformation?, name: String) {
val parentPath = doc?.path ?: workingDirectoryPath
runCatching {
Path(parentPath, "$name.ppc").createFile()
}
Expand Down Expand Up @@ -144,3 +123,4 @@ class ApplicationState {
}


fun Path(path: Path, varargs: String): Path = Path(path.pathString, varargs)
8 changes: 3 additions & 5 deletions src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ import data.Settings
// todo: proposed solutions:
// todo: stroke has an global offset for the starting point afterwards only the between the points can be stored by using smaller datatypes like bytes
fun main() = application {
PPCApplication(rememberApplicationState())
// val a = 1.0
// // override fun contains(value: T): Boolean = lessThanOrEquals(start, value) && lessThanOrEquals(value, endInclusive)
// print(a in -1.0..3.0)

PPCApplication(rememberApplicationState())
// val state = ApplicationState()
// state.loadDocumentInformation()
}


Expand Down
2 changes: 0 additions & 2 deletions src/main/kotlin/control/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ import ui.documentView.DocumentView

@Composable
fun ApplicationScope.PPCApplication(state: ApplicationState) {

for (window in state.windows) {
key(window) {
val documentViewControlState = remember { DocumentViewControlState(DocumentController(Document(PageSize.A4)), state) }

PPCWindow(window, documentViewControlState, DocumentView)
}
}
Expand Down
20 changes: 10 additions & 10 deletions src/main/kotlin/control/AutoSaveJob.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,33 @@ import data.Document
import kotlinx.coroutines.*
import java.nio.file.Path

class AutoSaveJob(val application: ApplicationState) :
CoroutineScope { // implement CoroutineScope to create local scope
class AutoSaveJob(private val application: ApplicationState) : CoroutineScope {
private var job: Job = Job()
override val coroutineContext
get() = Dispatchers.Default + job

private var _doc: Document? = null
private var _path: Path? = null
var _doc: Document? = null
var _path: Path? = null

// this method will help to stop execution of a coroutine.
// Call it to cancel coroutine and to break the while loop defined in the coroutine below
fun cancel() {
println("$_doc -0- $_path")
if (_doc != null && _path != null) {
application.saveDocument(_doc!!, _path!!)
job.cancel()
}
}

fun schedule(document: Document, path: Path) = launch { // launching the coroutine
val delaySeconds = 10L
_doc = document
_path = path
while (coroutineContext.isActive) {
println("save")
application.saveDocument(document, path)
delay(delaySeconds * 1000)
delay(application.settings.saveDelay.toLong() * 1000)
}
}

fun save() {
if (_doc != null && _path != null) {
application.saveDocument(_doc!!, _path!!)
}
}
}
20 changes: 8 additions & 12 deletions src/main/kotlin/control/DocumentControllerBase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import kotlinx.serialization.decodeFromByteArray
import kotlinx.serialization.encodeToByteArray
import util.Point

abstract class DocumentControllerBase(document: Document): Controller {
abstract class DocumentControllerBase(document: Document) : Controller {
val mouse = MouseInputHandler(this)
val state = mutableStateOf( DocumentControlState(this, document))
val state = mutableStateOf(DocumentControlState(this, document))

val strokeController = StrokeController()
private var canvasSize = Point(0, 0)
Expand All @@ -32,11 +32,11 @@ abstract class DocumentControllerBase(document: Document): Controller {
is Selector -> {
if (selection.value != null && !selection.value!!.isEmpty())
selection.value?.selectionComplete?.value = true

else
selection.value = null
}
is TPen -> strokeController.endStroke()

is TPen -> strokeController.endStroke()

else -> {}

Expand All @@ -59,9 +59,11 @@ abstract class DocumentControllerBase(document: Document): Controller {
fun toolDown(mousePos: Point) {
when (selectedTool.value) {
is Selector -> {
if (selection.value?.selectionComplete?.value!!)
// if (selection.value?.selectionComplete?.value!!) // NOTE: crashes application when selection starts over the toolbar canvas
if (selection.value?.selectionComplete?.value == true)
selection.value = null
}

is TPen -> penDown(mousePos)

else -> {}
Expand All @@ -71,6 +73,7 @@ abstract class DocumentControllerBase(document: Document): Controller {
fun toolClicked() {

}

abstract fun penDown(mousePos: Point)

abstract fun selectorMoved(globalPoint: Point)
Expand All @@ -84,9 +87,6 @@ abstract class DocumentControllerBase(document: Document): Controller {
abstract fun newStroke(start: Point)





fun resize(newXDim: Int, newYDim: Int, localCenter: Point) {
canvasSize = Point(newXDim, newYDim)
this.localCenter = localCenter
Expand All @@ -96,15 +96,11 @@ abstract class DocumentControllerBase(document: Document): Controller {
if (state.value.document.value.scrollY + scrollDelta < 0) return
state.value.document.value.centerPoint.value += Point(0, scrollDelta.toDouble())
state.value.document.value.scrollY += scrollDelta
println(state.value.document.value.centerPoint.value)

}

fun scrollX(scrollDelta: Float) {
println("X: $scrollDelta")
state.value.document.value.centerPoint.value += Point(scrollDelta.toDouble(), 0)
state.value.document.value.scrollX += scrollDelta

}


Expand Down
Loading