refactors. pawn placement is weird but usable

This commit is contained in:
2019-06-07 15:32:32 +02:00
parent 8f4635eaba
commit de02b27949

View File

@@ -4,6 +4,7 @@ pub struct Grid {
inner: gtk::Grid, inner: gtk::Grid,
} }
/// Position of a cell as (x,y) tuple.
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
pub struct CellPosition(i32, i32); 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<gtk::EventBox> for Cell {
#[inline]
fn as_ref(&self) -> &gtk::EventBox {
self.inner.as_ref()
}
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
struct CellWidget { struct CellWidget {
eventbox: gtk::EventBox, eventbox: gtk::EventBox,
@@ -48,45 +72,63 @@ impl CellWidget {
header, header,
desc_btn, desc_btn,
}; };
// Drag-and-drop capacity Self::set_content(&cell, None);
cell.set_drag();
cell.set_drop();
cell
}
// The data to be sent // The data to be sent
let targets = vec![gtk::TargetEntry::new( fn targets() -> Vec<gtk::TargetEntry> {
"text/plain", vec![
gtk::TargetFlags::SAME_APP, gtk::TargetEntry::new("text/plain", gtk::TargetFlags::SAME_APP, 0)
0, ]
)]; }
fn unset_drag(&self) { dbg!("Unset drag"); self.eventbox.drag_source_unset(); }
fn set_drag(&self) {
dbg!("Set drag");
// Acting as source // Acting as source
cell.eventbox.drag_source_set( self.eventbox.drag_source_set(
gdk::ModifierType::MODIFIER_MASK, gdk::ModifierType::MODIFIER_MASK,
&targets, &Self::targets(),
gdk::DragAction::MOVE, gdk::DragAction::MOVE,
); );
let h = cell.header.clone(); let c = self.clone();
// Send data to the drop site // Send data to the drop site
cell.eventbox self.eventbox
.connect_drag_data_get(move |_, _, data, info, time| { .connect_drag_data_get(move |_, _, data, info, time| {
println!("Send..."); println!("Send...");
// TODO: Refactoring, this is the inverse of 'placing', // TODO: Refactoring, this is the inverse of 'placing',
// building a PawnData instead of destructuring it. // 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); data.set_text(&to_send);
} else {
dbg!("Should not happen !!");
} }
}); });
let c = cell.clone();
// Empty the cell on successfull move // 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())); println!("Cleaning... {:#?}", (&drag.drag_drop_succeeded()));
Self::set_content(&c, None); 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 // Acting as destination
cell.eventbox.drag_dest_set( self.eventbox.drag_dest_set(
gtk::DestDefaults::ALL, gtk::DestDefaults::ALL,
&targets, &Self::targets(),
gdk::DragAction::MOVE, gdk::DragAction::MOVE,
); );
// Retrieve data from the source // Retrieve data from the source
let c = cell.clone(); let c = self.clone();
cell.eventbox self.eventbox
.connect_drag_data_received(move |w, _, _, _, data, _, _| { .connect_drag_data_received(move |w, _, _, _, data, _, _| {
// Check if cell is not already occupied // Check if cell is not already occupied
if !(c.header.get_text().unwrap().as_str() == "") { if !(c.header.get_text().unwrap().as_str() == "") {
@@ -102,7 +144,6 @@ impl CellWidget {
} }
}; };
}); });
cell
} }
/// Updates content of the Cell /// Updates content of the Cell
@@ -138,23 +179,3 @@ impl AsRef<gtk::EventBox> for CellWidget {
} }
} }
struct Cell {
inner: CellWidget,
position: CellPosition,
}
impl Cell {
fn new(position: CellPosition) -> Self {
Cell {
inner: CellWidget::new(position, ""),
position,
}
}
}
impl AsRef<gtk::EventBox> for Cell {
#[inline]
fn as_ref(&self) -> &gtk::EventBox {
self.inner.as_ref()
}
}