-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): 🎸 Added the builtin widget of tooltips
- Loading branch information
Showing
21 changed files
with
182 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
use std::cell::RefCell; | ||
|
||
use crate::prelude::*; | ||
|
||
class_names! { | ||
#[doc = "Class name for the tooltips"] | ||
TOOLTIPS, | ||
} | ||
/// Add attributes of tooltips to Widget Declarer. | ||
/// | ||
/// ### Example: | ||
/// ```no_run | ||
/// use ribir::prelude::*; | ||
/// | ||
/// let w = fn_widget! { | ||
/// @FilledButton{ | ||
/// text: "hover to show tooltips!", | ||
/// tooltips: "this is tooltips", | ||
/// } | ||
/// }; | ||
/// App::run(w); | ||
/// ``` | ||
#[derive(Default)] | ||
pub struct Tooltips { | ||
pub tooltips: CowArc<str>, | ||
|
||
overlay: RefCell<Option<Overlay>>, | ||
} | ||
|
||
impl Declare for Tooltips { | ||
type Builder = FatObj<()>; | ||
fn declarer() -> Self::Builder { FatObj::new(()) } | ||
} | ||
|
||
impl Tooltips { | ||
fn tooltips(&self) -> &CowArc<str> { &self.tooltips } | ||
|
||
pub fn show(&self, wnd: Sc<Window>) { | ||
if let Some(overlay) = self.overlay.borrow().clone() { | ||
if !overlay.is_showing() { | ||
overlay.show(wnd); | ||
} | ||
} | ||
} | ||
|
||
pub fn hidden(&self) { | ||
if let Some(overlay) = self.overlay.borrow().clone() { | ||
if overlay.is_showing() { | ||
overlay.close(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl<'c> ComposeChild<'c> for Tooltips { | ||
type Child = Widget<'c>; | ||
fn compose_child(this: impl StateWriter<Value = Self>, child: Self::Child) -> Widget<'c> { | ||
fn_widget! { | ||
let mut child = FatObj::new(child); | ||
*$this.overlay.borrow_mut() = Some(Overlay::new( | ||
move || { | ||
let w = @Text { | ||
text: pipe!($this.tooltips().clone()), | ||
class: TOOLTIPS, | ||
}; | ||
|
||
@ $w { | ||
global_anchor_x: pipe!( | ||
GlobalAnchorX::center_align_to( | ||
$child.track_id(), 0. | ||
).always_follow() | ||
), | ||
global_anchor_y: pipe!( | ||
GlobalAnchorY::bottom_align_to( | ||
$child.track_id(), $child.layout_size().height | ||
).always_follow() | ||
), | ||
}.into_widget() | ||
}, OverlayStyle { | ||
auto_close_policy: AutoClosePolicy::NOT_AUTO_CLOSE, | ||
mask: None, | ||
} | ||
)); | ||
|
||
let wnd = BuildCtx::get().window(); | ||
let u = watch!($child.is_hover()) | ||
.distinct_until_changed() | ||
.subscribe(move |v| { | ||
if v { | ||
$this.show(wnd.clone()); | ||
} else { | ||
$this.hidden(); | ||
} | ||
}); | ||
|
||
@ $child { | ||
on_disposed: move|_| { | ||
u.unsubscribe(); | ||
$this.hidden(); | ||
}, | ||
} | ||
} | ||
.into_widget() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -214,6 +214,7 @@ impl Overlay { | |
} | ||
} | ||
}, | ||
@{ w } | ||
}.into_widget(); | ||
}; | ||
if close_policy.contains(AutoClosePolicy::ESC) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use ribir_core::prelude::*; | ||
|
||
use crate::md; | ||
|
||
pub(super) fn init(classes: &mut Classes) { | ||
classes.insert(TOOLTIPS, |w| { | ||
fn_widget! { | ||
let w = FatObj::new(w); | ||
let mut w = @BoxDecoration { | ||
background: Palette::of(BuildCtx::get()).inverse_surface(), | ||
margin: EdgeInsets::only_bottom(4.), | ||
border_radius: Radius::all(4.), | ||
@ $w { | ||
margin: EdgeInsets::new(4., 8., 4., 8.), | ||
foreground: Palette::of(BuildCtx::get()).inverse_on_surface(), | ||
v_align: VAlign::Center, | ||
h_align: HAlign::Center, | ||
} | ||
}; | ||
let animate = part_writer!(&mut w.opacity) | ||
.transition(EasingTransition{ | ||
easing: md::easing::STANDARD_ACCELERATE, | ||
duration: md::easing::duration::SHORT2 | ||
}.box_it()); | ||
@ $w { | ||
keep_alive: pipe!($animate.is_running() || $w.opacity != 0.), | ||
on_disposed: move |_| { | ||
$w.write().opacity = 0.; | ||
} | ||
} | ||
} | ||
.into_widget() | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters