Add new Ime event for desktop platforms

This commit brings new Ime event to account for preedit state of input
method, also adding `Window::set_ime_allowed` to toggle IME input on
the particular window.

This commit implements API as designed in #1497 for desktop platforms.

Co-authored-by: Artur Kovacs <kovacs.artur.barnabas@gmail.com>
Co-authored-by: Markus Siglreithmaier <m.siglreith@gmail.com>
Co-authored-by: Murarth <murarth@gmail.com>
Co-authored-by: Yusuke Kominami <yukke.konan@gmail.com>
Co-authored-by: moko256 <koutaro.mo@gmail.com>
This commit is contained in:
Kirill Chibisov 2022-05-07 05:29:25 +03:00 committed by GitHub
parent b4175c1454
commit f04fa5d54f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
30 changed files with 1346 additions and 311 deletions

View file

@ -4,6 +4,7 @@ mod cursor;
pub use self::{cursor::*, r#async::*};
use std::ops::{BitAnd, Deref};
use std::os::raw::c_uchar;
use cocoa::{
appkit::{NSApp, NSWindowStyleMask},
@ -11,7 +12,7 @@ use cocoa::{
foundation::{NSPoint, NSRect, NSString, NSUInteger},
};
use core_graphics::display::CGDisplay;
use objc::runtime::{Class, Object};
use objc::runtime::{Class, Object, BOOL, NO};
use crate::dpi::LogicalPosition;
use crate::platform_impl::platform::ffi;
@ -165,3 +166,21 @@ pub unsafe fn toggle_style_mask(window: id, view: id, mask: NSWindowStyleMask, o
// If we don't do this, key handling will break. Therefore, never call `setStyleMask` directly!
window.makeFirstResponder_(view);
}
/// For invalid utf8 sequences potentially returned by `UTF8String`,
/// it behaves identically to `String::from_utf8_lossy`
///
/// Safety: Assumes that `string` is an instance of `NSAttributedString` or `NSString`
pub unsafe fn id_to_string_lossy(string: id) -> String {
let has_attr: BOOL = msg_send![string, isKindOfClass: class!(NSAttributedString)];
let characters = if has_attr != NO {
// This is a *mut NSAttributedString
msg_send![string, string]
} else {
// This is already a *mut NSString
string
};
let utf8_sequence =
std::slice::from_raw_parts(characters.UTF8String() as *const c_uchar, characters.len());
String::from_utf8_lossy(utf8_sequence).into_owned()
}