Minibrowser: Add Back and Forward navigation (#30805)

* add back and forward button

* use left_to_right egui layout

* use nested allocate_ui_with_layout
This commit is contained in:
atbrakhi 2023-12-06 11:03:15 +01:00 committed by GitHub
parent 8ded1072ce
commit a326a60c16
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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),
));
},
}
}
}