Skip to content

Commit

Permalink
feat: add status tooltips
Browse files Browse the repository at this point in the history
  • Loading branch information
xiandu.wl committed Apr 7, 2024
1 parent e0e3408 commit a807737
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
37 changes: 34 additions & 3 deletions ports/servoshell/minibrowser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use std::sync::Arc;
use std::time::Instant;

use egui::{
CentralPanel, Color32, Frame, Key, Modifiers, PaintCallback, Pos2, Spinner, TopBottomPanel,
Vec2,
pos2, CentralPanel, Color32, Frame, Key, Modifiers, PaintCallback, Pos2, Spinner,
TopBottomPanel, Vec2,
};
use egui_glow::CallbackFn;
use egui_winit::EventResponse;
Expand Down Expand Up @@ -48,6 +48,8 @@ pub struct Minibrowser {
location_dirty: Cell<bool>,

load_status: LoadStatus,

hovered_link: Option<String>,
}

pub enum MinibrowserEvent {
Expand Down Expand Up @@ -90,6 +92,7 @@ impl Minibrowser {
location: RefCell::new(initial_url.to_string()),
location_dirty: false.into(),
load_status: LoadStatus::LoadComplete,
hovered_link: None,
}
}

Expand Down Expand Up @@ -217,6 +220,23 @@ impl Minibrowser {
return;
};
let mut embedder_events = vec![];

if self.hovered_link.is_some() {
TopBottomPanel::bottom("innner_hover_links")
.max_height(0.0)
.show(ctx, |ui| {
let position = Some(pos2(0.0, ui.cursor().max.y));
egui::containers::popup::show_tooltip_at(
ctx,
"tooltip_for_hovered_link".into(),
position,
|ui| {
ui.label(self.hovered_link.clone().unwrap().to_string());
},
)
});
}

CentralPanel::default()
.frame(Frame::none())
.show(ctx, |ui| {
Expand Down Expand Up @@ -371,6 +391,15 @@ impl Minibrowser {
need_update
}

pub fn update_hovered_link(
&mut self,
browser: &mut WebViewManager<dyn WindowPortsMethods>,
) -> bool {
let need_update = browser.hovered_link() != self.hovered_link;
self.hovered_link = browser.hovered_link();
need_update
}

/// Updates all fields taken from the given [WebViewManager], such as the location field.
/// Returns true iff the egui needs an update.
pub fn update_webview_data(
Expand All @@ -381,6 +410,8 @@ impl Minibrowser {
// because logical OR would short-circuit if any of the functions return true.
// We want to ensure that all functions are called. The "bitwise OR" operator
// does not short-circuit.
self.update_location_in_toolbar(browser) | self.update_spinner_in_toolbar(browser)
self.update_location_in_toolbar(browser) |
self.update_spinner_in_toolbar(browser) |
self.update_hovered_link(browser)
}
}
9 changes: 8 additions & 1 deletion ports/servoshell/webview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use crate::window_trait::{WindowPortsMethods, LINE_HEIGHT};
pub struct WebViewManager<Window: WindowPortsMethods + ?Sized> {
current_url: Option<ServoUrl>,
current_url_string: Option<String>,
hovered_link: Option<String>,

/// List of top-level browsing contexts.
/// Modified by EmbedderMsg::WebViewOpened and EmbedderMsg::WebViewClosed,
Expand Down Expand Up @@ -86,6 +87,7 @@ where
title: None,
current_url: None,
current_url_string: None,
hovered_link: None,
webviews: HashMap::default(),
creation_order: vec![],
focused_webview_id: None,
Expand Down Expand Up @@ -130,6 +132,10 @@ where
self.load_status
}

pub fn hovered_link(&self) -> Option<String> {
self.hovered_link.clone()
}

pub fn get_events(&mut self) -> Vec<EmbedderEvent> {
std::mem::take(&mut self.event_queue)
}
Expand Down Expand Up @@ -443,7 +449,8 @@ where
}
match msg {
EmbedderMsg::Status(_status) => {
// FIXME: surface this status string in the UI somehow
self.hovered_link = _status;
need_update = true;
},
EmbedderMsg::ChangePageTitle(title) => {
self.title = title;
Expand Down

0 comments on commit a807737

Please sign in to comment.