basic text editor

This commit is contained in:
Tumble 2023-12-16 15:42:30 +00:00
commit 7d9854a175
No known key found for this signature in database
6 changed files with 4872 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

4763
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

10
Cargo.toml Normal file
View File

@ -0,0 +1,10 @@
[package]
name = "file-maker"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
native-dialog = "0.7.0"
slint = "1.3.2"

1
face.txt Normal file
View File

@ -0,0 +1 @@
noses

1
hi.txt Normal file
View File

@ -0,0 +1 @@
noses of dooom

96
src/main.rs Normal file
View File

@ -0,0 +1,96 @@
use native_dialog::FileDialog;
use slint::SharedString;
use std::fs;
slint::slint! {
import { TextEdit , Button} from "std-widgets.slint";
export component MainWindow inherits Window {
width: 500px;
height: 500px;
// in-out property <string> content;
title: "hello";
// callback new <=> new.clicked;
// callback open <=> open.clicked;
// callback save <=> save.clicked;
callback action(string);
public function set_content(text:string) {
textarea.text = text;
}
public function get_content() -> string {
return textarea.text;
}
VerticalLayout {
HorizontalLayout {
Button {
text: "New";
clicked => {textarea.text = ""}
}
Button {
text: "Open";
clicked => {action("open")}
}
Button {
text: "Save";
clicked => {action("save")}
}
}
textarea:=TextEdit {
edited(text) => {self.text = text;}
}
}
}
}
fn main() {
let app = MainWindow::new().unwrap();
let weak = app.as_weak();
app.on_action(move|action| {
let app = weak.upgrade().unwrap();
match action.as_str() {
"open" =>{
let path = FileDialog::new()
.set_location(".")
.show_open_single_file()
.unwrap();
let path = match path {
Some(path) => path,
None => return,
};
let contents = fs::read_to_string(path)
.expect("unable to read file");
app.invoke_set_content(SharedString::from(contents));
},
"save"=>{
let path = FileDialog::new()
.set_location(".")
.show_save_single_file()
.unwrap();
let path = match path {
Some(path) => path,
None => return,
};
let file_contents = app.invoke_get_content();
fs::write(path.to_str().unwrap(), file_contents.as_bytes())
.expect("unable to write file");
},
&_=>{}
};
});
app.run().unwrap();
}