30 Commits

Author SHA1 Message Date
zxq5 7272d19207 added filesystem abstraction layer and implemented basic filesystem - testing not complete yet, still features missing
Continuous integration / build (push) Successful in 3m27s
2025-08-21 23:11:09 +01:00
zxq5 9614d2884b added filesystem abstraction layer and implemented basic filesystem - testing not complete yet, still features missing
Continuous integration / build (push) Has been cancelled
2025-08-21 23:07:46 +01:00
zxq5 c891a8be58 ui improvements and feature flags for AI integration
Continuous integration / build (push) Failing after 4m9s
2025-08-18 01:06:30 +01:00
zxq5 b6d0e10a7d UI fix
Continuous integration / build (push) Has been cancelled
2025-08-11 01:34:27 +01:00
zxq5 8cf54f3346 moved ai assistant to editor side panel
Continuous integration / build (push) Successful in 1m32s
2025-08-10 00:55:02 +01:00
zxq5 874d7ff377 updated .gitignore
Continuous integration / build (push) Successful in 1m32s
2025-08-09 23:44:21 +01:00
zxq5 3c47ae1305 using a gitea compatible upload-artifact action
Continuous integration / build (push) Successful in 1m41s
2025-08-09 23:20:47 +01:00
zxq5 67d8902eaf using a gitea compatible upload-artifact action
Continuous integration / build (push) Failing after 13s
2025-08-09 23:19:49 +01:00
zxq5 2a7ec348c5 updated repo name in actiosn
Continuous integration / build (push) Failing after 6m19s
2025-08-09 23:03:15 +01:00
zxq5 0653427557 updated repo name in actiosn
Continuous integration / build (push) Successful in 6m17s
2025-08-09 22:56:26 +01:00
zxq5 6b6f65713d commented out windows target
Continuous integration / build (push) Successful in 1m32s
2025-08-09 22:47:43 +01:00
zxq5 c21819e786 fixed cargo warns
Continuous integration / build (push) Failing after 1m48s
2025-08-09 22:42:42 +01:00
zxq5 cc7eb3e7fb updated gitignore and setup PKGBUILD for archlinux packaging
Continuous integration / build (push) Failing after 7m6s
2025-08-09 22:35:05 +01:00
zxq5 745e03a74f idk what i changed
Continuous integration / build (push) Has been cancelled
2025-07-30 22:47:02 +01:00
zxq5 e5a485d3a7 reworked settings, general interface improvements, more AI configuration, bugfixes and QOL.
Continuous integration / build (push) Has been cancelled
2025-07-30 02:48:49 +01:00
zxq5 6c40f34122 added streaming for AI content generation
Continuous integration / build (push) Failing after 6m27s
2025-07-28 01:27:03 +01:00
zxq5 5294feb5ff updated roadmap
Continuous integration / build (push) Failing after 6m28s
2025-07-28 00:05:29 +01:00
zxq5 6832f1c5bc test
Continuous integration / build (push) Has been cancelled
2025-07-28 00:03:24 +01:00
zxq5 65213d3a9c test
Continuous integration / build (push) Has been cancelled
2025-07-28 00:03:04 +01:00
zxq5 73d5654e25 removed unnecessary
Continuous integration / build (push) Has been cancelled
2025-07-28 00:02:00 +01:00
zxq5 71f8f76f99 added font to repo
Continuous integration / build (push) Has been cancelled
2025-07-28 00:01:16 +01:00
zxq5 4d0e0c90a7 minor changes
Continuous integration / build (push) Failing after 6m40s
2025-07-27 23:23:47 +01:00
zxq5 bc71a30bfa fixed ai integration
Continuous integration / build (push) Failing after 6m27s
2025-07-27 19:15:30 +01:00
zxq5 6a25c6c05c idk
Continuous integration / build (push) Failing after 6m47s
2025-07-24 00:18:41 +01:00
zxq5 c3ae04ab75 idk 2025-07-24 00:18:09 +01:00
zxq5 9c50db2b62 worklflow works?
Continuous integration / Check (push) Failing after 3m15s
Continuous integration / build (push) Failing after 6m47s
2025-07-24 00:05:53 +01:00
zxq5 bbf9b9b00d worklflow works?
Continuous integration / Check (push) Failing after 1m34s
Continuous integration / build (push) Has been cancelled
2025-07-24 00:02:23 +01:00
zxq5 59f3a24d2c e 2025-07-23 23:56:27 +01:00
zxq5 ba468cafa7 finally another commit 2025-07-23 23:56:07 +01:00
zxq5 224300f3ea progress 2025-07-17 02:18:29 +01:00
40 changed files with 4125 additions and 2312 deletions
-2
View File
@@ -1,2 +0,0 @@
[build]
rustc-wrapper = "sccache"
+25
View File
@@ -0,0 +1,25 @@
on: [push, pull_request]
name: Continuous integration
jobs:
build:
name: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions-rust-lang/setup-rust-toolchain@v1
# with:
# target: x86_64-pc-windows-gnu
- run: cargo build --release
# - run: cargo build --release --target x86_64-pc-windows-gnu
- uses: christopherhx/gitea-upload-artifact@v4
with:
name: linux-release
path: target/release/worldcoder
# - uses: actions/upload-artifact@v4
# with:
# name: windows-release
# path: target/x86_64-pc-windows-gnu/release/doc_writing_tool.exe
+5
View File
@@ -1,2 +1,7 @@
/target
*/target
/project
Cargo.lock
*.pkg.tar.zst
/pkg
/.config
+7 -2
View File
@@ -1,9 +1,14 @@
{
"rust-analyzer.check.command": "clippy",
"editor.formatOnSave": true,
"rust-analyzer.cargo.features": "all",
"files.eol": "\n",
"files.insertFinalNewline": true,
"files.trimFinalNewlines": true,
"files.trimTrailingWhitespace": true
"files.trimTrailingWhitespace": true,
"rust-analyzer.cargo.features": [
"native",
"llm"
],
"rust-analyzer.cargo.noDefaultFeatures": true,
"rust-analyzer.cargo.allFeatures": false
}
Generated
+996 -558
View File
File diff suppressed because it is too large Load Diff
+16 -14
View File
@@ -1,29 +1,31 @@
[package]
name = "somewhatusefultool"
name = "worldcoder"
version = "0.1.0"
edition = "2024"
[dependencies]
eframe = "0.31.1"
egui = "0.31.1"
editor = { path = "./editor" }
egui_extras = { version = "0.31.1", features = [
eframe = "0.32.0"
egui = { version = "0.32.0", features = ["serde"] }
egui_extras = { version = "0.32.0", features = [
"chrono",
"datepicker",
"file",
"image",
] }
egui_file = "0.22.1"
egui_file = "0.23.0"
image = { version = "0.25.6", features = ["jpeg", "png"] }
serde = { version = "1.0.219", features = ["derive"] }
serde_json = "1.0.140"
serde_json = "1.0.141"
chrono = { version = "0.4.41", features = ["serde"] }
thiserror = "2.0.12"
egui_commonmark = { version = "0.20.0", features = ["embedded_image"] }
walkdir = "2.5.0"
thiserror = "2.0.14"
egui_commonmark = { version = "0.21.1", features = ["embedded_image"] }
uuid = { version = "1.17.0", features = ["v4"] }
reqwest = { version = "0.12.23", features = ["blocking", "json"] }
tempfile = "3.20.0"
itertools = "0.14.0"
[target.x86_64-pc-windows-gnu]
linker = "x86_64-w64-mingw32-gcc"
ar = "x86_64-w64-mingw32-gcc-ar"
[features]
default = ["native", "llm"]
web = []
native = []
llm = []
+24
View File
@@ -0,0 +1,24 @@
pkgname=worldcoder
pkgver=0.1.1
pkgrel=3
makedepends=('rust' 'cargo')
arch=('i686' 'x86_64' 'armv6h' 'armv7h')
prepare() {
cargo fetch --locked --target "$CARCH-unknown-linux-gnu"
}
build() {
export RUSTUP_TOOLCHAIN=stable
export CARGO_TARGET_DIR=target
cargo build --frozen --release --all-features
}
check() {
export RUSTUP_TOOLCHAIN=stable
cargo test --frozen --all-features
}
package() {
install -Dm0755 -t "$pkgdir/usr/bin/" "target/release/$pkgname"
}
-354
View File
@@ -1,354 +0,0 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "ab_glyph"
version = "0.2.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e0f4f6fbdc5ee39f2ede9f5f3ec79477271a6d6a2baff22310d51736bda6cea"
dependencies = [
"ab_glyph_rasterizer",
"owned_ttf_parser",
]
[[package]]
name = "ab_glyph_rasterizer"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b2187590a23ab1e3df8681afdf0987c48504d80291f002fcdb651f0ef5e25169"
[[package]]
name = "ahash"
version = "0.8.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a15f179cd60c4584b8a8c596927aadc462e27f2ca70c04e0071964a73ba7a75"
dependencies = [
"cfg-if",
"once_cell",
"version_check",
"zerocopy",
]
[[package]]
name = "autocfg"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8"
[[package]]
name = "bitflags"
version = "2.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967"
[[package]]
name = "cfg-if"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9555578bc9e57714c812a1f84e4fc5b4d21fcb063490c624de019f7464c91268"
[[package]]
name = "ecolor"
version = "0.32.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4a631732d995184114016fab22fc7e3faf73d6841c2d7650395fe251fbcd9285"
dependencies = [
"emath",
]
[[package]]
name = "editor"
version = "0.1.0"
dependencies = [
"egui",
"serde",
]
[[package]]
name = "egui"
version = "0.32.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8470210c95a42cc985d9ffebfd5067eea55bdb1c3f7611484907db9639675e28"
dependencies = [
"ahash",
"bitflags",
"emath",
"epaint",
"nohash-hasher",
"profiling",
"smallvec",
"unicode-segmentation",
]
[[package]]
name = "emath"
version = "0.32.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "45f057b141e7e46340c321400be74b793543b1b213036f0f989c35d35957c32e"
[[package]]
name = "epaint"
version = "0.32.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94cca02195f0552c17cabdc02f39aa9ab6fbd815dac60ab1cd3d5b0aa6f9551c"
dependencies = [
"ab_glyph",
"ahash",
"ecolor",
"emath",
"epaint_default_fonts",
"nohash-hasher",
"parking_lot",
"profiling",
]
[[package]]
name = "epaint_default_fonts"
version = "0.32.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e8495e11ed527dff39663b8c36b6c2b2799d7e4287fb90556e455d72eca0b4d3"
[[package]]
name = "libc"
version = "0.2.174"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1171693293099992e19cddea4e8b849964e9846f4acee11b3948bcc337be8776"
[[package]]
name = "lock_api"
version = "0.4.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96936507f153605bddfcda068dd804796c84324ed2510809e5b2a624c81da765"
dependencies = [
"autocfg",
"scopeguard",
]
[[package]]
name = "nohash-hasher"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2bf50223579dc7cdcfb3bfcacf7069ff68243f8c363f62ffa99cf000a6b9c451"
[[package]]
name = "once_cell"
version = "1.21.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
[[package]]
name = "owned_ttf_parser"
version = "0.25.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22ec719bbf3b2a81c109a4e20b1f129b5566b7dce654bc3872f6a05abf82b2c4"
dependencies = [
"ttf-parser",
]
[[package]]
name = "parking_lot"
version = "0.12.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70d58bf43669b5795d1576d0641cfb6fbb2057bf629506267a92807158584a13"
dependencies = [
"lock_api",
"parking_lot_core",
]
[[package]]
name = "parking_lot_core"
version = "0.9.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc838d2a56b5b1a6c25f55575dfc605fabb63bb2365f6c2353ef9159aa69e4a5"
dependencies = [
"cfg-if",
"libc",
"redox_syscall",
"smallvec",
"windows-targets",
]
[[package]]
name = "proc-macro2"
version = "1.0.95"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778"
dependencies = [
"unicode-ident",
]
[[package]]
name = "profiling"
version = "1.0.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3eb8486b569e12e2c32ad3e204dbaba5e4b5b216e9367044f25f1dba42341773"
[[package]]
name = "quote"
version = "1.0.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d"
dependencies = [
"proc-macro2",
]
[[package]]
name = "redox_syscall"
version = "0.5.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d04b7d0ee6b4a0207a0a7adb104d23ecb0b47d6beae7152d0fa34b692b29fd6"
dependencies = [
"bitflags",
]
[[package]]
name = "scopeguard"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
[[package]]
name = "serde"
version = "1.0.219"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f0e2c6ed6606019b4e29e69dbaba95b11854410e5347d525002456dbbb786b6"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.219"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "smallvec"
version = "1.15.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03"
[[package]]
name = "syn"
version = "2.0.104"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "17b6f705963418cdb9927482fa304bc562ece2fdd4f616084c50b7023b435a40"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "ttf-parser"
version = "0.25.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2df906b07856748fa3f6e0ad0cbaa047052d4a7dd609e231c4f72cee8c36f31"
[[package]]
name = "unicode-ident"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512"
[[package]]
name = "unicode-segmentation"
version = "1.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6ccf251212114b54433ec949fd6a7841275f9ada20dddd2f29e9ceea4501493"
[[package]]
name = "version_check"
version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
[[package]]
name = "windows-targets"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_gnullvm",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
[[package]]
name = "windows_i686_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
[[package]]
name = "windows_i686_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
[[package]]
name = "windows_i686_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
[[package]]
name = "zerocopy"
version = "0.8.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1039dd0d3c310cf05de012d8a39ff557cb0d23087fd44cad61df08fc31907a2f"
dependencies = [
"zerocopy-derive",
]
[[package]]
name = "zerocopy-derive"
version = "0.8.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ecf5b4cc5364572d7f4c329661bcc82724222973f2cab6f050a4e5c22f75181"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
-13
View File
@@ -1,13 +0,0 @@
[package]
name = "editor"
version = "0.1.0"
edition = "2024"
description = "a basic text editor widget with line numbers"
[dependencies]
egui = "0.31.1"
serde = "1"
[lib]
name = "editor"
path = "src/lib.rs"
-222
View File
@@ -1,222 +0,0 @@
use egui::{text::LayoutJob, Color32};
use egui::widgets::text_edit::TextEditOutput;
use std::hash::{Hash, Hasher};
#[derive(Clone, Debug, PartialEq)]
/// CodeEditor struct which stores settings for highlighting.
pub struct CodeEditor {
id: String,
numlines: bool,
numlines_shift: isize,
numlines_only_natural: bool,
fontsize: f32,
stick_to_bottom: bool,
desired_width: f32,
}
impl Hash for CodeEditor {
fn hash<H: Hasher>(&self, state: &mut H) {
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
(self.fontsize as u32).hash(state);
}
}
impl Default for CodeEditor {
fn default() -> CodeEditor {
CodeEditor {
id: String::from("Code Editor"),
numlines: true,
numlines_shift: 0,
numlines_only_natural: false,
fontsize: 10.0,
stick_to_bottom: false,
desired_width: f32::INFINITY,
}
}
}
impl CodeEditor {
pub fn id_source(self, id_source: impl Into<String>) -> Self {
CodeEditor {
id: id_source.into(),
..self
}
}
/// Use custom font size
///
/// **Default: 10.0**
pub fn with_fontsize(self, fontsize: f32) -> Self {
CodeEditor { fontsize, ..self }
}
/// Use UI font size
pub fn with_ui_fontsize(self, ui: &mut egui::Ui) -> Self {
CodeEditor {
fontsize: egui::TextStyle::Monospace.resolve(ui.style()).size,
..self
}
}
/// Show or hide lines numbering
///
/// **Default: true**
pub fn with_numlines(self, numlines: bool) -> Self {
CodeEditor { numlines, ..self }
}
/// Shift lines numbering by this value
///
/// **Default: 0**
pub fn with_numlines_shift(self, numlines_shift: isize) -> Self {
CodeEditor {
numlines_shift,
..self
}
}
/// Show lines numbering only above zero, useful for enabling numbering since nth row
///
/// **Default: false**
pub fn with_numlines_only_natural(self, numlines_only_natural: bool) -> Self {
CodeEditor {
numlines_only_natural,
..self
}
}
/// Should the containing area shrink if the content is small?
///
/// **Default: false**
pub fn auto_shrink(self, shrink: bool) -> Self {
CodeEditor {
desired_width: if shrink { 0.0 } else { self.desired_width },
..self
}
}
/// Sets the desired width of the code editor
///
/// **Default: `f32::INFINITY`**
pub fn desired_width(self, width: f32) -> Self {
CodeEditor {
desired_width: width,
..self
}
}
/// Stick to bottom
/// The scroll handle will stick to the bottom position even while the content size
/// changes dynamically. This can be useful to simulate terminal UIs or log/info scrollers.
/// The scroll handle remains stuck until user manually changes position. Once "unstuck"
/// it will remain focused on whatever content viewport the user left it on. If the scroll
/// handle is dragged to the bottom it will again become stuck and remain there until manually
/// pulled from the end position.
///
/// **Default: false**
pub fn stick_to_bottom(self, stick_to_bottom: bool) -> Self {
CodeEditor {
stick_to_bottom,
..self
}
}
pub fn format(&self, string: &str) -> egui::text::TextFormat {
let font_id = egui::FontId::monospace(self.fontsize);
let color = Color32::WHITE;
egui::text::TextFormat::simple(font_id, color)
}
fn numlines_show(&self, ui: &mut egui::Ui, text: &str) {
let total = if text.ends_with('\n') || text.is_empty() {
text.lines().count() + 1
} else {
text.lines().count()
} as isize;
let max_indent = total.to_string().len().max(
!self.numlines_only_natural as usize * self.numlines_shift.to_string().len(),
);
let mut counter = (1..=total)
.map(|i| {
let num = i + self.numlines_shift;
if num <= 0 && self.numlines_only_natural {
String::new()
} else {
let label = num.to_string();
format!(
"{}{label}",
" ".repeat(max_indent.saturating_sub(label.len()))
)
}
})
.collect::<Vec<String>>()
.join("\n");
#[allow(clippy::cast_precision_loss)]
let width = max_indent as f32
* self.fontsize
* 0.5
* !(total + self.numlines_shift <= 0 && self.numlines_only_natural) as u8
as f32;
let mut layouter = |ui: &egui::Ui, string: &str, _wrap_width: f32| {
let layout_job = egui::text::LayoutJob::single_section(
string.to_string(),
egui::TextFormat::simple(
egui::FontId::monospace(self.fontsize),
Color32::WHITE,
),
);
ui.fonts(|f| f.layout_job(layout_job))
};
ui.add(
egui::TextEdit::multiline(&mut counter)
.id_source(format!("{}_numlines", self.id))
.font(egui::TextStyle::Monospace)
.interactive(false)
.frame(false)
.desired_width(width)
.layouter(&mut layouter),
);
}
/// Show Code Editor
pub fn show(
&mut self,
ui: &mut egui::Ui,
text: &mut dyn egui::TextBuffer,
) -> TextEditOutput {
let mut text_edit_output: Option<TextEditOutput> = None;
let code_editor = |ui: &mut egui::Ui| {
ui.horizontal_top(|h| {
if self.numlines {
self.numlines_show(h, text.as_str());
}
egui::ScrollArea::horizontal()
.hscroll(true)
.id_salt(format!("{}_inner_scroll", self.id))
.show(h, |ui| {
let output = egui::TextEdit::multiline(text)
.id_source(&self.id)
.lock_focus(true)
.frame(false)
.desired_width(self.desired_width)
.show(ui);
text_edit_output = Some(output);
});
});
};
egui::ScrollArea::vertical()
.id_salt(format!("{}_outer_scroll", self.id))
.stick_to_bottom(self.stick_to_bottom)
.show(ui, code_editor);
text_edit_output.expect("TextEditOutput should exist at this point")
}
}
Binary file not shown.
@@ -1,4 +0,0 @@
{
"name": "New Document",
"text": "# The effects of Whimsum dust:\nmore aggression, higher damage, higher rate of fire, lower accuracy"
}
@@ -1 +0,0 @@
{"name":"Note","content":"this is the note! gfjh gfdhgj fgfjhghfd iughuifghuifghuifghuifghuifdg"}
@@ -1,10 +0,0 @@
{
"id": "0078bb24-2fb0-4ecb-b5cb-20d29c5f2f77",
"template_id": "b5745688-3c1c-40de-bc3a-2a3e354dd19d",
"name": "The SPOONS!",
"fields": {
"description": {
"value": "full of smilers\n"
}
}
}
@@ -1,10 +0,0 @@
{
"id": "20beeb2f-363c-49bf-9621-f156d7c7cdd7",
"template_id": "b5745688-3c1c-40de-bc3a-2a3e354dd19d",
"name": "the brewdog.",
"fields": {
"description": {
"value": "full of smilers\n"
}
}
}
@@ -1,16 +0,0 @@
{
"id": "bd13d252-3f19-4618-bb10-cc45e9f7d301",
"template_id": "c96f5e87-7517-44cc-a5ab-42ffd537801d",
"name": "Cast Iron Pan",
"fields": {
"durability": {
"value": "9999999"
},
"Icon": {
"value": ""
},
"description": {
"value": "An unburnt pan for bitchslapping, comes with a free punchcard."
}
}
}
@@ -1,37 +0,0 @@
{
"name": "Character",
"id": "a24b3ab7-2572-4af4-8457-df26937fd773",
"description": "character",
"fields": [
{
"name": "Portrait / Image",
"field_type": "Image",
"required": true,
"description": "image of character"
},
{
"name": "Date of Birth",
"field_type": "Date",
"required": false,
"description": "date of birth"
},
{
"name": "Age",
"field_type": "Number",
"required": false,
"description": "age"
},
{
"name": "Appearance",
"field_type": "MultiLine",
"required": false,
"description": "character's appearance"
},
{
"name": "Personality",
"field_type": "MultiLine",
"required": false,
"description": "character's personality"
}
]
}
@@ -1,13 +0,0 @@
{
"name": "Location",
"id": "b5745688-3c1c-40de-bc3a-2a3e354dd19d",
"description": "a place",
"fields": [
{
"name": "description",
"field_type": "MultiLine",
"required": true,
"description": "what is it like?"
}
]
}
@@ -1,25 +0,0 @@
{
"name": "Item",
"id": "c96f5e87-7517-44cc-a5ab-42ffd537801d",
"description": "an in-game item",
"fields": [
{
"name": "durability",
"field_type": "Number",
"required": false,
"description": "the item's durability"
},
{
"name": "description",
"field_type": "MultiLine",
"required": true,
"description": "the item's description"
},
{
"name": "Icon",
"field_type": "Image",
"required": false,
"description": "yes"
}
]
}
+6 -6
View File
@@ -24,9 +24,9 @@
### v0.1.1 - Editor
- [ ] Basic editor (markdown formatting)
- [x] Basic editor (markdown formatting)
- [x] Basic text editing
- [ ] Load & Save text file
- [x] Load & Save text file
- [x] Editor preview
- [x] Preview text in markdown
@@ -34,13 +34,13 @@
### v0.2.0 - links & context building
- [ ] Links between objects
- [x] Links between objects
- [ ] Links in templates
### v0.2.1 - writing projects & organisation
- [ ] Project management
- [ ] Chapters/organisation
- [x] Project management
- [x] Chapters/organisation
## v0.3 - Workflows & AI integration
@@ -70,7 +70,7 @@
- [ ] Content generation AI
- [ ] Collect context from across a story or project
- [ ] Generate content for stories
- [x] Generate content for stories (Done to a very basic level)
- [ ] Create original content in the form of objects using templates
- [ ] Create a new template
+91
View File
@@ -0,0 +1,91 @@
use egui::{TextEdit, vec2};
use crate::{
PROJECT_FOLDER,
filesystem::{FILESYSTEM, FsError, LegacyFileSystem},
util,
};
#[derive(Debug, Clone)]
pub struct Asset {
pub new_name: String,
pub name: String,
pub saved: bool,
}
impl Asset {
pub fn open(name: String) -> Self {
Self {
new_name: name.clone(),
name,
saved: true,
}
}
pub fn save(&mut self) {
let old_path = Self::path(&self.name);
let new_path = Self::path(&self.new_name);
println!("old_path: {old_path:?}");
println!("new_path: {new_path:?}");
if let Err(FsError::Io(err)) = FILESYSTEM.rename(&old_path, &new_path) {
match err.kind() {
std::io::ErrorKind::NotFound => {
let dir = new_path.parent().unwrap();
if !dir.exists() {
FILESYSTEM.mkdir(dir).unwrap();
}
FILESYSTEM.rename(&old_path, &new_path).unwrap();
}
_ => panic!("Failed to rename file: {err}"),
}
}
self.saved = true;
self.name = self.new_name.clone();
}
pub fn path(name: &str) -> std::path::PathBuf {
PROJECT_FOLDER.join("assets").join(name)
}
pub fn ui(&mut self, ui: &mut egui::Ui) {
ui.vertical(|ui| {
util::saved_status(ui, self.saved, &self.name, &self.new_name);
if ui.input(|i| i.key_pressed(egui::Key::S) && i.modifiers.ctrl)
|| ui.button("Save").clicked()
{
self.save();
}
ui.separator();
ui.horizontal(|ui| {
ui.strong("Filename:");
if TextEdit::singleline(&mut self.new_name)
.desired_width(f32::INFINITY)
.frame(false)
.show(ui)
.response
.changed()
{
self.saved = false;
}
});
ui.separator();
if let Ok(bytes) = FILESYSTEM.read_bytes(&Self::path(&self.name)) {
let image_source = egui::ImageSource::Bytes {
uri: std::borrow::Cow::Owned(self.name.clone()),
bytes: bytes.into(),
};
ui.add(
egui::Image::new(image_source)
.max_size(vec2(ui.available_width(), f32::INFINITY)),
);
}
});
}
}
+358
View File
@@ -0,0 +1,358 @@
use std::path::Path;
use egui::TextEdit;
use egui_commonmark::{CommonMarkCache, CommonMarkViewer};
use serde::{self, Deserialize, Serialize};
use crate::{
editors::{settings_editor::ProjectSettings, tags::Tag},
filesystem::{FILESYSTEM, LegacyFileSystem},
util,
};
#[cfg(feature = "llm")]
use crate::llm_integration::content_llm::{ContentAI, ReadyState};
pub struct MainEditor {
pub content: ContentSection,
pub show_editor: bool,
pub editor_separate_window: bool,
pub show_preview: bool,
preview_cache: CommonMarkCache,
#[cfg(feature = "llm")]
dialog: ContentAI,
#[cfg(feature = "llm")]
pub show_ai: bool,
}
impl Clone for MainEditor {
fn clone(&self) -> Self {
Self {
content: self.content.clone(),
show_editor: self.show_editor,
editor_separate_window: self.editor_separate_window,
show_preview: self.show_preview,
preview_cache: CommonMarkCache::default(),
#[cfg(feature = "llm")]
dialog: self.dialog.clone(),
#[cfg(feature = "llm")]
show_ai: self.show_ai,
}
}
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct ContentSection {
pub title: String,
pub id: String,
#[serde(default)]
pub description: String,
#[serde(default)]
pub tags: Vec<String>,
#[serde(default)]
pub content: String,
// parent id
#[serde(default)]
pub parent: Option<String>,
#[serde(skip)]
pub saved: bool,
}
impl ContentSection {
pub fn new() -> Self {
Self {
title: String::new(),
id: uuid::Uuid::new_v4().to_string(),
description: String::new(),
tags: Vec::new(),
content: String::new(),
parent: None,
saved: false,
}
}
pub fn save(&mut self) -> Result<(), Box<dyn std::error::Error>> {
FILESYSTEM.write(
Path::new(&format!("documents/{id}.json", id = &self.id)),
self.clone(),
)?;
self.saved = true;
Ok(())
}
pub fn load(id: &str) -> Result<Self, Box<dyn std::error::Error>> {
let mut section: Self = FILESYSTEM.read(Path::new(&format!("documents/{id}.json")))?;
section.saved = true;
section.id = id.to_string();
Ok(section)
}
pub fn create_child(&self) -> Self {
let mut child = Self::new();
child.title = format!("{} (Child)", self.title);
child.parent = Some(self.id.clone());
child
}
}
impl MainEditor {
pub fn new() -> Self {
Self {
content: ContentSection::new(),
show_editor: false, // Start with editor hidden
show_preview: false,
editor_separate_window: false,
preview_cache: CommonMarkCache::default(),
#[cfg(feature = "llm")]
show_ai: false,
#[cfg(feature = "llm")]
dialog: ContentAI::new(String::new()),
}
}
pub fn open(content: ContentSection) -> Self {
Self {
content,
show_editor: true,
show_preview: false,
editor_separate_window: false,
preview_cache: CommonMarkCache::default(),
#[cfg(feature = "llm")]
show_ai: false,
#[cfg(feature = "llm")]
dialog: ContentAI::new(String::new()),
}
}
pub fn render_ui(&mut self, project: &mut ProjectSettings, ui: &mut egui::Ui) {
ui.vertical(|ui| {
// check for Ctrl+S to save
if ui.input(|i| i.key_pressed(egui::Key::S) && i.modifiers.ctrl) {
if let Err(e) = self.content.save() {
eprintln!("Failed to save: {e}");
}
}
// display save state
util::saved_status(
ui,
self.content.saved,
&self.content.id,
&self.content.title,
);
// Save/Cancel buttons
ui.horizontal(|ui| {
// save button
if ui.button("Save").clicked() {
if let Err(e) = self.content.save() {
eprintln!("Failed to save: {e}");
}
}
// create copy button
if ui.button("Create Copy").clicked() {
let mut copy = self.clone();
copy.content.id = uuid::Uuid::new_v4().to_string();
copy.content.title = format!("{} (Copy)", self.content.title);
copy.content.save().unwrap();
}
// delete button
if ui.button("Delete").clicked() {
FILESYSTEM
.delete(Path::new(&format!(
"documents/{id}.json",
id = &self.content.id
)))
.unwrap();
*self = Self::new();
}
// revert changes button
if ui.button("Revert changes").clicked() {
self.content = ContentSection::load(&self.content.id).unwrap();
}
// preview toggle
ui.checkbox(&mut self.show_preview, "Preview");
// assistant toggle
#[cfg(feature = "llm")]
if project.ai_enabled() {
ui.checkbox(&mut self.show_ai, "AI Assistant");
}
// editor toggle
ui.checkbox(&mut self.editor_separate_window, "Pop out editor");
});
});
ui.separator();
// Name and description grid
egui::Grid::new("top_grid")
.striped(true)
.num_columns(2)
.show(ui, |ui| {
ui.strong("Name");
if ui
.add(
TextEdit::singleline(&mut self.content.title)
.desired_width(f32::INFINITY)
.frame(false),
)
.changed()
{
self.content.saved = false;
}
ui.end_row();
ui.strong("Description");
if ui
.add(
TextEdit::singleline(&mut self.content.description)
.desired_width(f32::INFINITY)
.frame(false),
)
.changed()
{
self.content.saved = false;
}
ui.end_row();
ui.strong("Tags");
Tag::selector_ui(&mut self.content.tags, ui, Some(&mut self.content.saved));
ui.end_row();
});
ui.separator();
#[cfg(feature = "llm")]
if self.show_ai && project.ai_enabled() {
let dialog = &mut self.dialog;
dialog.content = self.content.content.clone();
dialog.ui(ui, project);
if *dialog.ready.lock().unwrap() == ReadyState::Ready {
self.content
.content
.push_str(&dialog.result.lock().unwrap());
self.content.saved = false;
*dialog.ready.lock().unwrap() = ReadyState::Idle;
} else if *dialog.ready.lock().unwrap() == ReadyState::Halted {
*dialog.ready.lock().unwrap() = ReadyState::Idle;
}
}
if self.show_preview {
self.preview_ui(ui);
}
self.editor_ui(ui, project);
}
pub fn ui(&mut self, ctx: &egui::Context, project: &mut ProjectSettings) {
// Show the editor window if enabled
let mut show = self.show_editor;
if show {
if self.editor_separate_window {
egui::Window::new("Editor")
.resizable(true)
.default_width(1000.0)
.default_height(800.0)
.open(&mut show)
.show(ctx, |ui| {
self.render_ui(project, ui);
});
} else {
egui::CentralPanel::default().show(ctx, |ui| {
self.render_ui(project, ui);
});
}
}
self.show_editor = show;
}
fn preview_ui(&mut self, ui: &mut egui::Ui) {
// Preview area
egui::SidePanel::right("preview_panel")
.resizable(true)
.default_width(ui.available_width() / 2.0)
.show_inside(ui, |ui| {
// Preview area with centered content and max width
egui::ScrollArea::both()
.auto_shrink([false, false])
.id_salt("preview_scroll")
.show(ui, |ui| {
let max_width = 600;
let available_width = ui.available_width();
let content_width = (max_width as f32).min(available_width);
let padding = (available_width - content_width) / 2.0;
ui.horizontal(|ui| {
ui.add_space(padding);
ui.vertical(|ui| {
ui.set_width(content_width);
ui.add_space(15.0);
ui.set_min_width(max_width as f32);
CommonMarkViewer::new()
.default_width(Some(max_width))
.max_image_width(Some(512))
.show(ui, &mut self.preview_cache, &self.content.content);
});
});
});
});
}
fn editor_ui(&mut self, ui: &mut egui::Ui, _project: &mut ProjectSettings) {
let _response = egui::ScrollArea::both()
.auto_shrink([false, false])
.id_salt("editor_scroll")
.show(ui, |ui| {
let max_width = 600;
let available_width = ui.available_width();
let content_width = (max_width as f32).min(available_width);
let padding = (available_width - content_width).max(30.0) / 2.0;
ui.horizontal(|ui| {
ui.add_space(padding);
ui.vertical(|ui| {
ui.set_width(content_width);
ui.add_space(15.0);
ui.set_min_width(max_width as f32);
let response = ui.add(
TextEdit::multiline(&mut self.content.content)
.id_source("MainEditor_editor")
.font(egui::TextStyle::Monospace)
.interactive(true)
.frame(false)
.lock_focus(true)
.hint_text("Type here...")
.desired_width(max_width as f32),
);
if response.changed() {
self.content.saved = false;
}
});
});
});
}
}
+7
View File
@@ -0,0 +1,7 @@
pub mod asset_editor;
pub mod content_editor;
pub mod note_editor;
pub mod object_editor;
pub mod settings_editor;
pub mod tags;
pub mod template_editor;
+157
View File
@@ -0,0 +1,157 @@
use std::path::Path;
use egui::TextEdit;
use serde::{Deserialize, Serialize};
use crate::{
editors::tags::Tag,
filesystem::{FILESYSTEM, LegacyFileSystem},
util,
};
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Note {
pub id: String,
pub name: String,
#[serde(default)]
pub content: String,
#[serde(default)]
pub subject: String,
#[serde(default)]
pub tags: Vec<String>,
#[serde(skip)]
#[serde(default = "default_saved")]
pub saved: bool,
}
pub fn default_saved() -> bool {
true
}
impl Default for Note {
fn default() -> Self {
Self {
id: uuid::Uuid::new_v4().to_string(),
name: "New Note".to_string(),
subject: "".to_string(),
content: "".to_string(),
tags: Vec::new(),
saved: false,
}
}
}
impl Note {
pub fn new() -> Self {
Self {
id: uuid::Uuid::new_v4().to_string(),
name: "New Note".to_string(),
subject: "".to_string(),
content: "".to_string(),
tags: Vec::new(),
saved: false,
}
}
pub fn save(&mut self) -> Result<(), Box<dyn std::error::Error>> {
let id = &self.id;
FILESYSTEM.write(Path::new(&format!("notes/{id}.json")), self.clone())?;
self.saved = true;
Ok(())
}
pub fn load(id: &str) -> Result<Self, Box<dyn std::error::Error>> {
let mut note: Self = FILESYSTEM.read(Path::new(&format!("notes/{id}.json")))?;
note.id = id.to_string();
note.saved = true;
Ok(note)
}
pub fn ui(&mut self, ui: &mut egui::Ui) {
if ui.input(|i| i.key_pressed(egui::Key::S) && i.modifiers.ctrl) {
if let Err(e) = self.save() {
eprintln!("Failed to save: {e}");
}
}
util::saved_status(ui, self.saved, &self.id, &self.name);
if ui.button("Save").clicked() {
if let Err(e) = self.save() {
eprintln!("Failed to save: {e}");
}
}
let id = ui.make_persistent_id("note_name");
egui::collapsing_header::CollapsingState::load_with_default_open(ui.ctx(), id, true)
.show_header(ui, |ui| {
ui.strong("Name");
})
.body(|ui| {
ui.separator();
if TextEdit::singleline(&mut self.name)
.desired_width(f32::INFINITY)
.frame(false)
.show(ui)
.response
.changed()
{
self.saved = false;
}
ui.separator();
});
let id = ui.make_persistent_id("note_tags");
egui::collapsing_header::CollapsingState::load_with_default_open(ui.ctx(), id, true)
.show_header(ui, |ui| {
ui.strong("Tags");
})
.body(|ui| {
ui.separator();
Tag::selector_ui(&mut self.tags, ui, Some(&mut self.saved));
ui.separator();
});
let id = ui.make_persistent_id("note_subject");
egui::collapsing_header::CollapsingState::load_with_default_open(ui.ctx(), id, true)
.show_header(ui, |ui| {
ui.strong("Subject");
})
.body(|ui| {
ui.separator();
if TextEdit::singleline(&mut self.subject)
.desired_width(f32::INFINITY)
.frame(false)
.show(ui)
.response
.changed()
{
self.saved = false;
}
ui.separator();
});
let id = ui.make_persistent_id("note_content");
egui::collapsing_header::CollapsingState::load_with_default_open(ui.ctx(), id, true)
.show_header(ui, |ui| {
ui.strong("Content");
})
.body(|ui| {
ui.separator();
if TextEdit::multiline(&mut self.content)
.desired_width(f32::INFINITY)
.desired_rows(5)
.frame(false)
.show(ui)
.response
.changed()
{
self.saved = false;
}
ui.separator();
});
}
}
+341
View File
@@ -0,0 +1,341 @@
use core::f32;
use egui::{CollapsingHeader, RichText, Sense, TextEdit, Ui, UiBuilder, vec2};
use serde::{Deserialize, Serialize};
use std::path::Path;
use crate::{
PROJECT_FOLDER, RightPanelContent,
editors::{
tags::Tag,
template_editor::{FieldValue, Template},
},
filesystem::{FILESYSTEM, LegacyFileSystem},
util,
};
pub type ObjectId = String;
#[derive(Debug, Serialize, Deserialize)]
pub struct ObjectInstance {
// template info
pub id: ObjectId,
pub template_id: String,
// instance info
pub name: String,
pub fields: std::collections::HashMap<String, FieldValue>,
#[serde(default)]
pub tags: Vec<String>,
#[serde(skip)]
pub saved: bool,
#[serde(skip)]
pub dialog: Option<egui_file::FileDialog>,
}
impl Clone for ObjectInstance {
fn clone(&self) -> Self {
Self {
id: self.id.clone(),
template_id: self.template_id.clone(),
name: self.name.clone(),
fields: self.fields.clone(),
tags: self.tags.clone(),
saved: self.saved,
dialog: None,
}
}
}
impl Default for ObjectInstance {
fn default() -> Self {
Self {
id: uuid::Uuid::new_v4().to_string(),
template_id: "new_template_instance".to_string(),
name: "new_object".to_string(),
fields: std::collections::HashMap::new(),
tags: Vec::new(),
saved: false,
dialog: None,
}
}
}
impl ObjectInstance {
pub fn new(template: &Template) -> Self {
let mut fields = std::collections::HashMap::new();
for field in &template.fields {
fields.insert(field.name.clone(), FieldValue::from_type(&field.field_type));
}
Self {
id: uuid::Uuid::new_v4().to_string(),
template_id: template.id.clone(),
name: "new_object".to_string(),
fields,
tags: Vec::new(),
saved: false,
dialog: None,
}
}
pub fn save(&mut self) -> Result<(), Box<dyn std::error::Error>> {
let id = &self.id;
FILESYSTEM.write(Path::new(&format!("objects/{id}.json")), self.clone())?;
self.saved = true;
Ok(())
}
pub fn load(id: &str) -> Result<Self, Box<dyn std::error::Error>> {
let mut instance: Self = FILESYSTEM.read(Path::new(&format!("objects/{id}.json")))?;
instance.saved = true;
Ok(instance)
}
pub fn ui(
&mut self,
ui: &mut Ui,
template: &Template,
right_panel: &mut Option<RightPanelContent>,
objects: &mut [ObjectInstance],
) {
let _ = right_panel;
if ui.input(|i| i.key_pressed(egui::Key::S) && i.modifiers.ctrl) {
if let Err(e) = self.save() {
eprintln!("Failed to save: {e}");
}
}
ui.vertical(|ui| {
// Show save status and button
util::saved_status(ui, self.saved, &self.id, &self.name);
ui.horizontal(|ui| {
if ui.button("Save").clicked() {
if let Err(e) = self.save() {
eprintln!("Failed to save: {e}");
}
}
if ui.button("Create Copy").clicked() {
let mut copy = self.clone();
copy.id = uuid::Uuid::new_v4().to_string();
copy.dialog = None;
copy.name = format!("{} (Copy)", self.name);
copy.save().unwrap();
*right_panel = Some(RightPanelContent::Object(Box::new(copy)));
}
if ui.button("Delete").clicked() {
let id = &self.id;
FILESYSTEM
.delete(Path::new(&format!("objects/{id}.json")))
.expect("Failed to delete object");
*right_panel = Some(RightPanelContent::None);
}
});
ui.separator();
egui::ScrollArea::vertical().show(ui, |ui| {
// Render each field
// allow name to be edited
CollapsingHeader::new("Name")
.default_open(true)
.show(ui, |ui| {
ui.separator();
let _ = TextEdit::singleline(&mut self.name)
.desired_width(f32::INFINITY)
.frame(false)
.show(ui)
.response;
ui.separator();
});
CollapsingHeader::new("Tags")
.default_open(true)
.show(ui, |ui| {
ui.separator();
Tag::selector_ui(&mut self.tags, ui, Some(&mut self.saved));
ui.separator();
});
for field_def in &template.fields {
if let Some(field_value) = self.fields.get_mut(&field_def.name) {
let id = ui.make_persistent_id(format!("field_{}", field_def.name));
egui::collapsing_header::CollapsingState::load_with_default_open(
ui.ctx(),
id,
true,
)
.show_header(ui, |ui| {
ui.strong(&field_def.name);
})
.body(|ui| {
if let Some(desc) = &field_def.description {
ui.label(RichText::new(desc).italics().weak());
}
ui.separator();
Self::render_field(field_value, ui, &mut self.saved, objects);
ui.separator();
});
}
}
});
});
}
fn render_field(
field_value: &mut FieldValue,
ui: &mut egui::Ui,
saved: &mut bool,
objects: &mut [ObjectInstance],
) {
match field_value {
FieldValue::SingleLine(value) => {
if TextEdit::singleline(value)
.desired_width(f32::INFINITY)
.frame(false)
.show(ui)
.response
.changed()
{
*saved = false;
}
}
FieldValue::MultiLine(value) => {
if TextEdit::multiline(value)
.desired_width(f32::INFINITY)
.desired_rows(5)
.frame(false)
.show(ui)
.response
.changed()
{
*saved = false;
}
}
FieldValue::Date(value) => {
let response = ui.add(egui_extras::DatePickerButton::new(value));
if response.changed() {
*saved = false;
}
}
FieldValue::Number(value) => {
let response = ui.add(egui::DragValue::new(value).speed(0.1));
if response.changed() {
*saved = false;
}
}
FieldValue::Image(value) => {
ui.scope_builder(UiBuilder::new().sense(Sense::HOVER), |ui| {
let id = ui.make_persistent_id("is_hovered");
let should_show = value.is_empty()
|| ui.response().hovered()
|| ui.memory(|mem| mem.data.get_temp(id).unwrap_or(false))
|| !PROJECT_FOLDER.join("assets").join(&value).exists();
// Simple path input for now
if should_show {
let response = TextEdit::singleline(value)
.hint_text("Asset name (ignore file extension)")
.desired_width(f32::INFINITY)
.frame(false)
.show(ui)
.response;
if response.changed() {
*saved = false;
}
ui.memory_mut(|mem| {
*mem.data.get_temp_mut_or_insert_with(id, || true) = response.hovered();
});
}
// If we have a valid path, try to display a preview
if !value.is_empty() {
let path = PROJECT_FOLDER.join("assets").join(&value);
if let Ok(bytes) = FILESYSTEM.read_bytes(&path) {
let image_source = egui::ImageSource::Bytes {
uri: std::borrow::Cow::Owned(path.to_str().unwrap().to_string()),
bytes: bytes.into(),
};
ui.add(
egui::Image::new(image_source).max_size(vec2(256.0, f32::INFINITY)),
);
}
}
});
}
FieldValue::Link(template_id) => {
ObjectInstance::selector_ui(template_id, objects, ui, saved)
}
FieldValue::Links(_template_ids) => {
let mut value = String::new();
if ui.text_edit_singleline(&mut value).changed() {
*saved = false;
}
}
};
}
fn selector_ui(
selected: &mut ObjectId,
objects: &mut [ObjectInstance],
ui: &mut egui::Ui,
saved: &mut bool,
) {
if !selected.is_empty() {
if let Ok(object) = ObjectInstance::load(selected) {
ui.strong(&object.name);
}
}
ui.horizontal(|ui| {
let id = ui.make_persistent_id("new_object");
let ctx = ui.ctx();
let mut object_selection: usize =
ctx.memory(|mem| mem.data.get_temp::<usize>(id).unwrap_or(0));
if objects.is_empty() {
ui.label("No objects available");
} else {
egui::ComboBox::from_id_salt(id)
.selected_text(&objects[object_selection].name)
.show_ui(ui, |ui| {
for (i, obj) in objects.iter().enumerate() {
ui.selectable_value(&mut object_selection, i, &obj.name);
}
});
}
let ctx = ui.ctx();
ctx.memory_mut(|mem| {
*mem.data.get_temp_mut_or_default::<usize>(id) = object_selection;
});
if ui.button("Set").clicked() && object_selection < objects.len() {
*selected = objects[object_selection].id.clone();
*saved = false;
}
if ui.button("Remove").clicked() {
*selected = String::new();
*saved = false;
}
});
}
}
+396
View File
@@ -0,0 +1,396 @@
use chrono::NaiveDate;
use egui::TextEdit;
use egui_extras::DatePickerButton;
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use crate::{
filesystem::{FILESYSTEM, LegacyFileSystem},
util::saved_status,
};
#[derive(Serialize, Deserialize, Clone)]
pub struct ProjectSettings {
date: NaiveDate,
project_name: String,
project_author: String,
project_description: String,
// AI settings
#[cfg(feature = "llm")]
pub ai_context: String,
// settings
#[serde(skip)]
pub global_settings: EditorSettings,
#[serde(skip)]
pub local_overrides: EditorSettings,
#[serde(skip)]
pub open: bool,
#[serde(skip)]
pub saved: bool,
}
impl ProjectSettings {
#[allow(dead_code)]
pub fn new() -> Self {
Self::default()
}
pub fn load() -> Self {
if let Ok(mut proj) = FILESYSTEM.read::<Self>(Path::new("project.json")) {
proj.saved = true;
proj.global_settings = EditorSettings::load_global();
proj.local_overrides = EditorSettings::load();
proj
} else {
Self::default()
}
}
pub fn save(&mut self) {
FILESYSTEM
.write(Path::new("project.json"), self.clone())
.unwrap();
self.global_settings.save();
self.local_overrides.save();
self.saved = true;
}
#[allow(unused)]
fn config_str_override(
label: &str,
field: &mut Option<String>,
default: &str,
ui: &mut egui::Ui,
) -> bool {
let mut changed = false;
ui.label(label);
if let Some(value) = field {
if ui.text_edit_singleline(value).changed() {
changed = true;
};
if ui.button("Remove Override").clicked() {
*field = None;
changed = true;
}
} else if ui.button("Override").clicked() {
*field = Some(default.to_string());
changed = true;
}
ui.end_row();
changed
}
#[allow(unused)]
fn config_bool_override(
label: &str,
field: &mut Option<bool>,
default: bool,
ui: &mut egui::Ui,
) -> bool {
let mut changed = false;
ui.label(label);
if let Some(value) = field {
if ui.checkbox(value, "Enable AI").changed() {
changed = true;
};
if ui.button("Remove Override").clicked() {
*field = None;
changed = true;
}
} else if ui.button("Override").clicked() {
*field = Some(default);
changed = true;
}
ui.end_row();
changed
}
#[allow(unused)]
fn config_str(field: &mut String, label: &str, ui: &mut egui::Ui) -> bool {
let mut changed = false;
ui.label(label);
if ui.text_edit_singleline(field).changed() {
changed = true;
}
ui.end_row();
changed
}
#[allow(unused)]
fn config_bool(label: &str, field: &mut bool, ui: &mut egui::Ui) -> bool {
let mut changed = false;
ui.label(label);
if ui.checkbox(field, "Enable AI").changed() {
changed = true;
}
ui.end_row();
changed
}
#[allow(unused)]
pub fn ui(&mut self, ui: &mut egui::Ui) {
// save state
saved_status(ui, self.saved, "N/A", "Project Settings");
if ui.button("Save").clicked() {
self.save();
}
if ui.input(|i| i.key_pressed(egui::Key::S) && i.modifiers.ctrl) {
self.save();
}
ui.separator();
// project settings
ui.heading("Project Settings");
egui::Grid::new("project settings")
.striped(true)
.num_columns(2)
.show(ui, |ui| {
if Self::config_str(&mut self.project_name, "Project Name", ui) { self.saved = false };
if Self::config_str(&mut self.project_author, "Project Author", ui) { self.saved = false };
if Self::config_str(&mut self.project_description, "Project Description", ui) { self.saved = false };
ui.label("Date");
if ui.add(DatePickerButton::new(&mut self.date)).changed() { self.saved = false };
ui.end_row();
#[cfg(feature = "llm")]
{
ui.label("AI Context Prompt");
if ui.add(TextEdit::multiline(&mut self.ai_context)
.font(egui::TextStyle::Monospace)
.interactive(true)
.frame(false)
.lock_focus(true)
.hint_text("What is this project about? what should the LLM know when generating content for this project?")).changed() { self.saved = false };
ui.end_row();
}
});
ui.separator();
// local settings overrides for editor
ui.heading("Local Overrides");
egui::Grid::new("local overrides")
.striped(true)
.num_columns(2)
.show(ui, |ui| {
#[cfg(feature = "llm")]
if ProjectSettings::config_str_override(
"LLM API URI",
&mut self.local_overrides.llm_api_uri,
"http://localhost:1234",
ui,
) {
self.saved = false;
}
#[cfg(feature = "llm")]
if ProjectSettings::config_str_override(
"LLM API Key",
&mut self.local_overrides.llm_api_key,
"1234",
ui,
) {
self.saved = false;
}
#[cfg(feature = "llm")]
if ProjectSettings::config_bool_override(
"Enable AI",
&mut self.local_overrides.ai_enabled,
true,
ui,
) {
self.saved = false;
}
});
ui.separator();
// global editor settings
ui.heading("Global Editor Settings");
egui::Grid::new("global settings")
.striped(true)
.num_columns(2)
.show(ui, |ui| {
#[cfg(feature = "llm")]
if Self::config_bool(
"Enable AI",
self.global_settings.ai_enabled.as_mut().unwrap(),
ui,
) {
self.saved = false;
}
#[cfg(feature = "llm")]
if Self::config_str(
self.global_settings.llm_api_uri.as_mut().unwrap(),
"LLM API URI",
ui,
) {
self.saved = false
};
#[cfg(feature = "llm")]
if Self::config_str(
self.global_settings.llm_api_key.as_mut().unwrap(),
"LLM API Key",
ui,
) {
self.saved = false
};
});
}
#[cfg(feature = "llm")]
#[allow(unused)]
pub fn ai_enabled(&mut self) -> bool {
let client = reqwest::blocking::Client::new();
if self.global_settings.ai_enabled.unwrap() {
return true;
}
if client
.get(self.global_settings.llm_api_uri.clone().unwrap() + "/v1/models")
.send()
.is_ok()
{
self.global_settings.ai_enabled = Some(true);
return true;
}
false
}
pub fn open(&mut self) {
self.open = true;
}
pub fn _close(&mut self) {
self.open = false;
}
}
impl Default for ProjectSettings {
fn default() -> Self {
Self {
date: chrono::Local::now().naive_local().into(),
project_name: "New Project".to_string(),
project_author: "Your Name".to_string(),
project_description: "Description of your project".to_string(),
#[cfg(feature = "llm")]
ai_context: "".to_string(),
global_settings: EditorSettings::default(),
local_overrides: EditorSettings::new(),
// window state
open: false,
saved: false,
}
}
}
#[derive(Serialize, Deserialize, Clone)]
pub struct EditorSettings {
pub dark_theme: Option<bool>,
#[cfg(feature = "llm")]
pub llm_api_uri: Option<String>,
#[cfg(feature = "llm")]
pub llm_api_key: Option<String>,
#[cfg(feature = "llm")]
pub ai_enabled: Option<bool>,
#[serde(skip)]
is_global: bool,
}
impl Default for EditorSettings {
fn default() -> Self {
Self {
#[cfg(feature = "llm")]
llm_api_uri: Some("http://localhost:1234".to_string()),
#[cfg(feature = "llm")]
llm_api_key: Some("".to_string()),
#[cfg(feature = "llm")]
ai_enabled: Some(true),
dark_theme: Some(true),
// window state
is_global: true,
}
}
}
impl EditorSettings {
pub fn new() -> Self {
Self {
#[cfg(feature = "llm")]
llm_api_uri: None,
#[cfg(feature = "llm")]
llm_api_key: None,
#[cfg(feature = "llm")]
ai_enabled: None,
dark_theme: None,
is_global: false,
}
}
pub fn load() -> Self {
if let Ok(mut settings) = FILESYSTEM.read::<Self>(Path::new("settings.json")) {
settings.is_global = false;
return settings;
}
Self::new()
}
pub fn save(&self) {
let path = if self.is_global {
FILESYSTEM.config_path()
} else {
PathBuf::from("settings.json")
};
FILESYSTEM.write(path.as_path(), self.clone()).unwrap()
}
pub fn load_global() -> Self {
let path = FILESYSTEM.config_path();
if !path.exists() {
FILESYSTEM.mkdir(path.parent().unwrap()).unwrap();
FILESYSTEM.write(path.as_path(), Self::default()).unwrap();
}
let mut settings = FILESYSTEM.read::<Self>(path.as_path()).unwrap();
settings.is_global = true;
settings
}
}
+244
View File
@@ -0,0 +1,244 @@
use egui::{Response, RichText, TextEdit};
use serde::{Deserialize, Serialize};
use crate::{PROJECT_FOLDER, util};
#[derive(Serialize, Deserialize)]
pub struct Tag {
pub id: String,
pub name: String,
pub description: String,
pub color: egui::Color32,
#[serde(skip)]
pub saved: bool,
#[serde(skip)]
pub error: Option<util::Error>,
}
impl Default for Tag {
fn default() -> Self {
Self {
id: uuid::Uuid::new_v4().to_string(),
name: String::new(),
description: String::new(),
color: egui::Color32::from_rgb(20, 20, 20),
saved: false,
error: None,
}
}
}
impl Clone for Tag {
fn clone(&self) -> Self {
Self {
id: self.id.clone(),
name: self.name.clone(),
description: self.description.clone(),
color: self.color,
saved: self.saved,
error: None,
}
}
}
impl Tag {
pub fn display_ui(&mut self, ui: &mut egui::Ui) -> bool {
let mut remove = false;
egui::Frame::new()
.shadow(egui::Shadow {
offset: [2, 2],
blur: 16,
spread: 0,
color: egui::Color32::from_black_alpha(180),
})
.stroke(egui::Stroke::new(2.0, self.color))
.corner_radius(4.0)
.show(ui, |ui| {
ui.horizontal(|ui| {
if ui.add(egui::Button::new("").frame(false)).clicked() {
remove = true;
}
ui.strong(&self.name);
});
});
remove
}
pub fn list_ui(&mut self, ui: &mut egui::Ui) -> Response {
ui.add(
egui::Button::new(RichText::new(self.name.clone()).strong())
.frame(false)
.stroke(egui::Stroke::new(2.0, self.color))
.corner_radius(4.0),
)
}
pub fn ui(&mut self, ui: &mut egui::Ui) {
util::saved_status(ui, self.saved, &self.id, &self.name);
if let Some(error) = &mut self.error {
error.show(ui);
}
if ui.input(|i| i.key_pressed(egui::Key::S) && i.modifiers.ctrl) {
if let Err(e) = self.save() {
self.error = Some(util::Error::new(format!("Failed to save tag: {e}")));
}
}
egui::Grid::new("tag_grid")
.striped(true)
.num_columns(2)
.show(ui, |ui| {
ui.strong("Name");
if ui
.add(
TextEdit::singleline(&mut self.name)
.desired_width(f32::INFINITY)
.frame(false),
)
.changed()
{
self.saved = false;
}
ui.end_row();
ui.strong("Description");
if ui
.add(
TextEdit::singleline(&mut self.description)
.desired_width(f32::INFINITY)
.frame(false),
)
.changed()
{
self.saved = false;
}
ui.end_row();
ui.strong("Color");
if ui.color_edit_button_srgba(&mut self.color).changed() {
self.saved = false;
}
ui.end_row();
});
}
pub fn selector_ui(tag_ids: &mut Vec<String>, ui: &mut egui::Ui, saved: Option<&mut bool>) {
// remove duplicate tag ids
tag_ids.sort();
tag_ids.dedup();
let mut remove: Vec<usize> = Vec::new();
let mut modified = false;
let id = ui.make_persistent_id("new_tag");
let available_tags = Self::load_all();
ui.horizontal(|ui| {
let ctx = ui.ctx();
let mut tag_selection: usize =
ctx.memory_mut(|mem| *mem.data.get_temp_mut_or_default::<usize>(id));
if available_tags.is_empty() {
ui.label("No tags available");
} else {
egui::ComboBox::from_id_salt(id)
.selected_text(&available_tags[tag_selection].name)
.show_ui(ui, |ui| {
for (i, tag) in available_tags.iter().enumerate() {
if ui
.add(
egui::Button::new(RichText::new(tag.name.clone()).strong())
.frame(false)
.stroke(egui::Stroke::new(2.0, tag.color))
.corner_radius(4.0),
)
.clicked()
{
tag_selection = i;
}
}
});
}
if ui.button("Add").clicked() && tag_selection < available_tags.len() {
tag_ids.push(available_tags[tag_selection].id.clone());
tag_selection = 0;
modified = true;
}
let ctx = ui.ctx();
ctx.memory_mut(|mem| {
*mem.data.get_temp_mut_or_default::<usize>(id) = tag_selection;
});
for (i, tag_id) in tag_ids.iter().enumerate() {
if let Ok(mut tag) = Self::load(tag_id) {
if tag.display_ui(ui) {
remove.push(i)
}
} else {
// if the tag doesn't exist (AKA it's been deleted)
remove.push(i)
}
}
if !remove.is_empty() {
modified = true;
for i in remove {
tag_ids.remove(i);
}
}
if let Some(saved) = saved {
if modified {
*saved = false;
}
}
});
}
pub fn load(id: &str) -> Result<Self, Box<dyn std::error::Error>> {
let path = PROJECT_FOLDER.join("tags").join(format!("{id}.json"));
Ok(serde_json::from_str(&std::fs::read_to_string(path)?)?)
}
pub fn save(&mut self) -> Result<(), Box<dyn std::error::Error>> {
if self.name.is_empty() {
self.error = Some(util::Error::new("Tag name cannot be empty".to_string()));
return Ok(());
}
self.error = None;
let path = PROJECT_FOLDER
.join("tags")
.join(format!("{}.json", &self.id));
let content = serde_json::to_string_pretty(self)?;
std::fs::write(path, content)?;
self.saved = true;
Ok(())
}
pub fn load_all() -> Vec<Self> {
let mut tags = Vec::new();
// scan tags folder. load tag json files
let tags_folder = PROJECT_FOLDER.join("tags");
if tags_folder.exists() {
for entry in std::fs::read_dir(tags_folder).unwrap() {
let path = entry.unwrap().path();
if path.is_file() && path.extension().unwrap() == "json" {
let tag =
serde_json::from_str(&std::fs::read_to_string(path).unwrap()).unwrap();
tags.push(tag);
}
}
}
tags
}
}
@@ -1,9 +1,15 @@
use chrono::NaiveDate;
use core::fmt;
use egui::{RichText, ScrollArea};
use egui::ScrollArea;
use serde::{Deserialize, Serialize};
use std::path::Path;
use crate::{PROJECT_FOLDER, RightPanelContent, error::Error, object::ObjectInstance};
use crate::{
RightPanelContent,
editors::object_editor::ObjectInstance,
filesystem::{FILESYSTEM, LegacyFileSystem},
util::{self, Error},
};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum FieldType {
@@ -12,6 +18,62 @@ pub enum FieldType {
MultiLine,
Date,
Number,
Link { template_id: Option<String> },
Links,
}
impl Default for FieldType {
fn default() -> Self {
Self::SingleLine
}
}
impl FieldType {
fn types() -> Vec<FieldType> {
vec![
FieldType::Image,
FieldType::SingleLine,
FieldType::MultiLine,
FieldType::Date,
FieldType::Number,
FieldType::Link { template_id: None },
FieldType::Links,
]
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum FieldValue {
Image(String),
SingleLine(String),
MultiLine(String),
Date(NaiveDate),
Number(f64),
Link(String),
Links(Vec<String>),
}
impl FieldValue {
pub fn from_type(_type: &FieldType) -> Self {
match _type {
FieldType::Image => Self::Image(String::new()),
FieldType::SingleLine => Self::SingleLine(String::new()),
FieldType::MultiLine => Self::MultiLine(String::new()),
FieldType::Date => Self::Date(NaiveDate::from_ymd_opt(1970, 1, 1).unwrap()),
FieldType::Number => Self::Number(0.0),
FieldType::Link { template_id: None } => Self::Link(String::new()),
FieldType::Link {
template_id: Some(template_id),
} => Self::Link(template_id.clone()),
FieldType::Links => Self::Links(Vec::new()),
}
}
}
impl Default for FieldValue {
fn default() -> Self {
Self::SingleLine(String::new())
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -19,6 +81,9 @@ pub struct FieldDefinition {
pub name: String,
pub field_type: FieldType,
pub required: bool,
#[serde(default)]
pub on_preview: bool,
pub description: Option<String>,
}
@@ -35,6 +100,21 @@ pub struct Template {
#[serde(skip)]
pub error: Option<Error>,
#[serde(skip)]
pub new_field_name: String,
#[serde(skip)]
pub new_field_type: FieldType,
#[serde(skip)]
pub new_field_required: bool,
#[serde(skip)]
pub new_field_description: String,
#[serde(skip)]
pub new_field_on_preview: bool,
}
impl fmt::Debug for Template {
@@ -45,6 +125,10 @@ impl fmt::Debug for Template {
.field("description", &self.description)
.field("fields", &self.fields)
.field("saved", &self.saved)
.field("new_field_name", &self.new_field_name)
.field("new_field_type", &self.new_field_type)
.field("new_field_required", &self.new_field_required)
.field("new_field_description", &self.new_field_description)
.finish()
}
}
@@ -58,6 +142,12 @@ impl Clone for Template {
fields: self.fields.clone(),
saved: self.saved,
error: None,
new_field_name: "".to_string(),
new_field_type: FieldType::default(),
new_field_required: false,
new_field_description: "".to_string(),
new_field_on_preview: false,
}
}
}
@@ -71,40 +161,31 @@ impl Default for Template {
fields: Vec::new(),
saved: false,
error: None,
new_field_name: "".to_string(),
new_field_type: FieldType::default(),
new_field_required: false,
new_field_description: "".to_string(),
new_field_on_preview: false,
}
}
}
impl Template {
pub fn load(id: &str) -> Result<Self, Box<dyn std::error::Error>> {
let path = PROJECT_FOLDER.join("templates").join(format!("{id}.json"));
let content = std::fs::read_to_string(&path)?;
let mut template: Self = serde_json::from_str(&content)?;
let mut template = FILESYSTEM.read::<Self>(Path::new(&format!("templates/{id}.json")))?;
template.saved = true;
Ok(template)
}
pub fn save(&mut self) -> Result<(), Box<dyn std::error::Error>> {
let path = PROJECT_FOLDER
.join("templates")
.join(format!("{}.json", &self.id));
let content = serde_json::to_string_pretty(self)?;
std::fs::write(path, content)?;
let id = &self.id;
FILESYSTEM.write(Path::new(&format!("templates/{id}.json")), self.clone())?;
self.saved = true;
Ok(())
}
pub fn ui(
&mut self,
ui: &mut egui::Ui,
new_instance: &mut Option<RightPanelContent>,
new_field_name: &mut String,
new_field_type: &mut FieldType,
new_field_required: &mut bool,
new_field_description: &mut String,
) {
pub fn ui(&mut self, ui: &mut egui::Ui, new_instance: &mut Option<RightPanelContent>) {
if let Some(error) = &mut self.error {
error.show(ui);
}
@@ -117,16 +198,7 @@ impl Template {
ScrollArea::vertical().show(ui, |ui| {
ui.vertical(|ui| {
ui.group(|ui| {
ui.horizontal(|ui| {
if self.saved {
ui.label(RichText::new("✓ Saved").color(egui::Color32::GREEN));
} else {
ui.label(RichText::new("* Unsaved").color(egui::Color32::YELLOW));
}
ui.label(format!("id: {}", self.id));
});
});
util::saved_status(ui, self.saved, &self.id, &self.name);
// Save/Cancel buttons
ui.horizontal(|ui| {
@@ -144,13 +216,10 @@ impl Template {
}
if ui.button("Delete").clicked() {
std::fs::remove_file(
PROJECT_FOLDER
.join("templates")
.join(format!("{}.json", self.id)),
)
.unwrap();
let id = &self.id;
FILESYSTEM
.delete(Path::new(&format!("templates/{id}.json")))
.unwrap();
*new_instance = Some(RightPanelContent::None);
}
@@ -161,9 +230,9 @@ impl Template {
if ui.button("Use Template").clicked() {
if self.saved {
*new_instance = Some(RightPanelContent::Object {
object: Box::new(ObjectInstance::new(self)),
});
*new_instance = Some(RightPanelContent::Object(Box::new(
ObjectInstance::new(self),
)));
} else {
self.error = Some(Error::new(
"You must save the template before creating a new instance!"
@@ -173,25 +242,12 @@ impl Template {
}
});
self.editor_ui(
ui,
new_field_name,
new_field_type,
new_field_required,
new_field_description,
);
self.editor_ui(ui);
});
});
}
pub fn editor_ui(
&mut self,
ui: &mut egui::Ui,
new_field_name: &mut String,
new_field_type: &mut FieldType,
new_field_required: &mut bool,
new_field_description: &mut String,
) {
pub fn editor_ui(&mut self, ui: &mut egui::Ui) {
egui::Grid::new("template_grid")
.num_columns(2)
.striped(true)
@@ -254,7 +310,7 @@ impl Template {
if ui.button("").clicked() {
to_remove = Some(i);
}
ui.label(field.name.clone());
ui.strong(field.name.clone());
})
.body(|ui| {
ui.separator();
@@ -272,13 +328,7 @@ impl Template {
egui::ComboBox::from_id_salt(format!("field_type_{i}"))
.selected_text(format!("{:?}", field.field_type))
.show_ui(ui, |ui| {
for variant in [
FieldType::SingleLine,
FieldType::MultiLine,
FieldType::Number,
FieldType::Date,
FieldType::Image,
] {
for variant in FieldType::types() {
if ui
.selectable_value(
&mut field.field_type,
@@ -299,6 +349,12 @@ impl Template {
}
ui.end_row();
ui.label("On Preview:");
if ui.checkbox(&mut field.on_preview, "").clicked() {
self.saved = false;
}
ui.end_row();
ui.label("Description:");
if ui
.text_edit_singleline(
@@ -330,22 +386,16 @@ impl Template {
.striped(true)
.show(ui, |ui| {
ui.label("Name:");
ui.text_edit_singleline(new_field_name);
ui.text_edit_singleline(&mut self.new_field_name);
ui.end_row();
ui.label("Type:");
egui::ComboBox::from_id_salt("new_field_type")
.selected_text(format!("{new_field_type:?}"))
.selected_text(format!("{:?}", self.new_field_type))
.show_ui(ui, |ui| {
for variant in [
FieldType::SingleLine,
FieldType::MultiLine,
FieldType::Number,
FieldType::Date,
FieldType::Image,
] {
for variant in FieldType::types() {
ui.selectable_value(
new_field_type,
&mut self.new_field_type,
variant.clone(),
format!("{variant:?}"),
);
@@ -354,40 +404,38 @@ impl Template {
ui.end_row();
ui.label("Required:");
ui.checkbox(new_field_required, "");
ui.checkbox(&mut self.new_field_required, "");
ui.end_row();
ui.label("On Preview:");
ui.checkbox(&mut self.new_field_on_preview, "");
ui.end_row();
ui.label("Description:");
ui.text_edit_singleline(new_field_description);
ui.text_edit_singleline(&mut self.new_field_description);
ui.end_row();
if ui.button("Add Field").clicked() && !new_field_name.is_empty() {
if ui.button("Add Field").clicked() && !self.new_field_name.is_empty() {
self.fields.push(FieldDefinition {
name: new_field_name.clone(),
field_type: new_field_type.clone(),
required: *new_field_required,
description: if new_field_description.is_empty() {
name: self.new_field_name.clone(),
field_type: self.new_field_type.clone(),
on_preview: self.new_field_on_preview,
required: self.new_field_required,
description: if self.new_field_description.is_empty() {
None
} else {
Some(new_field_description.clone())
Some(self.new_field_description.clone())
},
});
self.saved = false;
// Reset new field form
new_field_name.clear();
*new_field_required = false;
new_field_description.clear();
self.new_field_name.clear();
self.new_field_required = false;
self.new_field_description.clear();
}
});
});
}
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct FieldValue {
pub value: String,
#[serde(skip)]
pub modified: bool,
}
-22
View File
@@ -1,22 +0,0 @@
pub struct Error {
message: String,
visible: bool,
}
impl Error {
pub fn new(message: String) -> Self {
Self {
message,
visible: true,
}
}
pub fn show(&mut self, ui: &mut egui::Ui) {
egui::Window::new("Error")
.open(&mut self.visible)
.fixed_size([200.0, 100.0])
.show(ui.ctx(), |ui| {
ui.label(self.message.clone());
});
}
}
+347 -134
View File
@@ -1,18 +1,39 @@
use egui::RichText;
use itertools::Itertools;
use std::fs::{self, DirEntry};
// use walkdir::{DirEntry, WalkDir};
use crate::{
PROJECT_FOLDER, RightPanelContent,
main_editor::MainEditor,
note::Note,
object::ObjectInstance,
template::{FieldType, Template},
content_editor::MainEditor,
editors::{
asset_editor::Asset, content_editor::ContentSection, object_editor::ObjectInstance,
tags::Tag, template_editor::Template,
},
note_editor::Note,
};
pub struct Explorer {}
pub struct Explorer {
templates: Vec<Template>,
objects: Vec<ObjectInstance>,
notes: Vec<Note>,
documents: Vec<MainEditor>,
tags: Vec<Tag>,
}
impl Explorer {
pub fn new() -> Self {
Self {}
Self {
templates: Vec::new(),
objects: Vec::new(),
notes: Vec::new(),
documents: Vec::new(),
tags: Vec::new(),
}
}
pub fn objects(&self) -> Vec<ObjectInstance> {
self.objects.clone()
}
pub fn ui(
@@ -21,151 +42,329 @@ impl Explorer {
load_doc: &mut Option<MainEditor>,
ui: &mut egui::Ui,
) {
let (templates, objects) = match Self::load_templates() {
Ok((templates, objects)) => (templates, objects),
Err(e) => {
eprintln!("Failed to load project: {e}");
ui.label(RichText::new("Failed to load project").color(egui::Color32::RED));
return;
}
};
self.load_templates().expect("Failed to load templates");
self.load_objects().expect("Failed to load objects");
self.load_notes().expect("Failed to load notes");
self.load_documents().expect("Failed to load documents");
self.load_tags().expect("Failed to load tags");
ui.vertical(|ui| {
egui::collapsing_header::CollapsingState::load_with_default_open(
ui.ctx(),
ui.make_persistent_id("templates"),
true,
)
.show_header(ui, |ui| {
ui.horizontal(|ui| {
ui.label("Templates");
if ui.button("+").clicked() {
*to_load = Some(RightPanelContent::Template {
template: Box::new(Template::default()),
new_field_name: Default::default(),
new_field_type: FieldType::SingleLine,
new_field_required: false,
new_field_description: Default::default(),
});
}
});
})
.body(|ui| {
for template in &templates {
let id = ui.make_persistent_id(template.name.clone());
egui::collapsing_header::CollapsingState::load_with_default_open(
ui.ctx(),
id,
true,
)
.show_header(ui, |ui| {
// load the template
if ui.selectable_label(false, template.name.clone()).clicked() {
*to_load = Some(RightPanelContent::template(Some(template.clone())));
}
// create a new object based on this template
if ui.button("+").clicked() {
*to_load = Some(RightPanelContent::Object {
object: Box::new(ObjectInstance::new(template)),
});
}
})
.body(|ui| {
for object in &objects {
if object.template_id == template.id {
ui.horizontal(|ui| {
ui.add_space(10.0);
// load the object
if ui.selectable_label(false, &object.name).clicked() {
*to_load = Some(RightPanelContent::Object {
object: Box::new(object.clone()),
});
}
});
}
}
});
}
});
let notes = Self::load_notes().unwrap();
egui::collapsing_header::CollapsingState::load_with_default_open(
ui.ctx(),
ui.make_persistent_id("notes"),
true,
)
.show_header(ui, |ui| {
ui.horizontal(|ui| {
ui.label("Notes");
if ui.button("+").clicked() {
*to_load = Some(RightPanelContent::Note {
note: Box::new(Note::default()),
});
}
});
})
.body(|ui| {
for note in &notes {
ui.horizontal(|ui| {
ui.add_space(10.0);
// load the note
if ui.selectable_label(false, &note.name).clicked() {
*to_load = Some(RightPanelContent::Note {
note: Box::new(note.clone()),
});
}
});
}
});
let documents = Self::load_documents().unwrap();
egui::CollapsingHeader::new("Projects").show(ui, |ui| {
for document in &documents {
ui.horizontal(|ui| {
ui.add_space(10.0);
// load the document
if ui.selectable_label(false, &document.name).clicked() {
*load_doc = Some(document.clone());
}
});
}
});
self.render_templates(ui, to_load);
self.render_notes(ui, to_load);
self.render_doc_root(ui, load_doc);
self.render_tags(ui, to_load);
self.render_assets(ui, to_load);
});
}
fn load_templates() -> std::io::Result<(Vec<Template>, Vec<ObjectInstance>)> {
let mut templates = Vec::new();
let mut objects = Vec::new();
fn render_templates(&mut self, ui: &mut egui::Ui, to_load: &mut Option<RightPanelContent>) {
egui::collapsing_header::CollapsingState::load_with_default_open(
ui.ctx(),
ui.make_persistent_id("templates"),
true,
)
.show_header(ui, |ui| {
ui.horizontal(|ui| {
ui.label("Templates");
if ui.button("+").clicked() {
*to_load = Some(RightPanelContent::template(Some(Template::default())));
}
});
})
.body(|ui| {
for template in &self.templates {
let id = ui.make_persistent_id(template.name.clone());
egui::collapsing_header::CollapsingState::load_with_default_open(
ui.ctx(),
id,
true,
)
.show_header(ui, |ui| {
// load the template
if ui.selectable_label(false, template.name.clone()).clicked() {
*to_load = Some(RightPanelContent::template(Some(template.clone())));
}
for entry in std::fs::read_dir(PROJECT_FOLDER.join("templates")).unwrap() {
// create a new object based on this template
if ui.button("+").clicked() {
*to_load = Some(RightPanelContent::object(Some(ObjectInstance::new(
template,
))));
}
})
.body(|ui| {
for object in &self.objects {
if object.template_id == template.id {
ui.horizontal(|ui| {
ui.add_space(10.0);
// load the object
if ui.selectable_label(false, &object.name).clicked() {
*to_load =
Some(RightPanelContent::object(Some(object.clone())));
}
});
}
}
});
}
});
}
fn render_notes(&mut self, ui: &mut egui::Ui, to_load: &mut Option<RightPanelContent>) {
egui::collapsing_header::CollapsingState::load_with_default_open(
ui.ctx(),
ui.make_persistent_id("notes"),
true,
)
.show_header(ui, |ui| {
ui.horizontal(|ui| {
ui.label("Notes");
if ui.button("+").clicked() {
*to_load = Some(RightPanelContent::note(Some(Note::default())));
}
});
})
.body(|ui| {
for note in &self.notes {
ui.horizontal(|ui| {
ui.add_space(10.0);
// load the note
if ui.selectable_label(false, &note.name).clicked() {
*to_load = Some(RightPanelContent::note(Some(note.clone())));
}
});
}
});
}
fn render_doc_root(&self, ui: &mut egui::Ui, load_doc: &mut Option<MainEditor>) {
egui::collapsing_header::CollapsingState::load_with_default_open(
ui.ctx(),
ui.make_persistent_id("projects"),
true,
)
.show_header(ui, |ui| {
ui.horizontal(|ui| {
ui.label("Projects");
if ui.button("+").clicked() {
*load_doc = Some(MainEditor::open(ContentSection::new()));
}
});
})
.body(|ui| {
// Convert MainEditor vec to ContentSection vec
let content_sections: Vec<ContentSection> = self
.documents
.iter()
.map(|doc| doc.content.clone())
.collect();
Self::render_doc_branch(ui, &content_sections, None, load_doc);
});
}
/// Recursively renders a tree of documents.
///
/// Each document is represented by a single element in the `documents` array.
/// The `parent_id` parameter is used to filter out documents that do not have the current
/// parent. If `parent_id` is `None`, all documents are rendered.
///
/// `load_doc` is a mutable reference to a `MainEditor`. When a document is clicked, it
/// is loaded into the `MainEditor` and returned as `Some`.
fn render_doc_branch(
ui: &mut egui::Ui,
documents: &[ContentSection],
parent_id: Option<&str>,
load_doc: &mut Option<MainEditor>,
) {
// Filter documents that have the current parent (or no parent if this is the root)
let child_docs: Vec<&ContentSection> = documents
.iter()
.filter(|doc| doc.parent.as_deref() == parent_id)
.collect();
for doc in child_docs {
egui::collapsing_header::CollapsingState::load_with_default_open(
ui.ctx(),
ui.make_persistent_id(&doc.id),
false,
)
.show_header(ui, |ui| {
ui.horizontal(|ui| {
// Document title
if ui.selectable_label(false, &doc.title).clicked() {
*load_doc = Some(MainEditor::open(doc.clone()));
}
// Add child button
if ui.button("+").clicked() {
let child = doc.create_child();
*load_doc = Some(MainEditor::open(child));
}
});
})
.body(|ui| {
// recursive call to render the next level of documents
Self::render_doc_branch(ui, documents, Some(&doc.id), load_doc);
});
}
}
fn render_tags(&mut self, ui: &mut egui::Ui, to_load: &mut Option<RightPanelContent>) {
egui::collapsing_header::CollapsingState::load_with_default_open(
ui.ctx(),
ui.make_persistent_id("tags"),
true,
)
.show_header(ui, |ui| {
ui.horizontal(|ui| {
ui.label("Tags");
if ui.button("+").clicked() {
*to_load = Some(RightPanelContent::Tag(Tag::default()));
}
});
})
.body(|ui| {
for tag in &mut self.tags {
ui.horizontal(|ui| {
ui.add_space(10.0);
// load the tag
if tag.list_ui(ui).clicked() {
*to_load = Some(RightPanelContent::Tag(tag.clone()));
}
});
}
});
}
fn render_assets(&mut self, ui: &mut egui::Ui, to_load: &mut Option<RightPanelContent>) {
egui::collapsing_header::CollapsingState::load_with_default_open(
ui.ctx(),
ui.make_persistent_id("assets"),
true,
)
.show_header(ui, |ui| {
ui.horizontal(|ui| {
ui.label("Assets");
});
})
.body(|ui| {
let entries = fs::read_dir(PROJECT_FOLDER.join("assets"))
.unwrap()
.filter_map(Result::ok)
.sorted_by(|a, b| {
// Directories first, then files
let a_is_dir = a.file_type().unwrap().is_dir();
let b_is_dir = b.file_type().unwrap().is_dir();
if a_is_dir == b_is_dir {
a.file_name().cmp(&b.file_name())
} else if a_is_dir {
std::cmp::Ordering::Less
} else {
std::cmp::Ordering::Greater
}
})
.collect::<Vec<_>>();
for entry in entries {
Self::render_entry(ui, to_load, &entry);
}
});
}
fn render_entry(ui: &mut egui::Ui, to_load: &mut Option<RightPanelContent>, entry: &DirEntry) {
let file_type = entry.file_type().unwrap();
let is_dir = file_type.is_dir();
let file_name = entry.file_name().to_str().unwrap().to_string();
let path = entry.path();
if is_dir {
let entries = fs::read_dir(path)
.unwrap()
.filter_map(Result::ok)
.collect::<Vec<_>>();
egui::collapsing_header::CollapsingState::load_with_default_open(
ui.ctx(),
ui.make_persistent_id(&file_name),
false,
)
.show_header(ui, |ui| {
ui.horizontal(|ui| {
ui.label(file_name);
let _clicked = ui.button("+").on_hover_text("Add new item").clicked();
});
})
.body(|ui| {
// recursive call to render the next level of documents
for entry in entries {
Self::render_entry(ui, to_load, &entry);
}
});
} else {
// Handle file
if ui
.selectable_label(false, format!("📄 {file_name}"))
.clicked()
{
// use asset::load to get the file at the path
let asset_path = path.strip_prefix(PROJECT_FOLDER.join("assets")).unwrap();
let asset = Asset::open(asset_path.to_string_lossy().to_string());
*to_load = Some(RightPanelContent::Asset(Box::new(asset)));
}
}
}
// load templates from the templates folder
fn load_templates(&mut self) -> std::io::Result<()> {
let templates_folder = PROJECT_FOLDER.join("templates");
if !templates_folder.exists() {
std::fs::create_dir_all(&templates_folder)?;
}
let mut templates = Vec::new();
for entry in std::fs::read_dir(&templates_folder).unwrap() {
let path = entry.unwrap().path();
match Template::load(path.file_stem().unwrap().to_str().unwrap()) {
Ok(t) => templates.push(t),
Err(err) => eprintln!("Could not parse file {path:?}: {err}"),
}
}
self.templates = templates;
for entry in std::fs::read_dir(PROJECT_FOLDER.join("objects")).unwrap() {
Ok(())
}
// load objects from the objects folder
fn load_objects(&mut self) -> std::io::Result<()> {
let objects_folder = PROJECT_FOLDER.join("objects");
if !objects_folder.exists() {
std::fs::create_dir_all(&objects_folder)?;
}
let mut objects = Vec::new();
for entry in std::fs::read_dir(&objects_folder).unwrap() {
let path = entry.unwrap().path();
match ObjectInstance::load(path.file_stem().unwrap().to_str().unwrap()) {
Ok(o) => objects.push(o),
Err(err) => eprintln!("Could not parse file {path:?}: {err}"),
}
}
self.objects = objects;
Ok((templates, objects))
Ok(())
}
fn load_notes() -> std::io::Result<Vec<Note>> {
// load notes from the notes folder
fn load_notes(&mut self) -> std::io::Result<()> {
let notes_folder = PROJECT_FOLDER.join("notes");
if !notes_folder.exists() {
std::fs::create_dir_all(&notes_folder)?;
}
let mut notes = Vec::new();
for entry in std::fs::read_dir(PROJECT_FOLDER.join("notes")).unwrap() {
for entry in std::fs::read_dir(&notes_folder).unwrap() {
let path = entry.unwrap().path();
match Note::load(path.file_stem().unwrap().to_str().unwrap()) {
Ok(note) => notes.push(note),
@@ -173,20 +372,34 @@ impl Explorer {
}
}
Ok(notes)
self.notes = notes;
Ok(())
}
fn load_documents() -> std::io::Result<Vec<MainEditor>> {
// load documents from the documents folder
fn load_documents(&mut self) -> std::io::Result<()> {
let documents_folder = PROJECT_FOLDER.join("documents");
if !documents_folder.exists() {
std::fs::create_dir_all(&documents_folder)?;
}
let mut documents = Vec::new();
for entry in std::fs::read_dir(PROJECT_FOLDER.join("documents")).unwrap() {
for entry in std::fs::read_dir(&documents_folder).unwrap() {
let path = entry.unwrap().path();
match MainEditor::load(path.file_stem().unwrap().to_str().unwrap()) {
Ok(document) => documents.push(document),
match ContentSection::load(path.file_stem().unwrap().to_str().unwrap()) {
Ok(document) => documents.push(MainEditor::open(document)),
Err(err) => eprintln!("Could not parse file {path:?}: {err}"),
}
}
Ok(documents)
self.documents = documents;
Ok(())
}
fn load_tags(&mut self) -> std::io::Result<()> {
self.tags = Tag::load_all();
Ok(())
}
}
+103
View File
@@ -0,0 +1,103 @@
use std::{
collections::HashMap,
io,
path::{Path, PathBuf},
sync::LazyLock,
};
use serde::{Serialize, de::DeserializeOwned};
#[cfg(feature = "native")]
use crate::PROJECT_FOLDER;
use crate::filesystem::native::NativeFileSystem;
#[cfg(feature = "native")]
pub mod native;
#[cfg(feature = "web")]
pub mod web;
pub static FILESYSTEM: LazyLock<NativeFileSystem> = LazyLock::new(|| {
#[cfg(feature = "native")]
return NativeFileSystem::new(PROJECT_FOLDER.clone());
#[cfg(feature = "web")]
return Box::new(web::WebFileSystem::new());
});
pub trait LegacyFileSystem {
fn read<T: DeserializeOwned>(&self, path: &Path) -> Result<T, FsError>;
fn read_bytes(&self, path: &Path) -> Result<Vec<u8>, FsError>;
fn write<T: Serialize>(&self, path: &Path, data: T) -> Result<(), FsError>;
fn delete(&self, path: &Path) -> Result<(), FsError>;
fn mkdir(&self, path: &Path) -> Result<(), FsError>;
fn rename(&self, path: &Path, new_path: &Path) -> Result<(), FsError>;
#[allow(unused)]
fn exists(&self, path: &Path) -> bool;
fn config_path(&self) -> PathBuf;
}
// ────────────────────────────────────────────────────────────────
// Custom error type
// ────────────────────────────────────────────────────────────────
#[derive(Debug)]
pub enum FsError {
Io(io::Error),
Serde(serde_json::Error),
}
impl std::fmt::Display for FsError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
FsError::Io(e) => write!(f, "IO error: {e}"),
FsError::Serde(e) => write!(f, "Serialization error: {e}"),
}
}
}
impl std::error::Error for FsError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
FsError::Io(e) => Some(e),
FsError::Serde(e) => Some(e),
}
}
}
// Convert the two underlying error types into our own
impl From<io::Error> for FsError {
fn from(err: io::Error) -> Self {
FsError::Io(err)
}
}
impl From<serde_json::Error> for FsError {
fn from(err: serde_json::Error) -> Self {
FsError::Serde(err)
}
}
#[allow(dead_code)]
pub struct Id(String);
#[allow(dead_code)]
pub trait FileSystem {
fn load<T: DeserializeOwned>(&self, id: Id) -> Result<T, FsError>;
fn save<T: Serialize>(&self, id: Id, data: T) -> Result<(), FsError>;
fn mkdir(&self, path: Path) -> Result<(), FsError>;
fn exists(&self, path: Path) -> bool;
}
#[allow(dead_code)]
pub struct Index {
file_cache: HashMap<Id, PathBuf>,
project_root: Directory,
}
#[allow(dead_code)]
pub struct Directory {
name: String,
id: Id,
children: HashMap<Id, Directory>,
files: Vec<Id>,
}
+105
View File
@@ -0,0 +1,105 @@
// ────────────────────────────────────────────────────────────────
// Imports
// ────────────────────────────────────────────────────────────────
use std::fs;
use std::io::{ErrorKind, Read};
use std::path::{Path, PathBuf};
use serde::Serialize;
use serde::de::DeserializeOwned;
use crate::filesystem::{FsError, LegacyFileSystem};
// ────────────────────────────────────────────────────────────────
// Concrete implementation
// ────────────────────────────────────────────────────────────────
/// The concrete filesystem. All paths are interpreted relative to
/// `project_root`.
pub struct NativeFileSystem {
project_root: PathBuf,
}
impl NativeFileSystem {
/// Create a new instance.
pub fn new(root: impl Into<PathBuf>) -> Self {
Self {
project_root: root.into(),
}
}
/// Resolve the user supplied *relative* path against the project root.
#[inline]
fn full_path(&self, path: &Path) -> PathBuf {
self.project_root.join(path)
}
}
// ────────────────────────────────────────────────────────────────
// Implementation of the trait
// ────────────────────────────────────────────────────────────────
impl LegacyFileSystem for NativeFileSystem {
fn read<T: DeserializeOwned>(&self, path: &Path) -> Result<T, FsError> {
let full_path = self.full_path(path);
let file = fs::File::open(full_path).map_err(FsError::Io)?;
serde_json::from_reader(file).map_err(FsError::Serde)
}
fn read_bytes(&self, path: &Path) -> Result<Vec<u8>, FsError> {
let full_path = self.full_path(path);
let mut contents = Vec::new();
fs::File::open(full_path)?.read_to_end(&mut contents)?;
Ok(contents)
}
fn write<T: Serialize>(&self, path: &Path, data: T) -> Result<(), FsError> {
let full_path = self.full_path(path);
// Ensure the parent directory exists.
if let Some(parent) = full_path.parent() {
fs::create_dir_all(parent)?;
}
let file = fs::File::create(full_path)?;
serde_json::to_writer(file, &data).map_err(FsError::Serde)
}
fn delete(&self, path: &Path) -> Result<(), FsError> {
let full_path = self.full_path(path);
match fs::remove_file(&full_path) {
Ok(()) => Ok(()),
Err(e) if e.kind() == ErrorKind::IsADirectory => {
// Remove a directory tree.
fs::remove_dir_all(full_path).map_err(FsError::Io)
}
Err(e) => Err(FsError::Io(e)),
}
}
fn mkdir(&self, path: &Path) -> Result<(), FsError> {
let full_path = self.full_path(path);
fs::create_dir_all(full_path).map_err(FsError::Io)
}
fn exists(&self, path: &Path) -> bool {
let full_path = self.full_path(path);
full_path.exists()
}
fn rename(&self, path: &Path, other: &Path) -> Result<(), FsError> {
let full_path = self.full_path(path);
let full_other = self.full_path(other);
fs::rename(full_path, full_other).map_err(FsError::Io)
}
fn config_path(&self) -> PathBuf {
match std::env::var("HOME") {
Ok(path) => PathBuf::from(path + "/.config/worldcoder/settings.json"),
Err(_) => {
eprintln!(
"XDG_CONFIG_HOME not set, using default path of ~/.config/worldcoder/settings.json"
);
"~/.config/worldcoder/settings.json".into()
}
}
}
}
+536
View File
@@ -0,0 +1,536 @@
use std::{
io::{BufRead, BufReader},
sync::{Arc, Mutex},
};
use serde::{Deserialize, Serialize};
use crate::editors::settings_editor::ProjectSettings;
#[derive(Clone)]
pub struct ContentAI {
pub open: bool,
// model input
pub content: String,
pub instruction: String,
pub context_override: String,
pub system_prompt: String,
// model settings
pub max_tokens: usize,
pub temperature: f32,
pub reasoning_effort: ReasoningEffort,
pub model_override: String,
// model output
pub reasoning: Arc<Mutex<String>>,
pub result: Arc<Mutex<String>>,
pub ready: Arc<Mutex<ReadyState>>,
}
impl ContentAI {
pub fn new(content: String) -> Self {
Self {
// model input
content,
instruction: String::new(),
context_override: String::new(),
system_prompt: String::new(),
// model settings
max_tokens: 2048,
reasoning_effort: ReasoningEffort::default(),
temperature: 0.7,
model_override: String::new(),
reasoning: Arc::new(Mutex::new(String::new())),
// output
result: Arc::new(Mutex::new(String::new())),
ready: Arc::new(Mutex::new(ReadyState::Idle)),
// ui
open: true,
}
}
pub fn ui(&mut self, ui: &mut egui::Ui, project: &mut ProjectSettings) {
let is_open = self.open;
if is_open {
egui::SidePanel::right("ai_assistant").show_inside(ui, |ui| {
Self::ui_main(self, ui, project);
});
}
self.open = is_open;
}
fn ui_output_box(&mut self, ui: &mut egui::Ui, project: &mut ProjectSettings) {
let mut ready_lock = self.ready.lock().unwrap();
ui.horizontal(|ui| {
if *ready_lock == ReadyState::Generating {
if ui.button("Cancel").clicked() {
*ready_lock = ReadyState::Halted;
}
if ui.button("Stop").clicked() {
*ready_lock = ReadyState::Idle;
}
ui.spinner();
ui.label("Generating...");
}
if *ready_lock == ReadyState::Idle {
let continue_content = || {
let content = self.content.clone();
let project = project.clone();
let result = self.result.clone();
let reasoning = self.reasoning.clone();
let ready = self.ready.clone();
let options = AIOptions {
max_completion_tokens: self.max_tokens,
reasoning_effort: self.reasoning_effort,
temperature: self.temperature,
model_override: if !self.model_override.is_empty() {
Some(self.model_override.clone())
} else {
None
},
};
let ai_input = AIInput {
system_prompt: self.system_prompt.clone(),
user_prompt: format!(
"{}\n\n{} {}",
self.instruction.clone(),
project.ai_context.clone(),
self.context_override.clone()
),
previous_content: content.clone(),
structure: None,
};
result.lock().unwrap().clear();
std::thread::spawn(move || {
let result = crate::llm_integration::content_llm::continue_content(
ai_input,
options,
project,
result,
reasoning,
ready.clone(),
);
if let Err(e) = result {
eprintln!("Error in content generation: {e}");
}
});
};
if ui.button("Generate ").clicked() {
continue_content();
}
ui.label("Idle");
}
// show regardless of state
if ui.button("Insert").clicked() {
*ready_lock = ReadyState::Ready;
}
if ui.button("Clear").clicked() {
self.result.lock().unwrap().clear();
self.reasoning.lock().unwrap().clear();
}
});
ui.spacing();
ui.vertical(|ui| {
egui::TopBottomPanel::top("reasoning_output")
.resizable(true)
.show_inside(ui, |ui| {
egui::ScrollArea::both()
.auto_shrink([false, true])
.id_salt("reasoning_output")
.max_width(ui.available_width())
// .max_height(ui.available_height() / 3.0)
.show(ui, |ui| {
ui.add(
egui::TextEdit::multiline(&mut *self.reasoning.lock().unwrap())
.font(egui::TextStyle::Monospace)
.interactive(false)
.desired_rows(5)
.frame(false)
.desired_width(ui.available_width())
.lock_focus(true)
.hint_text("Reasoning will appear here..."),
);
});
});
egui::ScrollArea::both()
.auto_shrink([false, false])
.id_salt("llm_output")
.max_width(ui.available_width())
.show(ui, |ui| {
ui.add(
egui::TextEdit::multiline(&mut *self.result.lock().unwrap())
.font(egui::TextStyle::Monospace)
.interactive(false)
.desired_rows(0)
.frame(false)
.desired_width(ui.available_width())
.lock_focus(true)
.hint_text("Content will appear here..."),
);
});
});
}
fn ui_main(&mut self, ui: &mut egui::Ui, project: &mut ProjectSettings) {
{
ui.weak("(The model will see current file content)");
egui::CollapsingHeader::new("Settings")
.default_open(true)
.show(ui, |ui| {
egui::Grid::new("continue_grid")
.num_columns(2)
.striped(true)
.show(ui, |ui| {
ui.label("Max Tokens");
ui.add(
egui::DragValue::new(&mut self.max_tokens)
.range(128..=u32::MAX)
.speed(128),
);
ui.end_row();
ui.label("Temperature");
ui.add(
egui::DragValue::new(&mut self.temperature)
.range(0.0..=2.0)
.speed(0.1),
);
ui.label("Reasoning effort");
egui::ComboBox::from_id_salt("reasoning_effort")
.selected_text(self.reasoning_effort.to_string())
.show_ui(ui, |ui| {
ui.selectable_value(
&mut self.reasoning_effort,
ReasoningEffort::Minimal,
"Minimal",
);
ui.selectable_value(
&mut self.reasoning_effort,
ReasoningEffort::Low,
"Low",
);
ui.selectable_value(
&mut self.reasoning_effort,
ReasoningEffort::Medium,
"Medium",
);
ui.selectable_value(
&mut self.reasoning_effort,
ReasoningEffort::High,
"High",
);
});
ui.end_row();
ui.label("Model override");
ui.add(egui::TextEdit::singleline(&mut self.model_override));
ui.end_row();
});
});
egui::TopBottomPanel::top("continue_instruction")
.resizable(true)
.show_inside(ui, |ui| {
egui::CollapsingHeader::new("Instructions")
.default_open(true)
.show(ui, |ui| {
egui::ScrollArea::vertical()
.auto_shrink([false, false])
.max_height(ui.available_height())
.show(ui, |ui| {
ui.add(
egui::TextEdit::multiline(&mut self.instruction)
.frame(false)
.desired_width(ui.available_width())
.hint_text("Writing Instructions"),
);
});
});
});
egui::TopBottomPanel::top("continue_context")
.resizable(true)
.show_inside(ui, |ui| {
egui::CollapsingHeader::new("Context")
.default_open(true)
.show(ui, |ui| {
egui::ScrollArea::vertical()
.auto_shrink([false, false])
.max_height(ui.available_height())
.show(ui, |ui| {
ui.add(
egui::TextEdit::multiline(&mut self.context_override)
.frame(false)
.desired_width(ui.available_width())
.hint_text("Any additional context?"),
);
});
});
});
egui::TopBottomPanel::top("continue_system_prompt")
.resizable(true)
.show_inside(ui, |ui| {
egui::CollapsingHeader::new("System prompt")
.default_open(true)
.show(ui, |ui| {
egui::ScrollArea::vertical()
.auto_shrink([false, false])
.max_height(ui.available_height())
.show(ui, |ui| {
ui.add(
egui::TextEdit::multiline(&mut self.system_prompt)
.frame(false)
.desired_width(ui.available_width())
.hint_text("System prompt"),
);
});
});
});
self.ui_output_box(ui, project);
}
}
}
#[allow(clippy::too_many_arguments)]
pub fn continue_content(
ai_input: AIInput,
// context: String,
// previous_content: String,
// instruction: String,
options: AIOptions,
project: ProjectSettings,
result: Arc<Mutex<String>>,
reasoning: Arc<Mutex<String>>,
ready: Arc<Mutex<ReadyState>>,
) -> Result<(), Box<dyn std::error::Error>> {
*ready.lock().unwrap() = ReadyState::Generating;
let client = reqwest::blocking::Client::new();
let messages = vec![
Message {
role: "system".to_string(),
content: ai_input.system_prompt,
},
Message {
role: "user".to_string(),
content: format!(
"<Instructions> {}\n\n<Previous content> {}\n\n",
ai_input.user_prompt, ai_input.previous_content
),
},
];
let request = ChatRequest {
messages,
temperature: options.temperature,
max_tokens: options.max_completion_tokens,
model: options.model_override,
reasoning_effort: options.reasoning_effort,
stream: true,
};
let llm_api_uri = if let Some(uri) = project.local_overrides.llm_api_uri {
uri
} else {
project.global_settings.llm_api_uri.unwrap()
};
let api_key = if let Some(key) = project.local_overrides.llm_api_key {
if key.is_empty() { None } else { Some(key) }
} else if let Some(key) = project.global_settings.llm_api_key {
if key.is_empty() { None } else { Some(key) }
} else {
return Err("No API key found".into());
};
let response = if let Some(k) = api_key {
client
.post(llm_api_uri + "/api/v0/chat/completions")
.json(&request)
.bearer_auth(k)
.send()?
} else {
client
.post(llm_api_uri + "/api/v0/chat/completions")
.json(&request)
.send()?
};
println!("success!");
// println!("response: {}", response.text().unwrap());
let reader = BufReader::new(response);
for line in reader.lines() {
// initial loop to check if the user has terminated the generation
{
let mut ready = ready.lock().unwrap();
if *ready == ReadyState::Halted {
result.lock().unwrap().clear();
reasoning.lock().unwrap().clear();
}
if *ready != ReadyState::Generating {
*ready = ReadyState::Idle;
break;
}
}
let line = line?;
if line == "data: [DONE]" {
break;
}
if let Some(json) = line.strip_prefix("data: ") {
if let Ok(chunk) = serde_json::from_str::<StreamingChatResponse>(json) {
println!("chunk: {chunk:?}");
if let Some(content) = chunk.choices[0].delta.content.as_ref() {
println!("content: {content}");
result.lock().unwrap().push_str(content);
}
if let Some(reasoning_content) = chunk.choices[0].delta.reasoning_content.as_ref() {
println!("reasoning_content: {reasoning_content}");
reasoning.lock().unwrap().push_str(reasoning_content);
}
}
}
}
*ready.lock().unwrap() = ReadyState::Idle;
Ok(())
}
pub struct AIOptions {
pub max_completion_tokens: usize,
pub temperature: f32,
pub reasoning_effort: ReasoningEffort,
pub model_override: Option<String>,
}
pub struct AIInput {
pub system_prompt: String,
pub user_prompt: String,
pub previous_content: String,
#[allow(unused)]
pub structure: Option<String>,
}
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum ReadyState {
Idle,
Generating,
Ready,
Halted,
}
#[derive(Serialize, Copy, Clone, PartialEq, Default)]
pub enum ReasoningEffort {
#[serde(rename = "minimal")]
Minimal,
#[default]
#[serde(rename = "low")]
Low,
#[serde(rename = "medium")]
Medium,
#[serde(rename = "high")]
High,
}
impl std::fmt::Display for ReasoningEffort {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ReasoningEffort::Minimal => write!(f, "Minimal"),
ReasoningEffort::Low => write!(f, "Low"),
ReasoningEffort::Medium => write!(f, "Medium"),
ReasoningEffort::High => write!(f, "High"),
}
}
}
// Simple request structure
#[derive(Serialize)]
struct ChatRequest {
messages: Vec<Message>,
temperature: f32,
max_tokens: usize,
stream: bool,
reasoning_effort: ReasoningEffort,
// if we give the API model:null it returns 500
#[serde(skip_serializing_if = "Option::is_none")]
model: Option<String>,
}
// Streaming response structures
#[derive(Deserialize, Debug)]
struct StreamingChatResponse {
choices: Vec<StreamingChoice>,
}
#[derive(Deserialize, Debug)]
struct StreamingChoice {
delta: Delta,
#[serde(default)]
#[allow(unused)]
finish_reason: Option<String>,
}
#[derive(Deserialize, Debug)]
struct Delta {
#[serde(default)]
#[allow(unused)]
role: Option<String>,
#[serde(default)]
content: Option<String>,
#[serde(default)]
reasoning_content: Option<String>,
}
#[derive(Serialize, Deserialize, Debug)]
struct Message {
role: String,
content: String,
}
#[derive(Deserialize, Debug)]
struct ChatResponse {
#[allow(unused)]
choices: Vec<Choice>,
}
#[derive(Deserialize, Debug)]
struct Choice {
#[allow(unused)]
message: Message,
}
+1
View File
@@ -0,0 +1 @@
pub mod content_llm;
+157 -153
View File
@@ -1,20 +1,28 @@
use std::{fs, path::PathBuf, sync::LazyLock};
#![windows_subsystem = "windows"]
use egui::{RichText, ScrollArea};
use std::{path::PathBuf, sync::LazyLock};
mod error;
use egui::ScrollArea;
mod editors;
mod explorer;
mod main_editor;
mod note;
mod object;
mod scene;
mod template;
use egui_file::DialogType;
use object::ObjectInstance;
use template::{FieldType, Template};
use crate::{explorer::Explorer, main_editor::MainEditor, note::Note};
#[cfg(feature = "llm")]
mod llm_integration;
mod util;
mod filesystem;
use crate::{
editors::{
asset_editor::Asset, content_editor, note_editor, object_editor::ObjectInstance,
settings_editor::ProjectSettings, tags::Tag, template_editor::Template,
},
explorer::Explorer,
};
static VERSION: &str = "0.1.0";
static PROJECT_FOLDER: LazyLock<PathBuf> = LazyLock::new(|| {
let mut path = std::env::current_dir().unwrap();
path.push("project");
@@ -23,131 +31,110 @@ static PROJECT_FOLDER: LazyLock<PathBuf> = LazyLock::new(|| {
fn main() {
let app = Interface::new();
let options = eframe::NativeOptions {
viewport: egui::ViewportBuilder::default().with_inner_size([800.0, 600.0]),
..Default::default()
};
let _ = eframe::run_native("Code Editor", options, Box::new(|_cc| Ok(Box::new(app))));
if let Err(e) = eframe::run_native("World Coder", options, Box::new(|_cc| Ok(Box::new(app)))) {
eprintln!("Failed to run app: {e}");
}
eprintln!("App closed");
}
pub struct Interface {
dialog: Option<egui_file::FileDialog>,
right_panel_content: RightPanelContent,
editor: main_editor::MainEditor,
scene: scene::EditorScene,
editor: content_editor::MainEditor,
explorer: Explorer,
project: ProjectSettings,
}
impl eframe::App for Interface {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui_extras::install_image_loaders(ctx);
self.configure_appearance(ctx);
self.render_top_panel(ctx);
self.render_left_panel(ctx);
self.render_right_panel(ctx);
self.render_main_content(ctx);
}
}
pub enum RightPanelContent {
Template(Box<Template>),
Object(Box<ObjectInstance>),
Note(Box<note_editor::Note>),
Tag(Tag),
Asset(Box<Asset>),
None,
}
impl RightPanelContent {
fn template(template: Option<Template>) -> Self {
Self::Template(Box::new(template.unwrap_or_default()))
}
fn object(instance: Option<ObjectInstance>) -> Self {
Self::Object(Box::new(instance.unwrap_or_default()))
}
fn note(note: Option<note_editor::Note>) -> Self {
Self::Note(Box::new(note.unwrap_or_default()))
}
}
impl Interface {
#[must_use]
pub fn new() -> Self {
Self {
dialog: None,
right_panel_content: RightPanelContent::None,
editor: main_editor::MainEditor::new(),
scene: scene::EditorScene::new(),
editor: content_editor::MainEditor::new(),
explorer: Explorer::new(),
project: ProjectSettings::load(),
}
}
}
impl Default for Interface {
fn default() -> Self {
Self::new()
}
}
// /home/zxq5/Pictures/logos and pfps/YT profile picture background.png
impl eframe::App for Interface {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui_extras::install_image_loaders(ctx);
{
let mut visuals = egui::Visuals::dark();
visuals.window_fill = egui::Color32::from_rgb(20, 20, 20);
visuals.panel_fill = egui::Color32::from_rgb(20, 20, 20);
ctx.set_visuals(visuals);
}
if let Some(dialog) = &mut self.dialog {
if dialog.show(ctx).selected() {
if let Some(path) = dialog.path() {
if dialog.dialog_type() == DialogType::OpenFile {
// Handle file dialog for loading templates/instances
if let Ok(object) =
ObjectInstance::load(path.file_stem().unwrap().to_str().unwrap())
{
// Instance
self.right_panel_content = RightPanelContent::instance(Some(object));
self.dialog = None;
} else if let Ok(template) =
Template::load(path.file_stem().unwrap().to_str().unwrap())
{
// Template
self.right_panel_content = RightPanelContent::template(Some(template));
self.dialog = None;
}
} else if dialog.dialog_type() == DialogType::SaveFile {
// Handle file dialog for saving templates/instances
if let RightPanelContent::Template { template, .. } =
&mut self.right_panel_content
{
// set the save location and save
if template.save().is_err() {
eprintln!("Failed to save template");
} else {
self.dialog = None;
}
}
}
}
}
}
fn render_top_panel(&mut self, ctx: &egui::Context) {
// Top bar with actions
egui::TopBottomPanel::top("top").show(ctx, |ui| {
ui.horizontal(|ui| {
// Open Markdown Editor button
if ui.button("📝 Open Editor").clicked() {
self.editor.show_editor = true;
}
// title widget
ui.heading("WorldCoder");
ui.separator();
// create new template
if ui.button("New Template").clicked() {
self.right_panel_content = RightPanelContent::Template {
template: Box::new(Template::default()),
new_field_name: String::new(),
new_field_type: FieldType::SingleLine,
new_field_required: false,
new_field_description: String::new(),
};
}
// version
ui.label(VERSION);
// load instance or template from file
if ui.button("Load Template/Instance").clicked() {
self.dialog = Some(egui_file::FileDialog::open_file(Some(
PROJECT_FOLDER.clone(),
)));
self.dialog.as_mut().unwrap().open();
// Settings
if ui.button("Settings").clicked() {
self.project.open();
}
});
});
if self.project.open {
let mut open = self.project.open;
egui::Window::new("Settings")
.open(&mut open)
.show(ctx, |ui| {
self.project.ui(ui);
});
self.project.open = open;
}
}
fn render_left_panel(&mut self, ctx: &egui::Context) {
// Left panel - File browser
egui::SidePanel::left("file_browser")
.resizable(true)
.default_width(250.0)
.show(ctx, |ui| {
ui.heading("Project Files");
ui.separator();
ui.heading("Explorer");
let mut to_load: Option<RightPanelContent> = None;
let mut load_doc: Option<MainEditor> = None;
let mut load_doc: Option<content_editor::MainEditor> = None;
ScrollArea::vertical().show(ui, |ui| {
self.explorer.ui(&mut to_load, &mut load_doc, ui);
@@ -159,25 +146,30 @@ impl eframe::App for Interface {
if let Some(load_doc) = load_doc {
self.editor = load_doc;
self.editor.show_editor = true;
self.editor.show_preview = true;
}
});
}
fn render_right_panel(&mut self, ctx: &egui::Context) {
// Main content area
egui::SidePanel::right("templates").show(ctx, |ui| {
let mut new_instance: Option<RightPanelContent> = None;
match &mut self.right_panel_content {
// an instance of a template
RightPanelContent::Object { object } => {
RightPanelContent::Object(object) => {
// load template from path
let mut right_panel = None;
let template = Template::load(&object.template_id).unwrap();
ScrollArea::vertical().show(ui, |ui| {
object.ui(ui, &template, &mut right_panel);
object.ui(
ui,
&template,
&mut right_panel,
&mut self.explorer.objects(),
);
});
if let Some(right_panel) = right_panel {
@@ -186,22 +178,15 @@ impl eframe::App for Interface {
}
// an editable template
RightPanelContent::Template {
template,
new_field_name,
new_field_type,
new_field_required,
new_field_description,
} => template.ui(
ui,
&mut new_instance,
new_field_name,
new_field_type,
new_field_required,
new_field_description,
),
RightPanelContent::Template(template) => {
template.ui(ui, &mut new_instance);
}
RightPanelContent::Note { note } => note.ui(ui),
RightPanelContent::Note(note) => note.ui(ui),
RightPanelContent::Tag(tag) => tag.ui(ui),
RightPanelContent::Asset(asset) => asset.ui(ui),
RightPanelContent::None => {
ui.centered_and_justified(|ui| {
@@ -217,44 +202,63 @@ impl eframe::App for Interface {
self.right_panel_content = new;
}
});
}
self.editor.ui(ctx);
self.scene.ui(ctx);
// render main content area
fn render_main_content(&mut self, ctx: &egui::Context) {
self.editor.ui(ctx, &mut self.project);
}
// configure appearance of UI elements
fn configure_appearance(&self, ctx: &egui::Context) {
// configure appearance of UI elements
let mut visuals = egui::Visuals::dark();
visuals.window_fill = egui::Color32::from_rgb(20, 20, 20);
visuals.panel_fill = egui::Color32::from_rgb(20, 20, 20);
visuals.widgets.inactive.fg_stroke =
egui::Stroke::from((1.0, egui::Color32::from_rgb(255, 255, 255)));
visuals.widgets.inactive.bg_stroke =
egui::Stroke::from((1.0, egui::Color32::from_rgb(60, 60, 60)));
visuals.widgets.inactive.corner_radius = egui::CornerRadius::from(4);
visuals.widgets.inactive.bg_fill = egui::Color32::from_rgb(20, 20, 20);
visuals.widgets.inactive.weak_bg_fill = egui::Color32::from_rgb(20, 20, 20);
visuals.widgets.inactive.expansion = 1.0;
ctx.set_visuals(visuals);
// setup fonts.
let mut fonts = egui::FontDefinitions::default();
fonts.font_data.insert(
"JetBrains Mono Nerd Font".to_string(),
std::sync::Arc::new(egui::FontData::from_static(include_bytes!(
"../font/JetBrainsMonoNerdFontMono_Regular.ttf",
))),
);
fonts
.families
.entry(egui::FontFamily::Proportional)
.or_default()
.insert(0, "JetBrains Mono Nerd Font".to_string());
fonts
.families
.entry(egui::FontFamily::Monospace)
.or_default()
.insert(0, "JetBrains Mono Nerd Font".to_string());
ctx.set_fonts(fonts);
}
}
pub enum RightPanelContent {
Template {
template: Box<Template>,
// fields to edit
new_field_name: String,
new_field_type: FieldType,
new_field_required: bool,
new_field_description: String,
},
Object {
object: Box<ObjectInstance>,
},
Note {
note: Box<Note>,
},
None,
}
impl RightPanelContent {
fn template(template: Option<Template>) -> Self {
Self::Template {
template: Box::new(template.unwrap_or_default()),
new_field_name: String::new(),
new_field_type: FieldType::SingleLine,
new_field_required: false,
new_field_description: String::new(),
}
}
fn instance(instance: Option<ObjectInstance>) -> Self {
Self::Object {
object: Box::new(instance.unwrap_or_default()),
}
impl Default for Interface {
fn default() -> Self {
Self::new()
}
}
impl Drop for Interface {
fn drop(&mut self) {
self.project.save();
}
}
-211
View File
@@ -1,211 +0,0 @@
use egui::{RichText, TextEdit, Ui};
use egui_commonmark::{CommonMarkCache, CommonMarkViewer};
use serde::{self, Deserialize, Serialize};
use crate::PROJECT_FOLDER;
#[derive(Serialize, Deserialize)]
pub struct MainEditor {
pub name: String,
pub text: String,
#[serde(skip)]
pub id: String,
#[serde(skip)]
saved: bool,
#[serde(skip)]
pub show_editor: bool,
#[serde(skip)]
pub show_preview: bool,
#[serde(skip)]
preview_cache: CommonMarkCache,
}
impl Clone for MainEditor {
fn clone(&self) -> Self {
Self {
name: self.name.clone(),
text: self.text.clone(),
id: self.id.clone(),
saved: self.saved,
show_editor: self.show_editor,
show_preview: self.show_preview,
preview_cache: CommonMarkCache::default(),
}
}
}
impl MainEditor {
pub fn new() -> Self {
Self {
text: String::new(),
id: uuid::Uuid::new_v4().to_string(),
name: "New Document".to_string(),
saved: false,
show_editor: false, // Start with editor hidden
show_preview: true,
preview_cache: CommonMarkCache::default(),
}
}
pub fn load(id: &str) -> Result<Self, Box<dyn std::error::Error>> {
let path = PROJECT_FOLDER
.join("documents")
.join(format!("{}.json", id));
let content = std::fs::read_to_string(&path)?;
let mut editor: Self = serde_json::from_str(&content)?;
editor.saved = true;
editor.id = id.to_string();
Ok(editor)
}
pub fn save(&mut self) -> Result<(), Box<dyn std::error::Error>> {
let path = PROJECT_FOLDER
.join("documents")
.join(format!("{}.json", &self.id));
let content = serde_json::to_string_pretty(self)?;
std::fs::write(path, content)?;
self.saved = true;
Ok(())
}
pub fn ui(&mut self, ctx: &egui::Context) {
// Show the editor window if enabled
let mut show = self.show_editor;
if show {
egui::Window::new("Markdown Editor")
.resizable(true)
.default_width(1000.0)
.default_height(800.0)
.open(&mut show)
.show(ctx, |ui| {
ui.vertical(|ui| {
ui.group(|ui| {
ui.horizontal(|ui| {
if self.saved {
ui.label(RichText::new("✓ Saved").color(egui::Color32::GREEN));
} else {
ui.label(
RichText::new("* Unsaved").color(egui::Color32::YELLOW),
);
}
ui.label(format!("id: {}", self.id));
});
});
// Save/Cancel buttons
ui.horizontal(|ui| {
if ui.button("Save").clicked() {
if let Err(e) = self.save() {
eprintln!("Failed to save: {e}");
}
}
if ui.button("Create Copy").clicked() {
let mut copy = self.clone();
copy.id = uuid::Uuid::new_v4().to_string();
copy.name = format!("{} (Copy)", self.name);
copy.save().unwrap();
}
if ui.button("Delete").clicked() {
std::fs::remove_file(
PROJECT_FOLDER
.join("documents")
.join(format!("{}.json", self.id)),
)
.unwrap();
*self = Self::new();
}
if ui.button("Revert changes").clicked() {
// load default state
*self = Self::load(&self.id).unwrap();
}
});
});
ui.horizontal(|ui| {
ui.label("Content Editor");
ui.checkbox(&mut self.show_preview, "Preview");
});
ui.separator();
if self.show_preview {
// Preview area
egui::SidePanel::right("preview_panel")
.resizable(true)
.default_width(ui.available_width() / 2.0)
.show_inside(ui, |ui| {
// Preview area with centered content and max width
egui::ScrollArea::both()
.auto_shrink([false, false])
.id_salt("preview_scroll")
.show(ui, |ui| {
let max_width = 600;
let available_width = ui.available_width();
let content_width = (max_width as f32).min(available_width);
let padding = (available_width - content_width) / 2.0;
ui.horizontal(|ui| {
ui.add_space(padding);
ui.vertical(|ui| {
ui.set_width(content_width);
ui.add_space(15.0);
ui.set_min_width(max_width as f32);
CommonMarkViewer::new()
.default_width(Some(max_width))
.max_image_width(Some(512))
.show(ui, &mut self.preview_cache, &self.text);
});
});
});
});
}
// Editor area with centered content and max width
egui::ScrollArea::both()
.auto_shrink([false, false])
.id_salt("editor_scroll")
.show(ui, |ui| {
let max_width = 600;
let available_width = ui.available_width();
let content_width = (max_width as f32).min(available_width);
let padding = (available_width - content_width) / 2.0;
ui.horizontal(|ui| {
ui.add_space(padding);
ui.vertical(|ui| {
ui.set_width(content_width);
ui.add_space(15.0);
ui.set_min_width(max_width as f32);
let text_edit = TextEdit::multiline(&mut self.text)
.id_source("MainEditor_editor")
.font(egui::TextStyle::Monospace)
.interactive(true)
.frame(false)
.lock_focus(true)
.hint_text("Type here...")
.desired_width(max_width as f32);
ui.add_sized(
egui::vec2(max_width as f32, ui.available_height()),
text_edit,
);
});
});
});
});
}
self.show_editor = show;
}
}
-109
View File
@@ -1,109 +0,0 @@
use std::fs;
use egui::RichText;
use serde::{Deserialize, Serialize};
use crate::PROJECT_FOLDER;
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Note {
pub name: String,
pub content: String,
#[serde(skip)]
pub id: String,
#[serde(skip)]
pub saved: bool,
}
impl Default for Note {
fn default() -> Self {
Self {
id: uuid::Uuid::new_v4().to_string(),
name: "New Note".to_string(),
content: "".to_string(),
saved: false,
}
}
}
impl Note {
pub fn new(name: String, content: String) -> Self {
Self {
id: uuid::Uuid::new_v4().to_string(),
name,
content,
saved: false,
}
}
pub fn save(&mut self) -> std::io::Result<()> {
let id = &self.id;
let path = PROJECT_FOLDER.join("notes").join(format!("{id}.json"));
fs::write(path, serde_json::to_string(&self)?)?;
self.saved = true;
Ok(())
}
pub fn load(id: &str) -> std::io::Result<Self> {
let path = PROJECT_FOLDER.join("notes").join(format!("{id}.json"));
let content = fs::read_to_string(path)?;
let mut note: Note = serde_json::from_str(&content)?;
note.id = id.to_string();
note.saved = true;
Ok(note)
}
pub fn ui(&mut self, ui: &mut egui::Ui) {
if ui.input(|i| i.key_pressed(egui::Key::S) && i.modifiers.ctrl) {
if let Err(e) = self.save() {
eprintln!("Failed to save: {e}");
}
}
ui.group(|ui| {
ui.horizontal(|ui| {
if self.saved {
ui.label(RichText::new("✓ Saved").color(egui::Color32::GREEN));
} else {
ui.label(RichText::new("* Unsaved").color(egui::Color32::YELLOW));
}
ui.label(format!("id: {}", self.id));
});
});
if ui.button("Save").clicked() {
if let Err(e) = self.save() {
eprintln!("Failed to save: {e}");
}
}
egui::Grid::new("note_grid")
.striped(true)
.num_columns(2)
.show(ui, |ui| {
ui.label("Name:");
if ui
.add(egui::TextEdit::singleline(&mut self.name).frame(false))
.changed()
{
self.saved = false;
}
ui.end_row();
ui.label("Content:");
if ui
.add(
egui::TextEdit::multiline(&mut self.content)
.desired_rows(1)
.frame(false),
)
.changed()
{
self.saved = false;
}
ui.end_row();
});
}
}
-276
View File
@@ -1,276 +0,0 @@
use core::f32;
use chrono::NaiveDate;
use egui::{CollapsingHeader, RichText, TextEdit, Ui, vec2};
use serde::{Deserialize, Serialize};
use crate::{
PROJECT_FOLDER, RightPanelContent,
template::{FieldType, FieldValue, Template},
};
#[derive(Debug, Serialize, Deserialize)]
pub struct ObjectInstance {
// template info
pub id: String,
pub template_id: String,
// instance info
pub name: String,
pub fields: std::collections::HashMap<String, FieldValue>,
#[serde(skip)]
pub saved: bool,
#[serde(skip)]
pub dialog: Option<egui_file::FileDialog>,
}
impl Clone for ObjectInstance {
fn clone(&self) -> Self {
Self {
id: self.id.clone(),
template_id: self.template_id.clone(),
name: self.name.clone(),
fields: self.fields.clone(),
saved: self.saved,
dialog: None,
}
}
}
impl Default for ObjectInstance {
fn default() -> Self {
Self {
id: uuid::Uuid::new_v4().to_string(),
template_id: "new_template_instance".to_string(),
name: "new_object".to_string(),
fields: std::collections::HashMap::new(),
saved: false,
dialog: None,
}
}
}
impl ObjectInstance {
pub fn new(template: &Template) -> Self {
let mut fields = std::collections::HashMap::new();
for field in &template.fields {
fields.insert(field.name.clone(), FieldValue::default());
}
Self {
id: uuid::Uuid::new_v4().to_string(),
template_id: template.id.clone(),
name: "new_object".to_string(),
fields,
saved: false,
dialog: None,
}
}
pub fn save(&mut self) -> Result<(), Box<dyn std::error::Error>> {
let path = PROJECT_FOLDER
.join("objects")
.join(format!("{}.json", &self.id));
let content = serde_json::to_string_pretty(self)?;
std::fs::write(&path, content)?;
self.saved = true;
Ok(())
}
pub fn load(id: &str) -> Result<Self, Box<dyn std::error::Error>> {
let path = PROJECT_FOLDER.join("objects").join(format!("{id}.json"));
let content = std::fs::read_to_string(&path)?;
let mut instance: ObjectInstance = serde_json::from_str(&content)?;
instance.saved = true;
Ok(instance)
}
pub fn ui(
&mut self,
ui: &mut Ui,
template: &Template,
right_panel: &mut Option<RightPanelContent>,
) {
let _ = right_panel;
if ui.input(|i| i.key_pressed(egui::Key::S) && i.modifiers.ctrl) {
if let Err(e) = self.save() {
eprintln!("Failed to save: {e}");
}
}
ui.vertical(|ui| {
// Show save status and button
ui.group(|ui| {
ui.horizontal(|ui| {
if self.saved {
ui.label(RichText::new("✓ Saved").color(egui::Color32::GREEN));
} else {
ui.label(RichText::new("* Unsaved").color(egui::Color32::YELLOW));
}
ui.label(format!("id: {}", self.id));
});
});
ui.horizontal(|ui| {
if ui.button("Save").clicked() {
if let Err(e) = self.save() {
eprintln!("Failed to save: {e}");
}
}
if ui.button("Create Copy").clicked() {
let mut copy = self.clone();
copy.id = uuid::Uuid::new_v4().to_string();
copy.dialog = None;
copy.name = format!("{} (Copy)", self.name);
copy.save().unwrap();
*right_panel = Some(RightPanelContent::Object {
object: Box::new(copy),
});
}
if ui.button("Delete").clicked() {
std::fs::remove_file(
PROJECT_FOLDER
.join("objects")
.join(format!("{}.json", self.id)),
)
.unwrap();
*right_panel = Some(RightPanelContent::None);
}
});
ui.separator();
egui::ScrollArea::vertical().show(ui, |ui| {
// Render each field
// allow name to be edited
CollapsingHeader::new("Name")
.default_open(true)
.show(ui, |ui| {
ui.heading(RichText::new("Name").size(14.0).strong());
ui.separator();
let _ = TextEdit::singleline(&mut self.name)
.desired_width(f32::INFINITY)
.frame(false)
.show(ui)
.response;
ui.separator();
});
for field_def in &template.fields {
if let Some(field_value) = self.fields.get_mut(&field_def.name) {
CollapsingHeader::new(&field_def.name)
.default_open(true)
.show(ui, |ui| {
ui.heading(RichText::new(&field_def.name).size(14.0).strong());
if let Some(desc) = &field_def.description {
ui.label(RichText::new(desc).italics().weak());
}
ui.separator();
let response = match field_def.field_type {
FieldType::SingleLine => {
TextEdit::singleline(&mut field_value.value)
.desired_width(f32::INFINITY)
.frame(false)
.show(ui)
.response
}
FieldType::MultiLine => {
TextEdit::multiline(&mut field_value.value)
.desired_width(f32::INFINITY)
.desired_rows(5)
.frame(false)
.show(ui)
.response
}
FieldType::Date => {
let date_str = &field_value.value;
let mut date =
NaiveDate::parse_from_str(date_str, "%Y-%m-%d")
.unwrap_or_else(|_| {
chrono::Local::now().date_naive()
});
let response =
ui.add(egui_extras::DatePickerButton::new(&mut date));
if response.changed() {
field_value.value = date.format("%Y-%m-%d").to_string();
field_value.modified = true;
self.saved = false;
}
response
}
FieldType::Number => {
let mut num =
field_value.value.parse::<f64>().unwrap_or(0.0);
let response =
ui.add(egui::DragValue::new(&mut num).speed(0.1));
if response.changed() {
field_value.value = num.to_string();
field_value.modified = true;
self.saved = false;
}
response
}
FieldType::Image => {
// Simple path input for now
let response = TextEdit::singleline(&mut field_value.value)
.hint_text("Path to image")
.desired_width(f32::INFINITY)
.frame(false)
.show(ui)
.response;
// If we have a valid path, try to display a preview
if !field_value.value.is_empty() {
if let Ok(bytes) = std::fs::read(&field_value.value) {
let image_source = egui::ImageSource::Bytes {
uri: std::borrow::Cow::Owned(
field_value.value.clone(),
),
bytes: bytes.into(),
};
ui.add(
egui::Image::new(image_source)
.max_size(vec2(256.0, f32::INFINITY)),
);
}
}
response
}
};
if response.changed() {
field_value.modified = true;
self.saved = false;
}
ui.separator();
});
}
}
});
});
}
}
-27
View File
@@ -1,27 +0,0 @@
pub struct EditorScene {
rect: egui::Rect,
}
impl EditorScene {
pub fn new() -> Self {
Self {
rect: egui::Rect::ZERO,
}
}
pub fn ui(&mut self, ctx: &egui::Context) {
egui::CentralPanel::default()
.frame(egui::Frame::NONE)
.show(ctx, |ui| {
egui::Scene::default()
.zoom_range(0.1..=10.0)
.show(ui, &mut self.rect, |ui| {
egui::Resize::default().auto_sized().show(ui, |ui| {
ui.group(|ui| {
ui.label("Scene");
});
});
});
});
}
}
+62
View File
@@ -0,0 +1,62 @@
use egui::{
RichText,
scroll_area::{ScrollBarVisibility, ScrollSource},
};
pub struct Error {
message: String,
visible: bool,
}
impl Error {
pub fn new(message: String) -> Self {
Self {
message,
visible: true,
}
}
pub fn show(&mut self, ui: &mut egui::Ui) {
egui::Window::new("Error")
.open(&mut self.visible)
.fixed_size([200.0, 100.0])
.show(ui.ctx(), |ui| {
ui.label(self.message.clone());
});
}
}
pub fn saved_status(ui: &mut egui::Ui, saved: bool, id: &str, name: &str) {
ui.group(|ui| {
ui.set_max_width(ui.available_width());
// Create a container that will take up the full width
ui.horizontal(|ui| {
// Add a ScrollArea that will contain our content
egui::ScrollArea::horizontal()
.scroll_source(ScrollSource::ALL)
.scroll_bar_visibility(ScrollBarVisibility::AlwaysHidden)
.auto_shrink([false, false]) // Don't shrink in either direction
.show(ui, |ui| {
// Create a horizontal layout that will contain our content
ui.horizontal(|ui| {
// Now add your content
ui.strong(name);
if saved {
ui.label(RichText::new("✓ Saved").color(egui::Color32::GREEN));
} else {
ui.label(RichText::new("* Unsaved").color(egui::Color32::YELLOW));
}
if ui
.add(egui::Button::new(format!("[id: {id}]")).frame(false))
.on_hover_text(" Click id to copy!")
.clicked()
{
ui.ctx().copy_text(id.to_string());
}
});
});
});
});
}