cosmic-workspaces/src/widgets/toplevels/mod.rs

179 lines
5.5 KiB
Rust
Raw Normal View History

use cosmic::iced::advanced::layout::flex::Axis;
use cosmic::iced::advanced::layout::{self};
use cosmic::iced::advanced::widget::{Operation, Tree};
use cosmic::iced::advanced::{Clipboard, Layout, Shell, Widget, mouse, renderer};
use cosmic::iced::event::{self, Event};
use cosmic::iced::{Length, Rectangle, Size, Vector};
use std::marker::PhantomData;
2025-01-09 16:08:50 -08:00
mod toplevel_layout;
use toplevel_layout::{LayoutToplevel, ToplevelLayout, TwoRowColToplevelLayout};
2024-04-24 13:51:20 -07:00
pub fn toplevels<Msg>(children: Vec<cosmic::Element<Msg>>) -> Toplevels<Msg> {
Toplevels {
// TODO configurable
layout: TwoRowColToplevelLayout::new(Axis::Horizontal, 16),
children,
_msg: PhantomData,
}
}
pub struct Toplevels<'a, Msg> {
layout: TwoRowColToplevelLayout,
children: Vec<cosmic::Element<'a, Msg>>,
_msg: PhantomData<Msg>,
}
2025-01-15 11:53:59 -08:00
impl<Msg> Widget<Msg, cosmic::Theme, cosmic::Renderer> for Toplevels<'_, Msg> {
2024-02-06 13:32:29 -08:00
fn size(&self) -> Size<Length> {
2025-01-09 16:08:50 -08:00
self.layout.size()
}
fn layout(
2026-03-03 13:53:45 -05:00
&mut self,
tree: &mut Tree,
renderer: &cosmic::Renderer,
limits: &layout::Limits,
) -> layout::Node {
2025-01-09 16:08:50 -08:00
// Call `.layout()` on each child with full limits to determine "preferred" sizes
let layout_toplevels = self
.children
2026-03-03 13:53:45 -05:00
.iter_mut()
.zip(tree.children.iter_mut())
.map(|(child, tree)| {
2026-03-03 13:53:45 -05:00
let preferred_size = child.as_widget_mut().layout(tree, renderer, limits).size();
2025-01-09 16:08:50 -08:00
LayoutToplevel {
preferred_size,
_phantom_data: PhantomData,
}
})
.collect::<Vec<_>>();
2025-01-09 16:08:50 -08:00
// Assign rectangles for each child using `ToplevelLayout` backend
let assigned_rects = self.layout.layout(limits.max(), &layout_toplevels);
let nodes = self
.children
2026-03-03 13:53:45 -05:00
.iter_mut()
.zip(tree.children.iter_mut())
2025-01-09 16:08:50 -08:00
.zip(assigned_rects)
.map(|((child, tree), assigned_rect)| {
let child_limits = layout::Limits::new(Size::ZERO, assigned_rect.size());
2026-03-03 13:53:45 -05:00
let layout = child.as_widget_mut().layout(tree, renderer, &child_limits);
2025-01-09 16:08:50 -08:00
// Center on both axes, if child didn't consume full size allocation
let centering_offset = Vector::new(
((assigned_rect.size().width - layout.size().width) / 2.).max(0.),
((assigned_rect.size().height - layout.size().height) / 2.).max(0.),
);
2025-01-09 16:08:50 -08:00
layout.move_to(assigned_rect.position() + centering_offset)
})
.collect();
2025-01-09 16:08:50 -08:00
layout::Node::with_children(limits.max(), nodes)
}
fn operate(
2026-03-03 13:53:45 -05:00
&mut self,
tree: &mut Tree,
layout: Layout<'_>,
renderer: &cosmic::Renderer,
2024-10-18 13:13:53 -07:00
operation: &mut dyn Operation<()>,
) {
2026-03-03 13:53:45 -05:00
operation.container(None, layout.bounds());
operation.traverse(&mut |operation| {
self.children
2026-03-03 13:53:45 -05:00
.iter_mut()
.zip(&mut tree.children)
.zip(layout.children())
.for_each(|((child, state), layout)| {
child
2026-03-03 13:53:45 -05:00
.as_widget_mut()
.operate(state, layout, renderer, operation);
});
});
}
2026-03-03 13:53:45 -05:00
fn update(
&mut self,
tree: &mut Tree,
2026-03-03 13:53:45 -05:00
event: &Event,
layout: Layout<'_>,
cursor: mouse::Cursor,
renderer: &cosmic::Renderer,
clipboard: &mut dyn Clipboard,
shell: &mut Shell<'_, Msg>,
viewport: &Rectangle,
2026-03-03 13:53:45 -05:00
) {
for ((child, state), layout) in self
.children
.iter_mut()
.zip(&mut tree.children)
.zip(layout.children())
2026-03-03 13:53:45 -05:00
{
child.as_widget_mut().update(
state, event, layout, cursor, renderer, clipboard, shell, viewport,
);
}
}
fn mouse_interaction(
&self,
tree: &Tree,
layout: Layout<'_>,
cursor: mouse::Cursor,
viewport: &Rectangle,
renderer: &cosmic::Renderer,
) -> mouse::Interaction {
self.children
.iter()
.zip(&tree.children)
.zip(layout.children())
.map(|((child, state), layout)| {
child
.as_widget()
.mouse_interaction(state, layout, cursor, viewport, renderer)
})
.max()
.unwrap_or_default()
}
fn draw(
&self,
tree: &Tree,
renderer: &mut cosmic::Renderer,
theme: &cosmic::Theme,
style: &renderer::Style,
layout: Layout<'_>,
cursor: mouse::Cursor,
viewport: &Rectangle,
) {
if let Some(viewport) = layout.bounds().intersection(viewport) {
for ((child, state), layout) in self
.children
.iter()
.zip(&tree.children)
.zip(layout.children())
{
child
.as_widget()
.draw(state, renderer, theme, style, layout, cursor, &viewport);
}
}
}
fn children(&self) -> Vec<Tree> {
self.children.iter().map(Tree::new).collect()
}
fn diff(&mut self, tree: &mut Tree) {
tree.diff_children(&mut self.children);
}
}
impl<'a, Msg: 'static> From<Toplevels<'a, Msg>> for cosmic::Element<'a, Msg> {
fn from(widget: Toplevels<'a, Msg>) -> Self {
cosmic::Element::new(widget)
}
}