Move Cosmic Applets into new Dir & remove old applets
This commit is contained in:
parent
813e6c0aff
commit
a682b8deb0
134 changed files with 0 additions and 1354 deletions
53
cosmic-applet-workspaces/src/colors.rs
Normal file
53
cosmic-applet-workspaces/src/colors.rs
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
use std::{
|
||||
collections::HashMap,
|
||||
fs::File,
|
||||
io::{BufRead, BufReader},
|
||||
path::PathBuf,
|
||||
};
|
||||
|
||||
fn get_default_color(names: &[&str], is_dark: bool) -> HashMap<String, [f64; 4]> {
|
||||
let css = if is_dark {
|
||||
adw_user_colors_lib::colors::ColorOverrides::dark_default().as_css()
|
||||
} else {
|
||||
adw_user_colors_lib::colors::ColorOverrides::light_default().as_css()
|
||||
};
|
||||
names
|
||||
.iter()
|
||||
.filter_map(|name| {
|
||||
let window_bg_color_pattern = &format!("@define-color {name}");
|
||||
css.rfind(window_bg_color_pattern)
|
||||
.and_then(|i| css.get(i + window_bg_color_pattern.len()..))
|
||||
.and_then(|color_str| {
|
||||
csscolorparser::parse(&color_str.trim().replace(";", "")).ok()
|
||||
})
|
||||
.map(|c| (name.to_string(), c.to_array()))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn get_colors(names: &[&str], path: &PathBuf) -> HashMap<String, [f64; 4]> {
|
||||
let file = match File::open(path) {
|
||||
Ok(f) => f,
|
||||
_ => return Default::default(),
|
||||
};
|
||||
|
||||
BufReader::new(file)
|
||||
.lines()
|
||||
.filter_map(|l| l.ok())
|
||||
.filter_map(|line| {
|
||||
names.iter().find_map(|name| {
|
||||
line.rfind(&format!("@define-color {name}"))
|
||||
.map(|i| (name, i))
|
||||
.and_then(|(name, i)| {
|
||||
line.get(i + format!("@define-color {name}").len()..)
|
||||
.map(|s| (name, s))
|
||||
.and_then(|(name, color_str)| {
|
||||
csscolorparser::parse(&color_str.trim().replace(";", ""))
|
||||
.ok()
|
||||
.map(|c| (name.to_string(), c.to_array()))
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
210
cosmic-applet-workspaces/src/components/app.rs
Normal file
210
cosmic-applet-workspaces/src/components/app.rs
Normal file
|
|
@ -0,0 +1,210 @@
|
|||
use std::cmp::Ordering;
|
||||
use calloop::channel::SyncSender;
|
||||
use cosmic::applet::CosmicAppletHelper;
|
||||
use cosmic::iced::alignment::{Horizontal, Vertical};
|
||||
use cosmic::iced::mouse::{self, ScrollDelta};
|
||||
use cosmic::iced::widget::{column, container, row, text};
|
||||
use cosmic::iced::{
|
||||
executor, subscription, widget::button, window, Application, Command, Event::Mouse, Length,
|
||||
Settings, Subscription,
|
||||
};
|
||||
use cosmic::iced_style::application::{self, Appearance};
|
||||
use cosmic::theme::Button;
|
||||
use cosmic::{Element, Theme};
|
||||
use cosmic_panel_config::PanelAnchor;
|
||||
use cosmic_protocols::workspace::v1::client::zcosmic_workspace_handle_v1;
|
||||
use iced_sctk::application::SurfaceIdWrapper;
|
||||
use iced_sctk::command::platform_specific::wayland::window::SctkWindowSettings;
|
||||
use iced_sctk::settings::InitialSurface;
|
||||
use iced_sctk::{commands, Color};
|
||||
use wayland_backend::client::ObjectId;
|
||||
|
||||
use crate::config;
|
||||
use crate::wayland::{WorkspaceEvent, WorkspaceList};
|
||||
use crate::wayland_subscription::{workspaces, WorkspacesUpdate};
|
||||
|
||||
pub fn run() -> cosmic::iced::Result {
|
||||
let mut settings = Settings::default();
|
||||
settings.initial_surface = InitialSurface::XdgWindow(SctkWindowSettings {
|
||||
iced_settings: cosmic::iced_native::window::Settings {
|
||||
size: (32, 32),
|
||||
..Default::default()
|
||||
},
|
||||
..Default::default()
|
||||
});
|
||||
IcedWorkspacesApplet::run(settings)
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Layout {
|
||||
Row,
|
||||
Column,
|
||||
}
|
||||
|
||||
struct IcedWorkspacesApplet {
|
||||
theme: Theme,
|
||||
workspaces: WorkspaceList,
|
||||
workspace_tx: Option<SyncSender<WorkspaceEvent>>,
|
||||
layout: Layout,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
enum Message {
|
||||
WorkspaceUpdate(WorkspacesUpdate),
|
||||
WorkspacePressed(ObjectId),
|
||||
WheelScrolled(ScrollDelta),
|
||||
Errored,
|
||||
}
|
||||
|
||||
impl Application for IcedWorkspacesApplet {
|
||||
type Message = Message;
|
||||
type Theme = Theme;
|
||||
type Executor = executor::Default;
|
||||
type Flags = ();
|
||||
|
||||
fn new(_flags: ()) -> (Self, Command<Message>) {
|
||||
let applet_helper = CosmicAppletHelper::default();
|
||||
(
|
||||
IcedWorkspacesApplet {
|
||||
layout: match &applet_helper.anchor {
|
||||
PanelAnchor::Left | PanelAnchor::Right => Layout::Column,
|
||||
PanelAnchor::Top | PanelAnchor::Bottom => Layout::Row,
|
||||
},
|
||||
theme: Default::default(),
|
||||
workspaces: Vec::new(),
|
||||
workspace_tx: Default::default(),
|
||||
},
|
||||
Command::none(),
|
||||
)
|
||||
}
|
||||
|
||||
fn title(&self) -> String {
|
||||
config::APP_ID.to_string()
|
||||
}
|
||||
|
||||
fn update(&mut self, message: Message) -> Command<Message> {
|
||||
match message {
|
||||
Message::WorkspaceUpdate(msg) => match msg {
|
||||
WorkspacesUpdate::Workspaces(mut list) => {
|
||||
list.retain(|w| {
|
||||
!matches!(w.1, Some(zcosmic_workspace_handle_v1::State::Hidden))
|
||||
});
|
||||
list.sort_by(|a, b| match a.0.len().cmp(&b.0.len()) {
|
||||
Ordering::Equal => a.0.cmp(&b.0),
|
||||
Ordering::Less => Ordering::Less,
|
||||
Ordering::Greater => Ordering::Greater,
|
||||
});
|
||||
self.workspaces = list;
|
||||
let unit = 32;
|
||||
let (w, h) = match self.layout {
|
||||
Layout::Row => (unit * self.workspaces.len().max(1) as u32, unit),
|
||||
Layout::Column => (unit, unit * self.workspaces.len().max(1) as u32),
|
||||
};
|
||||
return commands::window::resize_window(window::Id::new(0), w, h);
|
||||
}
|
||||
WorkspacesUpdate::Started(tx) => {
|
||||
self.workspace_tx.replace(tx);
|
||||
}
|
||||
WorkspacesUpdate::Errored => {
|
||||
// TODO
|
||||
}
|
||||
},
|
||||
Message::WorkspacePressed(id) => {
|
||||
if let Some(tx) = self.workspace_tx.as_mut() {
|
||||
let _ = tx.try_send(WorkspaceEvent::Activate(id));
|
||||
}
|
||||
}
|
||||
Message::WheelScrolled(delta) => {
|
||||
let delta = match delta {
|
||||
ScrollDelta::Lines { x, y } => x + y,
|
||||
ScrollDelta::Pixels { x, y } => x + y,
|
||||
} as f64;
|
||||
if let Some(tx) = self.workspace_tx.as_mut() {
|
||||
let _ = tx.try_send(WorkspaceEvent::Scroll(delta));
|
||||
}
|
||||
}
|
||||
Message::Errored => {}
|
||||
}
|
||||
Command::none()
|
||||
}
|
||||
|
||||
fn view(&self, _id: SurfaceIdWrapper) -> Element<Message> {
|
||||
if self.workspaces.is_empty() {
|
||||
return row![].padding(8).into();
|
||||
}
|
||||
let buttons = self
|
||||
.workspaces
|
||||
.iter()
|
||||
.filter_map(|w| {
|
||||
let btn = button(
|
||||
text(w.0.clone())
|
||||
.horizontal_alignment(Horizontal::Center)
|
||||
.vertical_alignment(Vertical::Center)
|
||||
.width(Length::Fill)
|
||||
.height(Length::Fill),
|
||||
)
|
||||
.width(Length::Fill)
|
||||
.height(Length::Fill)
|
||||
.on_press(Message::WorkspacePressed(w.2.clone()))
|
||||
.padding(0);
|
||||
Some(
|
||||
btn.style(match w.1 {
|
||||
Some(zcosmic_workspace_handle_v1::State::Active) => Button::Primary,
|
||||
Some(zcosmic_workspace_handle_v1::State::Urgent) => Button::Destructive,
|
||||
None => Button::Secondary,
|
||||
_ => return None,
|
||||
})
|
||||
.into(),
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
let layout_section: Element<_> = match self.layout {
|
||||
Layout::Row => row(buttons)
|
||||
.width(Length::Fill)
|
||||
.height(Length::Fill)
|
||||
.padding(0)
|
||||
.into(),
|
||||
Layout::Column => column(buttons)
|
||||
.width(Length::Fill)
|
||||
.height(Length::Fill)
|
||||
.padding(0)
|
||||
.into(),
|
||||
};
|
||||
|
||||
container(layout_section)
|
||||
.width(Length::Fill)
|
||||
.height(Length::Fill)
|
||||
.padding(0)
|
||||
.into()
|
||||
}
|
||||
|
||||
fn subscription(&self) -> Subscription<Message> {
|
||||
Subscription::batch(
|
||||
vec![
|
||||
workspaces(0).map(|(_, msg)| Message::WorkspaceUpdate(msg)),
|
||||
subscription::events_with(|e, _| match e {
|
||||
Mouse(mouse::Event::WheelScrolled { delta }) => {
|
||||
Some(Message::WheelScrolled(delta))
|
||||
}
|
||||
_ => None,
|
||||
}),
|
||||
]
|
||||
.into_iter(),
|
||||
)
|
||||
}
|
||||
|
||||
fn theme(&self) -> Theme {
|
||||
self.theme
|
||||
}
|
||||
|
||||
fn close_requested(&self, _id: iced_sctk::application::SurfaceIdWrapper) -> Self::Message {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn style(&self) -> <Self::Theme as application::StyleSheet>::Style {
|
||||
<Self::Theme as application::StyleSheet>::Style::Custom(|theme| Appearance {
|
||||
background_color: Color::from_rgba(0.0, 0.0, 0.0, 0.0),
|
||||
text_color: theme.cosmic().on_bg_color().into(),
|
||||
})
|
||||
}
|
||||
}
|
||||
1
cosmic-applet-workspaces/src/components/mod.rs
Normal file
1
cosmic-applet-workspaces/src/components/mod.rs
Normal file
|
|
@ -0,0 +1 @@
|
|||
pub mod app;
|
||||
3
cosmic-applet-workspaces/src/config.rs
Normal file
3
cosmic-applet-workspaces/src/config.rs
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
pub const APP_ID: &str = "com.system76.CosmicWorkspacesApplet";
|
||||
pub const PROFILE: &str = "";
|
||||
pub const VERSION: &str = "0.1.0";
|
||||
47
cosmic-applet-workspaces/src/localize.rs
Normal file
47
cosmic-applet-workspaces/src/localize.rs
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
// SPDX-License-Identifier: MPL-2.0-only
|
||||
|
||||
use i18n_embed::{
|
||||
fluent::{fluent_language_loader, FluentLanguageLoader},
|
||||
DefaultLocalizer, LanguageLoader, Localizer,
|
||||
};
|
||||
use once_cell::sync::Lazy;
|
||||
use rust_embed::RustEmbed;
|
||||
|
||||
#[derive(RustEmbed)]
|
||||
#[folder = "i18n/"]
|
||||
struct Localizations;
|
||||
|
||||
pub static LANGUAGE_LOADER: Lazy<FluentLanguageLoader> = Lazy::new(|| {
|
||||
let loader: FluentLanguageLoader = fluent_language_loader!();
|
||||
|
||||
loader
|
||||
.load_fallback_language(&Localizations)
|
||||
.expect("Error while loading fallback language");
|
||||
|
||||
loader
|
||||
});
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! fl {
|
||||
($message_id:literal) => {{
|
||||
i18n_embed_fl::fl!($crate::localize::LANGUAGE_LOADER, $message_id)
|
||||
}};
|
||||
|
||||
($message_id:literal, $($args:expr),*) => {{
|
||||
i18n_embed_fl::fl!($crate::localize::LANGUAGE_LOADER, $message_id, $($args), *)
|
||||
}};
|
||||
}
|
||||
|
||||
// Get the `Localizer` to be used for localizing this library.
|
||||
pub fn localizer() -> Box<dyn Localizer> {
|
||||
Box::from(DefaultLocalizer::new(&*LANGUAGE_LOADER, &Localizations))
|
||||
}
|
||||
|
||||
pub fn localize() {
|
||||
let localizer = localizer();
|
||||
let requested_languages = i18n_embed::DesktopLanguageRequester::requested_languages();
|
||||
|
||||
if let Err(error) = localizer.select(&requested_languages) {
|
||||
eprintln!("Error while loading language for App List {}", error);
|
||||
}
|
||||
}
|
||||
28
cosmic-applet-workspaces/src/main.rs
Normal file
28
cosmic-applet-workspaces/src/main.rs
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
mod components;
|
||||
#[rustfmt::skip]
|
||||
mod config;
|
||||
mod localize;
|
||||
mod wayland;
|
||||
mod wayland_subscription;
|
||||
|
||||
use config::APP_ID;
|
||||
use log::info;
|
||||
|
||||
use localize::localize;
|
||||
|
||||
use crate::{
|
||||
components::app,
|
||||
config::{PROFILE, VERSION},
|
||||
};
|
||||
|
||||
fn main() -> cosmic::iced::Result {
|
||||
// Initialize logger
|
||||
pretty_env_logger::init();
|
||||
info!("Iced Workspaces Applet ({})", APP_ID);
|
||||
info!("Version: {} ({})", VERSION, PROFILE);
|
||||
|
||||
// Prepare i18n
|
||||
localize();
|
||||
|
||||
app::run()
|
||||
}
|
||||
53
cosmic-applet-workspaces/src/meson.build
Normal file
53
cosmic-applet-workspaces/src/meson.build
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
global_conf = configuration_data()
|
||||
global_conf.set_quoted('APP_ID', application_id)
|
||||
global_conf.set_quoted('PROFILE', profile)
|
||||
global_conf.set_quoted('VERSION', version + version_suffix)
|
||||
config = configure_file(
|
||||
input: 'config.rs.in',
|
||||
output: 'config.rs',
|
||||
configuration: global_conf
|
||||
)
|
||||
# Copy the config.rs output to the source directory.
|
||||
run_command(
|
||||
'cp',
|
||||
meson.project_build_root() / 'src' / 'config.rs',
|
||||
meson.project_source_root() / 'src' / 'config.rs',
|
||||
check: true
|
||||
)
|
||||
|
||||
cargo_options = [ '--manifest-path', meson.project_source_root() / 'Cargo.toml' ]
|
||||
cargo_options += [ '--target-dir', meson.project_build_root() / 'src' ]
|
||||
|
||||
if get_option('profile') == 'default'
|
||||
cargo_options += [ '--release' ]
|
||||
rust_target = 'release'
|
||||
message('Building in release mode')
|
||||
else
|
||||
rust_target = 'debug'
|
||||
message('Building in debug mode')
|
||||
endif
|
||||
|
||||
if get_option('vendor') == true
|
||||
cargo_options += [ '--locked' ]
|
||||
message('Building with vendoring')
|
||||
endif
|
||||
|
||||
cargo_env = [ 'CARGO_HOME=' + meson.project_build_root() / 'cargo-home' ]
|
||||
|
||||
cargo_build = custom_target(
|
||||
'cargo-build',
|
||||
build_by_default: true,
|
||||
build_always_stale: true,
|
||||
output: meson.project_name(),
|
||||
console: true,
|
||||
install: true,
|
||||
install_dir: bindir,
|
||||
command: [
|
||||
'env',
|
||||
cargo_env,
|
||||
cargo, 'build',
|
||||
cargo_options,
|
||||
'&&',
|
||||
'cp', 'src' / rust_target / meson.project_name(), '@OUTPUT@',
|
||||
]
|
||||
)
|
||||
448
cosmic-applet-workspaces/src/wayland.rs
Normal file
448
cosmic-applet-workspaces/src/wayland.rs
Normal file
|
|
@ -0,0 +1,448 @@
|
|||
use calloop::channel::*;
|
||||
use cosmic_panel_config::CosmicPanelOuput;
|
||||
use cosmic_protocols::workspace::v1::client::{
|
||||
zcosmic_workspace_group_handle_v1::{self, ZcosmicWorkspaceGroupHandleV1},
|
||||
zcosmic_workspace_handle_v1::{self, ZcosmicWorkspaceHandleV1},
|
||||
zcosmic_workspace_manager_v1::{self, ZcosmicWorkspaceManagerV1},
|
||||
};
|
||||
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, Proxy,
|
||||
};
|
||||
use wayland_client::{Connection, Dispatch, QueueHandle};
|
||||
|
||||
#[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")
|
||||
.map_err(anyhow::Error::msg)
|
||||
.map(|display_str| {
|
||||
let mut socket_path = env::var_os("XDG_RUNTIME_DIR")
|
||||
.map(Into::<PathBuf>::into)
|
||||
.ok_or(ConnectError::NoCompositor)?;
|
||||
socket_path.push(display_str);
|
||||
|
||||
Ok(UnixStream::connect(socket_path).map_err(|_| ConnectError::NoCompositor)?)
|
||||
})
|
||||
.and_then(|s| s.map(|s| Connection::from_socket(s).map_err(anyhow::Error::msg)))
|
||||
{
|
||||
std::thread::spawn(move || {
|
||||
let output = std::env::var("COSMIC_PANEL_OUTPUT")
|
||||
.ok()
|
||||
.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>();
|
||||
let qhandle = event_queue.handle();
|
||||
|
||||
WaylandSource::new(event_queue)
|
||||
.expect("Failed to create wayland source")
|
||||
.insert(loop_handle)
|
||||
.unwrap();
|
||||
|
||||
let display = conn.display();
|
||||
display.get_registry(&qhandle, ());
|
||||
|
||||
let mut state = State {
|
||||
outputs_to_handle: Default::default(),
|
||||
wm_name: None,
|
||||
workspace_manager: None,
|
||||
workspace_groups: Vec::new(),
|
||||
configured_output: output,
|
||||
expected_output: None,
|
||||
tx,
|
||||
running: true,
|
||||
};
|
||||
let loop_handle = event_loop.handle();
|
||||
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.workspace_handle.id() == id)
|
||||
}) {
|
||||
w.workspace_handle.activate();
|
||||
state.workspace_manager.as_ref().unwrap().commit();
|
||||
}
|
||||
}
|
||||
Event::Msg(WorkspaceEvent::Scroll(v)) => {
|
||||
if let Some((w_g, w_i)) = state.workspace_groups.iter().find_map(|g| {
|
||||
if g.output != state.expected_output {
|
||||
return None;
|
||||
}
|
||||
g.workspaces
|
||||
.iter()
|
||||
.position(|w| {
|
||||
w.states
|
||||
.contains(&zcosmic_workspace_handle_v1::State::Active)
|
||||
})
|
||||
.map(|w_i| (g, w_i))
|
||||
}) {
|
||||
let max_w = w_g.workspaces.len().wrapping_sub(1);
|
||||
let d_i = if v > 0.0 {
|
||||
if w_i == max_w {
|
||||
0
|
||||
} else {
|
||||
w_i.wrapping_add(1)
|
||||
}
|
||||
} else {
|
||||
if w_i == 0 {
|
||||
max_w
|
||||
} else {
|
||||
w_i.wrapping_sub(1)
|
||||
}
|
||||
};
|
||||
if let Some(w) = w_g.workspaces.get(d_i) {
|
||||
w.workspace_handle.activate();
|
||||
state.workspace_manager.as_ref().unwrap().commit();
|
||||
}
|
||||
}
|
||||
}
|
||||
Event::Closed => {
|
||||
if let Some(workspace_manager) = &mut state.workspace_manager {
|
||||
for g in &mut state.workspace_groups {
|
||||
g.workspace_group_handle.destroy();
|
||||
}
|
||||
workspace_manager.stop();
|
||||
}
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
while state.running {
|
||||
event_loop
|
||||
.dispatch(Duration::from_millis(16), &mut state)
|
||||
.unwrap();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
eprintln!("ENV variable WAYLAND_DISPLAY is missing. Exiting...");
|
||||
std::process::exit(1);
|
||||
}
|
||||
|
||||
workspaces_tx
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct State {
|
||||
outputs_to_handle: Option<Vec<WlOutput>>,
|
||||
wm_name: Option<(u32, WlRegistry)>,
|
||||
running: bool,
|
||||
tx: mpsc::Sender<WorkspaceList>,
|
||||
configured_output: String,
|
||||
expected_output: Option<WlOutput>,
|
||||
workspace_manager: Option<ZcosmicWorkspaceManagerV1>,
|
||||
workspace_groups: Vec<WorkspaceGroup>,
|
||||
}
|
||||
|
||||
impl State {
|
||||
// XXX
|
||||
pub fn workspace_list(
|
||||
&self,
|
||||
) -> Vec<(String, Option<zcosmic_workspace_handle_v1::State>, ObjectId)> {
|
||||
self.workspace_groups
|
||||
.iter()
|
||||
.filter_map(|g| {
|
||||
// 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) => {
|
||||
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 {
|
||||
None
|
||||
}
|
||||
})
|
||||
.flatten()
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct WorkspaceGroup {
|
||||
workspace_group_handle: ZcosmicWorkspaceGroupHandleV1,
|
||||
output: Option<WlOutput>,
|
||||
workspaces: Vec<Workspace>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
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,
|
||||
registry: &wl_registry::WlRegistry,
|
||||
event: wl_registry::Event,
|
||||
_: &(),
|
||||
_: &Connection,
|
||||
qh: &QueueHandle<Self>,
|
||||
) {
|
||||
if let wl_registry::Event::Global {
|
||||
name,
|
||||
interface,
|
||||
version: _version,
|
||||
} = event
|
||||
{
|
||||
match &interface[..] {
|
||||
"zcosmic_workspace_manager_v1" => {
|
||||
if let Some(outputs_to_handle) = state.outputs_to_handle.as_ref() {
|
||||
if outputs_to_handle.is_empty() {
|
||||
let workspace_manager =
|
||||
registry.bind::<ZcosmicWorkspaceManagerV1, _, _>(name, 1, qh, ());
|
||||
state.workspace_manager = Some(workspace_manager);
|
||||
return;
|
||||
}
|
||||
}
|
||||
// will be handled when outputs are done...
|
||||
state.wm_name.replace((name, registry.clone()));
|
||||
}
|
||||
"wl_output" => {
|
||||
let _output = registry.bind::<WlOutput, _, _>(name, 4, qh, ());
|
||||
match state.outputs_to_handle.as_mut() {
|
||||
Some(outputs_to_handle) => outputs_to_handle.push(_output),
|
||||
None => {
|
||||
state.outputs_to_handle.replace(vec![_output]);
|
||||
}
|
||||
};
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Dispatch<ZcosmicWorkspaceManagerV1, ()> for State {
|
||||
fn event(
|
||||
state: &mut Self,
|
||||
_: &ZcosmicWorkspaceManagerV1,
|
||||
event: zcosmic_workspace_manager_v1::Event,
|
||||
_: &(),
|
||||
_: &Connection,
|
||||
_: &QueueHandle<Self>,
|
||||
) {
|
||||
match event {
|
||||
zcosmic_workspace_manager_v1::Event::WorkspaceGroup { workspace_group } => {
|
||||
state.workspace_groups.push(WorkspaceGroup {
|
||||
workspace_group_handle: workspace_group,
|
||||
output: None,
|
||||
workspaces: Vec::new(),
|
||||
});
|
||||
}
|
||||
zcosmic_workspace_manager_v1::Event::Done => {
|
||||
for group in &mut state.workspace_groups {
|
||||
group.workspaces.sort_by(|w1, w2| {
|
||||
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 _ = block_on(state.tx.send(state.workspace_list()));
|
||||
}
|
||||
zcosmic_workspace_manager_v1::Event::Finished => {
|
||||
state.workspace_manager.take();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
event_created_child!(State, ZcosmicWorkspaceManagerV1, [
|
||||
0 => (ZcosmicWorkspaceGroupHandleV1, ())
|
||||
]);
|
||||
}
|
||||
|
||||
impl Dispatch<ZcosmicWorkspaceGroupHandleV1, ()> for State {
|
||||
fn event(
|
||||
state: &mut Self,
|
||||
group: &ZcosmicWorkspaceGroupHandleV1,
|
||||
event: zcosmic_workspace_group_handle_v1::Event,
|
||||
_: &(),
|
||||
_: &Connection,
|
||||
_: &QueueHandle<Self>,
|
||||
) {
|
||||
match event {
|
||||
zcosmic_workspace_group_handle_v1::Event::OutputEnter { output } => {
|
||||
if let Some(group) = state
|
||||
.workspace_groups
|
||||
.iter_mut()
|
||||
.find(|g| &g.workspace_group_handle == group)
|
||||
{
|
||||
group.output = Some(output);
|
||||
}
|
||||
}
|
||||
zcosmic_workspace_group_handle_v1::Event::OutputLeave { output } => {
|
||||
if let Some(group) = state.workspace_groups.iter_mut().find(|g| {
|
||||
&g.workspace_group_handle == group && g.output.as_ref() == Some(&output)
|
||||
}) {
|
||||
group.output = None;
|
||||
}
|
||||
}
|
||||
zcosmic_workspace_group_handle_v1::Event::Workspace { workspace } => {
|
||||
if let Some(group) = state
|
||||
.workspace_groups
|
||||
.iter_mut()
|
||||
.find(|g| &g.workspace_group_handle == group)
|
||||
{
|
||||
group.workspaces.push(Workspace {
|
||||
workspace_handle: workspace,
|
||||
name: String::new(),
|
||||
coordinates: Vec::new(),
|
||||
states: Vec::new(),
|
||||
})
|
||||
}
|
||||
}
|
||||
zcosmic_workspace_group_handle_v1::Event::Remove => {
|
||||
if let Some(group) = state
|
||||
.workspace_groups
|
||||
.iter()
|
||||
.position(|g| &g.workspace_group_handle == group)
|
||||
{
|
||||
state.workspace_groups.remove(group);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
event_created_child!(State, ZcosmicWorkspaceGroupHandleV1, [
|
||||
3 => (ZcosmicWorkspaceHandleV1, ())
|
||||
]);
|
||||
}
|
||||
|
||||
impl Dispatch<ZcosmicWorkspaceHandleV1, ()> for State {
|
||||
fn event(
|
||||
state: &mut Self,
|
||||
workspace: &ZcosmicWorkspaceHandleV1,
|
||||
event: zcosmic_workspace_handle_v1::Event,
|
||||
_: &(),
|
||||
_: &Connection,
|
||||
_: &QueueHandle<Self>,
|
||||
) {
|
||||
match event {
|
||||
zcosmic_workspace_handle_v1::Event::Name { name } => {
|
||||
if let Some(w) = state.workspace_groups.iter_mut().find_map(|g| {
|
||||
g.workspaces
|
||||
.iter_mut()
|
||||
.find(|w| &w.workspace_handle == workspace)
|
||||
}) {
|
||||
w.name = name;
|
||||
}
|
||||
}
|
||||
zcosmic_workspace_handle_v1::Event::Coordinates { coordinates } => {
|
||||
if let Some(w) = state.workspace_groups.iter_mut().find_map(|g| {
|
||||
g.workspaces
|
||||
.iter_mut()
|
||||
.find(|w| &w.workspace_handle == workspace)
|
||||
}) {
|
||||
// wayland is host byte order
|
||||
w.coordinates = coordinates
|
||||
.chunks(4)
|
||||
.map(|chunk| u32::from_ne_bytes(chunk.try_into().unwrap()))
|
||||
.collect();
|
||||
}
|
||||
}
|
||||
zcosmic_workspace_handle_v1::Event::State {
|
||||
state: workspace_state,
|
||||
} => {
|
||||
if let Some(w) = state.workspace_groups.iter_mut().find_map(|g| {
|
||||
g.workspaces
|
||||
.iter_mut()
|
||||
.find(|w| &w.workspace_handle == workspace)
|
||||
}) {
|
||||
// wayland is host byte order
|
||||
w.states = workspace_state
|
||||
.chunks(4)
|
||||
.map(|chunk| {
|
||||
zcosmic_workspace_handle_v1::State::try_from(u32::from_ne_bytes(
|
||||
chunk.try_into().unwrap(),
|
||||
))
|
||||
.unwrap()
|
||||
})
|
||||
.collect();
|
||||
}
|
||||
}
|
||||
zcosmic_workspace_handle_v1::Event::Remove => {
|
||||
if let Some((g, w_i)) = state.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);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Dispatch<WlOutput, ()> for State {
|
||||
fn event(
|
||||
state: &mut Self,
|
||||
o: &WlOutput,
|
||||
e: wl_output::Event,
|
||||
_: &(),
|
||||
_: &Connection,
|
||||
qh: &QueueHandle<Self>,
|
||||
) {
|
||||
match e {
|
||||
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 _ = block_on(state.tx.send(state.workspace_list()));
|
||||
}
|
||||
wl_output::Event::Done => {
|
||||
let outputs_to_handle = state.outputs_to_handle.as_mut().unwrap();
|
||||
outputs_to_handle.retain(|o_to_handle| o != o_to_handle);
|
||||
if outputs_to_handle.is_empty() {
|
||||
if let Some((wm_name, registry)) = state.wm_name.as_ref() {
|
||||
let workspace_manager =
|
||||
registry.bind::<ZcosmicWorkspaceManagerV1, _, _>(*wm_name, 1, qh, ());
|
||||
state.workspace_manager = Some(workspace_manager);
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {} // ignored
|
||||
}
|
||||
}
|
||||
}
|
||||
72
cosmic-applet-workspaces/src/wayland_subscription.rs
Normal file
72
cosmic-applet-workspaces/src/wayland_subscription.rs
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
use crate::wayland::{self, WorkspaceEvent, WorkspaceList};
|
||||
use calloop::channel::SyncSender;
|
||||
use futures::{channel::mpsc, StreamExt};
|
||||
use std::hash::Hash;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum WorkspacesUpdate {
|
||||
Workspaces(WorkspaceList),
|
||||
Started(SyncSender<WorkspaceEvent>),
|
||||
Errored,
|
||||
}
|
||||
|
||||
pub fn workspaces<I: 'static + Hash + Copy + Send + Sync>(
|
||||
id: I,
|
||||
) -> cosmic::iced::Subscription<(I, WorkspacesUpdate)> {
|
||||
use cosmic::iced::subscription;
|
||||
|
||||
subscription::unfold(id, State::Ready, move |state| _workspaces(id, state))
|
||||
}
|
||||
|
||||
async fn _workspaces<I: Copy>(id: I, state: State) -> (Option<(I, WorkspacesUpdate)>, State) {
|
||||
match state {
|
||||
State::Ready => {
|
||||
if let Ok(watcher) = WorkspacesWatcher::new() {
|
||||
(
|
||||
Some((id, WorkspacesUpdate::Started(watcher.get_sender()))),
|
||||
State::Waiting(watcher),
|
||||
)
|
||||
} else {
|
||||
(Some((id, WorkspacesUpdate::Errored)), State::Error)
|
||||
}
|
||||
}
|
||||
State::Waiting(mut t) => {
|
||||
if let Some(w) = t.workspaces().await {
|
||||
(
|
||||
Some((id, WorkspacesUpdate::Workspaces(w))),
|
||||
State::Waiting(t),
|
||||
)
|
||||
} else {
|
||||
(Some((id, WorkspacesUpdate::Errored)), State::Error)
|
||||
}
|
||||
}
|
||||
State::Error => cosmic::iced::futures::future::pending().await,
|
||||
}
|
||||
}
|
||||
|
||||
pub enum State {
|
||||
Ready,
|
||||
Waiting(WorkspacesWatcher),
|
||||
Error,
|
||||
}
|
||||
|
||||
pub struct WorkspacesWatcher {
|
||||
rx: mpsc::Receiver<WorkspaceList>,
|
||||
tx: SyncSender<WorkspaceEvent>,
|
||||
}
|
||||
|
||||
impl WorkspacesWatcher {
|
||||
pub fn new() -> anyhow::Result<Self> {
|
||||
let (tx, rx) = mpsc::channel(20);
|
||||
let tx = wayland::spawn_workspaces(tx);
|
||||
Ok(Self { tx, rx })
|
||||
}
|
||||
|
||||
pub fn get_sender(&self) -> SyncSender<WorkspaceEvent> {
|
||||
self.tx.clone()
|
||||
}
|
||||
|
||||
pub async fn workspaces(&mut self) -> Option<WorkspaceList> {
|
||||
self.rx.next().await
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue