Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve web text agent #4561

Merged
merged 8 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading