refactor: use iced workspaces applet
This commit is contained in:
parent
67869ba5ac
commit
a7b099b4b3
27 changed files with 3958 additions and 999 deletions
|
|
@ -1,4 +1,3 @@
|
|||
use crate::{utils::WorkspaceEvent, wayland_source::WaylandSource};
|
||||
use calloop::channel::*;
|
||||
use cosmic_panel_config::CosmicPanelOuput;
|
||||
use cosmic_protocols::workspace::v1::client::{
|
||||
|
|
@ -6,19 +5,28 @@ use cosmic_protocols::workspace::v1::client::{
|
|||
zcosmic_workspace_handle_v1::{self, ZcosmicWorkspaceHandleV1},
|
||||
zcosmic_workspace_manager_v1::{self, ZcosmicWorkspaceManagerV1},
|
||||
};
|
||||
use gtk4::glib;
|
||||
use std::{env, os::unix::net::UnixStream, path::PathBuf, time::Duration};
|
||||
use futures::{channel::mpsc, executor::block_on, SinkExt};
|
||||
use sctk::event_loop::WaylandSource;
|
||||
use std::{env, os::unix::net::UnixStream, path::PathBuf, str::FromStr, time::Duration};
|
||||
use wayland_backend::client::ObjectId;
|
||||
use wayland_client::{
|
||||
event_created_child,
|
||||
protocol::{
|
||||
wl_output::{self, WlOutput},
|
||||
wl_registry::{self, WlRegistry},
|
||||
},
|
||||
ConnectError,
|
||||
ConnectError, Proxy,
|
||||
};
|
||||
use wayland_client::{Connection, Dispatch, QueueHandle};
|
||||
|
||||
pub fn spawn_workspaces(tx: glib::Sender<State>) -> SyncSender<WorkspaceEvent> {
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum WorkspaceEvent {
|
||||
Activate(ObjectId),
|
||||
Scroll(f64),
|
||||
}
|
||||
pub type WorkspaceList = Vec<(String, Option<zcosmic_workspace_handle_v1::State>, ObjectId)>;
|
||||
|
||||
pub fn spawn_workspaces(tx: mpsc::Sender<WorkspaceList>) -> SyncSender<WorkspaceEvent> {
|
||||
let (workspaces_tx, workspaces_rx) = calloop::channel::sync_channel(100);
|
||||
|
||||
if let Ok(Ok(conn)) = std::env::var("WAYLAND_DISPLAY")
|
||||
|
|
@ -36,13 +44,11 @@ pub fn spawn_workspaces(tx: glib::Sender<State>) -> SyncSender<WorkspaceEvent> {
|
|||
std::thread::spawn(move || {
|
||||
let output = std::env::var("COSMIC_PANEL_OUTPUT")
|
||||
.ok()
|
||||
.and_then(|output| match output.parse::<CosmicPanelOuput>() {
|
||||
Ok(CosmicPanelOuput::Name(n)) => Some(n),
|
||||
// TODO handle Active & panic if the space is still configured for All instead of being assigned a named output
|
||||
_ => Some("".to_string()),
|
||||
.map(|output_str| match CosmicPanelOuput::from_str(&output_str) {
|
||||
Ok(CosmicPanelOuput::Name(name)) => name,
|
||||
_ => "".to_string(),
|
||||
})
|
||||
.unwrap_or_default();
|
||||
|
||||
let mut event_loop = calloop::EventLoop::<State>::try_new().unwrap();
|
||||
let loop_handle = event_loop.handle();
|
||||
let event_queue = conn.new_event_queue::<State>();
|
||||
|
|
@ -70,11 +76,9 @@ pub fn spawn_workspaces(tx: glib::Sender<State>) -> SyncSender<WorkspaceEvent> {
|
|||
loop_handle
|
||||
.insert_source(workspaces_rx, |e, _, state| match e {
|
||||
Event::Msg(WorkspaceEvent::Activate(id)) => {
|
||||
if let Some(w) = state
|
||||
.workspace_groups
|
||||
.iter()
|
||||
.find_map(|g| g.workspaces.iter().find(|w| w.name == id))
|
||||
{
|
||||
if let Some(w) = state.workspace_groups.iter().find_map(|g| {
|
||||
g.workspaces.iter().find(|w| w.workspace_handle.id() == id)
|
||||
}) {
|
||||
w.workspace_handle.activate();
|
||||
state.workspace_manager.as_ref().unwrap().commit();
|
||||
}
|
||||
|
|
@ -141,7 +145,7 @@ pub struct State {
|
|||
outputs_to_handle: Option<Vec<WlOutput>>,
|
||||
wm_name: Option<(u32, WlRegistry)>,
|
||||
running: bool,
|
||||
tx: glib::Sender<State>,
|
||||
tx: mpsc::Sender<WorkspaceList>,
|
||||
configured_output: String,
|
||||
expected_output: Option<WlOutput>,
|
||||
workspace_manager: Option<ZcosmicWorkspaceManagerV1>,
|
||||
|
|
@ -150,20 +154,30 @@ pub struct State {
|
|||
|
||||
impl State {
|
||||
// XXX
|
||||
pub fn workspace_list(&self) -> impl Iterator<Item = (String, u32)> + '_ {
|
||||
pub fn workspace_list(
|
||||
&self,
|
||||
) -> Vec<(String, Option<zcosmic_workspace_handle_v1::State>, ObjectId)> {
|
||||
self.workspace_groups
|
||||
.iter()
|
||||
.filter_map(|g| {
|
||||
if g.output == self.expected_output {
|
||||
// TODO remove none check when workspace groups receive output event
|
||||
if g.output.is_none() || g.output == self.expected_output {
|
||||
Some(g.workspaces.iter().map(|w| {
|
||||
(
|
||||
w.name.clone(),
|
||||
match &w.states {
|
||||
x if x.contains(&zcosmic_workspace_handle_v1::State::Active) => 0,
|
||||
x if x.contains(&zcosmic_workspace_handle_v1::State::Urgent) => 1,
|
||||
x if x.contains(&zcosmic_workspace_handle_v1::State::Hidden) => 2,
|
||||
_ => 3,
|
||||
x if x.contains(&zcosmic_workspace_handle_v1::State::Active) => {
|
||||
Some(zcosmic_workspace_handle_v1::State::Active)
|
||||
}
|
||||
x if x.contains(&zcosmic_workspace_handle_v1::State::Urgent) => {
|
||||
Some(zcosmic_workspace_handle_v1::State::Urgent)
|
||||
}
|
||||
x if x.contains(&zcosmic_workspace_handle_v1::State::Hidden) => {
|
||||
Some(zcosmic_workspace_handle_v1::State::Hidden)
|
||||
}
|
||||
_ => None,
|
||||
},
|
||||
w.workspace_handle.id(),
|
||||
)
|
||||
}))
|
||||
} else {
|
||||
|
|
@ -171,6 +185,7 @@ impl State {
|
|||
}
|
||||
})
|
||||
.flatten()
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -182,14 +197,13 @@ struct WorkspaceGroup {
|
|||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct Workspace {
|
||||
pub struct Workspace {
|
||||
workspace_handle: ZcosmicWorkspaceHandleV1,
|
||||
name: String,
|
||||
coordinates: Vec<u32>,
|
||||
states: Vec<zcosmic_workspace_handle_v1::State>,
|
||||
}
|
||||
|
||||
|
||||
impl Dispatch<wl_registry::WlRegistry, ()> for State {
|
||||
fn event(
|
||||
state: &mut Self,
|
||||
|
|
@ -256,13 +270,14 @@ impl Dispatch<ZcosmicWorkspaceManagerV1, ()> for State {
|
|||
w1.coordinates
|
||||
.iter()
|
||||
.zip(w2.coordinates.iter())
|
||||
.rev()
|
||||
.skip_while(|(coord1, coord2)| coord1 == coord2)
|
||||
.next()
|
||||
.map(|(coord1, coord2)| coord1.cmp(coord2))
|
||||
.unwrap_or(std::cmp::Ordering::Equal)
|
||||
});
|
||||
}
|
||||
let _ = state.tx.send(state.clone());
|
||||
let _ = block_on(state.tx.send(state.workspace_list()));
|
||||
}
|
||||
zcosmic_workspace_manager_v1::Event::Finished => {
|
||||
state.workspace_manager.take();
|
||||
|
|
@ -414,7 +429,7 @@ impl Dispatch<WlOutput, ()> for State {
|
|||
wl_output::Event::Name { name } if name == state.configured_output => {
|
||||
state.expected_output.replace(o.clone());
|
||||
// Necessary bc often the output is handled after the workspaces
|
||||
let _ = state.tx.send(state.clone());
|
||||
let _ = block_on(state.tx.send(state.workspace_list()));
|
||||
}
|
||||
wl_output::Event::Done => {
|
||||
let outputs_to_handle = state.outputs_to_handle.as_mut().unwrap();
|
||||
|
|
@ -430,4 +445,4 @@ impl Dispatch<WlOutput, ()> for State {
|
|||
_ => {} // ignored
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue