makes content moving work
This commit is contained in:
@@ -19,11 +19,12 @@
|
|||||||
<property name="shadow_type">in</property>
|
<property name="shadow_type">in</property>
|
||||||
<child>
|
<child>
|
||||||
<object class="GtkBox" id="box">
|
<object class="GtkBox" id="box">
|
||||||
<property name="width_request">128</property>
|
<property name="width_request">96</property>
|
||||||
<property name="height_request">128</property>
|
<property name="height_request">96</property>
|
||||||
<property name="visible">True</property>
|
<property name="visible">True</property>
|
||||||
<property name="can_focus">False</property>
|
<property name="can_focus">False</property>
|
||||||
<property name="orientation">vertical</property>
|
<property name="orientation">vertical</property>
|
||||||
|
<property name="homogeneous">True</property>
|
||||||
<child>
|
<child>
|
||||||
<object class="GtkLabel" id="header">
|
<object class="GtkLabel" id="header">
|
||||||
<property name="visible">True</property>
|
<property name="visible">True</property>
|
||||||
@@ -43,14 +44,17 @@
|
|||||||
</packing>
|
</packing>
|
||||||
</child>
|
</child>
|
||||||
<child>
|
<child>
|
||||||
<object class="GtkLabel" id="content">
|
<object class="GtkButton" id="desc_btn">
|
||||||
<property name="visible">True</property>
|
<property name="label" translatable="yes">Desc. </property>
|
||||||
<property name="can_focus">False</property>
|
<property name="can_focus">True</property>
|
||||||
<property name="label" translatable="yes">Occupancy
|
<property name="receives_default">False</property>
|
||||||
Status</property>
|
<property name="halign">center</property>
|
||||||
|
<property name="valign">end</property>
|
||||||
|
<property name="margin_bottom">5</property>
|
||||||
|
<property name="relief">none</property>
|
||||||
</object>
|
</object>
|
||||||
<packing>
|
<packing>
|
||||||
<property name="expand">True</property>
|
<property name="expand">False</property>
|
||||||
<property name="fill">True</property>
|
<property name="fill">True</property>
|
||||||
<property name="position">1</property>
|
<property name="position">1</property>
|
||||||
</packing>
|
</packing>
|
||||||
@@ -59,7 +63,6 @@
|
|||||||
</child>
|
</child>
|
||||||
<child type="label">
|
<child type="label">
|
||||||
<object class="GtkLabel" id="position">
|
<object class="GtkLabel" id="position">
|
||||||
<property name="visible">True</property>
|
|
||||||
<property name="can_focus">False</property>
|
<property name="can_focus">False</property>
|
||||||
<property name="label" translatable="yes">_x_</property>
|
<property name="label" translatable="yes">_x_</property>
|
||||||
<attributes>
|
<attributes>
|
||||||
|
|||||||
@@ -71,51 +71,6 @@
|
|||||||
<property name="valign">center</property>
|
<property name="valign">center</property>
|
||||||
<property name="hexpand">True</property>
|
<property name="hexpand">True</property>
|
||||||
<property name="vexpand">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>
|
<child>
|
||||||
<placeholder/>
|
<placeholder/>
|
||||||
</child>
|
</child>
|
||||||
|
|||||||
118
src/grid.rs
118
src/grid.rs
@@ -18,7 +18,7 @@ impl Grid {
|
|||||||
CellPosition(x,y)
|
CellPosition(x,y)
|
||||||
);
|
);
|
||||||
cell.inner.connect_clicked(app_state.clone());
|
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);
|
inner.attach(cell.as_ref(), x, y, 1, 1);
|
||||||
}
|
}
|
||||||
Grid { inner }
|
Grid { inner }
|
||||||
@@ -30,13 +30,13 @@ struct CellWidget {
|
|||||||
eventbox: gtk::EventBox,
|
eventbox: gtk::EventBox,
|
||||||
position: gtk::Label,
|
position: gtk::Label,
|
||||||
header : gtk::Label,
|
header : gtk::Label,
|
||||||
content: gtk::Label,
|
desc_btn: gtk::Button,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CellWidget {
|
impl CellWidget {
|
||||||
fn new(pos: CellPosition, header_text: &str) -> Self {
|
fn new(pos: CellPosition, header_text: &str) -> Self {
|
||||||
let cell_src = include_str!("../res/cell.glade");
|
let cell_src = "/home/artus/Projets/rust/playmat/res/cell.glade";
|
||||||
let builder = gtk::Builder::new_from_string(&cell_src);
|
let builder = gtk::Builder::new_from_file(cell_src);
|
||||||
|
|
||||||
// Set up reactivity on buttons
|
// Set up reactivity on buttons
|
||||||
let eventbox: gtk::EventBox =
|
let eventbox: gtk::EventBox =
|
||||||
@@ -54,85 +54,93 @@ impl CellWidget {
|
|||||||
.get_object("position")
|
.get_object("position")
|
||||||
.unwrap();
|
.unwrap();
|
||||||
position.set_text(&format!("{}x{}", pos.0, pos.1));
|
position.set_text(&format!("{}x{}", pos.0, pos.1));
|
||||||
|
|
||||||
// Drag-and-drop capacity
|
// 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(
|
eventbox.drag_source_set(
|
||||||
gdk::ModifierType::MODIFIER_MASK,
|
gdk::ModifierType::MODIFIER_MASK,
|
||||||
&targets,
|
&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(
|
eventbox.drag_dest_set(
|
||||||
gtk::DestDefaults::ALL,
|
gtk::DestDefaults::ALL,
|
||||||
&targets,
|
&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 {
|
let cell = CellWidget {
|
||||||
eventbox,
|
eventbox,
|
||||||
position,
|
position,
|
||||||
header,
|
header,
|
||||||
content: builder.get_object("content").unwrap(),
|
desc_btn
|
||||||
};
|
};
|
||||||
cell
|
cell
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn connect_clicked(&self, state: AppState) {
|
pub fn connect_clicked(&self, state: AppState) {
|
||||||
let header = self.header.clone();
|
let header = self.header.clone();
|
||||||
let content = self.content.clone();
|
|
||||||
self.eventbox
|
self.eventbox
|
||||||
.connect_button_press_event(move |_,_| {
|
.connect_button_press_event(move |_,_| {
|
||||||
let mut state = state.borrow_mut();
|
let mut state = state.borrow_mut();
|
||||||
if let Some(data) = state.pending.take() {
|
if let Some(data) = state.pending.take() {
|
||||||
println!("{:?}", data);
|
println!("{:?}", data);
|
||||||
content.set_text(&data.name);
|
header.set_text(&data.name);
|
||||||
};
|
};
|
||||||
header.set_text("Is Clicked.");
|
|
||||||
gtk::Inhibit(true)
|
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)
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AsRef<gtk::EventBox> for CellWidget {
|
impl AsRef<gtk::EventBox> for CellWidget {
|
||||||
|
|||||||
@@ -61,8 +61,8 @@ impl App {
|
|||||||
self.inner.connect_activate(
|
self.inner.connect_activate(
|
||||||
move |app| {
|
move |app| {
|
||||||
println!("Activate App");
|
println!("Activate App");
|
||||||
let main_src = include_str!("../res/main.glade");
|
let main_src = "/home/artus/Projets/rust/playmat/res/main.glade";
|
||||||
let builder = gtk::Builder::new_from_string(main_src);
|
let builder = gtk::Builder::new_from_file(main_src);
|
||||||
let win: gtk::ApplicationWindow =
|
let win: gtk::ApplicationWindow =
|
||||||
builder
|
builder
|
||||||
.get_object("app")
|
.get_object("app")
|
||||||
|
|||||||
@@ -39,8 +39,8 @@ pub struct Pawn {
|
|||||||
|
|
||||||
impl Pawn {
|
impl Pawn {
|
||||||
pub fn new<S: Into<String>>(name: S) -> Self {
|
pub fn new<S: Into<String>>(name: S) -> Self {
|
||||||
let pawn_src = include_str!("../res/pawn.glade");
|
let pawn_src = "/home/artus/Projets/rust/playmat/res/pawn.glade";
|
||||||
let builder = gtk::Builder::new_from_string(pawn_src);
|
let builder = gtk::Builder::new_from_file(pawn_src);
|
||||||
let name = name.into();
|
let name = name.into();
|
||||||
let label: gtk::Label =
|
let label: gtk::Label =
|
||||||
builder
|
builder
|
||||||
|
|||||||
Reference in New Issue
Block a user