2023-07-25 14:13:05 -04:00
|
|
|
use cctk::sctk::reexports::{calloop::channel::SyncSender, client::backend::ObjectId};
|
2023-08-03 13:22:17 -07:00
|
|
|
use cosmic::app::{applet::cosmic_panel_config::PanelAnchor, Command};
|
2022-11-29 16:52:31 -05:00
|
|
|
use cosmic::iced::alignment::{Horizontal, Vertical};
|
|
|
|
|
use cosmic::iced::mouse::{self, ScrollDelta};
|
|
|
|
|
use cosmic::iced::widget::{column, container, row, text};
|
2023-08-03 13:22:17 -07:00
|
|
|
use cosmic::iced::{subscription, widget::button, Event::Mouse, Length, Subscription};
|
|
|
|
|
use cosmic::iced_style::application;
|
2022-11-29 16:52:31 -05:00
|
|
|
use cosmic::{Element, Theme};
|
2023-08-03 13:22:17 -07:00
|
|
|
|
2022-11-29 16:52:31 -05:00
|
|
|
use cosmic_protocols::workspace::v1::client::zcosmic_workspace_handle_v1;
|
2022-12-27 18:35:06 -05:00
|
|
|
use std::cmp::Ordering;
|
2022-11-29 16:52:31 -05:00
|
|
|
|
|
|
|
|
use crate::config;
|
|
|
|
|
use crate::wayland::{WorkspaceEvent, WorkspaceList};
|
|
|
|
|
use crate::wayland_subscription::{workspaces, WorkspacesUpdate};
|
|
|
|
|
|
|
|
|
|
pub fn run() -> cosmic::iced::Result {
|
2023-08-03 13:22:17 -07:00
|
|
|
cosmic::app::applet::run::<IcedWorkspacesApplet>(true, ())
|
2022-11-29 16:52:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
|
pub enum Layout {
|
|
|
|
|
Row,
|
|
|
|
|
Column,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct IcedWorkspacesApplet {
|
2023-08-03 13:22:17 -07:00
|
|
|
core: cosmic::app::Core,
|
2022-11-29 16:52:31 -05:00
|
|
|
workspaces: WorkspaceList,
|
|
|
|
|
workspace_tx: Option<SyncSender<WorkspaceEvent>>,
|
|
|
|
|
layout: Layout,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
enum Message {
|
|
|
|
|
WorkspaceUpdate(WorkspacesUpdate),
|
|
|
|
|
WorkspacePressed(ObjectId),
|
|
|
|
|
WheelScrolled(ScrollDelta),
|
|
|
|
|
Errored,
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-03 13:22:17 -07:00
|
|
|
impl cosmic::Application for IcedWorkspacesApplet {
|
2022-11-29 16:52:31 -05:00
|
|
|
type Message = Message;
|
2023-01-18 16:51:30 -08:00
|
|
|
type Executor = cosmic::SingleThreadExecutor;
|
2022-11-29 16:52:31 -05:00
|
|
|
type Flags = ();
|
2023-08-03 13:22:17 -07:00
|
|
|
const APP_ID: &'static str = config::APP_ID;
|
2022-11-29 16:52:31 -05:00
|
|
|
|
2023-08-03 13:22:17 -07:00
|
|
|
fn init(core: cosmic::app::Core, _flags: ()) -> (Self, Command<Message>) {
|
2022-11-29 16:52:31 -05:00
|
|
|
(
|
|
|
|
|
IcedWorkspacesApplet {
|
2023-08-03 13:22:17 -07:00
|
|
|
layout: match &core.applet_helper.anchor {
|
2022-11-29 16:52:31 -05:00
|
|
|
PanelAnchor::Left | PanelAnchor::Right => Layout::Column,
|
|
|
|
|
PanelAnchor::Top | PanelAnchor::Bottom => Layout::Row,
|
|
|
|
|
},
|
2023-08-03 13:22:17 -07:00
|
|
|
core,
|
2022-11-29 16:52:31 -05:00
|
|
|
workspaces: Vec::new(),
|
|
|
|
|
workspace_tx: Default::default(),
|
|
|
|
|
},
|
|
|
|
|
Command::none(),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-03 13:22:17 -07:00
|
|
|
fn core(&self) -> &cosmic::app::Core {
|
|
|
|
|
&self.core
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn core_mut(&mut self) -> &mut cosmic::app::Core {
|
|
|
|
|
&mut self.core
|
2022-11-29 16:52:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-03 13:22:17 -07:00
|
|
|
fn view(&self) -> Element<Message> {
|
2022-12-07 12:46:54 -05:00
|
|
|
if self.workspaces.is_empty() {
|
|
|
|
|
return row![].padding(8).into();
|
|
|
|
|
}
|
2022-11-29 16:52:31 -05:00
|
|
|
let buttons = self
|
|
|
|
|
.workspaces
|
|
|
|
|
.iter()
|
|
|
|
|
.filter_map(|w| {
|
|
|
|
|
let btn = button(
|
|
|
|
|
text(w.0.clone())
|
2023-06-01 12:23:12 -04:00
|
|
|
.size(14)
|
2022-11-29 16:52:31 -05:00
|
|
|
.horizontal_alignment(Horizontal::Center)
|
|
|
|
|
.vertical_alignment(Vertical::Center)
|
|
|
|
|
.width(Length::Fill)
|
|
|
|
|
.height(Length::Fill),
|
|
|
|
|
)
|
2023-08-03 13:22:17 -07:00
|
|
|
.width(Length::Fixed(
|
|
|
|
|
self.core.applet_helper.suggested_size().0 as f32 + 16.0,
|
|
|
|
|
))
|
|
|
|
|
.height(Length::Fixed(
|
|
|
|
|
self.core.applet_helper.suggested_size().0 as f32 + 16.0,
|
|
|
|
|
))
|
2022-11-29 16:52:31 -05:00
|
|
|
.on_press(Message::WorkspacePressed(w.2.clone()))
|
|
|
|
|
.padding(0);
|
|
|
|
|
Some(
|
|
|
|
|
btn.style(match w.1 {
|
2023-09-18 00:24:21 -07:00
|
|
|
Some(zcosmic_workspace_handle_v1::State::Active) => {
|
|
|
|
|
cosmic::theme::iced::Button::Primary
|
|
|
|
|
}
|
|
|
|
|
Some(zcosmic_workspace_handle_v1::State::Urgent) => {
|
|
|
|
|
cosmic::theme::iced::Button::Destructive
|
|
|
|
|
}
|
|
|
|
|
None => cosmic::theme::iced::Button::Secondary,
|
2022-11-29 16:52:31 -05:00
|
|
|
_ => return None,
|
|
|
|
|
})
|
|
|
|
|
.into(),
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
let layout_section: Element<_> = match self.layout {
|
|
|
|
|
Layout::Row => row(buttons)
|
2023-06-16 20:37:02 -04:00
|
|
|
.width(Length::Shrink)
|
|
|
|
|
.height(Length::Shrink)
|
2022-11-29 16:52:31 -05:00
|
|
|
.padding(0)
|
|
|
|
|
.into(),
|
|
|
|
|
Layout::Column => column(buttons)
|
2023-06-16 20:37:02 -04:00
|
|
|
.width(Length::Shrink)
|
|
|
|
|
.height(Length::Shrink)
|
2022-11-29 16:52:31 -05:00
|
|
|
.padding(0)
|
|
|
|
|
.into(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
container(layout_section)
|
2023-06-16 20:37:02 -04:00
|
|
|
.width(Length::Shrink)
|
|
|
|
|
.height(Length::Shrink)
|
2022-11-29 16:52:31 -05:00
|
|
|
.padding(0)
|
|
|
|
|
.into()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn subscription(&self) -> Subscription<Message> {
|
|
|
|
|
Subscription::batch(
|
|
|
|
|
vec![
|
2023-07-11 15:23:27 -04:00
|
|
|
workspaces(0).map(Message::WorkspaceUpdate),
|
2022-11-29 16:52:31 -05:00
|
|
|
subscription::events_with(|e, _| match e {
|
|
|
|
|
Mouse(mouse::Event::WheelScrolled { delta }) => {
|
|
|
|
|
Some(Message::WheelScrolled(delta))
|
|
|
|
|
}
|
|
|
|
|
_ => None,
|
|
|
|
|
}),
|
|
|
|
|
]
|
|
|
|
|
.into_iter(),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-03 13:22:17 -07:00
|
|
|
fn style(&self) -> Option<<Theme as application::StyleSheet>::Style> {
|
|
|
|
|
Some(cosmic::app::applet::style())
|
2022-11-29 16:52:31 -05:00
|
|
|
}
|
|
|
|
|
}
|