Skip to content

Commit

Permalink
Move clipboard functionality to util crate
Browse files Browse the repository at this point in the history
  • Loading branch information
Barugon committed Apr 16, 2023
1 parent 5ec6021 commit f2647d4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
19 changes: 4 additions & 15 deletions src/experience.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::{
skill_info::{self, AvatarPlan, SkillCategory, SkillInfo, SkillInfoGroup},
util::{self, AppState, Cancel, FAIL_ERR, LEVEL_EXP, LVL_RANGE, NONE_ERR, SKILL_EXP},
};
use clipboard::{ClipboardContext, ClipboardProvider};
use eframe::{
egui::{ComboBox, Context, DragValue, Label, Layout, RichText, ScrollArea, Sense, Ui, Widget},
emath::{Align, Vec2},
Expand Down Expand Up @@ -119,13 +118,9 @@ impl Experience {
String::new()
};

let text = RichText::from(text).color(Color32::WHITE);
let response = Label::new(text).sense(Sense::click()).ui(ui);
if response.on_hover_text_at_pointer("Click to copy").clicked() {
// Copy the value to the clipboard.
if let Ok::<ClipboardContext, _>(mut ctx) = ClipboardProvider::new() {
err!(ctx.set_contents(format!("{exp}")));
}
util::set_clipboard_contents(format!("{exp}"));
}
}
},
Expand Down Expand Up @@ -263,22 +258,16 @@ impl Experience {
let (text, exp) = if exp < 0 {
// Half experience returned for un-training.
let exp = exp / 2;
let text =
format!("({})", exp.abs().to_formatted_string(&self.locale));
let text = RichText::from(text).color(Color32::LIGHT_RED);
let text = exp.abs().to_formatted_string(&self.locale);
let text = format!("({})", text);
(text, exp)
} else {
let text = exp.to_formatted_string(&self.locale);
let text = RichText::from(text).color(Color32::WHITE);
(text, exp)
};
let response = Label::new(text).sense(Sense::click()).ui(ui);
if response.on_hover_text_at_pointer("Click to copy").clicked() {
// Copy the value to the clipboard.
if let Ok::<ClipboardContext, _>(mut ctx) = ClipboardProvider::new()
{
err!(ctx.set_contents(format!("{exp}")));
}
util::set_clipboard_contents(format!("{exp}"));
}
}
});
Expand Down
6 changes: 6 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use chrono::{DateTime, NaiveDateTime, TimeZone, Utc};
use clipboard::{ClipboardContext, ClipboardProvider};
use eframe::egui::{TextStyle, Ui};
use num_format::Locale;
use regex::Regex;
Expand Down Expand Up @@ -84,6 +85,11 @@ macro_rules! f64_to_string {
};
}

pub fn set_clipboard_contents(text: String) {
let mut ctx: ClipboardContext = ok!(ClipboardProvider::new());
err!(ctx.set_contents(text));
}

/// SotA epoch (date/time of lunar cataclysm).
pub fn get_epoch() -> DateTime<Utc> {
Utc.with_ymd_and_hms(1997, 9, 2, 0, 0, 0).unwrap() // LocalResult does not have expect.
Expand Down

0 comments on commit f2647d4

Please sign in to comment.