Skip to content

Commit

Permalink
add settings to FlDialect
Browse files Browse the repository at this point in the history
  • Loading branch information
Artem V. Ageev committed Jul 20, 2024
1 parent 211fc99 commit a55f8b9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
20 changes: 10 additions & 10 deletions demos/dialect/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use {
OnEvent, OnMenuEvent, Sandbox, Settings,
},
model::Model,
std::{env, fs, path::Path, process::Command, thread},
std::{env, process::Command, thread},
};

const SPINNER: Event = Event::from_i32(405);
Expand All @@ -30,7 +30,7 @@ const WIDTH: i32 = 105;

fn main() {
if crate::once() {
app::GlobalState::<String>::new(env::var("HOME").unwrap() + "/.config" + NAME);
app::GlobalState::<String>::new(env::var("HOME").unwrap() + "/.config/" + NAME);
Model::new().run(Settings {
ignore_esc_close: true,
resizable: true,
Expand All @@ -45,14 +45,14 @@ fn main() {

#[derive(Clone)]
pub enum Message {
Switch,
From(i32),
To(i32),
Source(String),
Size(i32),
Font(i32),
Page(i32),
Translate,
Source(String),
Switch,
Click,
Open,
Save,
Quit,
Expand Down Expand Up @@ -104,7 +104,7 @@ impl Sandbox for Model {
);
Frame::default();
crate::button("Translate", "@#circle", &mut header)
.on_event(move |_| Message::Translate);
.on_event(move |_| Message::Click);
}
header.end();
header.set_pad(PAD);
Expand Down Expand Up @@ -190,7 +190,7 @@ impl Sandbox for Model {
}
if dialog.count() > 0 {
if let Some(file) = dialog.value(1) {
self.source = fs::read_to_string(Path::new(&file)).unwrap();
self.open(&file);
};
};
}
Expand All @@ -208,14 +208,14 @@ impl Sandbox for Model {
}
if dialog.count() > 0 {
if let Some(file) = dialog.value(1) {
fs::write(file, self.target.as_bytes()).unwrap();
self.target(&file)
};
};
} else {
alert_default("Target is empty.");
};
}
Message::Translate => {
Message::Click => {
let clone = self.clone();
if clone.from != clone.to && !clone.source.is_empty() {
let handler = thread::spawn(move || -> String { clone.click() });
Expand Down Expand Up @@ -361,7 +361,7 @@ fn menu(flex: &mut Flex) {
"@#circle T&ranslate",
Shortcut::Ctrl | 'r',
MenuFlag::Normal,
move |_| Message::Translate,
move |_| Message::Click,
)
.on_item_event(
"@#search &Info",
Expand Down
10 changes: 8 additions & 2 deletions demos/dialect/src/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ impl Model {
to: 0,
font: 1,
size: 14,
source: String::new(),
target: String::new(),
source: String::from("Source"),
target: String::from("Target"),
lang: Lang::init(),
};
if let Ok(value) = fs::read(file) {
Expand All @@ -78,4 +78,10 @@ impl Model {
pub fn save(&mut self, file: &str) {
fs::write(file, rmp_serde::to_vec(&self).unwrap()).unwrap();
}
pub fn open(&mut self, file: &str) {
self.source = fs::read_to_string(file).unwrap();
}
pub fn target(&mut self, file: &str) {
fs::write(file, self.target.as_bytes()).unwrap();
}
}

0 comments on commit a55f8b9

Please sign in to comment.