Skip to content

Commit

Permalink
feat: popup example
Browse files Browse the repository at this point in the history
  • Loading branch information
Decodetalkers committed Aug 7, 2024
1 parent f73b5ff commit 8aefed9
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 18 deletions.
26 changes: 22 additions & 4 deletions iced_examples/counter_muti/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::collections::HashMap;

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

#[derive(Debug, Clone, Copy)]
Expand All @@ -51,6 +52,7 @@ enum Message {
DecrementPressed,
NewWindowLeft,
NewWindowRight,
NewPopup,
Close(Id),
TextInput(String),
Direction(WindowDirection),
Expand Down Expand Up @@ -243,6 +245,19 @@ 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 @@ -254,12 +269,15 @@ 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 button("close PopUp").on_press(Message::Close(id)).into();
}
let center = column![
button("Increment").on_press(Message::IncrementPressed),
text(self.value).size(50),
button("Decrement").on_press(Message::DecrementPressed),
button("newwindowLeft").on_press(Message::NewWindowLeft),
button("newwindowRight").on_press(Message::NewWindowRight)
button("newwindowRight").on_press(Message::NewWindowRight),
button("popup").on_press(Message::NewPopup)
]
.padding(20)
.align_items(Alignment::Center)
Expand Down
7 changes: 7 additions & 0 deletions iced_layershell/src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ pub(crate) enum LayerShellActions<INFO: Clone> {
RedrawWindow(LayerId),
}

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

#[derive(Debug, Clone, Copy)]
pub enum LayershellCustomActionsWithInfo<INFO: Clone> {
AnchorChange(Anchor),
Expand All @@ -24,6 +30,7 @@ pub enum LayershellCustomActionsWithInfo<INFO: Clone> {
key: u32,
},
NewLayerShell((NewLayerShellSettings, INFO)),
NewMenu((NewMenuSettings, INFO)),
/// is same with WindowAction::Close(id)
RemoveLayerShell(IcedId),
}
Expand Down
17 changes: 15 additions & 2 deletions iced_layershell/src/multi_window.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
mod state;
use crate::{
actions::{LayershellCustomActionsWithIdAndInfo, LayershellCustomActionsWithIdInner},
actions::{
LayershellCustomActionsWithIdAndInfo, LayershellCustomActionsWithIdInner, NewMenuSettings,
},
multi_window::window_manager::WindowManager,
settings::VirtualKeyboardSettings,
};
Expand All @@ -26,7 +28,7 @@ use iced_futures::{Executor, Runtime, Subscription};
use layershellev::{
calloop::timer::{TimeoutAction, Timer},
reexport::zwp_virtual_keyboard_v1,
LayerEvent, ReturnData, WindowState,
LayerEvent, NewPopUpSettings, ReturnData, WindowState,
};

use futures::{channel::mpsc, SinkExt, StreamExt};
Expand Down Expand Up @@ -343,6 +345,17 @@ 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;
let Some(id) = ev.current_surface_id() else {
continue;
};
let popup_settings = NewPopUpSettings {size, position,id};
return ReturnData::NewPopUp((
popup_settings,
Some(info),
))
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion layershellev/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub struct NewLayerShellSettings {
pub keyboard_interactivity: KeyboardInteractivity,
}

#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct NewPopUpSettings {
pub size: (u32, u32),
pub position: (i32, i32),
Expand Down
26 changes: 15 additions & 11 deletions layershellev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
//! ```
//!
pub use events::NewLayerShellSettings;
use events::NewPopUpSettings;
pub use events::NewPopUpSettings;
use sctk::reexports::calloop::LoopHandle;
pub use waycrate_xkbkeycode::keyboard;
pub use waycrate_xkbkeycode::xkb_keyboard;
Expand Down Expand Up @@ -163,6 +163,7 @@ use wayland_protocols_wlr::layer_shell::v1::client::{
use wayland_protocols::xdg::shell::client::{
xdg_popup::{self, XdgPopup},
xdg_positioner::XdgPositioner,
xdg_surface::XdgSurface,
xdg_wm_base::XdgWmBase,
};

Expand Down Expand Up @@ -294,7 +295,7 @@ impl ZxdgOutputInfo {
#[derive(Debug)]
enum Shell {
LayerShell(ZwlrLayerSurfaceV1),
PopUp(XdgPopup),
PopUp((XdgPopup, XdgSurface)),
}

impl PartialEq<ZwlrLayerSurfaceV1> for Shell {
Expand All @@ -309,7 +310,7 @@ impl PartialEq<ZwlrLayerSurfaceV1> for Shell {
impl PartialEq<XdgPopup> for Shell {
fn eq(&self, other: &XdgPopup) -> bool {
match self {
Self::PopUp(popup) => popup == other,
Self::PopUp((popup, _)) => popup == other,
_ => false,
}
}
Expand All @@ -318,7 +319,10 @@ impl PartialEq<XdgPopup> for Shell {
impl Shell {
fn destroy(&self) {
match self {
Self::PopUp(popup) => popup.destroy(),
Self::PopUp((popup, xdg_surface)) => {
popup.destroy();
xdg_surface.destroy();
}
Self::LayerShell(shell) => shell.destroy(),
}
}
Expand Down Expand Up @@ -1944,20 +1948,18 @@ impl<T: 'static> WindowState<T> {
else {
continue;
};
let wl_surface = wmcompositer.create_surface(&qh, ());
let positioner = wmbase.create_positioner(&qh, ());
positioner.set_size(width as i32, height as i32);
positioner.set_anchor_rect(x, y, width as i32, height as i32);
let wl_xdg_surface =
wmbase.get_xdg_surface(&self.units[index].wl_surface, &qh, ());
let wl_xdg_surface = wmbase.get_xdg_surface(&wl_surface, &qh, ());
let popup = wl_xdg_surface.get_popup(None, &positioner, &qh, ());

let Shell::LayerShell(shell) = &self.units[index].shell else {
unreachable!()
};
shell.get_popup(&popup);

let wl_surface = wmcompositer.create_surface(&qh, ()); // and create a surface. if two or more,

let mut fractional_scale = None;
if let Some(ref fractional_scale_manager) = fractional_scale_manager {
fractional_scale =
Expand All @@ -1967,15 +1969,15 @@ impl<T: 'static> WindowState<T> {
(),
));
}
self.units[index].wl_surface.commit();
wl_surface.commit();

self.units.push(WindowStateUnit {
id: id::Id::unique(),
display: connection.display(),
wl_surface,
size: (0, 0),
size: (width, height),
buffer: None,
shell: Shell::PopUp(popup),
shell: Shell::PopUp((popup, wl_xdg_surface)),
zxdgoutput: None,
fractional_scale,
becreated: true,
Expand All @@ -1991,8 +1993,10 @@ impl<T: 'static> WindowState<T> {
else {
continue;
};

self.units[index].shell.destroy();
self.units[index].wl_surface.destroy();

if let Some(buffer) = self.units[index].buffer.as_ref() {
buffer.destroy()
}
Expand Down

0 comments on commit 8aefed9

Please sign in to comment.