Skip to content

Commit

Permalink
feat: add status tooltips (#32011)
Browse files Browse the repository at this point in the history
* feat: add status tooltips

* rebase and review fix

---------

Co-authored-by: atbrakhi <[email protected]>
  • Loading branch information
iterminatorheart and atbrakhi committed May 21, 2024
1 parent 67e556e commit 9d57c0d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
33 changes: 30 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 @@ -49,6 +49,8 @@ pub struct Minibrowser {
location_dirty: Cell<bool>,

load_status: LoadStatus,

status_text: Option<String>,
}

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

Expand Down Expand Up @@ -235,6 +238,19 @@ impl Minibrowser {
return;
};
let mut embedder_events = vec![];

if let Some(status_text) = &self.status_text {
let position = Some(pos2(0.0, ctx.available_rect().max.y));
egui::containers::popup::show_tooltip_at(
ctx,
"tooltip_for_status_text".into(),
position,
|ui| {
ui.label(status_text.clone());
},
);
}

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

pub fn update_status_text(
&mut self,
browser: &mut WebViewManager<dyn WindowPortsMethods>,
) -> bool {
let need_update = browser.status_text() != self.status_text;
self.status_text = browser.status_text();
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 @@ -399,6 +424,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_status_text(browser)
}
}
11 changes: 9 additions & 2 deletions ports/servoshell/webview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use crate::window_trait::{WindowPortsMethods, LINE_HEIGHT};
pub struct WebViewManager<Window: WindowPortsMethods + ?Sized> {
current_url: Option<ServoUrl>,
current_url_string: Option<String>,
status_text: Option<String>,

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

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

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

0 comments on commit 9d57c0d

Please sign in to comment.