7 Commits

11 changed files with 425 additions and 141 deletions

2
Cargo.lock generated
View File

@ -1344,7 +1344,7 @@ dependencies = [
[[package]] [[package]]
name = "inkr" name = "inkr"
version = "0.1.0" version = "1.0.0"
dependencies = [ dependencies = [
"base64 0.22.1", "base64 0.22.1",
"eframe", "eframe",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "inkr" name = "inkr"
version = "0.1.0" version = "1.0.0"
authors = [] authors = []
edition = "2024" edition = "2024"

27
PKGBUILD Normal file
View 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"
}

9
assets/inkr.desktop Executable file
View 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;

View File

@ -2,11 +2,14 @@ use std::{
fs, fs,
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::{file_editor::FileEditor, preferences::Preferences, util::GuiSender};
use egui::{ use egui::{
Align, Button, Color32, FontData, FontDefinitions, PointerButton, RichText, ScrollArea, Stroke, Align, Button, Color32, Context, FontData, FontDefinitions, Key, Modifiers, PointerButton,
RichText, ScrollArea, Stroke,
}; };
#[derive(serde::Deserialize, serde::Serialize)] #[derive(serde::Deserialize, serde::Serialize)]
@ -18,6 +21,8 @@ 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)>,
open_tab_index: Option<usize>, open_tab_index: Option<usize>,
@ -25,6 +30,33 @@ pub struct App {
next_tab_id: TabId, next_tab_id: TabId,
} }
pub struct Jobs {
handles: Vec<JoinHandle<()>>,
actions_tx: mpsc::Sender<Action>,
}
impl Jobs {
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,6 +68,12 @@ impl Tab {
Tab::File(file_editor) => file_editor.title(), Tab::File(file_editor) => file_editor.title(),
} }
} }
pub fn is_dirty(&self) -> bool {
match self {
Tab::File(file_editor) => file_editor.is_dirty,
}
}
} }
pub type TabId = usize; pub type TabId = usize;
@ -55,8 +93,12 @@ 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,
@ -146,7 +188,7 @@ 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)
} }
@ -175,9 +217,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,11 +230,11 @@ 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::containers::menu::Bar::new().ui(ui, |ui| {
@ -205,22 +249,15 @@ 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 text = fs::read_to_string(&file_path)
.inspect_err(|e| log::error!("Failed to read {file_path:?}: {e}"))
let text = match fs::read_to_string(&file_path) { .ok()?;
Ok(text) => text,
Err(e) => {
log::error!("Failed to read {file_path:?}: {e}");
return;
}
};
let editor = FileEditor::from_file(file_path, &text); let editor = FileEditor::from_file(file_path, &text);
let _ = actions_tx.send(Action::OpenFile(editor)); Some(Action::OpenFile(editor))
}); });
} }
@ -237,6 +274,19 @@ impl eframe::App for App {
} }
} }
let can_save_file = self
.open_tab_index
.and_then(|i| self.tabs.get(i))
.and_then(|(id, tab)| match tab {
Tab::File(file_editor) => Some((*id, file_editor)),
})
.and_then(|(_, file_editor)| file_editor.path().zip(Some(file_editor)))
.is_some();
if ui.add_enabled(can_save_file, Button::new("Save")).clicked() {
self.save_active_tab(ui.ctx());
}
let open_file = let open_file =
self.open_tab_index self.open_tab_index
.and_then(|i| self.tabs.get(i)) .and_then(|i| self.tabs.get(i))
@ -244,45 +294,22 @@ impl eframe::App for App {
Tab::File(file_editor) => Some((*id, file_editor)), Tab::File(file_editor) => Some((*id, file_editor)),
}); });
let open_file_with_path = open_file
.clone()
.and_then(|(_, file_editor)| file_editor.path().zip(Some(file_editor)));
if ui
.add_enabled(open_file_with_path.is_some(), Button::new("Save"))
.clicked()
{
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 +324,9 @@ impl eframe::App for App {
} }
}); });
ui.add_space(16.0); if !self.jobs.handles.is_empty() {
ui.spinner();
}
ui.add_space(16.0); ui.add_space(16.0);
@ -306,8 +335,7 @@ 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 tab.is_dirty() {
if dirty {
button = button.right_text(RichText::new("*").strong()) button = button.right_text(RichText::new("*").strong())
} }
@ -354,5 +382,33 @@ 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_file = self
.open_tab_index
.and_then(|i| self.tabs.get_mut(i))
.and_then(|(id, tab)| match tab {
Tab::File(file_editor) => Some((*id, file_editor)),
})
.and_then(|(_, file_editor)| {
file_editor
.path()
.map(ToOwned::to_owned)
.zip(Some(file_editor))
});
if let Some((file_path, file_editor)) = open_file {
file_editor.is_dirty = false;
let text = file_editor.to_string();
let file_path = file_path.to_owned();
self.jobs.start(ctx, move || {
if let Err(e) = fs::write(file_path, text.as_bytes()) {
log::error!("{e}");
};
None
});
}
} }
} }

View File

@ -12,7 +12,7 @@ use egui::{
use crate::{ use crate::{
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,
}; };
@ -22,6 +22,9 @@ pub struct FileEditor {
title: String, title: String,
pub path: Option<PathBuf>, pub path: Option<PathBuf>,
pub buffer: Vec<BufferItem>, pub buffer: Vec<BufferItem>,
/// Whether the file has been edited since it was laste saved to disk.
pub is_dirty: bool,
} }
#[derive(serde::Deserialize, serde::Serialize)] #[derive(serde::Deserialize, serde::Serialize)]
@ -37,6 +40,7 @@ impl FileEditor {
title: title.into(), title: title.into(),
path: None, path: None,
buffer, buffer,
is_dirty: false,
} }
} }
@ -69,9 +73,11 @@ impl FileEditor {
ui.horizontal(|ui| { ui.horizontal(|ui| {
ui.label("new"); ui.label("new");
if ui.button("text").clicked() { if ui.button("text").clicked() {
self.is_dirty = true;
self.buffer.push(BufferItem::Text(Default::default())); self.buffer.push(BufferItem::Text(Default::default()));
} }
if ui.button("writing").clicked() { if ui.button("writing").clicked() {
self.is_dirty = true;
self.buffer self.buffer
.push(BufferItem::Handwriting(Default::default())); .push(BufferItem::Handwriting(Default::default()));
} }
@ -138,14 +144,18 @@ impl FileEditor {
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,
..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;
}
} }
}); });
@ -211,10 +221,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 => {}
} }
@ -275,7 +287,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') {

View 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)
}
}

View File

