Skip to content

Commit

Permalink
feat: seems finished
Browse files Browse the repository at this point in the history
  • Loading branch information
Decodetalkers committed Nov 17, 2024
1 parent 6a631c1 commit 80f0e61
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 37 deletions.
7 changes: 4 additions & 3 deletions iced_examples/counter_muti/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,13 @@ impl MultiApplication for Counter {
Command::done(Message::NewLayerShell {
settings: NewLayerShellSettings {
size: Some((0, 20)),
exclusive_zone: None,
exclusive_zone: Some(20),
anchor: Anchor::Top | Anchor::Right | Anchor::Left,
layer: Layer::Top,
margin: None,
keyboard_interactivity: KeyboardInteractivity::Exclusive,
keyboard_interactivity: KeyboardInteractivity::None,
output_setting: LayerOutputSetting::ChosenOutput(output),
..Default::default()
},
info: WindowInfo::TopBar,
})
Expand All @@ -234,7 +235,7 @@ impl MultiApplication for Counter {
return button("close right").on_press(Message::Close(id)).into();
}
if let Some(WindowInfo::TopBar) = self.id_info(id) {
return text("topbar").into();
return text("hello here is topbar").into();
}
if let Some(WindowInfo::PopUp) = self.id_info(id) {
return container(button("close PopUp").on_press(Message::Close(id)))
Expand Down
56 changes: 53 additions & 3 deletions iced_wayland_subscriber/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,38 @@
use futures::SinkExt;
use wayland_client::{
delegate_noop,
globals::{registry_queue_init, GlobalListContents},
protocol::{
wl_output::{self, WlOutput},
wl_registry,
},
Connection, Dispatch, Proxy,
};

use wayland_protocols::xdg::xdg_output::zv1::client::zxdg_output_manager_v1::ZxdgOutputManagerV1;
use wayland_protocols::xdg::xdg_output::zv1::client::{
zxdg_output_manager_v1::ZxdgOutputManagerV1, zxdg_output_v1,
};

#[derive(Debug)]
struct BaseState;

// so interesting, it is just need to invoke once, it just used to get the globals
impl Dispatch<wl_registry::WlRegistry, GlobalListContents> for BaseState {
fn event(
_state: &mut Self,
_proxy: &wl_registry::WlRegistry,
_event: <wl_registry::WlRegistry as wayland_client::Proxy>::Event,
_data: &GlobalListContents,
_conn: &Connection,
_qh: &wayland_client::QueueHandle<Self>,
) {
}
}

#[derive(Debug, Default)]
struct SubscribeState {
events: Vec<WaylandEvents>,
padding_wloutputs: Vec<wl_output::WlOutput>,
}

impl Dispatch<wl_registry::WlRegistry, ()> for SubscribeState {
Expand All @@ -32,35 +52,65 @@ impl Dispatch<wl_registry::WlRegistry, ()> for SubscribeState {
} => {
if interface == wl_output::WlOutput::interface().name {
let output = proxy.bind::<wl_output::WlOutput, _, _>(name, version, qh, ());
state.events.push(WaylandEvents::OutputInsert(output));
state.padding_wloutputs.push(output);
}
}
wl_registry::Event::GlobalRemove { .. } => {}
_ => unreachable!(),
}
}
}
impl Dispatch<zxdg_output_v1::ZxdgOutputV1, ()> for SubscribeState {
fn event(
state: &mut Self,
_proxy: &zxdg_output_v1::ZxdgOutputV1,
event: <zxdg_output_v1::ZxdgOutputV1 as Proxy>::Event,
_data: &(),
_conn: &Connection,
_qhandle: &wayland_client::QueueHandle<Self>,
) {
if let zxdg_output_v1::Event::Name { name } = event {
state.events.push(WaylandEvents::OutputInsert(name));
}
}
}
delegate_noop!(SubscribeState: ignore WlOutput); // output is need to place layer_shell, although here
delegate_noop!(SubscribeState: ignore ZxdgOutputManagerV1);

#[derive(Debug, Clone)]
pub enum WaylandEvents {
OutputInsert(wl_output::WlOutput),
OutputInsert(String),
}

