Skip to content

Commit

Permalink
feat: language selector, lazy spellchecking
Browse files Browse the repository at this point in the history
  • Loading branch information
Kneemund committed Nov 1, 2024
1 parent b0b024d commit 719ad75
Show file tree
Hide file tree
Showing 11 changed files with 270 additions and 70 deletions.
4 changes: 4 additions & 0 deletions crates/rnote-engine/src/document/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub use background::Background;
pub use format::Format;

// Imports
use crate::engine::Spellchecker;
use crate::{Camera, CloneConfig, StrokeStore, WidgetFlags};
use core::fmt::Display;
use p2d::bounding_volume::{Aabb, BoundingVolume};
Expand Down Expand Up @@ -107,6 +108,8 @@ pub struct Document {
pub layout: Layout,
#[serde(rename = "snap_positions")]
pub snap_positions: bool,
#[serde(rename = "spellcheck_language")]
pub spellcheck_language: Option<String>,
}

impl Default for Document {
Expand All @@ -120,6 +123,7 @@ impl Default for Document {
background: Background::default(),
layout: Layout::default(),
snap_positions: false,
spellcheck_language: Spellchecker::default_language(),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/rnote-engine/src/engine/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ impl Engine {
widget_flags |= self.doc_resize_to_fit_content();
widget_flags.redraw = true;
widget_flags.refresh_ui = true;
widget_flags.spellcheck_language_modified = true;
widget_flags
}

Expand Down
50 changes: 44 additions & 6 deletions crates/rnote-engine/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,31 @@ use std::time::Instant;
use tracing::error;

pub struct Spellchecker {
pub broker: enchant::Broker,
broker: enchant::Broker,
pub dict: Option<enchant::Dict>,
}

impl Spellchecker {
pub fn default_language() -> Option<String> {
glib::language_names()
.get(0)
.map(|language| language.to_string())
}

pub fn available_languages() -> Vec<String> {
enchant::Broker::new()
.list_dicts()
.iter()
.map(|dict| dict.lang.to_owned())
.collect()
}
}

impl Default for Spellchecker {
fn default() -> Self {
let mut enchant_broker = enchant::Broker::new();
let enchant_dict = enchant_broker.request_dict(glib::language_names().first().unwrap());

Self {
broker: enchant_broker,
dict: enchant_dict.ok(),
broker: enchant::Broker::new(),
dict: None,
}
}
}
Expand Down Expand Up @@ -329,6 +342,31 @@ impl Engine {
}
}

pub fn refresh_spellcheck_language(&mut self) {
self.spellchecker.dict = self
.document
.spellcheck_language
.as_ref()
.and_then(|language| {
self.spellchecker
.broker
.request_dict(language.as_str())
.ok()
});

if let Pen::Typewriter(typewriter) = self.penholder.current_pen_ref() {
typewriter.refresh_spellcheck_cache_in_modifying_stroke(&mut EngineViewMut {
tasks_tx: self.tasks_tx.clone(),
pens_config: &mut self.pens_config,
document: &mut self.document,
store: &mut self.store,
camera: &mut self.camera,
audioplayer: &mut self.audioplayer,
spellchecker: &mut self.spellchecker,
});
}
}

pub fn optimize_epd(&self) -> bool {
self.optimize_epd
}
Expand Down
25 changes: 22 additions & 3 deletions crates/rnote-engine/src/pens/typewriter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ impl DrawableOnDoc for Typewriter {
}

// Draw error ranges
for (start_index, length) in &textstroke.error_words {
for (start_index, length) in &textstroke.spellcheck_result.errors {
textstroke.text_style.draw_text_error(
cx,
textstroke.text.clone(),
Expand Down Expand Up @@ -654,7 +654,10 @@ impl Typewriter {
let text_len = text.len();
text_style.ranged_text_attributes.clear();
text_style.set_max_width(Some(text_width));
let textstroke = TextStroke::new(text, pos, text_style, engine_view.spellchecker);

let mut textstroke = TextStroke::new(text, pos, text_style);
textstroke.check_spelling_refresh_cache(engine_view.spellchecker);

let cursor = GraphemeCursor::new(text_len, textstroke.text.len(), true);

let stroke_key = engine_view
Expand All @@ -681,7 +684,10 @@ impl Typewriter {
let text_len = text.len();
text_style.ranged_text_attributes.clear();
text_style.set_max_width(Some(text_width));
let textstroke = TextStroke::new(text, *pos, text_style, engine_view.spellchecker);

let mut textstroke = TextStroke::new(text, *pos, text_style);
textstroke.check_spelling_refresh_cache(engine_view.spellchecker);

let cursor = GraphemeCursor::new(text_len, textstroke.text.len(), true);

let stroke_key = engine_view
Expand Down Expand Up @@ -808,6 +814,19 @@ impl Typewriter {
widget_flags
}

pub(crate) fn refresh_spellcheck_cache_in_modifying_stroke(
&self,
engine_view: &mut EngineViewMut,
) {
if let TypewriterState::Modifying { stroke_key, .. } = self.state {
if let Some(Stroke::TextStroke(textstroke)) =
engine_view.store.get_stroke_mut(stroke_key)
{
textstroke.check_spelling_refresh_cache(engine_view.spellchecker);
}
}
}

pub(crate) fn toggle_text_attribute_current_selection(
&mut self,
text_attribute: TextAttribute,
Expand Down
17 changes: 9 additions & 8 deletions crates/rnote-engine/src/pens/typewriter/penevents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl Typewriter {
{
// When clicked on a textstroke, we start modifying it
if let Some(Stroke::TextStroke(textstroke)) =
engine_view.store.get_stroke_ref(stroke_key)
engine_view.store.get_stroke_mut(stroke_key)
{
let cursor = if let Ok(new_cursor) =
// get the cursor for the current position
Expand All @@ -48,6 +48,7 @@ impl Typewriter {
GraphemeCursor::new(0, textstroke.text.len(), true)
};

textstroke.check_spelling_refresh_cache(&engine_view.spellchecker);
engine_view.store.update_chrono_to_last(stroke_key);

new_state = TypewriterState::Modifying {
Expand Down Expand Up @@ -502,12 +503,9 @@ impl Typewriter {
text_style.ranged_text_attributes.clear();
text_style.set_max_width(Some(text_width));

let textstroke = TextStroke::new(
String::from(keychar),
*pos,
text_style,
engine_view.spellchecker,
);
let mut textstroke =
TextStroke::new(String::from(keychar), *pos, text_style);
textstroke.check_spelling_refresh_cache(engine_view.spellchecker);

let mut cursor = GraphemeCursor::new(0, textstroke.text.len(), true);

Expand Down Expand Up @@ -1105,7 +1103,10 @@ impl Typewriter {
text_style.ranged_text_attributes.clear();
text_style.set_max_width(Some(text_width));
let text_len = text.len();
let textstroke = TextStroke::new(text, *pos, text_style, engine_view.spellchecker);

let mut textstroke = TextStroke::new(text, *pos, text_style);
textstroke.check_spelling_refresh_cache(engine_view.spellchecker);

let cursor = GraphemeCursor::new(text_len, text_len, true);

let stroke_key = engine_view
Expand Down
2 changes: 2 additions & 0 deletions crates/rnote-engine/src/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ impl StrokeStore {
widget_flags.hide_undo = Some(!self.can_undo());
widget_flags.hide_redo = Some(!self.can_redo());
widget_flags.store_modified = true;
widget_flags.spellcheck_language_modified = true;

widget_flags
}
Expand All @@ -284,6 +285,7 @@ impl StrokeStore {
widget_flags.hide_undo = Some(!self.can_undo());
widget_flags.hide_redo = Some(!self.can_redo());
widget_flags.store_modified = true;
widget_flags.spellcheck_language_modified = true;

widget_flags
}
Expand Down
Loading

0 comments on commit 719ad75

Please sign in to comment.