From de02b27949fd2214f02211f4831451125e352e64 Mon Sep 17 00:00:00 2001 From: Artus Date: Fri, 7 Jun 2019 15:32:32 +0200 Subject: [PATCH] refactors. pawn placement is weird but usable --- src/grid.rs | 99 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 60 insertions(+), 39 deletions(-) diff --git a/src/grid.rs b/src/grid.rs index 88ae98a..46752c8 100644 --- a/src/grid.rs +++ b/src/grid.rs @@ -4,6 +4,7 @@ pub struct Grid { inner: gtk::Grid, } +/// Position of a cell as (x,y) tuple. #[derive(Debug, Copy, Clone)] pub struct CellPosition(i32, i32); @@ -22,6 +23,29 @@ impl Grid { } } +/// A single cell of the grid +struct Cell { + /// The inner widget + inner: CellWidget, + position: CellPosition, +} + +impl Cell { + fn new(position: CellPosition) -> Self { + Cell { + inner: CellWidget::new(position, ""), + position, + } + } +} + +impl AsRef for Cell { + #[inline] + fn as_ref(&self) -> >k::EventBox { + self.inner.as_ref() + } +} + #[derive(Debug, Clone)] struct CellWidget { eventbox: gtk::EventBox, @@ -48,45 +72,63 @@ impl CellWidget { header, desc_btn, }; - // Drag-and-drop capacity - // The data to be sent - let targets = vec![gtk::TargetEntry::new( - "text/plain", - gtk::TargetFlags::SAME_APP, - 0, - )]; + Self::set_content(&cell, None); + cell.set_drag(); + cell.set_drop(); + cell + } + + // The data to be sent + fn targets() -> Vec { + vec![ + gtk::TargetEntry::new("text/plain", gtk::TargetFlags::SAME_APP, 0) + ] + } + + fn unset_drag(&self) { dbg!("Unset drag"); self.eventbox.drag_source_unset(); } + + fn set_drag(&self) { + dbg!("Set drag"); // Acting as source - cell.eventbox.drag_source_set( + self.eventbox.drag_source_set( gdk::ModifierType::MODIFIER_MASK, - &targets, + &Self::targets(), gdk::DragAction::MOVE, ); - let h = cell.header.clone(); + let c = self.clone(); // Send data to the drop site - cell.eventbox + self.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() { + if let Some(to_send) = c.header.get_text() { data.set_text(&to_send); + } else { + dbg!("Should not happen !!"); } }); - let c = cell.clone(); // Empty the cell on successfull move - cell.eventbox.connect_drag_data_delete(move |w, drag| { + let c = self.clone(); + self.eventbox.connect_drag_data_delete(move |w, drag| { println!("Cleaning... {:#?}", (&drag.drag_drop_succeeded())); Self::set_content(&c, None); }); + } + + fn unset_drop(&self) { dbg!("Unset drop"); self.eventbox.drag_dest_unset(); } + + fn set_drop(&self) { + dbg!("Set drop"); // Acting as destination - cell.eventbox.drag_dest_set( + self.eventbox.drag_dest_set( gtk::DestDefaults::ALL, - &targets, + &Self::targets(), gdk::DragAction::MOVE, ); // Retrieve data from the source - let c = cell.clone(); - cell.eventbox + let c = self.clone(); + self.eventbox .connect_drag_data_received(move |w, _, _, _, data, _, _| { // Check if cell is not already occupied if !(c.header.get_text().unwrap().as_str() == "") { @@ -102,7 +144,6 @@ impl CellWidget { } }; }); - cell } /// Updates content of the Cell @@ -138,23 +179,3 @@ impl AsRef for CellWidget { } } -struct Cell { - inner: CellWidget, - position: CellPosition, -} - -impl Cell { - fn new(position: CellPosition) -> Self { - Cell { - inner: CellWidget::new(position, ""), - position, - } - } -} - -impl AsRef for Cell { - #[inline] - fn as_ref(&self) -> >k::EventBox { - self.inner.as_ref() - } -}