pub fn listen() -> iced::Subscription<WaylandEvents> {
iced::Subscription::run(|| {
iced::stream::channel(100, |mut output| async move {
let connection = Connection::connect_to_env().unwrap();
let (globals, _) = registry_queue_init::<BaseState>(&connection).unwrap(); // We just need the
// global, the
// event_queue is
// not needed, we
// do not need
// BaseState after

let mut state = SubscribeState::default();

let mut event_queue = connection.new_event_queue::<SubscribeState>();
let qhandle = event_queue.handle();
let display = connection.display();

let xdg_output_manager = globals
.bind::<ZxdgOutputManagerV1, _, _>(&qhandle, 1..=3, ())
.unwrap(); // b
display.get_registry(&qhandle, ());
loop {
event_queue.blocking_dispatch(&mut state).unwrap();
let mut current_outputs = vec![];
std::mem::swap(&mut current_outputs, &mut state.padding_wloutputs);
for output in current_outputs {
xdg_output_manager.get_xdg_output(&output, &qhandle, ());
}

let mut current_events = vec![];
std::mem::swap(&mut current_events, &mut state.events);
for event in current_events {
Expand Down
4 changes: 2 additions & 2 deletions layershellev/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use wayland_client::{
protocol::{
wl_buffer::WlBuffer,
wl_compositor::WlCompositor,
wl_output::{self, WlOutput},
wl_output::WlOutput,
wl_pointer::{self, ButtonState, WlPointer},
wl_shm::WlShm,
},
Expand Down Expand Up @@ -71,7 +71,7 @@ pub enum LayerEvent<'a, T, Message> {
/// This allow the new layershell can be selected on target output
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LayerOutputSetting {
ChosenOutput(wl_output::WlOutput),
ChosenOutput(String),
FollowLastOutput,
None,
}
Expand Down
47 changes: 18 additions & 29 deletions layershellev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1663,55 +1663,41 @@ impl<T> Dispatch<zxdg_output_v1::ZxdgOutputV1, ()> for WindowState<T> {
_conn: &Connection,
_qhandle: &QueueHandle<Self>,
) {
if state.is_with_target() && !state.init_finished {
let Some((_, xdg_info)) = state
.xdg_info_cache
.iter_mut()
.find(|(_, info)| info.zxdgoutput == *proxy)
else {
return;
};
match event {
zxdg_output_v1::Event::LogicalSize { width, height } => {
xdg_info.logical_size = (width, height);
}
zxdg_output_v1::Event::LogicalPosition { x, y } => {
xdg_info.position = (x, y);
}
zxdg_output_v1::Event::Name { name } => {
xdg_info.name = name;
}
zxdg_output_v1::Event::Description { description } => {
xdg_info.description = description;
}
_ => {}
};
return;
}
let Some(index) = state.units.iter().position(|info| {
info.zxdgoutput
.as_ref()
.is_some_and(|zxdgoutput| zxdgoutput.zxdgoutput == *proxy)
}) else {
return;
};
let Some((_, xdg_info_cached)) = state
.xdg_info_cache
.iter_mut()
.find(|(_, info)| info.zxdgoutput == *proxy)
else {
return;
};
let info = &mut state.units[index];
let xdg_info = info.zxdgoutput.as_mut().unwrap();
let change_type = match event {
zxdg_output_v1::Event::LogicalSize { width, height } => {
xdg_info.logical_size = (width, height);
xdg_info_cached.logical_size = (width, height);
XdgInfoChangedType::Size
}
zxdg_output_v1::Event::LogicalPosition { x, y } => {
xdg_info.position = (x, y);
xdg_info_cached.position = (x, y);
XdgInfoChangedType::Position
}
zxdg_output_v1::Event::Name { name } => {
xdg_info.name = name;
xdg_info.name = name.clone();
xdg_info_cached.name = name;
XdgInfoChangedType::Name
}
zxdg_output_v1::Event::Description { description } => {
xdg_info.description = description;
xdg_info.description = description.clone();
xdg_info_cached.description = description;
XdgInfoChangedType::Description
}
_ => {
Expand Down Expand Up @@ -1860,7 +1846,6 @@ impl<T: 'static> WindowState<T> {
// clear binded_output_name, it is not used anymore
}

self.xdg_info_cache.clear();
let binded_output = output.as_ref().map(|(output, _)| output);
let binded_xdginfo = output.as_ref().map(|(_, xdginfo)| xdginfo);

Expand Down Expand Up @@ -2451,7 +2436,11 @@ impl<T: 'static> WindowState<T> {
events::LayerOutputSetting::FollowLastOutput => {
self.last_wloutput.clone()
}
events::LayerOutputSetting::ChosenOutput(output) => Some(output),
events::LayerOutputSetting::ChosenOutput(output) => self
.xdg_info_cache
.iter()
.find(|(_, xdg_output_info)| xdg_output_info.name == output)
.map(|(output, _)| output.clone()),
};

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

0 comments on commit 80f0e61

Please sign in to comment.