Skip to content

Commit c3d3bce

Browse files
author
yggverse
committed
highlight copied button
1 parent 1ba5749 commit c3d3bce

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

  • src/app/browser/window/tab/item/page/content/text/markdown/tags

src/app/browser/window/tab/item/page/content/text/markdown/tags/code.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use gtk::{
1111
},
1212
};
1313
use regex::Regex;
14-
use std::collections::HashMap;
14+
use std::{cell::Cell, collections::HashMap, rc::Rc};
1515
use syntax::Syntax;
1616

1717
const REGEX_CODE: &str = r"(?s)```[ \t]*(?P<alt>.*?)\n(?P<data>.*?)```";
@@ -81,6 +81,7 @@ impl Code {
8181
pub fn render(&mut self, text_view: &TextView) {
8282
let buffer = text_view.buffer();
8383
let syntax = Syntax::new();
84+
let copied = Rc::new(Cell::new(None));
8485

8586
assert!(buffer.tag_table().add(&self.alt));
8687

@@ -122,21 +123,31 @@ impl Code {
122123
);
123124
}
124125
header.append(&{
126+
const TOGGLE_BUTTON_CLASS: &str = "dimmed";
127+
const TOGGLE_BUTTON_TOOLTIP: (&str, &str) = ("Copy", "Copied");
125128
let copy = Button::builder()
126-
.css_classes(["circular", "flat"])
129+
.css_classes(["circular", "flat", TOGGLE_BUTTON_CLASS])
127130
.halign(Align::End)
128131
.icon_name("edit-copy-symbolic")
129132
.margin_bottom(MARGIN / 2)
130133
.margin_end(MARGIN / 2)
131134
.margin_start(MARGIN / 2)
132135
.margin_top(MARGIN / 2)
133-
.tooltip_text("Copy")
136+
.tooltip_text(TOGGLE_BUTTON_TOOLTIP.0)
134137
.valign(Align::Center)
135138
.build();
136139
copy.set_cursor_from_name(Some("pointer"));
137140
copy.connect_clicked({
138141
let source = v.data.clone();
139-
move |_| {
142+
let copied = copied.clone();
143+
move |this| {
144+
if let Some(prev) = copied.replace(Some(this.clone())) {
145+
prev.set_tooltip_text(Some(TOGGLE_BUTTON_TOOLTIP.0));
146+
prev.add_css_class(TOGGLE_BUTTON_CLASS)
147+
}
148+
this.set_tooltip_text(Some(TOGGLE_BUTTON_TOOLTIP.1));
149+
this.remove_css_class(TOGGLE_BUTTON_CLASS);
150+
140151
Display::default().unwrap().clipboard().set_text(&source)
141152
}
142153
});

0 commit comments

Comments
 (0)