Skip to content

Commit

Permalink
chore: finished
Browse files Browse the repository at this point in the history
  • Loading branch information
Decodetalkers committed Jul 31, 2024
1 parent 5b89b49 commit f2c0f78
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 35 deletions.
9 changes: 2 additions & 7 deletions iced_examples/counter_virtual_keyboard/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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![
Expand Down
3 changes: 1 addition & 2 deletions iced_layershell/src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -21,7 +21,6 @@ pub enum LayershellCustomActions {
VirtualKeyboardPressed {
time: u32,
key: u32,
keystate: KeyState,
},
}

Expand Down
41 changes: 25 additions & 16 deletions iced_layershell/src/application.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand All @@ -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};
Expand Down Expand Up @@ -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;
Expand All @@ -200,7 +201,6 @@ where
def_returndata = ReturnData::RequestBind;
}
}
// TODO: maybe use it later
LayerEvent::BindProvide(globals, qh) => {
let virtual_keyboard_manager = globals
.bind::<zwp_virtual_keyboard_v1::ZwpVirtualKeyboardManagerV1, _, _>(
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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);
}
_ => {}
}
Expand Down
33 changes: 23 additions & 10 deletions iced_layershell/src/multi_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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};

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
24 changes: 24 additions & 0 deletions layershellev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -459,6 +462,7 @@ pub struct WindowState<T: Debug> {

pointer: Option<WlPointer>,
touch: Option<WlTouch>,
virtual_keyboard: Option<ZwpVirtualKeyboardV1>,

// states
namespace: String,
Expand All @@ -471,6 +475,7 @@ pub struct WindowState<T: Debug> {

// settings
use_display_handle: bool,
loop_handler: Option<LoopHandle<'static, Self>>,
}

impl<T: Debug> WindowState<T> {
Expand Down Expand Up @@ -657,6 +662,7 @@ impl<T: Debug> Default for WindowState<T> {
xdg_output_manager: None,
globals: None,
fractional_scale_manager: None,
virtual_keyboard: None,

seat: None,
keyboard_state: None,
Expand All @@ -672,11 +678,27 @@ impl<T: Debug> Default for WindowState<T> {
margin: None,

use_display_handle: false,
loop_handler: None,
}
}
}

impl<T: Debug> WindowState<T> {
/// 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<T> {
&mut self.units[index]
Expand Down Expand Up @@ -1377,6 +1399,8 @@ impl<T: Debug + 'static> WindowState<T> {
.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)?;

Expand Down

0 comments on commit f2c0f78

Please sign in to comment.