mirror of
https://github.com/storytold/designcraft.git
synced 2026-12-02 20:21:15 -05:00
Merge pull request #1197
This commit is contained in:
commit
0cbf7e698b
7 files changed
+130
-25
No files matched your search
@@ -100,6 +100,8 @@ impl eframe::App for App {
|
||||
self.startup.opening(apple_events::poll(&self.apple_events, &mut self.app, ui.ctx()));
|
||||
}
|
||||
}
|
||||
#[cfg(target_os = "macos")]
|
||||
self.app.set_input_source(selected_input_source());
|
||||
self.app.ui(ui);
|
||||
self.startup.leave();
|
||||
#[cfg(target_os = "macos")]
|
||||
@@ -134,6 +136,15 @@ fn discard_marked_text() {
|
||||
}
|
||||
}
|
||||
|
||||
/// The keyboard input source macOS has selected (its ID), or `None` without a key window. Space
|
||||
/// in a Japanese input mode types an ideographic space, which winit reports as a plain one.
|
||||
#[cfg(target_os = "macos")]
|
||||
fn selected_input_source() -> Option<String> {
|
||||
let mtm = objc2::MainThreadMarker::new()?;
|
||||
let id = objc2_app_kit::NSTextInputContext::currentInputContext(mtm)?.selectedKeyboardInputSource()?;
|
||||
Some(id.to_string())
|
||||
}
|
||||
|
||||
/// Orderly OS termination can bypass egui's cancelable close event. Finish only a complete,
|
||||
/// current quit plan; otherwise preserve every live document, including edits after the timer.
|
||||
fn preserve_documents_on_exit(app: &mut DesignApp) -> Result<(), String> {
|
||||
|
||||
@@ -155,6 +155,7 @@ impl DesignApp {
|
||||
object_clip_text: None,
|
||||
object_clip_change: None,
|
||||
ime_marked: None,
|
||||
input_source: None,
|
||||
ime_discard: false,
|
||||
canvas_ime: false,
|
||||
ime_tracker: egui::ime_context::ImeContextTracker::new(),
|
||||
|
||||
@@ -9,6 +9,14 @@ impl DesignApp {
|
||||
std::mem::take(&mut self.ime_discard)
|
||||
}
|
||||
|
||||
/// The keyboard input source the system has selected now (macOS: its ID), read by the host
|
||||
/// before each frame. The window layer reports Space as a plain space even in a Japanese
|
||||
/// input mode, where the input method types an ideographic one; the source tells them apart
|
||||
/// (see `canvas::ime::japanese_input_source`).
|
||||
pub fn set_input_source(&mut self, id: Option<String>) {
|
||||
self.input_source = id;
|
||||
}
|
||||
|
||||
/// Did the canvas ask for the system IME in the last frame (a Type tool caret)? The web
|
||||
/// build's hidden text input has the keyboard then, so text events hold what the browser
|
||||
/// typed (see [`drop_key_name_text`]).
|
||||
|
||||
@@ -28,6 +28,31 @@ fn drop_stale_ime_events(app: &mut DesignApp) {
|
||||
app.ime_interrupt = true;
|
||||
}
|
||||
|
||||
/// Is `id`, a macOS keyboard input source, a Japanese input method in a mode whose Space types an
|
||||
/// ideographic space? Input methods name their modes after a `Japanese` part: none for hiragana
|
||||
/// (`com.apple.inputmethod.Kotoeri.RomajiTyping.Japanese`, `…Kawasemi4.Japanese`, Google's
|
||||
/// `…Japanese.base`), then `Katakana` or `FullWidthRoman`. Their Roman, half-width and code
|
||||
/// modes, plain keyboard layouts (`com.apple.keylayout.ABC`) and Chinese and Korean input
|
||||
/// methods type a plain space, and so does any mode not named here.
|
||||
pub(crate) fn japanese_input_source(id: &str) -> bool {
|
||||
let mut parts = id.split('.');
|
||||
if !parts.any(|p| p == "Japanese") {
|
||||
return false;
|
||||
}
|
||||
matches!(parts.collect::<Vec<_>>().as_slice(), [] | ["base" | "Katakana" | "FullWidthRoman"])
|
||||
}
|
||||
|
||||
/// The text a key typed, given `text` as the window system reports it.
|
||||
///
|
||||
/// On macOS the window layer drops what the input method inserts when nothing is being composed
|
||||
/// and reports the key's own character: Space in a Japanese input mode comes as a plain space,
|
||||
/// where the input method types an ideographic one. With the selected input source in such a
|
||||
/// mode (`japanese`), a plain space is therefore the ideographic one; with Shift it stays plain,
|
||||
/// as the input methods have it.
|
||||
pub(crate) fn typed_text(text: &str, japanese: bool, shift: bool) -> &str {
|
||||
if japanese && !shift && text == " " { "\u{3000}" } else { text }
|
||||
}
|
||||
|
||||
/// One input method event for the text caret: marked text previews in the story, a commit types.
|
||||
pub(super) fn ime_event(app: &mut DesignApp, ctx: &egui::Context, e: egui::ImeEvent, vi: designcraft_engine::ViewInfo) {
|
||||
let r = match e {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use super::caret;
|
||||
use super::column_guides::{COLUMN_DRAG, ColumnDrag, column_guide_drag};
|
||||
use super::guides::guide_drag;
|
||||
use super::ime::{finish_pending_ime_delete, ime_event};
|
||||
use super::ime::{finish_pending_ime_delete, ime_event, japanese_input_source, typed_text};
|
||||
use super::rulers::zero_point_drag;
|
||||
use super::scroll::{mods, navigation_space, pan, spring_zoom_input, spring_zoom_mode, wheel_alt};
|
||||
use super::view::zoom_at;
|
||||
@@ -60,19 +60,15 @@ fn field_focused_id(ctx: &egui::Context) -> egui::Id {
|
||||
pub fn begin_frame(ctx: &egui::Context) {
|
||||
let (id, focused) = (field_focused_id(ctx), ctx.text_edit_focused());
|
||||
ctx.data_mut(|d| d.insert_temp(id, focused));
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
let now = ctx.input(|i| i.modifiers);
|
||||
// The modifiers last frame ended with are the ones this frame's events began with.
|
||||
ctx.data_mut(|d| {
|
||||
let (_, end) = d.get_temp::<(egui::Modifiers, egui::Modifiers)>(modifiers_id()).unwrap_or((now, now));
|
||||
d.insert_temp(modifiers_id(), (end, now));
|
||||
});
|
||||
}
|
||||
let now = ctx.input(|i| i.modifiers);
|
||||
// The modifiers last frame ended with are the ones this frame's events began with.
|
||||
ctx.data_mut(|d| {
|
||||
let (_, end) = d.get_temp::<(egui::Modifiers, egui::Modifiers)>(modifiers_id()).unwrap_or((now, now));
|
||||
d.insert_temp(modifiers_id(), (end, now));
|
||||
});
|
||||
}
|
||||
|
||||
/// (held when this frame's events began, held after them).
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn modifiers_id() -> egui::Id {
|
||||
egui::Id::unique("canvas-frame-modifiers")
|
||||
}
|
||||
@@ -326,6 +322,12 @@ fn note_release(ui: &egui::Ui, clicks_id: egui::Id, latest: Option<Pos2>, now: f
|
||||
}
|
||||
}
|
||||
|
||||
/// Text typed with no composition: into the story the caret is in now.
|
||||
fn insert_typed(app: &mut DesignApp, t: &str, shift: bool) {
|
||||
let japanese = app.input_source.as_deref().is_some_and(japanese_input_source);
|
||||
let _ = app.run("text.insert", json!({"text": typed_text(t, japanese, shift)}));
|
||||
}
|
||||
|
||||
/// Typing, IME input, a paste or a key press restarts the caret's blink.
|
||||
fn restart_caret_blink(ctx: &egui::Context, e: &egui::Event) {
|
||||
if matches!(e, egui::Event::Text(_) | egui::Event::Ime(_) | egui::Event::Paste(_) | egui::Event::Key { pressed: true, .. }) {
|
||||
@@ -470,29 +472,24 @@ fn canvas_events(app: &mut DesignApp, ui: &egui::Ui, wants_text: bool, vi: desig
|
||||
// text must not delete the character before it).
|
||||
let composing_at_start = app.session.tool_composing();
|
||||
let evs = ui.input(|i| i.events.clone());
|
||||
// Native only: egui-winit sends a ⌘+V chord as a paste without its key press, so the modifiers
|
||||
// held at the paste tell Paste in Place from Paste. The web also gets the key press, and the
|
||||
// shortcut table has already run the command. A chord's modifiers may be released in the same
|
||||
// frame, hence the modifiers held at each event.
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
// The modifiers held at each event: they may be released later in the same frame. Shift+Space
|
||||
// needs them, and so does Paste in Place natively, where egui-winit sends a ⌘+V chord as a
|
||||
// paste without its key press (the web also gets the key press, and the shortcut table has
|
||||
// already run the command).
|
||||
let mut held =
|
||||
ui.data(|d| d.get_temp::<(egui::Modifiers, egui::Modifiers)>(modifiers_id())).map_or_else(|| ui.input(|i| i.modifiers), |(start, _)| start);
|
||||
for e in evs {
|
||||
restart_caret_blink(ui.ctx(), &e);
|
||||
if let egui::Event::ModifiersChanged(m) = &e {
|
||||
held = *m;
|
||||
}
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
let chord = {
|
||||
if let egui::Event::ModifiersChanged(m) = &e {
|
||||
held = *m;
|
||||
}
|
||||
if matches!(e, egui::Event::Paste(_)) { crate::menus::paste_chord_command(app, held) } else { None }
|
||||
};
|
||||
let chord = if matches!(e, egui::Event::Paste(_)) { crate::menus::paste_chord_command(app, held) } else { None };
|
||||
let composing = composing_at_start || app.session.tool_composing();
|
||||
match e {
|
||||
egui::Event::Ime(ime) if wants_text => ime_event(app, ui.ctx(), ime, vi),
|
||||
// Keys the IME passes through come as text only while nothing is marked.
|
||||
egui::Event::Text(t) if wants_text && !composing => {
|
||||
let _ = app.run("text.insert", json!({"text": t}));
|
||||
}
|
||||
egui::Event::Text(t) if wants_text && !composing => insert_typed(app, &t, held.shift),
|
||||
// The IME owns the keyboard until it commits: no typing, clipboard or editing keys.
|
||||
egui::Event::Text(_)
|
||||
| egui::Event::Paste(_)
|
||||
|
||||
@@ -3571,6 +3571,65 @@ fn leaving_parent_editing_for_the_left_page_of_a_spread_keeps_that_page_selected
|
||||
assert_eq!(current_page(h.state()), Some(1));
|
||||
}
|
||||
|
||||
/// macOS input sources whose Space types an ideographic space: a Japanese input method's
|
||||
/// hiragana, katakana and full-width modes. Its Roman, half-width and code modes, keyboard
|
||||
/// layouts and Chinese and Korean input methods type a plain one.
|
||||
#[test]
|
||||
fn japanese_input_sources_are_told_by_their_mode() {
|
||||
use super::ime::japanese_input_source;
|
||||
for id in [
|
||||
"com.apple.inputmethod.Kotoeri.RomajiTyping.Japanese",
|
||||
"com.apple.inputmethod.Kotoeri.KanaTyping.Japanese",
|
||||
"com.apple.inputmethod.Kotoeri.RomajiTyping.Japanese.Katakana",
|
||||
"com.apple.inputmethod.Kotoeri.RomajiTyping.Japanese.FullWidthRoman",
|
||||
"jp.monokakido.inputmethod.Kawasemi4.Japanese",
|
||||
"com.google.inputmethod.Japanese.base",
|
||||
] {
|
||||
assert!(japanese_input_source(id), "{id}");
|
||||
}
|
||||
for id in [
|
||||
"com.apple.inputmethod.Kotoeri.RomajiTyping.Roman",
|
||||
"com.apple.inputmethod.Kotoeri.RomajiTyping.Japanese.HalfWidthKana",
|
||||
"jp.monokakido.inputmethod.Kawasemi4.Japanese.HalfWidthRoman",
|
||||
"jp.monokakido.inputmethod.Kawasemi4.Japanese.Code",
|
||||
"jp.monokakido.inputmethod.Kawasemi4.Roman",
|
||||
"com.google.inputmethod.Japanese.Roman",
|
||||
"com.apple.keylayout.ABC",
|
||||
"com.apple.inputmethod.SCIM.ITABC",
|
||||
"com.apple.inputmethod.TCIM.Zhuyin",
|
||||
"com.apple.inputmethod.Korean.2SetKorean",
|
||||
"",
|
||||
] {
|
||||
assert!(!japanese_input_source(id), "{id}");
|
||||
}
|
||||
}
|
||||
|
||||
/// Space in a Japanese input mode is the ideographic space, which the window layer reports as a
|
||||
/// plain one; with Shift, in the Roman mode, in a keyboard layout, with a Chinese or Korean input
|
||||
/// method or with no source known it stays plain. Shift counts as held at the space, even when it
|
||||
/// comes up later in the same frame.
|
||||
#[test]
|
||||
fn space_follows_the_selected_input_source() {
|
||||
let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());
|
||||
let ctx = egui::Context::default();
|
||||
let sid = typing_app(&mut app, &ctx, "");
|
||||
let space = egui::Event::Text(" ".into());
|
||||
let source = |app: &mut DesignApp, id: Option<&str>, events: Vec<egui::Event>| {
|
||||
app.set_input_source(id.map(str::to_owned));
|
||||
ime_frame(app, &ctx, events);
|
||||
};
|
||||
let japanese = Some("com.apple.inputmethod.Kotoeri.RomajiTyping.Japanese");
|
||||
source(&mut app, japanese, vec![space.clone()]);
|
||||
let (shift, up) = (egui::Event::ModifiersChanged(egui::Modifiers::SHIFT), egui::Event::ModifiersChanged(egui::Modifiers::NONE));
|
||||
source(&mut app, japanese, vec![shift, space.clone(), up]);
|
||||
source(&mut app, Some("com.apple.inputmethod.Kotoeri.RomajiTyping.Roman"), vec![space.clone()]);
|
||||
source(&mut app, Some("com.apple.keylayout.ABC"), vec![space.clone()]);
|
||||
source(&mut app, Some("com.apple.inputmethod.Korean.2SetKorean"), vec![space.clone()]);
|
||||
source(&mut app, None, vec![space.clone()]);
|
||||
source(&mut app, japanese, vec![egui::Event::Text("a".into()), space]);
|
||||
assert_eq!(story_text(&mut app, &sid), "\u{3000} a\u{3000}");
|
||||
}
|
||||
|
||||
/// Selected text shows what lies under it inverted: paper turns black, black turns white,
|
||||
/// and a half-covered pixel is half inverted.
|
||||
#[test]
|
||||
|
||||
@@ -309,6 +309,10 @@ pub struct DesignApp {
|
||||
/// The marked text the system IME last sent (`None` once it commits or clears). When the
|
||||
/// composition ends some other way (a click, a tool switch), the IME is told to drop it.
|
||||
pub(crate) ime_marked: Option<String>,
|
||||
/// The keyboard input source the system has selected, as the host last read it (macOS: its
|
||||
/// ID, such as `com.apple.inputmethod.Kotoeri.RomajiTyping.Japanese`). `None` where the host
|
||||
/// can't tell: see [`Self::set_input_source`].
|
||||
pub(crate) input_source: Option<String>,
|
||||
/// The system IME must drop its marked text (see [`Self::take_ime_discard`]).
|
||||
pub(crate) ime_discard: bool,
|
||||
/// The canvas asked for the system IME in the last frame (the web build's text input has the
|
||||
|
||||
Reference in new issue
Block a user