makes content moving work

This commit is contained in:
2019-06-06 16:36:10 +02:00
parent b0cfa3f9aa
commit 60312be5d8
5 changed files with 79 additions and 113 deletions

View File

@@ -19,11 +19,12 @@
<property name="shadow_type">in</property>
<child>
<object class="GtkBox" id="box">
<property name="width_request">128</property>
<property name="height_request">128</property>
<property name="width_request">96</property>
<property name="height_request">96</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<property name="homogeneous">True</property>
<child>
<object class="GtkLabel" id="header">
<property name="visible">True</property>
@@ -43,14 +44,17 @@
</packing>
</child>
<child>
<object class="GtkLabel" id="content">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Occupancy
Status</property>
<object class="GtkButton" id="desc_btn">
<property name="label" translatable="yes">Desc. </property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="halign">center</property>
<property name="valign">end</property>
<property name="margin_bottom">5</property>
<property name="relief">none</property>
</object>
<packing>
<property name="expand">True</property>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
@@ -59,7 +63,6 @@
</child>
<child type="label">
<object class="GtkLabel" id="position">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">_x_</property>
<attributes>

View File

@@ -71,51 +71,6 @@
<property name="valign">center</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<child>
<object class="GtkEventBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<signal name="drag-begin" handler="drag_begin" swapped="no"/>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">label</property>
<signal name="drag-begin" handler="drag_begin" swapped="no"/>
</object>
</child>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkEventBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">label</property>
</object>
</child>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">1</property>
</packing>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>

View File

@@ -18,7 +18,7 @@ impl Grid {
CellPosition(x,y)
);
cell.inner.connect_clicked(app_state.clone());
cell.inner.connect_dragging(app_state.clone());
cell.inner.desc_btn.set_visible(false);
inner.attach(cell.as_ref(), x, y, 1, 1);
}
Grid { inner }
@@ -30,13 +30,13 @@ struct CellWidget {
eventbox: gtk::EventBox,
position: gtk::Label,
header : gtk::Label,
content: gtk::Label,
desc_btn: gtk::Button,
}
impl CellWidget {
fn new(pos: CellPosition, header_text: &str) -> Self {
let cell_src = include_str!("../res/cell.glade");
let builder = gtk::Builder::new_from_string(&cell_src);
let cell_src = "/home/artus/Projets/rust/playmat/res/cell.glade";
let builder = gtk::Builder::new_from_file(cell_src);
// Set up reactivity on buttons
let eventbox: gtk::EventBox =
@@ -54,82 +54,90 @@ impl CellWidget {
.get_object("position")
.unwrap();
position.set_text(&format!("{}x{}", pos.0, pos.1));
// Drag-and-drop capacity
let targets = vec![];
let desc_btn: gtk::Button =
builder
.get_object("desc_btn")
.unwrap();
desc_btn.set_visible(false);
// The data to be send
let targets = vec![
gtk::TargetEntry::new("text/plain", gtk::TargetFlags::SAME_APP, 0)
];
eventbox.drag_source_set(
gdk::ModifierType::MODIFIER_MASK,
&targets,
gdk::DragAction::COPY,
gdk::DragAction::MOVE,
);
let h = header.clone();
// Send data to the drop site
eventbox.connect_drag_data_get(
move |_,_,data,info,time| {
println!("Send...");
if let Some(to_send) = h.get_text() {
data.set_text(&to_send);
}
}
);
let h = header.clone();
let b = desc_btn.clone();
// Empty the cell on successfull move
eventbox.connect_drag_data_delete(
move |w,drag| {
println!("Cleaning... {:#?}", (&drag.drag_drop_succeeded()));
h.set_text("");
b.set_visible(false);
}
);
eventbox.drag_dest_set(
gtk::DestDefaults::ALL,
&targets,
gdk::DragAction::COPY,
gdk::DragAction::MOVE,
);
// Retrieve data from the source
let h = header.clone();
let b = desc_btn.clone();
eventbox.connect_drag_data_received(
move |_,drag,_,_,data,info,time| {
let del =
// Check if cell is not already occupied
if !(h.get_text().unwrap().as_str() == "") {
// TODO: Find what to do to abort, since
// DragContext methods don't seem to work...
println!("Overriding !!");
false
} else {
println!("Dropped !");
if let Some(text) = data.get_text() {
h.set_text(&text);
b.set_visible(true);
} else {
eprintln!("No data !");
}
true
};
}
);
let cell = CellWidget {
eventbox,
position,
header,
content: builder.get_object("content").unwrap(),
desc_btn
};
cell
}
pub fn connect_clicked(&self, state: AppState) {
let header = self.header.clone();
let content = self.content.clone();
self.eventbox
.connect_button_press_event(move |_,_| {
let mut state = state.borrow_mut();
if let Some(data) = state.pending.take() {
println!("{:?}", data);
content.set_text(&data.name);
header.set_text(&data.name);
};
header.set_text("Is Clicked.");
gtk::Inhibit(true)
});
}
pub fn connect_dragging(&self, state: AppState) {
// TODO: start over.
// Use this for reference :
// https://python-gtk-3-tutorial.readthedocs.io/en/latest/drag_and_drop.html
// For starters, avoid using app_state and just copy the content of
// source cell inside dest cell.
let state1 = state.clone();
self.eventbox.connect_drag_begin(move |_,_| {
println!("Lift...");
let mut state = state1.borrow_mut();
state.pending = Some(pawn::PawnData{name: "Dropped".to_string(), position: None});
// Copy the current instance data
// inside app_state.pending
});
// TODO: Add failed signal to remove data
let state2 = state.clone();
let content = self.content.clone();
self.eventbox.connect_drag_end(move |_,_| {
println!("Drop !");
let mut state = state2.borrow_mut();
if state.pending.is_none() {
content.set_text("Emptied");
}
state.pending = None;
// Same as clicked, grab data
});
let state3 = state.clone();
let content = self.content.clone();
self.eventbox.connect_drag_drop(move |_,drag_ctx,_,_,time| {
println!("Got ! {:?}", "");
let mut state = state3.borrow_mut();
if let Some(data) = state.pending.take() {
content.set_text(&data.name);
};
drag_ctx.drag_finish(true, true, time);
gtk::Inhibit(true)
});
}

View File

@@ -61,8 +61,8 @@ impl App {
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 main_src = "/home/artus/Projets/rust/playmat/res/main.glade";
let builder = gtk::Builder::new_from_file(main_src);
let win: gtk::ApplicationWindow =
builder
.get_object("app")

View File

@@ -39,8 +39,8 @@ pub struct Pawn {
impl Pawn {
pub fn new<S: Into<String>>(name: S) -> Self {
let pawn_src = include_str!("../res/pawn.glade");
let builder = gtk::Builder::new_from_string(pawn_src);
let pawn_src = "/home/artus/Projets/rust/playmat/res/pawn.glade";
let builder = gtk::Builder::new_from_file(pawn_src);
let name = name.into();
let label: gtk::Label =
builder