@ -6,6 +6,7 @@ use std::{
}; };
use base64::{Engine, prelude::BASE64_STANDARD}; use base64::{Engine, prelude::BASE64_STANDARD};
use disk_format::{DiskFormat, RawStroke, RawStrokeHeader, f16_le};
use egui::{ use egui::{
Color32, ColorImage, CornerRadius, Event, Frame, Id, Mesh, PointerButton, Pos2, Rect, Sense, Color32, ColorImage, CornerRadius, Event, Frame, Id, Mesh, PointerButton, Pos2, Rect, Sense,
Shape, Stroke, TextureHandle, Theme, Ui, Vec2, Shape, Stroke, TextureHandle, Theme, Ui, Vec2,
@ -16,7 +17,7 @@ use egui::{
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, custom_code_block::try_from_custom_code_block,
@ -24,6 +25,8 @@ use crate::{
}; };
use crate::{custom_code_block::write_custom_code_block, util::random_id}; use crate::{custom_code_block::write_custom_code_block, util::random_id};
mod disk_format;
const HANDWRITING_MIN_HEIGHT: f32 = 100.0; const HANDWRITING_MIN_HEIGHT: f32 = 100.0;
const HANDWRITING_BOTTOM_PADDING: f32 = 80.0; const HANDWRITING_BOTTOM_PADDING: f32 = 80.0;
const HANDWRITING_MARGIN: f32 = 0.05; const HANDWRITING_MARGIN: f32 = 0.05;
@ -94,6 +97,10 @@ pub struct Handwriting {
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 {
@ -164,6 +171,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 {
@ -175,12 +183,14 @@ impl Handwriting {
if ui.button("Clear Painting").clicked() { if ui.button("Clear Painting").clicked() {
self.strokes.clear(); self.strokes.clear();
self.refresh_texture = true; self.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.refresh_texture = true;
response.changed = true;
} }
}); });
@ -190,12 +200,18 @@ impl Handwriting {
.response .response
} }
fn commit_current_line(&mut self) { fn commit_current_line(&mut self, response: &mut HandwritingResponse) {
debug_assert!(!self.current_stroke.is_empty()); debug_assert!(!self.current_stroke.is_empty());
self.strokes.push(mem::take(&mut self.current_stroke)); self.strokes.push(mem::take(&mut self.current_stroke));
response.changed = true;
} }
pub fn ui_content(&mut self, style: &HandwritingStyle, ui: &mut Ui) -> egui::Response { pub fn ui_content(
&mut self,
style: &HandwritingStyle,
ui: &mut Ui,
hw_response: &mut HandwritingResponse,
) -> 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.id.with("height animation"),
@ -206,8 +222,8 @@ 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 (response, painter) = ui.allocate_painter(desired_size, Sense::drag());
let mut response = response let mut response = response
//.on_hover_cursor(CursorIcon::Crosshair) //.on_hover_cursor(CursorIcon::Crosshair)
@ -216,20 +232,24 @@ impl Handwriting {
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();
let is_drawing = response.interact_pointer_pos().is_some(); // Was the user in the process of drawing a stroke last frame?
let was_drawing = !self.current_stroke.is_empty(); let was_drawing = !self.current_stroke.is_empty();
// Is the user in the process of drawing a stroke now?
let is_drawing = response.interact_pointer_pos().is_some();
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();
} }
@ -241,6 +261,8 @@ impl Handwriting {
.map(|p| p.y + HANDWRITING_BOTTOM_PADDING) .map(|p| p.y + HANDWRITING_BOTTOM_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();
@ -275,6 +297,7 @@ impl Handwriting {
.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.current_stroke.last();
@ -324,7 +347,7 @@ impl Handwriting {
(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();
} }
@ -341,7 +364,7 @@ impl Handwriting {
// 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.current_stroke.is_empty() {
self.commit_current_line(); self.commit_current_line(hw_response);
break; break;
} }
} }
@ -361,6 +384,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,9 +397,12 @@ impl Handwriting {
painter.add(shape); painter.add(shape);
}); });
// Get the 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(),
@ -383,24 +410,25 @@ impl Handwriting {
stroke: style.stroke, stroke: style.stroke,
}; };
// Figure out if we need to re-rasterize the mesh.
if Some(&new_context) != self.last_mesh_ctx.as_ref() { if Some(&new_context) != self.last_mesh_ctx.as_ref() {
self.refresh_texture = true; self.refresh_texture = true;
} }
if self.refresh_texture { if self.refresh_texture {
// rasterize the entire texture from scratch // ...if we do, rasterize the entire texture from scratch
self.refresh_texture(style, new_context, ui); self.refresh_texture(style, new_context, ui);
self.unblitted_lines.clear(); self.unblitted_lines.clear();
} else if !self.unblitted_lines.is_empty() { } else if !self.unblitted_lines.is_empty() {
// only rasterize the new lines onto the existing texture // ...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.unblitted_lines) { for [from, to] in std::mem::take(&mut self.unblitted_lines) {
self.draw_line_to_texture(from, to, &new_context, ui); self.draw_line_to_texture(from, to, &new_context, ui);
} }
self.unblitted_lines.clear(); self.unblitted_lines.clear();
} }
//painter.add(self.mesh.clone()); // Draw the texture
if let Some(texture) = &self.texture { if let Some(texture) = &self.texture {
let texture = SizedTexture::new(texture.id(), texture.size_vec2()); let texture = SizedTexture::new(texture.id(), texture.size_vec2());
let shape = RectShape { let shape = RectShape {
@ -460,6 +488,13 @@ impl Handwriting {
tesselator.tessellate_shape(shape, mesh); tesselator.tessellate_shape(shape, mesh);
}); });
// sanity-check that tesselation did not produce any NaNs.
// 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 texture = texture!(self, ui, &mesh_context); let texture = texture!(self, ui, &mesh_context);
let triangles = mesh_triangles(&self.mesh); let triangles = mesh_triangles(&self.mesh);
@ -475,9 +510,11 @@ impl Handwriting {
} }
} }
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!"); //ui.label("Paint with your mouse/touch!");
Frame::canvas(ui.style()) Frame::canvas(ui.style())
@ -485,9 +522,11 @@ impl Handwriting {
.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
} }
fn push_to_stroke(&mut self, new_canvas_pos: Pos2) { fn push_to_stroke(&mut self, new_canvas_pos: Pos2) {
@ -557,24 +596,40 @@ 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()
}
} }
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 +645,71 @@ 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]; //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);
} }

View File

@ -5,7 +5,7 @@ pub mod constants;
pub mod custom_code_block; pub mod custom_code_block;
pub mod easy_mark; pub mod easy_mark;
pub mod file_editor; pub mod file_editor;
pub mod painting; pub mod handwriting;
pub mod preferences; pub mod preferences;
pub mod rasterizer; pub mod rasterizer;
pub mod text_editor; pub mod text_editor;

View File

@ -72,15 +72,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;
@ -169,6 +165,12 @@ 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) {
if weights.into_iter().any(f32::is_nan) {
panic!("weights must not be NaN! {weights:?} {triangle_area:?} {areas:?} {sides:?}");
}
}
PointInTriangle { inside, weights } PointInTriangle { inside, weights }
} }

View File

@ -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(),
}
} }
} }