2022-02-21 14:26:26 +00:00
|
|
|
use egui_extras::RetainedImage;
|
2022-01-15 19:34:03 +00:00
|
|
|
use poll_promise::Promise;
|
2020-11-17 23:43:58 +00:00
|
|
|
|
2020-11-18 20:38:29 +00:00
|
|
|
struct Resource {
|
|
|
|
/// HTTP response
|
2021-09-03 19:04:43 +00:00
|
|
|
response: ehttp::Response,
|
2020-11-18 20:38:29 +00:00
|
|
|
|
2021-08-15 14:56:46 +00:00
|
|
|
text: Option<String>,
|
|
|
|
|
2020-11-18 20:38:29 +00:00
|
|
|
/// If set, the response was an image.
|
2022-02-21 14:26:26 +00:00
|
|
|
image: Option<RetainedImage>,
|
2020-12-28 18:50:48 +00:00
|
|
|
|
|
|
|
/// If set, the response was text with some supported syntax highlighting (e.g. ".rs" or ".md").
|
|
|
|
colored_text: Option<ColoredText>,
|
2020-11-18 20:38:29 +00:00
|
|
|
}
|
|
|
|
|
2020-11-20 18:50:47 +00:00
|
|
|
impl Resource {
|
2021-10-07 19:31:58 +00:00
|
|
|
fn from_response(ctx: &egui::Context, response: ehttp::Response) -> Self {
|
2021-08-15 14:56:46 +00:00
|
|
|
let content_type = response.content_type().unwrap_or_default();
|
|
|
|
let image = if content_type.starts_with("image/") {
|
2022-02-21 14:26:26 +00:00
|
|
|
RetainedImage::from_image_bytes(&response.url, &response.bytes).ok()
|
2020-11-20 18:50:47 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2020-12-28 18:50:48 +00:00
|
|
|
|
2022-01-15 19:34:03 +00:00
|
|
|
let text = response.text();
|
|
|
|
let colored_text = text.and_then(|text| syntax_highlighting(ctx, &response, text));
|
|
|
|
let text = text.map(|text| text.to_owned());
|
2020-12-28 18:50:48 +00:00
|
|
|
|
|
|
|
Self {
|
|
|
|
response,
|
2021-08-15 14:56:46 +00:00
|
|
|
text,
|
2022-02-21 14:26:26 +00:00
|
|
|
image,
|
2020-12-28 18:50:48 +00:00
|
|
|
colored_text,
|
|
|
|
}
|
2020-11-20 18:50:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-29 06:45:13 +00:00
|
|
|
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
|
2021-01-01 16:11:05 +00:00
|
|
|
pub struct HttpApp {
|
2020-11-17 23:43:58 +00:00
|
|
|
url: String,
|
2021-01-01 16:11:05 +00:00
|
|
|
|
2021-09-29 06:45:13 +00:00
|
|
|
#[cfg_attr(feature = "serde", serde(skip))]
|
2022-01-15 19:34:03 +00:00
|
|
|
promise: Option<Promise<ehttp::Result<Resource>>>,
|
2020-11-17 22:24:14 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 16:11:05 +00:00
|
|
|
impl Default for HttpApp {
|
2020-11-17 22:24:14 +00:00
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
2020-11-17 23:43:58 +00:00
|
|
|
url: "https://raw.githubusercontent.com/emilk/egui/master/README.md".to_owned(),
|
2022-01-15 19:34:03 +00:00
|
|
|
promise: Default::default(),
|
2020-11-17 22:24:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-28 09:23:34 +00:00
|
|
|
impl eframe::App for HttpApp {
|
|
|
|
fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
|
2021-10-13 09:47:57 +00:00
|
|
|
egui::TopBottomPanel::bottom("http_bottom").show(ctx, |ui| {
|
|
|
|
let layout = egui::Layout::top_down(egui::Align::Center).with_main_justify(true);
|
|
|
|
ui.allocate_ui_with_layout(ui.available_size(), layout, |ui| {
|
2022-04-28 09:23:34 +00:00
|
|
|
ui.add(egui_demo_lib::egui_github_link_file!())
|
2021-10-13 09:47:57 +00:00
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2020-11-17 22:24:14 +00:00
|
|
|
egui::CentralPanel::default().show(ctx, |ui| {
|
2021-10-13 09:47:57 +00:00
|
|
|
let trigger_fetch = ui_url(ui, frame, &mut self.url);
|
|
|
|
|
|
|
|
ui.horizontal_wrapped(|ui| {
|
2021-09-03 19:04:43 +00:00
|
|
|
ui.spacing_mut().item_spacing.x = 0.0;
|
|
|
|
ui.label("HTTP requests made using ");
|
|
|
|
ui.hyperlink_to("ehttp", "https://www.github.com/emilk/ehttp");
|
|
|
|
ui.label(".");
|
|
|
|
});
|
2021-10-13 09:47:57 +00:00
|
|
|
|
|
|
|
if trigger_fetch {
|
2022-01-15 19:34:03 +00:00
|
|
|
let ctx = ctx.clone();
|
|
|
|
let (sender, promise) = Promise::new();
|
|
|
|
let request = ehttp::Request::get(&self.url);
|
2021-09-03 19:04:43 +00:00
|
|
|
ehttp::fetch(request, move |response| {
|
2022-03-15 16:21:52 +00:00
|
|
|
ctx.request_repaint(); // wake up UI thread
|
2022-01-15 19:34:03 +00:00
|
|
|
let resource = response.map(|response| Resource::from_response(&ctx, response));
|
|
|
|
sender.send(resource);
|
2020-11-17 23:43:58 +00:00
|
|
|
});
|
2022-01-15 19:34:03 +00:00
|
|
|
self.promise = Some(promise);
|
2020-11-17 23:43:58 +00:00
|
|
|
}
|
2020-11-17 22:24:14 +00:00
|
|
|
|
2020-11-18 20:38:29 +00:00
|
|
|
ui.separator();
|
|
|
|
|
2022-01-15 19:34:03 +00:00
|
|
|
if let Some(promise) = &self.promise {
|
|
|
|
if let Some(result) = promise.ready() {
|
|
|
|
match result {
|
|
|
|
Ok(resource) => {
|
|
|
|
ui_resource(ui, resource);
|
|
|
|
}
|
|
|
|
Err(error) => {
|
|
|
|
// This should only happen if the fetch API isn't available or something similar.
|
|
|
|
ui.colored_label(
|
2022-07-29 13:32:32 +00:00
|
|
|
ui.visuals().error_fg_color,
|
2022-01-15 19:34:03 +00:00
|
|
|
if error.is_empty() { "Error" } else { error },
|
|
|
|
);
|
|
|
|
}
|
2020-11-17 23:43:58 +00:00
|
|
|
}
|
2022-01-15 19:34:03 +00:00
|
|
|
} else {
|
2022-04-15 05:39:08 +00:00
|
|
|
ui.spinner();
|
2020-11-17 23:43:58 +00:00
|
|
|
}
|
2020-11-17 22:24:14 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2020-11-17 23:43:58 +00:00
|
|
|
|
2022-04-28 09:23:34 +00:00
|
|
|
fn ui_url(ui: &mut egui::Ui, frame: &mut eframe::Frame, url: &mut String) -> bool {
|
2020-11-18 20:38:29 +00:00
|
|
|
let mut trigger_fetch = false;
|
|
|
|
|
2021-10-13 09:47:57 +00:00
|
|
|
ui.horizontal(|ui| {
|
2020-11-18 20:38:29 +00:00
|
|
|
ui.label("URL:");
|
2021-10-13 09:47:57 +00:00
|
|
|
trigger_fetch |= ui
|
|
|
|
.add(egui::TextEdit::singleline(url).desired_width(f32::INFINITY))
|
|
|
|
.lost_focus();
|
2020-11-20 18:50:47 +00:00
|
|
|
});
|
2020-11-18 20:38:29 +00:00
|
|
|
|
2021-01-02 00:01:01 +00:00
|
|
|
if frame.is_web() {
|
|
|
|
ui.label("HINT: paste the url of this page into the field above!");
|
|
|
|
}
|
2020-12-28 18:50:48 +00:00
|
|
|
|
2020-11-20 18:50:47 +00:00
|
|
|
ui.horizontal(|ui| {
|
2021-01-25 17:50:19 +00:00
|
|
|
if ui.button("Source code for this example").clicked() {
|
2020-11-18 20:38:29 +00:00
|
|
|
*url = format!(
|
|
|
|
"https://raw.githubusercontent.com/emilk/egui/master/{}",
|
|
|
|
file!()
|
|
|
|
);
|
|
|
|
trigger_fetch = true;
|
|
|
|
}
|
2021-01-25 17:50:19 +00:00
|
|
|
if ui.button("Random image").clicked() {
|
2020-11-18 20:38:29 +00:00
|
|
|
let seed = ui.input().time;
|
2021-10-13 09:47:57 +00:00
|
|
|
let side = 640;
|
|
|
|
*url = format!("https://picsum.photos/seed/{}/{}", seed, side);
|
2020-11-18 20:38:29 +00:00
|
|
|
trigger_fetch = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-10-13 09:47:57 +00:00
|
|
|
trigger_fetch
|
2020-11-18 20:38:29 +00:00
|
|
|
}
|
|
|
|
|
2022-01-15 19:34:03 +00:00
|
|
|
fn ui_resource(ui: &mut egui::Ui, resource: &Resource) {
|
2020-12-28 18:50:48 +00:00
|
|
|
let Resource {
|
|
|
|
response,
|
2021-08-15 14:56:46 +00:00
|
|
|
text,
|
2022-02-21 14:26:26 +00:00
|
|
|
image,
|
2020-12-28 18:50:48 +00:00
|
|
|
colored_text,
|
|
|
|
} = resource;
|
2020-11-18 20:38:29 +00:00
|
|
|
|
|
|
|
ui.monospace(format!("url: {}", response.url));
|
2020-11-17 23:43:58 +00:00
|
|
|
ui.monospace(format!(
|
2020-11-18 20:38:29 +00:00
|
|
|
"status: {} ({})",
|
2020-11-17 23:43:58 +00:00
|
|
|
response.status, response.status_text
|
|
|
|
));
|
2020-11-18 20:38:29 +00:00
|
|
|
ui.monospace(format!(
|
2021-08-15 14:56:46 +00:00
|
|
|
"content-type: {}",
|
|
|
|
response.content_type().unwrap_or_default()
|
|
|
|
));
|
|
|
|
ui.monospace(format!(
|
|
|
|
"size: {:.1} kB",
|
2020-11-18 20:38:29 +00:00
|
|
|
response.bytes.len() as f32 / 1000.0
|
|
|
|
));
|
2020-11-17 23:43:58 +00:00
|
|
|
|
2021-08-15 14:56:46 +00:00
|
|
|
ui.separator();
|
|
|
|
|
2021-10-13 09:47:57 +00:00
|
|
|
egui::ScrollArea::vertical()
|
|
|
|
.auto_shrink([false; 2])
|
2021-08-15 14:56:46 +00:00
|
|
|
.show(ui, |ui| {
|
2021-10-13 09:47:57 +00:00
|
|
|
egui::CollapsingHeader::new("Response headers")
|
|
|
|
.default_open(false)
|
2021-08-15 14:56:46 +00:00
|
|
|
.show(ui, |ui| {
|
2021-10-13 09:47:57 +00:00
|
|
|
egui::Grid::new("response_headers")
|
|
|
|
.spacing(egui::vec2(ui.spacing().item_spacing.x * 2.0, 0.0))
|
|
|
|
.show(ui, |ui| {
|
|
|
|
for header in &response.headers {
|
|
|
|
ui.label(header.0);
|
|
|
|
ui.label(header.1);
|
|
|
|
ui.end_row();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
2021-08-15 14:56:46 +00:00
|
|
|
|
2021-10-13 09:47:57 +00:00
|
|
|
ui.separator();
|
2020-12-28 18:50:48 +00:00
|
|
|
|
2021-10-13 09:47:57 +00:00
|
|
|
if let Some(text) = &text {
|
|
|
|
let tooltip = "Click to copy the response body";
|
|
|
|
if ui.button("📋").on_hover_text(tooltip).clicked() {
|
|
|
|
ui.output().copied_text = text.clone();
|
|
|
|
}
|
|
|
|
ui.separator();
|
|
|
|
}
|
2020-12-28 18:50:48 +00:00
|
|
|
|
2022-02-21 14:26:26 +00:00
|
|
|
if let Some(image) = image {
|
|
|
|
let mut size = image.size_vec2();
|
2022-01-15 12:59:52 +00:00
|
|
|
size *= (ui.available_width() / size.x).min(1.0);
|
2022-02-21 14:26:26 +00:00
|
|
|
image.show_size(ui, size);
|
2021-10-07 19:08:01 +00:00
|
|
|
} else if let Some(colored_text) = colored_text {
|
|
|
|
colored_text.ui(ui);
|
|
|
|
} else if let Some(text) = &text {
|
2021-10-13 09:47:57 +00:00
|
|
|
selectable_text(ui, text);
|
2021-10-07 19:08:01 +00:00
|
|
|
} else {
|
|
|
|
ui.monospace("[binary]");
|
2020-12-28 18:50:48 +00:00
|
|
|
}
|
2021-10-07 19:08:01 +00:00
|
|
|
});
|
2020-12-28 18:50:48 +00:00
|
|
|
}
|
|
|
|
|
2021-10-13 09:47:57 +00:00
|
|
|
fn selectable_text(ui: &mut egui::Ui, mut text: &str) {
|
|
|
|
ui.add(
|
|
|
|
egui::TextEdit::multiline(&mut text)
|
|
|
|
.desired_width(f32::INFINITY)
|
2022-01-24 13:32:36 +00:00
|
|
|
.font(egui::TextStyle::Monospace),
|
2021-10-13 09:47:57 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-12-28 18:50:48 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Syntax highlighting:
|
|
|
|
|
2021-01-31 15:52:26 +00:00
|
|
|
#[cfg(feature = "syntect")]
|
2021-10-07 19:31:58 +00:00
|
|
|
fn syntax_highlighting(
|
|
|
|
ctx: &egui::Context,
|
|
|
|
response: &ehttp::Response,
|
|
|
|
text: &str,
|
|
|
|
) -> Option<ColoredText> {
|
2020-12-28 18:50:48 +00:00
|
|
|
let extension_and_rest: Vec<&str> = response.url.rsplitn(2, '.').collect();
|
|
|
|
let extension = extension_and_rest.get(0)?;
|
2021-10-07 19:31:58 +00:00
|
|
|
let theme = crate::syntax_highlighting::CodeTheme::from_style(&ctx.style());
|
|
|
|
Some(ColoredText(crate::syntax_highlighting::highlight(
|
|
|
|
ctx, &theme, text, extension,
|
|
|
|
)))
|
2020-12-28 18:50:48 +00:00
|
|
|
}
|
|
|
|
|
2021-10-13 09:47:57 +00:00
|
|
|
#[cfg(not(feature = "syntect"))]
|
|
|
|
fn syntax_highlighting(_ctx: &egui::Context, _: &ehttp::Response, _: &str) -> Option<ColoredText> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
New text layout (#682)
This PR introduces a completely rewritten text layout engine which is simpler and more powerful. It allows mixing different text styles (heading, body, etc) and formats (color, underlining, strikethrough, …) in the same layout pass, and baked into the same `Galley`.
This opens up the door to having a syntax-highlighed code editor, or a WYSIWYG markdown editor.
One major change is the color is now baked in at layout time. However, many widgets changes text color on hovered. But we need to do the text layout before we know if it is hovered. Therefor the painter has an option to override the text color of a galley.
## Performance
Text layout alone is about 20% slower, but a lot of that is because more tessellation is done upfront. Text tessellation is now a lot faster, but text layout + tessellation still lands at a net loss of 5-10% in performance. There are however a few tricks to speed it up (like using `smallvec`) which I am saving for later. Text layout is also cached, meaning that in most cases (when all text isn't changing each frame) text tessellation is actually more important (and that's more than 2x faster!).
Sadly, the actual text cache lookup is significantly slower (300ns -> 600ns). That's because the `TextLayoutJob` is a lot bigger (it has more options, like underlining, fonts etc), so it is slower to hash and compare. I have an idea how to speed this up, but I need to do some other work before I can implement that.
All in all, the performance impact on `demo_with_tesselate__realistic` is about 5-6% in the red. Not great; not terrible. The benefits are worth it, but I also think with some work I can get that down significantly, hopefully down to the old levels.
2021-09-03 16:18:00 +00:00
|
|
|
struct ColoredText(egui::text::LayoutJob);
|
2020-12-28 18:50:48 +00:00
|
|
|
|
|
|
|
impl ColoredText {
|
|
|
|
pub fn ui(&self, ui: &mut egui::Ui) {
|
2021-10-13 09:47:57 +00:00
|
|
|
if true {
|
|
|
|
// Selectable text:
|
|
|
|
let mut layouter = |ui: &egui::Ui, _string: &str, wrap_width: f32| {
|
|
|
|
let mut layout_job = self.0.clone();
|
2022-04-03 18:28:47 +00:00
|
|
|
layout_job.wrap.max_width = wrap_width;
|
2021-10-13 09:47:57 +00:00
|
|
|
ui.fonts().layout_job(layout_job)
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut text = self.0.text.as_str();
|
|
|
|
ui.add(
|
|
|
|
egui::TextEdit::multiline(&mut text)
|
2022-01-24 13:32:36 +00:00
|
|
|
.font(egui::TextStyle::Monospace)
|
2021-10-13 09:47:57 +00:00
|
|
|
.desired_width(f32::INFINITY)
|
|
|
|
.layouter(&mut layouter),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
let mut job = self.0.clone();
|
2022-04-03 18:28:47 +00:00
|
|
|
job.wrap.max_width = ui.available_width();
|
2021-10-13 09:47:57 +00:00
|
|
|
let galley = ui.fonts().layout_job(job);
|
|
|
|
let (response, painter) = ui.allocate_painter(galley.size(), egui::Sense::hover());
|
|
|
|
painter.add(egui::Shape::galley(response.rect.min, galley));
|
|
|
|
}
|
2020-11-18 20:38:29 +00:00
|
|
|
}
|
2021-01-31 15:52:26 +00:00
|
|
|
}
|