Skip to content
Snippets Groups Projects
Commit 5bc1421d authored by Jeremy Soller's avatar Jeremy Soller
Browse files

Significant improvements to editor

parent e2a38d83
No related branches found
No related tags found
No related merge requests found
#![deny(warnings)] //#![deny(warnings)]
extern crate orbclient; extern crate orbclient;
extern crate orbtk; extern crate orbtk;
...@@ -8,149 +8,164 @@ use orbtk::{Action, Button, Menu, Point, Rect, Separator, TextBox, Window}; ...@@ -8,149 +8,164 @@ use orbtk::{Action, Button, Menu, Point, Rect, Separator, TextBox, Window};
use orbtk::traits::{Click, Place, Resize, Text}; use orbtk::traits::{Click, Place, Resize, Text};
use std::{cmp, env}; use std::{cmp, env};
use std::cell::RefCell;
use std::fs::File; use std::fs::File;
use std::io::{Read, Write}; use std::io::{Read, Write};
use std::ops::DerefMut;
use std::sync::Arc;
fn main(){ pub struct Editor {
let path_option = env::args().nth(1); path_option: Option<String>,
text_box: Arc<TextBox>,
window: *mut Window,
}
let title = if let Some(ref path) = path_option { impl Editor {
format!("{} - Editor", path) pub fn new(path_option: Option<String>, width: u32, height: u32) -> Box<Window> {
} else { // DESIGN {
format!("Editor") let mut window = Box::new(Window::new_flags(Rect::new(-1, -1, width, height), "Editor", &[WindowFlag::Resizable]));
};
let (display_width, display_height) = orbclient::get_display_size().expect("viewer: failed to get display size"); let text_box = TextBox::new();
let (width, height) = (cmp::min(1024, display_width * 4/5), cmp::min(768, display_height * 4/5)); text_box.position(0, 16)
.size(width, height - 16);
window.add(&text_box);
let mut window = Window::new_flags(Rect::new(-1, -1, width, height), &title, &[WindowFlag::Resizable]); let menu = Menu::new("File");
menu.position(0, 0).size(32, 16);
let text_box = TextBox::new(); let open_action = Action::new("Open");
text_box.position(0, 16) menu.add(&open_action);
.size(width, height - 16);
window.add(&text_box);
if let Some(ref path) = path_option { let reload_action = Action::new("Reload");
match File::open(path) { menu.add(&reload_action);
Ok(mut file) => {
let mut text = String::new(); menu.add(&Separator::new());
match file.read_to_string(&mut text) {
Ok(_) => text_box.text.set(text), let save_action = Action::new("Save");
Err(err) => println!("Failed to read {}: {}", path, err) menu.add(&save_action);
}
}, let save_as_action = Action::new("Save As");
Err(err) => println!("Failed to open {}: {}", path, err) menu.add(&save_as_action);
menu.add(&Separator::new());
let close_action = Action::new("Close");
menu.add(&close_action);
window.add(&menu);
// } DESIGN
// CODE {
let editor_cell = Arc::new(RefCell::new(Editor {
path_option: path_option,
text_box: text_box.clone(),
window: window.deref_mut() as *mut Window,
}));
{
let mut editor = editor_cell.borrow_mut();
if editor.path_option.is_some() {
editor.load();
}
} }
}
let menu = Menu::new("File"); {
menu.position(0, 0).size(32, 16); let editor_cell = editor_cell.clone();
open_action.on_click(move |_action: &Action, _point: Point| {
println!("Open");
let open_action = Action::new("Open"); let mut window = {
let open_text_box = text_box.clone(); let editor_dialog = editor_cell.clone();
open_action.on_click(move |_action: &Action, _point: Point| { editor_cell.borrow_mut().path_dialog("Open", move |path| {
println!("Open"); println!("Open {}", path);
let mut window = Window::new(Rect::new(-1, -1, 320, 8 + 28 + 8 + 28 + 8), "Open"); editor_dialog.borrow_mut().open(&path);
})
};
let path_box = TextBox::new(); window.exec();
path_box.position(8, 8) });
.size(320 - 16, 28) }
.text_offset(6, 6)
.grab_focus(true);
window.add(&path_box);
{ {
let window_cancel = &mut window as *mut Window; let editor_cell = editor_cell.clone();
let button = Button::new(); reload_action.on_click(move |_action: &Action, _point: Point| {
button.position(8, 8 + 28 + 8) println!("Reload");
.size((320 - 16)/2 - 4, 28) editor_cell.borrow_mut().load();
.text_offset(6, 6) });
.text("Cancel")
.on_click(move |_button: &Button, _point: Point| {
unsafe { (&mut *window_cancel).close(); }
});
window.add(&button);
} }
{ {
let open_text_box = open_text_box.clone(); let editor_cell = editor_cell.clone();
let window_save_as = &mut window as *mut Window; save_action.on_click(move |_action: &Action, _point: Point| {
let button = Button::new(); println!("Save");
button.position(320/2 + 4, 8 + 28 + 8) editor_cell.borrow_mut().save();
.size((320 - 16)/2 - 4, 28) });
.text_offset(6, 6)
.text("Open")
.on_click(move |_button: &Button, _point: Point| {
match File::open(path_box.text.get()) {
Ok(mut f) => {
let mut contents = String::new();
f.read_to_string(&mut contents).expect("Cannot read file");
open_text_box.text.set(contents);
},
Err(e) => {
println!("File does not exist: {}", e);
}
}
unsafe { (&mut *window_save_as).close(); }
});
window.add(&button);
} }
window.exec(); {
let editor_cell = editor_cell.clone();
save_as_action.on_click(move |_action: &Action, _point: Point| {
println!("Save As");
}); let mut window = {
menu.add(&open_action); let editor_dialog = editor_cell.clone();
editor_cell.borrow_mut().path_dialog("Save As", move |path| {
println!("Save As {}", path);
editor_dialog.borrow_mut().save_as(&path);
})
};
menu.add(&Separator::new()); window.exec();
});
}
let save_action = Action::new("Save"); {
let save_path_option = path_option.clone(); let editor_cell = editor_cell.clone();
let save_text_box = text_box.clone(); close_action.on_click(move |_action: &Action, _point: Point| {
save_action.on_click(move |_action: &Action, _point: Point| { println!("Close");
println!("Save"); editor_cell.borrow_mut().close();
if let Some(ref path) = save_path_option { });
println!("Create {}", path);
match File::create(path) {
Ok(mut file) => {
let text = save_text_box.text.borrow();
match file.write(&text.as_bytes()) {
Ok(_) => match file.set_len(text.len() as u64) {
Ok(_) => println!("Successfully saved {}", path),
Err(err) => println!("Failed to truncate {}: {}", path, err)
},
Err(err) => println!("Failed to write {}: {}", path, err)
}
},
Err(err) => println!("Failed to open {}: {}", path, err)
}
} else {
println!("Need to create file!");
} }
});
menu.add(&save_action);
let save_as_action = Action::new("Save As"); {
let save_as_path_option = path_option.clone(); let editor_cell = editor_cell.clone();
save_as_action.on_click(move |_action: &Action, _point: Point| { window.on_resize(move |_, width, height| {
println!("Save As"); editor_cell.borrow().text_box.size(width, height - 16);
let mut window = Window::new(Rect::new(-1, -1, 320, 8 + 28 + 8 + 28 + 8), "Save As"); });
}
// } CODE
window
}
fn path_dialog<F: Fn(&str) + 'static>(&mut self, title: &str, func: F) -> Box<Window> {
let (p_x, p_y, p_w, p_h) = {
let window = unsafe { &mut *self.window };
(window.x(), window.y(), window.width(), window.height())
};
let w = 320;
let h = 8 + 28 + 8 + 28 + 8;
let x = p_x + (p_w as i32 - w as i32)/2;
let y = p_y + (p_h as i32 - h as i32)/2;
let mut window = Box::new(Window::new(Rect::new(x, y, w, h), title));
let text_box = TextBox::new(); let text_box = TextBox::new();
text_box.position(8, 8) text_box.position(8, 8)
.size(320 - 16, 28) .size(w - 16, 28)
.text_offset(6, 6) .text_offset(6, 6)
.grab_focus(true); .grab_focus(true);
window.add(&text_box); window.add(&text_box);
if let Some(ref path) = save_as_path_option { if let Some(ref path) = self.path_option {
text_box.text.set(path.clone()); text_box.text.set(path.clone());
} }
{ {
let window_cancel = &mut window as *mut Window; let window_cancel = window.deref_mut() as *mut Window;
let button = Button::new(); let button = Button::new();
button.position(8, 8 + 28 + 8) button.position(8, 8 + 28 + 8)
.size((320 - 16)/2 - 4, 28) .size((w - 16)/2 - 4, 28)
.text_offset(6, 6) .text_offset(6, 6)
.text("Cancel") .text("Cancel")
.on_click(move |_button: &Button, _point: Point| { .on_click(move |_button: &Button, _point: Point| {
...@@ -160,38 +175,95 @@ fn main(){ ...@@ -160,38 +175,95 @@ fn main(){
} }
{ {
let window_save_as = &mut window as *mut Window; let window_save_as = window.deref_mut() as *mut Window;
let button = Button::new(); let button = Button::new();
button.position(320/2 + 4, 8 + 28 + 8) button.position((w as i32)/2 + 4, 8 + 28 + 8)
.size((320 - 16)/2 - 4, 28) .size((w - 16)/2 - 4, 28)
.text_offset(6, 6) .text_offset(6, 6)
.text("Save As") .text(title)
.on_click(move |_button: &Button, _point: Point| { .on_click(move |_button: &Button, _point: Point| {
println!("Save {}", text_box.text.get()); let path = text_box.text.get();
func(&path);
unsafe { (&mut *window_save_as).close(); } unsafe { (&mut *window_save_as).close(); }
}); });
window.add(&button); window.add(&button);
} }
window.exec(); window
}); }
menu.add(&save_as_action);
menu.add(&Separator::new()); fn load(&mut self) {
if let Some(ref path) = self.path_option {
println!("Load {}", path);
match File::open(path) {
Ok(mut f) => {
let mut contents = String::new();
match f.read_to_string(&mut contents) {
Ok(_) => {
self.text_box.text.set(contents);
},
Err(e) => {
println!("Failed to read {}: {}", path, e);
}
}
},
Err(e) => {
println!("Failed to open {}: {}", path, e);
}
}
} else {
println!("Path not set");
}
}
let close_action = Action::new("Close"); fn save(&mut self) {
let window_close = &mut window as *mut Window; if let Some(ref path) = self.path_option {
close_action.on_click(move |_action: &Action, _point: Point| { println!("Save {}", path);
println!("Close"); match File::create(path) {
unsafe { (&mut *window_close).close(); } Ok(mut file) => {
}); let text = self.text_box.text.borrow();
menu.add(&close_action); match file.write(&text.as_bytes()) {
Ok(_) => match file.set_len(text.len() as u64) {
Ok(_) => println!("Successfully saved {}", path),
Err(err) => println!("Failed to truncate {}: {}", path, err)
},
Err(err) => println!("Failed to write {}: {}", path, err)
}
},
Err(err) => println!("Failed to open {}: {}", path, err)
}
} else {
println!("Path not set");
}
}
window.add(&menu); fn set_path(&mut self, path: &str) {
self.path_option = Some(path.to_string());
let window = unsafe { &mut *self.window };
window.set_title(&format!("{} - Editor", path));
}
window.on_resize(move |_, width, height| { fn open(&mut self, path: &str) {
text_box.size(width, height - 16); self.set_path(path);
}); self.load();
}
fn save_as(&mut self, path: &str) {
self.set_path(path);
self.save();
}
fn close(&mut self) {
let window = unsafe { &mut *self.window };
window.close();
}
}
fn main(){
let path_option = env::args().nth(1);
let (display_width, display_height) = orbclient::get_display_size().expect("viewer: failed to get display size");
let (width, height) = (cmp::min(1024, display_width * 4/5), cmp::min(768, display_height * 4/5));
window.exec(); Editor::new(path_option, width, height).exec();
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment