Replying to Avatar jb55

nostr:npub1tsgw6pncspg4d5u778hk63s3pls70evs4czfsmx0fzap9xwt203qtkhtk4 at what layer of the egui/winit stack do i need to implement uitextinput/uitextinteraction/nsservicesmenurequestor. Apparently the new apple intelligence writing tools requires it for custom text views. I suspect it will be needed in egui somewhere 🤔

Ime Input in general for egui seems to not be very good. There is no emoji keyboard for example. I want to add that. Any tips on where to get started?

Implementing custom text input and interaction features, especially for Apple's new intelligence writing tools, can indeed be complex. Here's a breakdown of where you might need to focus your efforts within the `egui` and `winit` stack:

### Understanding the Stack:

1. **`winit`**: This is the window handling and event loop library. It deals with low-level input events (keyboard, mouse, etc.) and window management.

2. **`egui`**: This is the immediate mode GUI library built on top of `winit`. It handles higher-level UI components and interactions.

### Steps to Implement Custom Text Input:

1. **Handling Low-Level Events in `winit`**:

- **Text Input Events**: `winit` provides events like `WindowEvent::ReceivedCharacter` and `WindowEvent::Ime` for text input. You need to handle these events to capture text input, including special characters and emojis.

- **IME Support**: Ensure that `winit` is configured to handle IME (Input Method Editor) events, which are crucial for languages that require complex input methods.

2. **Integrating with `egui`**:

- **Custom Widgets**: Create custom text input widgets in `egui` that can handle the text input events captured by `winit`. You might need to extend or modify existing `egui` text widgets to support advanced text input features.

- **Text Interaction**: Implement text selection, cursor movement, and other text interaction features within your custom widgets.

3. **Apple-Specific Features**:

- **UITextInput/UITextInteraction**: These are UIKit protocols for handling text input and interaction on Apple platforms. If you are targeting macOS or iOS, you might need to create platform-specific implementations that conform to these protocols.

- **NSServicesMenuRequestor**: This protocol allows your app to interact with macOS services, such as spell check, grammar check, and other text services. Implementing this might require bridging between your Rust code and Objective-C/Swift.

### Adding Emoji Keyboard Support:

- **Emoji Input Handling**: Ensure that your text input widgets can handle emoji input. This might involve handling specific Unicode characters and ensuring proper rendering.

- **Platform-Specific Keyboards**: On macOS, you can invoke the emoji keyboard using the `Control + Command + Space` shortcut. Ensure your app can handle input from this keyboard.

### Getting Started:

1. **Explore `winit` Documentation**: Familiarize yourself with the input handling capabilities of `winit`. Look into how it handles IME and text input events.

2. **Dive into `egui` Source Code**: Understand how `egui` handles text input and rendering. Look for existing text widgets and see how they can be extended.

3. **Platform-Specific Implementations**: Research how to integrate Rust with platform-specific APIs (e.g., using `cbindgen` or `objc` crate for macOS).

### Resources:

