From b0709a5706a5564f822655b82db5ace4bbc6c2bb Mon Sep 17 00:00:00 2001 From: ShootingStarDragons Date: Wed, 31 Jul 2024 19:56:58 +0859 Subject: [PATCH] chore: finished --- .../counter_virtual_keyboard/src/main.rs | 9 +--- iced_layershell/src/actions.rs | 3 +- iced_layershell/src/application.rs | 41 +++++++++++-------- iced_layershell/src/multi_window.rs | 33 ++++++++++----- layershellev/src/lib.rs | 24 +++++++++++ 5 files changed, 75 insertions(+), 35 deletions(-) diff --git a/iced_examples/counter_virtual_keyboard/src/main.rs b/iced_examples/counter_virtual_keyboard/src/main.rs index 2d35bc3..7cad336 100644 --- a/iced_examples/counter_virtual_keyboard/src/main.rs +++ b/iced_examples/counter_virtual_keyboard/src/main.rs @@ -10,7 +10,7 @@ use iced_layershell::reexport::{Anchor, KeyboardInteractivity}; use iced_layershell::settings::{LayerShellSettings, Settings, VirtualKeyboardSettings}; use iced_layershell::Application; -use iced_layershell::reexport::wl_keyboard::{KeyState, KeymapFormat}; +use iced_layershell::reexport::wl_keyboard::KeymapFormat; use xkbcommon::xkb; pub fn get_keymap_as_file() -> (File, u32) { @@ -117,12 +117,7 @@ impl Application for Counter { Command::none() } Message::InputTest => Command::single( - LayershellCustomActions::VirtualKeyboardPressed { - time: 100, - key: 16, - keystate: KeyState::Pressed, - } - .into(), + LayershellCustomActions::VirtualKeyboardPressed { time: 100, key: 16 }.into(), ), Message::Direction(direction) => match direction { WindowDirection::Left => Command::batch(vec![ diff --git a/iced_layershell/src/actions.rs b/iced_layershell/src/actions.rs index cc8fce4..15560e0 100644 --- a/iced_layershell/src/actions.rs +++ b/iced_layershell/src/actions.rs @@ -2,7 +2,7 @@ use crate::reexport::{Anchor, Layer}; use iced::window::Id as IcedId; use iced_core::mouse::Interaction; use iced_runtime::command::Action; -use layershellev::{id::Id as LayerId, reexport::wayland_client::KeyState}; +use layershellev::id::Id as LayerId; #[allow(unused)] #[derive(Debug, Clone)] pub(crate) enum LayerShellActions { @@ -21,7 +21,6 @@ pub enum LayershellCustomActions { VirtualKeyboardPressed { time: u32, key: u32, - keystate: KeyState, }, } diff --git a/iced_layershell/src/application.rs b/iced_layershell/src/application.rs index f622940..437d222 100644 --- a/iced_layershell/src/application.rs +++ b/iced_layershell/src/application.rs @@ -1,6 +1,6 @@ mod state; -use std::{mem::ManuallyDrop, os::fd::AsFd, sync::Arc}; +use std::{mem::ManuallyDrop, os::fd::AsFd, sync::Arc, time::Duration}; use crate::{ actions::{LayerShellActions, LayershellCustomActions}, @@ -22,7 +22,9 @@ use iced_style::application::StyleSheet; use iced_futures::{Executor, Runtime, Subscription}; use layershellev::{ - reexport::zwp_virtual_keyboard_v1, LayerEvent, ReturnData, WindowState, WindowWrapper, + calloop::timer::{TimeoutAction, Timer}, + reexport::zwp_virtual_keyboard_v1, + LayerEvent, ReturnData, WindowState, WindowWrapper, }; use futures::{channel::mpsc, SinkExt, StreamExt}; @@ -190,7 +192,6 @@ where let mut pointer_serial: u32 = 0; - let mut virtuan_keyboard = None; let _ = ev.running_with_proxy(message_receiver, move |event, ev, _| { use layershellev::DispatchMessage; let mut def_returndata = ReturnData::None; @@ -200,7 +201,6 @@ where def_returndata = ReturnData::RequestBind; } } - // TODO: maybe use it later LayerEvent::BindProvide(globals, qh) => { let virtual_keyboard_manager = globals .bind::( @@ -218,7 +218,7 @@ where let virtual_keyboard_in = virtual_keyboard_manager.create_virtual_keyboard(seat, qh, ()); virtual_keyboard_in.keymap((*keymap_format).into(), file.as_fd(), *keymap_size); - virtuan_keyboard = Some(virtual_keyboard_in); + ev.set_virtual_keyboard(virtual_keyboard_in); } LayerEvent::RequestMessages(message) => { if let DispatchMessage::MouseEnter { serial, .. } = message { @@ -243,7 +243,7 @@ where } let poll = instance.as_mut().poll(&mut context); match poll { - task::Poll::Pending => 'peddingBlock: { + task::Poll::Pending => { if let Ok(Some(flow)) = control_receiver.try_next() { for flow in flow { match flow { @@ -262,33 +262,42 @@ where LayershellCustomActions::VirtualKeyboardPressed { time, key, - keystate, } => { - virtuan_keyboard.as_ref().unwrap().key( - time, - key, - keystate.into(), - ); + use layershellev::reexport::wayland_client::KeyState; + let ky = ev.get_virtual_keyboard().unwrap(); + ky.key(time, key, KeyState::Pressed.into()); + + let eh = ev.get_loop_handler().unwrap(); + eh.insert_source( + Timer::from_duration(Duration::from_micros(100)), + move |_, _, state| { + let ky = state.get_virtual_keyboard().unwrap(); + + ky.key(time, key, KeyState::Released.into()); + TimeoutAction::Drop + }, + ) + .ok(); } } } } LayerShellActions::Mouse(mouse) => { let Some(pointer) = ev.get_pointer() else { - break 'peddingBlock ReturnData::None; + return ReturnData::None; }; - break 'peddingBlock ReturnData::RequestSetCursorShape(( + return ReturnData::RequestSetCursorShape(( conversion::mouse_interaction(mouse), pointer.clone(), pointer_serial, )); } LayerShellActions::RedrawAll => { - break 'peddingBlock ReturnData::RedrawAllRequest; + return ReturnData::RedrawAllRequest; } LayerShellActions::RedrawWindow(index) => { - break 'peddingBlock ReturnData::RedrawIndexRequest(index); + return ReturnData::RedrawIndexRequest(index); } _ => {} } diff --git a/iced_layershell/src/multi_window.rs b/iced_layershell/src/multi_window.rs index c05f698..1b21025 100644 --- a/iced_layershell/src/multi_window.rs +++ b/iced_layershell/src/multi_window.rs @@ -4,7 +4,7 @@ use crate::{ multi_window::window_manager::WindowManager, settings::VirtualKeyboardSettings, }; -use std::{collections::HashMap, f64, mem::ManuallyDrop, os::fd::AsFd, sync::Arc}; +use std::{collections::HashMap, f64, mem::ManuallyDrop, os::fd::AsFd, sync::Arc, time::Duration}; use crate::{ actions::{LayerShellActions, LayershellCustomActions}, @@ -23,7 +23,11 @@ use iced_style::application::StyleSheet; use iced_futures::{Executor, Runtime, Subscription}; -use layershellev::{reexport::zwp_virtual_keyboard_v1, LayerEvent, ReturnData, WindowState}; +use layershellev::{ + calloop::timer::{TimeoutAction, Timer}, + reexport::zwp_virtual_keyboard_v1, + LayerEvent, ReturnData, WindowState, +}; use futures::{channel::mpsc, SinkExt, StreamExt}; @@ -197,7 +201,6 @@ where let mut pointer_serial: u32 = 0; - let mut virtuan_keyboard = None; let _ = ev.running_with_proxy(message_receiver, move |event, ev, index| { use layershellev::DispatchMessage; let mut def_returndata = ReturnData::None; @@ -225,7 +228,7 @@ where let virtual_keyboard_in = virtual_keyboard_manager.create_virtual_keyboard(seat, qh, ()); virtual_keyboard_in.keymap((*keymap_format).into(), file.as_fd(), *keymap_size); - virtuan_keyboard = Some(virtual_keyboard_in); + ev.set_virtual_keyboard(virtual_keyboard_in); } LayerEvent::RequestMessages(message) => 'outside: { match message { @@ -292,17 +295,27 @@ where LayershellCustomActions::VirtualKeyboardPressed { time, key, - keystate, } => { - virtuan_keyboard.as_ref().unwrap().key( - time, - key, - keystate.into(), - ); + use layershellev::reexport::wayland_client::KeyState; + let ky = ev.get_virtual_keyboard().unwrap(); + ky.key(time, key, KeyState::Pressed.into()); + + let eh = ev.get_loop_handler().unwrap(); + eh.insert_source( + Timer::from_duration(Duration::from_micros(100)), + move |_, _, state| { + let ky = state.get_virtual_keyboard().unwrap(); + + ky.key(time, key, KeyState::Released.into()); + TimeoutAction::Drop + }, + ) + .ok(); } } } } + LayerShellActions::Mouse(mouse) => { let Some(pointer) = ev.get_pointer() else { break 'peddingBlock ReturnData::None; diff --git a/layershellev/src/lib.rs b/layershellev/src/lib.rs index 4f30843..ee1df8f 100644 --- a/layershellev/src/lib.rs +++ b/layershellev/src/lib.rs @@ -111,9 +111,12 @@ //! } //! ``` //! +use sctk::reexports::calloop::LoopHandle; pub use waycrate_xkbkeycode::keyboard; pub use waycrate_xkbkeycode::xkb_keyboard; +pub use sctk::reexports::calloop; + mod events; mod strtoshape; @@ -459,6 +462,7 @@ pub struct WindowState { pointer: Option, touch: Option, + virtual_keyboard: Option, // states namespace: String, @@ -471,6 +475,7 @@ pub struct WindowState { // settings use_display_handle: bool, + loop_handler: Option>, } impl WindowState { @@ -657,6 +662,7 @@ impl Default for WindowState { xdg_output_manager: None, globals: None, fractional_scale_manager: None, + virtual_keyboard: None, seat: None, keyboard_state: None, @@ -672,11 +678,27 @@ impl Default for WindowState { margin: None, use_display_handle: false, + loop_handler: None, } } } impl WindowState { + /// You can save the virtual_keyboard here + pub fn set_virtual_keyboard(&mut self, keyboard: ZwpVirtualKeyboardV1) { + self.virtual_keyboard = Some(keyboard); + } + + /// get the saved virtual_keyboard + pub fn get_virtual_keyboard(&self) -> Option<&ZwpVirtualKeyboardV1> { + self.virtual_keyboard.as_ref() + } + + /// with loop_handler you can do more thing + pub fn get_loop_handler(&self) -> Option<&LoopHandle<'static, Self>> { + self.loop_handler.as_ref() + } + /// get the unit with the index returned by eventloop pub fn get_unit(&mut self, index: usize) -> &mut WindowStateUnit { &mut self.units[index] @@ -1377,6 +1399,8 @@ impl WindowState { .insert(event_loop.handle()) .expect("Failed to init wayland source"); + self.loop_handler = Some(event_loop.handle()); + 'out: loop { event_loop.dispatch(Duration::from_millis(1), &mut self)?;