From 82d55cb67e9aa86eeb9d2add10b7ea8479fe1772 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sat, 31 Oct 2020 09:17:15 +0100 Subject: [PATCH] [demo] Add helper macros for linking to the source code on GitHub --- egui/src/demos/mod.rs | 46 +++++++++++++++++++++++++++++++++ egui/src/demos/toggle_switch.rs | 3 +-- egui/src/demos/widgets.rs | 3 +-- 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/egui/src/demos/mod.rs b/egui/src/demos/mod.rs index 02c3ec18..163e2635 100644 --- a/egui/src/demos/mod.rs +++ b/egui/src/demos/mod.rs @@ -43,3 +43,49 @@ pub fn warn_if_debug_build(ui: &mut crate::Ui) { .on_hover_text("Egui has detect that debug assertions are enabled."); } } + +// ---------------------------------------------------------------------------- + +/// Create a `Hyperlink` to this file (and line) on Github +/// Example: `ui.add(github_link_file_line!("https://github.com/YOUR/PROJECT/blob/master/", "(source code)"));` +#[macro_export] +macro_rules! github_link_file_line { + ($github_url:expr, $label:expr) => {{ + let url = format!("{}{}#L{}", $github_url, file!(), line!()); + Hyperlink::new(url).text($label) + }}; +} + +/// Create a `Hyperlink` to this file on github. +/// Example: `ui.add(github_link_file!("https://github.com/YOUR/PROJECT/blob/master/", "(source code)"));` +#[macro_export] +macro_rules! github_link_file { + ($github_url:expr, $label:expr) => {{ + let url = format!("{}{}", $github_url, file!()); + Hyperlink::new(url).text($label) + }}; +} + +/// Create a `Hyperlink` to this egui source code file on github. +#[doc(hidden)] +#[macro_export] +macro_rules! __egui_github_link_file { + () => { + __egui_github_link_file!("(source code)") + }; + ($label:expr) => { + github_link_file!("https://github.com/emilk/egui/blob/master/", $label).small() + }; +} + +/// Create a `Hyperlink` to this egui source code file and line on github. +#[doc(hidden)] +#[macro_export] +macro_rules! __egui_github_link_file_line { + () => { + __egui_github_link_file_line!("(source code)") + }; + ($label:expr) => { + github_link_file_line!("https://github.com/emilk/egui/blob/master/", $label).small() + }; +} diff --git a/egui/src/demos/toggle_switch.rs b/egui/src/demos/toggle_switch.rs index 40473df9..b55766d1 100644 --- a/egui/src/demos/toggle_switch.rs +++ b/egui/src/demos/toggle_switch.rs @@ -90,10 +90,9 @@ fn toggle_compact(ui: &mut Ui, on: &mut bool) -> Response { pub fn demo(ui: &mut Ui, on: &mut bool) { ui.label("It's easy to create your own widgets!"); - let url = format!("https://github.com/emilk/egui/blob/master/{}", file!()); ui.horizontal(|ui| { ui.label("Like this toggle switch:"); toggle(ui, on).on_hover_text("Click to toggle"); - ui.add(Hyperlink::new(url).text("(source code)")); + ui.add(__egui_github_link_file!()); }); } diff --git a/egui/src/demos/widgets.rs b/egui/src/demos/widgets.rs index 617d9df6..4fd512f1 100644 --- a/egui/src/demos/widgets.rs +++ b/egui/src/demos/widgets.rs @@ -46,8 +46,7 @@ impl Default for Widgets { impl Widgets { pub fn ui(&mut self, ui: &mut Ui) { - let url = format!("https://github.com/emilk/egui/blob/master/{}", file!()); - ui.add(Hyperlink::new(url).text("Click here to read the source code for this section")); + ui.add(crate::__egui_github_link_file_line!()); ui.horizontal(|ui| { ui.style_mut().spacing.item_spacing.x = 0.0;