Skip to content

Commit

Permalink
Add more docs and fix all warnings and tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmerlin committed Jul 13, 2023
1 parent 3650036 commit 450ddb5
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 33 deletions.
5 changes: 2 additions & 3 deletions examples/nested.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use eframe::egui;
use eframe::egui::{CollapsingHeader, Id, Ui};

use egui_dnd::utils::shift_vec;
use egui_dnd::{DragDropItem, DragDropUi, Handle};

pub fn main() {
Expand Down Expand Up @@ -96,7 +95,7 @@ impl MyApp {
let response = item.drag_drop_ui.ui(
ui,
children.iter_mut(),
|item, ui, handle, pressed| {
|item, ui, handle, _pressed| {
Self::draw_item(ui, item, handle);
},
);
Expand All @@ -112,7 +111,7 @@ impl eframe::App for MyApp {
egui::CentralPanel::default().show(ctx, |ui| {
let response =
self.drag_drop_ui
.ui(ui, self.items.iter_mut(), |item, ui, handle, pressed| {
.ui(ui, self.items.iter_mut(), |item, ui, handle, _pressed| {
MyApp::draw_item(ui, item, handle);
});
response.update_vec(&mut self.items);
Expand Down
6 changes: 3 additions & 3 deletions examples/scroll.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use eframe::NativeOptions;
use egui::{CentralPanel, Id, ScrollArea, Sense};
use egui::{CentralPanel, ScrollArea, Sense};
use std::hash::{Hash, Hasher};

use egui_dnd::{DragDropItem, DragDropUi};
use egui_dnd::DragDropUi;

struct ItemType {
number: u32,
Expand All @@ -28,7 +28,7 @@ fn main() -> eframe::Result<()> {
let response = dnd.ui::<&mut ItemType>(
ui,
items.iter_mut(),
|item, ui, handle, dragging| {
|item, ui, handle, _dragging| {
ui.horizontal(|ui| {
if handle
.sense(Sense::click())
Expand Down
3 changes: 1 addition & 2 deletions examples/simple.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use eframe::egui;
use egui::CentralPanel;
use egui_dnd::dnd;
use std::hash::Hash;

pub fn main() -> eframe::Result<()> {
let mut items = vec!["alfred", "bernhard", "christian"];
Expand All @@ -11,7 +10,7 @@ pub fn main() -> eframe::Result<()> {
Default::default(),
move |ctx, _frame| {
CentralPanel::default().show(ctx, |ui| {
dnd(ui, "dnd_example").show_vec(&mut items, |ui, item, handle, state| {
dnd(ui, "dnd_example").show_vec(&mut items, |ui, item, handle, _state| {
handle.ui(ui, |ui| {
ui.label("drag");
});
Expand Down
3 changes: 1 addition & 2 deletions examples/table.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use eframe::egui::{Context, Id};
use eframe::{egui, App, Frame, NativeOptions};

use egui_dnd::utils::shift_vec;
use egui_dnd::{DragDropItem, DragDropUi};

struct DnDApp {
Expand Down Expand Up @@ -41,7 +40,7 @@ impl App for DnDApp {
let response =
// make sure this is called in a vertical layout.
// Horizontal sorting is not supported yet.
self.dnd.ui(ui, self.items.iter_mut(), |item, ui, handle, pressure| {
self.dnd.ui(ui, self.items.iter_mut(), |item, ui, handle, _pressure| {
ui.horizontal(|ui| {
// Anything in the handle can be used to drag the item
handle.ui(ui, |ui| {
Expand Down
20 changes: 16 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
use crate::state::DragDropResponse;
use egui::Id;
pub use state::{DragDropItem, DragDropUi, Handle};
use std::borrow::BorrowMut;

use std::hash::Hash;
use std::ops::{Deref, DerefMut};
use std::os::unix::raw::time_t;

mod state;

Expand Down Expand Up @@ -59,6 +58,7 @@ pub mod utils {
}
}

/// Helper struct for ease of use.
pub struct Dnd<'a> {
id: Id,
ui: &'a mut egui::Ui,
Expand Down Expand Up @@ -121,10 +121,20 @@ pub fn dnd(ui: &mut egui::Ui, id_source: impl Hash) -> Dnd {
}

impl<'a> Dnd<'a> {
/// Initialize the drag and drop UI. Same as [dnd].
pub fn new(ui: &'a mut egui::Ui, id_source: impl Hash) -> Self {
dnd(ui, id_source)
}

/// Display the drag and drop UI.
/// [items] should be an iterator over items that should be sorted.
///
/// The items won't be sorted automatically, but you can use [Dnd::show_vec] or [DragDropResponse::update_vec] to do so.
/// If your items aren't in a vec, you have to sort them yourself.
///
/// [item_ui] is called for each item. Display your item there.
/// [item_ui] gets a [Handle] that can be used to display the drag handle.
/// Only the handle can be used to drag the item. If you want the whole item to be draggable, put everything in the handle.
pub fn show<T: DragDropItem>(
self,
items: impl Iterator<Item = T>,
Expand All @@ -145,11 +155,11 @@ impl<'a> Dnd<'a> {
response
}

//
/// Same as [Dnd::show], but automatically sorts the items.
pub fn show_vec<T: Hash>(
self,
items: &mut Vec<T>,
mut item_ui: impl FnMut(&mut egui::Ui, &mut T, Handle, ItemState),
item_ui: impl FnMut(&mut egui::Ui, &mut T, Handle, ItemState),
) -> DragDropResponse {
let i = &mut items[0];

Expand All @@ -161,6 +171,8 @@ impl<'a> Dnd<'a> {
}
}

/// State of the current item.
pub struct ItemState {
/// True if the item is currently being dragged.
pub dragged: bool,
}
27 changes: 8 additions & 19 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ impl DragDropResponse {
/// Holds the data needed to draw the floating item while it is being dragged
/// Deprecated: Use [crate::dnd] or [crate::Dnd::new] instead
#[derive(Clone, Debug)]
#[deprecated]
pub struct DragDropUi {
detection_state: DragDetectionState,
drag_count: usize,
Expand Down Expand Up @@ -164,7 +163,6 @@ enum DragPhase {
hovering_above_item: Option<Id>,
/// This will be set if we are at the bottom of the list
hovering_below_item: Option<Id>,
source_item: Id,

// These should only be used during for output, as to not cause issues when item indexes change
hovering_idx: usize,
Expand Down Expand Up @@ -247,16 +245,6 @@ impl DragDetectionState {
_ => None,
}
}

fn is_dragging_and_past_first_frame(&self) -> bool {
matches!(
self,
DragDetectionState::Dragging {
phase: DragPhase::Rest { .. },
..
}
)
}
}

impl<'a> Handle<'a> {
Expand All @@ -267,6 +255,8 @@ impl<'a> Handle<'a> {
self
}

/// Draw the drag handle. Use [Handle::sense] to add a click sense.
/// You can also add buttons in the handle, but they won't be interactive if you pass Sense::click
pub fn ui(self, ui: &mut Ui, contents: impl FnOnce(&mut Ui)) -> egui::Response {
let u = ui.scope(contents);

Expand Down Expand Up @@ -350,7 +340,7 @@ impl Default for DragDropConfig {

impl DragDropConfig {
/// Optimized for mouse usage
fn mouse() -> Self {
pub fn mouse() -> Self {
Self {
click_tolerance: 1.0,
drag_delay: Duration::from_millis(0),
Expand All @@ -360,7 +350,7 @@ impl DragDropConfig {

/// Optimized for touch usage in a fixed size area (no scrolling)
/// Has a higher click tolerance than [DragDropConfig::mouse]
fn touch() -> Self {
pub fn touch() -> Self {
Self {
scroll_tolerance: None,
click_tolerance: 3.0,
Expand All @@ -369,7 +359,7 @@ impl DragDropConfig {
}

/// Optimized for touch usage in a scrollable area
fn touch_scroll() -> Self {
pub fn touch_scroll() -> Self {
Self {
scroll_tolerance: Some(6.0),
click_tolerance: 3.0,
Expand Down Expand Up @@ -527,7 +517,7 @@ impl DragDropUi {
let mut animation_budget = 1.0;

DragDropUi::drop_target(ui, true, |ui| {
values.enumerate().for_each(|(idx, mut item)| {
values.enumerate().for_each(|(idx, item)| {
let item_id = item.id();
let is_dragged_item = self.detection_state.is_dragging_item(item_id);

Expand All @@ -552,7 +542,7 @@ impl DragDropUi {

let mut x = ui.ctx().animate_bool(animation_id, add_space);

let space = (dragged_item_rect.height() + item_spacing);
let space = dragged_item_rect.height() + item_spacing;
if x > 0.0 {
x = x.min(animation_budget);
ui.allocate_space(Vec2::new(0.0, space * x));
Expand Down Expand Up @@ -600,7 +590,7 @@ impl DragDropUi {
);
x = x.min(animation_budget);
if x > 0.0 {
let space = (dragged_item_rect.height() + item_spacing);
let space = dragged_item_rect.height() + item_spacing;
ui.allocate_exact_size(Vec2::new(0.0, space * x), Sense::hover());
}
});
Expand All @@ -624,7 +614,6 @@ impl DragDropUi {
dragged_item_size,
hovering_above_item: hovering_item_id,
hovering_below_item: below_item.map(|i| i.1),
source_item: source_idx.1,
hovering_idx: hovering_item
.map(|i| i.0)
.or(below_item.map(|i| i.0 + 1))
Expand Down

0 comments on commit 450ddb5

Please sign in to comment.