Skip to content

Commit 45ea836

Browse files
committed
Add copy to clipboard functionality
1 parent 396cac1 commit 45ea836

File tree

3 files changed

+49
-22
lines changed

3 files changed

+49
-22
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ strum_macros = "0.26.4"
2424
[dependencies.iced]
2525
git = "https://github.com/iced-rs/iced.git"
2626
version = "0.13.0-dev"
27-
features = ["multi-window"]
27+
features = ["advanced", "multi-window"]

src/ui/calc_window.rs

+47-21
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@
2525
// the main view / update logic of the main Application for ease of understanding
2626

2727
use iced::{Background, Border, Color, Degrees, Element, gradient, Length, Padding, Pixels, Radians, Renderer, Shadow, Task, Theme, Vector};
28+
use iced::clipboard;
2829
use iced::alignment::{Horizontal, Vertical};
2930
use iced::theme::palette::Pair;
30-
use iced::widget::{Button, button, Column, container, Container, horizontal_rule, Row, rule, text, text_editor};
31+
use iced::widget::{Button, button, Column, container, Container, horizontal_rule, Row, rule, text, Text, text_editor, tooltip};
3132
use iced::widget::button::Status;
3233
use iced::widget::text_editor::{Action, Content, Edit, Motion};
34+
use iced::widget::tooltip::Position;
3335
use iced::window::Id;
3436
use log::warn;
3537
use palette::{convert::FromColor, Hsl};
@@ -91,11 +93,8 @@ impl CalcWindow {
9193
}
9294
Task::none()
9395
}
94-
Message::Constant(s) => {
95-
for c in s.chars() {
96-
self.content.perform(Action::Edit(Edit::Insert(c)));
97-
}
98-
Task::none()
96+
Message::Copy(v) => {
97+
clipboard::write(v.to_string())
9998
}
10099
Message::Func(s) => {
101100
// If we have a selection, we want to surround it with the function
@@ -234,18 +233,18 @@ impl CalcWindow {
234233
.into();
235234

236235
let r64 = &self.result;
237-
let result: Element<Message> = text(match r64 {
236+
let result: Element<Message> = match r64 {
238237
Some(r) => {
239238
match r {
240239
Ok(v) => {
241-
Self::format_result(v)
240+
wrap_with_copy(text(Self::format_result(v)), v.clone())
242241
}
243-
Err(e) => e.clone()
242+
Err(e) => text(e.clone()).into()
244243
}
245244
}
246-
None => String::from(""),
247-
})
248-
.into();
245+
None => text("".to_string()).into(),
246+
};
247+
249248

250249
let mode: Element<Message> = Button::new(text(self.calc.angle_mode().to_string()))
251250
.style(|theme: &Theme, _status| {
@@ -293,24 +292,26 @@ impl CalcWindow {
293292
text("")
294293
}.horizontal_alignment(Horizontal::Left)
295294
.into();
296-
let converted_result = text(match &self.result {
295+
let converted_result = match &self.result {
297296
Some(r) => {
298297
match r {
299298
Ok(v) => {
300299
let cv = try_convert(v, &self.convert_from.as_ref(), &self.convert_to.as_ref());
301-
Self::format_result(&cv)
300+
wrap_with_copy(text(Self::format_result(&cv)), cv)
302301
}
303-
Err(e) => e.clone()
302+
Err(e) => text(e.clone()).into()
304303
}
305304
}
306-
None => String::from("")
307-
})
308-
.width(Length::FillPortion(1))
309-
.horizontal_alignment(Horizontal::Right)
310-
.into();
305+
None => text(String::from("")).into()
306+
};
307+
let con_conv_result = Container::new(converted_result)
308+
.width(Length::Fill)
309+
.align_x(Horizontal::Right)
310+
.clip(false)
311+
.into();
311312

312313
let r1 = Row::with_children([conv_from, con_result]).into();
313-
let r2 = Row::with_children([conv_to, converted_result]).into();
314+
let r2 = Row::with_children([conv_to, con_conv_result]).into();
314315

315316
let rule1:Element<Message> = horizontal_rule(1)
316317
.style(|theme| {
@@ -434,6 +435,31 @@ impl CalcWindow {
434435

435436
}
436437

438+
fn wrap_with_copy(text: Text, value: f64) -> Element<Message> {
439+
let b= Button::new(text)
440+
.style(|theme: &Theme, _status| {
441+
button::Style {
442+
background: Some(Background::Color(Color::TRANSPARENT)),
443+
text_color: theme.extended_palette().background.base.text,
444+
.. button::Style::default()
445+
}
446+
})
447+
.padding(Padding::from(0))
448+
.on_press(Message::Copy(value))
449+
.height(Length::Shrink);
450+
451+
tooltip(b, "Click to copy", Position::Left)
452+
.style(|theme| -> container::Style {
453+
container::Style{
454+
text_color: Some(theme.extended_palette().primary.weak.text),
455+
background: Some(Background::from(theme.extended_palette().primary.weak.color)),
456+
border: Default::default(),
457+
shadow: Default::default(),
458+
}
459+
})
460+
.into()
461+
}
462+
437463
/// A builder for making the button widgets.
438464
/// Note that the functions all take ownership of self and then return self; this allows
439465
/// us to avoid returning mutable references and so avoid ugly 'static life times.

src/ui/messages.rs

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ pub enum Message {
4343
MoveEnd,
4444
BackSpace,
4545
Clear,
46+
Copy(f64),
4647
Evaluate,
4748
ToggleMode,
4849
FuncPopup,

0 commit comments

Comments
 (0)