fn insert_text_inner()

in src/platform_impl/macos/view.rs [509:557]


fn insert_text_inner(this: &Object, string: *mut Object) {
    unsafe {
        let state_ptr: *mut c_void = *this.get_ivar("winitState");
        let state = &mut *(state_ptr as *mut ViewState);

        let has_attr = msg_send![string, isKindOfClass: class!(NSAttributedString)];
        let characters = if has_attr {
            // This is a *mut NSAttributedString
            msg_send![string, string]
        } else {
            // This is already a *mut NSString
            string
        };

        let slice =
            slice::from_raw_parts(characters.UTF8String() as *const c_uchar, characters.len());
        let string = str::from_utf8_unchecked(slice);
        let event: id = msg_send![NSApp(), currentEvent];
        let marked_text: id = *this.get_ivar("markedText");
        let trim: usize = marked_text.length() as usize;
        let mut events = VecDeque::with_capacity(characters.len() + trim as usize);
        #[allow(deprecated)]
        for _ in 0..trim {
            // it's better to send marked text as a special event so the client will be able to
            // highlight the symbol and handle next insert with removal
            // instead we send it as a regular keyboard input and just trim it here
            events.push_back(EventWrapper::StaticEvent(Event::WindowEvent {
                window_id: WindowId(get_window_id(state.ns_window)),
                event: WindowEvent::KeyboardInput {
                    device_id: DEVICE_ID,
                    input: KeyboardInput {
                        state: ElementState::Pressed,
                        scancode: 0x33,
                        virtual_keycode: Some(VirtualKeyCode::Back),
                        modifiers: event_mods(event),
                    },
                    is_synthetic: true,
                },
            }));
        }
        for character in string.chars().filter(|c| !is_corporate_character(*c)) {
            events.push_back(EventWrapper::StaticEvent(Event::WindowEvent {
                window_id: WindowId(get_window_id(state.ns_window)),
                event: WindowEvent::ReceivedCharacter(character),
            }));
        }
        AppState::queue_events(events);
    }
}