Skip to content

Commit

Permalink
Handle _selected_range sent to NSTextInputClient.setMarkedText().
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit b6ce256
Merge: 4f36636 4cd6877
Author: ShikiSuen <shikisuen@outlook.com>
Date:   Wed Apr 3 20:49:25 2024 +0800

    Merge branch 'master' into shiki/fix_set_marked_text

commit 4f36636
Author: ShikiSuen <shikisuen@outlook.com>
Date:   Wed Apr 3 20:15:07 2024 +0800

    Amend a831406, fixing the sanity check conditions.

commit 534db85
Author: ShikiSuen <shikisuen@outlook.com>
Date:   Wed Apr 3 20:02:15 2024 +0800

    Amend 40a4d13.

commit 40a4d13
Author: ShikiSuen <shikisuen@outlook.com>
Date:   Wed Apr 3 19:59:38 2024 +0800

    Refine the documentation for get_actual_range_bounds().

commit 0bf4f67
Author: ShikiSuen <shikisuen@outlook.com>
Date:   Wed Apr 3 19:51:26 2024 +0800

    PlatformImpl/macOS/View// Remove an unnecessary unsafe wrapper.

commit 1c45089
Author: ShikiSuen <shikisuen@outlook.com>
Date:   Wed Apr 3 19:48:18 2024 +0800

    Clang-format.

commit bd63218
Author: ShikiSuen <shikisuen@outlook.com>
Date:   Wed Apr 3 19:47:58 2024 +0800

    PlatformImpl/macOS/View// Fix the wrecked unsafe wrapper for an if-let.

commit 899d868
Author: ShikiSuen <shikisuen@outlook.com>
Date:   Wed Apr 3 19:34:16 2024 +0800

    Update src/platform_impl/macos/view.rs

    Co-authored-by: pan93412 <pan93412@gmail.com>

commit 2824f84
Author: ShikiSuen <shikisuen@outlook.com>
Date:   Wed Apr 3 19:33:49 2024 +0800

    Update src/platform_impl/macos/view.rs

    Co-authored-by: pan93412 <pan93412@gmail.com>

commit d14309e
Author: ShikiSuen <shikisuen@outlook.com>
Date:   Wed Apr 3 19:33:25 2024 +0800

    Update src/platform_impl/macos/view.rs

    Co-authored-by: pan93412 <pan93412@gmail.com>

commit af55e74
Author: ShikiSuen <shikisuen@outlook.com>
Date:   Wed Apr 3 19:33:08 2024 +0800

    Update src/platform_impl/macos/view.rs

    Co-authored-by: pan93412 <pan93412@gmail.com>

commit a831406
Author: ShikiSuen <shikisuen@outlook.com>
Date:   Wed Apr 3 19:32:45 2024 +0800

    Update src/platform_impl/macos/view.rs

    Co-authored-by: pan93412 <pan93412@gmail.com>

commit f24b209
Author: ShikiSuen <shikisuen@outlook.com>
Date:   Wed Apr 3 19:31:20 2024 +0800

    Update src/platform_impl/macos/view.rs

    Co-authored-by: Mads Marquart <mads@marquart.dk>

commit e5b4ed4
Author: ShikiSuen <shikisuen@outlook.com>
Date:   Tue Apr 2 09:25:20 2024 +0800

    Fix cargo build error in 0ae86de.

commit 0ae86de
Author: ShikiSuen <shikisuen@outlook.com>
Date:   Tue Apr 2 09:13:35 2024 +0800

    PlatformImpl/macOS/View// Remove unnecessary `return` statement.

commit 330b44a
Author: ShikiSuen <shikisuen@outlook.com>
Date:   Tue Apr 2 02:41:47 2024 +0800

    PlatformImpl/macOS/View// +Handle selected_range.

commit cf3288e
Author: ShikiSuen <shikisuen@outlook.com>
Date:   Tue Apr 2 02:12:37 2024 +0800

    PlatformImpl/macOS/View// +get_actual_range_bounds().

commit 058b8d1
Author: ShikiSuen <shikisuen@outlook.com>
Date:   Tue Apr 2 02:13:01 2024 +0800

    PlatformImpl/macOS/View// Add some TODO memos.

    - TODO: We need some Nihonjin IME devs' help regarding how to use _replacement_range correctly. The "_replacement_range" is at least used in Japanese IMEs. Therefore, Nihonjin IME devs are more familiar with Japanese IMEs on mac.
  • Loading branch information
