data_device: move default_action_chooser into smithay

This commit is contained in:
Victor Berger 2018-11-22 15:37:31 +01:00
parent cc80233318
commit ed00fb1d47
5 changed files with 47 additions and 24 deletions

View File

@ -16,7 +16,6 @@ use smithay::wayland_server::{calloop::EventLoop, Display};
mod shaders;
mod glium_drawer;
mod input_handler;
mod misc;
#[cfg(feature = "tty_launch")]
mod raw_drm;
mod shell;

View File

@ -1,19 +0,0 @@
use smithay::wayland_server::protocol::wl_data_device_manager::DndAction;
pub fn dnd_action_chooser(available: DndAction, preferred: DndAction) -> DndAction {
// if the preferred action is valid (a single action) and in the available actions, use it
// otherwise, follow a fallback stategy
if [DndAction::Move, DndAction::Copy, DndAction::Ask].contains(&preferred)
&& available.contains(preferred)
{
preferred
} else if available.contains(DndAction::Ask) {
DndAction::Ask
} else if available.contains(DndAction::Copy) {
DndAction::Copy
} else if available.contains(DndAction::Move) {
DndAction::Move
} else {
DndAction::empty()
}
}

View File

@ -42,7 +42,7 @@ use smithay::{
input::Libinput,
wayland::{
compositor::CompositorToken,
data_device::{init_data_device, set_data_device_focus},
data_device::{default_action_chooser, init_data_device, set_data_device_focus},
output::{Mode, Output, PhysicalProperties},
seat::{Seat, XkbConfig},
shm::init_shm_global,
@ -111,7 +111,7 @@ pub fn run_udev(mut display: Display, mut event_loop: EventLoop<()>, log: Logger
init_data_device(
&mut display.borrow_mut(),
|_| {},
::misc::dnd_action_chooser,
default_action_chooser,
log.clone(),
);

View File

@ -11,7 +11,7 @@ use smithay::{
winit,
},
wayland::{
data_device::{init_data_device, set_data_device_focus},
data_device::{default_action_chooser, init_data_device, set_data_device_focus},
output::{Mode, Output, PhysicalProperties},
seat::{Seat, XkbConfig},
shm::init_shm_global,
@ -54,7 +54,7 @@ pub fn run_winit(display: &mut Display, event_loop: &mut EventLoop<()>, log: Log
let (compositor_token, _, _, window_map) = init_shell(display, log.clone());
init_data_device(display, |_| {}, ::misc::dnd_action_chooser, log.clone());
init_data_device(display, |_| {}, default_action_chooser, log.clone());
let (mut seat, _) = Seat::new(display, "winit".into(), log.clone());

View File

@ -23,6 +23,27 @@
//! for your clients
//! - the freestanding function `start_dnd` allows you to initiate a drag'n'drop event from the compositor
//! itself and receive interactions of clients with it via an other dedicated callback.
//!
//! ## Initialization
//!
//! ```
//! # extern crate wayland_server;
//! # #[macro_use] extern crate smithay;
//! use smithay::wayland::data_device::{init_data_device, default_action_chooser};
//!
//! # fn main(){
//! # let mut event_loop = wayland_server::calloop::EventLoop::<()>::new().unwrap();
//! # let mut display = wayland_server::Display::new(event_loop.handle());
//! // init the data device:
//! init_data_device(
//! &mut display, // the display
//! |dnd_event| { /* a callback to react to client DnD/selection actions */ },
//! default_action_chooser, // a closure to choose the DnD action depending on clients
//! // negociation
//! None // insert a logger here
//! );
//! # }
//! ```
use std::os::unix::io::RawFd;
use std::sync::{Arc, Mutex};
@ -423,3 +444,25 @@ where
dd_data,
)
}
/// A simple action chooser for DnD negociation
///
/// If the preferred action is available, it'll pick it. Otherwise, it'll pick the first
/// available in the following order: Ask, Copy, Move.
pub fn default_action_chooser(available: DndAction, preferred: DndAction) -> DndAction {
// if the preferred action is valid (a single action) and in the available actions, use it
// otherwise, follow a fallback stategy
if [DndAction::Move, DndAction::Copy, DndAction::Ask].contains(&preferred)
&& available.contains(preferred)
{
preferred
} else if available.contains(DndAction::Ask) {
DndAction::Ask
} else if available.contains(DndAction::Copy) {
DndAction::Copy
} else if available.contains(DndAction::Move) {
DndAction::Move
} else {
DndAction::empty()
}
}