tooltips PoC
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
use crate::{
|
||||
animation::{Animation, AnimationEasing},
|
||||
components::{Component, ComponentBase, ComponentTrait, InitData},
|
||||
components::{self, Component, ComponentBase, ComponentTrait, InitData, tooltip::ComponentTooltip},
|
||||
drawing::{self, Boundary, Color},
|
||||
event::{CallbackDataCommon, EventListenerCollection, EventListenerKind, ListenerHandleVec},
|
||||
i18n::Translation,
|
||||
layout::{WidgetID, WidgetPair},
|
||||
layout::{LayoutTask, WidgetID, WidgetPair},
|
||||
renderer_vk::{
|
||||
text::{FontWeight, TextStyle},
|
||||
util::centered_matrix,
|
||||
@@ -55,6 +55,8 @@ struct State {
|
||||
hovered: bool,
|
||||
down: bool,
|
||||
on_click: Option<ButtonClickCallback>,
|
||||
|
||||
tooltip: Option<ComponentTooltip>,
|
||||
}
|
||||
|
||||
struct Data {
|
||||
@@ -158,7 +160,26 @@ fn register_event_mouse_enter<U1, U2>(
|
||||
event_data.widget_id,
|
||||
true,
|
||||
));
|
||||
state.borrow_mut().hovered = true;
|
||||
let mut state = state.borrow_mut();
|
||||
|
||||
// todo
|
||||
/*common.alterables.tasks.push(LayoutTask::ModifyLayoutState({
|
||||
let parent = data.id_rect.clone();
|
||||
Box::new(move |m| {
|
||||
components::tooltip::construct(
|
||||
&mut ConstructEssentials {
|
||||
layout: m.layout,
|
||||
listeners: &listeners,
|
||||
parent,
|
||||
},
|
||||
components::tooltip::Params {
|
||||
text: Translation::from_raw_text("this is a tooltip"),
|
||||
},
|
||||
);
|
||||
})
|
||||
}));*/
|
||||
|
||||
state.hovered = true;
|
||||
Ok(EventResult::Pass)
|
||||
}),
|
||||
);
|
||||
@@ -182,7 +203,9 @@ fn register_event_mouse_leave<U1, U2>(
|
||||
event_data.widget_id,
|
||||
false,
|
||||
));
|
||||
state.borrow_mut().hovered = false;
|
||||
let mut state = state.borrow_mut();
|
||||
state.tooltip = None;
|
||||
state.hovered = false;
|
||||
Ok(EventResult::Pass)
|
||||
}),
|
||||
);
|
||||
@@ -266,7 +289,7 @@ fn register_event_mouse_release<U1, U2>(
|
||||
}
|
||||
|
||||
pub fn construct<U1, U2>(
|
||||
ess: ConstructEssentials<U1, U2>,
|
||||
ess: &mut ConstructEssentials<U1, U2>,
|
||||
params: Params,
|
||||
) -> anyhow::Result<(WidgetPair, Rc<ComponentButton>)> {
|
||||
let globals = ess.layout.state.globals.clone();
|
||||
@@ -358,6 +381,7 @@ pub fn construct<U1, U2>(
|
||||
down: false,
|
||||
hovered: false,
|
||||
on_click: None,
|
||||
tooltip: None,
|
||||
}));
|
||||
|
||||
let mut base = ComponentBase::default();
|
||||
|
||||
@@ -248,7 +248,7 @@ fn register_event_mouse_release<U1, U2>(
|
||||
}
|
||||
|
||||
pub fn construct<U1, U2>(
|
||||
ess: ConstructEssentials<U1, U2>,
|
||||
ess: &mut ConstructEssentials<U1, U2>,
|
||||
params: Params,
|
||||
) -> anyhow::Result<(WidgetPair, Rc<ComponentCheckbox>)> {
|
||||
let mut style = params.style;
|
||||
|
||||
@@ -8,6 +8,7 @@ use crate::{
|
||||
pub mod button;
|
||||
pub mod checkbox;
|
||||
pub mod slider;
|
||||
pub mod tooltip;
|
||||
|
||||
pub struct InitData<'a> {
|
||||
pub common: &'a mut CallbackDataCommon<'a>,
|
||||
|
||||
@@ -308,7 +308,7 @@ fn register_event_mouse_release<U1, U2>(
|
||||
}
|
||||
|
||||
pub fn construct<U1, U2>(
|
||||
ess: ConstructEssentials<U1, U2>,
|
||||
ess: &mut ConstructEssentials<U1, U2>,
|
||||
params: Params,
|
||||
) -> anyhow::Result<(WidgetPair, Rc<ComponentSlider>)> {
|
||||
let mut style = params.style;
|
||||
|
||||
158
wgui/src/components/tooltip.rs
Normal file
158
wgui/src/components/tooltip.rs
Normal file
@@ -0,0 +1,158 @@
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
use taffy::prelude::length;
|
||||
|
||||
use crate::{
|
||||
components::{Component, ComponentBase, ComponentTrait, InitData},
|
||||
drawing::Color,
|
||||
event::{EventListenerCollection, EventListenerKind, ListenerHandleVec},
|
||||
i18n::Translation,
|
||||
layout::{LayoutTasks, WidgetID, WidgetPair},
|
||||
renderer_vk::text::{FontWeight, TextStyle},
|
||||
widget::{
|
||||
ConstructEssentials, EventResult,
|
||||
label::{WidgetLabel, WidgetLabelParams},
|
||||
rectangle::{WidgetRectangle, WidgetRectangleParams},
|
||||
util::WLength,
|
||||
},
|
||||
};
|
||||
|
||||
pub struct Params {
|
||||
pub text: Translation,
|
||||
}
|
||||
|
||||
impl Default for Params {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
text: Translation::from_raw_text(""),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct State {}
|
||||
|
||||
#[allow(clippy::struct_field_names)]
|
||||
struct Data {
|
||||
id_container: WidgetID, // Rectangle
|
||||
id_label: WidgetID, // Label, parent of container
|
||||
}
|
||||
|
||||
pub struct ComponentTooltip {
|
||||
base: ComponentBase,
|
||||
data: Rc<Data>,
|
||||
state: Rc<RefCell<State>>,
|
||||
tasks: LayoutTasks,
|
||||
}
|
||||
|
||||
impl ComponentTrait for ComponentTooltip {
|
||||
fn base(&mut self) -> &mut ComponentBase {
|
||||
&mut self.base
|
||||
}
|
||||
|
||||
fn init(&self, _data: &mut InitData) {}
|
||||
}
|
||||
|
||||
impl ComponentTooltip {}
|
||||
|
||||
fn register_event_mouse_enter<U1, U2>(
|
||||
data: &Rc<Data>,
|
||||
state: Rc<RefCell<State>>,
|
||||
listeners: &mut EventListenerCollection<U1, U2>,
|
||||
listener_handles: &mut ListenerHandleVec,
|
||||
) {
|
||||
listeners.register(
|
||||
listener_handles,
|
||||
data.id_container,
|
||||
EventListenerKind::MouseEnter,
|
||||
Box::new(move |common, event_data, _, _| {
|
||||
common.alterables.trigger_haptics();
|
||||
Ok(EventResult::Pass)
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
fn register_event_mouse_leave<U1, U2>(
|
||||
data: &Rc<Data>,
|
||||
state: Rc<RefCell<State>>,
|
||||
listeners: &mut EventListenerCollection<U1, U2>,
|
||||
listener_handles: &mut ListenerHandleVec,
|
||||
) {
|
||||
listeners.register(
|
||||
listener_handles,
|
||||
data.id_container,
|
||||
EventListenerKind::MouseEnter,
|
||||
Box::new(move |common, event_data, _, _| {
|
||||
common.alterables.trigger_haptics();
|
||||
Ok(EventResult::Pass)
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
pub fn construct<U1, U2>(
|
||||
ess: &mut ConstructEssentials<U1, U2>,
|
||||
params: Params,
|
||||
) -> anyhow::Result<(WidgetPair, Rc<ComponentTooltip>)> {
|
||||
let style = taffy::Style {
|
||||
align_items: Some(taffy::AlignItems::Center),
|
||||
justify_content: Some(taffy::JustifyContent::Center),
|
||||
gap: length(4.0),
|
||||
padding: taffy::Rect {
|
||||
left: length(8.0),
|
||||
right: length(8.0),
|
||||
top: length(4.0),
|
||||
bottom: length(4.0),
|
||||
},
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let globals = ess.layout.state.globals.clone();
|
||||
|
||||
let (root, _) = ess.layout.add_child(
|
||||
ess.parent,
|
||||
WidgetRectangle::create(WidgetRectangleParams {
|
||||
color: Color::new(1.0, 1.0, 1.0, 0.0),
|
||||
border_color: Color::new(1.0, 1.0, 1.0, 0.0),
|
||||
round: WLength::Units(5.0),
|
||||
..Default::default()
|
||||
}),
|
||||
style,
|
||||
)?;
|
||||
|
||||
let id_container = root.id;
|
||||
|
||||
let (label, _node_label) = ess.layout.add_child(
|
||||
id_container,
|
||||
WidgetLabel::create(
|
||||
&mut globals.get(),
|
||||
WidgetLabelParams {
|
||||
content: params.text,
|
||||
style: TextStyle {
|
||||
weight: Some(FontWeight::Bold),
|
||||
..Default::default()
|
||||
},
|
||||
},
|
||||
),
|
||||
Default::default(),
|
||||
)?;
|
||||
|
||||
let data = Rc::new(Data {
|
||||
id_container,
|
||||
id_label: label.id,
|
||||
});
|
||||
|
||||
let state = Rc::new(RefCell::new(State {}));
|
||||
|
||||
let mut base = ComponentBase::default();
|
||||
|
||||
register_event_mouse_enter(&data, state.clone(), ess.listeners, &mut base.lhandles);
|
||||
register_event_mouse_leave(&data, state.clone(), ess.listeners, &mut base.lhandles);
|
||||
|
||||
let tooltip = Rc::new(ComponentTooltip {
|
||||
base,
|
||||
data,
|
||||
state,
|
||||
tasks: ess.layout.tasks.clone(),
|
||||
});
|
||||
|
||||
ess.layout.defer_component_init(Component(tooltip.clone()));
|
||||
Ok((root, tooltip))
|
||||
}
|
||||
Reference in New Issue
Block a user