basic text editor
This commit is contained in:
commit
7d9854a175
6 changed files with 4872 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
/target
|
4763
Cargo.lock
generated
Normal file
4763
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
10
Cargo.toml
Normal file
10
Cargo.toml
Normal 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
1
face.txt
Normal file
|
@ -0,0 +1 @@
|
|||
noses
|
1
hi.txt
Normal file
1
hi.txt
Normal file
|
@ -0,0 +1 @@
|
|||
noses of dooom
|
96
src/main.rs
Normal file
96
src/main.rs
Normal 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();
|
||||
}
|
Loading…
Reference in a new issue