Skip to content

Commit

Permalink
improve text agent
Browse files Browse the repository at this point in the history
  • Loading branch information
jprochazk committed May 28, 2024
1 parent be46389 commit fd3af1a
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 214 deletions.
15 changes: 9 additions & 6 deletions crates/eframe/src/web/app_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use egui::TexturesDelta;

use crate::{epi, App};

use super::{now_sec, web_painter::WebPainter, NeedRepaint};
use super::{now_sec, text_agent::TextAgent, web_painter::WebPainter, NeedRepaint};

pub struct AppRunner {
#[allow(dead_code)]
Expand All @@ -14,7 +14,7 @@ pub struct AppRunner {
app: Box<dyn epi::App>,
pub(crate) needs_repaint: std::sync::Arc<NeedRepaint>,
last_save_time: f64,
pub(crate) ime: Option<egui::output::IMEOutput>,
pub(crate) text_agent: TextAgent,
pub(crate) mutable_text_under_cursor: bool,

// Output for the last run:
Expand All @@ -35,6 +35,7 @@ impl AppRunner {
canvas_id: &str,
web_options: crate::WebOptions,
app_creator: epi::AppCreator,
text_agent: TextAgent,
) -> Result<Self, String> {
let painter = super::ActiveWebPainter::new(canvas_id, &web_options).await?;

Expand Down Expand Up @@ -118,7 +119,7 @@ impl AppRunner {
app,
needs_repaint,
last_save_time: now_sec(),
ime: None,
text_agent,
mutable_text_under_cursor: false,
textures_delta: Default::default(),
clipped_primitives: None,
Expand Down Expand Up @@ -269,9 +270,11 @@ impl AppRunner {

self.mutable_text_under_cursor = mutable_text_under_cursor;

if self.ime != ime {
super::text_agent::move_text_cursor(ime, self.canvas());
self.ime = ime;
if let Err(err) = self.text_agent.move_to(ime, self.canvas()) {
log::error!(
"failed to update text agent position: {}",
super::string_from_js_value(&err)
);
}
}
}
Expand Down
18 changes: 11 additions & 7 deletions crates/eframe/src/web/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ pub(crate) fn install_document_events(runner_ref: &WebRunner) -> Result<(), JsVa
if !modifiers.ctrl
&& !modifiers.command
&& !should_ignore_key(&key)
// When text agent is shown, it sends text event instead.
&& text_agent::text_agent().hidden()
// When text agent is focused, it is responsible for handling input events
&& !runner.text_agent.has_focus()
{
runner.input.raw.events.push(egui::Event::Text(key));
}
Expand Down Expand Up @@ -375,10 +375,12 @@ pub(crate) fn install_canvas_events(runner_ref: &WebRunner) -> Result<(), JsValu
// event callback, which is why we run the app logic here and now:
runner.logic();

runner
.text_agent
.set_focus(runner.mutable_text_under_cursor);

// Make sure we paint the output of the above logic call asap:
runner.needs_repaint.repaint_asap();

text_agent::update_text_agent(runner);
}
event.stop_propagation();
event.prevent_default();
Expand Down Expand Up @@ -467,13 +469,15 @@ pub(crate) fn install_canvas_events(runner_ref: &WebRunner) -> Result<(), JsValu
runner.input.raw.events.push(egui::Event::PointerGone);

push_touches(runner, egui::TouchPhase::End, &event);

runner
.text_agent
.set_focus(runner.mutable_text_under_cursor);

runner.needs_repaint.repaint_asap();
event.stop_propagation();
event.prevent_default();
}

// Finally, focus or blur text agent to toggle mobile keyboard:
text_agent::update_text_agent(runner);
},
)?;

Expand Down
7 changes: 7 additions & 0 deletions crates/eframe/src/web/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ pub(crate) fn string_from_js_value(value: &JsValue) -> String {
value.as_string().unwrap_or_else(|| format!("{value:#?}"))
}

pub(crate) fn focused_element() -> Option<web_sys::HtmlElement> {
web_sys::window()?
.document()?
.active_element()
.and_then(|v| v.dyn_into().ok())
}

/// Current time in seconds (since undefined point in time).
///
/// Monotonically increasing.
Expand Down
Loading

0 comments on commit fd3af1a

Please sign in to comment.