improves ui, cleans up a little

This commit is contained in:
2019-06-07 16:27:52 +02:00
parent 96b6c523d7
commit b2d372f8a7
4 changed files with 97 additions and 52 deletions

View File

@@ -33,7 +33,7 @@ struct Cell {
impl Cell {
fn new(position: CellPosition) -> Self {
Cell {
inner: CellWidget::new(position, ""),
inner: CellWidget::new(position),
position,
}
}
@@ -47,6 +47,7 @@ impl AsRef<gtk::EventBox> for Cell {
}
/// The cell widget is either empty or has a pawn
/// Pawn can be moved around cells using drag-n-drop. However, due to my inability
/// to dynamically change the behavior with set/unset methods, all cells are always
@@ -57,41 +58,33 @@ impl AsRef<gtk::EventBox> for Cell {
struct CellWidget {
eventbox: gtk::EventBox,
position: gtk::Label,
header: gtk::Label,
name: gtk::Label,
desc_btn: gtk::Button,
targets: Vec<gtk::TargetEntry>,
}
impl CellWidget {
fn new(pos: CellPosition, header_text: &str) -> Self {
fn new(pos: CellPosition) -> Self {
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();
header.set_text(header_text);
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();
desc_btn.set_visible(false);
let cell = CellWidget {
eventbox,
position,
header,
desc_btn,
eventbox: builder.get_object("cell").unwrap(),
position: builder.get_object("position").unwrap(),
name: builder.get_object("name").unwrap(),
desc_btn: builder.get_object("desc_btn").unwrap(),
targets: vec![
gtk::TargetEntry::new("text/plain", gtk::TargetFlags::SAME_APP, 0),
],
};
Self::set_content(&cell, None);
cell.desc_btn.set_visible(false);
cell.position.set_text(&format!("{}x{}", pos.0, pos.1));
cell.set_drag();
cell.set_drop();
Self::set_content(&cell, None);
cell
}
// The data to be sent
fn targets() -> Vec<gtk::TargetEntry> {
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) {
@@ -99,7 +92,7 @@ impl CellWidget {
// Acting as source
self.eventbox.drag_source_set(
gdk::ModifierType::MODIFIER_MASK,
&Self::targets(),
&self.targets,
gdk::DragAction::MOVE,
);
let c = self.clone();
@@ -109,7 +102,7 @@ impl CellWidget {
println!("Send...");
// TODO: Refactoring, this is the inverse of 'placing',
// building a PawnData instead of destructuring it.
if let Some(to_send) = c.header.get_text() {
if let Some(to_send) = c.name.get_text() {
data.set_text(&to_send);
} else {
dbg!("Should not happen !!");
@@ -130,7 +123,7 @@ impl CellWidget {
// Acting as destination
self.eventbox.drag_dest_set(
gtk::DestDefaults::ALL,
&Self::targets(),
&self.targets,
gdk::DragAction::MOVE,
);
// Retrieve data from the source
@@ -138,7 +131,7 @@ impl CellWidget {
self.eventbox
.connect_drag_data_received(move |w, _, _, _, data, _, _| {
// Check if cell is not already occupied
if !(c.header.get_text().unwrap().as_str() == "") {
if !(c.name.get_text().unwrap().as_str() == "") {
// TODO: Find what to do to abort, since
// DragContext methods don't seem to work...
println!("Overriding !!");
@@ -158,10 +151,10 @@ impl CellWidget {
println!("Set content to {:?}", &data);
// TODO: there is surely something cleaner...
if let Some(name) = data {
self.header.set_text(name);
self.name.set_text(name);
self.desc_btn.set_visible(true);
} else {
self.header.set_text("");
self.name.set_text("");
self.desc_btn.set_visible(false);
};
}