highlight copied button

This commit is contained in:
yggverse 2026-03-18 02:51:00 +02:00
parent 1ba5749623
commit c3d3bce7d0

View file

@ -11,7 +11,7 @@ use gtk::{
}, },
}; };
use regex::Regex; use regex::Regex;
use std::collections::HashMap; use std::{cell::Cell, collections::HashMap, rc::Rc};
use syntax::Syntax; use syntax::Syntax;
const REGEX_CODE: &str = r"(?s)```[ \t]*(?P<alt>.*?)\n(?P<data>.*?)```"; const REGEX_CODE: &str = r"(?s)```[ \t]*(?P<alt>.*?)\n(?P<data>.*?)```";
@ -81,6 +81,7 @@ impl Code {
pub fn render(&mut self, text_view: &TextView) { pub fn render(&mut self, text_view: &TextView) {
let buffer = text_view.buffer(); let buffer = text_view.buffer();
let syntax = Syntax::new(); let syntax = Syntax::new();
let copied = Rc::new(Cell::new(None));
assert!(buffer.tag_table().add(&self.alt)); assert!(buffer.tag_table().add(&self.alt));
@ -122,21 +123,31 @@ impl Code {
); );
} }
header.append(&{ header.append(&{
const TOGGLE_BUTTON_CLASS: &str = "dimmed";
const TOGGLE_BUTTON_TOOLTIP: (&str, &str) = ("Copy", "Copied");
let copy = Button::builder() let copy = Button::builder()
.css_classes(["circular", "flat"]) .css_classes(["circular", "flat", TOGGLE_BUTTON_CLASS])
.halign(Align::End) .halign(Align::End)
.icon_name("edit-copy-symbolic") .icon_name("edit-copy-symbolic")
.margin_bottom(MARGIN / 2) .margin_bottom(MARGIN / 2)
.margin_end(MARGIN / 2) .margin_end(MARGIN / 2)
.margin_start(MARGIN / 2) .margin_start(MARGIN / 2)
.margin_top(MARGIN / 2) .margin_top(MARGIN / 2)
.tooltip_text("Copy") .tooltip_text(TOGGLE_BUTTON_TOOLTIP.0)
.valign(Align::Center) .valign(Align::Center)
.build(); .build();
copy.set_cursor_from_name(Some("pointer")); copy.set_cursor_from_name(Some("pointer"));
copy.connect_clicked({ copy.connect_clicked({
let source = v.data.clone(); let source = v.data.clone();
move |_| { let copied = copied.clone();
move |this| {
if let Some(prev) = copied.replace(Some(this.clone())) {
prev.set_tooltip_text(Some(TOGGLE_BUTTON_TOOLTIP.0));
prev.add_css_class(TOGGLE_BUTTON_CLASS)
}
this.set_tooltip_text(Some(TOGGLE_BUTTON_TOOLTIP.1));
this.remove_css_class(TOGGLE_BUTTON_CLASS);
Display::default().unwrap().clipboard().set_text(&source) Display::default().unwrap().clipboard().set_text(&source)
} }
}); });