forge
Explore
Log in

Don't you dare go Hollow

Unconstrain InputMethod's PopupSurface

Browse files at this commit

279659ac90eef4082157f427f006c9a4c5a02ac2Kirill ChibisovParent: c2d03d8
Make IME popup to be visible inside the parent and not obscure the
text input rectangle region.

Fixes https://github.com/YaLTeR/niri/issues/221
Cargo.lockmodified+31 -11
@@ -889,13 +889,13 @@ checksum = "9ea835d29036a4087793836fa931b08837ad5e957da9e23886b29586fb9b6650"
889889
890890 [[package]]
891891 name = "drm"
892-version = "0.11.1"
892+version = "0.12.0"
893893 source = "registry+https://github.com/rust-lang/crates.io-index"
894-checksum = "a0f8a69e60d75ae7dab4ef26a59ca99f2a89d4c142089b537775ae0c198bdcde"
894+checksum = "98888c4bbd601524c11a7ed63f814b8825f420514f78e96f752c437ae9cbb5d1"
895895 dependencies = [
896896 "bitflags 2.5.0",
897897 "bytemuck",
898- "drm-ffi",
898+ "drm-ffi 0.8.0",
899899 "drm-fourcc",
900900 "rustix 0.38.32",
901901 ]
@@ -906,7 +906,17 @@ version = "0.7.1"
906906 source = "registry+https://github.com/rust-lang/crates.io-index"
907907 checksum = "41334f8405792483e32ad05fbb9c5680ff4e84491883d2947a4757dc54cb2ac6"
908908 dependencies = [
909- "drm-sys",
909+ "drm-sys 0.6.1",
910+ "rustix 0.38.32",
911+]
912+
913+[[package]]
914+name = "drm-ffi"
915+version = "0.8.0"
916+source = "registry+https://github.com/rust-lang/crates.io-index"
917+checksum = "97c98727e48b7ccb4f4aea8cfe881e5b07f702d17b7875991881b41af7278d53"
918+dependencies = [
919+ "drm-sys 0.7.0",
910920 "rustix 0.38.32",
911921 ]
912922
@@ -926,6 +936,16 @@ dependencies = [
926936 "linux-raw-sys 0.6.4",
927937 ]
928938
939+[[package]]
940+name = "drm-sys"
941+version = "0.7.0"
942+source = "registry+https://github.com/rust-lang/crates.io-index"
943+checksum = "fd39dde40b6e196c2e8763f23d119ddb1a8714534bf7d77fa97a65b0feda3986"
944+dependencies = [
945+ "libc",
946+ "linux-raw-sys 0.6.4",
947+]
948+
929949 [[package]]
930950 name = "edid-rs"
931951 version = "0.1.0"
@@ -1239,11 +1259,11 @@ dependencies = [
12391259
12401260 [[package]]
12411261 name = "gbm"
1242-version = "0.14.2"
1262+version = "0.15.0"
12431263 source = "registry+https://github.com/rust-lang/crates.io-index"
1244-checksum = "313702b30cdeb83ddc72bc14dcee67803cd0ae2d12282ea06e368c25a900c844"
1264+checksum = "45bf55ba6dd53ad0ac115046ff999c5324c283444ee6e0be82454c4e8eb2f36a"
12451265 dependencies = [
1246- "bitflags 1.3.2",
1266+ "bitflags 2.5.0",
12471267 "drm",
12481268 "drm-fourcc",
12491269 "gbm-sys",
@@ -2149,7 +2169,7 @@ dependencies = [
21492169 "calloop 0.13.0",
21502170 "clap",
21512171 "directories",
2152- "drm-ffi",
2172+ "drm-ffi 0.7.1",
21532173 "futures-util",
21542174 "git-version",
21552175 "glam",
@@ -3116,7 +3136,7 @@ checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67"
31163136 [[package]]
31173137 name = "smithay"
31183138 version = "0.3.0"
3119-source = "git+https://github.com/Smithay/smithay.git#c5e9a697e41f50dc56b918d9ef1e3d2e52c84ac0"
3139+source = "git+https://github.com/Smithay/smithay.git#40acc58b83400aa10fc62a51a235d1991ab46be5"
31203140 dependencies = [
31213141 "appendlist",
31223142 "bitflags 2.5.0",
@@ -3126,7 +3146,7 @@ dependencies = [
31263146 "cursor-icon",
31273147 "downcast-rs",
31283148 "drm",
3129- "drm-ffi",
3149+ "drm-ffi 0.8.0",
31303150 "drm-fourcc",
31313151 "errno",
31323152 "gbm",
@@ -3188,7 +3208,7 @@ dependencies = [
31883208 [[package]]
31893209 name = "smithay-drm-extras"
31903210 version = "0.1.0"
3191-source = "git+https://github.com/Smithay/smithay.git#c5e9a697e41f50dc56b918d9ef1e3d2e52c84ac0"
3211+source = "git+https://github.com/Smithay/smithay.git#40acc58b83400aa10fc62a51a235d1991ab46be5"
31923212 dependencies = [
31933213 "drm",
31943214 "edid-rs",
src/handlers/mod.rsmodified+11 -2
@@ -133,25 +133,34 @@ delegate_pointer_constraints!(State);
133133
134134 impl InputMethodHandler for State {
135135 fn new_popup(&mut self, surface: PopupSurface) {
136- let popup = PopupKind::from(surface.clone());
136+ let popup = PopupKind::InputMethod(surface);
137137 if let Some(output) = self.output_for_popup(&popup) {
138138 let scale = output.current_scale().integer_scale();
139139 let transform = output.current_transform();
140- let wl_surface = surface.wl_surface();
140+ let wl_surface = popup.wl_surface();
141141 with_states(wl_surface, |data| {
142142 send_surface_state(wl_surface, data, scale, transform);
143143 });
144144 }
145145
146+ self.unconstrain_popup(&popup);
147+
146148 if let Err(err) = self.niri.popups.track_popup(popup) {
147149 warn!("error tracking ime popup {err:?}");
148150 }
149151 }
152+
153+ fn popup_repositioned(&mut self, surface: PopupSurface) {
154+ let popup = PopupKind::InputMethod(surface);
155+ self.unconstrain_popup(&popup);
156+ }
157+
150158 fn dismiss_popup(&mut self, surface: PopupSurface) {
151159 if let Some(parent) = surface.get_parent().map(|parent| parent.surface.clone()) {
152160 let _ = PopupManager::dismiss_popup(&parent, &PopupKind::from(surface));
153161 }
154162 }
163+
155164 fn parent_geometry(&self, parent: &WlSurface) -> Rectangle<i32, Logical> {
156165 self.niri
157166 .layout
src/handlers/xdg_shell.rsmodified+44 -16
@@ -1,5 +1,5 @@
11 use smithay::desktop::{
2- find_popup_root_surface, get_popup_toplevel_coords, layer_map_for_output, LayerSurface,
2+ find_popup_root_surface, get_popup_toplevel_coords, layer_map_for_output, utils, LayerSurface,
33 PopupKeyboardGrab, PopupKind, PopupManager, PopupPointerGrab, PopupUngrabStrategy, Window,
44 WindowSurfaceType,
55 };
@@ -177,7 +177,7 @@ impl XdgShellHandler for State {
177177
178178 trace!("new grab for root {:?}", root);
179179 keyboard.set_focus(self, grab.current_grab(), serial);
180- keyboard.set_grab(PopupKeyboardGrab::new(&grab), serial);
180+ keyboard.set_grab(self, PopupKeyboardGrab::new(&grab), serial);
181181 pointer.set_grab(self, PopupPointerGrab::new(&grab), serial, Focus::Keep);
182182 self.niri.popup_grab = Some(PopupGrabState { root, grab });
183183 }
@@ -649,8 +649,11 @@ impl State {
649649 popup.send_configure().expect("initial configure failed");
650650 }
651651 }
652- // Input method popups don't require a configure.
653- PopupKind::InputMethod(_) => (),
652+ // Input method popup can arbitrary change its geometry, so we need to unconstraint
653+ // it on commit.
654+ PopupKind::InputMethod(_) => {
655+ self.unconstrain_popup(&popup);
656+ }
654657 }
655658 }
656659 }
@@ -696,14 +699,7 @@ impl State {
696699 target.loc.y -= self.niri.layout.window_y(window).unwrap();
697700 target.loc -= get_popup_toplevel_coords(popup);
698701
699- match popup {
700- PopupKind::Xdg(popup) => {
701- popup.with_pending_state(|state| {
702- state.geometry = unconstrain_with_padding(state.positioner, target);
703- });
704- }
705- PopupKind::InputMethod(_) => todo!(),
706- }
702+ self.position_popup_within_rect(popup, target);
707703 }
708704
709705 pub fn unconstrain_layer_shell_popup(
@@ -724,11 +720,43 @@ impl State {
724720 target.loc -= layer_geo.loc;
725721 target.loc -= get_popup_toplevel_coords(popup);
726722
723+ self.position_popup_within_rect(popup, target);
724+ }
725+
726+ fn position_popup_within_rect(&self, popup: &PopupKind, target: Rectangle<i32, Logical>) {
727727 match popup {
728- PopupKind::Xdg(popup) => popup.with_pending_state(|state| {
729- state.geometry = unconstrain_with_padding(state.positioner, target);
730- }),
731- PopupKind::InputMethod(_) => todo!(),
728+ PopupKind::Xdg(popup) => {
729+ popup.with_pending_state(|state| {
730+ state.geometry = unconstrain_with_padding(state.positioner, target);
731+ });
732+ }
733+ PopupKind::InputMethod(popup) => {
734+ let text_input_rectangle = popup.text_input_rectangle();
735+ let mut bbox =
736+ utils::bbox_from_surface_tree(popup.wl_surface(), text_input_rectangle.loc);
737+
738+ // Position bbox horizontally first.
739+ let overflow_x = (bbox.loc.x + bbox.size.w) - (target.loc.x + target.size.w);
740+ if overflow_x > 0 {
741+ bbox.loc.x -= overflow_x;
742+ }
743+
744+ // Ensure that the popup starts within the window.
745+ bbox.loc.x = bbox.loc.x.max(target.loc.x);
746+
747+ // Try to position IME popup below the text input rectangle.
748+ let mut below = bbox;
749+ below.loc.y += text_input_rectangle.size.h;
750+
751+ let mut above = bbox;
752+ above.loc.y -= bbox.size.h;
753+
754+ if target.loc.y + target.size.h >= below.loc.y + below.size.h {
755+ popup.set_location(below.loc);
756+ } else {
757+ popup.set_location(above.loc);
758+ }
759+ }
732760 }
733761 }
734762
src/niri.rsmodified+1 -1
@@ -768,7 +768,7 @@ impl State {
768768 );
769769
770770 grab.grab.ungrab(PopupUngrabStrategy::All);
771- keyboard.unset_grab();
771+ keyboard.unset_grab(self);
772772 self.niri.seat.get_pointer().unwrap().unset_grab(
773773 self,
774774 SERIAL_COUNTER.next_serial(),