From 1594dda0c93f4fe3261ae3e33987ecf1a9df3d69 Mon Sep 17 00:00:00 2001
From: Jeremy Soller <jackpot51@gmail.com>
Date: Thu, 11 May 2017 20:13:50 -0600
Subject: [PATCH] Process backspace/enter on key up. Fix look of open dialogs

---
 src/browser/main.rs | 90 ++++++++++++++++++++++-----------------------
 src/editor/main.rs  | 36 +++++++++++-------
 2 files changed, 67 insertions(+), 59 deletions(-)

diff --git a/src/browser/main.rs b/src/browser/main.rs
index 3ec658d..cbd62df 100644
--- a/src/browser/main.rs
+++ b/src/browser/main.rs
@@ -562,14 +562,15 @@ fn open_dialog(url: &Url) -> Option<Url> {
 
     {
         let w = 400;
-        let mut window = Window::new(Rect::new(-1, -1, w, 32), "Open");
+        let mut window = Window::new(Rect::new(-1, -1, w, 8 + 28 + 8 + 28 + 8), "Open");
 
         let path_box = TextBox::new();
         {
             let ret_path = ret.clone();
             let window_path = &mut window as *mut Window;
-            path_box.position(0, 0)
-                .size(w, 16)
+            path_box.position(8, 8)
+                .size(w - 16, 28)
+                .text_offset(6, 6)
                 .text(format!("{}", url))
                 .on_enter(move |me: &TextBox| {
                     if let Ok(new_url) = Url::parse(&me.text.get()) {
@@ -583,8 +584,9 @@ fn open_dialog(url: &Url) -> Option<Url> {
         {
             let window_cancel = &mut window as *mut Window;
             let button = Button::new();
-            button.position(0, 16)
-                .size(w/2, 16)
+            button.position(8, 8 + 28 + 8)
+                .size((w - 16)/2 - 4, 28)
+                .text_offset(6, 6)
                 .text("Cancel")
                 .on_click(move |_button: &Button, _point: Point| {
                     unsafe { (&mut *window_cancel).close(); }
@@ -596,8 +598,9 @@ fn open_dialog(url: &Url) -> Option<Url> {
             let ret_open = ret.clone();
             let window_open = &mut window as *mut Window;
             let button = Button::new();
-            button.position((w as i32)/2, 16)
-                .size(w/2, 16)
+            button.position((w as i32)/2 + 4, 8 + 28 + 8)
+                .size((w - 16)/2 - 4, 28)
+                .text_offset(6, 6)
                 .text("Open")
                 .on_click(move |_button: &Button, _point: Point| {
                     if let Ok(new_url) = Url::parse(&path_box.text.get()) {
@@ -690,45 +693,42 @@ fn main_window(arg: &str, font: &Font, font_bold: &Font) {
 
         for event in window.events() {
             match event.to_option() {
-                EventOption::Key(key_event) => if key_event.pressed {
-                    match key_event.scancode {
-                        K_ESC => return,
-                        K_LEFT => {
-                            redraw = true;
-                            offset.0 = cmp::max(0, offset.0 - 60);
-                        },
-                        K_RIGHT => {
-                            redraw = true;
-                            offset.0 = cmp::min(cmp::max(0, max_offset.0 - window_w), offset.0 + 60);
-                        },
-                        K_UP => {
-                            redraw = true;
-                            offset.1 = cmp::max(0, offset.1 - 60);
-                        },
-                        K_PGUP => {
-                            redraw = true;
-                            offset.1 = cmp::max(0, offset.1 - 600);
-                        },
-                        K_DOWN => {
-                            redraw = true;
-                            offset.1 = cmp::min(cmp::max(0, max_offset.1 - window_h), offset.1 + 60);
-                        },
-                        K_PGDN => {
-                            redraw = true;
-                            offset.1 = cmp::min(cmp::max(0, max_offset.1 - window_h), offset.1 + 600);
-                        },
-                        K_BKSP => if let Some(last_url) = history.pop() {
-                            url = last_url;
+                EventOption::Key(key_event) => match key_event.scancode {
+                    K_LEFT if key_event.pressed => {
+                        redraw = true;
+                        offset.0 = cmp::max(0, offset.0 - 60);
+                    },
+                    K_RIGHT if key_event.pressed => {
+                        redraw = true;
+                        offset.0 = cmp::min(cmp::max(0, max_offset.0 - window_w), offset.0 + 60);
+                    },
+                    K_UP if key_event.pressed => {
+                        redraw = true;
+                        offset.1 = cmp::max(0, offset.1 - 60);
+                    },
+                    K_PGUP if key_event.pressed => {
+                        redraw = true;
+                        offset.1 = cmp::max(0, offset.1 - 600);
+                    },
+                    K_DOWN if key_event.pressed => {
+                        redraw = true;
+                        offset.1 = cmp::min(cmp::max(0, max_offset.1 - window_h), offset.1 + 60);
+                    },
+                    K_PGDN if key_event.pressed => {
+                        redraw = true;
+                        offset.1 = cmp::min(cmp::max(0, max_offset.1 - window_h), offset.1 + 600);
+                    },
+                    K_BKSP if ! key_event.pressed => if let Some(last_url) = history.pop() {
+                        url = last_url;
+                        reload = true;
+                    },
+                    K_ENTER if ! key_event.pressed => {
+                        if let Some(new_url) = open_dialog(&url) {
+                            url = new_url;
                             reload = true;
-                        },
-                        K_ENTER => {
-                            if let Some(new_url) = open_dialog(&url) {
-                                url = new_url;
-                                reload = true;
-                            }
-                        },
-                        _ => ()
-                    }
+                        }
+                    },
+                    _ => ()
                 },
                 EventOption::Mouse(mouse_event) => {
                     mouse_x = mouse_event.x;
diff --git a/src/editor/main.rs b/src/editor/main.rs
index 967d6ff..5286bda 100644
--- a/src/editor/main.rs
+++ b/src/editor/main.rs
@@ -50,18 +50,21 @@ fn main(){
     let open_text_box = text_box.clone();
     open_action.on_click(move |_action: &Action, _point: Point| {
         println!("Open");
-        let mut window = Window::new(Rect::new(-1, -1, 220, 32), "Open");
+        let mut window = Window::new(Rect::new(-1, -1, 320, 8 + 28 + 8 + 28 + 8), "Open");
 
         let path_box = TextBox::new();
-        path_box.position(0, 0)
-            .size(220, 16);
+        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 button = Button::new();
-            button.position(0, 16)
-                .size(220/2, 16)
+            button.position(8, 8 + 28 + 8)
+                .size((320 - 16)/2 - 4, 28)
+                .text_offset(6, 6)
                 .text("Cancel")
                 .on_click(move |_button: &Button, _point: Point| {
                     unsafe { (&mut *window_cancel).close(); }
@@ -73,8 +76,9 @@ fn main(){
             let open_text_box = open_text_box.clone();
             let window_save_as = &mut window as *mut Window;
             let button = Button::new();
-            button.position(220/2, 16)
-                .size(220/2, 16)
+            button.position(320/2 + 4, 8 + 28 + 8)
+                .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()) {
@@ -129,11 +133,13 @@ fn main(){
     let save_as_path_option = path_option.clone();
     save_as_action.on_click(move |_action: &Action, _point: Point| {
         println!("Save As");
-        let mut window = Window::new(Rect::new(-1, -1, 320, 32), "Save As");
+        let mut window = Window::new(Rect::new(-1, -1, 320, 8 + 28 + 8 + 28 + 8), "Save As");
 
         let text_box = TextBox::new();
-        text_box.position(0, 0)
-            .size(320, 16);
+        text_box.position(8, 8)
+            .size(320 - 16, 28)
+            .text_offset(6, 6)
+            .grab_focus(true);
         window.add(&text_box);
 
         if let Some(ref path) = save_as_path_option {
@@ -143,8 +149,9 @@ fn main(){
         {
             let window_cancel = &mut window as *mut Window;
             let button = Button::new();
-            button.position(0, 16)
-                .size(320/2, 16)
+            button.position(8, 8 + 28 + 8)
+                .size((320 - 16)/2 - 4, 28)
+                .text_offset(6, 6)
                 .text("Cancel")
                 .on_click(move |_button: &Button, _point: Point| {
                     unsafe { (&mut *window_cancel).close(); }
@@ -155,8 +162,9 @@ fn main(){
         {
             let window_save_as = &mut window as *mut Window;
             let button = Button::new();
-            button.position(320/2, 16)
-                .size(320/2, 16)
+            button.position(320/2 + 4, 8 + 28 + 8)
+                .size((320 - 16)/2 - 4, 28)
+                .text_offset(6, 6)
                 .text("Save As")
                 .on_click(move |_button: &Button, _point: Point| {
                     println!("Save {}", text_box.text.get());
-- 
GitLab