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))
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
use std::{
|
||||
cell::{RefCell, RefMut},
|
||||
cell::{Ref, RefCell, RefMut},
|
||||
collections::HashSet,
|
||||
rc::Rc,
|
||||
};
|
||||
@@ -10,7 +10,7 @@ use slotmap::SecondaryMap;
|
||||
use crate::{
|
||||
animation::{self, Animation},
|
||||
i18n::I18n,
|
||||
layout::{LayoutState, WidgetID},
|
||||
layout::{LayoutState, LayoutTask, WidgetID},
|
||||
stack::{ScissorStack, Transform, TransformStack},
|
||||
widget::{EventResult, WidgetData, WidgetObj},
|
||||
};
|
||||
@@ -100,6 +100,7 @@ pub struct EventAlterables {
|
||||
pub widgets_to_tick: HashSet<WidgetID>, // widgets which needs to be ticked in the next `Layout::update()` fn
|
||||
pub transform_stack: TransformStack,
|
||||
pub scissor_stack: ScissorStack,
|
||||
pub tasks: Vec<LayoutTask>,
|
||||
pub needs_redraw: bool,
|
||||
pub trigger_haptics: bool,
|
||||
}
|
||||
@@ -239,7 +240,7 @@ impl<U1, U2> EventListenerVec<U1, U2> {
|
||||
}
|
||||
|
||||
pub struct EventListenerCollection<U1, U2> {
|
||||
map: SecondaryMap<WidgetID, EventListenerVec<U1, U2>>,
|
||||
pub map: SecondaryMap<WidgetID, EventListenerVec<U1, U2>>,
|
||||
needs_gc: Rc<RefCell<bool>>,
|
||||
}
|
||||
|
||||
@@ -291,12 +292,14 @@ impl<U1, U2> EventListenerCollection<U1, U2> {
|
||||
|
||||
// clean-up expired events
|
||||
pub fn gc(&mut self) {
|
||||
{
|
||||
let mut needs_gc = self.needs_gc.borrow_mut();
|
||||
if !*needs_gc {
|
||||
return;
|
||||
}
|
||||
|
||||
*needs_gc = false;
|
||||
}
|
||||
|
||||
let mut count = 0;
|
||||
|
||||
@@ -315,8 +318,4 @@ impl<U1, U2> EventListenerCollection<U1, U2> {
|
||||
|
||||
log::debug!("EventListenerCollection: cleaned-up {count} expired events");
|
||||
}
|
||||
|
||||
pub fn get(&self, widget_id: WidgetID) -> Option<&EventListenerVec<U1, U2>> {
|
||||
self.map.get(widget_id)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,8 +110,15 @@ pub struct LayoutState {
|
||||
pub tree: taffy::tree::TaffyTree<WidgetID>,
|
||||
}
|
||||
|
||||
pub struct ModifyLayoutStateData<'a> {
|
||||
pub layout: &'a mut Layout,
|
||||
// don't uncomment this, todo!
|
||||
// pub listeners: &'a mut EventListenerCollection<U1, U2>,
|
||||
}
|
||||
|
||||
pub enum LayoutTask {
|
||||
RemoveWidget(WidgetID),
|
||||
ModifyLayoutState(Box<dyn Fn(ModifyLayoutStateData)>),
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -305,7 +312,7 @@ impl Layout {
|
||||
|
||||
fn push_event_children<U1, U2>(
|
||||
&self,
|
||||
listeners: &EventListenerCollection<U1, U2>,
|
||||
listeners: &mut EventListenerCollection<U1, U2>,
|
||||
parent_node_id: taffy::NodeId,
|
||||
event: &event::Event,
|
||||
alterables: &mut EventAlterables,
|
||||
@@ -345,7 +352,7 @@ impl Layout {
|
||||
|
||||
fn push_event_widget<U1, U2>(
|
||||
&self,
|
||||
listeners: &EventListenerCollection<U1, U2>,
|
||||
listeners: &mut EventListenerCollection<U1, U2>,
|
||||
node_id: taffy::NodeId,
|
||||
event: &event::Event,
|
||||
alterables: &mut EventAlterables,
|
||||
@@ -396,8 +403,7 @@ impl Layout {
|
||||
style,
|
||||
};
|
||||
|
||||
let listeners_vec = listeners.get(widget_id);
|
||||
|
||||
let listeners_vec = listeners.map.get(widget_id);
|
||||
let this_evt_result = widget.process_event(widget_id, listeners_vec, node_id, event, user_data, &mut params)?;
|
||||
if this_evt_result != EventResult::Pass {
|
||||
evt_result = this_evt_result;
|
||||
@@ -585,11 +591,16 @@ impl Layout {
|
||||
LayoutTask::RemoveWidget(widget_id) => {
|
||||
self.remove_widget(widget_id);
|
||||
}
|
||||
LayoutTask::ModifyLayoutState(_fn) => todo!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn process_alterables(&mut self, alterables: EventAlterables) -> anyhow::Result<()> {
|
||||
for task in alterables.tasks {
|
||||
self.tasks.push(task);
|
||||
}
|
||||
|
||||
self.process_tasks();
|
||||
|
||||
for node in alterables.dirty_nodes {
|
||||
|
||||
@@ -61,11 +61,7 @@ pub fn parse_component_button<'a, U1, U2>(
|
||||
}
|
||||
|
||||
let (widget, component) = button::construct(
|
||||
ConstructEssentials {
|
||||
layout: ctx.layout,
|
||||
listeners: ctx.listeners,
|
||||
parent: parent_id,
|
||||
},
|
||||
&mut ctx.get_construct_essentials(parent_id),
|
||||
button::Params {
|
||||
color,
|
||||
border,
|
||||
|
||||
@@ -37,11 +37,7 @@ pub fn parse_component_checkbox<U1, U2>(
|
||||
}
|
||||
|
||||
let (widget, component) = checkbox::construct(
|
||||
ConstructEssentials {
|
||||
layout: ctx.layout,
|
||||
listeners: ctx.listeners,
|
||||
parent: parent_id,
|
||||
},
|
||||
&mut ctx.get_construct_essentials(parent_id),
|
||||
checkbox::Params {
|
||||
box_size,
|
||||
text: translation,
|
||||
|
||||
@@ -33,7 +33,7 @@ pub fn parse_component_slider<U1, U2>(
|
||||
}
|
||||
|
||||
let (widget, component) = slider::construct(
|
||||
ConstructEssentials {
|
||||
&mut ConstructEssentials {
|
||||
layout: ctx.layout,
|
||||
listeners: ctx.listeners,
|
||||
parent: parent_id,
|
||||
|
||||
@@ -19,6 +19,7 @@ use crate::{
|
||||
component_slider::parse_component_slider, widget_div::parse_widget_div, widget_label::parse_widget_label,
|
||||
widget_rectangle::parse_widget_rectangle, widget_sprite::parse_widget_sprite,
|
||||
},
|
||||
widget::ConstructEssentials,
|
||||
};
|
||||
use ouroboros::self_referencing;
|
||||
use smallvec::SmallVec;
|
||||
@@ -303,6 +304,14 @@ struct ParserContext<'a, U1, U2> {
|
||||
}
|
||||
|
||||
impl<U1, U2> ParserContext<'_, U1, U2> {
|
||||
const fn get_construct_essentials(&mut self, parent: WidgetID) -> ConstructEssentials<'_, U1, U2> {
|
||||
ConstructEssentials {
|
||||
layout: self.layout,
|
||||
listeners: self.listeners,
|
||||
parent,
|
||||
}
|
||||
}
|
||||
|
||||
fn get_template(&self, name: &str) -> Option<Rc<Template>> {
|
||||
// find in local
|
||||
if let Some(template) = self.data_local.templates.get(name) {
|
||||
|
||||
Reference in New Issue
Block a user