Compare commits
33 Commits
markdown-p
...
efbea10532
| Author | SHA1 | Date | |
|---|---|---|---|
| efbea10532 | |||
| 3669f54936 | |||
| 2fb9908329 | |||
| 0acab0413c | |||
| 8f16741705 | |||
| a663de3ca0 | |||
| 61575fbf65 | |||
|
579aace306
|
|||
| fe0b9d049e | |||
| c59febd924 | |||
|
276508713f
|
|||
| 3a2f058456 | |||
|
38d26f0028
|
|||
|
462c27e111
|
|||
|
e0fd726f02
|
|||
| 6e59cb86dc | |||
| 98a4f50031 | |||
|
0a19462b0f
|
|||
|
cfed4fd5ed
|
|||
|
eaf0c3cb55
|
|||
|
61669e15bd
|
|||
|
f2556f7125
|
|||
| 7494dc6b75 | |||
| 43afb9dfd3 | |||
| 6b5bbfbc54 | |||
|
b39419888b
|
|||
|
4e9eacc7b0
|
|||
|
8251937be9
|
|||
|
5ca9dfabb8
|
|||
|
1df81509df
|
|||
|
7d234641cb
|
|||
| 1ed278cc55 | |||
|
2a830f0539
|
1972
Cargo.lock
generated
1972
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
21
Cargo.toml
21
Cargo.toml
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "inkr"
|
name = "inkr"
|
||||||
version = "0.1.0"
|
version = "1.0.0"
|
||||||
authors = []
|
authors = []
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
@ -12,21 +12,25 @@ targets = ["x86_64-unknown-linux-gnu", "wasm32-unknown-unknown"]
|
|||||||
pinenote = []
|
pinenote = []
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
egui = "0.31"
|
egui = "0.32"
|
||||||
eframe = { version = "0.31", default-features = false, features = [
|
egui_extras = { version = "0.32", features = ["svg"] }
|
||||||
|
egui_glow = "0.32"
|
||||||
|
eframe = { version = "0.32", default-features = false, features = [
|
||||||
"glow", # alt: "wgpu".
|
"glow", # alt: "wgpu".
|
||||||
"persistence",
|
"persistence",
|
||||||
"wayland",
|
"wayland",
|
||||||
] }
|
] }
|
||||||
log = "0.4.27"
|
log = "0.4.27"
|
||||||
serde = { version = "1.0.219", features = ["derive"] }
|
serde = { version = "1.0.219", features = ["derive"] }
|
||||||
egui_glow = "0.31.1"
|
|
||||||
rfd = { version = "0.15.3", default-features = false, features = ["gtk3"] }
|
rfd = { version = "0.15.3", default-features = false, features = ["gtk3"] }
|
||||||
rand = "0.9.1"
|
rand = "0.9.1"
|
||||||
eyre = "0.6.12"
|
eyre = "0.6.12"
|
||||||
half = "2.6.0"
|
half = "2.6.0"
|
||||||
zerocopy = { version = "0.8.25", features = ["derive", "std"] }
|
zerocopy = { version = "0.8.25", features = ["derive", "std"] }
|
||||||
base64 = "0.22.1"
|
base64 = "0.22.1"
|
||||||
|
chrono = { version = "0.4.41", features = ["serde"] }
|
||||||
|
notify = "8.1.0"
|
||||||
|
arboard = "3.6.0"
|
||||||
|
|
||||||
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
|
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
|
||||||
env_logger = "0.11.8"
|
env_logger = "0.11.8"
|
||||||
@ -38,13 +42,14 @@ wasm-bindgen-futures = "0.4.50"
|
|||||||
web-sys = "0.3.77"
|
web-sys = "0.3.77"
|
||||||
|
|
||||||
[patch.crates-io]
|
[patch.crates-io]
|
||||||
egui = { git = "https://github.com/emilk/egui", rev = "f2ce6424f3a32f47308fb9871d540c01377b2cd9" }
|
# egui = { path = "../egui/crates/egui" }
|
||||||
eframe = { git = "https://github.com/emilk/egui", rev = "f2ce6424f3a32f47308fb9871d540c01377b2cd9" }
|
# eframe = { path = "../egui/crates/eframe" }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
insta = { version = "1.43.1", features = ["yaml"] }
|
insta = { version = "1.43.1", features = ["yaml"] }
|
||||||
# egui = { path = "../egui/crates/egui" }
|
|
||||||
# eframe = { path = "../egui/crates/eframe" }
|
[lints.clippy]
|
||||||
|
unnecessary_lazy_evaluations = "allow"
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
opt-level = 2 # fast and small wasm
|
opt-level = 2 # fast and small wasm
|
||||||
|
|||||||
27
PKGBUILD
Normal file
27
PKGBUILD
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
pkgname=inkr
|
||||||
|
pkgver=1.0.0
|
||||||
|
pkgrel=1
|
||||||
|
pkgdesc="A note-taking and handwriting tool"
|
||||||
|
arch=('x86_64' 'aarch64')
|
||||||
|
url="https://git.nubo.sh/hulthe/inkr"
|
||||||
|
#license=('GPL')
|
||||||
|
groups=('base-devel')
|
||||||
|
depends=('glibc')
|
||||||
|
makedepends=('cargo')
|
||||||
|
#optdepends=('ed: for "patch -e" functionality')
|
||||||
|
#source=(" ftp://ftp.gnu.org/gnu/$pkgname/$pkgname-$pkgver.tar.xz"{,.sig})
|
||||||
|
#sha256sums=('SKIP')
|
||||||
|
prepare() {
|
||||||
|
export RUSTUP_TOOLCHAIN=stable
|
||||||
|
cargo fetch --locked --target "$(rustc -vV | sed -n 's/host: //p')"
|
||||||
|
}
|
||||||
|
build() {
|
||||||
|
export RUSTUP_TOOLCHAIN=stable
|
||||||
|
cargo build --frozen --release
|
||||||
|
}
|
||||||
|
package() {
|
||||||
|
cd ..
|
||||||
|
install -Dm0755 -t "$pkgdir/usr/bin/" "${CARGO_TARGET_DIR:-target}/release/$pkgname"
|
||||||
|
install -Dm0755 -t "$pkgdir/usr/share/applications/" "assets/$pkgname.desktop"
|
||||||
|
install -Dm0755 "assets/icon.svg" "$pkgdir/usr/share/pixmaps/$pkgname.svg"
|
||||||
|
}
|
||||||
73
assets/collapse-icon.svg
Normal file
73
assets/collapse-icon.svg
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||||
|
<!-- Created with Inkscape (http://www.inkscape.org/) -->
|
||||||
|
|
||||||
|
<svg
|
||||||
|
width="256"
|
||||||
|
height="256"
|
||||||
|
viewBox="0 0 67.733332 67.733332"
|
||||||
|
version="1.1"
|
||||||
|
id="svg1"
|
||||||
|
inkscape:version="1.4.2 (ebf0e940, 2025-05-08)"
|
||||||
|
sodipodi:docname="collapse-icon.svg"
|
||||||
|
inkscape:export-filename="collapse-icon.png"
|
||||||
|
inkscape:export-xdpi="96"
|
||||||
|
inkscape:export-ydpi="96"
|
||||||
|
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
|
||||||
|
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
|
||||||
|
xmlns="http://www.w3.org/2000/svg"
|
||||||
|
xmlns:svg="http://www.w3.org/2000/svg">
|
||||||
|
<sodipodi:namedview
|
||||||
|
id="namedview1"
|
||||||
|
pagecolor="#505050"
|
||||||
|
bordercolor="#eeeeee"
|
||||||
|
borderopacity="1"
|
||||||
|
inkscape:showpageshadow="0"
|
||||||
|
inkscape:pageopacity="0"
|
||||||
|
inkscape:pagecheckerboard="0"
|
||||||
|
inkscape:deskcolor="#505050"
|
||||||
|
inkscape:document-units="mm"
|
||||||
|
inkscape:zoom="2.0010679"
|
||||||
|
inkscape:cx="97.198102"
|
||||||
|
inkscape:cy="140.42502"
|
||||||
|
inkscape:window-width="1472"
|
||||||
|
inkscape:window-height="815"
|
||||||
|
inkscape:window-x="0"
|
||||||
|
inkscape:window-y="38"
|
||||||
|
inkscape:window-maximized="1"
|
||||||
|
inkscape:current-layer="layer1" />
|
||||||
|
<defs
|
||||||
|
id="defs1" />
|
||||||
|
<g
|
||||||
|
inkscape:label="Lager 1"
|
||||||
|
inkscape:groupmode="layer"
|
||||||
|
id="layer1">
|
||||||
|
<rect
|
||||||
|
style="fill:#000000;fill-opacity:0;stroke:#ffffff;stroke-width:4.92907;stroke-linejoin:miter;stroke-dasharray:none;stroke-opacity:1"
|
||||||
|
id="rect1"
|
||||||
|
width="62.804268"
|
||||||
|
height="52.220932"
|
||||||
|
x="2.4645352"
|
||||||
|
y="7.7562032"
|
||||||
|
ry="6.9627905" />
|
||||||
|
<path
|
||||||
|
style="fill:#000000;fill-opacity:0;stroke:#ffffff;stroke-width:4.92628;stroke-linejoin:miter;stroke-dasharray:none;stroke-opacity:1"
|
||||||
|
d="M 28.921473,6.35 V 61.383333"
|
||||||
|
id="path1" />
|
||||||
|
<path
|
||||||
|
style="fill:#000000;fill-opacity:0;stroke:#ffffff;stroke-width:3.12398;stroke-linecap:round;stroke-linejoin:miter;stroke-dasharray:none;stroke-opacity:1"
|
||||||
|
d="M 22.250504,15.411171 9.4994923,15.320325"
|
||||||
|
id="path1-7" />
|
||||||
|
<path
|
||||||
|
style="fill:#000000;fill-opacity:0;stroke:#ffffff;stroke-width:3.12398;stroke-linecap:round;stroke-linejoin:miter;stroke-dasharray:none;stroke-opacity:1"
|
||||||
|
d="M 22.250506,21.743139 9.4994938,21.652293"
|
||||||
|
id="path1-7-4" />
|
||||||
|
<path
|
||||||
|
style="fill:#000000;fill-opacity:0;stroke:#ffffff;stroke-width:3.12398;stroke-linecap:round;stroke-linejoin:miter;stroke-dasharray:none;stroke-opacity:1"
|
||||||
|
d="M 22.250506,28.075107 9.4994938,27.984261"
|
||||||
|
id="path1-7-5" />
|
||||||
|
<path
|
||||||
|
style="fill:#000000;fill-opacity:0;stroke:#ffffff;stroke-width:3.12398;stroke-linecap:round;stroke-linejoin:miter;stroke-dasharray:none;stroke-opacity:1"
|
||||||
|
d="M 22.250508,34.407075 9.4994958,34.316229"
|
||||||
|
id="path1-7-4-4" />
|
||||||
|
</g>
|
||||||
|
</svg>
|
||||||
|
After Width: | Height: | Size: 2.8 KiB |
9
assets/inkr.desktop
Executable file
9
assets/inkr.desktop
Executable file
@ -0,0 +1,9 @@
|
|||||||
|
[Desktop Entry]
|
||||||
|
Name=inkr
|
||||||
|
Exec=inkr
|
||||||
|
Terminal=false
|
||||||
|
Type=Application
|
||||||
|
Icon=inkr
|
||||||
|
StartupWMClass=inkr
|
||||||
|
MimeType=x-scheme-handler/inkr;
|
||||||
|
Categories=Office;
|
||||||
302
src/app.rs
302
src/app.rs
@ -1,13 +1,24 @@
|
|||||||
use std::{
|
use std::{
|
||||||
fs,
|
fs,
|
||||||
|
io::Read,
|
||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
sync::{Arc, mpsc},
|
sync::{Arc, mpsc},
|
||||||
|
thread::JoinHandle,
|
||||||
|
time::{Duration, Instant},
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{file_editor::FileEditor, preferences::Preferences, util::GuiSender};
|
use crate::{
|
||||||
use egui::{
|
file_editor::{FileEditor, SaveStatus},
|
||||||
Align, Button, Color32, FontData, FontDefinitions, PointerButton, RichText, ScrollArea, Stroke,
|
folder::Folder,
|
||||||
|
preferences::Preferences,
|
||||||
|
text_styles::{H1, H1_MONO, H2, H2_MONO, H3, H3_MONO, H4, H4_MONO, H5, H5_MONO, H6, H6_MONO},
|
||||||
|
util::{GuiSender, file_mtime, log_error},
|
||||||
};
|
};
|
||||||
|
use egui::{
|
||||||
|
Align, Button, Context, FontData, FontDefinitions, FontFamily, FontId, Image, Key, Modifiers,
|
||||||
|
PointerButton, RichText, ScrollArea, Theme, Widget, include_image,
|
||||||
|
};
|
||||||
|
use eyre::eyre;
|
||||||
|
|
||||||
#[derive(serde::Deserialize, serde::Serialize)]
|
#[derive(serde::Deserialize, serde::Serialize)]
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
@ -18,13 +29,46 @@ pub struct App {
|
|||||||
actions_tx: mpsc::Sender<Action>,
|
actions_tx: mpsc::Sender<Action>,
|
||||||
#[serde(skip)]
|
#[serde(skip)]
|
||||||
actions_rx: mpsc::Receiver<Action>,
|
actions_rx: mpsc::Receiver<Action>,
|
||||||
|
#[serde(skip)]
|
||||||
|
jobs: Jobs,
|
||||||
|
|
||||||
tabs: Vec<(TabId, Tab)>,
|
tabs: Vec<(TabId, Tab)>,
|
||||||
|
|
||||||
|
show_folders: bool,
|
||||||
|
folders: Vec<Folder>,
|
||||||
|
|
||||||
open_tab_index: Option<usize>,
|
open_tab_index: Option<usize>,
|
||||||
|
|
||||||
next_tab_id: TabId,
|
next_tab_id: TabId,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct Jobs {
|
||||||
|
handles: Vec<JoinHandle<()>>,
|
||||||
|
actions_tx: mpsc::Sender<Action>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Jobs {
|
||||||
|
pub fn start(&mut self, ctx: &Context, job: impl FnOnce() -> Option<Action> + Send + 'static) {
|
||||||
|
let ctx = ctx.clone();
|
||||||
|
let actions_tx = self.actions_tx.clone();
|
||||||
|
self.handles.push(std::thread::spawn(move || {
|
||||||
|
// start rendering the spinner thingy
|
||||||
|
ctx.request_repaint();
|
||||||
|
|
||||||
|
let start = Instant::now();
|
||||||
|
|
||||||
|
if let Some(action) = job() {
|
||||||
|
let _ = actions_tx.send(action);
|
||||||
|
ctx.request_repaint();
|
||||||
|
};
|
||||||
|
|
||||||
|
// Make sure that task takes at least 250ms to run, so that the spinner won't blink
|
||||||
|
let sleep_for = Duration::from_millis(250).saturating_sub(start.elapsed());
|
||||||
|
std::thread::sleep(sleep_for);
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(serde::Deserialize, serde::Serialize)]
|
#[derive(serde::Deserialize, serde::Serialize)]
|
||||||
enum Tab {
|
enum Tab {
|
||||||
File(FileEditor),
|
File(FileEditor),
|
||||||
@ -36,12 +80,31 @@ impl Tab {
|
|||||||
Tab::File(file_editor) => file_editor.title(),
|
Tab::File(file_editor) => file_editor.title(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn notice_symbol(&self) -> Option<&'static str> {
|
||||||
|
match self {
|
||||||
|
Tab::File(file_editor) => match file_editor.save_status() {
|
||||||
|
SaveStatus::Synced => None,
|
||||||
|
SaveStatus::NoFile => Some("?"),
|
||||||
|
SaveStatus::FileOutdated => Some("*"),
|
||||||
|
SaveStatus::BufferOutdated => Some("!"),
|
||||||
|
SaveStatus::Desynced => Some("!!"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn save(&mut self, ctx: &Context, jobs: &mut Jobs) {
|
||||||
|
match self {
|
||||||
|
Tab::File(file_editor) => file_editor.save(ctx, jobs),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type TabId = usize;
|
pub type TabId = usize;
|
||||||
|
|
||||||
pub enum Action {
|
pub enum Action {
|
||||||
OpenFile(FileEditor),
|
OpenFile(FileEditor),
|
||||||
|
OpenFolder(Folder),
|
||||||
MoveFile(TabId, PathBuf),
|
MoveFile(TabId, PathBuf),
|
||||||
CloseTab(TabId),
|
CloseTab(TabId),
|
||||||
// TODO
|
// TODO
|
||||||
@ -55,11 +118,17 @@ impl Default for App {
|
|||||||
let (actions_tx, actions_rx) = mpsc::channel();
|
let (actions_tx, actions_rx) = mpsc::channel();
|
||||||
Self {
|
Self {
|
||||||
preferences: Preferences::default(),
|
preferences: Preferences::default(),
|
||||||
actions_tx,
|
actions_tx: actions_tx.clone(/* this is silly, i know */),
|
||||||
actions_rx,
|
actions_rx,
|
||||||
|
jobs: Jobs {
|
||||||
|
handles: Default::default(),
|
||||||
|
actions_tx,
|
||||||
|
},
|
||||||
tabs: vec![(1, Tab::File(FileEditor::new("note.md")))],
|
tabs: vec![(1, Tab::File(FileEditor::new("note.md")))],
|
||||||
open_tab_index: None,
|
open_tab_index: None,
|
||||||
next_tab_id: 2,
|
next_tab_id: 2,
|
||||||
|
show_folders: false,
|
||||||
|
folders: vec![],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -122,22 +191,41 @@ impl App {
|
|||||||
.map(|(name, data)| (name.to_string(), Arc::new(FontData::from_static(data))))
|
.map(|(name, data)| (name.to_string(), Arc::new(FontData::from_static(data))))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
fonts.families.insert(
|
fonts
|
||||||
egui::FontFamily::Proportional,
|
.families
|
||||||
vec!["IosevkaAile-Regular".into()],
|
.insert(FontFamily::Proportional, vec!["IosevkaAile-Regular".into()]);
|
||||||
);
|
|
||||||
|
|
||||||
fonts
|
fonts
|
||||||
.families
|
.families
|
||||||
.insert(egui::FontFamily::Monospace, vec!["Iosevka-Thin".into()]);
|
.insert(FontFamily::Monospace, vec!["Iosevka-Thin".into()]);
|
||||||
|
|
||||||
cc.egui_ctx.set_fonts(fonts);
|
cc.egui_ctx.set_fonts(fonts);
|
||||||
|
|
||||||
cc.egui_ctx.style_mut(|style| {
|
// markdown font styles
|
||||||
// TODO: change color of text in TextEdit
|
for theme in [Theme::Dark, Theme::Light] {
|
||||||
style.visuals.widgets.noninteractive.fg_stroke =
|
cc.egui_ctx.style_mut_of(theme, |style| {
|
||||||
Stroke::new(1.0, Color32::from_rgb(200, 200, 200));
|
for (name, size, family) in [
|
||||||
|
(H1, 28.0, FontFamily::Proportional),
|
||||||
|
(H2, 26.0, FontFamily::Proportional),
|
||||||
|
(H3, 24.0, FontFamily::Proportional),
|
||||||
|
(H4, 22.0, FontFamily::Proportional),
|
||||||
|
(H5, 20.0, FontFamily::Proportional),
|
||||||
|
(H6, 18.0, FontFamily::Proportional),
|
||||||
|
(H1_MONO, 28.0, FontFamily::Monospace),
|
||||||
|
(H2_MONO, 26.0, FontFamily::Monospace),
|
||||||
|
(H3_MONO, 24.0, FontFamily::Monospace),
|
||||||
|
(H4_MONO, 22.0, FontFamily::Monospace),
|
||||||
|
(H5_MONO, 20.0, FontFamily::Monospace),
|
||||||
|
(H6_MONO, 18.0, FontFamily::Monospace),
|
||||||
|
] {
|
||||||
|
let name = egui::TextStyle::Name(name.into());
|
||||||
|
style.text_styles.insert(name, FontId { size, family });
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// enable features on egui_extras to add more image types
|
||||||
|
egui_extras::install_image_loaders(&cc.egui_ctx);
|
||||||
|
|
||||||
if let Some(storage) = cc.storage {
|
if let Some(storage) = cc.storage {
|
||||||
return eframe::get_value(storage, eframe::APP_KEY).unwrap_or_default();
|
return eframe::get_value(storage, eframe::APP_KEY).unwrap_or_default();
|
||||||
@ -146,12 +234,24 @@ impl App {
|
|||||||
Default::default()
|
Default::default()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn actions_tx(&self, ctx: &egui::Context) -> GuiSender<Action> {
|
fn actions_tx(&self, ctx: &Context) -> GuiSender<Action> {
|
||||||
GuiSender::new(self.actions_tx.clone(), ctx)
|
GuiSender::new(self.actions_tx.clone(), ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_action(&mut self, action: Action) {
|
fn handle_action(&mut self, action: Action) {
|
||||||
match action {
|
match action {
|
||||||
|
Action::OpenFolder(new_folder) => {
|
||||||
|
if let Some(folder) = self
|
||||||
|
.folders
|
||||||
|
.iter_mut()
|
||||||
|
.find(|folder| folder.path() == new_folder.path())
|
||||||
|
{
|
||||||
|
*folder = new_folder;
|
||||||
|
} else {
|
||||||
|
self.folders.push(new_folder);
|
||||||
|
self.folders.sort_by(|a, b| a.name().cmp(b.name()));
|
||||||
|
}
|
||||||
|
}
|
||||||
Action::OpenFile(file_editor) => {
|
Action::OpenFile(file_editor) => {
|
||||||
self.open_tab(Tab::File(file_editor));
|
self.open_tab(Tab::File(file_editor));
|
||||||
}
|
}
|
||||||
@ -175,9 +275,11 @@ impl eframe::App for App {
|
|||||||
eframe::set_value(storage, eframe::APP_KEY, self);
|
eframe::set_value(storage, eframe::APP_KEY, self);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
fn update(&mut self, ctx: &Context, _frame: &mut eframe::Frame) {
|
||||||
self.preferences.apply(ctx);
|
self.preferences.apply(ctx);
|
||||||
|
|
||||||
|
self.jobs.handles.retain(|job| !job.is_finished());
|
||||||
|
|
||||||
while let Ok(action) = self.actions_rx.try_recv() {
|
while let Ok(action) = self.actions_rx.try_recv() {
|
||||||
self.handle_action(action);
|
self.handle_action(action);
|
||||||
}
|
}
|
||||||
@ -186,14 +288,14 @@ impl eframe::App for App {
|
|||||||
self.open_tab_index = Some(self.tabs.len().saturating_sub(1));
|
self.open_tab_index = Some(self.tabs.len().saturating_sub(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
//ctx.input_mut(|input| {
|
ctx.input_mut(|input| {
|
||||||
// if input.consume_key(Modifiers::CTRL, Key::H) {
|
if input.consume_key(Modifiers::CTRL, Key::S) {
|
||||||
// self.buffer.push(BufferItem::Painting(Default::default()));
|
self.save_active_tab(ctx);
|
||||||
// }
|
}
|
||||||
//});
|
});
|
||||||
|
|
||||||
egui::TopBottomPanel::top("top_panel").show(ctx, |ui| {
|
egui::TopBottomPanel::top("top_panel").show(ctx, |ui| {
|
||||||
egui::containers::menu::Bar::new().ui(ui, |ui| {
|
egui::MenuBar::new().ui(ui, |ui| {
|
||||||
// NOTE: no File->Quit on web pages!
|
// NOTE: no File->Quit on web pages!
|
||||||
ui.menu_button("Menu ⚙", |ui| {
|
ui.menu_button("Menu ⚙", |ui| {
|
||||||
ui.label(RichText::new("Action").weak());
|
ui.label(RichText::new("Action").weak());
|
||||||
@ -205,27 +307,32 @@ impl eframe::App for App {
|
|||||||
|
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
if ui.button("Open File").clicked() {
|
if ui.button("Open File").clicked() {
|
||||||
let actions_tx = self.actions_tx(ui.ctx());
|
self.jobs.start(ui.ctx(), move || {
|
||||||
std::thread::spawn(move || {
|
let file_path = rfd::FileDialog::new().pick_file()?;
|
||||||
let file = rfd::FileDialog::new().pick_file();
|
|
||||||
|
|
||||||
let Some(file_path) = file else { return };
|
let mut file = fs::File::open(&file_path)
|
||||||
|
.inspect_err(|e| log::error!("Failed to open {file_path:?}: {e}"))
|
||||||
|
.ok()?;
|
||||||
|
|
||||||
let text = match fs::read_to_string(&file_path) {
|
let mtime = log_error(eyre!("file_path:?"), || file_mtime(&file))?;
|
||||||
Ok(text) => text,
|
|
||||||
Err(e) => {
|
|
||||||
log::error!("Failed to read {file_path:?}: {e}");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let editor = FileEditor::from_file(file_path, &text);
|
let mut text = String::new();
|
||||||
let _ = actions_tx.send(Action::OpenFile(editor));
|
file.read_to_string(&mut text)
|
||||||
|
.inspect_err(|e| log::error!("Failed to read {file_path:?}: {e}"))
|
||||||
|
.ok()?;
|
||||||
|
|
||||||
|
let editor = FileEditor::from_file(file_path, &text, mtime);
|
||||||
|
Some(Action::OpenFile(editor))
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if ui.button("Open Folder").clicked() {
|
if ui.button("Open Folder").clicked() {
|
||||||
log::error!("Open Folder not implemented");
|
self.jobs.start(ui.ctx(), move || {
|
||||||
|
let path = rfd::FileDialog::new().pick_folder()?;
|
||||||
|
let name = path.file_name()?.to_string_lossy().to_string();
|
||||||
|
let folder = Folder::NotLoaded { name, path };
|
||||||
|
Some(Action::OpenFolder(folder))
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if ui
|
if ui
|
||||||
@ -237,52 +344,41 @@ impl eframe::App for App {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let open_file =
|
let can_save_file = self
|
||||||
self.open_tab_index
|
.open_tab_index
|
||||||
.and_then(|i| self.tabs.get(i))
|
.and_then(|i| self.tabs.get(i))
|
||||||
.and_then(|(id, tab)| match tab {
|
.map(|(id, tab)| match tab {
|
||||||
Tab::File(file_editor) => Some((*id, file_editor)),
|
Tab::File(file_editor) => (*id, file_editor),
|
||||||
});
|
})
|
||||||
|
.and_then(|(_, file_editor)| file_editor.path().zip(Some(file_editor)))
|
||||||
|
.is_some();
|
||||||
|
|
||||||
let open_file_with_path = open_file
|
if ui.add_enabled(can_save_file, Button::new("Save")).clicked() {
|
||||||
.clone()
|
self.save_active_tab(ui.ctx());
|
||||||
.and_then(|(_, file_editor)| file_editor.path().zip(Some(file_editor)));
|
}
|
||||||
|
|
||||||
if ui
|
let open_file = self.open_tab_index.and_then(|i| self.tabs.get(i)).map(
|
||||||
.add_enabled(open_file_with_path.is_some(), Button::new("Save"))
|
|(id, tab)| match tab {
|
||||||
.clicked()
|
Tab::File(file_editor) => (*id, file_editor),
|
||||||
{
|
},
|
||||||
if let Some((file_path, file_editor)) = open_file_with_path {
|
);
|
||||||
let text = file_editor.to_string();
|
|
||||||
let file_path = file_path.to_owned();
|
|
||||||
std::thread::spawn(move || {
|
|
||||||
if let Err(e) = fs::write(file_path, text.as_bytes()) {
|
|
||||||
log::error!("{e}");
|
|
||||||
};
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
if ui
|
if ui
|
||||||
.add_enabled(open_file.is_some(), Button::new("Save As"))
|
.add_enabled(open_file.is_some(), Button::new("Save As"))
|
||||||
.clicked()
|
.clicked()
|
||||||
{
|
{
|
||||||
let actions_tx = self.actions_tx(ui.ctx());
|
|
||||||
let (tab_id, editor) =
|
let (tab_id, editor) =
|
||||||
open_file.expect("We checked that open_file is_some");
|
open_file.expect("We checked that open_file is_some");
|
||||||
let text = editor.to_string();
|
let text = editor.to_string();
|
||||||
std::thread::spawn(move || {
|
self.jobs.start(ui.ctx(), move || {
|
||||||
let Some(file_path) = rfd::FileDialog::new().save_file() else {
|
let file_path = rfd::FileDialog::new().save_file()?;
|
||||||
return;
|
|
||||||
};
|
|
||||||
|
|
||||||
if let Err(e) = fs::write(&file_path, text.as_bytes()) {
|
fs::write(&file_path, text.as_bytes())
|
||||||
log::error!("{e}");
|
.inspect_err(|e| log::error!("{e}"))
|
||||||
return;
|
.ok()?;
|
||||||
};
|
|
||||||
|
|
||||||
let _ = actions_tx.send(Action::MoveFile(tab_id, file_path));
|
Some(Action::MoveFile(tab_id, file_path))
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -297,7 +393,18 @@ impl eframe::App for App {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
ui.add_space(16.0);
|
ui.add_space(8.0);
|
||||||
|
|
||||||
|
let image = Image::new(include_image!("../assets/collapse-icon.svg"));
|
||||||
|
let image = image.tint(ui.style().visuals.text_color());
|
||||||
|
if Button::image(image).ui(ui).clicked() {
|
||||||
|
self.show_folders = !self.show_folders;
|
||||||
|
}
|
||||||
|
|
||||||
|
if !self.jobs.handles.is_empty() {
|
||||||
|
ui.add_space(8.0);
|
||||||
|
ui.spinner();
|
||||||
|
}
|
||||||
|
|
||||||
ui.add_space(16.0);
|
ui.add_space(16.0);
|
||||||
|
|
||||||
@ -306,9 +413,8 @@ impl eframe::App for App {
|
|||||||
let selected = self.open_tab_index == Some(i);
|
let selected = self.open_tab_index == Some(i);
|
||||||
let mut button = Button::new(tab.title()).selected(selected);
|
let mut button = Button::new(tab.title()).selected(selected);
|
||||||
|
|
||||||
let dirty = i == 0; // TODO: mark as dirty when contents hasn't been saved
|
if let Some(symbol) = tab.notice_symbol() {
|
||||||
if dirty {
|
button = button.right_text(RichText::new(symbol).strong())
|
||||||
button = button.right_text(RichText::new("*").strong())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let response = ui.add(button);
|
let response = ui.add(button);
|
||||||
@ -323,6 +429,48 @@ impl eframe::App for App {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
egui::SidePanel::left("file browser")
|
||||||
|
.resizable(true)
|
||||||
|
.show_animated(ctx, self.show_folders, |ui| {
|
||||||
|
if ui.button("refresh").clicked() {
|
||||||
|
for folder in &mut self.folders {
|
||||||
|
folder.unload();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ScrollArea::both().auto_shrink(false).show(ui, |ui| {
|
||||||
|
self.folders.retain_mut(|folder| {
|
||||||
|
let response = folder.show(ui);
|
||||||
|
|
||||||
|
if let Some(file_path) = response.open_file {
|
||||||
|
let file_path = file_path.to_owned();
|
||||||
|
self.jobs.start(ui.ctx(), move || {
|
||||||
|
let mut file = fs::File::open(&file_path)
|
||||||
|
.inspect_err(|e| {
|
||||||
|
log::error!("Failed to open {file_path:?}: {e}")
|
||||||
|
})
|
||||||
|
.ok()?;
|
||||||
|
|
||||||
|
let mtime = log_error(eyre!("file_path:?"), || file_mtime(&file))?;
|
||||||
|
|
||||||
|
let mut text = String::new();
|
||||||
|
file.read_to_string(&mut text)
|
||||||
|
.inspect_err(|e| {
|
||||||
|
log::error!("Failed to read {file_path:?}: {e}")
|
||||||
|
})
|
||||||
|
.ok()?;
|
||||||
|
|
||||||
|
let editor = FileEditor::from_file(file_path, &text, mtime);
|
||||||
|
Some(Action::OpenFile(editor))
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// delete on right-click
|
||||||
|
!response.clicked_by(PointerButton::Secondary)
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
egui::CentralPanel::default().show(ctx, |ui| {
|
egui::CentralPanel::default().show(ctx, |ui| {
|
||||||
if let Some(Tab::File(file_editor)) = self
|
if let Some(Tab::File(file_editor)) = self
|
||||||
.open_tab_index
|
.open_tab_index
|
||||||
@ -354,5 +502,17 @@ impl App {
|
|||||||
let id = self.next_tab_id;
|
let id = self.next_tab_id;
|
||||||
self.next_tab_id += 1;
|
self.next_tab_id += 1;
|
||||||
self.tabs.insert(i, (id, tab));
|
self.tabs.insert(i, (id, tab));
|
||||||
|
self.open_tab_index = Some(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn save_active_tab(&mut self, ctx: &Context) {
|
||||||
|
let open_tab = self
|
||||||
|
.open_tab_index
|
||||||
|
.and_then(|i| self.tabs.get_mut(i))
|
||||||
|
.map(|(_id, tab)| tab);
|
||||||
|
|
||||||
|
if let Some(open_tab) = open_tab {
|
||||||
|
open_tab.save(ctx, &mut self.jobs);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,53 +1,116 @@
|
|||||||
use std::{
|
use std::{
|
||||||
cmp::Ordering,
|
cmp::Ordering,
|
||||||
fmt::{self, Display},
|
fmt::{self, Display},
|
||||||
|
fs::{self, File},
|
||||||
|
io::Write,
|
||||||
ops::{Div as _, Sub as _},
|
ops::{Div as _, Sub as _},
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
str::FromStr,
|
str::FromStr,
|
||||||
|
sync::mpsc,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use chrono::{DateTime, Local};
|
||||||
use egui::{
|
use egui::{
|
||||||
Align, Button, DragAndDrop, Frame, Layout, ScrollArea, Ui, UiBuilder, Vec2, Widget as _, vec2,
|
Align, Button, Context, DragAndDrop, Frame, Layout, ScrollArea, Ui, UiBuilder, Vec2, vec2,
|
||||||
};
|
};
|
||||||
|
use eyre::eyre;
|
||||||
|
use notify::{EventKind, Watcher};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
app::Jobs,
|
||||||
custom_code_block::{MdItem, iter_lines_and_code_blocks},
|
custom_code_block::{MdItem, iter_lines_and_code_blocks},
|
||||||
painting::{self, Handwriting, HandwritingStyle},
|
handwriting::{self, Handwriting, HandwritingStyle},
|
||||||
preferences::Preferences,
|
preferences::Preferences,
|
||||||
text_editor::MdTextEdit,
|
text_editor::MdTextEdit,
|
||||||
|
util::{file_mtime, log_error},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(serde::Deserialize, serde::Serialize)]
|
#[derive(serde::Deserialize, serde::Serialize)]
|
||||||
pub struct FileEditor {
|
pub struct FileEditor {
|
||||||
title: String,
|
title: String,
|
||||||
pub path: Option<PathBuf>,
|
|
||||||
|
path: Option<PathBuf>,
|
||||||
pub buffer: Vec<BufferItem>,
|
pub buffer: Vec<BufferItem>,
|
||||||
|
|
||||||
|
pub file_mtime: Option<DateTime<Local>>,
|
||||||
|
pub buffer_mtime: DateTime<Local>,
|
||||||
|
|
||||||
|
// TODO: instantiate these on load
|
||||||
|
#[serde(skip)]
|
||||||
|
inner: Option<Inner>,
|
||||||
|
|
||||||
|
/// The distance to scroll when paging up or down.
|
||||||
|
///
|
||||||
|
/// This is calculated when the view is rendered.
|
||||||
|
#[serde(skip)]
|
||||||
|
scroll_delta: f32,
|
||||||
|
|
||||||
|
/// Whether the file has been edited since it was last saved to disk.
|
||||||
|
is_dirty: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Inner {
|
||||||
|
file_events: mpsc::Receiver<FileEvent>,
|
||||||
|
file_events_tx: mpsc::Sender<FileEvent>,
|
||||||
|
_file_watcher: notify::RecommendedWatcher,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum SaveStatus {
|
||||||
|
/// The contents of the buffer is the same as on disk.
|
||||||
|
Synced,
|
||||||
|
|
||||||
|
/// The contents exits only in memory and has never been saved to disk.
|
||||||
|
NoFile,
|
||||||
|
|
||||||
|
/// The buffer has been edited but not saved to disk.
|
||||||
|
FileOutdated,
|
||||||
|
|
||||||
|
/// The contents on disk has changes that are newer than the buffer.
|
||||||
|
BufferOutdated,
|
||||||
|
|
||||||
|
/// The contents on disk and in the buffer has diverged.
|
||||||
|
Desynced,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
enum FileEvent {
|
||||||
|
NewFileMTime(DateTime<Local>),
|
||||||
|
NewBufferMTime(DateTime<Local>),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(serde::Deserialize, serde::Serialize)]
|
#[derive(serde::Deserialize, serde::Serialize)]
|
||||||
pub enum BufferItem {
|
pub enum BufferItem {
|
||||||
Text(MdTextEdit),
|
Text(Box<MdTextEdit>),
|
||||||
Handwriting(Handwriting),
|
Handwriting(Box<Handwriting>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FileEditor {
|
impl FileEditor {
|
||||||
pub fn new(title: impl Into<String>) -> Self {
|
pub fn new(title: impl Into<String>) -> Self {
|
||||||
let buffer = vec![BufferItem::Text(MdTextEdit::new())];
|
let buffer = vec![BufferItem::Text(Box::new(MdTextEdit::new()))];
|
||||||
Self {
|
Self {
|
||||||
title: title.into(),
|
title: title.into(),
|
||||||
path: None,
|
path: None,
|
||||||
buffer,
|
buffer,
|
||||||
|
file_mtime: None,
|
||||||
|
buffer_mtime: Local::now(),
|
||||||
|
scroll_delta: 0.0,
|
||||||
|
is_dirty: false,
|
||||||
|
inner: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_file(file_path: PathBuf, contents: &str) -> Self {
|
pub fn from_file(file_path: PathBuf, contents: &str, mtime: DateTime<Local>) -> Self {
|
||||||
let file_title = file_path
|
let file_title = file_path
|
||||||
.file_name()
|
.file_name()
|
||||||
.map(|name| name.to_string_lossy().to_string())
|
.map(|name| name.to_string_lossy().to_string())
|
||||||
.unwrap_or_else(|| String::from("untitled.md"));
|
.unwrap_or_else(|| String::from("untitled.md"));
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
title: file_title,
|
title: file_title,
|
||||||
path: Some(file_path),
|
path: Some(file_path),
|
||||||
|
file_mtime: Some(mtime),
|
||||||
|
buffer_mtime: mtime,
|
||||||
..FileEditor::from(contents)
|
..FileEditor::from(contents)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -60,24 +123,91 @@ impl FileEditor {
|
|||||||
self.path.as_deref()
|
self.path.as_deref()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn save_status(&self) -> SaveStatus {
|
||||||
|
let Some(file_mtime) = self.file_mtime else {
|
||||||
|
return SaveStatus::NoFile;
|
||||||
|
};
|
||||||
|
|
||||||
|
let buffer_is_newer = self.buffer_mtime > file_mtime;
|
||||||
|
let file_is_newer = self.buffer_mtime < file_mtime;
|
||||||
|
|
||||||
|
if buffer_is_newer || (file_is_newer && self.is_dirty) {
|
||||||
|
SaveStatus::Desynced
|
||||||
|
} else if file_is_newer {
|
||||||
|
SaveStatus::BufferOutdated
|
||||||
|
} else if self.is_dirty {
|
||||||
|
SaveStatus::FileOutdated
|
||||||
|
} else {
|
||||||
|
SaveStatus::Synced
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn show(&mut self, ui: &mut Ui, preferences: &Preferences) {
|
pub fn show(&mut self, ui: &mut Ui, preferences: &Preferences) {
|
||||||
|
if let Some(path) = &self.path
|
||||||
|
&& self.inner.is_none()
|
||||||
|
{
|
||||||
|
self.inner = Some(spawn_file_watcher(path));
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(inner) = &mut self.inner {
|
||||||
|
while let Ok(event) = inner.file_events.try_recv() {
|
||||||
|
match event {
|
||||||
|
FileEvent::NewFileMTime(mtime) => {
|
||||||
|
self.file_mtime = Some(mtime);
|
||||||
|
}
|
||||||
|
FileEvent::NewBufferMTime(mtime) => {
|
||||||
|
self.buffer_mtime = mtime;
|
||||||
|
self.file_mtime = Some(mtime);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ui.vertical_centered_justified(|ui| {
|
ui.vertical_centered_justified(|ui| {
|
||||||
ui.heading(&self.title);
|
ui.heading(&self.title);
|
||||||
|
|
||||||
const MAX_NOTE_WIDTH: f32 = 600.0;
|
const MAX_NOTE_WIDTH: f32 = 600.0;
|
||||||
|
|
||||||
ui.horizontal(|ui| {
|
// distance to scroll when paging up or down.
|
||||||
ui.label("new");
|
let mut scroll_delta = 0.0;
|
||||||
if ui.button("text").clicked() {
|
|
||||||
self.buffer.push(BufferItem::Text(Default::default()));
|
ui.input_mut(|input| {
|
||||||
|
if input.consume_key(egui::Modifiers::NONE, egui::Key::PageUp) {
|
||||||
|
scroll_delta += self.scroll_delta;
|
||||||
}
|
}
|
||||||
if ui.button("writing").clicked() {
|
if input.consume_key(egui::Modifiers::NONE, egui::Key::PageDown) {
|
||||||
self.buffer
|
scroll_delta -= self.scroll_delta;
|
||||||
.push(BufferItem::Handwriting(Default::default()));
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
ScrollArea::vertical().show(ui, |ui| {
|
ui.horizontal(|ui| {
|
||||||
|
ui.label("new");
|
||||||
|
if ui.button(" text ").clicked() {
|
||||||
|
self.is_dirty = true;
|
||||||
|
self.buffer.push(BufferItem::Text(Default::default()));
|
||||||
|
}
|
||||||
|
if ui.button("writing").clicked() {
|
||||||
|
self.is_dirty = true;
|
||||||
|
self.buffer
|
||||||
|
.push(BufferItem::Handwriting(Default::default()));
|
||||||
|
}
|
||||||
|
|
||||||
|
ui.add_space(16.0);
|
||||||
|
|
||||||
|
ui.label("scroll");
|
||||||
|
if ui.button(" up ").clicked() {
|
||||||
|
scroll_delta += self.scroll_delta;
|
||||||
|
}
|
||||||
|
if ui.button("down").clicked() {
|
||||||
|
scroll_delta -= self.scroll_delta;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let scroll_area = ScrollArea::vertical().show(ui, |ui| {
|
||||||
|
if scroll_delta != 0.0 {
|
||||||
|
ui.scroll_with_delta(Vec2::new(0.0, scroll_delta));
|
||||||
|
}
|
||||||
|
|
||||||
ui.horizontal(|ui| {
|
ui.horizontal(|ui| {
|
||||||
let side_padding = ui.available_width().sub(MAX_NOTE_WIDTH).max(0.0).div(2.0);
|
let side_padding = ui.available_width().sub(MAX_NOTE_WIDTH).max(0.0).div(2.0);
|
||||||
ui.add_space(side_padding);
|
ui.add_space(side_padding);
|
||||||
@ -88,6 +218,8 @@ impl FileEditor {
|
|||||||
ui.add_space(side_padding);
|
ui.add_space(side_padding);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
self.scroll_delta = scroll_area.inner_rect.height() * 0.5;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -114,54 +246,49 @@ impl FileEditor {
|
|||||||
|
|
||||||
let mut retain = true;
|
let mut retain = true;
|
||||||
|
|
||||||
if is_dragging {
|
// Createa horizontal area to draw the buffer item. The three things drawn here are:
|
||||||
let (_, drop) = ui.dnd_drop_zone::<DraggingItem, _>(Frame::NONE, |ui| {
|
// - The controls that exist at the left-size of the buffer item, i.e. "up"/"down".
|
||||||
ui.set_min_size(vec2(ui.available_width(), drag_zone_height));
|
// - The buffer item.
|
||||||
});
|
// - The controls that exist at the right-size of the buffer item, i.e. "delete".
|
||||||
if let Some(drop) = drop {
|
|
||||||
drop_from_to = Some((drop.index, i));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// the dnd_drop_zone adds 3pts work of extra space
|
|
||||||
ui.add_space(drag_zone_height + 3.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
ui.horizontal(|ui| {
|
ui.horizontal(|ui| {
|
||||||
// We don't know how tall the buffer item will be, so we'll reserve
|
// At this point, we don't know how tall the buffer item will be, so we'll reserve
|
||||||
// some horizontal space here and come back to drawing the dragger
|
// some horizontal space here and come back to drawing the controls later.
|
||||||
// later.
|
let (_id, mut left_controls_rect) = ui.allocate_space(Vec2::new(20.0, 1.0));
|
||||||
let (dragger_id, mut dragger_rect) = ui.allocate_space(Vec2::new(20.0, 1.0));
|
|
||||||
|
|
||||||
// Leave some space at the end for the delete button..
|
// Leave some space at the end for the delete button.
|
||||||
let w = ui.available_width();
|
let w = ui.available_width();
|
||||||
let item_size = Vec2::new(w - 20.0, 0.0);
|
let item_size = Vec2::new(w - 20.0, 0.0);
|
||||||
|
|
||||||
let item_response = ui.allocate_ui(item_size, |ui| match item {
|
let item_response = ui.allocate_ui(item_size, |ui| match item {
|
||||||
BufferItem::Text(text_edit) => {
|
BufferItem::Text(text_edit) => {
|
||||||
text_edit.ui(ui);
|
if text_edit.ui(ui).changed {
|
||||||
|
self.is_dirty = true;
|
||||||
}
|
}
|
||||||
BufferItem::Handwriting(painting) => {
|
}
|
||||||
|
BufferItem::Handwriting(handwriting) => {
|
||||||
let style = HandwritingStyle {
|
let style = HandwritingStyle {
|
||||||
animate: preferences.animations,
|
animate: preferences.animations,
|
||||||
|
hide_cursor: preferences.hide_handwriting_cursor,
|
||||||
..HandwritingStyle::from_theme(ui.ctx().theme())
|
..HandwritingStyle::from_theme(ui.ctx().theme())
|
||||||
};
|
};
|
||||||
painting.ui(&style, ui);
|
if handwriting.ui(&style, ui).changed {
|
||||||
|
self.is_dirty = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Delete-button
|
// Delete-button
|
||||||
if ui.button("x").clicked() {
|
if ui.button("⌫").clicked() {
|
||||||
retain = false;
|
retain = false;
|
||||||
ui.ctx().request_repaint();
|
ui.ctx().request_repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw the dragger using the height from the buffer item
|
left_controls_rect.set_height(item_response.response.rect.height());
|
||||||
dragger_rect.set_height(item_response.response.rect.height());
|
|
||||||
|
|
||||||
// Controls for moving the buffer item
|
// Controls for moving the buffer item
|
||||||
ui.allocate_new_ui(
|
ui.scope_builder(
|
||||||
UiBuilder::new()
|
UiBuilder::new()
|
||||||
.max_rect(dragger_rect)
|
.max_rect(left_controls_rect)
|
||||||
.layout(Layout::top_down(Align::Center)),
|
.layout(Layout::top_down(Align::Center)),
|
||||||
|ui| {
|
|ui| {
|
||||||
let up_button_response = ui.add_enabled(!is_first, Button::new("⇡"));
|
let up_button_response = ui.add_enabled(!is_first, Button::new("⇡"));
|
||||||
@ -169,17 +296,12 @@ impl FileEditor {
|
|||||||
drop_from_to = Some((i, i - 1));
|
drop_from_to = Some((i, i - 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
ui.dnd_drag_source(dragger_id, DraggingItem { index: i }, |ui| {
|
// Add some space so that the next button is drawn
|
||||||
Button::new("≡")
|
// at the bottom of the buffer item.
|
||||||
.min_size(
|
ui.add_space(
|
||||||
// Use all available height, save for the height taken up by
|
left_controls_rect.height()
|
||||||
// the up/down buttons + padding. Assume down-button is the
|
- (up_button_response.rect.height() * 2.0 + 4.0),
|
||||||
// equally tall as the up-button.
|
);
|
||||||
dragger_rect.size()
|
|
||||||
- Vec2::Y * (up_button_response.rect.height() * 2.0 + 4.0),
|
|
||||||
)
|
|
||||||
.ui(ui);
|
|
||||||
});
|
|
||||||
|
|
||||||
if ui.add_enabled(!is_last, Button::new("⇣")).clicked() {
|
if ui.add_enabled(!is_last, Button::new("⇣")).clicked() {
|
||||||
drop_from_to = Some((i, i + 2));
|
drop_from_to = Some((i, i + 2));
|
||||||
@ -211,10 +333,12 @@ impl FileEditor {
|
|||||||
Ordering::Greater => {
|
Ordering::Greater => {
|
||||||
let item = self.buffer.remove(from);
|
let item = self.buffer.remove(from);
|
||||||
self.buffer.insert(to, item);
|
self.buffer.insert(to, item);
|
||||||
|
self.is_dirty = true;
|
||||||
}
|
}
|
||||||
Ordering::Less => {
|
Ordering::Less => {
|
||||||
let item = self.buffer.remove(from);
|
let item = self.buffer.remove(from);
|
||||||
self.buffer.insert(to - 1, item);
|
self.buffer.insert(to - 1, item);
|
||||||
|
self.is_dirty = true;
|
||||||
}
|
}
|
||||||
Ordering::Equal => {}
|
Ordering::Equal => {}
|
||||||
}
|
}
|
||||||
@ -230,6 +354,38 @@ impl FileEditor {
|
|||||||
self.title = title.to_string_lossy().to_string();
|
self.title = title.to_string_lossy().to_string();
|
||||||
self.path = Some(new_path);
|
self.path = Some(new_path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn set_dirty(&mut self, value: bool) {
|
||||||
|
self.is_dirty = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn save(&mut self, ctx: &Context, jobs: &mut Jobs) {
|
||||||
|
let Some(file_path) = self.path.clone() else {
|
||||||
|
log::info!("Can't save {}, no path set.", self.title);
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
self.is_dirty = false;
|
||||||
|
let text = self.to_string();
|
||||||
|
let inner = self
|
||||||
|
.inner
|
||||||
|
.get_or_insert_with(|| spawn_file_watcher(&file_path));
|
||||||
|
let file_event_tx = inner.file_events_tx.clone();
|
||||||
|
|
||||||
|
jobs.start(ctx, move || {
|
||||||
|
log_error(eyre!("Failed to save file {file_path:?}"), || {
|
||||||
|
let mut file = fs::File::create(file_path)?;
|
||||||
|
file.write_all(text.as_bytes())?;
|
||||||
|
let mtime = file_mtime(&file)?;
|
||||||
|
|
||||||
|
let _ = file_event_tx.send(FileEvent::NewBufferMTime(mtime));
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
});
|
||||||
|
|
||||||
|
None
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for BufferItem {
|
impl Display for BufferItem {
|
||||||
@ -267,7 +423,7 @@ impl From<&str> for FileEditor {
|
|||||||
_ => {
|
_ => {
|
||||||
let mut text_edit = MdTextEdit::new();
|
let mut text_edit = MdTextEdit::new();
|
||||||
text_edit.text.push_str(text);
|
text_edit.text.push_str(text);
|
||||||
buffer.push(BufferItem::Text(text_edit));
|
buffer.push(BufferItem::Text(Box::new(text_edit)));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -275,7 +431,7 @@ impl From<&str> for FileEditor {
|
|||||||
match item {
|
match item {
|
||||||
MdItem::Line(line) => push_text(buffer, line),
|
MdItem::Line(line) => push_text(buffer, line),
|
||||||
MdItem::CodeBlock { key, content, span } => match key {
|
MdItem::CodeBlock { key, content, span } => match key {
|
||||||
painting::CODE_BLOCK_KEY => match Handwriting::from_str(span) {
|
handwriting::CODE_BLOCK_KEY => match Handwriting::from_str(span) {
|
||||||
Ok(handwriting) => {
|
Ok(handwriting) => {
|
||||||
if let Some(BufferItem::Text(text_edit)) = buffer.last_mut() {
|
if let Some(BufferItem::Text(text_edit)) = buffer.last_mut() {
|
||||||
if text_edit.text.ends_with('\n') {
|
if text_edit.text.ends_with('\n') {
|
||||||
@ -285,7 +441,7 @@ impl From<&str> for FileEditor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
buffer.push(BufferItem::Handwriting(handwriting))
|
buffer.push(BufferItem::Handwriting(Box::new(handwriting)))
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
log::error!("Failed to decode handwriting {content:?}: {e}");
|
log::error!("Failed to decode handwriting {content:?}: {e}");
|
||||||
@ -301,6 +457,40 @@ impl From<&str> for FileEditor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn spawn_file_watcher(p: &Path) -> Inner {
|
||||||
|
let (tx, rx) = mpsc::channel();
|
||||||
|
|
||||||
|
let path = p.to_owned();
|
||||||
|
let events_tx = tx.clone();
|
||||||
|
let mut watcher = notify::recommended_watcher(move |event: notify::Result<notify::Event>| {
|
||||||
|
log_error(eyre!("watch {path:?} error"), || {
|
||||||
|
match event?.kind {
|
||||||
|
EventKind::Create(..) | EventKind::Modify(..) | EventKind::Remove(..) => {}
|
||||||
|
|
||||||
|
EventKind::Access(..) | EventKind::Any | EventKind::Other => return Ok(()),
|
||||||
|
}
|
||||||
|
|
||||||
|
let file = File::open(&path)?;
|
||||||
|
let mtime = file_mtime(&file)?;
|
||||||
|
|
||||||
|
let _ = events_tx.send(FileEvent::NewFileMTime(mtime));
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
if let Err(e) = watcher.watch(p, notify::RecursiveMode::NonRecursive) {
|
||||||
|
log::error!("Failed to watch {p:?}: {e}");
|
||||||
|
};
|
||||||
|
|
||||||
|
Inner {
|
||||||
|
file_events: rx,
|
||||||
|
file_events_tx: tx,
|
||||||
|
_file_watcher: watcher,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
|
|
||||||
|
|||||||
218
src/folder.rs
Normal file
218
src/folder.rs
Normal file
@ -0,0 +1,218 @@
|
|||||||
|
use std::{
|
||||||
|
fs::read_dir,
|
||||||
|
mem,
|
||||||
|
ops::Deref,
|
||||||
|
path::{Path, PathBuf},
|
||||||
|
sync::mpsc,
|
||||||
|
thread,
|
||||||
|
};
|
||||||
|
|
||||||
|
use egui::{Response, Ui};
|
||||||
|
use eyre::{Context, OptionExt, eyre};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
pub enum Folder {
|
||||||
|
NotLoaded {
|
||||||
|
name: String,
|
||||||
|
path: PathBuf,
|
||||||
|
},
|
||||||
|
Loading {
|
||||||
|
name: String,
|
||||||
|
path: PathBuf,
|
||||||
|
recv: mpsc::Receiver<LoadedFolder>,
|
||||||
|
},
|
||||||
|
Loaded(LoadedFolder),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct LoadedFolder {
|
||||||
|
pub name: String,
|
||||||
|
pub path: PathBuf,
|
||||||
|
pub child_folders: Vec<Folder>,
|
||||||
|
pub child_files: Vec<File>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct File {
|
||||||
|
pub name: String,
|
||||||
|
pub path: PathBuf,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct FolderResponse<'a> {
|
||||||
|
inner: Response,
|
||||||
|
pub open_file: Option<&'a Path>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Deref for FolderResponse<'_> {
|
||||||
|
type Target = Response;
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&self.inner
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl LoadedFolder {
|
||||||
|
pub fn show<'a>(&'a mut self, ui: &mut Ui) -> FolderResponse<'a> {
|
||||||
|
let mut open_file = None;
|
||||||
|
let inner = ui
|
||||||
|
.collapsing(&self.name, |ui| {
|
||||||
|
for folder in &mut self.child_folders {
|
||||||
|
open_file = open_file.or(folder.show(ui).open_file);
|
||||||
|
}
|
||||||
|
|
||||||
|
for file in &mut self.child_files {
|
||||||
|
if ui.button(&file.name).clicked() {
|
||||||
|
open_file = Some(file.path.as_path())
|
||||||
|
};
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.header_response;
|
||||||
|
|
||||||
|
FolderResponse { inner, open_file }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn load(path: PathBuf) -> eyre::Result<Self> {
|
||||||
|
let name = path
|
||||||
|
.file_name()
|
||||||
|
.ok_or_eyre("Path is missing a file-name")?
|
||||||
|
.to_string_lossy()
|
||||||
|
.to_string();
|
||||||
|
|
||||||
|
let mut child_folders = vec![];
|
||||||
|
let mut child_files = vec![];
|
||||||
|
|
||||||
|
for entry in read_dir(&path).with_context(|| eyre!("Couldn't read dir {path:?}"))? {
|
||||||
|
let entry = entry.with_context(|| eyre!("Couldn't read dir {path:?}"))?;
|
||||||
|
let path = entry.path();
|
||||||
|
let name = path
|
||||||
|
.file_name()
|
||||||
|
.ok_or_eyre("Path is missing a file-name")?
|
||||||
|
.to_string_lossy()
|
||||||
|
.to_string();
|
||||||
|
|
||||||
|
let file_type = entry.file_type()?;
|
||||||
|
|
||||||
|
if file_type.is_symlink() {
|
||||||
|
log::error!("Symlinks not yet supported, skipping {path:?}");
|
||||||
|
continue;
|
||||||
|
} else if file_type.is_file() {
|
||||||
|
child_files.push(File { name, path });
|
||||||
|
} else if file_type.is_dir() {
|
||||||
|
child_folders.push(Folder::NotLoaded { name, path });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let folder = LoadedFolder {
|
||||||
|
name,
|
||||||
|
path,
|
||||||
|
child_folders,
|
||||||
|
child_files,
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(folder)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Folder {
|
||||||
|
fn load(&mut self, ui: &mut Ui) -> Option<&mut LoadedFolder> {
|
||||||
|
if let Folder::NotLoaded { name, path } = self {
|
||||||
|
let (tx, rx) = mpsc::channel();
|
||||||
|
|
||||||
|
{
|
||||||
|
let path = path.clone();
|
||||||
|
let ctx = ui.ctx().clone();
|
||||||
|
thread::spawn(move || match LoadedFolder::load(path) {
|
||||||
|
Err(e) => log::error!("Failed to load folder: {e}"),
|
||||||
|
Ok(folder) => {
|
||||||
|
let _ = tx.send(folder);
|
||||||
|
ctx.request_repaint();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
*self = Folder::Loading {
|
||||||
|
name: mem::take(name),
|
||||||
|
path: mem::take(path),
|
||||||
|
recv: rx,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Folder::Loading { recv, .. } = self {
|
||||||
|
match recv.try_recv() {
|
||||||
|
Ok(folder) => *self = Folder::Loaded(folder),
|
||||||
|
Err(_) => return None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let Folder::Loaded(folder) = self else {
|
||||||
|
unreachable!()
|
||||||
|
};
|
||||||
|
|
||||||
|
Some(folder)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn show<'a>(&'a mut self, ui: &mut Ui) -> FolderResponse<'a> {
|
||||||
|
self.load(ui);
|
||||||
|
|
||||||
|
if let Folder::Loaded(folder) = self {
|
||||||
|
return folder.show(ui);
|
||||||
|
}
|
||||||
|
|
||||||
|
FolderResponse {
|
||||||
|
inner: ui.label(self.name()),
|
||||||
|
open_file: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn path(&self) -> &Path {
|
||||||
|
match self {
|
||||||
|
Folder::NotLoaded { path, .. } => path,
|
||||||
|
Folder::Loading { path, .. } => path,
|
||||||
|
Folder::Loaded(folder) => &folder.path,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn name(&self) -> &str {
|
||||||
|
match self {
|
||||||
|
Folder::NotLoaded { name, .. } => name,
|
||||||
|
Folder::Loading { name, .. } => name,
|
||||||
|
Folder::Loaded(folder) => &folder.name,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn unload(&mut self) {
|
||||||
|
let (name, path) = match self {
|
||||||
|
Folder::NotLoaded { .. } => return,
|
||||||
|
Folder::Loading { name, path, .. } => (name, path),
|
||||||
|
Folder::Loaded(folder) => (&mut folder.name, &mut folder.path),
|
||||||
|
};
|
||||||
|
|
||||||
|
*self = Folder::NotLoaded {
|
||||||
|
name: mem::take(name),
|
||||||
|
path: mem::take(path),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Serialize for Folder {
|
||||||
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
where
|
||||||
|
S: serde::Serializer,
|
||||||
|
{
|
||||||
|
self.path().serialize(serializer)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'de> Deserialize<'de> for Folder {
|
||||||
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||||
|
where
|
||||||
|
D: serde::Deserializer<'de>,
|
||||||
|
{
|
||||||
|
use serde::de::Error;
|
||||||
|
let path = PathBuf::deserialize(deserializer)?;
|
||||||
|
let name = path
|
||||||
|
.file_name()
|
||||||
|
.ok_or(D::Error::custom("Path is missing a file-name"))?
|
||||||
|
.to_string_lossy()
|
||||||
|
.to_string();
|
||||||
|
Ok(Folder::NotLoaded { name, path })
|
||||||
|
}
|
||||||
|
}
|
||||||
176
src/handwriting/canvas_rasterizer.rs
Normal file
176
src/handwriting/canvas_rasterizer.rs
Normal file
@ -0,0 +1,176 @@
|
|||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use egui::{
|
||||||
|
Color32, ColorImage, CornerRadius, Painter, Pos2, Rect, Stroke, StrokeKind, TextureHandle,
|
||||||
|
Vec2,
|
||||||
|
ahash::HashMap,
|
||||||
|
emath::TSTransform,
|
||||||
|
epaint::{Brush, RectShape, Vertex},
|
||||||
|
load::SizedTexture,
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::rasterizer::{PxBoundingBox, rasterize_triangle_onto, triangle_bounding_box};
|
||||||
|
|
||||||
|
use super::StrokeBlendMode;
|
||||||
|
|
||||||
|
const CHUNK_SIZE: usize = 64;
|
||||||
|
|
||||||
|
/// Rasterize onto a resizeable canvas.
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct CanvasRasterizer {
|
||||||
|
image_size: [usize; 2],
|
||||||
|
tiles: HashMap<[usize; 2], Tile>,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Tile {
|
||||||
|
bounding_box: PxBoundingBox,
|
||||||
|
image: ColorImage,
|
||||||
|
texture: Option<TextureHandle>,
|
||||||
|
texture_is_dirty: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Tile {
|
||||||
|
fn new(xi: usize, yi: usize) -> Self {
|
||||||
|
let x_from = xi * CHUNK_SIZE;
|
||||||
|
let y_from = yi * CHUNK_SIZE;
|
||||||
|
let bounding_box = PxBoundingBox {
|
||||||
|
x_from,
|
||||||
|
y_from,
|
||||||
|
x_to: x_from + CHUNK_SIZE,
|
||||||
|
y_to: y_from + CHUNK_SIZE,
|
||||||
|
};
|
||||||
|
|
||||||
|
Self {
|
||||||
|
bounding_box,
|
||||||
|
image: ColorImage::filled([CHUNK_SIZE, CHUNK_SIZE], Color32::TRANSPARENT),
|
||||||
|
texture: None,
|
||||||
|
texture_is_dirty: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CanvasRasterizer {
|
||||||
|
pub fn set_size(&mut self, width: usize, height: usize) {
|
||||||
|
self.image_size = [width, height];
|
||||||
|
self.populate_tiles();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn clear(&mut self) {
|
||||||
|
log::debug!("clearing all tiles");
|
||||||
|
self.tiles.clear();
|
||||||
|
self.populate_tiles();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn populate_tiles(&mut self) {
|
||||||
|
let [width, height] = self.image_size;
|
||||||
|
|
||||||
|
// discard tiles that are out of bounds
|
||||||
|
self.tiles.retain(|_, tile| {
|
||||||
|
tile.bounding_box.x_from <= width && tile.bounding_box.y_from <= height
|
||||||
|
});
|
||||||
|
|
||||||
|
let chunk = |max: usize| {
|
||||||
|
(0..)
|
||||||
|
.step_by(CHUNK_SIZE)
|
||||||
|
.take_while(move |n| n <= &max)
|
||||||
|
.enumerate()
|
||||||
|
};
|
||||||
|
|
||||||
|
// create new tiles where we need them
|
||||||
|
for (xi, _x) in chunk(width) {
|
||||||
|
for (yi, _y) in chunk(height) {
|
||||||
|
self.tiles
|
||||||
|
.entry([xi, yi])
|
||||||
|
.or_insert_with(|| Tile::new(xi, yi));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn rasterize<'a>(
|
||||||
|
&mut self,
|
||||||
|
point_to_pixel: TSTransform,
|
||||||
|
triangles: impl Iterator<Item = [&'a Vertex; 3]> + Clone,
|
||||||
|
) {
|
||||||
|
for triangle in triangles {
|
||||||
|
let triangle_bounding_box = triangle_bounding_box(&triangle, point_to_pixel);
|
||||||
|
for chunk in chunks_from_bounding_box(triangle_bounding_box) {
|
||||||
|
let Some(tile) = self.tiles.get_mut(&chunk) else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut point_to_tile_pixel = point_to_pixel;
|
||||||
|
point_to_tile_pixel.translation -= Vec2::new(
|
||||||
|
tile.bounding_box.x_from as f32,
|
||||||
|
tile.bounding_box.y_from as f32,
|
||||||
|
);
|
||||||
|
|
||||||
|
tile.texture_is_dirty = true;
|
||||||
|
rasterize_triangle_onto::<StrokeBlendMode>(
|
||||||
|
&mut tile.image,
|
||||||
|
point_to_tile_pixel,
|
||||||
|
triangle,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `at` defines the location in screen-coordinates where the canvas should be drawn.
|
||||||
|
pub fn show(&mut self, ctx: &egui::Context, painter: &Painter, at: Rect) {
|
||||||
|
let pixels_per_point = ctx.pixels_per_point();
|
||||||
|
let chunk_vec = Vec2::splat(CHUNK_SIZE as f32) / pixels_per_point;
|
||||||
|
|
||||||
|
for ([xi, yi], tile) in &mut self.tiles {
|
||||||
|
if tile.texture_is_dirty {
|
||||||
|
tile.texture_is_dirty = false;
|
||||||
|
if let Some(texture) = &mut tile.texture {
|
||||||
|
texture.set(tile.image.clone(), Default::default());
|
||||||
|
} else {
|
||||||
|
tile.texture = Some(ctx.load_texture(
|
||||||
|
"handwriting",
|
||||||
|
tile.image.clone(),
|
||||||
|
Default::default(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(texture) = &mut tile.texture {
|
||||||
|
let texture = SizedTexture::new(texture.id(), texture.size_vec2());
|
||||||
|
let shape = RectShape {
|
||||||
|
rect: Rect::from_min_size(
|
||||||
|
at.min + Vec2::new(*xi as f32, *yi as f32) * chunk_vec,
|
||||||
|
chunk_vec,
|
||||||
|
),
|
||||||
|
corner_radius: CornerRadius::ZERO,
|
||||||
|
fill: Color32::WHITE,
|
||||||
|
stroke: Stroke::NONE,
|
||||||
|
stroke_kind: StrokeKind::Inside,
|
||||||
|
round_to_pixels: None,
|
||||||
|
blur_width: 0.0,
|
||||||
|
brush: Some(Arc::new(Brush {
|
||||||
|
fill_texture_id: texture.id,
|
||||||
|
uv: Rect {
|
||||||
|
min: Pos2::ZERO,
|
||||||
|
max: Pos2::new(1.0, 1.0),
|
||||||
|
},
|
||||||
|
})),
|
||||||
|
};
|
||||||
|
painter.add(shape);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get all chunk indices that overlaps with a PxBoundingBox.
|
||||||
|
fn chunks_from_bounding_box(
|
||||||
|
triangle_bounding_box: PxBoundingBox,
|
||||||
|
) -> impl Iterator<Item = [usize; 2]> {
|
||||||
|
let x_from_chunk = triangle_bounding_box.x_from / CHUNK_SIZE;
|
||||||
|
let y_from_chunk = triangle_bounding_box.y_from / CHUNK_SIZE;
|
||||||
|
let x_to_chunk = triangle_bounding_box.x_to.saturating_sub(1) / CHUNK_SIZE;
|
||||||
|
let y_to_chunk = triangle_bounding_box.y_to.saturating_sub(1) / CHUNK_SIZE;
|
||||||
|
|
||||||
|
let xs = x_from_chunk..=x_to_chunk;
|
||||||
|
let ys = y_from_chunk..=y_to_chunk;
|
||||||
|
|
||||||
|
ys.flat_map(move |yi| xs.clone().map(move |xi| [xi, yi]))
|
||||||
|
}
|
||||||
97
src/handwriting/disk_format.rs
Normal file
97
src/handwriting/disk_format.rs
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
//! see [Packet]
|
||||||
|
|
||||||
|
use std::fmt::Display;
|
||||||
|
|
||||||
|
use half::f16;
|
||||||
|
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};
|
||||||
|
|
||||||
|
/// A `u16` encoded in little-endian.
|
||||||
|
#[allow(non_camel_case_types)]
|
||||||
|
#[derive(Clone, Copy, FromBytes, IntoBytes, KnownLayout, Immutable, PartialEq, Eq)]
|
||||||
|
#[repr(C, packed)]
|
||||||
|
pub struct u16_le([u8; 2]);
|
||||||
|
|
||||||
|
/// An `f16` encoded in little-endian.
|
||||||
|
#[allow(non_camel_case_types)]
|
||||||
|
#[derive(Clone, Copy, FromBytes, IntoBytes, KnownLayout, Immutable)]
|
||||||
|
#[repr(C, packed)]
|
||||||
|
pub struct f16_le(u16_le);
|
||||||
|
|
||||||
|
/// Top-level type describing the handwriting disk-format.
|
||||||
|
#[derive(FromBytes, KnownLayout, Immutable)]
|
||||||
|
#[repr(C, packed)]
|
||||||
|
pub struct DiskFormat {
|
||||||
|
pub header: Header,
|
||||||
|
|
||||||
|
/// A packed array of [Stroke]s.
|
||||||
|
pub strokes: [u8],
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const V1: u16_le = u16_le::new(1);
|
||||||
|
|
||||||
|
#[derive(FromBytes, IntoBytes, KnownLayout, Immutable)]
|
||||||
|
#[repr(C, packed)]
|
||||||
|
pub struct Header {
|
||||||
|
/// Version of the disk format
|
||||||
|
pub version: u16_le,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(FromBytes, IntoBytes, KnownLayout, Immutable)]
|
||||||
|
#[repr(C, packed)]
|
||||||
|
pub struct RawStrokeHeader {
|
||||||
|
/// Number of points in the stroke.
|
||||||
|
pub len: u16_le,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(FromBytes, KnownLayout, Immutable)]
|
||||||
|
#[repr(C, packed)]
|
||||||
|
pub struct RawStroke {
|
||||||
|
pub header: RawStrokeHeader,
|
||||||
|
pub positions: [f16_le],
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RawStroke {
|
||||||
|
pub const MIN_LEN: usize = size_of::<RawStrokeHeader>();
|
||||||
|
}
|
||||||
|
|
||||||
|
impl u16_le {
|
||||||
|
pub const fn new(init: u16) -> Self {
|
||||||
|
u16_le(init.to_le_bytes())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl f16_le {
|
||||||
|
pub const fn new(init: f16) -> Self {
|
||||||
|
f16_le(u16_le::new(init.to_bits()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for u16_le {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
u16::from(*self).fmt(f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<u16_le> for u16 {
|
||||||
|
fn from(value: u16_le) -> Self {
|
||||||
|
u16::from_le_bytes(value.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<f16_le> for f16 {
|
||||||
|
fn from(value: f16_le) -> Self {
|
||||||
|
f16::from_bits(u16::from(value.0))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<u16> for u16_le {
|
||||||
|
fn from(value: u16) -> Self {
|
||||||
|
u16_le::new(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<f16> for f16_le {
|
||||||
|
fn from(value: f16) -> Self {
|
||||||
|
f16_le::new(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,3 +1,6 @@
|
|||||||
|
mod canvas_rasterizer;
|
||||||
|
mod disk_format;
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
fmt::{self, Display},
|
fmt::{self, Display},
|
||||||
iter, mem,
|
iter, mem,
|
||||||
@ -5,23 +8,22 @@ use std::{
|
|||||||
sync::Arc,
|
sync::Arc,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use arboard::Clipboard;
|
||||||
use base64::{Engine, prelude::BASE64_STANDARD};
|
use base64::{Engine, prelude::BASE64_STANDARD};
|
||||||
|
use canvas_rasterizer::CanvasRasterizer;
|
||||||
|
use disk_format::{DiskFormat, RawStroke, RawStrokeHeader, f16_le};
|
||||||
use egui::{
|
use egui::{
|
||||||
Color32, ColorImage, CornerRadius, Event, Frame, Id, Mesh, PointerButton, Pos2, Rect, Sense,
|
Color32, Event, Frame, Id, Mesh, PointerButton, Pos2, Rect, Sense, Shape, Stroke, Theme, Ui,
|
||||||
Shape, Stroke, TextureHandle, Theme, Ui, Vec2,
|
Vec2,
|
||||||
emath::{self, TSTransform},
|
emath::{self, TSTransform},
|
||||||
epaint::{Brush, RectShape, TessellationOptions, Tessellator, Vertex},
|
epaint::{TessellationOptions, Tessellator, Vertex},
|
||||||
load::SizedTexture,
|
|
||||||
};
|
};
|
||||||
use eyre::{Context, bail};
|
use eyre::{Context, bail};
|
||||||
use eyre::{OptionExt, eyre};
|
use eyre::{OptionExt, eyre};
|
||||||
use half::f16;
|
use half::f16;
|
||||||
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};
|
use zerocopy::{FromBytes, IntoBytes};
|
||||||
|
|
||||||
use crate::{
|
use crate::{custom_code_block::try_from_custom_code_block, rasterizer};
|
||||||
custom_code_block::try_from_custom_code_block,
|
|
||||||
rasterizer::{self, rasterize, rasterize_onto},
|
|
||||||
};
|
|
||||||
use crate::{custom_code_block::write_custom_code_block, util::random_id};
|
use crate::{custom_code_block::write_custom_code_block, util::random_id};
|
||||||
|
|
||||||
const HANDWRITING_MIN_HEIGHT: f32 = 100.0;
|
const HANDWRITING_MIN_HEIGHT: f32 = 100.0;
|
||||||
@ -33,7 +35,7 @@ pub const CODE_BLOCK_KEY: &str = "handwriting";
|
|||||||
|
|
||||||
type StrokeBlendMode = rasterizer::blend::Normal;
|
type StrokeBlendMode = rasterizer::blend::Normal;
|
||||||
|
|
||||||
const TESSELATION_OPTIONS: TessellationOptions = TessellationOptions {
|
const TESSELLATION_OPTIONS: TessellationOptions = TessellationOptions {
|
||||||
feathering: true,
|
feathering: true,
|
||||||
feathering_size_in_pixels: 1.0,
|
feathering_size_in_pixels: 1.0,
|
||||||
coarse_tessellation_culling: true,
|
coarse_tessellation_culling: true,
|
||||||
@ -55,45 +57,53 @@ pub struct HandwritingStyle {
|
|||||||
pub bg_line_stroke: Stroke,
|
pub bg_line_stroke: Stroke,
|
||||||
pub bg_color: Color32,
|
pub bg_color: Color32,
|
||||||
pub animate: bool,
|
pub animate: bool,
|
||||||
|
pub hide_cursor: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(serde::Deserialize, serde::Serialize)]
|
#[derive(serde::Deserialize, serde::Serialize)]
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub struct Handwriting {
|
pub struct Handwriting {
|
||||||
#[serde(skip, default = "random_id")]
|
|
||||||
id: Id,
|
|
||||||
|
|
||||||
strokes: Vec<Vec<Pos2>>,
|
strokes: Vec<Vec<Pos2>>,
|
||||||
|
|
||||||
/// The stroke that is currently being drawed.
|
|
||||||
#[serde(skip)]
|
|
||||||
current_stroke: Vec<Pos2>,
|
|
||||||
|
|
||||||
/// The lines that have not been blitted to `texture` yet.
|
|
||||||
#[serde(skip)]
|
|
||||||
unblitted_lines: Vec<[Pos2; 2]>,
|
|
||||||
|
|
||||||
height: f32,
|
height: f32,
|
||||||
desired_height: f32,
|
desired_height: f32,
|
||||||
|
|
||||||
/// Tesselated mesh of all strokes
|
|
||||||
#[serde(skip)]
|
#[serde(skip)]
|
||||||
|
e: Ephemeral,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Handwriting data that isn't persisted across restarts.
|
||||||
|
struct Ephemeral {
|
||||||
|
id: Id,
|
||||||
|
|
||||||
|
canvas_rasterizer: CanvasRasterizer,
|
||||||
|
|
||||||
|
/// Whether the handwriting element is being interacted with.
|
||||||
|
is_focused: bool,
|
||||||
|
|
||||||
|
/// The stroke that is currently being drawed.
|
||||||
|
current_stroke: Vec<Pos2>,
|
||||||
|
|
||||||
|
/// The lines that have not been blitted to `texture` yet.
|
||||||
|
///
|
||||||
|
/// Each pair of [Pos2]s is the start and end of one line.
|
||||||
|
unblitted_lines: Vec<[Pos2; 2]>,
|
||||||
|
|
||||||
|
tessellator: Option<Tessellator>,
|
||||||
|
|
||||||
|
/// Tessellated mesh of all strokes
|
||||||
mesh: Arc<Mesh>,
|
mesh: Arc<Mesh>,
|
||||||
|
|
||||||
#[serde(skip)]
|
|
||||||
texture: Option<TextureHandle>,
|
|
||||||
|
|
||||||
#[serde(skip)]
|
|
||||||
image: ColorImage,
|
|
||||||
|
|
||||||
#[serde(skip)]
|
|
||||||
refresh_texture: bool,
|
refresh_texture: bool,
|
||||||
|
|
||||||
/// Context of the last mesh render.
|
/// Context of the last mesh render.
|
||||||
#[serde(skip)]
|
|
||||||
last_mesh_ctx: Option<MeshContext>,
|
last_mesh_ctx: Option<MeshContext>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct HandwritingResponse {
|
||||||
|
pub changed: bool,
|
||||||
|
}
|
||||||
|
|
||||||
/// Context of a mesh render.
|
/// Context of a mesh render.
|
||||||
#[derive(Clone, Copy, PartialEq)]
|
#[derive(Clone, Copy, PartialEq)]
|
||||||
struct MeshContext {
|
struct MeshContext {
|
||||||
@ -102,56 +112,29 @@ struct MeshContext {
|
|||||||
|
|
||||||
pub pixels_per_point: f32,
|
pub pixels_per_point: f32,
|
||||||
|
|
||||||
/// Canvas size in points
|
|
||||||
pub size: Vec2,
|
|
||||||
|
|
||||||
pub stroke: Stroke,
|
pub stroke: Stroke,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get [Painting::texture], initializing it if necessary.
|
|
||||||
macro_rules! texture {
|
|
||||||
($self_:expr, $ui:expr, $mesh_context:expr) => {{
|
|
||||||
let ui: &Ui = $ui;
|
|
||||||
let mesh_context: &MeshContext = $mesh_context;
|
|
||||||
let image_size = mesh_context.pixel_size();
|
|
||||||
|
|
||||||
let new_image = || {
|
|
||||||
let image = ColorImage::new(image_size, Color32::TRANSPARENT);
|
|
||||||
ui.ctx()
|
|
||||||
.load_texture("handwriting", image, Default::default())
|
|
||||||
};
|
|
||||||
|
|
||||||
let texture = $self_.texture.get_or_insert_with(new_image);
|
|
||||||
|
|
||||||
if texture.size() != image_size {
|
|
||||||
$self_.refresh_texture = true;
|
|
||||||
// TODO: don't redraw the entire mesh, just blit the old texture onto the new one
|
|
||||||
*texture = new_image()
|
|
||||||
};
|
|
||||||
|
|
||||||
texture
|
|
||||||
}};
|
|
||||||
}
|
|
||||||
|
|
||||||
impl MeshContext {
|
|
||||||
/// Calculate canvas size in pixels
|
|
||||||
pub fn pixel_size(&self) -> [usize; 2] {
|
|
||||||
let Vec2 { x, y } = self.size * self.pixels_per_point;
|
|
||||||
[x, y].map(|f| f.ceil() as usize)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for Handwriting {
|
impl Default for Handwriting {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
id: random_id(),
|
|
||||||
strokes: Default::default(),
|
strokes: Default::default(),
|
||||||
current_stroke: Default::default(),
|
|
||||||
height: HANDWRITING_MIN_HEIGHT,
|
height: HANDWRITING_MIN_HEIGHT,
|
||||||
desired_height: HANDWRITING_MIN_HEIGHT,
|
desired_height: HANDWRITING_MIN_HEIGHT,
|
||||||
|
e: Default::default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Ephemeral {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
id: random_id(),
|
||||||
|
canvas_rasterizer: Default::default(),
|
||||||
|
is_focused: false,
|
||||||
|
current_stroke: Default::default(),
|
||||||
|
tessellator: None,
|
||||||
mesh: Default::default(),
|
mesh: Default::default(),
|
||||||
texture: None,
|
|
||||||
image: ColorImage::new([0, 0], Color32::WHITE),
|
|
||||||
refresh_texture: true,
|
refresh_texture: true,
|
||||||
last_mesh_ctx: None,
|
last_mesh_ctx: None,
|
||||||
unblitted_lines: Default::default(),
|
unblitted_lines: Default::default(),
|
||||||
@ -164,6 +147,7 @@ impl Handwriting {
|
|||||||
&mut self,
|
&mut self,
|
||||||
style: Option<&mut HandwritingStyle>,
|
style: Option<&mut HandwritingStyle>,
|
||||||
ui: &mut egui::Ui,
|
ui: &mut egui::Ui,
|
||||||
|
response: &mut HandwritingResponse,
|
||||||
) -> egui::Response {
|
) -> egui::Response {
|
||||||
ui.horizontal(|ui| {
|
ui.horizontal(|ui| {
|
||||||
if let Some(style) = style {
|
if let Some(style) = style {
|
||||||
@ -172,33 +156,41 @@ impl Handwriting {
|
|||||||
ui.separator();
|
ui.separator();
|
||||||
}
|
}
|
||||||
|
|
||||||
if ui.button("Clear Painting").clicked() {
|
if ui.button("clear").clicked() {
|
||||||
self.strokes.clear();
|
self.strokes.clear();
|
||||||
self.refresh_texture = true;
|
self.e.refresh_texture = true;
|
||||||
|
response.changed = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
ui.add_enabled_ui(!self.strokes.is_empty(), |ui| {
|
ui.add_enabled_ui(!self.strokes.is_empty(), |ui| {
|
||||||
if ui.button("Undo").clicked() {
|
if ui.button("undo").clicked() {
|
||||||
self.strokes.pop();
|
self.strokes.pop();
|
||||||
self.refresh_texture = true;
|
self.e.refresh_texture = true;
|
||||||
|
response.changed = true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
let vertex_count: usize = self.mesh.indices.len() / 3;
|
if ui.button("copy").clicked() {
|
||||||
|
let text = self.to_string();
|
||||||
|
// TODO: move to a job
|
||||||
|
let _ = Clipboard::new().unwrap().set_text(text);
|
||||||
|
}
|
||||||
|
|
||||||
|
let vertex_count: usize = self.e.mesh.indices.len() / 3;
|
||||||
ui.label(format!("vertices: {vertex_count}"));
|
ui.label(format!("vertices: {vertex_count}"));
|
||||||
})
|
})
|
||||||
.response
|
.response
|
||||||
}
|
}
|
||||||
|
|
||||||
fn commit_current_line(&mut self) {
|
pub fn ui_content(
|
||||||
debug_assert!(!self.current_stroke.is_empty());
|
&mut self,
|
||||||
self.strokes.push(mem::take(&mut self.current_stroke));
|
style: &HandwritingStyle,
|
||||||
}
|
ui: &mut Ui,
|
||||||
|
hw_response: &mut HandwritingResponse,
|
||||||
pub fn ui_content(&mut self, style: &HandwritingStyle, ui: &mut Ui) -> egui::Response {
|
) -> egui::Response {
|
||||||
if style.animate {
|
if style.animate {
|
||||||
self.height = ui.ctx().animate_value_with_time(
|
self.height = ui.ctx().animate_value_with_time(
|
||||||
self.id.with("height animation"),
|
self.e.id.with("height animation"),
|
||||||
self.desired_height,
|
self.desired_height,
|
||||||
0.4,
|
0.4,
|
||||||
);
|
);
|
||||||
@ -206,41 +198,59 @@ impl Handwriting {
|
|||||||
self.height = self.desired_height;
|
self.height = self.desired_height;
|
||||||
}
|
}
|
||||||
|
|
||||||
let size = Vec2::new(ui.available_width(), self.height);
|
let desired_size = Vec2::new(ui.available_width(), self.height);
|
||||||
let (response, painter) = ui.allocate_painter(size, Sense::drag());
|
let (mut response, painter) = ui.allocate_painter(desired_size, Sense::drag());
|
||||||
|
|
||||||
let mut response = response
|
if style.hide_cursor {
|
||||||
//.on_hover_cursor(CursorIcon::Crosshair)
|
response = response.on_hover_and_drag_cursor(egui::CursorIcon::None);
|
||||||
//.on_hover_and_drag_cursor(CursorIcon::None)
|
}
|
||||||
;
|
|
||||||
|
|
||||||
let size = response.rect.size();
|
let size = response.rect.size();
|
||||||
|
|
||||||
let to_screen = emath::RectTransform::from_to(
|
// Calculate matrices that convert between screen-space and image-space.
|
||||||
//Rect::from_min_size(Pos2::ZERO, response.rect.square_proportions()),
|
// - image-space: 0,0 is the top-left of the texture.
|
||||||
Rect::from_min_size(Pos2::ZERO, size),
|
// - screen-space: 0,0 is the top-left of the window.
|
||||||
response.rect,
|
// Both spaces use the same logical points, not pixels.
|
||||||
);
|
let to_screen =
|
||||||
|
emath::RectTransform::from_to(Rect::from_min_size(Pos2::ZERO, size), response.rect);
|
||||||
let from_screen = to_screen.inverse();
|
let from_screen = to_screen.inverse();
|
||||||
|
|
||||||
|
// Was the user in the process of drawing a stroke last frame?
|
||||||
|
let was_drawing = !self.e.current_stroke.is_empty();
|
||||||
|
|
||||||
|
// Is the user in the process of drawing a stroke now?;
|
||||||
let is_drawing = response.interact_pointer_pos().is_some();
|
let is_drawing = response.interact_pointer_pos().is_some();
|
||||||
let was_drawing = !self.current_stroke.is_empty();
|
|
||||||
|
if is_drawing {
|
||||||
|
self.e.is_focused = true;
|
||||||
|
} else if ui.ctx().input(|i| i.pointer.any_down()) {
|
||||||
|
// if the user starts interactive with something else, unfocus the handwriting area.
|
||||||
|
self.e.is_focused = false;
|
||||||
|
}
|
||||||
|
|
||||||
if !is_drawing {
|
if !is_drawing {
|
||||||
// commit current line
|
|
||||||
if was_drawing {
|
if was_drawing {
|
||||||
self.commit_current_line();
|
// commit current line
|
||||||
|
self.commit_current_line(hw_response);
|
||||||
response.mark_changed();
|
response.mark_changed();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let padding = if self.e.is_focused {
|
||||||
|
HANDWRITING_BOTTOM_PADDING
|
||||||
|
} else {
|
||||||
|
0.0
|
||||||
|
};
|
||||||
|
|
||||||
// recalculate how tall the widget should be
|
// recalculate how tall the widget should be
|
||||||
let lines_max_y = self
|
let lines_max_y = self
|
||||||
.strokes
|
.strokes
|
||||||
.iter()
|
.iter()
|
||||||
.flatten()
|
.flatten()
|
||||||
.map(|p| p.y + HANDWRITING_BOTTOM_PADDING)
|
.map(|p| p.y + padding)
|
||||||
.fold(HANDWRITING_MIN_HEIGHT, |max, y| max.max(y));
|
.fold(HANDWRITING_MIN_HEIGHT, |max, y| max.max(y));
|
||||||
|
|
||||||
|
// Change the height of the handwriting item.
|
||||||
|
// We don't do this mid-stroke, only when the user e.g. lifts the pen.
|
||||||
if self.desired_height != lines_max_y {
|
if self.desired_height != lines_max_y {
|
||||||
self.desired_height = lines_max_y;
|
self.desired_height = lines_max_y;
|
||||||
response.mark_changed();
|
response.mark_changed();
|
||||||
@ -267,16 +277,17 @@ impl Handwriting {
|
|||||||
_ => Some(next),
|
_ => Some(next),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.cloned()
|
|
||||||
.filter(|event| {
|
.filter(|event| {
|
||||||
// FIXME: pinenote: PointerMoved are duplicated after the MouseMoved events
|
// FIXME: pinenote: PointerMoved are duplicated after the MouseMoved events
|
||||||
cfg!(not(feature = "pinenote")) || !matches!(event, Event::PointerMoved(..))
|
cfg!(not(feature = "pinenote")) || !matches!(event, Event::PointerMoved(..))
|
||||||
})
|
})
|
||||||
|
.cloned()
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Process input events and turn them into strokes
|
||||||
for event in events {
|
for event in events {
|
||||||
let last_canvas_pos = self.current_stroke.last();
|
let last_canvas_pos = self.e.current_stroke.last();
|
||||||
|
|
||||||
match event {
|
match event {
|
||||||
Event::PointerMoved(new_position) => {
|
Event::PointerMoved(new_position) => {
|
||||||
@ -318,13 +329,13 @@ impl Handwriting {
|
|||||||
} => match (button, pressed) {
|
} => match (button, pressed) {
|
||||||
(PointerButton::Primary, true) => {
|
(PointerButton::Primary, true) => {
|
||||||
if last_canvas_pos.is_none() {
|
if last_canvas_pos.is_none() {
|
||||||
self.current_stroke.push(from_screen * pos);
|
self.e.current_stroke.push(from_screen * pos);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(PointerButton::Primary, false) => {
|
(PointerButton::Primary, false) => {
|
||||||
if last_canvas_pos.is_some() {
|
if last_canvas_pos.is_some() {
|
||||||
self.push_to_stroke(from_screen * pos);
|
self.push_to_stroke(from_screen * pos);
|
||||||
self.commit_current_line();
|
self.commit_current_line(hw_response);
|
||||||
response.mark_changed();
|
response.mark_changed();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -340,8 +351,8 @@ impl Handwriting {
|
|||||||
// TODO: In theory, we can get multiple press->draw->release series
|
// TODO: In theory, we can get multiple press->draw->release series
|
||||||
// in the same frame. Should handle this.
|
// in the same frame. Should handle this.
|
||||||
Event::PointerGone | Event::WindowFocused(false) => {
|
Event::PointerGone | Event::WindowFocused(false) => {
|
||||||
if !self.current_stroke.is_empty() {
|
if !self.e.current_stroke.is_empty() {
|
||||||
self.commit_current_line();
|
self.commit_current_line(hw_response);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -361,6 +372,7 @@ impl Handwriting {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Draw the horizontal ruled lines
|
||||||
(1..)
|
(1..)
|
||||||
.map(|n| n as f32 * HANDWRITING_LINE_SPACING)
|
.map(|n| n as f32 * HANDWRITING_LINE_SPACING)
|
||||||
.take_while(|&y| y < size.y)
|
.take_while(|&y| y < size.y)
|
||||||
@ -373,163 +385,169 @@ impl Handwriting {
|
|||||||
painter.add(shape);
|
painter.add(shape);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Get the position and dimensions of the image
|
||||||
let mesh_rect = response
|
let mesh_rect = response
|
||||||
.rect
|
.rect
|
||||||
.with_max_y(response.rect.min.y + self.desired_height);
|
.with_max_y(response.rect.min.y + self.desired_height);
|
||||||
|
|
||||||
|
// These are the values that, if changed, would require the mesh to be re-rendered.
|
||||||
let new_context = MeshContext {
|
let new_context = MeshContext {
|
||||||
ui_theme: ui.ctx().theme(),
|
ui_theme: ui.ctx().theme(),
|
||||||
pixels_per_point: ui.pixels_per_point(),
|
pixels_per_point: ui.pixels_per_point(),
|
||||||
size: mesh_rect.size(),
|
|
||||||
stroke: style.stroke,
|
stroke: style.stroke,
|
||||||
};
|
};
|
||||||
|
|
||||||
if Some(&new_context) != self.last_mesh_ctx.as_ref() {
|
// Figure out if we need to re-rasterize the mesh.
|
||||||
self.refresh_texture = true;
|
if Some(&new_context) != self.e.last_mesh_ctx.as_ref() {
|
||||||
|
self.e.refresh_texture = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.refresh_texture {
|
let [px_width, px_height] = {
|
||||||
// rasterize the entire texture from scratch
|
let Vec2 { x, y } = mesh_rect.size() * new_context.pixels_per_point;
|
||||||
self.refresh_texture(style, new_context, ui);
|
[x, y].map(|f| f.ceil() as usize)
|
||||||
self.unblitted_lines.clear();
|
|
||||||
} else if !self.unblitted_lines.is_empty() {
|
|
||||||
// only rasterize the new lines onto the existing texture
|
|
||||||
for [from, to] in std::mem::take(&mut self.unblitted_lines) {
|
|
||||||
self.draw_line_to_texture(from, to, &new_context, ui);
|
|
||||||
}
|
|
||||||
self.unblitted_lines.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
//painter.add(self.mesh.clone());
|
|
||||||
|
|
||||||
if let Some(texture) = &self.texture {
|
|
||||||
let texture = SizedTexture::new(texture.id(), texture.size_vec2());
|
|
||||||
let shape = RectShape {
|
|
||||||
rect: mesh_rect,
|
|
||||||
corner_radius: CornerRadius::ZERO,
|
|
||||||
fill: Color32::WHITE,
|
|
||||||
stroke: Stroke::NONE,
|
|
||||||
stroke_kind: egui::StrokeKind::Inside,
|
|
||||||
round_to_pixels: None,
|
|
||||||
blur_width: 0.0,
|
|
||||||
brush: Some(Arc::new(Brush {
|
|
||||||
fill_texture_id: texture.id,
|
|
||||||
uv: Rect {
|
|
||||||
min: Pos2::ZERO,
|
|
||||||
max: Pos2::new(1.0, 1.0),
|
|
||||||
},
|
|
||||||
})),
|
|
||||||
};
|
};
|
||||||
painter.add(shape);
|
|
||||||
|
self.e.canvas_rasterizer.set_size(px_width, px_height);
|
||||||
|
|
||||||
|
if self.e.refresh_texture {
|
||||||
|
// ...if we do, rasterize the entire texture from scratch
|
||||||
|
self.refresh_texture(style, new_context);
|
||||||
|
self.e.unblitted_lines.clear();
|
||||||
|
} else if !self.e.unblitted_lines.is_empty() {
|
||||||
|
// ...if we don't, we can get away with only rasterizing the *new* lines onto the
|
||||||
|
// existing texture.
|
||||||
|
for [from, to] in std::mem::take(&mut self.e.unblitted_lines) {
|
||||||
|
self.draw_line_to_texture(from, to, &new_context);
|
||||||
}
|
}
|
||||||
|
self.e.unblitted_lines.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw the texture
|
||||||
|
self.e.canvas_rasterizer.show(ui.ctx(), &painter, mesh_rect);
|
||||||
|
|
||||||
response
|
response
|
||||||
}
|
}
|
||||||
|
|
||||||
fn refresh_texture(
|
fn commit_current_line(&mut self, response: &mut HandwritingResponse) {
|
||||||
&mut self,
|
debug_assert!(!self.e.current_stroke.is_empty());
|
||||||
style: &HandwritingStyle,
|
self.strokes.push(mem::take(&mut self.e.current_stroke));
|
||||||
mesh_context: MeshContext,
|
response.changed = true;
|
||||||
ui: &mut Ui,
|
}
|
||||||
) {
|
|
||||||
self.last_mesh_ctx = Some(mesh_context);
|
|
||||||
|
|
||||||
self.refresh_texture = false;
|
/// Tessellate and rasterize the strokes into a new texture.
|
||||||
|
fn refresh_texture(&mut self, style: &HandwritingStyle, mesh_context: MeshContext) {
|
||||||
|
let Ephemeral {
|
||||||
|
current_stroke,
|
||||||
|
tessellator,
|
||||||
|
mesh,
|
||||||
|
refresh_texture,
|
||||||
|
last_mesh_ctx,
|
||||||
|
..
|
||||||
|
} = &mut self.e;
|
||||||
|
// TODO: avoid tessellating and rasterizing on the GUI thread
|
||||||
|
|
||||||
|
*last_mesh_ctx = Some(mesh_context);
|
||||||
|
|
||||||
|
*refresh_texture = false;
|
||||||
|
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
let start_time = std::time::Instant::now();
|
let start_time = std::time::Instant::now();
|
||||||
|
|
||||||
let mut tesselator = Tessellator::new(
|
let mesh = Arc::make_mut(mesh);
|
||||||
mesh_context.pixels_per_point,
|
|
||||||
TESSELATION_OPTIONS,
|
|
||||||
Default::default(), // we don't tesselate fonts
|
|
||||||
vec![],
|
|
||||||
);
|
|
||||||
|
|
||||||
let mesh = Arc::make_mut(&mut self.mesh);
|
|
||||||
mesh.clear();
|
mesh.clear();
|
||||||
|
|
||||||
|
// TODO: re-use tessellator if pixels_per_point hasn't changed
|
||||||
|
let tessellator = tessellator.insert(new_tessellator(mesh_context.pixels_per_point));
|
||||||
|
|
||||||
self.strokes
|
self.strokes
|
||||||
.iter()
|
.iter()
|
||||||
.chain([&self.current_stroke])
|
.chain([&*current_stroke])
|
||||||
.filter(|stroke| stroke.len() >= 2)
|
.filter(|stroke| stroke.len() >= 2)
|
||||||
.map(|stroke| {
|
.map(|stroke| egui::Shape::line(stroke.clone(), style.stroke))
|
||||||
//let points: Vec<Pos2> = stroke.iter().map(|&p| to_screen * p).collect();
|
|
||||||
egui::Shape::line(stroke.clone(), style.stroke)
|
|
||||||
})
|
|
||||||
.for_each(|shape| {
|
.for_each(|shape| {
|
||||||
tesselator.tessellate_shape(shape, mesh);
|
tessellator.tessellate_shape(shape, mesh);
|
||||||
});
|
});
|
||||||
|
|
||||||
let texture = texture!(self, ui, &mesh_context);
|
// sanity-check that tessellation did not produce any NaNs.
|
||||||
let triangles = mesh_triangles(&self.mesh);
|
// this can happen if the line contains duplicated consecutive positions
|
||||||
|
//for vertex in &mesh.vertices {
|
||||||
|
// debug_assert!(vertex.pos.x.is_finite(), "{} must be finite", vertex.pos.x);
|
||||||
|
// debug_assert!(vertex.pos.y.is_finite(), "{} must be finite", vertex.pos.y);
|
||||||
|
//}
|
||||||
|
|
||||||
let [px_x, px_y] = mesh_context.pixel_size();
|
|
||||||
let point_to_pixel = TSTransform::from_scaling(mesh_context.pixels_per_point);
|
let point_to_pixel = TSTransform::from_scaling(mesh_context.pixels_per_point);
|
||||||
self.image = rasterize::<StrokeBlendMode>(px_x, px_y, point_to_pixel, triangles);
|
let triangles = mesh_triangles(&self.e.mesh);
|
||||||
texture.set(self.image.clone(), Default::default());
|
|
||||||
|
self.e.canvas_rasterizer.clear();
|
||||||
|
self.e
|
||||||
|
.canvas_rasterizer
|
||||||
|
.rasterize(point_to_pixel, triangles);
|
||||||
|
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
{
|
{
|
||||||
let elapsed = start_time.elapsed();
|
let elapsed = start_time.elapsed();
|
||||||
println!("refreshed mesh in {:.3}s", elapsed.as_secs_f32());
|
log::debug!("refreshed mesh in {:.3}s", elapsed.as_secs_f32());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ui(&mut self, style: &HandwritingStyle, ui: &mut Ui) {
|
pub fn ui(&mut self, style: &HandwritingStyle, ui: &mut Ui) -> HandwritingResponse {
|
||||||
|
let mut response = HandwritingResponse { changed: false };
|
||||||
|
|
||||||
ui.vertical_centered_justified(|ui| {
|
ui.vertical_centered_justified(|ui| {
|
||||||
self.ui_control(None, ui);
|
self.ui_control(None, ui, &mut response);
|
||||||
|
|
||||||
//ui.label("Paint with your mouse/touch!");
|
|
||||||
Frame::canvas(ui.style())
|
Frame::canvas(ui.style())
|
||||||
.corner_radius(20.0)
|
.corner_radius(20.0)
|
||||||
.stroke(Stroke::new(5.0, Color32::from_black_alpha(40)))
|
.stroke(Stroke::new(5.0, Color32::from_black_alpha(40)))
|
||||||
.fill(style.bg_color)
|
.fill(style.bg_color)
|
||||||
.show(ui, |ui| {
|
.show(ui, |ui| {
|
||||||
self.ui_content(style, ui);
|
self.ui_content(style, ui, &mut response);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
response
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Append a new [Pos2] to [Self::current_stroke].
|
||||||
|
///
|
||||||
|
/// Queue a new line to be drawn onto [Self::texture].
|
||||||
fn push_to_stroke(&mut self, new_canvas_pos: Pos2) {
|
fn push_to_stroke(&mut self, new_canvas_pos: Pos2) {
|
||||||
if let Some(&last_canvas_pos) = self.current_stroke.last() {
|
if let Some(&last_canvas_pos) = self.e.current_stroke.last() {
|
||||||
if last_canvas_pos == new_canvas_pos {
|
if last_canvas_pos == new_canvas_pos {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.unblitted_lines.push([last_canvas_pos, new_canvas_pos]);
|
self.e
|
||||||
|
.unblitted_lines
|
||||||
|
.push([last_canvas_pos, new_canvas_pos]);
|
||||||
}
|
}
|
||||||
|
|
||||||
self.current_stroke.push(new_canvas_pos);
|
self.e.current_stroke.push(new_canvas_pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Draw a single line onto the existing texture.
|
/// Draw a single line onto the existing texture.
|
||||||
fn draw_line_to_texture(
|
fn draw_line_to_texture(&mut self, from: Pos2, to: Pos2, mesh_context: &MeshContext) {
|
||||||
&mut self,
|
// INVARIANT: if this function was called, then pixels_per_point is the same as last frame,
|
||||||
from: Pos2,
|
// so there's no need to create a new tessellator.
|
||||||
to: Pos2,
|
let tessellator = self
|
||||||
mesh_context: &MeshContext,
|
.e
|
||||||
ui: &mut Ui,
|
.tessellator
|
||||||
) {
|
.get_or_insert_with(|| new_tessellator(mesh_context.pixels_per_point));
|
||||||
let mut tesselator = Tessellator::new(
|
|
||||||
mesh_context.pixels_per_point,
|
|
||||||
TESSELATION_OPTIONS,
|
|
||||||
Default::default(), // we don't tesselate fonts
|
|
||||||
vec![],
|
|
||||||
);
|
|
||||||
|
|
||||||
let mut mesh = Mesh::default();
|
let mut mesh = Mesh::default();
|
||||||
let line = egui::Shape::line_segment([from, to], mesh_context.stroke);
|
let line = egui::Shape::line_segment([from, to], mesh_context.stroke);
|
||||||
tesselator.tessellate_shape(line, &mut mesh);
|
tessellator.tessellate_shape(line, &mut mesh);
|
||||||
|
|
||||||
self.draw_mesh_to_texture(&mesh, mesh_context, ui);
|
self.draw_mesh_to_texture(&mesh, mesh_context);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Draw a single mesh onto the existing texture.
|
/// Draw a single mesh onto the existing texture.
|
||||||
fn draw_mesh_to_texture(&mut self, mesh: &Mesh, mesh_context: &MeshContext, ui: &mut Ui) {
|
fn draw_mesh_to_texture(&mut self, mesh: &Mesh, mesh_context: &MeshContext) {
|
||||||
let triangles = mesh_triangles(mesh);
|
let triangles = mesh_triangles(mesh);
|
||||||
let point_to_pixel = TSTransform::from_scaling(mesh_context.pixels_per_point);
|
let point_to_pixel = TSTransform::from_scaling(mesh_context.pixels_per_point);
|
||||||
rasterize_onto::<StrokeBlendMode>(&mut self.image, point_to_pixel, triangles);
|
self.e
|
||||||
texture!(self, ui, mesh_context).set(self.image.clone(), Default::default());
|
.canvas_rasterizer
|
||||||
|
.rasterize(point_to_pixel, triangles);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn strokes(&self) -> &[Vec<Pos2>] {
|
pub fn strokes(&self) -> &[Vec<Pos2>] {
|
||||||
@ -557,24 +575,49 @@ impl Handwriting {
|
|||||||
..Default::default()
|
..Default::default()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn encode_as_disk_format(&self) -> Box<[u8]> {
|
||||||
|
let mut bytes = vec![];
|
||||||
|
let header = disk_format::Header {
|
||||||
|
version: disk_format::V1,
|
||||||
|
};
|
||||||
|
|
||||||
|
bytes.extend_from_slice(header.as_bytes());
|
||||||
|
|
||||||
|
for stroke in &self.strokes {
|
||||||
|
let Ok(len) = u16::try_from(stroke.len()) else {
|
||||||
|
log::error!("More than u16::MAX points in a stroke!");
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
|
||||||
|
let header = RawStrokeHeader { len: len.into() };
|
||||||
|
bytes.extend_from_slice(header.as_bytes());
|
||||||
|
|
||||||
|
for position in stroke {
|
||||||
|
for v in [position.x, position.y] {
|
||||||
|
let v = f16::from_f32(v);
|
||||||
|
let v = f16_le::from(v);
|
||||||
|
bytes.extend_from_slice(v.as_bytes());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bytes.into_boxed_slice()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn new_tessellator(pixels_per_point: f32) -> Tessellator {
|
||||||
|
Tessellator::new(
|
||||||
|
pixels_per_point,
|
||||||
|
TESSELLATION_OPTIONS,
|
||||||
|
Default::default(), // we don't tessellate fonts
|
||||||
|
vec![],
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for Handwriting {
|
impl Display for Handwriting {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
let mut raw = vec![];
|
let raw = self.encode_as_disk_format();
|
||||||
|
|
||||||
for stroke in &self.strokes {
|
|
||||||
raw.push((stroke.len() as u16).to_le_bytes());
|
|
||||||
for position in stroke {
|
|
||||||
let x = half::f16::from_f32(position.x);
|
|
||||||
let y = half::f16::from_f32(position.y);
|
|
||||||
raw.push(x.to_bits().to_le_bytes());
|
|
||||||
raw.push(y.to_bits().to_le_bytes());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let raw = raw.as_slice().as_bytes();
|
|
||||||
|
|
||||||
write_custom_code_block(f, CODE_BLOCK_KEY, BASE64_STANDARD.encode(raw))
|
write_custom_code_block(f, CODE_BLOCK_KEY, BASE64_STANDARD.encode(raw))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -590,53 +633,72 @@ impl FromStr for Handwriting {
|
|||||||
.decode(s)
|
.decode(s)
|
||||||
.wrap_err("Failed to decode painting data from base64")?;
|
.wrap_err("Failed to decode painting data from base64")?;
|
||||||
|
|
||||||
#[allow(non_camel_case_types)]
|
// HACK: first iteration of disk format did not have version header
|
||||||
type u16_le = [u8; 2];
|
//let mut bytes = bytes;
|
||||||
|
//bytes.insert(0, 0);
|
||||||
|
//bytes.insert(0, 1);
|
||||||
|
|
||||||
#[allow(non_camel_case_types)]
|
let disk_format = DiskFormat::ref_from_bytes(&bytes[..]).map_err(|_| eyre!("Too short"))?;
|
||||||
type f16_le = [u8; 2];
|
|
||||||
|
|
||||||
#[derive(FromBytes, KnownLayout, Immutable)]
|
if disk_format.header.version != disk_format::V1 {
|
||||||
#[repr(C, packed)]
|
bail!(
|
||||||
struct Stroke {
|
"Unknown disk_format version: {}",
|
||||||
pub len: u16_le,
|
disk_format.header.version
|
||||||
pub positions: [f16_le],
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut bytes = &bytes[..];
|
let mut raw_strokes = &disk_format.strokes[..];
|
||||||
let mut strokes = vec![];
|
let mut strokes = vec![];
|
||||||
|
|
||||||
while !bytes.is_empty() {
|
while !raw_strokes.is_empty() {
|
||||||
let header_len = size_of::<u16_le>();
|
if raw_strokes.len() < RawStroke::MIN_LEN {
|
||||||
if bytes.len() < header_len {
|
bail!("Invalid remaining length: {}", raw_strokes.len());
|
||||||
bail!("Invalid remaining length: {}", bytes.len());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let stroke = Stroke::ref_from_bytes(&bytes[..header_len]).expect("length is correct");
|
let stroke = RawStroke::ref_from_bytes(&raw_strokes[..RawStroke::MIN_LEN])
|
||||||
let len = usize::from(u16::from_le_bytes(stroke.len));
|
.expect("length is correct");
|
||||||
let len = len * size_of::<f16_le>() * 2;
|
|
||||||
|
|
||||||
if bytes.len() < len {
|
// get length as number of points
|
||||||
bail!("Invalid remaining length: {}", bytes.len());
|
let len = usize::from(u16::from(stroke.header.len));
|
||||||
|
|
||||||
|
// convert to length in bytes
|
||||||
|
let byte_len = 2 * size_of::<f16_le>() * len;
|
||||||
|
|
||||||
|
if raw_strokes.len() < byte_len {
|
||||||
|
bail!("Invalid remaining length: {}", raw_strokes.len());
|
||||||
}
|
}
|
||||||
|
|
||||||
let (stroke, rest) = bytes.split_at(header_len + len);
|
let (stroke, rest) = raw_strokes.split_at(RawStroke::MIN_LEN + byte_len);
|
||||||
bytes = rest;
|
raw_strokes = rest;
|
||||||
let stroke = Stroke::ref_from_bytes(stroke)
|
|
||||||
.map_err(|e| eyre!("Failed to decode stroke bytes: {e}"))?;
|
|
||||||
|
|
||||||
let mut positions = stroke
|
let stroke = RawStroke::ref_from_bytes(stroke).expect("length is correct");
|
||||||
|
|
||||||
|
debug_assert_eq!(
|
||||||
|
stroke.positions.len().rem_euclid(2),
|
||||||
|
0,
|
||||||
|
"{} must be divisible by 2",
|
||||||
|
stroke.positions.len()
|
||||||
|
);
|
||||||
|
debug_assert_eq!(stroke.positions.len(), len * 2);
|
||||||
|
|
||||||
|
let mut last_pos = Pos2::new(f32::NEG_INFINITY, f32::INFINITY);
|
||||||
|
|
||||||
|
// positions are encoded as an array of f16s [x, y, x, y, x, y, ..]
|
||||||
|
let stroke: Vec<Pos2> = stroke
|
||||||
.positions
|
.positions
|
||||||
.iter()
|
.chunks_exact(2)
|
||||||
.map(|&position| f16::from_bits(u16::from_le_bytes(position)));
|
.map(|chunk| [chunk[0], chunk[1]])
|
||||||
|
.map(|pos| pos.map(f16::from)) // interpret bytes as f16
|
||||||
|
.map(|pos| pos.map(f32::from)) // widen to f32
|
||||||
|
.filter(|pos| pos.iter().all(|f| f.is_finite())) // filter out NaNs and Infs
|
||||||
|
.map(|[x, y]| Pos2::new(x, y))
|
||||||
|
.filter(|pos| {
|
||||||
|
let is_duplicate = pos == &last_pos;
|
||||||
|
last_pos = *pos;
|
||||||
|
!is_duplicate // skip duplicates
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
let mut stroke = vec![];
|
|
||||||
while let Some(x) = positions.next() {
|
|
||||||
let Some(y) = positions.next() else {
|
|
||||||
unreachable!("len is a multiple of two");
|
|
||||||
};
|
|
||||||
stroke.push(Pos2::new(x.into(), y.into()));
|
|
||||||
}
|
|
||||||
strokes.push(stroke);
|
strokes.push(stroke);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -667,16 +729,19 @@ impl HandwritingStyle {
|
|||||||
}
|
}
|
||||||
|
|
||||||
HandwritingStyle {
|
HandwritingStyle {
|
||||||
stroke: Stroke::new(1.6, stroke_color),
|
stroke: Stroke::new(1.0, stroke_color),
|
||||||
bg_color,
|
bg_color,
|
||||||
bg_line_stroke: Stroke::new(0.5, line_color),
|
bg_line_stroke: Stroke::new(0.5, line_color),
|
||||||
animate: true,
|
animate: true,
|
||||||
|
hide_cursor: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn mesh_triangles(mesh: &Mesh) -> impl Iterator<Item = [&Vertex; 3]> {
|
fn mesh_triangles(mesh: &Mesh) -> impl Iterator<Item = [&Vertex; 3]> + Clone {
|
||||||
mesh.triangles()
|
mesh.indices
|
||||||
|
.chunks_exact(3)
|
||||||
|
.map(|chunk| [chunk[0], chunk[1], chunk[2]])
|
||||||
.map(|indices| indices.map(|i| &mesh.vertices[i as usize]))
|
.map(|indices| indices.map(|i| &mesh.vertices[i as usize]))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -0,0 +1,19 @@
|
|||||||
|
---
|
||||||
|
source: src/handwriting/mod.rs
|
||||||
|
expression: deserialized.strokes
|
||||||
|
---
|
||||||
|
[
|
||||||
|
[
|
||||||
|
[-1.0 1.0],
|
||||||
|
[3.0 1.0],
|
||||||
|
[3.0 3.0],
|
||||||
|
[1.5 2.0],
|
||||||
|
[0.0 0.0],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
[3.0 3.0],
|
||||||
|
[-1.0 1.0],
|
||||||
|
[0.0 0.0],
|
||||||
|
[3.0 1.0],
|
||||||
|
],
|
||||||
|
]
|
||||||
@ -0,0 +1,19 @@
|
|||||||
|
---
|
||||||
|
source: src/handwriting/mod.rs
|
||||||
|
expression: handwriting.strokes
|
||||||
|
---
|
||||||
|
[
|
||||||
|
[
|
||||||
|
[-1.0 1.0],
|
||||||
|
[3.0 1.0],
|
||||||
|
[3.0 3.0],
|
||||||
|
[1.5 2.0],
|
||||||
|
[0.0 0.0],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
[3.0 3.0],
|
||||||
|
[-1.0 1.0],
|
||||||
|
[0.0 0.0],
|
||||||
|
[3.0 1.0],
|
||||||
|
],
|
||||||
|
]
|
||||||
@ -0,0 +1,7 @@
|
|||||||
|
---
|
||||||
|
source: src/handwriting/mod.rs
|
||||||
|
expression: serialized
|
||||||
|
---
|
||||||
|
```handwriting
|
||||||
|
AQAFAAC8ADwAQgA8AEIAQgA+AEAAAAAABAAAQgBCALwAPAAAAAAAQgA8
|
||||||
|
```
|
||||||
@ -1,14 +1,14 @@
|
|||||||
#![warn(clippy::all, rust_2018_idioms)]
|
|
||||||
|
|
||||||
pub mod app;
|
pub mod app;
|
||||||
pub mod constants;
|
pub mod constants;
|
||||||
pub mod custom_code_block;
|
pub mod custom_code_block;
|
||||||
pub mod file_editor;
|
pub mod file_editor;
|
||||||
|
pub mod folder;
|
||||||
|
pub mod handwriting;
|
||||||
pub mod markdown;
|
pub mod markdown;
|
||||||
pub mod painting;
|
|
||||||
pub mod preferences;
|
pub mod preferences;
|
||||||
pub mod rasterizer;
|
pub mod rasterizer;
|
||||||
pub mod text_editor;
|
pub mod text_editor;
|
||||||
|
pub mod text_styles;
|
||||||
pub mod util;
|
pub mod util;
|
||||||
|
|
||||||
pub use app::App;
|
pub use app::App;
|
||||||
|
|||||||
@ -40,7 +40,7 @@ pub struct Style {
|
|||||||
pub raised: bool,
|
pub raised: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum MarkdownItem<'a> {
|
pub enum Item<'a> {
|
||||||
Text {
|
Text {
|
||||||
span: Span<'a>,
|
span: Span<'a>,
|
||||||
style: Style,
|
style: Style,
|
||||||
|
|||||||
@ -1,10 +1,12 @@
|
|||||||
use egui::text::{CCursorRange, LayoutJob};
|
use egui::text::{CCursorRange, LayoutJob};
|
||||||
|
|
||||||
use crate::markdown::{
|
use crate::{
|
||||||
span::Span,
|
markdown::Heading,
|
||||||
tokenizer::{Heading, Token, TokenKind, tokenize},
|
text_styles::{H1, H1_MONO, H2, H2_MONO, H3, H3_MONO, H4, H4_MONO, H5, H5_MONO, H6, H6_MONO},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use super::{Item, Style, parse};
|
||||||
|
|
||||||
/// Highlight markdown, caching previous output to save CPU.
|
/// Highlight markdown, caching previous output to save CPU.
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct MemoizedHighlighter {
|
pub struct MemoizedHighlighter {
|
||||||
@ -13,36 +15,6 @@ pub struct MemoizedHighlighter {
|
|||||||
output: LayoutJob,
|
output: LayoutJob,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
|
|
||||||
pub struct Style {
|
|
||||||
/// # heading (large text)
|
|
||||||
pub heading: Option<Heading>,
|
|
||||||
|
|
||||||
/// > quoted (slightly dimmer color or other font style)
|
|
||||||
pub quoted: bool,
|
|
||||||
|
|
||||||
/// `code` (monospace, some other color)
|
|
||||||
pub code: bool,
|
|
||||||
|
|
||||||
/// self.strong* (emphasized, e.g. bold)
|
|
||||||
pub strong: bool,
|
|
||||||
|
|
||||||
/// _underline_
|
|
||||||
pub underline: bool,
|
|
||||||
|
|
||||||
/// ~strikethrough~
|
|
||||||
pub strikethrough: bool,
|
|
||||||
|
|
||||||
/// /italics/
|
|
||||||
pub italics: bool,
|
|
||||||
|
|
||||||
/// $small$
|
|
||||||
pub small: bool,
|
|
||||||
|
|
||||||
/// ^raised^
|
|
||||||
pub raised: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl MemoizedHighlighter {
|
impl MemoizedHighlighter {
|
||||||
pub fn highlight(
|
pub fn highlight(
|
||||||
&mut self,
|
&mut self,
|
||||||
@ -67,189 +39,87 @@ pub fn highlight_markdown(
|
|||||||
_cursor: Option<CCursorRange>,
|
_cursor: Option<CCursorRange>,
|
||||||
) -> LayoutJob {
|
) -> LayoutJob {
|
||||||
let mut job = LayoutJob::default();
|
let mut job = LayoutJob::default();
|
||||||
let mut style = Style::default();
|
let code_style = Style {
|
||||||
|
|
||||||
let mut prev = TokenKind::Newline;
|
|
||||||
|
|
||||||
let tokens: Vec<_> = tokenize(text).collect();
|
|
||||||
let mut tokens = &tokens[..];
|
|
||||||
|
|
||||||
const CODE_INDENT: f32 = 10.0;
|
|
||||||
|
|
||||||
while !tokens.is_empty() {
|
|
||||||
let token = tokens.first().unwrap();
|
|
||||||
tokens = &tokens[1..];
|
|
||||||
|
|
||||||
let start_of_line = prev == TokenKind::Newline;
|
|
||||||
prev = token.kind;
|
|
||||||
|
|
||||||
let mut basic_style: Option<fn(&mut Style) -> &mut bool> = None;
|
|
||||||
|
|
||||||
match token.kind {
|
|
||||||
TokenKind::CodeBlock if start_of_line => {
|
|
||||||
let span = collect_until(
|
|
||||||
token,
|
|
||||||
&mut tokens,
|
|
||||||
series([TokenKind::Newline, TokenKind::CodeBlock]),
|
|
||||||
);
|
|
||||||
|
|
||||||
let code_style = format_from_style(
|
|
||||||
egui_style,
|
|
||||||
&Style {
|
|
||||||
code: true,
|
code: true,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
};
|
||||||
);
|
|
||||||
|
|
||||||
job.append(&*span, CODE_INDENT, code_style.clone());
|
for item in parse(text) {
|
||||||
style = Default::default();
|
match item {
|
||||||
continue;
|
Item::Text { span, style } => {
|
||||||
|
job.append(&span, 0.0, format_from_style(egui_style, &style));
|
||||||
}
|
}
|
||||||
|
Item::CodeBlock {
|
||||||
TokenKind::Newline => style = Style::default(),
|
all,
|
||||||
|
language: _, // TODO
|
||||||
TokenKind::Strong => basic_style = Some(|s| &mut s.strong),
|
code: _, // TODO
|
||||||
TokenKind::Italic => basic_style = Some(|s| &mut s.italics),
|
} => {
|
||||||
TokenKind::Strikethrough => basic_style = Some(|s| &mut s.strikethrough),
|
job.append(&all, 100.0, format_from_style(egui_style, &code_style));
|
||||||
|
|
||||||
TokenKind::CodeBlock | TokenKind::Mono => {
|
|
||||||
style.code = true;
|
|
||||||
let span = collect_until(
|
|
||||||
token,
|
|
||||||
&mut tokens,
|
|
||||||
any_of([TokenKind::Mono, TokenKind::CodeBlock, TokenKind::Newline]),
|
|
||||||
);
|
|
||||||
job.append(&*span, 0.0, format_from_style(egui_style, &style));
|
|
||||||
style.code = false;
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: different heading strengths
|
|
||||||
TokenKind::Heading(h) if start_of_line => style.heading = Some(h),
|
|
||||||
TokenKind::Quote if start_of_line => style.quoted = true,
|
|
||||||
|
|
||||||
// TODO: indented list entries
|
|
||||||
TokenKind::ListEntry if start_of_line => {
|
|
||||||
job.append("• ", 0.0, format_from_style(egui_style, &style));
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TokenKind::Text
|
|
||||||
// the following tokens are only richly rendered if encountered e.g. at start_of_line.
|
|
||||||
| TokenKind::Indentation
|
|
||||||
| TokenKind::ListEntry
|
|
||||||
| TokenKind::Heading(..)
|
|
||||||
| TokenKind::Quote => {}
|
|
||||||
}
|
|
||||||
|
|
||||||
// if we encountered a marker for Bold, Italic, or Strikethrough, toggle that style and
|
|
||||||
// render the token with the style enabled.
|
|
||||||
if let Some(basic_style) = basic_style {
|
|
||||||
let mut tmp_style = style;
|
|
||||||
*basic_style(&mut tmp_style) = true;
|
|
||||||
*basic_style(&mut style) ^= true; // toggle
|
|
||||||
job.append(&token.span, 0.0, format_from_style(egui_style, &tmp_style));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
job.append(&token.span, 0.0, format_from_style(egui_style, &style));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
job
|
job
|
||||||
}
|
}
|
||||||
|
|
||||||
fn series<'a, const N: usize>(of: [TokenKind; N]) -> impl FnMut(&[Token<'a>; N]) -> bool {
|
fn format_from_style(egui_style: &egui::Style, style: &Style) -> egui::text::TextFormat {
|
||||||
move |token| {
|
|
||||||
of.iter()
|
|
||||||
.zip(token)
|
|
||||||
.all(|(kind, token)| kind == &token.kind)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn any_of<'a, const N: usize>(these: [TokenKind; N]) -> impl FnMut(&[Token<'a>; 1]) -> bool {
|
|
||||||
move |[token]| these.contains(&token.kind)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Collect all tokens up to and including `pattern`, and merge them into a signle span.
|
|
||||||
///
|
|
||||||
/// `N` determines how many specific and consecutive tokens we are looking for.
|
|
||||||
/// i.e. if we were looking for a [TokenKind::Newline] followed by a [TokenKind::Quote], `N`
|
|
||||||
/// would equal `2`.
|
|
||||||
///
|
|
||||||
/// `pattern` is a function that accepts an array of `N` tokens and returns `true` if they match,
|
|
||||||
/// i.e. if we should stop collecting. [any_of] and [series] can help to construct this function.
|
|
||||||
///
|
|
||||||
/// The collected tokens will be split off the head of the slice referred to by `tokens`.
|
|
||||||
///
|
|
||||||
/// # Panic
|
|
||||||
/// Panics if `tokens` does not contain only consecutive adjacent spans.
|
|
||||||
fn collect_until<'a, const N: usize>(
|
|
||||||
token: &Token<'a>,
|
|
||||||
tokens: &mut &[Token<'a>],
|
|
||||||
pattern: impl FnMut(&[Token<'a>; N]) -> bool,
|
|
||||||
) -> Span<'a>
|
|
||||||
where
|
|
||||||
for<'b> &'b [Token<'a>; N]: TryFrom<&'b [Token<'a>]>,
|
|
||||||
{
|
|
||||||
let mut windows = tokens
|
|
||||||
.windows(N)
|
|
||||||
.map(|slice| <&[Token<'a>; N]>::try_from(slice).ok().unwrap());
|
|
||||||
|
|
||||||
let split_at = match windows.position(pattern) {
|
|
||||||
Some(i) => i + N,
|
|
||||||
None => tokens.len(), // consume everything
|
|
||||||
};
|
|
||||||
|
|
||||||
let (consume, keep) = tokens.split_at(split_at);
|
|
||||||
*tokens = keep;
|
|
||||||
|
|
||||||
consume
|
|
||||||
.iter()
|
|
||||||
.fold(token.span.clone(), |span: Span<'_>, token| {
|
|
||||||
span.try_merge(&token.span).unwrap()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn format_from_style(egui_style: &egui::Style, emark_style: &Style) -> egui::text::TextFormat {
|
|
||||||
use egui::{Align, Color32, Stroke, TextStyle};
|
use egui::{Align, Color32, Stroke, TextStyle};
|
||||||
|
|
||||||
let color = if emark_style.strong || emark_style.heading.is_some() {
|
let color = if style.code {
|
||||||
|
egui_style.visuals.strong_text_color() * Color32::GREEN
|
||||||
|
} else if style.strong || style.heading.is_some() {
|
||||||
egui_style.visuals.strong_text_color()
|
egui_style.visuals.strong_text_color()
|
||||||
} else if emark_style.quoted {
|
} else if style.quoted {
|
||||||
egui_style.visuals.weak_text_color()
|
egui_style.visuals.weak_text_color()
|
||||||
} else {
|
} else {
|
||||||
egui_style.visuals.text_color()
|
egui_style.visuals.text_color()
|
||||||
};
|
};
|
||||||
|
|
||||||
let text_style = if emark_style.heading.is_some() {
|
let text_style = if let Some(heading) = style.heading {
|
||||||
TextStyle::Heading
|
let text_style = match (heading, style.code) {
|
||||||
} else if emark_style.code {
|
(Heading::H1, false) => H1,
|
||||||
|
(Heading::H2, false) => H2,
|
||||||
|
(Heading::H3, false) => H3,
|
||||||
|
(Heading::H4, false) => H4,
|
||||||
|
(Heading::H5, false) => H5,
|
||||||
|
(Heading::H6, false) => H6,
|
||||||
|
|
||||||
|
(Heading::H1, true) => H1_MONO,
|
||||||
|
(Heading::H2, true) => H2_MONO,
|
||||||
|
(Heading::H3, true) => H3_MONO,
|
||||||
|
(Heading::H4, true) => H4_MONO,
|
||||||
|
(Heading::H5, true) => H5_MONO,
|
||||||
|
(Heading::H6, true) => H6_MONO,
|
||||||
|
};
|
||||||
|
|
||||||
|
TextStyle::Name(text_style.into())
|
||||||
|
} else if style.code {
|
||||||
TextStyle::Monospace
|
TextStyle::Monospace
|
||||||
} else if emark_style.small | emark_style.raised {
|
} else if style.small | style.raised {
|
||||||
TextStyle::Small
|
TextStyle::Small
|
||||||
} else {
|
} else {
|
||||||
TextStyle::Body
|
TextStyle::Body
|
||||||
};
|
};
|
||||||
|
|
||||||
let background = if emark_style.code {
|
let background = if style.code {
|
||||||
egui_style.visuals.code_bg_color
|
egui_style.visuals.code_bg_color
|
||||||
} else {
|
} else {
|
||||||
Color32::TRANSPARENT
|
Color32::TRANSPARENT
|
||||||
};
|
};
|
||||||
|
|
||||||
let underline = if emark_style.underline {
|
let underline = if style.underline {
|
||||||
Stroke::new(1.0, color)
|
Stroke::new(1.0, color)
|
||||||
} else {
|
} else {
|
||||||
Stroke::NONE
|
Stroke::NONE
|
||||||
};
|
};
|
||||||
|
|
||||||
let strikethrough = if emark_style.strikethrough {
|
let strikethrough = if style.strikethrough {
|
||||||
Stroke::new(1.0, color)
|
Stroke::new(1.0, color)
|
||||||
} else {
|
} else {
|
||||||
Stroke::NONE
|
Stroke::NONE
|
||||||
};
|
};
|
||||||
|
|
||||||
let valign = if emark_style.raised {
|
let valign = if style.raised {
|
||||||
Align::TOP
|
Align::TOP
|
||||||
} else {
|
} else {
|
||||||
Align::BOTTOM
|
Align::BOTTOM
|
||||||
@ -259,7 +129,7 @@ fn format_from_style(egui_style: &egui::Style, emark_style: &Style) -> egui::tex
|
|||||||
font_id: text_style.resolve(egui_style),
|
font_id: text_style.resolve(egui_style),
|
||||||
color,
|
color,
|
||||||
background,
|
background,
|
||||||
italics: emark_style.italics,
|
italics: style.italics,
|
||||||
underline,
|
underline,
|
||||||
strikethrough,
|
strikethrough,
|
||||||
valign,
|
valign,
|
||||||
|
|||||||
@ -1,7 +1,11 @@
|
|||||||
|
mod ast;
|
||||||
mod highlighter;
|
mod highlighter;
|
||||||
|
mod parser;
|
||||||
mod span;
|
mod span;
|
||||||
mod tokenizer;
|
mod tokenizer;
|
||||||
|
|
||||||
|
pub use ast::*;
|
||||||
pub use highlighter::*;
|
pub use highlighter::*;
|
||||||
|
pub use parser::*;
|
||||||
pub use span::*;
|
pub use span::*;
|
||||||
pub use tokenizer::*;
|
pub use tokenizer::*;
|
||||||
|
|||||||
170
src/markdown/parser.rs
Normal file
170
src/markdown/parser.rs
Normal file
@ -0,0 +1,170 @@
|
|||||||
|
use std::iter::{self, once};
|
||||||
|
|
||||||
|
use crate::markdown::Style;
|
||||||
|
|
||||||
|
use super::{Item, Span, Token, TokenKind, tokenize};
|
||||||
|
|
||||||
|
pub fn parse(text: &str) -> Vec<Item<'_>> {
|
||||||
|
let tokens: Vec<_> = tokenize(text).collect();
|
||||||
|
parse_tokens(&tokens)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parse_tokens<'a>(mut tokens: &[Token<'a>]) -> Vec<Item<'a>> {
|
||||||
|
// pretend that the first token was preceeded by a newline.
|
||||||
|
// means we don't have to handle the first token as a special case.
|
||||||
|
let mut prev = TokenKind::Newline;
|
||||||
|
|
||||||
|
let mut style = Style::default();
|
||||||
|
|
||||||
|
iter::from_fn(move || {
|
||||||
|
if tokens.is_empty() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
let token = tokens.first().unwrap();
|
||||||
|
tokens = &tokens[1..];
|
||||||
|
|
||||||
|
let start_of_line = prev == TokenKind::Newline;
|
||||||
|
prev = token.kind;
|
||||||
|
|
||||||
|
let mut basic_style: Option<fn(&mut Style) -> &mut bool> = None;
|
||||||
|
|
||||||
|
match token.kind {
|
||||||
|
TokenKind::CodeBlock if start_of_line => {
|
||||||
|
let language = collect_until(
|
||||||
|
None,
|
||||||
|
&mut tokens,
|
||||||
|
any_of([TokenKind::Newline]),
|
||||||
|
);
|
||||||
|
|
||||||
|
let code = collect_until(
|
||||||
|
None,
|
||||||
|
&mut tokens,
|
||||||
|
series([TokenKind::Newline, TokenKind::CodeBlock]),
|
||||||
|
);
|
||||||
|
|
||||||
|
let all = [
|
||||||
|
&token.span,
|
||||||
|
&language,
|
||||||
|
&code,
|
||||||
|
].into_iter().fold(Span::empty(), |a, b| a.try_merge(b).unwrap());
|
||||||
|
|
||||||
|
let language = language.trim_end_matches("\n");
|
||||||
|
let code = code.trim_end_matches("\n```");
|
||||||
|
|
||||||
|
return Some(Item::CodeBlock { all, language, code });
|
||||||
|
}
|
||||||
|
|
||||||
|
TokenKind::Newline => style = Style::default(),
|
||||||
|
|
||||||
|
TokenKind::Strong => basic_style = Some(|s| &mut s.strong),
|
||||||
|
TokenKind::Italic => basic_style = Some(|s| &mut s.italics),
|
||||||
|
TokenKind::Strikethrough => basic_style = Some(|s| &mut s.strikethrough),
|
||||||
|
|
||||||
|
TokenKind::CodeBlock | TokenKind::Mono => {
|
||||||
|
let span = collect_until(
|
||||||
|
Some(token),
|
||||||
|
&mut tokens,
|
||||||
|
any_of([TokenKind::Mono, TokenKind::CodeBlock, TokenKind::Newline]),
|
||||||
|
);
|
||||||
|
|
||||||
|
let mut style = style;
|
||||||
|
style.code = true;
|
||||||
|
|
||||||
|
return Some(Item::Text { span, style });
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: different heading strengths
|
||||||
|
TokenKind::Heading(h) if start_of_line => style.heading = Some(h),
|
||||||
|
TokenKind::Quote if start_of_line => style.quoted = true,
|
||||||
|
|
||||||
|
// TODO: replace dashes with dots
|
||||||
|
//// TODO: indented list entries
|
||||||
|
//TokenKind::ListEntry if start_of_line => {
|
||||||
|
// job.append("• ", 0.0, format_from_style(egui_style, &style));
|
||||||
|
// continue;
|
||||||
|
//}
|
||||||
|
|
||||||
|
TokenKind::Text
|
||||||
|
// the following tokens are only richly rendered if encountered e.g. at start_of_line.
|
||||||
|
| TokenKind::Indentation
|
||||||
|
| TokenKind::ListEntry
|
||||||
|
| TokenKind::Heading(..)
|
||||||
|
| TokenKind::Quote => {}
|
||||||
|
}
|
||||||
|
|
||||||
|
// if we encountered a marker for Bold, Italic, or Strikethrough, toggle that style and
|
||||||
|
// render the token with the style enabled.
|
||||||
|
if let Some(basic_style) = basic_style {
|
||||||
|
let mut tmp_style = style;
|
||||||
|
*basic_style(&mut tmp_style) = true;
|
||||||
|
*basic_style(&mut style) ^= true; // toggle
|
||||||
|
return Some(Item::Text {
|
||||||
|
span: token.span.clone(),
|
||||||
|
style: tmp_style,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Some(Item::Text {
|
||||||
|
span: token.span.clone(),
|
||||||
|
style,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn series<'a, const N: usize>(of: [TokenKind; N]) -> impl FnMut(&[Token<'a>; N]) -> bool {
|
||||||
|
move |token| {
|
||||||
|
of.iter()
|
||||||
|
.zip(token)
|
||||||
|
.all(|(kind, token)| kind == &token.kind)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn any_of<'a, const N: usize>(these: [TokenKind; N]) -> impl FnMut(&[Token<'a>; 1]) -> bool {
|
||||||
|
move |[token]| these.contains(&token.kind)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Collect all tokens up to and including `pattern`, and merge them into a signle span.
|
||||||
|
///
|
||||||
|
/// `N` determines how many specific and consecutive tokens we are looking for.
|
||||||
|
/// i.e. if we were looking for a [TokenKind::Newline] followed by a [TokenKind::Quote], `N`
|
||||||
|
/// would equal `2`.
|
||||||
|
///
|
||||||
|
/// `pattern` is a function that accepts an array of `N` tokens and returns `true` if they match,
|
||||||
|
/// i.e. if we should stop collecting. [any_of] and [series] can help to construct this function.
|
||||||
|
///
|
||||||
|
/// The collected tokens will be split off the head of the slice referred to by `tokens`.
|
||||||
|
///
|
||||||
|
/// # Panic
|
||||||
|
/// Panics if `tokens` does not contain only consecutive adjacent spans.
|
||||||
|
fn collect_until<'a, const N: usize>(
|
||||||
|
first_token: Option<&Token<'a>>,
|
||||||
|
tokens: &mut &[Token<'a>],
|
||||||
|
pattern: impl FnMut(&[Token<'a>; N]) -> bool,
|
||||||
|
) -> Span<'a>
|
||||||
|
where
|
||||||
|
// &[T; N]: TryFrom<&[T]>
|
||||||
|
for<'b> &'b [Token<'a>; N]: TryFrom<&'b [Token<'a>]>,
|
||||||
|
{
|
||||||
|
let mut windows = tokens.windows(N).map(|slice| {
|
||||||
|
<&[Token<'a>; N]>::try_from(slice)
|
||||||
|
.ok()
|
||||||
|
.expect("`windows` promises to return slices of length N")
|
||||||
|
});
|
||||||
|
|
||||||
|
let split_at = match windows.position(pattern) {
|
||||||
|
Some(i) => i + N,
|
||||||
|
None => tokens.len(), // consume everything
|
||||||
|
};
|
||||||
|
|
||||||
|
let (consume, keep) = tokens.split_at(split_at);
|
||||||
|
*tokens = keep;
|
||||||
|
|
||||||
|
once(first_token)
|
||||||
|
.flatten()
|
||||||
|
.chain(consume)
|
||||||
|
.fold(Span::empty(), |span: Span<'_>, token| {
|
||||||
|
span.try_merge(&token.span).unwrap()
|
||||||
|
})
|
||||||
|
}
|
||||||
@ -27,20 +27,20 @@ expression: examples
|
|||||||
- "Token { span: Span(8..22, \"code code code\"), kind: Text }"
|
- "Token { span: Span(8..22, \"code code code\"), kind: Text }"
|
||||||
- "Token { span: Span(22..23, \"\\n\"), kind: Newline }"
|
- "Token { span: Span(22..23, \"\\n\"), kind: Newline }"
|
||||||
- "Token { span: Span(23..26, \"```\"), kind: CodeBlock }"
|
- "Token { span: Span(23..26, \"```\"), kind: CodeBlock }"
|
||||||
- string: "*/``/*"
|
- string: "*_``_*"
|
||||||
tokens:
|
tokens:
|
||||||
- "Token { span: Span(0..1, \"*\"), kind: Strong }"
|
- "Token { span: Span(0..1, \"*\"), kind: Strong }"
|
||||||
- "Token { span: Span(1..2, \"/\"), kind: Italic }"
|
- "Token { span: Span(1..2, \"_\"), kind: Italic }"
|
||||||
- "Token { span: Span(2..3, \"`\"), kind: Mono }"
|
- "Token { span: Span(2..3, \"`\"), kind: Mono }"
|
||||||
- "Token { span: Span(3..4, \"`\"), kind: Mono }"
|
- "Token { span: Span(3..4, \"`\"), kind: Mono }"
|
||||||
- "Token { span: Span(4..5, \"/\"), kind: Italic }"
|
- "Token { span: Span(4..5, \"_\"), kind: Italic }"
|
||||||
- "Token { span: Span(5..6, \"*\"), kind: Strong }"
|
- "Token { span: Span(5..6, \"*\"), kind: Strong }"
|
||||||
- string: "*/`*/*/"
|
- string: "*_`*_*_"
|
||||||
tokens:
|
tokens:
|
||||||
- "Token { span: Span(0..1, \"*\"), kind: Strong }"
|
- "Token { span: Span(0..1, \"*\"), kind: Strong }"
|
||||||
- "Token { span: Span(1..2, \"/\"), kind: Italic }"
|
- "Token { span: Span(1..2, \"_\"), kind: Italic }"
|
||||||
- "Token { span: Span(2..3, \"`\"), kind: Mono }"
|
- "Token { span: Span(2..3, \"`\"), kind: Mono }"
|
||||||
- "Token { span: Span(3..4, \"*\"), kind: Strong }"
|
- "Token { span: Span(3..4, \"*\"), kind: Strong }"
|
||||||
- "Token { span: Span(4..5, \"/\"), kind: Italic }"
|
- "Token { span: Span(4..5, \"_\"), kind: Italic }"
|
||||||
- "Token { span: Span(5..6, \"*\"), kind: Strong }"
|
- "Token { span: Span(5..6, \"*\"), kind: Strong }"
|
||||||
- "Token { span: Span(6..7, \"/\"), kind: Italic }"
|
- "Token { span: Span(6..7, \"_\"), kind: Italic }"
|
||||||
|
|||||||
@ -3,6 +3,8 @@ use std::{
|
|||||||
ops::{Deref, Range},
|
ops::{Deref, Range},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use eyre::{bail, eyre};
|
||||||
|
|
||||||
#[derive(Clone, Eq, PartialEq)]
|
#[derive(Clone, Eq, PartialEq)]
|
||||||
pub struct Span<'a> {
|
pub struct Span<'a> {
|
||||||
complete_str: &'a str,
|
complete_str: &'a str,
|
||||||
@ -17,6 +19,13 @@ impl<'a> Span<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub const fn empty() -> Self {
|
||||||
|
Span {
|
||||||
|
complete_str: "",
|
||||||
|
range: 0..0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get(&self, slice: Range<usize>) -> Option<Self> {
|
pub fn get(&self, slice: Range<usize>) -> Option<Self> {
|
||||||
let start = self.range.start.checked_add(slice.start)?;
|
let start = self.range.start.checked_add(slice.start)?;
|
||||||
let end = self.range.start.checked_add(slice.end)?;
|
let end = self.range.start.checked_add(slice.end)?;
|
||||||
@ -41,26 +50,49 @@ impl<'a> Span<'a> {
|
|||||||
Some((head, tail))
|
Some((head, tail))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn trim_end_matches(&self, p: &str) -> Self {
|
||||||
|
if !self.ends_with(p) {
|
||||||
|
return self.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
Self {
|
||||||
|
range: self.range.start..self.range.end - p.len(),
|
||||||
|
complete_str: self.complete_str,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Try to merge the spans.
|
/// Try to merge the spans.
|
||||||
///
|
///
|
||||||
|
/// If either spans is empty, this just returns the other one.
|
||||||
/// This only works if spans are pointing into the same backing buffer, and are adjacent.
|
/// This only works if spans are pointing into the same backing buffer, and are adjacent.
|
||||||
pub fn try_merge(&self, other: &Self) -> Option<Self> {
|
pub fn try_merge(&self, other: &Self) -> eyre::Result<Self> {
|
||||||
|
if self.is_empty() {
|
||||||
|
return Ok(other.clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
if other.is_empty() {
|
||||||
|
return Ok(self.clone());
|
||||||
|
}
|
||||||
|
|
||||||
if self.complete_str.as_ptr() != other.complete_str.as_ptr() {
|
if self.complete_str.as_ptr() != other.complete_str.as_ptr() {
|
||||||
return None;
|
bail!("Can't merge different strings");
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.range.end == other.range.start {
|
if self.range.end == other.range.start {
|
||||||
Some(Self {
|
Ok(Self {
|
||||||
range: self.range.start..other.range.end,
|
range: self.range.start..other.range.end,
|
||||||
..*self
|
..*self
|
||||||
})
|
})
|
||||||
} else if self.range.start == other.range.end {
|
} else if self.range.start == other.range.end {
|
||||||
Some(Self {
|
Ok(Self {
|
||||||
range: other.range.start..self.range.end,
|
range: other.range.start..self.range.end,
|
||||||
..*self
|
..*self
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
None
|
Err(eyre!("String: {:?}", self.complete_str)
|
||||||
|
.wrap_err(eyre!("Span 2: {:?}", other.deref()))
|
||||||
|
.wrap_err(eyre!("Span 1: {:?}", self.deref()))
|
||||||
|
.wrap_err("Can't merge disjoint string spans"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,16 +1,6 @@
|
|||||||
use std::iter;
|
use std::iter;
|
||||||
|
|
||||||
use super::span::Span;
|
use super::{Heading, span::Span};
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
||||||
pub enum Heading {
|
|
||||||
H6,
|
|
||||||
H5,
|
|
||||||
H4,
|
|
||||||
H3,
|
|
||||||
H2,
|
|
||||||
H1,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||||
pub enum TokenKind {
|
pub enum TokenKind {
|
||||||
@ -41,7 +31,7 @@ pub enum TokenKind {
|
|||||||
Text,
|
Text,
|
||||||
}
|
}
|
||||||
|
|
||||||
const TOKENS: &[(&'static str, TokenKind)] = &[
|
const TOKENS: &[(&str, TokenKind)] = &[
|
||||||
("\n", TokenKind::Newline),
|
("\n", TokenKind::Newline),
|
||||||
("######", TokenKind::Heading(Heading::H6)),
|
("######", TokenKind::Heading(Heading::H6)),
|
||||||
("#####", TokenKind::Heading(Heading::H5)),
|
("#####", TokenKind::Heading(Heading::H5)),
|
||||||
@ -128,8 +118,8 @@ mod tests {
|
|||||||
"normal *bold* normal",
|
"normal *bold* normal",
|
||||||
"normal * maybe bold? * normal",
|
"normal * maybe bold? * normal",
|
||||||
"```lang\ncode code code\n```",
|
"```lang\ncode code code\n```",
|
||||||
"*/``/*",
|
"*_``_*",
|
||||||
"*/`*/*/",
|
"*_`*_*_",
|
||||||
];
|
];
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
use egui::{Color32, Context, RichText, Theme, Ui, Visuals};
|
use egui::{Color32, Context, RichText, Theme, Ui, Visuals, style::ScrollAnimation};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
@ -10,8 +10,11 @@ pub struct Preferences {
|
|||||||
/// Enable high-contrast theme
|
/// Enable high-contrast theme
|
||||||
pub high_contrast: bool,
|
pub high_contrast: bool,
|
||||||
|
|
||||||
|
/// Hide the cursor when handwriting
|
||||||
|
pub hide_handwriting_cursor: bool,
|
||||||
|
|
||||||
#[serde(skip)]
|
#[serde(skip)]
|
||||||
has_applied_theme: bool,
|
has_applied_prefs: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Preferences {
|
impl Default for Preferences {
|
||||||
@ -19,7 +22,8 @@ impl Default for Preferences {
|
|||||||
Self {
|
Self {
|
||||||
animations: true,
|
animations: true,
|
||||||
high_contrast: false,
|
high_contrast: false,
|
||||||
has_applied_theme: false,
|
has_applied_prefs: false,
|
||||||
|
hide_handwriting_cursor: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -27,16 +31,33 @@ impl Default for Preferences {
|
|||||||
impl Preferences {
|
impl Preferences {
|
||||||
/// Apply preferences, if they haven't already been applied.
|
/// Apply preferences, if they haven't already been applied.
|
||||||
pub fn apply(&mut self, ctx: &Context) {
|
pub fn apply(&mut self, ctx: &Context) {
|
||||||
if !self.has_applied_theme {
|
if !self.has_applied_prefs {
|
||||||
self.has_applied_theme = true;
|
self.has_applied_prefs = true;
|
||||||
|
|
||||||
|
let scroll_animation = if self.animations {
|
||||||
|
ScrollAnimation::default()
|
||||||
|
} else {
|
||||||
|
ScrollAnimation::none()
|
||||||
|
};
|
||||||
|
|
||||||
|
for theme in [Theme::Dark, Theme::Light] {
|
||||||
|
ctx.style_mut_of(theme, |style| {
|
||||||
|
style.scroll_animation = scroll_animation;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut dark_visuals = Visuals::dark();
|
||||||
|
let mut light_visuals = Visuals::light();
|
||||||
|
|
||||||
|
dark_visuals.code_bg_color = Color32::BLACK;
|
||||||
|
dark_visuals.code_bg_color = Color32::BLACK;
|
||||||
|
light_visuals.code_bg_color = Color32::WHITE;
|
||||||
|
|
||||||
if self.high_contrast {
|
if self.high_contrast {
|
||||||
// widgets.active: color of headers in textedit
|
// widgets.active: color of headers in textedit
|
||||||
// widgets.inactive: color of button labels
|
// widgets.inactive: color of button labels
|
||||||
// widgets.hovered: color of hovered button labels
|
// widgets.hovered: color of hovered button labels
|
||||||
// widgets.noninteractive: color of labels and normal textedit text
|
// widgets.noninteractive: color of labels and normal textedit text
|
||||||
let mut dark_visuals = Visuals::dark();
|
|
||||||
let mut light_visuals = Visuals::light();
|
|
||||||
|
|
||||||
dark_visuals.widgets.noninteractive.fg_stroke.color = Color32::WHITE;
|
dark_visuals.widgets.noninteractive.fg_stroke.color = Color32::WHITE;
|
||||||
dark_visuals.widgets.inactive.fg_stroke.color = Color32::WHITE;
|
dark_visuals.widgets.inactive.fg_stroke.color = Color32::WHITE;
|
||||||
@ -45,13 +66,16 @@ impl Preferences {
|
|||||||
light_visuals.widgets.noninteractive.fg_stroke.color = Color32::BLACK;
|
light_visuals.widgets.noninteractive.fg_stroke.color = Color32::BLACK;
|
||||||
light_visuals.widgets.inactive.fg_stroke.color = Color32::BLACK;
|
light_visuals.widgets.inactive.fg_stroke.color = Color32::BLACK;
|
||||||
light_visuals.widgets.hovered.fg_stroke.color = Color32::BLACK;
|
light_visuals.widgets.hovered.fg_stroke.color = Color32::BLACK;
|
||||||
|
} else {
|
||||||
|
dark_visuals.widgets.noninteractive.fg_stroke.color =
|
||||||
|
Color32::from_rgb(0xaa, 0xaa, 0xaa);
|
||||||
|
|
||||||
|
light_visuals.widgets.noninteractive.fg_stroke.color =
|
||||||
|
Color32::from_rgb(0x11, 0x11, 0x11);
|
||||||
|
}
|
||||||
|
|
||||||
ctx.set_visuals_of(Theme::Dark, dark_visuals);
|
ctx.set_visuals_of(Theme::Dark, dark_visuals);
|
||||||
ctx.set_visuals_of(Theme::Light, light_visuals);
|
ctx.set_visuals_of(Theme::Light, light_visuals);
|
||||||
} else {
|
|
||||||
ctx.set_visuals_of(Theme::Dark, Visuals::dark());
|
|
||||||
ctx.set_visuals_of(Theme::Light, Visuals::light());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,14 +83,20 @@ impl Preferences {
|
|||||||
pub fn show(&mut self, ui: &mut Ui) {
|
pub fn show(&mut self, ui: &mut Ui) {
|
||||||
ui.label(RichText::new("Prefs").weak());
|
ui.label(RichText::new("Prefs").weak());
|
||||||
|
|
||||||
ui.toggle_value(&mut self.animations, "Animations");
|
let animations_toggle = ui.toggle_value(&mut self.animations, "Animations");
|
||||||
|
if animations_toggle.clicked() {
|
||||||
|
self.has_applied_prefs = false;
|
||||||
|
self.apply(ui.ctx());
|
||||||
|
}
|
||||||
|
|
||||||
let high_contrast_toggle = ui.toggle_value(&mut self.high_contrast, "High Contrast");
|
let high_contrast_toggle = ui.toggle_value(&mut self.high_contrast, "High Contrast");
|
||||||
if high_contrast_toggle.clicked() {
|
if high_contrast_toggle.clicked() {
|
||||||
self.has_applied_theme = false;
|
self.has_applied_prefs = false;
|
||||||
self.apply(ui.ctx());
|
self.apply(ui.ctx());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ui.toggle_value(&mut self.hide_handwriting_cursor, "Hide Handwriting Cursor");
|
||||||
|
|
||||||
egui::widgets::global_theme_preference_buttons(ui);
|
egui::widgets::global_theme_preference_buttons(ui);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
use core::f32;
|
use core::f32;
|
||||||
use egui::{Color32, ColorImage, Pos2, Rect, Vec2, emath::TSTransform, epaint::Vertex};
|
use egui::{Color32, ColorImage, Pos2, Rect, Vec2, emath::TSTransform, epaint::Vertex};
|
||||||
|
use std::ops::Range;
|
||||||
|
|
||||||
pub trait BlendFn {
|
pub trait BlendFn {
|
||||||
fn blend(a: Color32, b: Color32) -> Color32;
|
fn blend(a: Color32, b: Color32) -> Color32;
|
||||||
@ -21,7 +22,7 @@ pub fn rasterize<'a, Blend: BlendFn>(
|
|||||||
point_to_pixel: TSTransform,
|
point_to_pixel: TSTransform,
|
||||||
triangles: impl Iterator<Item = [&'a Vertex; 3]>,
|
triangles: impl Iterator<Item = [&'a Vertex; 3]>,
|
||||||
) -> ColorImage {
|
) -> ColorImage {
|
||||||
let mut image = ColorImage::new([width, height], Color32::TRANSPARENT);
|
let mut image = ColorImage::filled([width, height], Color32::TRANSPARENT);
|
||||||
rasterize_onto::<Blend>(&mut image, point_to_pixel, triangles);
|
rasterize_onto::<Blend>(&mut image, point_to_pixel, triangles);
|
||||||
image
|
image
|
||||||
}
|
}
|
||||||
@ -72,15 +73,11 @@ pub fn rasterize_onto<'a, Blend: BlendFn>(
|
|||||||
|
|
||||||
// If the pixel is within the triangle, fill it in.
|
// If the pixel is within the triangle, fill it in.
|
||||||
if point_in_triangle.inside {
|
if point_in_triangle.inside {
|
||||||
let c0 = triangle[0]
|
let [c0, c1, c2] = [0, 1, 2].map(|i| {
|
||||||
|
triangle[i]
|
||||||
.color
|
.color
|
||||||
.linear_multiply(point_in_triangle.weights[0]);
|
.linear_multiply(point_in_triangle.weights[i])
|
||||||
let c1 = triangle[1]
|
});
|
||||||
.color
|
|
||||||
.linear_multiply(point_in_triangle.weights[1]);
|
|
||||||
let c2 = triangle[2]
|
|
||||||
.color
|
|
||||||
.linear_multiply(point_in_triangle.weights[2]);
|
|
||||||
|
|
||||||
let color = c0 + c1 + c2;
|
let color = c0 + c1 + c2;
|
||||||
|
|
||||||
@ -90,9 +87,20 @@ pub fn rasterize_onto<'a, Blend: BlendFn>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Rasterize a single triangles onto an image,
|
||||||
|
///
|
||||||
|
/// Triangle positions must be in image-local point-coords.
|
||||||
|
pub fn rasterize_triangle_onto<Blend: BlendFn>(
|
||||||
|
image: &mut ColorImage,
|
||||||
|
point_to_pixel: TSTransform,
|
||||||
|
triangle: [&Vertex; 3],
|
||||||
|
) {
|
||||||
|
rasterize_onto::<Blend>(image, point_to_pixel, [triangle].into_iter());
|
||||||
|
}
|
||||||
|
|
||||||
/// A bounding box, measured in pixels.
|
/// A bounding box, measured in pixels.
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
struct PxBoundingBox {
|
pub struct PxBoundingBox {
|
||||||
pub x_from: usize,
|
pub x_from: usize,
|
||||||
pub y_from: usize,
|
pub y_from: usize,
|
||||||
pub x_to: usize,
|
pub x_to: usize,
|
||||||
@ -108,9 +116,41 @@ impl PxBoundingBox {
|
|||||||
y_to: self.y_to.min(other.y_to),
|
y_to: self.y_to.min(other.y_to),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn union(&self, other: &PxBoundingBox) -> PxBoundingBox {
|
||||||
|
PxBoundingBox {
|
||||||
|
x_from: self.x_from.min(other.x_from),
|
||||||
|
y_from: self.y_from.min(other.y_from),
|
||||||
|
x_to: self.x_to.max(other.x_to),
|
||||||
|
y_to: self.y_to.max(other.y_to),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn triangle_bounding_box(triangle: &[&Vertex; 3], point_to_pixel: TSTransform) -> PxBoundingBox {
|
pub fn x_range(&self) -> Range<usize> {
|
||||||
|
self.x_from..self.x_to
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn y_range(&self) -> Range<usize> {
|
||||||
|
self.y_from..self.y_to
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Test whether two boxes do NOT overlap
|
||||||
|
pub fn overlaps_with(&self, other: &PxBoundingBox) -> bool {
|
||||||
|
!self.is_disjoint_from(other)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_disjoint_from(&self, other: &PxBoundingBox) -> bool {
|
||||||
|
self.x_from > other.x_to
|
||||||
|
|| self.y_from > other.y_to
|
||||||
|
|| other.x_from > self.x_to
|
||||||
|
|| other.y_from > self.y_to
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn triangle_bounding_box(
|
||||||
|
triangle: &[&Vertex; 3],
|
||||||
|
point_to_pixel: TSTransform,
|
||||||
|
) -> PxBoundingBox {
|
||||||
// calculate bounding box in point coords
|
// calculate bounding box in point coords
|
||||||
let mut rect = Rect::NOTHING;
|
let mut rect = Rect::NOTHING;
|
||||||
for vertex in triangle {
|
for vertex in triangle {
|
||||||
@ -169,6 +209,10 @@ fn point_in_triangle(point: Pos2, triangle: [&Vertex; 3]) -> PointInTriangle {
|
|||||||
// Normalize the weights.
|
// Normalize the weights.
|
||||||
let weights = areas.map(|area| area / triangle_area);
|
let weights = areas.map(|area| area / triangle_area);
|
||||||
|
|
||||||
|
if cfg!(debug_assertions) && weights.into_iter().any(f32::is_nan) {
|
||||||
|
panic!("weights must not be NaN! {weights:?} {triangle_area:?} {areas:?} {sides:?}");
|
||||||
|
}
|
||||||
|
|
||||||
PointInTriangle { inside, weights }
|
PointInTriangle { inside, weights }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,5 @@
|
|||||||
---
|
---
|
||||||
source: src/custom_code_block.rs
|
source: src/custom_code_block.rs
|
||||||
assertion_line: 133
|
|
||||||
expression: list
|
expression: list
|
||||||
---
|
---
|
||||||
[
|
[
|
||||||
@ -1,6 +1,5 @@
|
|||||||
---
|
---
|
||||||
source: src/painting.rs
|
source: src/painting.rs
|
||||||
assertion_line: 695
|
|
||||||
expression: serialized
|
expression: serialized
|
||||||
---
|
---
|
||||||
```handwriting
|
```handwriting
|
||||||
@ -24,6 +24,10 @@ pub struct MdTextEdit {
|
|||||||
cursor: Option<CCursorRange>,
|
cursor: Option<CCursorRange>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct MdTextEditOutput {
|
||||||
|
pub changed: bool,
|
||||||
|
}
|
||||||
|
|
||||||
impl MdTextEdit {
|
impl MdTextEdit {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
MdTextEdit::default()
|
MdTextEdit::default()
|
||||||
@ -36,7 +40,7 @@ impl MdTextEdit {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ui(&mut self, ui: &mut Ui) {
|
pub fn ui(&mut self, ui: &mut Ui) -> MdTextEditOutput {
|
||||||
let Self {
|
let Self {
|
||||||
text,
|
text,
|
||||||
highlighter,
|
highlighter,
|
||||||
@ -72,6 +76,10 @@ impl MdTextEdit {
|
|||||||
*cursor = text_edit.cursor_range;
|
*cursor = text_edit.cursor_range;
|
||||||
//ui.ctx().request_repaint();
|
//ui.ctx().request_repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
MdTextEditOutput {
|
||||||
|
changed: text_edit.response.changed(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
14
src/text_styles.rs
Normal file
14
src/text_styles.rs
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
//! Name of custom [egui::TextStyle]s
|
||||||
|
|
||||||
|
pub const H1: &str = "H1";
|
||||||
|
pub const H2: &str = "H2";
|
||||||
|
pub const H3: &str = "H3";
|
||||||
|
pub const H4: &str = "H4";
|
||||||
|
pub const H5: &str = "H5";
|
||||||
|
pub const H6: &str = "H6";
|
||||||
|
pub const H1_MONO: &str = "H1-mono";
|
||||||
|
pub const H2_MONO: &str = "H2-mono";
|
||||||
|
pub const H3_MONO: &str = "H3-mono";
|
||||||
|
pub const H4_MONO: &str = "H4-mono";
|
||||||
|
pub const H5_MONO: &str = "H5-mono";
|
||||||
|
pub const H6_MONO: &str = "H6-mono";
|
||||||
28
src/util.rs
28
src/util.rs
@ -1,6 +1,8 @@
|
|||||||
use std::sync::mpsc;
|
use std::{fs::File, os::unix::fs::MetadataExt as _, sync::mpsc};
|
||||||
|
|
||||||
|
use chrono::{DateTime, Local};
|
||||||
use egui::Id;
|
use egui::Id;
|
||||||
|
use eyre::{Context, ContextCompat};
|
||||||
use rand::{Rng, rng};
|
use rand::{Rng, rng};
|
||||||
|
|
||||||
pub fn random_id() -> Id {
|
pub fn random_id() -> Id {
|
||||||
@ -28,3 +30,27 @@ impl<T> GuiSender<T> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[track_caller]
|
||||||
|
pub fn log_error<T>(chain: eyre::Report, f: impl FnOnce() -> eyre::Result<T>) -> Option<T> {
|
||||||
|
f().map_err(|e| e.wrap_err(chain))
|
||||||
|
.inspect_err(|e| log::error!("{e}"))
|
||||||
|
.ok()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn file_mtime(file: &File) -> eyre::Result<DateTime<Local>> {
|
||||||
|
(move || {
|
||||||
|
let meta = file.metadata().wrap_err("Failed to stat file")?;
|
||||||
|
|
||||||
|
let sec = meta.mtime();
|
||||||
|
let nsec = meta
|
||||||
|
.mtime_nsec()
|
||||||
|
.try_into()
|
||||||
|
.wrap_err("Nanoseconds overflowed")?;
|
||||||
|
|
||||||
|
DateTime::from_timestamp(sec, nsec)
|
||||||
|
.wrap_err("Bad timestamp")
|
||||||
|
.map(Into::into)
|
||||||
|
})()
|
||||||
|
.wrap_err("Failed to get file mtime")
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user