-
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
1 parent
8609f1b
commit 6e9925b
Showing
20 changed files
with
174 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,80 @@ | ||
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>, | ||
} | ||
|
||
impl Declare for Tooltips { | ||
type Builder = FatObj<()>; | ||
fn declarer() -> Self::Builder { FatObj::new(()) } | ||
} | ||
|
||
impl Tooltips { | ||
fn tooltips(&self) -> &CowArc<str> { &self.tooltips } | ||
} | ||
|
||
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 wnd = BuildCtx::get().window(); | ||
let mut child = FatObj::new(child); | ||
|
||
watch!($child.is_hover()) | ||
.distinct_until_changed() | ||
.filter(|v| *v) | ||
.subscribe(move |_| { | ||
let this = this.clone_writer(); | ||
let overlay = Overlay::new( | ||
text! { | ||
text: pipe!($this.tooltips().clone()), | ||
class: TOOLTIPS, | ||
}, OverlayStyle { | ||
auto_close_policy: AutoClosePolicy::NOT_AUTO_CLOSE, | ||
mask: None, | ||
}); | ||
|
||
overlay.clone().show_map(move |w: Widget| { | ||
let overlay = overlay.clone(); | ||
watch!($child.is_hover()) | ||
.distinct_until_changed() | ||
.filter(|v| !v) | ||
.take(1) | ||
.subscribe(move |_| overlay.close()); | ||
let mut w = FatObj::new(w); | ||
let anchor_widget = w.get_global_anchor_widget().clone_writer(); | ||
@ $w { | ||
anchor: pipe!(Anchor::left(-$w.layout_size().width / 2.)), | ||
on_mounted: move|e| { | ||
let wid = $child.track_id(); | ||
anchor_widget.bottom_align_to(wid.clone(), $child.layout_size().height, e.window()); | ||
anchor_widget.left_align_to(wid, $child.layout_size().width / 2., e.window()); | ||
} | ||
}.into_widget() | ||
}, wnd.clone()); | ||
}); | ||
@ { child } | ||
} | ||
.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
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,52 @@ | ||
use ribir_core::prelude::*; | ||
|
||
class_names! { | ||
TOOLTIPS_BASE, | ||
} | ||
|
||
pub(super) fn init(classes: &mut Classes) { | ||
classes.insert(TOOLTIPS_BASE, |w| { | ||
fn_widget! { | ||
let w = FatObj::new(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, | ||
} | ||
} | ||
} | ||
.into_widget() | ||
}); | ||
|
||
classes.insert(TOOLTIPS, |w| { | ||
fn_widget! { | ||
let w = FatObj::new(w); | ||
let mut w = @ $w { | ||
class: TOOLTIPS_BASE, | ||
}; | ||
$w.write().keep_alive = true; | ||
|
||
let animate = part_writer!(&mut w.opacity) | ||
.transition(transitions::LINEAR.of(BuildCtx::get())); | ||
@ $w { | ||
on_disposed: move |_| { | ||
$w.write().opacity = 0.; | ||
let _ = watch!($animate.is_running()) | ||
.distinct_until_changed() | ||
.pairwise() | ||
.filter(|(old, new)| *old && !*new ) | ||
.take(1) | ||
.subscribe(move|_| { | ||
$w.write().keep_alive = false; | ||
}); | ||
} | ||
} | ||
} | ||
.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