diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..038034f --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1 @@ +max_width = 89 diff --git a/src/grid.rs b/src/grid.rs index 7e9e9e7..88ae98a 100644 --- a/src/grid.rs +++ b/src/grid.rs @@ -4,7 +4,7 @@ pub struct Grid { inner: gtk::Grid, } -#[derive(Debug,Copy,Clone)] +#[derive(Debug, Copy, Clone)] pub struct CellPosition(i32, i32); impl Grid { @@ -14,9 +14,7 @@ impl Grid { for i in 0..(size * size) { let x = i % size; let y = i / size; - let cell = grid::Cell::new( - CellPosition(x,y) - ); + let cell = grid::Cell::new(CellPosition(x, y)); cell.inner.connect_clicked(app_state.clone()); inner.attach(cell.as_ref(), x, y, 1, 1); } @@ -24,11 +22,11 @@ impl Grid { } } -#[derive(Debug,Clone)] +#[derive(Debug, Clone)] struct CellWidget { eventbox: gtk::EventBox, position: gtk::Label, - header : gtk::Label, + header: gtk::Label, desc_btn: gtk::Button, } @@ -37,24 +35,12 @@ impl CellWidget { let cell_src = include_str!("../res/cell.glade"); let builder = gtk::Builder::new_from_string(cell_src); // Retrieve children - let eventbox: gtk::EventBox = - builder - .get_object("cell") - .unwrap(); - let header: gtk::Label = - builder - .get_object("header") - .unwrap(); + let eventbox: gtk::EventBox = builder.get_object("cell").unwrap(); + let header: gtk::Label = builder.get_object("header").unwrap(); header.set_text(header_text); - let position: gtk::Label = - builder - .get_object("position") - .unwrap(); + let position: gtk::Label = builder.get_object("position").unwrap(); position.set_text(&format!("{}x{}", pos.0, pos.1)); - let desc_btn: gtk::Button = - builder - .get_object("desc_btn") - .unwrap(); + let desc_btn: gtk::Button = builder.get_object("desc_btn").unwrap(); desc_btn.set_visible(false); let cell = CellWidget { eventbox, @@ -64,9 +50,11 @@ impl CellWidget { }; // Drag-and-drop capacity // The data to be sent - let targets = vec![ - gtk::TargetEntry::new("text/plain", gtk::TargetFlags::SAME_APP, 0) - ]; + let targets = vec![gtk::TargetEntry::new( + "text/plain", + gtk::TargetFlags::SAME_APP, + 0, + )]; // Acting as source cell.eventbox.drag_source_set( gdk::ModifierType::MODIFIER_MASK, @@ -75,24 +63,21 @@ impl CellWidget { ); let h = cell.header.clone(); // Send data to the drop site - cell.eventbox.connect_drag_data_get( - move |_,_,data,info,time| { + cell.eventbox + .connect_drag_data_get(move |_, _, data, info, time| { println!("Send..."); // TODO: Refactoring, this is the inverse of 'placing', // building a PawnData instead of destructuring it. if let Some(to_send) = h.get_text() { data.set_text(&to_send); } - } - ); + }); let c = cell.clone(); // Empty the cell on successfull move - cell.eventbox.connect_drag_data_delete( - move |w,drag| { - println!("Cleaning... {:#?}", (&drag.drag_drop_succeeded())); - Self::set_content(&c, None); - } - ); + cell.eventbox.connect_drag_data_delete(move |w, drag| { + println!("Cleaning... {:#?}", (&drag.drag_drop_succeeded())); + Self::set_content(&c, None); + }); // Acting as destination cell.eventbox.drag_dest_set( gtk::DestDefaults::ALL, @@ -101,8 +86,8 @@ impl CellWidget { ); // Retrieve data from the source let c = cell.clone(); - cell.eventbox.connect_drag_data_received( - move |w,_,_,_,data,_,_| { + cell.eventbox + .connect_drag_data_received(move |w, _, _, _, data, _, _| { // Check if cell is not already occupied if !(c.header.get_text().unwrap().as_str() == "") { // TODO: Find what to do to abort, since @@ -116,8 +101,7 @@ impl CellWidget { eprintln!("No data !"); } }; - } - ); + }); cell } @@ -136,15 +120,14 @@ impl CellWidget { pub fn connect_clicked(&self, state: AppState) { let c = self.clone(); - self.eventbox - .connect_button_press_event(move |_,_| { - let mut state = state.borrow_mut(); - if let Some(ref data) = state.pending.take() { - println!("{:?}", data); - Self::set_content(&c, Some(&data.name)); - }; - gtk::Inhibit(true) - }); + self.eventbox.connect_button_press_event(move |_, _| { + let mut state = state.borrow_mut(); + if let Some(ref data) = state.pending.take() { + println!("{:?}", data); + Self::set_content(&c, Some(&data.name)); + }; + gtk::Inhibit(true) + }); } } @@ -164,7 +147,7 @@ impl Cell { fn new(position: CellPosition) -> Self { Cell { inner: CellWidget::new(position, ""), - position + position, } } } diff --git a/src/main.rs b/src/main.rs index 6281820..994b2cb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,13 @@ -extern crate gtk; -extern crate gio; extern crate gdk; +extern crate gio; extern crate glib; +extern crate gtk; use std::cell::RefCell; use std::rc::Rc; -use gtk::prelude::*; use gio::prelude::*; +use gtk::prelude::*; use std::env::args; use std::process; @@ -16,7 +16,7 @@ mod pawn; /// Content of the Application state /// -#[derive(Debug,Clone,Default)] +#[derive(Debug, Clone, Default)] pub struct AppData { /// A slot for any pawn waiting for placement pending: Option, @@ -25,7 +25,7 @@ pub struct AppData { } /// A sharable state for the whole application -#[derive(Debug,Clone,Default)] +#[derive(Debug, Clone, Default)] pub struct AppState(Rc>); impl std::ops::Deref for AppState { @@ -40,17 +40,16 @@ struct App { state: AppState, } - impl App { fn new<'a>() -> Result { - let app = - App { - inner: gtk::Application::new( - "home.local.PlayMat", - gio::ApplicationFlags::FLAGS_NONE - ).expect("Failed to build Application"), - state: Default::default(), - }; + let app = App { + inner: gtk::Application::new( + "home.local.PlayMat", + gio::ApplicationFlags::FLAGS_NONE, + ) + .expect("Failed to build Application"), + state: Default::default(), + }; app.connect_all(); Ok(app) } @@ -58,45 +57,39 @@ impl App { fn connect_all(&self) { let app_state = self.state.clone(); self.inner.connect_startup(|_| {}); - self.inner.connect_activate( - move |app| { - println!("Activate App"); - let main_src = include_str!("../res/main.glade"); - let builder = gtk::Builder::new_from_string(main_src); - let win: gtk::ApplicationWindow = - builder - .get_object("app") - .unwrap(); - win.set_application(app); - // Set up a simple switch for the Pawn list panel - let panel: gtk::Paned = builder.get_object("panel").unwrap(); - Self::new_action(app, "panel_switch", move |_,_| { - if panel.get_position() == 0 { - panel.set_position(360); - } else { - panel.set_position(0); - } - }); - // Pawn list - let pawn_list = builder.get_object("pawn_list").unwrap(); - let pawn_list = pawn::PawnList::init(pawn_list); - - for pawn in pawn::pawn_factory() { - pawn_list.add(&pawn); - pawn.connect_place(app_state.clone()); - pawn.connect_stats(); - app_state.borrow_mut().pawns.push(pawn); + self.inner.connect_activate(move |app| { + println!("Activate App"); + let main_src = include_str!("../res/main.glade"); + let builder = gtk::Builder::new_from_string(main_src); + let win: gtk::ApplicationWindow = builder.get_object("app").unwrap(); + win.set_application(app); + // Set up a simple switch for the Pawn list panel + let panel: gtk::Paned = builder.get_object("panel").unwrap(); + Self::new_action(app, "panel_switch", move |_, _| { + if panel.get_position() == 0 { + panel.set_position(360); + } else { + panel.set_position(0); } - win.show_all(); // Before grid because we want to hide some widgets there - // Initialize grid - let grid: gtk::Grid = builder.get_object("map").unwrap(); - // TODO: implement drag-drop events - // * From pawn_list to cell : place pawn on dest cell - // * From cell to cell : move pawn from source to dest cell - let _grid = grid::Grid::init(grid, 10, app_state.clone()); + }); + // Pawn list + let pawn_list = builder.get_object("pawn_list").unwrap(); + let pawn_list = pawn::PawnList::init(pawn_list); + for pawn in pawn::pawn_factory() { + pawn_list.add(&pawn); + pawn.connect_place(app_state.clone()); + pawn.connect_stats(); + app_state.borrow_mut().pawns.push(pawn); } - ); + win.show_all(); // Before grid because we want to hide some widgets there + // Initialize grid + let grid: gtk::Grid = builder.get_object("map").unwrap(); + // TODO: implement drag-drop events + // * From pawn_list to cell : place pawn on dest cell + // * From cell to cell : move pawn from source to dest cell + let _grid = grid::Grid::init(grid, 10, app_state.clone()); + }); } /// Creates a simple action and connects the given handler to its activate signal. @@ -119,13 +112,13 @@ impl App { fn main() { if gtk::init().is_err() { println!("Failed to initialize Gtk"); - return + return; } let exit_code = match App::new() { Err(e) => { println!("Error !\n{}", e); 1 - }, + } Ok(app) => app.run(&args().collect::>()), }; process::exit(exit_code); diff --git a/src/pawn.rs b/src/pawn.rs index 84d3347..4cf6cce 100644 --- a/src/pawn.rs +++ b/src/pawn.rs @@ -1,8 +1,7 @@ - use super::*; /// A wrapper around gtk::ListBox for Pawns -#[derive(Debug,Clone)] +#[derive(Debug, Clone)] pub struct PawnList { inner: gtk::ListBox, } @@ -10,7 +9,7 @@ pub struct PawnList { impl PawnList { /// Initialize from the given existing ListBox pub fn init(inner: gtk::ListBox) -> Self { - PawnList{ inner } + PawnList { inner } } /// Adds a Pawn to the list @@ -22,7 +21,7 @@ impl PawnList { } /// Application data related to a Pawn -#[derive(Debug,Clone)] +#[derive(Debug, Clone)] pub struct PawnData { pub name: String, pub position: Option, // Content of label from CellWidget @@ -42,14 +41,14 @@ impl Pawn { let pawn_src = include_str!("../res/pawn.glade"); let builder = gtk::Builder::new_from_string(pawn_src); let name = name.into(); - let label: gtk::Label = - builder - .get_object("name") - .unwrap(); + let label: gtk::Label = builder.get_object("name").unwrap(); label.set_text(&name); let widget: gtk::Box = builder.get_object("pawn").unwrap(); Pawn { - data: PawnData{ name, position: None, }, + data: PawnData { + name, + position: None, + }, widget, place_btn: builder.get_object("place_btn").unwrap(), stats_btn: builder.get_object("stats_btn").unwrap(), @@ -59,23 +58,17 @@ impl Pawn { pub fn connect_place(&self, state: AppState) { let name = self.data.name.clone(); let data = self.data.clone(); - self.place_btn - .connect_clicked( - move |_| { - println!("Placing {}...", name); - let mut state = state.0.borrow_mut(); - state.pending = Some(data.clone()); // ??? - } - ); + self.place_btn.connect_clicked(move |_| { + println!("Placing {}...", name); + let mut state = state.0.borrow_mut(); + state.pending = Some(data.clone()); // ??? + }); } pub fn connect_stats(&self) { - self.stats_btn - .connect_clicked( - move |_| { - println!("Showing stats..."); - } - ); + self.stats_btn.connect_clicked(move |_| { + println!("Showing stats..."); + }); } }