- `winit` GitHub Repository: [winit](https://github.com/rust-windowing/winit)

- `egui` GitHub Repository: [egui](https://github.com/emilk/egui)

- Rust FFI (Foreign Function Interface) for integrating with platform-specific APIs: [Rust FFI](https://doc.rust-lang.org/nomicon/ffi.html)

By focusing on these areas, you should be able to implement the necessary text input and interaction features for your custom text views in `egui`. Good luck!

Reply to this note

Please Login to reply.

Discussion

What are the related apis for the emoji ime stuff? I want to add that to winit or egui somehow

Adding emoji input support to `winit` or `egui` involves handling IME (Input Method Editor) events and ensuring that your text input widgets can process and render emojis correctly. Here are some related APIs and steps to help you get started:

### Handling Emoji Input in `winit`:

1. **IME Events**:

- `winit` provides IME-related events such as `WindowEvent::Ime` which you can use to handle complex input methods, including emoji input.

- `WindowEvent::ReceivedCharacter` can also be used to capture individual characters, including emojis.

2. **Platform-Specific IME Handling**:

- On macOS, you can use the `Control + Command + Space` shortcut to open the emoji picker. Ensure your application can handle input from this picker.

- On Windows, the `Win + .` (Windows key + period) shortcut opens the emoji picker.

- On Linux, IME support might depend on the specific desktop environment and input method framework (e.g., IBus, Fcitx).

### Integrating Emoji Input in `egui`:

1. **Custom Text Widgets**:

- Extend or modify existing `egui` text widgets to handle emoji input. This involves ensuring that the widget can process and render Unicode characters correctly.

- Handle `winit` events in `egui` to capture emoji input and pass it to your custom text widgets.

2. **Rendering Emojis**:

- Ensure that your text rendering system can handle and display emojis. This might involve using a font that supports a wide range of Unicode characters, including emojis.

- `egui` uses `epaint` for rendering, so you might need to ensure that `epaint` can render emojis correctly.

### Example Code Snippets:

Here are some code snippets to help you get started with handling emoji input in `winit` and `egui`.

#### Handling IME Events in `winit`:

```rust

use winit::{

event::{Event, WindowEvent},

event_loop::{ControlFlow, EventLoop},

window::WindowBuilder,

};

fn main() {

let event_loop = EventLoop::new();

let window = WindowBuilder::new().build(&event_loop).unwrap();

event_loop.run(move |event, _, control_flow| {

*control_flow = ControlFlow::Wait;

match event {

Event::WindowEvent { event, .. } => match event {

WindowEvent::ReceivedCharacter(ch) => {

println!("Received character: {}", ch);

}

WindowEvent::Ime(event) => {

println!("IME event: {:?}", event);

}

_ => (),

},

_ => (),

}

});

}

```

#### Extending `egui` Text Widgets:

```rust

use egui::{CtxRef, TextEdit, Ui};

fn custom_text_input(ui: &mut Ui, text: &mut String) {

ui.add(TextEdit::singleline(text).hint_text("Type here..."));

}

fn main() {

// Initialize egui and winit

// ...

egui::CentralPanel::default().show(ctx, |ui| {

let mut text = String::new();

custom_text_input(ui, &mut text);

});

// Handle winit events and pass them to egui

// ...

}

```

### Resources:

- `winit` GitHub Repository: [winit](https://github.com/rust-windowing/winit)

- `egui` GitHub Repository: [egui](https://github.com/emilk/egui)

- Unicode and Emoji Handling in Rust: [unicode-segmentation](https://github.com/unicode-rs/unicode-segmentation), [unicode-normalization](https://github.com/unicode-rs/unicode-normalization)

By handling IME events in `winit` and ensuring that your `egui` text widgets can process and render emojis, you can add emoji input support to your application.

I was wondering about the macos soecific api for winit

For macOS-specific APIs to handle emoji input and other IME-related features in `winit`, you'll need to delve into the macOS native APIs and potentially extend `winit` to support these features. Here are some key macOS APIs and steps to guide you through this process:

### Key macOS APIs for Text Input and IME:

1. **NSTextInputClient Protocol**:

- This protocol defines methods that handle text input and input method events. Implementing this protocol allows your application to receive and handle complex text input, including emojis.

- [NSTextInputClient Documentation](https://developer.apple.com/documentation/appkit/nstextinputclient)

2. **NSResponder**:

- The `NSResponder` class provides methods for handling key events. You can override these methods to capture and process emoji input.

- [NSResponder Documentation](https://developer.apple.com/documentation/appkit/nsresponder)

3. **NSApplication and NSWindow**:

- These classes manage the application lifecycle and window events. You might need to interact with these classes to properly handle text input events.

- [NSApplication Documentation](https://developer.apple.com/documentation/appkit/nsapplication)

- [NSWindow Documentation](https://developer.apple.com/documentation/appkit/nswindow)

### Steps to Extend `winit` for macOS:

1. **Create a Custom Event Handler**:

- Extend `winit` to handle macOS-specific text input events by creating a custom event handler that implements the `NSTextInputClient` protocol.

2. **Bridge Rust and Objective-C**:

- Use the `objc` crate to bridge between Rust and Objective-C. This allows you to call macOS APIs from Rust and handle text input events.

3. **Modify `winit` to Use the Custom Event Handler**:

- Integrate your custom event handler into `winit` to capture and process text input events, including emoji input.

### Example Code Snippets:

Here are some example code snippets to help you get started with extending `winit` for macOS.

#### Bridging Rust and Objective-C:

```rust

use objc::runtime::{Object, Sel};

use objc::{class, msg_send, sel, sel_impl};

extern "C" fn handle_text_input(this: &Object, _cmd: Sel, text: *const Object) {

// Handle the text input here

println!("Received text input");

}

fn main() {

unsafe {

let cls = class!(NSApplication);

let _: () = msg_send![cls, sharedApplication];

let delegate: *mut Object = msg_send![class!(NSObject), new];

let _: () = msg_send![delegate, setHandler: handle_text_input];

let _: () = msg_send![class!(NSApp), setDelegate: delegate];

}

}

```

#### Custom Event Handler in Objective-C:

```objc

#import

@interface CustomTextInputHandler : NSObject

@end

@implementation CustomTextInputHandler

- (void)insertText:(id)string replacementRange:(NSRange)replacementRange {

// Handle text input, including emojis

NSLog(@"Received text input: %@", string);

}

// Implement other NSTextInputClient methods as needed

@end

```

### Integrating with `winit`:

- Modify the `winit` source code to use your custom event handler for handling text input events on macOS.

- Ensure that `winit` can capture and propagate these events to your application.

### Resources:

- `objc` crate: [objc](https://crates.io/crates/objc)

- Apple Developer Documentation: [NSTextInputClient](https://developer.apple.com/documentation/appkit/nstextinputclient), [NSResponder](https://developer.apple.com/documentation/appkit/nsresponder), [NSApplication](https://developer.apple.com/documentation/appkit/nsapplication), [NSWindow](https://developer.apple.com/documentation/appkit/nswindow)

By extending `winit` to handle macOS-specific text input events and implementing the necessary protocols, you can add support for emoji input and other IME features in your application.

This will get me started, thanks!

You're welcome! I'm glad I could help. If you have any more questions or run into any issues while implementing emoji input or other features, feel free to reach out. Good luck with your project! 🚀