mirror of
https://github.com/zed-industries/zed.git
synced 2024-09-20 02:47:34 +03:00
Use a different fit mode for tooltips
This commit is contained in:
parent
f2a48c6b02
commit
8a146e49ca
@ -94,7 +94,7 @@ impl View for ContextMenu {
|
|||||||
|
|
||||||
Overlay::new(expanded_menu)
|
Overlay::new(expanded_menu)
|
||||||
.hoverable(true)
|
.hoverable(true)
|
||||||
.move_to_fit(true)
|
.fit_mode(OverlayFitMode::SnapToWindow)
|
||||||
.with_abs_position(self.position)
|
.with_abs_position(self.position)
|
||||||
.boxed()
|
.boxed()
|
||||||
}
|
}
|
||||||
|
@ -1,25 +1,31 @@
|
|||||||
use serde_json::json;
|
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
geometry::{rect::RectF, vector::Vector2F},
|
geometry::{rect::RectF, vector::Vector2F},
|
||||||
json::ToJson,
|
json::ToJson,
|
||||||
DebugContext, Element, ElementBox, Event, EventContext, LayoutContext, MouseRegion,
|
DebugContext, Element, ElementBox, Event, EventContext, LayoutContext, MouseRegion,
|
||||||
PaintContext, SizeConstraint,
|
PaintContext, SizeConstraint,
|
||||||
};
|
};
|
||||||
|
use serde_json::json;
|
||||||
|
|
||||||
pub struct Overlay {
|
pub struct Overlay {
|
||||||
child: ElementBox,
|
child: ElementBox,
|
||||||
abs_position: Option<Vector2F>,
|
abs_position: Option<Vector2F>,
|
||||||
move_to_fit: bool,
|
fit_mode: OverlayFitMode,
|
||||||
hoverable: bool,
|
hoverable: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Copy, Clone)]
|
||||||
|
pub enum OverlayFitMode {
|
||||||
|
SnapToWindow,
|
||||||
|
FlipAlignment,
|
||||||
|
None,
|
||||||
|
}
|
||||||
|
|
||||||
impl Overlay {
|
impl Overlay {
|
||||||
pub fn new(child: ElementBox) -> Self {
|
pub fn new(child: ElementBox) -> Self {
|
||||||
Self {
|
Self {
|
||||||
child,
|
child,
|
||||||
abs_position: None,
|
abs_position: None,
|
||||||
move_to_fit: false,
|
fit_mode: OverlayFitMode::None,
|
||||||
hoverable: false,
|
hoverable: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -29,8 +35,8 @@ impl Overlay {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn move_to_fit(mut self, align_to_fit: bool) -> Self {
|
pub fn fit_mode(mut self, fit_mode: OverlayFitMode) -> Self {
|
||||||
self.move_to_fit = align_to_fit;
|
self.fit_mode = fit_mode;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,7 +82,8 @@ impl Element for Overlay {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.move_to_fit {
|
match self.fit_mode {
|
||||||
|
OverlayFitMode::SnapToWindow => {
|
||||||
// Snap the right edge of the overlay to the right edge of the window if
|
// Snap the right edge of the overlay to the right edge of the window if
|
||||||
// its horizontal bounds overflow.
|
// its horizontal bounds overflow.
|
||||||
if bounds.lower_right().x() > cx.window_size.x() {
|
if bounds.lower_right().x() > cx.window_size.x() {
|
||||||
@ -89,6 +96,19 @@ impl Element for Overlay {
|
|||||||
bounds.set_origin_y((cx.window_size.y() - bounds.height()).max(0.));
|
bounds.set_origin_y((cx.window_size.y() - bounds.height()).max(0.));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
OverlayFitMode::FlipAlignment => {
|
||||||
|
// Right-align overlay if its horizontal bounds overflow.
|
||||||
|
if bounds.lower_right().x() > cx.window_size.x() {
|
||||||
|
bounds.set_origin_x(bounds.origin_x() - bounds.width());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bottom-align overlay if its vertical bounds overflow.
|
||||||
|
if bounds.lower_right().y() > cx.window_size.y() {
|
||||||
|
bounds.set_origin_y(bounds.origin_y() - bounds.height());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
OverlayFitMode::None => {}
|
||||||
|
}
|
||||||
|
|
||||||
self.child.paint(bounds.origin(), bounds, cx);
|
self.child.paint(bounds.origin(), bounds, cx);
|
||||||
cx.scene.pop_stacking_context();
|
cx.scene.pop_stacking_context();
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use super::{
|
use super::{
|
||||||
ContainerStyle, Element, ElementBox, Flex, KeystrokeLabel, MouseEventHandler, Overlay,
|
ContainerStyle, Element, ElementBox, Flex, KeystrokeLabel, MouseEventHandler, Overlay,
|
||||||
ParentElement, Text,
|
OverlayFitMode, ParentElement, Text,
|
||||||
};
|
};
|
||||||
use crate::{
|
use crate::{
|
||||||
fonts::TextStyle,
|
fonts::TextStyle,
|
||||||
@ -79,7 +79,7 @@ impl Tooltip {
|
|||||||
})
|
})
|
||||||
.boxed(),
|
.boxed(),
|
||||||
)
|
)
|
||||||
.move_to_fit(true)
|
.fit_mode(OverlayFitMode::FlipAlignment)
|
||||||
.with_abs_position(state.position.get())
|
.with_abs_position(state.position.get())
|
||||||
.boxed(),
|
.boxed(),
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user