Add style and padding to Tooltip
This commit is contained in:
parent
9d4996cbab
commit
4e923290cc
3 changed files with 115 additions and 31 deletions
|
|
@ -1,8 +1,9 @@
|
|||
//! Decorate content and apply alignment.
|
||||
use crate::backend::{self, Backend};
|
||||
use crate::defaults::Defaults;
|
||||
use crate::defaults::{self, Defaults};
|
||||
use crate::{Primitive, Renderer, Vector};
|
||||
|
||||
use iced_native::container;
|
||||
use iced_native::layout::{self, Layout};
|
||||
use iced_native::{Element, Point, Rectangle, Size, Text};
|
||||
|
||||
|
|
@ -19,7 +20,7 @@ impl<B> iced_native::tooltip::Renderer for Renderer<B>
|
|||
where
|
||||
B: Backend + backend::Text,
|
||||
{
|
||||
type Style = ();
|
||||
const DEFAULT_PADDING: u16 = 5;
|
||||
|
||||
fn draw<Message>(
|
||||
&mut self,
|
||||
|
|
@ -30,10 +31,10 @@ where
|
|||
content: &Element<'_, Message, Self>,
|
||||
tooltip: &Text<Self>,
|
||||
position: Position,
|
||||
style_sheet: &<Self as container::Renderer>::Style,
|
||||
gap: u16,
|
||||
padding: u16,
|
||||
) -> Self::Output {
|
||||
let bounds = content_layout.bounds();
|
||||
|
||||
let (content, mouse_interaction) = content.draw(
|
||||
self,
|
||||
&defaults,
|
||||
|
|
@ -42,13 +43,26 @@ where
|
|||
viewport,
|
||||
);
|
||||
|
||||
let bounds = content_layout.bounds();
|
||||
|
||||
if bounds.contains(cursor_position) {
|
||||
use iced_native::Widget;
|
||||
|
||||
let gap = f32::from(gap);
|
||||
let padding = f32::from(padding);
|
||||
let style = style_sheet.style();
|
||||
|
||||
let defaults = Defaults {
|
||||
text: defaults::Text {
|
||||
color: style.text_color.unwrap_or(defaults.text.color),
|
||||
},
|
||||
};
|
||||
|
||||
let tooltip_layout = Widget::<(), Self>::layout(
|
||||
tooltip,
|
||||
self,
|
||||
&layout::Limits::new(Size::ZERO, viewport.size()),
|
||||
&layout::Limits::new(Size::ZERO, viewport.size())
|
||||
.pad(f32::from(padding)),
|
||||
);
|
||||
|
||||
let tooltip_bounds = tooltip_layout.bounds();
|
||||
|
|
@ -59,22 +73,23 @@ where
|
|||
let y_center =
|
||||
bounds.y + (bounds.height - tooltip_bounds.height) / 2.0;
|
||||
|
||||
let gap = f32::from(gap);
|
||||
|
||||
let offset = match position {
|
||||
Position::Top => Vector::new(
|
||||
x_center,
|
||||
bounds.y - tooltip_bounds.height - gap,
|
||||
bounds.y - tooltip_bounds.height - gap - padding,
|
||||
),
|
||||
Position::Bottom => Vector::new(
|
||||
x_center,
|
||||
bounds.y + bounds.height + gap + padding,
|
||||
),
|
||||
Position::Left => Vector::new(
|
||||
bounds.x - tooltip_bounds.width - gap - padding,
|
||||
y_center,
|
||||
),
|
||||
Position::Right => Vector::new(
|
||||
bounds.x + bounds.width + gap + padding,
|
||||
y_center,
|
||||
),
|
||||
Position::Bottom => {
|
||||
Vector::new(x_center, bounds.y + bounds.height + gap)
|
||||
}
|
||||
Position::Left => {
|
||||
Vector::new(bounds.x - tooltip_bounds.width - gap, y_center)
|
||||
}
|
||||
Position::Right => {
|
||||
Vector::new(bounds.x + bounds.width + gap, y_center)
|
||||
}
|
||||
Position::FollowCursor => Vector::new(
|
||||
cursor_position.x,
|
||||
cursor_position.y - tooltip_bounds.height,
|
||||
|
|
@ -84,19 +99,42 @@ where
|
|||
let (tooltip, _) = Widget::<(), Self>::draw(
|
||||
tooltip,
|
||||
self,
|
||||
defaults,
|
||||
&defaults,
|
||||
Layout::with_offset(offset, &tooltip_layout),
|
||||
cursor_position,
|
||||
viewport,
|
||||
);
|
||||
|
||||
let tooltip_bounds = Rectangle {
|
||||
x: offset.x - padding,
|
||||
y: offset.y - padding,
|
||||
width: tooltip_bounds.width + padding * 2.0,
|
||||
height: tooltip_bounds.height + padding * 2.0,
|
||||
};
|
||||
|
||||
(
|
||||
Primitive::Clip {
|
||||
bounds: *viewport,
|
||||
offset: Vector::new(0, 0),
|
||||
content: Box::new(Primitive::Group {
|
||||
primitives: vec![content, tooltip],
|
||||
}),
|
||||
Primitive::Group {
|
||||
primitives: vec![
|
||||
content,
|
||||
Primitive::Clip {
|
||||
bounds: *viewport,
|
||||
offset: Vector::new(0, 0),
|
||||
content: Box::new(
|
||||
if let Some(background) =
|
||||
crate::container::background(
|
||||
tooltip_bounds,
|
||||
&style,
|
||||
)
|
||||
{
|
||||
Primitive::Group {
|
||||
primitives: vec![background, tooltip],
|
||||
}
|
||||
} else {
|
||||
tooltip
|
||||
},
|
||||
),
|
||||
},
|
||||
],
|
||||
},
|
||||
mouse_interaction,
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue