Update iced/libcosmic

This commit is contained in:
Ian Douglas Scott 2024-10-18 13:13:53 -07:00 committed by Ian Douglas Scott
parent e0c0f27f67
commit b70828e23f
17 changed files with 2019 additions and 1262 deletions

View file

@ -8,10 +8,10 @@ use cosmic::{
self,
advanced::layout::flex::Axis,
widget::{column, row},
Border, Size,
Border,
},
iced_core::Shadow,
iced_sctk::subsurface_widget::Subsurface,
iced_winit::platform_specific::wayland::subsurface_widget::Subsurface,
widget,
};
use cosmic_bg_config::Source;
@ -19,7 +19,7 @@ use cosmic_comp_config::workspace::WorkspaceLayout;
use crate::{
backend::{self, CaptureImage},
App, DragSurface, LayerSurface, Msg, Toplevel, Workspace,
App, DragSurface, DragToplevel, LayerSurface, Msg, Toplevel, Workspace,
};
pub(crate) fn layer_surface<'a>(
@ -33,7 +33,7 @@ pub(crate) fn layer_surface<'a>(
}
}
let mut drag_toplevel = None;
if let Some((_, DragSurface::Toplevel { handle, .. }, _)) = &app.drag_surface {
if let Some((DragSurface::Toplevel { handle, .. }, _)) = &app.drag_surface {
drag_toplevel = Some(handle);
}
let layout = app.conf.workspace_config.workspace_layout;
@ -77,41 +77,10 @@ pub(crate) fn layer_surface<'a>(
container.into()
}
pub(crate) fn drag_surface<'a>(
app: &'a App,
drag_surface: &DragSurface,
size: Size,
) -> Option<cosmic::Element<'a, Msg>> {
let item = match drag_surface {
DragSurface::Workspace { handle, output } => {
if let Some(workspace) = app.workspaces.iter().find(|x| &x.handle == handle) {
workspace_item(workspace, output, false)
} else {
return None;
}
}
DragSurface::Toplevel { handle, .. } => {
if let Some(toplevel) = app.toplevels.iter().find(|x| &x.handle == handle) {
toplevel_preview(toplevel, true)
} else {
return None;
}
}
};
// TODO Use `mouse_interaction_wrapper` (need to modify iced_sctk to update view of
// drag surfaces)
Some(
widget::container(item)
.height(iced::Length::Fixed(size.height))
.width(iced::Length::Fixed(size.width))
.into(),
)
}
fn close_button(on_press: Msg) -> cosmic::Element<'static, Msg> {
widget::container(
widget::button(widget::icon::from_name("window-close-symbolic").size(16))
.style(cosmic::theme::Button::Destructive)
widget::button::custom(widget::icon::from_name("window-close-symbolic").size(16))
.class(cosmic::theme::Button::Destructive)
.on_press(on_press),
)
.align_x(iced::alignment::Horizontal::Right)
@ -123,9 +92,9 @@ fn workspace_item_appearance(
theme: &cosmic::Theme,
is_active: bool,
hovered: bool,
) -> cosmic::widget::button::Appearance {
) -> cosmic::widget::button::Style {
let cosmic = theme.cosmic();
let mut appearance = cosmic::widget::button::Appearance::new();
let mut appearance = cosmic::widget::button::Style::new();
appearance.border_radius = cosmic.corner_radii.radius_s.into();
if is_active {
appearance.border_width = 2.0;
@ -146,9 +115,9 @@ fn workspace_item<'a>(
let is_active = workspace.is_active;
column![
// TODO editable name?
widget::button(column![image, widget::text(&workspace.name)])
widget::button::custom(column![image, widget::text(&workspace.name)])
.selected(workspace.is_active)
.style(cosmic::theme::Button::Custom {
.class(cosmic::theme::Button::Custom {
active: Box::new(move |_focused, theme| workspace_item_appearance(
theme,
is_active,
@ -173,6 +142,7 @@ fn workspace_sidebar_entry<'a>(
workspace: &'a Workspace,
output: &'a wl_output::WlOutput,
is_drop_target: bool,
drag_id: u64,
) -> cosmic::Element<'a, Msg> {
/* XXX
let mouse_interaction = if is_drop_target {
@ -198,18 +168,30 @@ fn workspace_sidebar_entry<'a>(
*/
//crate::widgets::mouse_interaction_wrapper(
// mouse_interaction,
iced::widget::dnd_listener(workspace_item(workspace, output, is_drop_target))
.on_enter(|actions, mime, pos| {
Msg::DndWorkspaceEnter(workspace.handle.clone(), output.clone(), actions, mime, pos)
})
.on_exit(Msg::DndWorkspaceLeave(
workspace.handle.clone(),
output.clone(),
))
.on_drop(Msg::DndWorkspaceDrop)
.on_data(Msg::DndWorkspaceData)
//)
.into()
let workspace_handle = workspace.handle.clone();
let workspace_handle2 = workspace_handle.clone();
let output_clone = output.clone();
let output_clone2 = output.clone();
cosmic::widget::dnd_destination::dnd_destination_for_data(
workspace_item(workspace, output, is_drop_target),
|data: Option<DragToplevel>, _action| match data {
Some(toplevel) => Msg::DndWorkspaceDrop(toplevel),
None => Msg::Ignore,
},
)
.drag_id(drag_id)
.on_enter(move |actions, mime, pos| {
Msg::DndWorkspaceEnter(
workspace_handle.clone(),
output_clone.clone(),
actions,
mime,
pos,
)
})
.on_leave(move || Msg::DndWorkspaceLeave(workspace_handle2.clone(), output_clone2.clone()))
//)
.into()
}
fn workspaces_sidebar<'a>(
@ -219,7 +201,8 @@ fn workspaces_sidebar<'a>(
drop_target: Option<&backend::ZcosmicWorkspaceHandleV1>,
) -> cosmic::Element<'a, Msg> {
let sidebar_entries = workspaces
.map(|w| workspace_sidebar_entry(w, output, drop_target == Some(&w.handle)))
.enumerate()
.map(|(i, w)| workspace_sidebar_entry(w, output, drop_target == Some(&w.handle), i as u64))
.collect();
let axis = match layout {
WorkspaceLayout::Vertical => Axis::Vertical,
@ -260,8 +243,8 @@ fn workspaces_sidebar<'a>(
widget::container(sidebar_entries_container)
.width(width)
.height(height)
.style(cosmic::theme::Container::custom(|theme| {
cosmic::iced_style::container::Appearance {
.class(cosmic::theme::Container::custom(|theme| {
cosmic::iced::widget::container::Style {
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()),
@ -280,8 +263,9 @@ fn workspaces_sidebar<'a>(
.into()
}
fn toplevel_preview(toplevel: &Toplevel, is_being_dragged: bool) -> cosmic::Element<Msg> {
let label = widget::text(&toplevel.info.title);
fn toplevel_preview(toplevel: &Toplevel, is_being_dragged: bool) -> cosmic::Element<'static, Msg> {
// Clone so it can be 'static, for use in drag icon
let label = widget::text(toplevel.info.title.clone());
let label = if let Some(icon) = &toplevel.icon {
row![widget::icon(widget::icon::from_path(icon.clone())), label].spacing(4)
} else {
@ -292,17 +276,17 @@ fn toplevel_preview(toplevel: &Toplevel, is_being_dragged: bool) -> cosmic::Elem
crate::widgets::toplevel_item(
vec![
close_button(Msg::CloseToplevel(toplevel.handle.clone())),
widget::button(capture_image(toplevel.img.as_ref(), alpha))
widget::button::custom(capture_image(toplevel.img.as_ref(), alpha))
.selected(
toplevel
.info
.state
.contains(&zcosmic_toplevel_handle_v1::State::Activated),
)
.style(cosmic::theme::Button::Image)
.class(cosmic::theme::Button::Image)
.on_press(Msg::ActivateToplevel(toplevel.handle.clone()))
.into(),
widget::button(label)
widget::button::custom(label)
.on_press(Msg::ActivateToplevel(toplevel.handle.clone()))
.into(),
],
@ -325,19 +309,28 @@ fn toplevel_previews_entry<'a>(
toplevel_preview(toplevel, is_being_dragged),
!is_being_dragged,
);
iced::widget::dnd_source(preview)
.on_drag(|size, offset| {
Msg::StartDrag(
size,
offset,
DragSurface::Toplevel {
handle: toplevel.handle.clone(),
output: output.clone(),
},
let toplevel2 = toplevel.clone();
cosmic::widget::dnd_source::<_, DragToplevel>(preview)
.drag_threshold(5.)
.drag_content(|| DragToplevel {})
// XXX State?
.drag_icon(move |offset| {
(
toplevel_preview(&toplevel2, true).map(|_| ()),
cosmic::iced_core::widget::tree::State::None,
-offset,
)
})
.on_finished(Msg::SourceFinished)
.on_cancelled(Msg::SourceFinished)
.on_start(Some(Msg::StartDrag(
//size,
//offset,
DragSurface::Toplevel {
handle: toplevel.handle.clone(),
output: output.clone(),
},
)))
.on_finish(Some(Msg::SourceFinished))
.on_cancel(Some(Msg::SourceFinished))
.into()
}
@ -382,42 +375,38 @@ fn bg_element<'a>(
.width(iced::Length::Fill)
.height(iced::Length::Fill)
.into(),
Some(Source::Color(color)) => {
widget::layer_container(widget::horizontal_space(iced::Length::Fill))
.width(iced::Length::Fill)
.height(iced::Length::Fill)
.style(cosmic::theme::Container::Custom(Box::new(move |_| {
let color = color.clone();
cosmic::iced_style::container::Appearance {
background: Some(match color {
cosmic_bg_config::Color::Single(c) => iced::Background::Color(
cosmic::iced::Color::new(c[0], c[1], c[2], 1.0),
),
cosmic_bg_config::Color::Gradient(cosmic_bg_config::Gradient {
colors,
radius,
}) => {
let stop_increment = 1.0 / (colors.len() - 1) as f32;
let mut stop = 0.0;
Some(Source::Color(color)) => widget::layer_container(widget::horizontal_space())
.width(iced::Length::Fill)
.height(iced::Length::Fill)
.class(cosmic::theme::Container::Custom(Box::new(move |_| {
let color = color.clone();
cosmic::iced::widget::container::Style {
background: Some(match color {
cosmic_bg_config::Color::Single(c) => {
iced::Background::Color(cosmic::iced::Color::new(c[0], c[1], c[2], 1.0))
}
cosmic_bg_config::Color::Gradient(cosmic_bg_config::Gradient {
colors,
radius,
}) => {
let stop_increment = 1.0 / (colors.len() - 1) as f32;
let mut stop = 0.0;
let mut linear = iced::gradient::Linear::new(iced::Degrees(radius));
let mut linear = iced::gradient::Linear::new(iced::Degrees(radius));
for &[r, g, b] in colors.iter() {
linear = linear
.add_stop(stop, cosmic::iced::Color::from_rgb(r, g, b));
stop += stop_increment;
}
iced::Background::Gradient(cosmic::iced_core::Gradient::Linear(
linear,
))
for &[r, g, b] in colors.iter() {
linear =
linear.add_stop(stop, cosmic::iced::Color::from_rgb(r, g, b));
stop += stop_increment;
}
}),
..Default::default()
}
})))
.into()
}
iced::Background::Gradient(cosmic::iced_core::Gradient::Linear(linear))
}
}),
..Default::default()
}
})))
.into(),
None => {
widget::image::Image::<widget::image::Handle>::new(widget::image::Handle::from_path(
"/usr/share/backgrounds/pop/kate-hazen-COSMIC-desktop-wallpaper.png",
@ -430,7 +419,7 @@ fn bg_element<'a>(
}
}
fn capture_image(image: Option<&CaptureImage>, alpha: f32) -> cosmic::Element<'_, Msg> {
fn capture_image(image: Option<&CaptureImage>, alpha: f32) -> cosmic::Element<'static, Msg> {
if let Some(image) = image {
#[cfg(feature = "no-subsurfaces")]
{
@ -439,11 +428,9 @@ fn capture_image(image: Option<&CaptureImage>, alpha: f32) -> cosmic::Element<'_
}
#[cfg(not(feature = "no-subsurfaces"))]
{
Subsurface::new(image.width, image.height, &image.wl_buffer)
.alpha(alpha)
.into()
Subsurface::new(image.wl_buffer.clone()).alpha(alpha).into()
}
} else {
widget::Image::new(widget::image::Handle::from_pixels(1, 1, vec![0, 0, 0, 255])).into()
widget::Image::new(widget::image::Handle::from_rgba(1, 1, vec![0, 0, 0, 255])).into()
}
}