Skip to content

Commit

Permalink
hook ime events
Browse files Browse the repository at this point in the history
  • Loading branch information
dzhou121 committed Aug 30, 2023
1 parent 19bb4bc commit 5382365
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 12 deletions.
10 changes: 9 additions & 1 deletion src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{
};

use floem_reactive::Scope;
use kurbo::{Point, Vec2};
use kurbo::{Point, Size, Vec2};

use crate::{
app::{add_app_update_event, AppUpdateEvent},
Expand Down Expand Up @@ -130,3 +130,11 @@ pub fn set_window_menu(menu: Menu) {
pub fn set_window_title(title: String) {
add_update_message(UpdateMessage::SetWindowTitle { title });
}

pub fn set_ime_allowed(allowed: bool) {
add_update_message(UpdateMessage::SetImeAllowed { allowed });
}

pub fn set_ime_cursor_area(position: Point, size: Size) {
add_update_message(UpdateMessage::SetImeCursorArea { position, size });
}
4 changes: 3 additions & 1 deletion src/app_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ impl ApplicationHandle {
WindowEvent::ModifiersChanged(modifiers) => {
window_handle.modifiers = modifiers.state();
}
WindowEvent::Ime(_) => {}
WindowEvent::Ime(ime) => {
window_handle.ime(ime);
}
WindowEvent::CursorMoved { position, .. } => {
let position: LogicalPosition<f64> = position.to_logical(window_handle.scale);
let point = Point::new(position.x, position.y);
Expand Down
39 changes: 39 additions & 0 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ pub enum EventListener {
PointerUp,
PointerEnter,
PointerLeave,
ImeEnabled,
ImeDisabled,
ImePreedit,
ImeCommit,
PointerWheel,
FocusGained,
FocusLost,
Expand All @@ -42,6 +46,13 @@ pub enum Event {
PointerWheel(PointerWheelEvent),
KeyDown(KeyEvent),
KeyUp(KeyEvent),
ImeEnabled,
ImeDisabled,
ImePreedit {
text: String,
cursor: Option<(usize, usize)>,
},
ImeCommit(String),
WindowGotFocus,
WindowLostFocus,
WindowClosed,
Expand All @@ -56,6 +67,10 @@ impl Event {
| Event::PointerUp(_)
| Event::PointerMove(_)
| Event::PointerWheel(_)
| Event::ImeEnabled
| Event::ImeDisabled
| Event::ImePreedit { .. }
| Event::ImeCommit(_)
| Event::WindowClosed
| Event::WindowResized(_)
| Event::WindowMoved(_)
Expand All @@ -73,6 +88,10 @@ impl Event {
| Event::PointerWheel(_) => true,
Event::KeyDown(_)
| Event::KeyUp(_)
| Event::ImeEnabled
| Event::ImeDisabled
| Event::ImePreedit { .. }
| Event::ImeCommit(_)
| Event::WindowClosed
| Event::WindowResized(_)
| Event::WindowMoved(_)
Expand All @@ -99,6 +118,10 @@ impl Event {
Event::PointerDown(_)
| Event::PointerUp(_)
| Event::PointerWheel(_)
| Event::ImeEnabled
| Event::ImeDisabled
| Event::ImePreedit { .. }
| Event::ImeCommit(_)
| Event::KeyDown(_)
| Event::KeyUp(_) => false,
Event::PointerMove(_)
Expand All @@ -119,6 +142,10 @@ impl Event {
Event::PointerWheel(pointer_event) => Some(pointer_event.pos),
Event::KeyDown(_)
| Event::KeyUp(_)
| Event::ImeEnabled
| Event::ImeDisabled
| Event::ImePreedit { .. }
| Event::ImeCommit(_)
| Event::WindowClosed
| Event::WindowResized(_)
| Event::WindowMoved(_)
Expand All @@ -143,6 +170,10 @@ impl Event {
}
Event::KeyDown(_)
| Event::KeyUp(_)
| Event::ImeEnabled
| Event::ImeDisabled
| Event::ImePreedit { .. }
| Event::ImeCommit(_)
| Event::WindowClosed
| Event::WindowResized(_)
| Event::WindowMoved(_)
Expand All @@ -165,6 +196,10 @@ impl Event {
}
Event::KeyDown(_)
| Event::KeyUp(_)
| Event::ImeEnabled
| Event::ImeDisabled
| Event::ImePreedit { .. }
| Event::ImeCommit(_)
| Event::WindowClosed
| Event::WindowResized(_)
| Event::WindowMoved(_)
Expand All @@ -182,6 +217,10 @@ impl Event {
Event::PointerWheel(_) => Some(EventListener::PointerWheel),
Event::KeyDown(_) => Some(EventListener::KeyDown),
Event::KeyUp(_) => Some(EventListener::KeyDown),
Event::ImeEnabled => Some(EventListener::ImeEnabled),
Event::ImeDisabled => Some(EventListener::ImeDisabled),
Event::ImePreedit { .. } => Some(EventListener::ImePreedit),
Event::ImeCommit(_) => Some(EventListener::ImeCommit),
Event::WindowClosed => Some(EventListener::WindowClosed),
Event::WindowResized(_) => Some(EventListener::WindowResized),
Event::WindowMoved(_) => Some(EventListener::WindowMoved),
Expand Down
9 changes: 8 additions & 1 deletion src/update.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{any::Any, cell::RefCell, collections::HashMap};

use kurbo::{Point, Vec2};
use kurbo::{Point, Size, Vec2};

use crate::{
animate::{AnimUpdateMsg, Animation},
Expand Down Expand Up @@ -108,4 +108,11 @@ pub(crate) enum UpdateMessage {
SetWindowTitle {
title: String,
},
SetImeAllowed {
allowed: bool,
},
SetImeCursorArea {
position: Point,
size: Size,
},
}
2 changes: 1 addition & 1 deletion src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ pub trait View {
}

let viewport = cx.viewport.unwrap_or_default();
let window_origin = origin + cx.window_origin.to_vec2() + viewport.origin().to_vec2();
let window_origin = origin + cx.window_origin.to_vec2() - viewport.origin().to_vec2();
cx.window_origin = window_origin;

if let Some(resize) = cx.get_resize_listener(self.id()) {
Expand Down
49 changes: 44 additions & 5 deletions src/window_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use floem_renderer::Renderer;
use kurbo::{Affine, Point, Rect, Size, Vec2};
use winit::{
dpi::{LogicalPosition, LogicalSize},
event::{ElementState, MouseButton, MouseScrollDelta},
event::{ElementState, Ime, MouseButton, MouseScrollDelta},
keyboard::{Key, ModifiersState},
window::CursorIcon,
};
Expand Down Expand Up @@ -43,7 +43,7 @@ pub(crate) struct WindowHandle {
pub(crate) window: Option<Arc<winit::window::Window>>,
/// Reactive Scope for this WindowHandle
scope: Scope,
view: Box<dyn View>,
pub(crate) view: Box<dyn View>,
app_state: AppState,
paint_state: PaintState,
size: Size,
Expand All @@ -58,6 +58,7 @@ impl WindowHandle {
window: winit::window::Window,
view_fn: impl FnOnce(winit::window::WindowId) -> Box<dyn View> + 'static,
) -> Self {
let window = Arc::new(window);
let window_id = window.id();
let id = Id::next();
set_current_view(id);
Expand All @@ -71,7 +72,7 @@ impl WindowHandle {
let size = Size::new(size.width, size.height);
let paint_state = PaintState::new(&window, scale, size);
let mut window_handle = Self {
window: Some(Arc::new(window)),
window: Some(window),
scope: Scope::new(),
view,
app_state: AppState::new(),
Expand Down Expand Up @@ -628,6 +629,25 @@ impl WindowHandle {
window.set_title(&title);
}
}
UpdateMessage::SetImeAllowed { allowed } => {
if let Some(window) = self.window.as_ref() {
window.set_ime_allowed(allowed);
}
}
UpdateMessage::SetImeCursorArea { position, size } => {
if let Some(window) = self.window.as_ref() {
window.set_ime_cursor_area(
winit::dpi::Position::Logical(winit::dpi::LogicalPosition::new(
position.x * self.app_state.scale,
position.y * self.app_state.scale,
)),
winit::dpi::Size::Logical(winit::dpi::LogicalSize::new(
size.width * self.app_state.scale,
size.height * self.app_state.scale,
)),
);
}
}
}
}
}
Expand Down Expand Up @@ -828,7 +848,8 @@ impl WindowHandle {
menu,
pos.map(|pos| {
winit::dpi::Position::Logical(winit::dpi::LogicalPosition::new(
pos.x, pos.y,
pos.x * self.app_state.scale,
pos.y * self.app_state.scale,
))
}),
);
Expand All @@ -846,7 +867,8 @@ impl WindowHandle {
menu,
pos.map(|pos| {
winit::dpi::Position::Logical(winit::dpi::LogicalPosition::new(
pos.x, pos.y,
pos.x * self.app_state.scale,
pos.y * self.app_state.scale,
))
}),
);
Expand All @@ -867,6 +889,23 @@ impl WindowHandle {
self.process_update();
}
}

pub(crate) fn ime(&mut self, ime: Ime) {
match ime {
Ime::Enabled => {
self.event(Event::ImeEnabled);
}
Ime::Preedit(text, cursor) => {
self.event(Event::ImePreedit { text, cursor });
}
Ime::Commit(text) => {
self.event(Event::ImeCommit(text));
}
Ime::Disabled => {
self.event(Event::ImeDisabled);
}
}
}
}

pub(crate) fn get_current_view() -> Id {
Expand Down
10 changes: 7 additions & 3 deletions vger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,18 @@ impl Renderer for VgerRenderer {
None => return,
};
if let Some(rect) = path.as_rect() {
self.vger
.fill_rect(self.vger_rect(rect), 0.0, paint, blur_radius as f32);
self.vger.fill_rect(
self.vger_rect(rect),
0.0,
paint,
(blur_radius * self.scale) as f32,
);
} else if let Some(rect) = path.as_rounded_rect() {
self.vger.fill_rect(
self.vger_rect(rect.rect()),
(rect.radii().top_left * self.scale) as f32,
paint,
blur_radius as f32,
(blur_radius * self.scale) as f32,
);
} else if let Some(circle) = path.as_circle() {
self.vger.fill_circle(
Expand Down

0 comments on commit 5382365

Please sign in to comment.