cosmic-applets/cosmic-applet-workspaces/src/components/app.rs

329 lines
12 KiB
Rust
Raw Normal View History

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,
2024-10-30 22:51:08 -04:00
Length, Limits, Subscription,
2024-07-09 15:17:44 +02:00
},
iced_core::{Background, Border},
2024-10-30 22:51:08 -04:00
widget::{autosize, container, horizontal_space, vertical_space, Id},
Element, Task, Theme,
2024-07-09 15:17:44 +02:00
};
2022-11-29 16:52:31 -05:00
use cosmic_protocols::workspace::v1::client::zcosmic_workspace_handle_v1;
2024-10-30 22:51:08 -04:00
use once_cell::sync::Lazy;
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
use std::process::Command as ShellCommand;
2024-10-30 22:51:08 -04:00
static AUTOSIZE_MAIN_ID: Lazy<Id> = Lazy::new(|| Id::new("autosize-main"));
2022-11-29 16:52:31 -05:00
pub fn run() -> cosmic::iced::Result {
2024-10-30 22:51:08 -04:00
cosmic::applet::run::<IcedWorkspacesApplet>(())
2022-11-29 16:52:31 -05:00
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Layout {
Row,
Column,
}
struct IcedWorkspacesApplet {
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;
2024-10-30 22:51:08 -04:00
let Some(max_major_axis_len) = self.core.applet.suggested_bounds.as_ref().map(|c| {
2024-07-12 16:30:50 -04:00
// if we have a configure for width and height, we're in a overflow popup
match self.core.applet.anchor {
2024-10-30 22:51:08 -04:00
PanelAnchor::Top | PanelAnchor::Bottom => c.width as u32,
PanelAnchor::Left | PanelAnchor::Right => c.height as u32,
2024-07-12 16:30:50 -04:00
}
}) else {
return index;
};
let button_total_size = self.core.applet.suggested_size(true).0
+ self.core.applet.suggested_padding(true) * 2
+ 4;
2024-10-30 22:51:08 -04:00
let btn_count = max_major_axis_len / button_total_size as u32;
2024-07-12 16:30:50 -04:00
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),
WorkspaceOverview,
2022-11-29 16:52:31 -05:00
}
impl cosmic::Application for IcedWorkspacesApplet {
2022-11-29 16:52:31 -05:00
type Message = Message;
type Executor = cosmic::SingleThreadExecutor;
2022-11-29 16:52:31 -05:00
type Flags = ();
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,
2024-10-30 22:51:08 -04:00
cosmic::iced::Task<cosmic::app::Message<Self::Message>>,
2023-09-18 08:31:27 -07:00
) {
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,
},
core,
2022-11-29 16:52:31 -05:00
workspaces: Vec::new(),
workspace_tx: Default::default(),
},
2024-10-30 22:51:08 -04:00
Task::none(),
2022-11-29 16:52:31 -05: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,
2024-10-30 22:51:08 -04:00
) -> cosmic::iced::Task<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) => {
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() {
let _ = tx.try_send(WorkspaceEvent::Scroll(delta, debounce));
2022-11-29 16:52:31 -05:00
}
}
Message::WorkspaceOverview => {
let _ = ShellCommand::new("cosmic-workspaces").spawn();
}
2022-11-29 16:52:31 -05:00
}
2024-10-30 22:51:08 -04:00
Task::none()
2022-11-29 16:52:31 -05:00
}
fn view(&self) -> Element<Message> {
2022-12-07 12:46:54 -05:00
if self.workspaces.is_empty() {
return row![].padding(8).into();
}
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| {
let content = self
.core
.applet
.text(w.0.clone())
.font(cosmic::font::bold());
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)
};
2024-10-30 22:51:08 -04:00
let content = row!(content, vertical_space().height(Length::Fixed(height)))
.align_y(cosmic::iced::Alignment::Center);
2024-04-30 17:47:15 -04:00
2024-10-30 22:51:08 -04:00
let content = column!(content, horizontal_space().width(Length::Fixed(width)))
.align_x(cosmic::iced::Alignment::Center);
2024-02-02 16:16:32 -05:00
let btn = button(
container(content)
.align_x(Horizontal::Center)
.align_y(Vertical::Center),
2024-02-02 16:16:32 -05:00
)
.padding(if horizontal {
2024-04-15 18:37:00 -04:00
[0, self.core.applet.suggested_padding(true)]
} else {
2024-04-15 18:37:00 -04:00
[self.core.applet.suggested_padding(true), 0]
})
.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(
2024-10-30 22:51:08 -04:00
btn.class(match w.1 {
2024-02-02 16:16:32 -05:00
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();
2024-10-30 22:51:08 -04:00
button::Style {
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(),
2024-10-30 22:51:08 -04:00
..button::Style::default()
2023-10-06 23:29:04 +02:00
}
2024-02-02 16:16:32 -05:00
};
2024-10-30 22:51:08 -04:00
cosmic::theme::iced::Button::Custom(Box::new(move |theme, status| {
match status {
button::Status::Active => appearance(theme),
button::Status::Hovered => button::Style {
background: Some(Background::Color(
theme.current_container().component.hover.into(),
)),
border: Border {
radius: theme.cosmic().radius_xl().into(),
..Default::default()
},
..appearance(theme)
2024-02-02 16:16:32 -05:00
},
2024-10-30 22:51:08 -04:00
button::Status::Pressed => appearance(theme),
button::Status::Disabled => appearance(theme),
}
}))
2024-02-02 16:16:32 -05:00
}
None => {
let appearance = |theme: &Theme| {
let cosmic = theme.cosmic();
2024-10-30 22:51:08 -04:00
button::Style {
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(),
2024-10-30 22:51:08 -04:00
..button::Style::default()
2023-10-06 23:29:04 +02:00
}
2024-02-02 16:16:32 -05:00
};
2024-10-30 22:51:08 -04:00
cosmic::theme::iced::Button::Custom(Box::new(move |theme, status| {
match status {
button::Status::Active => appearance(theme),
button::Status::Hovered => button::Style {
background: Some(Background::Color(
theme.current_container().component.hover.into(),
)),
border: Border {
radius: theme.cosmic().radius_xl().into(),
..Default::default()
},
..appearance(theme)
2024-02-02 16:16:32 -05:00
},
2024-10-30 22:51:08 -04:00
button::Status::Pressed | button::Status::Disabled => {
appearance(theme)
}
}
}))
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 {
Layout::Row => row(buttons).spacing(4).into(),
Layout::Column => column(buttons).spacing(4).into(),
2022-11-29 16:52:31 -05:00
};
2024-10-30 22:51:08 -04:00
let mut limits = Limits::NONE.min_width(1.).min_height(1.);
if let Some(b) = self.core.applet.suggested_bounds {
if b.width as i32 > 0 {
limits = limits.max_width(b.width);
}
if b.height as i32 > 0 {
limits = limits.max_height(b.height);
}
}
2022-11-29 16:52:31 -05:00
2024-10-30 22:51:08 -04:00
autosize::autosize(
container(layout_section).padding(0),
AUTOSIZE_MAIN_ID.clone(),
)
.limits(limits)
.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![
workspaces().map(Message::WorkspaceUpdate),
2024-10-30 22:51:08 -04: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
}
2024-10-30 22:51:08 -04:00
fn style(&self) -> Option<cosmic::iced_runtime::Appearance> {
2023-09-18 08:31:27 -07:00
Some(cosmic::applet::style())
2022-11-29 16:52:31 -05:00
}
}