ShikiSuen committed Apr 3, 2024
1 parent 4cd6877 commit ed8c901
Showing 1 changed file with 47 additions and 2 deletions.
49 changes: 47 additions & 2 deletions src/platform_impl/macos/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,10 @@ declare_class!(
fn set_marked_text(
&self,
string: &NSObject,
_selected_range: NSRange,
selected_range: NSRange,
_replacement_range: NSRange,
) {
// TODO: Use `_replacement_range` correctly (known to be used by Japanese IMEs).
trace_scope!("setMarkedText:selectedRange:replacementRange:");

// SAFETY: This method is guaranteed to get either a `NSString` or a `NSAttributedString`.
Expand Down Expand Up @@ -315,10 +316,20 @@ declare_class!(

// Empty string basically means that there's no preedit, so indicate that by sending
// `None` cursor range.
let mut lowerbound: usize = preedit_string.len();
let mut upperbound: usize = lowerbound;
if !preedit_string.is_empty() {
let new_bounds = get_actual_range_bounds(&preedit_string, selected_range);
if let Some(fetched_value) = new_bounds {
lowerbound = fetched_value.0;
upperbound = fetched_value.1;
}
}

let cursor_range = if preedit_string.is_empty() {
None
} else {
Some((preedit_string.len(), preedit_string.len()))
Some((lowerbound, upperbound))
};

// Send WindowEvent for updating marked text
Expand Down Expand Up @@ -378,6 +389,9 @@ declare_class!(

#[method(insertText:replacementRange:)]
fn insert_text(&self, string: &NSObject, _replacement_range: NSRange) {
// TODO: We need some Nihonjin IME devs' help regarding how to use _replacement_range correctly.
// The "_replacement_range" is at least used in Japanese IMEs.
// Therefore, Nihonjin IME devs are more familiar with Japanese IMEs on mac.
trace_scope!("insertText:replacementRange:");

// SAFETY: This method is guaranteed to get either a `NSString` or a `NSAttributedString`.
Expand Down Expand Up @@ -1126,3 +1140,34 @@ fn replace_event(event: &NSEvent, option_as_alt: OptionAsAlt) -> Id<NSEvent> {
event.copy()
}
}

// Convert given NSRange to Rust String Range data used in this crate.
// Steps:
// 1. Use the given preedit text to initialize an NSString instance.
// This avoids handling unsafe ObjC APIs for retrieving the NSString
// from an NSAttributedString.
// Let experts rewrite this code if any of them wants to handle it like that.
// 2. Use the given NSRange's bounds as new upperbounds for two new NSRanges beginning with 0.
// 3. Use the two new NSRange to slice the NSString instance mentioned above.
// 4. Use the two new sliced NSStrings' rust len() as the returned calculated utf8 bounds.
// 5. Return nil if the sanity check fails.
fn get_actual_range_bounds(source_str: &str, range: NSRange) -> Option<(usize, usize)> {
// The given NSRange length may be 0 to indicate a cursor in lieu of a marked range.
let source_ns = NSString::from_str(source_str);
let source_bounds = NSRange::new(0, source_ns.length());
let lowerbound_u16: usize = range.location;
let upperbound_u16: usize = range.length + lowerbound_u16;
// Sanity Check.
if !(0..source_bounds.length).contains(&lowerbound_u16)
|| !(lowerbound_u16..source_bounds.length).contains(&upperbound_u16)
{
return None;
}
let u16_range_from_zero_a = NSRange::new(0, lowerbound_u16);
let u16_range_from_zero_b = NSRange::new(0, upperbound_u16);
let sub_string_a = unsafe { source_ns.substringWithRange(u16_range_from_zero_a) };
let sub_string_b = unsafe { source_ns.substringWithRange(u16_range_from_zero_b) };
let lowerbound_u8: usize = sub_string_a.len();
let upperbound_u8: usize = sub_string_b.len();
Some((lowerbound_u8, upperbound_u8))
}

0 comments on commit ed8c901

Please sign in to comment.