From a326a60c1646cde1a8b34b70d7f632b341644d0a Mon Sep 17 00:00:00 2001 From: atbrakhi Date: Wed, 6 Dec 2023 11:03:15 +0100 Subject: [PATCH] Minibrowser: Add Back and Forward navigation (#30805) * add back and forward button * use left_to_right egui layout * use nested allocate_ui_with_layout --- ports/servoshell/minibrowser.rs | 67 ++++++++++++++++++++++++--------- 1 file changed, 49 insertions(+), 18 deletions(-) diff --git a/ports/servoshell/minibrowser.rs b/ports/servoshell/minibrowser.rs index 5ed322aa7fa..3e16e4e3447 100644 --- a/ports/servoshell/minibrowser.rs +++ b/ports/servoshell/minibrowser.rs @@ -10,6 +10,7 @@ use egui::{Key, Modifiers, TopBottomPanel}; use euclid::Length; use log::{trace, warn}; use servo::compositing::windowing::EmbedderEvent; +use servo::msg::constellation_msg::TraversalDirection; use servo::servo_geometry::DeviceIndependentPixel; use servo::servo_url::ServoUrl; use servo::webrender_surfman::WebrenderSurfman; @@ -34,6 +35,8 @@ pub struct Minibrowser { pub enum MinibrowserEvent { /// Go button clicked. Go, + Back, + Forward, } impl Minibrowser { @@ -83,29 +86,43 @@ impl Minibrowser { TopBottomPanel::top("toolbar").show(ctx, |ui| { ui.allocate_ui_with_layout( ui.available_size(), - egui::Layout::right_to_left(egui::Align::Center), + egui::Layout::left_to_right(egui::Align::Center), |ui| { - if ui.button("go").clicked() { - event_queue.borrow_mut().push(MinibrowserEvent::Go); - location_dirty.set(false); + if ui.button("back").clicked() { + event_queue.borrow_mut().push(MinibrowserEvent::Back); + } + if ui.button("forward").clicked() { + event_queue.borrow_mut().push(MinibrowserEvent::Forward); } - let location_field = ui.add_sized( + ui.allocate_ui_with_layout( ui.available_size(), - egui::TextEdit::singleline(&mut *location.borrow_mut()), + egui::Layout::right_to_left(egui::Align::Center), + |ui| { + if ui.button("go").clicked() { + event_queue.borrow_mut().push(MinibrowserEvent::Go); + location_dirty.set(false); + } + + let location_field = ui.add_sized( + ui.available_size(), + egui::TextEdit::singleline(&mut *location.borrow_mut()), + ); + + if location_field.changed() { + location_dirty.set(true); + } + if ui.input(|i| i.clone().consume_key(Modifiers::COMMAND, Key::L)) { + location_field.request_focus(); + } + if location_field.lost_focus() && + ui.input(|i| i.clone().key_pressed(Key::Enter)) + { + event_queue.borrow_mut().push(MinibrowserEvent::Go); + location_dirty.set(false); + } + }, ); - if location_field.changed() { - location_dirty.set(true); - } - if ui.input(|i| i.clone().consume_key(Modifiers::COMMAND, Key::L)) { - location_field.request_focus(); - } - if location_field.lost_focus() && - ui.input(|i| i.clone().key_pressed(Key::Enter)) - { - event_queue.borrow_mut().push(MinibrowserEvent::Go); - location_dirty.set(false); - } }, ); }); @@ -139,6 +156,20 @@ impl Minibrowser { break; } }, + MinibrowserEvent::Back => { + let browser_id = browser.browser_id().unwrap(); + app_event_queue.push(EmbedderEvent::Navigation( + browser_id, + TraversalDirection::Back(1), + )); + }, + MinibrowserEvent::Forward => { + let browser_id = browser.browser_id().unwrap(); + app_event_queue.push(EmbedderEvent::Navigation( + browser_id, + TraversalDirection::Forward(1), + )); + }, } } }