summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2025-02-04 20:58:06 +0100
committerLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2025-02-04 20:58:06 +0100
commite8020f3eaf3baec2b41847f6250d8554136e8d89 (patch)
treebc077692333b71f48a7efe984db60f87f9555b4c /examples
parent387abafa3abda3ba68eb7a2e4ce4240ad3bdda53 (diff)
downloadiced-e8020f3eaf3baec2b41847f6250d8554136e8d89.tar.gz
iced-e8020f3eaf3baec2b41847f6250d8554136e8d89.tar.bz2
iced-e8020f3eaf3baec2b41847f6250d8554136e8d89.zip
Add `Copy` action to code blocks in `markdown` example
Diffstat (limited to 'examples')
-rw-r--r--examples/markdown/Cargo.toml5
-rw-r--r--examples/markdown/build.rs5
-rw-r--r--examples/markdown/fonts/markdown-icons.toml4
-rw-r--r--examples/markdown/fonts/markdown-icons.ttfbin0 -> 5856 bytes
-rw-r--r--examples/markdown/src/icon.rs15
-rw-r--r--examples/markdown/src/main.rs32
6 files changed, 59 insertions, 2 deletions
diff --git a/examples/markdown/Cargo.toml b/examples/markdown/Cargo.toml
index 4711b1c4..7af1741b 100644
--- a/examples/markdown/Cargo.toml
+++ b/examples/markdown/Cargo.toml
@@ -16,3 +16,8 @@ image.workspace = true
tokio.workspace = true
open = "5.3"
+
+# Disabled to keep amount of build dependencies low
+# This can be re-enabled on demand
+# [build-dependencies]
+# iced_fontello = "0.13"
diff --git a/examples/markdown/build.rs b/examples/markdown/build.rs
new file mode 100644
index 00000000..ecbb7666
--- /dev/null
+++ b/examples/markdown/build.rs
@@ -0,0 +1,5 @@
+pub fn main() {
+ // println!("cargo::rerun-if-changed=fonts/markdown-icons.toml");
+ // iced_fontello::build("fonts/markdown-icons.toml")
+ // .expect("Build icons font");
+}
diff --git a/examples/markdown/fonts/markdown-icons.toml b/examples/markdown/fonts/markdown-icons.toml
new file mode 100644
index 00000000..60c91d17
--- /dev/null
+++ b/examples/markdown/fonts/markdown-icons.toml
@@ -0,0 +1,4 @@
+module = "icon"
+
+[glyphs]
+copy = "fontawesome-docs"
diff --git a/examples/markdown/fonts/markdown-icons.ttf b/examples/markdown/fonts/markdown-icons.ttf
new file mode 100644
index 00000000..013f03a5
--- /dev/null
+++ b/examples/markdown/fonts/markdown-icons.ttf
Binary files differ
diff --git a/examples/markdown/src/icon.rs b/examples/markdown/src/icon.rs
new file mode 100644
index 00000000..cfe32541
--- /dev/null
+++ b/examples/markdown/src/icon.rs
@@ -0,0 +1,15 @@
+// Generated automatically by iced_fontello at build time.
+// Do not edit manually. Source: ../fonts/markdown-icons.toml
+// dcd2f0c969d603e2ee9237a4b70fa86b1a6e84d86f4689046d8fdd10440b06b9
+use iced::widget::{text, Text};
+use iced::Font;
+
+pub const FONT: &[u8] = include_bytes!("../fonts/markdown-icons.ttf");
+
+pub fn copy<'a>() -> Text<'a> {
+ icon("\u{F0C5}")
+}
+
+fn icon(codepoint: &str) -> Text<'_> {
+ text(codepoint).font(Font::with_name("markdown-icons"))
+}
diff --git a/examples/markdown/src/main.rs b/examples/markdown/src/main.rs
index 84c20b7e..6a881288 100644
--- a/examples/markdown/src/main.rs
+++ b/examples/markdown/src/main.rs
@@ -1,10 +1,13 @@
+mod icon;
+
use iced::animation;
+use iced::clipboard;
use iced::highlighter;
use iced::task;
use iced::time::{self, milliseconds, Instant};
use iced::widget::{
- self, center_x, horizontal_space, hover, image, markdown, pop, right, row,
- scrollable, text_editor, toggler,
+ self, button, center_x, horizontal_space, hover, image, markdown, pop,
+ right, row, scrollable, text_editor, toggler,
};
use iced::window;
use iced::{Animation, Element, Fill, Font, Subscription, Task, Theme};
@@ -15,6 +18,7 @@ use std::sync::Arc;
pub fn main() -> iced::Result {
iced::application("Markdown - Iced", Markdown::update, Markdown::view)
+ .font(icon::FONT)
.subscription(Markdown::subscription)
.theme(Markdown::theme)
.run_with(Markdown::new)
@@ -49,6 +53,7 @@ enum Image {
#[derive(Debug, Clone)]
enum Message {
Edit(text_editor::Action),
+ Copy(String),
LinkClicked(markdown::Url),
ImageShown(markdown::Url),
ImageDownloaded(markdown::Url, Result<image::Handle, Error>),
@@ -91,6 +96,7 @@ impl Markdown {
Task::none()
}
+ Message::Copy(content) => clipboard::write(content),
Message::LinkClicked(link) => {
let _ = open::that_in_background(link.to_string());
@@ -141,6 +147,8 @@ impl Markdown {
}
Message::ToggleStream(enable_stream) => {
if enable_stream {
+ self.content = markdown::Content::new();
+
self.mode = Mode::Stream {
pending: self.raw.text(),
};
@@ -282,6 +290,26 @@ impl<'a> markdown::Viewer<'a, Message> for CustomViewer<'a> {
.into()
}
}
+
+ fn code_block(
+ &self,
+ settings: markdown::Settings,
+ code: &'a str,
+ lines: &'a [markdown::Text],
+ ) -> Element<'a, Message> {
+ let code_block =
+ markdown::code_block(settings, code, lines, Message::LinkClicked);
+
+ hover(
+ code_block,
+ right(
+ button(icon::copy().size(12))
+ .padding(settings.spacing / 2)
+ .on_press_with(|| Message::Copy(code.to_owned()))
+ .style(button::text),
+ ),
+ )
+ }
}
async fn download_image(url: markdown::Url) -> Result<image::Handle, Error> {