2024-05-06 15:39:04 +02:00
|
|
|
// Copyright 2023 System76 <info@system76.com>
|
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
|
|
2023-07-25 14:13:05 -04:00
|
|
|
use cctk::sctk::reexports::{calloop::channel::SyncSender, client::backend::ObjectId};
|
2024-07-09 15:17:44 +02:00
|
|
|
use cosmic::{
|
|
|
|
|
applet::cosmic_panel_config::PanelAnchor,
|
|
|
|
|
iced::{
|
|
|
|
|
alignment::{Horizontal, Vertical},
|
|
|
|
|
event,
|
|
|
|
|
mouse::{self, ScrollDelta},
|
|
|
|
|
widget::{button, column, row},
|
|
|
|
|
Event::Mouse,
|
|
|
|
|
Length, Subscription,
|
|
|
|
|
},
|
|
|
|
|
iced_core::{Background, Border},
|
|
|
|
|
iced_style::application,
|
|
|
|
|
widget::{container, horizontal_space, vertical_space},
|
|
|
|
|
Command, 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
|
|
|
|
2024-07-09 15:17:44 +02:00
|
|
|
use crate::{
|
|
|
|
|
config,
|
|
|
|
|
wayland::{WorkspaceEvent, WorkspaceList},
|
|
|
|
|
wayland_subscription::{workspaces, WorkspacesUpdate},
|
|
|
|
|
};
|
2022-11-29 16:52:31 -05:00
|
|
|
|
2024-04-09 15:33:03 -05:00
|
|
|
use std::process::Command as ShellCommand;
|
|
|
|
|
|
2022-11-29 16:52:31 -05:00
|
|
|
pub fn run() -> cosmic::iced::Result {
|
2023-09-18 08:31:27 -07:00
|
|
|
cosmic::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,
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-12 16:30:50 -04:00
|
|
|
impl IcedWorkspacesApplet {
|
|
|
|
|
/// returns the index of the workspace button after which which must be moved to a popup
|
|
|
|
|
/// if it exists.
|
|
|
|
|
fn popup_index(&self) -> Option<usize> {
|
|
|
|
|
let mut index = None;
|
|
|
|
|
let Some(max_major_axis_len) = self.core.applet.configure.as_ref().and_then(|c| {
|
|
|
|
|
// if we have a configure for width and height, we're in a overflow popup
|
|
|
|
|
match self.core.applet.anchor {
|
|
|
|
|
PanelAnchor::Top | PanelAnchor::Bottom => c.new_size.0,
|
|
|
|
|
PanelAnchor::Left | PanelAnchor::Right => c.new_size.1,
|
|
|
|
|
}
|
|
|
|
|
}) else {
|
|
|
|
|
return index;
|
|
|
|
|
};
|
|
|
|
|
let button_total_size = self.core.applet.suggested_size(true).0
|
|
|
|
|
+ self.core.applet.suggested_padding(true) * 2
|
|
|
|
|
+ 4;
|
|
|
|
|
let btn_count = max_major_axis_len.get() / button_total_size as u32;
|
|
|
|
|
if btn_count >= self.workspaces.len() as u32 {
|
|
|
|
|
index = None;
|
|
|
|
|
} else {
|
|
|
|
|
index = Some((btn_count as usize).min(self.workspaces.len()));
|
|
|
|
|
}
|
|
|
|
|
index
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-29 16:52:31 -05:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
enum Message {
|
|
|
|
|
WorkspaceUpdate(WorkspacesUpdate),
|
|
|
|
|
WorkspacePressed(ObjectId),
|
|
|
|
|
WheelScrolled(ScrollDelta),
|
2024-04-09 15:33:03 -05:00
|
|
|
WorkspaceOverview,
|
2022-11-29 16:52:31 -05:00
|
|
|
}
|
|
|
|
|
|
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-09-18 08:31:27 -07:00
|
|
|
fn init(
|
|
|
|
|
core: cosmic::app::Core,
|
|
|
|
|
_flags: Self::Flags,
|
|
|
|
|
) -> (
|
|
|
|
|
Self,
|
|
|
|
|
cosmic::iced::Command<cosmic::app::Message<Self::Message>>,
|
|
|
|
|
) {
|
2022-11-29 16:52:31 -05:00
|
|
|
(
|
2023-11-16 18:32:31 +00:00
|
|
|
Self {
|
2023-09-18 08:31:27 -07:00
|
|
|
layout: match &core.applet.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
|
|
|
}
|
|
|
|
|
|
2023-09-18 08:31:27 -07:00
|
|
|
fn update(
|
|
|
|
|
&mut self,
|
|
|
|
|
message: Self::Message,
|
|
|
|
|
) -> cosmic::iced::Command<cosmic::app::Message<Self::Message>> {
|
2022-11-29 16:52:31 -05:00
|
|
|
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) => {
|
2024-05-14 21:02:18 -04:00
|
|
|
let (delta, debounce) = match delta {
|
|
|
|
|
ScrollDelta::Lines { x, y } => ((x + y) as f64, false),
|
|
|
|
|
ScrollDelta::Pixels { x, y } => ((x + y) as f64, true),
|
|
|
|
|
};
|
2022-11-29 16:52:31 -05:00
|
|
|
if let Some(tx) = self.workspace_tx.as_mut() {
|
2024-05-14 21:02:18 -04:00
|
|
|
let _ = tx.try_send(WorkspaceEvent::Scroll(delta, debounce));
|
2022-11-29 16:52:31 -05:00
|
|
|
}
|
|
|
|
|
}
|
2024-04-09 15:33:03 -05:00
|
|
|
Message::WorkspaceOverview => {
|
|
|
|
|
let _ = ShellCommand::new("cosmic-workspaces").spawn();
|
|
|
|
|
}
|
2022-11-29 16:52:31 -05:00
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
}
|
2024-03-28 19:10:59 -04:00
|
|
|
let horizontal = matches!(
|
|
|
|
|
self.core.applet.anchor,
|
|
|
|
|
PanelAnchor::Top | PanelAnchor::Bottom
|
|
|
|
|
);
|
2024-04-30 17:47:15 -04:00
|
|
|
let suggested_total =
|
|
|
|
|
self.core.applet.suggested_size(true).0 + self.core.applet.suggested_padding(true) * 2;
|
|
|
|
|
let suggested_window_size = self.core.applet.suggested_window_size();
|
2024-07-12 16:30:50 -04:00
|
|
|
let popup_index = self.popup_index().unwrap_or(self.workspaces.len());
|
|
|
|
|
|
|
|
|
|
let buttons = self.workspaces[..popup_index].iter().filter_map(|w| {
|
2024-10-16 18:52:34 -04:00
|
|
|
let content = self
|
|
|
|
|
.core
|
|
|
|
|
.applet
|
|
|
|
|
.text(w.0.clone())
|
|
|
|
|
.font(cosmic::font::bold());
|
2024-03-28 19:10:59 -04:00
|
|
|
|
2024-04-30 17:47:15 -04:00
|
|
|
let (width, height) = if self.core.applet.is_horizontal() {
|
|
|
|
|
(suggested_total as f32, suggested_window_size.1.get() as f32)
|
|
|
|
|
} else {
|
|
|
|
|
(suggested_window_size.0.get() as f32, suggested_total as f32)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let content = row!(content, vertical_space(Length::Fixed(height)))
|
|
|
|
|
.align_items(cosmic::iced::Alignment::Center);
|
|
|
|
|
|
|
|
|
|
let content = column!(content, horizontal_space(Length::Fixed(width)))
|
|
|
|
|
.align_items(cosmic::iced::Alignment::Center);
|
2024-03-28 19:10:59 -04:00
|
|
|
|
2024-02-02 16:16:32 -05:00
|
|
|
let btn = button(
|
2024-03-28 19:10:59 -04:00
|
|
|
container(content)
|
|
|
|
|
.align_x(Horizontal::Center)
|
|
|
|
|
.align_y(Vertical::Center),
|
2024-02-02 16:16:32 -05:00
|
|
|
)
|
2024-03-28 19:10:59 -04:00
|
|
|
.padding(if horizontal {
|
2024-04-15 18:37:00 -04:00
|
|
|
[0, self.core.applet.suggested_padding(true)]
|
2024-03-28 19:10:59 -04:00
|
|
|
} else {
|
2024-04-15 18:37:00 -04:00
|
|
|
[self.core.applet.suggested_padding(true), 0]
|
2024-03-28 19:10:59 -04:00
|
|
|
})
|
2024-04-09 15:33:03 -05:00
|
|
|
.on_press(match w.1 {
|
|
|
|
|
Some(zcosmic_workspace_handle_v1::State::Active) => Message::WorkspaceOverview,
|
|
|
|
|
_ => Message::WorkspacePressed(w.2.clone()),
|
|
|
|
|
})
|
2024-02-02 16:16:32 -05:00
|
|
|
.padding(0);
|
2023-10-06 23:29:04 +02:00
|
|
|
|
2024-02-02 16:16:32 -05:00
|
|
|
Some(
|
|
|
|
|
btn.style(match w.1 {
|
|
|
|
|
Some(zcosmic_workspace_handle_v1::State::Active) => {
|
|
|
|
|
cosmic::theme::iced::Button::Primary
|
|
|
|
|
}
|
|
|
|
|
Some(zcosmic_workspace_handle_v1::State::Urgent) => {
|
|
|
|
|
let appearance = |theme: &Theme| {
|
|
|
|
|
let cosmic = theme.cosmic();
|
|
|
|
|
button::Appearance {
|
2023-10-06 23:29:04 +02:00
|
|
|
background: Some(Background::Color(
|
2024-02-02 16:16:32 -05:00
|
|
|
cosmic.palette.neutral_3.into(),
|
2023-10-06 23:29:04 +02:00
|
|
|
)),
|
2024-02-02 16:16:32 -05:00
|
|
|
border: Border {
|
|
|
|
|
radius: cosmic.radius_xl().into(),
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
2023-10-06 23:29:04 +02:00
|
|
|
border_radius: theme.cosmic().radius_xl().into(),
|
|
|
|
|
text_color: theme.cosmic().destructive_button.base.into(),
|
|
|
|
|
..button::Appearance::default()
|
|
|
|
|
}
|
2024-02-02 16:16:32 -05:00
|
|
|
};
|
|
|
|
|
cosmic::theme::iced::Button::Custom {
|
|
|
|
|
active: Box::new(appearance),
|
|
|
|
|
hover: Box::new(move |theme| button::Appearance {
|
|
|
|
|
background: Some(Background::Color(
|
|
|
|
|
theme.current_container().component.hover.into(),
|
|
|
|
|
)),
|
|
|
|
|
border: Border {
|
|
|
|
|
radius: theme.cosmic().radius_xl().into(),
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
..appearance(theme)
|
|
|
|
|
}),
|
2023-10-06 23:29:04 +02:00
|
|
|
}
|
2024-02-02 16:16:32 -05:00
|
|
|
}
|
|
|
|
|
None => {
|
|
|
|
|
let appearance = |theme: &Theme| {
|
|
|
|
|
let cosmic = theme.cosmic();
|
|
|
|
|
button::Appearance {
|
2023-10-06 23:29:04 +02:00
|
|
|
background: None,
|
2024-02-02 16:16:32 -05:00
|
|
|
border: Border {
|
|
|
|
|
radius: cosmic.radius_xl().into(),
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
border_radius: cosmic.radius_xl().into(),
|
2023-10-06 23:29:04 +02:00
|
|
|
text_color: theme.current_container().component.on.into(),
|
|
|
|
|
..button::Appearance::default()
|
|
|
|
|
}
|
2024-02-02 16:16:32 -05:00
|
|
|
};
|
|
|
|
|
cosmic::theme::iced::Button::Custom {
|
|
|
|
|
active: Box::new(appearance),
|
|
|
|
|
hover: Box::new(move |theme| button::Appearance {
|
|
|
|
|
background: Some(Background::Color(
|
|
|
|
|
theme.current_container().component.hover.into(),
|
|
|
|
|
)),
|
|
|
|
|
border: Border {
|
|
|
|
|
radius: theme.cosmic().radius_xl().into(),
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
..appearance(theme)
|
|
|
|
|
}),
|
2023-09-18 00:24:21 -07:00
|
|
|
}
|
2024-02-02 16:16:32 -05:00
|
|
|
}
|
|
|
|
|
_ => return None,
|
|
|
|
|
})
|
|
|
|
|
.into(),
|
|
|
|
|
)
|
|
|
|
|
});
|
2024-07-12 16:30:50 -04:00
|
|
|
// TODO if there is a popup_index, create a button with a popup for the remaining workspaces
|
|
|
|
|
// Should it appear on hover or on click?
|
2022-11-29 16:52:31 -05:00
|
|
|
let layout_section: Element<_> = match self.layout {
|
2024-03-28 19:10:59 -04:00
|
|
|
Layout::Row => row(buttons).spacing(4).into(),
|
|
|
|
|
Layout::Column => column(buttons).spacing(4).into(),
|
2022-11-29 16:52:31 -05:00
|
|
|
};
|
|
|
|
|
|
2024-03-28 19:10:59 -04:00
|
|
|
container(layout_section).padding(0).into()
|
2022-11-29 16:52:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn subscription(&self) -> Subscription<Message> {
|
2023-11-16 17:32:40 +00:00
|
|
|
Subscription::batch(vec![
|
2024-01-18 21:02:35 -05:00
|
|
|
workspaces().map(Message::WorkspaceUpdate),
|
2023-12-04 17:01:22 -08:00
|
|
|
event::listen_with(|e, _| match e {
|
2023-11-16 17:32:40 +00:00
|
|
|
Mouse(mouse::Event::WheelScrolled { delta }) => Some(Message::WheelScrolled(delta)),
|
|
|
|
|
_ => None,
|
|
|
|
|
}),
|
|
|
|
|
])
|
2022-11-29 16:52:31 -05:00
|
|
|
}
|
|
|
|
|
|
2023-08-03 13:22:17 -07:00
|
|
|
fn style(&self) -> Option<<Theme as application::StyleSheet>::Style> {
|
2023-09-18 08:31:27 -07:00
|
|
|
Some(cosmic::applet::style())
|
2022-11-29 16:52:31 -05:00
|
|
|
}
|
|
|
|
|
}
|