Skip to content

Commit

Permalink
feat: support base popup (#38)
Browse files Browse the repository at this point in the history
* feat: support base popup

* feat: popup example

* feat: finishe menu popup

* chore: code tidy up

* chore: code style adjust

* chore: update example

* chore: update example

* chore: rename function and some adjust
  • Loading branch information
Decodetalkers authored Aug 8, 2024
1 parent c7a00f2 commit 6a08036
Show file tree
Hide file tree
Showing 9 changed files with 395 additions and 64 deletions.
64 changes: 51 additions & 13 deletions iced_examples/counter_muti/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use std::collections::HashMap;

use iced::widget::{button, column, row, text, text_input};
use iced::theme::Container;
use iced::widget::{button, column, container, row, text, text_input};
use iced::window::Id;
use iced::{event, Alignment, Command, Element, Event, Length, Theme};
use iced_layershell::actions::{
LayershellCustomActions, LayershellCustomActionsWithId, LayershellCustomActionsWithIdAndInfo,
LayershellCustomActionsWithInfo,
IcedNewMenuSettings, LayershellCustomActions, LayershellCustomActionsWithId,
LayershellCustomActionsWithIdAndInfo, LayershellCustomActionsWithInfo, MenuDirection,
};
use iced_runtime::command::Action;
use iced_runtime::window::Action as WindowAction;
Expand Down Expand Up @@ -35,6 +36,7 @@ struct Counter {
enum WindowInfo {
Left,
Right,
PopUp,
}

#[derive(Debug, Clone, Copy)]
Expand All @@ -57,6 +59,16 @@ enum Message {
IcedEvent(Event),
}

#[derive(Default)]
struct BlackMenu;

impl container::StyleSheet for BlackMenu {
type Style = iced::Theme;
fn appearance(&self, _style: &Self::Style) -> container::Appearance {
container::Appearance::default().with_background(iced::Color::new(0., 0.5, 0.7, 0.6))
}
}

impl Counter {
fn window_id(&self, info: &WindowInfo) -> Option<&iced::window::Id> {
for (k, v) in self.ids.iter() {
Expand All @@ -79,7 +91,7 @@ impl MultiApplication for Counter {
(
Self {
value: 0,
text: "eee".to_string(),
text: "type something".to_string(),
ids: HashMap::new(),
},
Command::none(),
Expand Down Expand Up @@ -112,14 +124,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((
IcedNewMenuSettings {
size: (100, 100),
direction: MenuDirection::Up,
},
WindowInfo::PopUp,
)),
)
.into(),
);
}
_ => {}
}
Command::none()
}
Expand Down Expand Up @@ -254,12 +283,21 @@ impl MultiApplication for Counter {
if let Some(WindowInfo::Right) = self.id_info(id) {
return button("close right").on_press(Message::Close(id)).into();
}
if let Some(WindowInfo::PopUp) = self.id_info(id) {
return container(button("close PopUp").on_press(Message::Close(id)))
.center_x()
.center_y()
.style(Container::Custom(Box::new(BlackMenu)))
.width(Length::Fill)
.height(Length::Fill)
.into();
}
let center = column![
button("Increment").on_press(Message::IncrementPressed),
text(self.value).size(50),
button("Decrement").on_press(Message::DecrementPressed),
text(self.value).size(50),
button("newwindowLeft").on_press(Message::NewWindowLeft),
button("newwindowRight").on_press(Message::NewWindowRight)
button("newwindowRight").on_press(Message::NewWindowRight),
]
.padding(20)
.align_items(Alignment::Center)
Expand Down
26 changes: 24 additions & 2 deletions iced_layershell/src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,34 @@ use iced_core::mouse::Interaction;
use iced_runtime::command::Action;
use layershellev::id::Id as LayerId;
use layershellev::NewLayerShellSettings;

#[allow(unused)]
#[derive(Debug, Clone)]
pub(crate) enum LayerShellActions<INFO: Clone> {
Mouse(Interaction),
CustomActions(Vec<LayershellCustomActionsWithInfo<INFO>>),
CustomActionsWithId(Vec<LayershellCustomActionsWithIdInner<INFO>>),
RedrawAll,
RedrawWindow(LayerId),
RedrawWindow(LayerId), // maybe one day it is useful, but now useless
NewMenu((IcedNewPopupSettings, INFO)),
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
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 IcedNewMenuSettings {
pub size: (u32, u32),
pub direction: MenuDirection,
}

#[derive(Debug, Clone, Copy)]
Expand All @@ -24,8 +44,10 @@ pub enum LayershellCustomActionsWithInfo<INFO: Clone> {
key: u32,
},
NewLayerShell((NewLayerShellSettings, INFO)),
NewPopUp((IcedNewPopupSettings, INFO)),
NewMenu((IcedNewMenuSettings, INFO)),
/// is same with WindowAction::Close(id)
RemoveLayerShell(IcedId),
RemoveWindow(IcedId),
}

pub type LayershellCustomActions = LayershellCustomActionsWithInfo<()>;
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::IcedNewMenuSettings;

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((IcedNewMenuSettings, 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
Loading

0 comments on commit 6a08036

Please sign in to comment.