event handling & state
This commit is contained in:
parent
d991f59c90
commit
3a41e58159
15 changed files with 212 additions and 68 deletions
|
|
@ -10,7 +10,7 @@ use gtk4::{
|
|||
use once_cell::sync::OnceCell;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use tokio::sync::mpsc;
|
||||
use utils::{Activate, Workspace};
|
||||
use utils::{Activate, WorkspaceEvent};
|
||||
use window::CosmicWorkspacesWindow;
|
||||
|
||||
mod localize;
|
||||
|
|
@ -56,7 +56,7 @@ fn main() {
|
|||
|
||||
app.connect_activate(|app| {
|
||||
load_css();
|
||||
let (tx, mut rx) = mpsc::channel::<Vec<Workspace>>(100);
|
||||
let (tx, mut rx) = mpsc::channel::<Vec<WorkspaceEvent>>(100);
|
||||
|
||||
let wayland_tx = wayland::spawn_workspaces(tx.clone());
|
||||
let window = CosmicWorkspacesWindow::new(app);
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ use std::future::Future;
|
|||
|
||||
pub type Activate = u32;
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Workspace {
|
||||
pub struct WorkspaceEvent {
|
||||
pub(crate) id: u32,
|
||||
pub(crate) active: bool,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,18 +1,17 @@
|
|||
use crate::utils::{Activate, Workspace};
|
||||
use std::{
|
||||
os::unix::{net::UnixStream}, env, path::PathBuf,
|
||||
};
|
||||
use wayland_client::{ConnectError, DelegateDispatch, protocol::wl_registry};
|
||||
use crate::utils::{Activate, WorkspaceEvent};
|
||||
use std::{env, os::unix::net::UnixStream, path::PathBuf};
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
use wayland_client::{
|
||||
Connection, Dispatch, QueueHandle,
|
||||
protocol::{wl_output::WlOutput, wl_registry},
|
||||
ConnectError,
|
||||
};
|
||||
|
||||
use wayland_client::{Connection, Dispatch, QueueHandle};
|
||||
|
||||
/// Generated protocol definitions
|
||||
mod generated {
|
||||
#![allow(dead_code,non_camel_case_types,unused_unsafe,unused_variables)]
|
||||
#![allow(non_upper_case_globals,non_snake_case,unused_imports)]
|
||||
#![allow(dead_code, non_camel_case_types, unused_unsafe, unused_variables)]
|
||||
#![allow(non_upper_case_globals, non_snake_case, unused_imports)]
|
||||
#![allow(missing_docs, clippy::all)]
|
||||
|
||||
pub mod client {
|
||||
|
|
@ -32,7 +31,12 @@ mod generated {
|
|||
|
||||
use generated::client::zext_workspace_manager_v1;
|
||||
|
||||
pub fn spawn_workspaces(tx: mpsc::Sender<Vec<Workspace>>) -> mpsc::Sender<Activate> {
|
||||
use self::generated::client::{
|
||||
zext_workspace_group_handle_v1::{self, ZextWorkspaceGroupHandleV1},
|
||||
zext_workspace_handle_v1::{self, ZextWorkspaceHandleV1},
|
||||
};
|
||||
|
||||
pub fn spawn_workspaces(tx: mpsc::Sender<Vec<WorkspaceEvent>>) -> mpsc::Sender<Activate> {
|
||||
let (workspaces_tx, mut workspaces_rx) = mpsc::channel(100);
|
||||
if let Ok(Ok(conn)) = std::env::var("HOST_WAYLAND_DISPLAY")
|
||||
.map_err(anyhow::Error::msg)
|
||||
|
|
@ -47,18 +51,19 @@ pub fn spawn_workspaces(tx: mpsc::Sender<Vec<Workspace>>) -> mpsc::Sender<Activa
|
|||
.and_then(|s| s.map(|s| Connection::from_socket(s).map_err(anyhow::Error::msg)))
|
||||
{
|
||||
std::thread::spawn(move || {
|
||||
let mut event_queue= conn.new_event_queue::<State>();
|
||||
let mut event_queue = conn.new_event_queue::<State>();
|
||||
let qhandle = event_queue.handle();
|
||||
|
||||
let display = conn.display();
|
||||
display.get_registry(&qhandle, ()).unwrap();
|
||||
|
||||
|
||||
let mut state = State {
|
||||
workspace_manager: None,
|
||||
workspace_groups: Vec::new(),
|
||||
tx,
|
||||
running: true,
|
||||
};
|
||||
|
||||
|
||||
while state.running {
|
||||
event_queue.blocking_dispatch(&mut state).unwrap();
|
||||
}
|
||||
|
|
@ -71,12 +76,24 @@ pub fn spawn_workspaces(tx: mpsc::Sender<Vec<Workspace>>) -> mpsc::Sender<Activa
|
|||
workspaces_tx
|
||||
}
|
||||
|
||||
|
||||
|
||||
struct State {
|
||||
running: bool,
|
||||
tx: mpsc::Sender<Vec<Workspace>>,
|
||||
tx: mpsc::Sender<Vec<WorkspaceEvent>>,
|
||||
workspace_manager: Option<zext_workspace_manager_v1::ZextWorkspaceManagerV1>,
|
||||
workspace_groups: Vec<WorkspaceGroup>,
|
||||
}
|
||||
|
||||
struct WorkspaceGroup {
|
||||
workspace_group_handle: ZextWorkspaceGroupHandleV1,
|
||||
output: Option<WlOutput>,
|
||||
workspaces: Vec<Workspace>,
|
||||
}
|
||||
|
||||
struct Workspace {
|
||||
workspace_handle: ZextWorkspaceHandleV1,
|
||||
name: String,
|
||||
coordinates: Vec<u8>,
|
||||
state: Vec<u8>,
|
||||
}
|
||||
|
||||
impl Dispatch<wl_registry::WlRegistry, ()> for State {
|
||||
|
|
@ -88,13 +105,26 @@ impl Dispatch<wl_registry::WlRegistry, ()> for State {
|
|||
_: &Connection,
|
||||
qh: &QueueHandle<Self>,
|
||||
) {
|
||||
if let wl_registry::Event::Global { name, interface, version } = event {
|
||||
if let wl_registry::Event::Global {
|
||||
name,
|
||||
interface,
|
||||
version,
|
||||
} = event
|
||||
{
|
||||
println!("[{}] {} (v{})", name, interface, version);
|
||||
match &interface[..] {
|
||||
"zext_workspace_manager_v1" => {
|
||||
println!("binding to workspace manager");
|
||||
registry.bind::<zext_workspace_manager_v1::ZextWorkspaceManagerV1, _, _>(name, 1, qh, ()).unwrap();
|
||||
},
|
||||
let workspace_manager = registry
|
||||
.bind::<zext_workspace_manager_v1::ZextWorkspaceManagerV1, _, _>(
|
||||
name,
|
||||
1,
|
||||
qh,
|
||||
(),
|
||||
)
|
||||
.unwrap();
|
||||
self.workspace_manager = Some(workspace_manager);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
|
@ -105,12 +135,132 @@ impl Dispatch<zext_workspace_manager_v1::ZextWorkspaceManagerV1, ()> for State {
|
|||
fn event(
|
||||
&mut self,
|
||||
_: &zext_workspace_manager_v1::ZextWorkspaceManagerV1,
|
||||
_: zext_workspace_manager_v1::Event,
|
||||
event: zext_workspace_manager_v1::Event,
|
||||
_: &(),
|
||||
_: &Connection,
|
||||
_: &QueueHandle<Self>,
|
||||
) {
|
||||
todo!()
|
||||
match event {
|
||||
zext_workspace_manager_v1::Event::WorkspaceGroup { workspace_group } => {
|
||||
self.workspace_groups.push(WorkspaceGroup {
|
||||
workspace_group_handle: workspace_group,
|
||||
output: None,
|
||||
workspaces: Vec::new(),
|
||||
});
|
||||
}
|
||||
zext_workspace_manager_v1::Event::Done => {
|
||||
// TODO
|
||||
println!("sending event with workspace list state");
|
||||
let _ = self.tx.send(Vec::new());
|
||||
}
|
||||
zext_workspace_manager_v1::Event::Finished => {
|
||||
self.workspace_manager.take();
|
||||
}
|
||||
}
|
||||
// wl_compositor has no event
|
||||
}
|
||||
}
|
||||
|
||||
impl Dispatch<ZextWorkspaceGroupHandleV1, ()> for State {
|
||||
fn event(
|
||||
&mut self,
|
||||
group: &ZextWorkspaceGroupHandleV1,
|
||||
event: zext_workspace_group_handle_v1::Event,
|
||||
_: &(),
|
||||
_: &Connection,
|
||||
_: &QueueHandle<Self>,
|
||||
) {
|
||||
match event {
|
||||
zext_workspace_group_handle_v1::Event::OutputEnter { output } => {
|
||||
if let Some(group) = self
|
||||
.workspace_groups
|
||||
.iter_mut()
|
||||
.find(|g| &g.workspace_group_handle == group)
|
||||
{
|
||||
group.output = Some(output);
|
||||
}
|
||||
}
|
||||
zext_workspace_group_handle_v1::Event::OutputLeave { output } => {
|
||||
if let Some(group) = self.workspace_groups.iter_mut().find(|g| {
|
||||
&g.workspace_group_handle == group && g.output.as_ref() == Some(&output)
|
||||
}) {
|
||||
group.output = None;
|
||||
}
|
||||
}
|
||||
zext_workspace_group_handle_v1::Event::Workspace { workspace } => {
|
||||
if let Some(group) = self
|
||||
.workspace_groups
|
||||
.iter_mut()
|
||||
.find(|g| &g.workspace_group_handle == group)
|
||||
{
|
||||
group.workspaces.push(Workspace {
|
||||
workspace_handle: workspace,
|
||||
name: String::new(),
|
||||
coordinates: Vec::new(),
|
||||
state: Vec::new(),
|
||||
})
|
||||
}
|
||||
}
|
||||
zext_workspace_group_handle_v1::Event::Remove => {
|
||||
if let Some(group) = self
|
||||
.workspace_groups
|
||||
.iter()
|
||||
.position(|g| &g.workspace_group_handle == group)
|
||||
{
|
||||
self.workspace_groups.remove(group);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Dispatch<ZextWorkspaceHandleV1, ()> for State {
|
||||
fn event(
|
||||
&mut self,
|
||||
workspace: &ZextWorkspaceHandleV1,
|
||||
event: zext_workspace_handle_v1::Event,
|
||||
_: &(),
|
||||
_: &Connection,
|
||||
_: &QueueHandle<Self>,
|
||||
) {
|
||||
match event {
|
||||
zext_workspace_handle_v1::Event::Name { name } => {
|
||||
if let Some(w) = self.workspace_groups.iter_mut().find_map(|g| {
|
||||
g.workspaces
|
||||
.iter_mut()
|
||||
.find(|w| &w.workspace_handle == workspace)
|
||||
}) {
|
||||
w.name = name;
|
||||
}
|
||||
}
|
||||
zext_workspace_handle_v1::Event::Coordinates { coordinates } => {
|
||||
if let Some(w) = self.workspace_groups.iter_mut().find_map(|g| {
|
||||
g.workspaces
|
||||
.iter_mut()
|
||||
.find(|w| &w.workspace_handle == workspace)
|
||||
}) {
|
||||
w.coordinates = coordinates;
|
||||
}
|
||||
}
|
||||
zext_workspace_handle_v1::Event::State { state } => {
|
||||
if let Some(w) = self.workspace_groups.iter_mut().find_map(|g| {
|
||||
g.workspaces
|
||||
.iter_mut()
|
||||
.find(|w| &w.workspace_handle == workspace)
|
||||
}) {
|
||||
w.state = state;
|
||||
}
|
||||
}
|
||||
zext_workspace_handle_v1::Event::Remove => {
|
||||
if let Some((g, w_i)) = self.workspace_groups.iter_mut().find_map(|g| {
|
||||
g.workspaces
|
||||
.iter_mut()
|
||||
.position(|w| &w.workspace_handle == workspace)
|
||||
.map(|p| (g, p))
|
||||
}) {
|
||||
g.workspaces.remove(w_i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue