|
25 | 25 | // the main view / update logic of the main Application for ease of understanding
|
26 | 26 |
|
27 | 27 | use iced::{Background, Border, Color, Degrees, Element, gradient, Length, Padding, Pixels, Radians, Renderer, Shadow, Task, Theme, Vector};
|
| 28 | +use iced::clipboard; |
28 | 29 | use iced::alignment::{Horizontal, Vertical};
|
29 | 30 | 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}; |
31 | 32 | use iced::widget::button::Status;
|
32 | 33 | use iced::widget::text_editor::{Action, Content, Edit, Motion};
|
| 34 | +use iced::widget::tooltip::Position; |
33 | 35 | use iced::window::Id;
|
34 | 36 | use log::warn;
|
35 | 37 | use palette::{convert::FromColor, Hsl};
|
@@ -91,11 +93,8 @@ impl CalcWindow {
|
91 | 93 | }
|
92 | 94 | Task::none()
|
93 | 95 | }
|
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()) |
99 | 98 | }
|
100 | 99 | Message::Func(s) => {
|
101 | 100 | // If we have a selection, we want to surround it with the function
|
@@ -234,18 +233,18 @@ impl CalcWindow {
|
234 | 233 | .into();
|
235 | 234 |
|
236 | 235 | let r64 = &self.result;
|
237 |
| - let result: Element<Message> = text(match r64 { |
| 236 | + let result: Element<Message> = match r64 { |
238 | 237 | Some(r) => {
|
239 | 238 | match r {
|
240 | 239 | Ok(v) => {
|
241 |
| - Self::format_result(v) |
| 240 | + wrap_with_copy(text(Self::format_result(v)), v.clone()) |
242 | 241 | }
|
243 |
| - Err(e) => e.clone() |
| 242 | + Err(e) => text(e.clone()).into() |
244 | 243 | }
|
245 | 244 | }
|
246 |
| - None => String::from(""), |
247 |
| - }) |
248 |
| - .into(); |
| 245 | + None => text("".to_string()).into(), |
| 246 | + }; |
| 247 | + |
249 | 248 |
|
250 | 249 | let mode: Element<Message> = Button::new(text(self.calc.angle_mode().to_string()))
|
251 | 250 | .style(|theme: &Theme, _status| {
|
@@ -293,24 +292,26 @@ impl CalcWindow {
|
293 | 292 | text("")
|
294 | 293 | }.horizontal_alignment(Horizontal::Left)
|
295 | 294 | .into();
|
296 |
| - let converted_result = text(match &self.result { |
| 295 | + let converted_result = match &self.result { |
297 | 296 | Some(r) => {
|
298 | 297 | match r {
|
299 | 298 | Ok(v) => {
|
300 | 299 | 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) |
302 | 301 | }
|
303 |
| - Err(e) => e.clone() |
| 302 | + Err(e) => text(e.clone()).into() |
304 | 303 | }
|
305 | 304 | }
|
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(); |
311 | 312 |
|
312 | 313 | 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(); |
314 | 315 |
|
315 | 316 | let rule1:Element<Message> = horizontal_rule(1)
|
316 | 317 | .style(|theme| {
|
@@ -434,6 +435,31 @@ impl CalcWindow {
|
434 | 435 |
|
435 | 436 | }
|
436 | 437 |
|
| 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 | + |
437 | 463 | /// A builder for making the button widgets.
|
438 | 464 | /// Note that the functions all take ownership of self and then return self; this allows
|
439 | 465 | /// us to avoid returning mutable references and so avoid ugly 'static life times.
|
|
0 commit comments