Skip to content

Commit

Permalink
feat: finishe menu popup
Browse files Browse the repository at this point in the history
  • Loading branch information
Decodetalkers committed Aug 7, 2024
1 parent 8aefed9 commit 5857735
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 45 deletions.
48 changes: 25 additions & 23 deletions iced_examples/counter_muti/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use iced::window::Id;
use iced::{event, Alignment, Command, Element, Event, Length, Theme};
use iced_layershell::actions::{
LayershellCustomActions, LayershellCustomActionsWithId, LayershellCustomActionsWithIdAndInfo,
LayershellCustomActionsWithInfo, NewMenuSettings,
LayershellCustomActionsWithInfo, MenuDirection, NewMenuSettings,
};
use iced_runtime::command::Action;
use iced_runtime::window::Action as WindowAction;
Expand Down Expand Up @@ -52,7 +52,6 @@ enum Message {
DecrementPressed,
NewWindowLeft,
NewWindowRight,
NewPopup,
Close(Id),
TextInput(String),
Direction(WindowDirection),
Expand Down Expand Up @@ -114,14 +113,31 @@ impl MultiApplication for Counter {
use iced::Event;
match message {
Message::IcedEvent(event) => {
if let Event::Keyboard(keyboard::Event::KeyPressed {
key: keyboard::Key::Named(Named::Escape),
..
}) = event
{
if let Some(id) = self.window_id(&WindowInfo::Left) {
return Command::single(Action::Window(WindowAction::Close(*id)));
match event {
Event::Keyboard(keyboard::Event::KeyPressed {
key: keyboard::Key::Named(Named::Escape),
..
}) => {
if let Some(id) = self.window_id(&WindowInfo::Left) {
return Command::single(Action::Window(WindowAction::Close(*id)));
}
}
Event::Mouse(iced::mouse::Event::ButtonPressed(iced::mouse::Button::Right)) => {
return Command::single(
LayershellCustomActionsWithIdAndInfo::new(
iced::window::Id::MAIN,
LayershellCustomActionsWithInfo::NewMenu((
NewMenuSettings {
size: (100, 100),
direction: MenuDirection::Up,
},
WindowInfo::PopUp,
)),
)
.into(),
);
}
_ => {}
}
Command::none()
}
Expand Down Expand Up @@ -245,19 +261,6 @@ impl MultiApplication for Counter {
)
.into(),
),
Message::NewPopup => Command::single(
LayershellCustomActionsWithIdAndInfo::new(
iced::window::Id::MAIN,
LayershellCustomActionsWithInfo::NewMenu((
NewMenuSettings {
size: (100, 100),
position: (0, -100),
},
WindowInfo::PopUp,
)),
)
.into(),
),
Message::Close(id) => Command::single(Action::Window(WindowAction::Close(id))),
}
}
Expand All @@ -277,7 +280,6 @@ impl MultiApplication for Counter {
button("Decrement").on_press(Message::DecrementPressed),
button("newwindowLeft").on_press(Message::NewWindowLeft),
button("newwindowRight").on_press(Message::NewWindowRight),
button("popup").on_press(Message::NewPopup)
]
.padding(20)
.align_items(Alignment::Center)
Expand Down
16 changes: 15 additions & 1 deletion iced_layershell/src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,27 @@ pub(crate) enum LayerShellActions<INFO: Clone> {
CustomActionsWithId(Vec<LayershellCustomActionsWithIdInner<INFO>>),
RedrawAll,
RedrawWindow(LayerId),
NewMenu((IcedNewPopupSettings, INFO)),
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct NewMenuSettings {
pub struct IcedNewPopupSettings {
pub size: (u32, u32),
pub position: (i32, i32),
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum MenuDirection {
Up,
Down,
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct NewMenuSettings {
pub size: (u32, u32),
pub direction: MenuDirection,
}

#[derive(Debug, Clone, Copy)]
pub enum LayershellCustomActionsWithInfo<INFO: Clone> {
AnchorChange(Anchor),
Expand All @@ -30,6 +43,7 @@ pub enum LayershellCustomActionsWithInfo<INFO: Clone> {
key: u32,
},
NewLayerShell((NewLayerShellSettings, INFO)),
NewPopUp((IcedNewPopupSettings, INFO)),
NewMenu((NewMenuSettings, INFO)),
/// is same with WindowAction::Close(id)
RemoveLayerShell(IcedId),
Expand Down
4 changes: 2 additions & 2 deletions iced_layershell/src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ pub fn window_event(
}
LayerShellEvent::CursorEnter { .. } => Some(IcedEvent::Mouse(mouse::Event::CursorEntered)),
LayerShellEvent::MouseInput(state) => Some(IcedEvent::Mouse(match state {
IcedButtonState::Pressed => mouse::Event::ButtonPressed(mouse::Button::Left),
IcedButtonState::Released => mouse::Event::ButtonReleased(mouse::Button::Left),
IcedButtonState::Pressed(btn) => mouse::Event::ButtonPressed(*btn),
IcedButtonState::Released(btn) => mouse::Event::ButtonReleased(*btn),
})),
LayerShellEvent::Axis { x, y } => Some(IcedEvent::Mouse(mouse::Event::WheelScrolled {
delta: mouse::ScrollDelta::Lines { x: *x, y: *y },
Expand Down
34 changes: 24 additions & 10 deletions iced_layershell/src/event.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
use iced::mouse;
use layershellev::id::Id;
use layershellev::keyboard::ModifiersState;
use layershellev::reexport::wayland_client::{ButtonState, KeyState, WEnum};
use layershellev::xkb_keyboard::KeyEvent as LayerShellKeyEvent;
use layershellev::{DispatchMessage, WindowWrapper};

use iced_core::keyboard::Modifiers as IcedModifiers;

use crate::actions::NewMenuSettings;

fn from_u32_to_icedmouse(code: u32) -> mouse::Button {
match code {
273 => mouse::Button::Right,
_ => mouse::Button::Left,
}
}
#[derive(Debug, Clone, Copy)]
pub enum IcedButtonState {
Pressed,
Released,
Pressed(mouse::Button),
Released(mouse::Button),
}

#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -77,6 +87,7 @@ pub enum IcedLayerEvent<Message: 'static, INFO: Clone> {
NormalUpdate,
UserEvent(Message),
WindowRemoved(iced_core::window::Id),
NewMenu((NewMenuSettings, INFO)),
}

#[allow(unused)]
Expand Down Expand Up @@ -117,15 +128,18 @@ impl<Message: 'static, INFO: Clone> From<&DispatchMessage> for IcedLayerEvent<Me
IcedLayerEvent::Window(WindowEvent::CursorMoved { x: *x, y: *y })
}
DispatchMessage::MouseLeave => IcedLayerEvent::Window(WindowEvent::CursorLeft),
DispatchMessage::MouseButton { state, .. } => match state {
WEnum::Value(ButtonState::Pressed) => {
IcedLayerEvent::Window(WindowEvent::MouseInput(IcedButtonState::Pressed))
DispatchMessage::MouseButton { state, button, .. } => {
let btn = from_u32_to_icedmouse(*button);
match state {
WEnum::Value(ButtonState::Pressed) => IcedLayerEvent::Window(
WindowEvent::MouseInput(IcedButtonState::Pressed(btn)),
),
WEnum::Value(ButtonState::Released) => IcedLayerEvent::Window(
WindowEvent::MouseInput(IcedButtonState::Released(btn)),
),
_ => unreachable!(),
}
WEnum::Value(ButtonState::Released) => {
IcedLayerEvent::Window(WindowEvent::MouseInput(IcedButtonState::Released))
}
_ => unreachable!(),
},
}
DispatchMessage::PrefredScale(scale) => {
IcedLayerEvent::Window(WindowEvent::ScaleChanged(*scale))
}
Expand Down
67 changes: 58 additions & 9 deletions iced_layershell/src/multi_window.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
mod state;
use crate::{
actions::{
LayershellCustomActionsWithIdAndInfo, LayershellCustomActionsWithIdInner, NewMenuSettings,
IcedNewPopupSettings, LayershellCustomActionsWithIdAndInfo,
LayershellCustomActionsWithIdInner, MenuDirection, NewMenuSettings,
},
multi_window::window_manager::WindowManager,
settings::VirtualKeyboardSettings,
Expand Down Expand Up @@ -215,7 +216,7 @@ where
let _ = ev.running_with_proxy(message_receiver, move |event, ev, index| {
use layershellev::DispatchMessage;
let mut def_returndata = ReturnData::None;
let id = index.map(|index| ev.get_unit(index).id());
let sended_id = index.map(|index| ev.get_unit(index).id());
match event {
LayerEvent::InitRequest => {
if settings.virtual_keyboard_support.is_some() {
Expand Down Expand Up @@ -251,7 +252,7 @@ where
let unit = ev.get_unit(index.unwrap());
event_sender
.start_send(MultiWindowIcedLayerEvent(
id,
sended_id,
IcedLayerEvent::RequestRefreshWithWrapper {
width: *width,
height: *height,
Expand All @@ -270,21 +271,21 @@ where
}

event_sender
.start_send(MultiWindowIcedLayerEvent(id, message.into()))
.start_send(MultiWindowIcedLayerEvent(sended_id, message.into()))
.expect("Cannot send");
}

LayerEvent::UserEvent(event) => {
event_sender
.start_send(MultiWindowIcedLayerEvent(
id,
sended_id,
IcedLayerEvent::UserEvent(event),
))
.ok();
}
LayerEvent::NormalDispatch => {
event_sender
.start_send(MultiWindowIcedLayerEvent(id, IcedLayerEvent::NormalUpdate))
.start_send(MultiWindowIcedLayerEvent(sended_id, IcedLayerEvent::NormalUpdate))
.expect("Cannot send");
}
_ => {}
Expand Down Expand Up @@ -345,8 +346,8 @@ where
event_sender.start_send(MultiWindowIcedLayerEvent(None, IcedLayerEvent::WindowRemoved(id))).ok();
return ReturnData::RemoveLayershell(option_id.unwrap())
}
LayershellCustomActionsWithInfo::NewMenu((menusettings, info)) => {
let NewMenuSettings { size, position } = menusettings;
LayershellCustomActionsWithInfo::NewPopUp((menusettings, info)) => {
let IcedNewPopupSettings { size, position } = menusettings;
let Some(id) = ev.current_surface_id() else {
continue;
};
Expand All @@ -356,10 +357,28 @@ where
Some(info),
))
}
LayershellCustomActionsWithInfo::NewMenu((menusetting, info)) => {
let Some(id) = ev.current_surface_id() else {
continue;
};
event_sender
.start_send(MultiWindowIcedLayerEvent(Some(id), IcedLayerEvent::NewMenu((menusetting, info))))
.expect("Cannot send");
}
}
}
}

LayerShellActions::NewMenu((menusettings, info)) => {
let IcedNewPopupSettings { size, position } = menusettings;
let Some(id) = ev.current_surface_id() else {
continue;
};
let popup_settings = NewPopUpSettings {size, position,id};
return ReturnData::NewPopUp((
popup_settings,
Some(info),
))
}
LayerShellActions::Mouse(mouse) => {
let Some(pointer) = ev.get_pointer() else {
return ReturnData::None;
Expand Down Expand Up @@ -709,6 +728,36 @@ async fn run_instance<A, E, C>(
cached_interfaces,
));
}
MultiWindowIcedLayerEvent(
Some(id),
IcedLayerEvent::NewMenu((
NewMenuSettings {
size: (width, height),
direction,
},
info,
)),
) => {
let Some((_, window)) = window_manager.get_alias(id) else {
continue;
};

let Some(point) = window.state.mouse_position() else {
continue;
};

let (x, mut y) = (point.x as i32, point.y as i32);
if let MenuDirection::Up = direction {
y -= height as i32;
}
custom_actions.push(LayerShellActions::NewMenu((
IcedNewPopupSettings {
size: (width, height),
position: (x, y),
},
info,
)));
}
_ => {}
}
control_sender.start_send(custom_actions.clone()).ok();
Expand Down
4 changes: 4 additions & 0 deletions iced_layershell/src/multi_window/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ where
&self.theme
}

pub fn mouse_position(&self) -> Option<&Point> {
self.mouse_position.as_ref()
}

pub fn cursor(&self) -> IcedMouse::Cursor {
self.mouse_position
.map(IcedMouse::Cursor::Available)
Expand Down
7 changes: 7 additions & 0 deletions iced_layershell/src/multi_window/window_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,19 @@ where

Some((id, self.get_mut(id)?))
}
pub fn get_alias(&self, id: LayerId) -> Option<(IcedId, &Window<A, C>)> {
let id = self.aliases.get(&id).copied()?;

Some((id, self.get(id)?))
}
pub fn get_layer_id(&self, id: IcedId) -> Option<LayerId> {
self.back_aliases.get(&id).copied()
}

pub fn get_mut(&mut self, id: IcedId) -> Option<&mut Window<A, C>> {
self.entries.get_mut(&id)
}
pub fn get(&self, id: IcedId) -> Option<&Window<A, C>> {
self.entries.get(&id)
}
}

0 comments on commit 5857735

Please sign in to comment.