diff src/main.rs @ 9:d1972fc49a5b

Reorganise the code a bit.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 01 Nov 2020 15:23:57 +0100
parents 51a6c86d3141
children 06d77bb94a50
line wrap: on
line diff
--- a/src/main.rs
+++ b/src/main.rs
@@ -15,8 +15,8 @@
 // along with this program.  If not, see <https://www.gnu.org/licenses/>.
 
 use gio::prelude::*;
+use glib::clone;
 use gtk::prelude::*;
-use glib::clone;
 
 use std::env::args;
 use std::fs::{File, OpenOptions};
@@ -216,7 +216,26 @@ struct State {
     pressed: bool,
 }
 
-fn build_ui(application: &gtk::Application) {
+impl State {
+    fn new() -> std::io::Result<Arc<Mutex<State>>> {
+        let dev = create_uinput_device()?;
+        println!(
+            "New device at {:?} ({:?})",
+            dev.evdev_path()?,
+            dev.sys_path()?
+        );
+
+        Ok(Arc::new(Mutex::new(State {
+            dev,
+            width: WIDTH as f64,
+            height: HEIGHT as f64,
+            selected_tool: Key::ButtonToolPen,
+            pressed: false,
+        })))
+    }
+}
+
+fn build_main_menu(application: &gtk::Application) {
     let quit = gio::SimpleAction::new("quit", None);
     application.set_accels_for_action("app.quit", &["<Control>q"]);
     application.add_action(&quit);
@@ -233,7 +252,9 @@ fn build_ui(application: &gtk::Applicati
         about.set_license_type(gtk::License::Agpl30);
         about.set_copyright(Some("© 2020 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>"));
         about.run();
-        unsafe { about.destroy(); }
+        unsafe {
+            about.destroy();
+        }
     });
 
     let menu = gio::Menu::new();
@@ -248,9 +269,13 @@ fn build_ui(application: &gtk::Applicati
         menu.append_submenu(Some("_Help"), &help);
     }
     application.set_menubar(Some(&menu));
+}
 
-    let dev = match create_uinput_device() {
-        Ok(dev) => dev,
+fn build_ui(application: &gtk::Application) {
+    build_main_menu(application);
+
+    let state = match State::new() {
+        Ok(state) => state,
         Err(err) => {
             match err.kind() {
                 ErrorKind::NotFound => {
@@ -263,25 +288,11 @@ fn build_ui(application: &gtk::Applicati
                 }
                 _ => eprintln!("Couldn’t open /dev/uinput for writing: {}", err),
             }
-            return;
+            std::process::exit(1);
         }
     };
-    println!(
-        "New device at {:?} ({:?})",
-        dev.evdev_path().unwrap(),
-        dev.sys_path().unwrap()
-    );
-
-    let state = Arc::new(Mutex::new(State {
-        dev,
-        width: WIDTH as f64,
-        height: HEIGHT as f64,
-        selected_tool: Key::ButtonToolPen,
-        pressed: false,
-    }));
 
     let window = gtk::ApplicationWindow::new(application);
-
     window.set_title("tablet-emu");
     window.set_position(gtk::WindowPosition::Center);