Draft draggable and graphics logic for TitleBar

This commit is contained in:
Héctor Ramón Jiménez 2020-06-05 14:02:29 +02:00
parent e8e656b330
commit 4dc5bffdfb
7 changed files with 268 additions and 73 deletions

View file

@ -39,20 +39,10 @@ where
let (content, mouse_interaction) =
content.draw(self, &defaults, content_layout, cursor_position);
if style.background.is_some() || style.border_width > 0 {
let quad = Primitive::Quad {
bounds,
background: style
.background
.unwrap_or(Background::Color(Color::TRANSPARENT)),
border_radius: style.border_radius,
border_width: style.border_width,
border_color: style.border_color,
};
if let Some(background) = background(bounds, &style) {
(
Primitive::Group {
primitives: vec![quad, content],
primitives: vec![background, content],
},
mouse_interaction,
)
@ -61,3 +51,22 @@ where
}
}
}
pub(crate) fn background(
bounds: Rectangle,
style: &container::Style,
) -> Option<Primitive> {
if style.background.is_none() && style.border_width > 0 {
return None;
}
Some(Primitive::Quad {
bounds,
background: style
.background
.unwrap_or(Background::Color(Color::TRANSPARENT)),
border_radius: style.border_radius,
border_width: style.border_width,
border_color: style.border_color,
})
}

View file

@ -35,7 +35,7 @@ where
&mut self,
defaults: &Self::Defaults,
content: &[(Pane, Content<'_, Message, Self>)],
dragging: Option<Pane>,
dragging: Option<(Pane, Point)>,
resizing: Option<Axis>,
layout: Layout<'_>,
cursor_position: Point,
@ -63,32 +63,40 @@ where
mouse_interaction = new_mouse_interaction;
}
if Some(*id) == dragging {
dragged_pane = Some((i, layout));
if let Some((dragging, origin)) = dragging {
if *id == dragging {
dragged_pane = Some((i, layout, origin));
}
}
primitive
})
.collect();
let primitives = if let Some((index, layout)) = dragged_pane {
let primitives = if let Some((index, layout, origin)) = dragged_pane {
let pane = panes.remove(index);
let bounds = layout.bounds();
if let Primitive::Group { primitives } = &pane {
panes.push(
primitives.first().cloned().unwrap_or(Primitive::None),
);
}
// TODO: Fix once proper layering is implemented.
// This is a pretty hacky way to achieve layering.
let clip = Primitive::Clip {
bounds: Rectangle {
x: cursor_position.x - bounds.width / 2.0,
y: cursor_position.y - bounds.height / 2.0,
x: cursor_position.x - origin.x,
y: cursor_position.y - origin.y,
width: bounds.width + 0.5,
height: bounds.height + 0.5,
},
offset: Vector::new(0, 0),
content: Box::new(Primitive::Translate {
translation: Vector::new(
cursor_position.x - bounds.x - bounds.width / 2.0,
cursor_position.y - bounds.y - bounds.height / 2.0,
cursor_position.x - bounds.x - origin.x,
cursor_position.y - bounds.y - origin.y,
),
content: Box::new(pane),
}),
@ -119,48 +127,77 @@ where
fn draw_pane<Message>(
&mut self,
defaults: &Self::Defaults,
bounds: Rectangle,
style_sheet: &Self::Style,
title_bar: Option<(&TitleBar<'_, Message, Self>, Layout<'_>)>,
body: (&Element<'_, Message, Self>, Layout<'_>),
cursor_position: Point,
) -> Self::Output {
let style = style_sheet.style();
let (body, body_layout) = body;
let (body_primitive, body_interaction) =
body.draw(self, defaults, body_layout, cursor_position);
let background = crate::widget::container::background(bounds, &style);
if let Some((title_bar, title_bar_layout)) = title_bar {
let show_controls = bounds.contains(cursor_position);
let is_over_draggable =
title_bar.is_over_draggable(title_bar_layout, cursor_position);
let (title_bar_primitive, title_bar_interaction) = title_bar.draw(
self,
defaults,
title_bar_layout,
cursor_position,
show_controls,
);
(
Primitive::Group {
primitives: vec![title_bar_primitive, body_primitive],
primitives: vec![
background.unwrap_or(Primitive::None),
title_bar_primitive,
body_primitive,
],
},
if title_bar_interaction > body_interaction {
if is_over_draggable {
mouse::Interaction::Grab
} else if title_bar_interaction > body_interaction {
title_bar_interaction
} else {
body_interaction
},
)
} else {
(body_primitive, body_interaction)
(
if let Some(background) = background {
Primitive::Group {
primitives: vec![background, body_primitive],
}
} else {
body_primitive
},
body_interaction,
)
}
}
fn draw_title_bar<Message>(
&mut self,
defaults: &Self::Defaults,
style: &<Self as iced_native::container::Renderer>::Style,
bounds: Rectangle,
style_sheet: &Self::Style,
title: (&Element<'_, Message, Self>, Layout<'_>),
controls: Option<(&Element<'_, Message, Self>, Layout<'_>)>,
cursor_position: Point,
) -> Self::Output {
let style = style_sheet.style();
let (title, title_layout) = title;
let background = crate::widget::container::background(bounds, &style);
let (title_primitive, _) =
title.draw(self, defaults, title_layout, cursor_position);
@ -170,12 +207,25 @@ where
(
Primitive::Group {
primitives: vec![title_primitive, controls_primitive],
primitives: vec![
background.unwrap_or(Primitive::None),
title_primitive,
controls_primitive,
],
},
controls_interaction,
)
} else {
(title_primitive, mouse::Interaction::default())
(
if let Some(background) = background {
Primitive::Group {
primitives: vec![background, title_primitive],
}
} else {
title_primitive
},
mouse::Interaction::default(),
)
}
}
}