cosmic-workspaces/src/view/mod.rs

309 lines
10 KiB
Rust
Raw Normal View History

2023-11-17 15:12:35 -08:00
use cctk::{
cosmic_protocols::toplevel_info::v1::client::zcosmic_toplevel_handle_v1,
2023-11-17 15:12:35 -08:00
wayland_client::protocol::wl_output,
};
2023-11-16 20:09:10 -08:00
use cosmic::{
2023-11-21 16:15:02 -05:00
cctk,
2023-11-16 20:09:10 -08:00
iced::{
self,
advanced::layout::flex::Axis,
2023-11-16 20:09:10 -08:00
widget::{column, row},
2024-02-06 13:32:29 -08:00
Border,
2023-11-16 20:09:10 -08:00
},
2024-02-06 13:32:29 -08:00
iced_core::Shadow,
iced_sctk::subsurface_widget::Subsurface,
2023-11-16 20:09:10 -08:00
widget,
};
use cosmic_comp_config::workspace::WorkspaceLayout;
2023-11-16 19:25:28 -08:00
use crate::{
backend::{self, CaptureImage},
App, DragSurface, LayerSurface, Msg, Toplevel, Workspace,
};
2023-11-16 19:25:28 -08:00
pub(crate) fn layer_surface<'a>(
app: &'a App,
surface: &'a LayerSurface,
) -> cosmic::Element<'a, Msg> {
2023-12-15 15:46:41 -08:00
let mut drop_target = None;
if let Some((workspace, output)) = &app.drop_target {
if output == &surface.output {
drop_target = Some(workspace);
}
}
let mut drag_toplevel = None;
if let Some((_, DragSurface::Toplevel { handle, .. }, _)) = &app.drag_surface {
drag_toplevel = Some(handle);
}
let layout = app.conf.workspace_config.workspace_layout;
let sidebar = workspaces_sidebar(
app.workspaces
.iter()
.filter(|i| i.outputs.contains(&surface.output)),
&surface.output,
layout,
2023-12-15 15:46:41 -08:00
drop_target,
);
2023-12-06 10:03:39 -08:00
let toplevels = toplevel_previews(
app.toplevels.iter().filter(|i| {
if !i.info.output.contains(&surface.output) {
return false;
}
2023-11-16 19:25:28 -08:00
2023-12-06 10:03:39 -08:00
i.info.workspace.iter().any(|workspace| {
app.workspace_for_handle(workspace)
.map_or(false, |x| x.is_active)
})
}),
&surface.output,
layout,
drag_toplevel,
2023-12-06 10:03:39 -08:00
);
let container = match layout {
2024-03-18 17:57:18 -07:00
WorkspaceLayout::Vertical => widget::layer_container(
2023-11-21 16:15:02 -05:00
row![sidebar, toplevels]
.spacing(12)
.height(iced::Length::Fill)
.width(iced::Length::Fill),
),
2024-03-18 17:57:18 -07:00
WorkspaceLayout::Horizontal => widget::layer_container(
2023-11-21 16:15:02 -05:00
column![sidebar, toplevels]
.spacing(12)
.height(iced::Length::Fill)
.width(iced::Length::Fill),
2023-11-21 16:15:02 -05:00
),
};
crate::widgets::image_bg(container).into()
2023-11-16 19:25:28 -08:00
}
fn close_button(on_press: Msg) -> cosmic::Element<'static, Msg> {
2023-12-18 19:05:22 -08:00
widget::container(
widget::button(widget::icon::from_name("window-close-symbolic").size(16))
.style(cosmic::theme::Button::Destructive)
.on_press(on_press),
)
.align_x(iced::alignment::Horizontal::Right)
.width(iced::Length::Fill)
.into()
2023-11-16 19:25:28 -08:00
}
pub(crate) fn workspace_item<'a>(
workspace: &'a Workspace,
output: &wl_output::WlOutput,
) -> cosmic::Element<'a, Msg> {
let image = capture_image(workspace.img_for_output.get(output));
2023-11-16 20:09:10 -08:00
column![
2024-01-03 10:57:47 -08:00
// TODO editable name?
widget::button(column![image, widget::text(&workspace.name)])
.selected(workspace.is_active)
.style(cosmic::theme::Button::Image)
.on_press(Msg::ActivateWorkspace(workspace.handle.clone())),
2023-11-16 19:25:28 -08:00
]
2023-12-18 19:05:22 -08:00
.spacing(4)
//.height(iced::Length::Fill)
2023-11-16 19:25:28 -08:00
.into()
}
fn workspace_sidebar_entry<'a>(
workspace: &'a Workspace,
output: &'a wl_output::WlOutput,
2024-03-05 12:25:20 -08:00
_is_drop_target: bool,
2023-11-16 19:25:28 -08:00
) -> cosmic::Element<'a, Msg> {
2023-12-15 15:46:41 -08:00
/* XXX
let mouse_interaction = if is_drop_target {
iced::mouse::Interaction::Crosshair
} else {
iced::mouse::Interaction::Idle
};
*/
/* TODO allow moving workspaces (needs compositor support)
2023-11-16 20:09:10 -08:00
iced::widget::dnd_source(workspace_item(workspace, output))
2023-11-16 19:25:28 -08:00
.on_drag(|size| {
Msg::StartDrag(
size,
DragSurface::Workspace {
handle: workspace.handle.clone(),
2023-11-16 19:25:28 -08:00
output: output.clone(),
},
)
})
.on_finished(Msg::SourceFinished)
.on_cancelled(Msg::SourceFinished)
.into()
*/
2023-12-15 15:46:41 -08:00
//crate::widgets::mouse_interaction_wrapper(
// mouse_interaction,
iced::widget::dnd_listener(workspace_item(workspace, output))
.on_enter(|actions, mime, pos| {
Msg::DndWorkspaceEnter(workspace.handle.clone(), output.clone(), actions, mime, pos)
})
.on_exit(Msg::DndWorkspaceLeave)
.on_drop(Msg::DndWorkspaceDrop)
.on_data(Msg::DndWorkspaceData)
2023-12-15 15:46:41 -08:00
//)
.into()
2023-11-16 19:25:28 -08:00
}
fn workspaces_sidebar<'a>(
workspaces: impl Iterator<Item = &'a Workspace>,
output: &'a wl_output::WlOutput,
layout: WorkspaceLayout,
drop_target: Option<&backend::ZcosmicWorkspaceHandleV1>,
2023-11-16 19:25:28 -08:00
) -> cosmic::Element<'a, Msg> {
let sidebar_entries = workspaces
2023-12-15 15:46:41 -08:00
.map(|w| workspace_sidebar_entry(w, output, drop_target == Some(&w.handle)))
.collect();
let axis = match layout {
WorkspaceLayout::Vertical => Axis::Vertical,
WorkspaceLayout::Horizontal => Axis::Horizontal,
};
let sidebar_entries_container =
widget::container(crate::widgets::workspace_bar(sidebar_entries, axis)).padding(12.0);
2024-03-05 12:25:20 -08:00
/*
2023-12-18 19:05:22 -08:00
let new_workspace_button = widget::button(
widget::container(row![
widget::icon::from_name("list-add-symbolic").symbolic(true),
2023-12-26 13:54:29 -08:00
widget::text(fl!("new-workspace"))
2023-12-18 19:05:22 -08:00
])
.width(iced::Length::Fill)
.align_x(iced::alignment::Horizontal::Center),
)
.on_press(Msg::NewWorkspace)
.width(iced::Length::Fill);
let bar: cosmic::Element<_> = if amount != WorkspaceAmount::Dynamic {
match layout {
WorkspaceLayout::Vertical => {
column![sidebar_entries_container, new_workspace_button,].into()
}
WorkspaceLayout::Horizontal => {
row![sidebar_entries_container, new_workspace_button,].into()
}
}
} else {
sidebar_entries_container.into()
};
*/
// Shrink?
let (width, height) = match layout {
WorkspaceLayout::Vertical => (iced::Length::Fill, iced::Length::Shrink),
WorkspaceLayout::Horizontal => (iced::Length::Shrink, iced::Length::Fill),
};
widget::container(
widget::container(sidebar_entries_container)
.width(width)
.height(height)
.style(cosmic::theme::Container::custom(|theme| {
cosmic::iced_style::container::Appearance {
text_color: Some(theme.cosmic().on_bg_color().into()),
icon_color: Some(theme.cosmic().on_bg_color().into()),
background: Some(iced::Color::from(theme.cosmic().background.base).into()),
border: Border {
radius: (12.0).into(),
width: 0.0,
color: iced::Color::TRANSPARENT,
},
shadow: Shadow::default(),
}
})),
)
.width(width)
.height(height)
.padding(24.0)
.into()
2023-11-16 19:25:28 -08:00
}
2023-12-06 10:03:39 -08:00
pub(crate) fn toplevel_preview(toplevel: &Toplevel) -> cosmic::Element<Msg> {
2023-12-18 19:41:30 -08:00
let label = widget::text(&toplevel.info.title);
let label = if let Some(icon) = &toplevel.icon {
row![widget::icon(widget::icon::from_path(icon.clone())), label].spacing(4)
} else {
row![label]
}
.padding(4);
crate::widgets::workspace_item(
vec![
close_button(Msg::CloseToplevel(toplevel.handle.clone())).into(),
widget::button(capture_image(toplevel.img.as_ref()))
.selected(
toplevel
.info
.state
.contains(&zcosmic_toplevel_handle_v1::State::Activated),
)
.style(cosmic::theme::Button::Image)
.on_press(Msg::ActivateToplevel(toplevel.handle.clone()))
.into(),
widget::button(label)
.on_press(Msg::ActivateToplevel(toplevel.handle.clone()))
.into(),
],
Axis::Vertical,
)
//.spacing(4)
//.align_items(iced::Alignment::Center)
//.width(iced::Length::Fill)
2023-11-16 19:25:28 -08:00
.into()
}
2023-12-06 10:03:39 -08:00
fn toplevel_previews_entry<'a>(
toplevel: &'a Toplevel,
output: &'a wl_output::WlOutput,
is_being_dragged: bool,
2023-12-06 10:03:39 -08:00
) -> cosmic::Element<'a, Msg> {
// Dragged window still takes up space until moved, but isn't rendered while drag surface is
// shown.
let preview = crate::widgets::visibility_wrapper(toplevel_preview(toplevel), !is_being_dragged);
iced::widget::dnd_source(preview)
2023-12-06 10:03:39 -08:00
.on_drag(|size| {
Msg::StartDrag(
size,
DragSurface::Toplevel {
handle: toplevel.handle.clone(),
output: output.clone(),
},
)
})
.on_finished(Msg::SourceFinished)
.on_cancelled(Msg::SourceFinished)
2023-12-06 10:03:39 -08:00
.into()
}
2023-11-16 19:25:28 -08:00
fn toplevel_previews<'a>(
toplevels: impl Iterator<Item = &'a Toplevel>,
2023-12-06 10:03:39 -08:00
output: &'a wl_output::WlOutput,
layout: WorkspaceLayout,
drag_toplevel: Option<&'a backend::ZcosmicToplevelHandleV1>,
2023-11-16 19:25:28 -08:00
) -> cosmic::Element<'a, Msg> {
let (width, height) = match layout {
WorkspaceLayout::Vertical => (iced::Length::FillPortion(4), iced::Length::Fill),
WorkspaceLayout::Horizontal => (iced::Length::Fill, iced::Length::FillPortion(4)),
};
let entries = toplevels
.map(|t| toplevel_previews_entry(t, output, drag_toplevel == Some(&t.handle)))
.collect();
//row(entries)
widget::container(crate::widgets::toplevels(entries))
.align_x(iced::alignment::Horizontal::Center)
.width(width)
.height(height)
//.spacing(16)
.padding(12)
//.align_items(iced::Alignment::Center)
.into()
2023-11-16 19:25:28 -08:00
}
2023-11-16 19:38:42 -08:00
fn capture_image(image: Option<&CaptureImage>) -> cosmic::Element<'_, Msg> {
if let Some(image) = image {
#[cfg(feature = "no-subsurfaces")]
{
widget::Image::new(image.image.clone()).into()
}
#[cfg(not(feature = "no-subsurfaces"))]
{
Subsurface::new(image.width, image.height, &image.wl_buffer).into()
}
2023-11-16 19:38:42 -08:00
} else {
widget::Image::new(widget::image::Handle::from_pixels(1, 1, vec![0, 0, 0, 255])).into()
}
}