Skip to content

Commit

Permalink
Improve ergonomics a bit for common cases
Browse files Browse the repository at this point in the history
  • Loading branch information
benfrankel committed Aug 20, 2024
1 parent 848786f commit 3e75e3f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl Default for TooltipContext {
target: Entity::PLACEHOLDER,
timer: 0,
cursor_pos: Vec2::ZERO,
tooltip: Tooltip::new(Entity::PLACEHOLDER),
tooltip: Tooltip::cursor(Entity::PLACEHOLDER),
}
}
}
Expand Down
38 changes: 20 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ use bevy_ecs::{
};
use bevy_hierarchy::BuildWorldChildren as _;
use bevy_render::view::Visibility;
use bevy_sprite::Anchor;
use bevy_text::{JustifyText, Text, TextSection, TextStyle};
use bevy_transform::TransformSystem;
use bevy_ui::{
Expand Down Expand Up @@ -200,8 +201,18 @@ pub struct Tooltip {
}

impl Tooltip {
/// Construct a new `Tooltip` with default behavior.
pub fn new(content: impl Into<TooltipContent>) -> Self {
/// Create a new anchor `Tooltip`.
pub fn anchor(content: impl Into<TooltipContent>, placement: Anchor) -> Self {
Self {
activation: TooltipActivation::IMMEDIATE,
transfer: TooltipTransfer::SHORT,
placement: placement.into(),
content: content.into(),
}
}

/// Create a new cursor `Tooltip`.
pub fn cursor(content: impl Into<TooltipContent>) -> Self {
Self {
activation: TooltipActivation::IDLE,
transfer: TooltipTransfer::NONE,
Expand All @@ -210,25 +221,10 @@ impl Tooltip {
}
}

/// Construct a new `Tooltip` from a single [`TextSection`] and default behavior.
pub fn from_section(value: impl Into<String>, style: TextStyle) -> Self {
Self::new(TooltipContent::Primary(Text::from_section(value, style)))
}

/// Construct a new `Tooltip` from a list of [`TextSection`]s and default behavior.
pub fn from_sections(sections: impl IntoIterator<Item = TextSection>) -> Self {
Self::new(TooltipContent::Primary(Text::from_sections(sections)))
}

/// Construct a new `Tooltip` from a given [`Text`] and default behavior.
pub fn from_text(text: impl Into<Text>) -> Self {
Self::new(TooltipContent::Primary(text.into()))
}

/// Set [`JustifyText`].
///
/// NOTE: This does nothing for custom tooltips.
pub fn with_justify_text(mut self, justify_text: JustifyText) -> Self {
pub fn with_justify(mut self, justify_text: JustifyText) -> Self {
// TODO: Warn otherwise?
if let TooltipContent::Primary(text) = &mut self.content {
text.justify = justify_text;
Expand Down Expand Up @@ -418,6 +414,12 @@ impl From<TextSection> for TooltipContent {
}
}

impl From<Vec<TextSection>> for TooltipContent {
fn from(value: Vec<TextSection>) -> Self {
Self::Primary(Text::from_sections(value))
}
}

impl From<Text> for TooltipContent {
fn from(value: Text) -> Self {
Self::Primary(value)
Expand Down

0 comments on commit 3e75e3f

Please sign in to comment.