Skip to content

Commit

Permalink
Merge pull request #770 from hecrj/feature/clipboard-write
Browse files Browse the repository at this point in the history
Write clipboard support and `TextInput` copy and cut behavior
  • Loading branch information
hecrj authored Mar 10, 2021
2 parents 939fcfe + 17dcfa8 commit 7eb5127
Show file tree
Hide file tree
Showing 32 changed files with 227 additions and 129 deletions.
5 changes: 3 additions & 2 deletions examples/integration/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use controls::Controls;
use scene::Scene;

use iced_wgpu::{wgpu, Backend, Renderer, Settings, Viewport};
use iced_winit::{conversion, futures, program, winit, Debug, Size};
use iced_winit::{conversion, futures, program, winit, Clipboard, Debug, Size};

use futures::task::SpawnExt;
use winit::{
Expand All @@ -28,6 +28,7 @@ pub fn main() {
);
let mut cursor_position = PhysicalPosition::new(-1.0, -1.0);
let mut modifiers = ModifiersState::default();
let mut clipboard = Clipboard::connect(&window);

// Initialize wgpu
let instance = wgpu::Instance::new(wgpu::BackendBit::PRIMARY);
Expand Down Expand Up @@ -141,8 +142,8 @@ pub fn main() {
cursor_position,
viewport.scale_factor(),
),
None,
&mut renderer,
&mut clipboard,
&mut debug,
);

Expand Down
4 changes: 2 additions & 2 deletions glutin/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ async fn run_instance<A, E, C>(
use glutin::event;
use iced_winit::futures::stream::StreamExt;

let clipboard = Clipboard::new(context.window());
let mut clipboard = Clipboard::connect(context.window());

let mut state = application::State::new(&application, context.window());
let mut viewport_version = state.viewport_version();
Expand Down Expand Up @@ -170,8 +170,8 @@ async fn run_instance<A, E, C>(
let statuses = user_interface.update(
&events,
state.cursor_position(),
clipboard.as_ref().map(|c| c as _),
&mut renderer,
&mut clipboard,
&mut messages,
);

Expand Down
4 changes: 2 additions & 2 deletions graphics/src/widget/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ where
event: iced_native::Event,
layout: Layout<'_>,
cursor_position: Point,
messages: &mut Vec<Message>,
_renderer: &Renderer<B>,
_clipboard: Option<&dyn Clipboard>,
_clipboard: &mut dyn Clipboard,
messages: &mut Vec<Message>,
) -> event::Status {
let bounds = layout.bounds();

Expand Down
21 changes: 19 additions & 2 deletions native/src/clipboard.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
//! Access the clipboard.
/// A buffer for short-term storage and transfer within and between
/// applications.
pub trait Clipboard {
/// Returns the current content of the [`Clipboard`] as text.
fn content(&self) -> Option<String>;
/// Reads the current content of the [`Clipboard`] as text.
fn read(&self) -> Option<String>;

/// Writes the given text contents to the [`Clipboard`].
fn write(&mut self, contents: String);
}

/// A null implementation of the [`Clipboard`] trait.
#[derive(Debug, Clone, Copy)]
pub struct Null;

impl Clipboard for Null {
fn read(&self) -> Option<String> {
None
}

fn write(&mut self, _contents: String) {}
}
18 changes: 9 additions & 9 deletions native/src/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,17 +223,17 @@ where
event: Event,
layout: Layout<'_>,
cursor_position: Point,
messages: &mut Vec<Message>,
renderer: &Renderer,
clipboard: Option<&dyn Clipboard>,
clipboard: &mut dyn Clipboard,
messages: &mut Vec<Message>,
) -> event::Status {
self.widget.on_event(
event,
layout,
cursor_position,
messages,
renderer,
clipboard,
messages,
)
}

Expand Down Expand Up @@ -311,19 +311,19 @@ where
event: Event,
layout: Layout<'_>,
cursor_position: Point,
messages: &mut Vec<B>,
renderer: &Renderer,
clipboard: Option<&dyn Clipboard>,
clipboard: &mut dyn Clipboard,
messages: &mut Vec<B>,
) -> event::Status {
let mut original_messages = Vec::new();

let status = self.widget.on_event(
event,
layout,
cursor_position,
&mut original_messages,
renderer,
clipboard,
&mut original_messages,
);

original_messages
Expand Down Expand Up @@ -401,17 +401,17 @@ where
event: Event,
layout: Layout<'_>,
cursor_position: Point,
messages: &mut Vec<Message>,
renderer: &Renderer,
clipboard: Option<&dyn Clipboard>,
clipboard: &mut dyn Clipboard,
messages: &mut Vec<Message>,
) -> event::Status {
self.element.widget.on_event(
event,
layout,
cursor_position,
messages,
renderer,
clipboard,
messages,
)
}

Expand Down
2 changes: 1 addition & 1 deletion native/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#![deny(unused_results)]
#![forbid(unsafe_code)]
#![forbid(rust_2018_idioms)]
pub mod clipboard;
pub mod event;
pub mod keyboard;
pub mod layout;
Expand All @@ -45,7 +46,6 @@ pub mod touch;
pub mod widget;
pub mod window;

mod clipboard;
mod element;
mod hasher;
mod runtime;
Expand Down
4 changes: 2 additions & 2 deletions native/src/overlay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ where
_event: Event,
_layout: Layout<'_>,
_cursor_position: Point,
_messages: &mut Vec<Message>,
_renderer: &Renderer,
_clipboard: Option<&dyn Clipboard>,
_clipboard: &mut dyn Clipboard,
_messages: &mut Vec<Message>,
) -> event::Status {
event::Status::Ignored
}
Expand Down
12 changes: 6 additions & 6 deletions native/src/overlay/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@ where
event: Event,
layout: Layout<'_>,
cursor_position: Point,
messages: &mut Vec<Message>,
renderer: &Renderer,
clipboard: Option<&dyn Clipboard>,
clipboard: &mut dyn Clipboard,
messages: &mut Vec<Message>,
) -> event::Status {
self.overlay.on_event(
event,
layout,
cursor_position,
messages,
renderer,
clipboard,
messages,
)
}

Expand Down Expand Up @@ -117,19 +117,19 @@ where
event: Event,
layout: Layout<'_>,
cursor_position: Point,
messages: &mut Vec<B>,
renderer: &Renderer,
clipboard: Option<&dyn Clipboard>,
clipboard: &mut dyn Clipboard,
messages: &mut Vec<B>,
) -> event::Status {
let mut original_messages = Vec::new();

let event_status = self.content.on_event(
event,
layout,
cursor_position,
&mut original_messages,
renderer,
clipboard,
&mut original_messages,
);

original_messages
Expand Down
10 changes: 5 additions & 5 deletions native/src/overlay/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,17 +219,17 @@ where
event: Event,
layout: Layout<'_>,
cursor_position: Point,
messages: &mut Vec<Message>,
renderer: &Renderer,
clipboard: Option<&dyn Clipboard>,
clipboard: &mut dyn Clipboard,
messages: &mut Vec<Message>,
) -> event::Status {
self.container.on_event(
event.clone(),
layout,
cursor_position,
messages,
renderer,
clipboard,
messages,
)
}

Expand Down Expand Up @@ -320,9 +320,9 @@ where
event: Event,
layout: Layout<'_>,
cursor_position: Point,
_messages: &mut Vec<Message>,
renderer: &Renderer,
_clipboard: Option<&dyn Clipboard>,
_clipboard: &mut dyn Clipboard,
_messages: &mut Vec<Message>,
) -> event::Status {
match event {
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) => {
Expand Down
4 changes: 2 additions & 2 deletions native/src/program/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ where
&mut self,
bounds: Size,
cursor_position: Point,
clipboard: Option<&dyn Clipboard>,
renderer: &mut P::Renderer,
clipboard: &mut dyn Clipboard,
debug: &mut Debug,
) -> Option<Command<P::Message>> {
let mut user_interface = build_user_interface(
Expand All @@ -109,8 +109,8 @@ where
let _ = user_interface.update(
&self.queued_events,
cursor_position,
clipboard,
renderer,
clipboard,
&mut messages,
);

Expand Down
16 changes: 9 additions & 7 deletions native/src/user_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ where
/// completing [the previous example](#example):
///
/// ```no_run
/// use iced_native::{UserInterface, Cache, Size, Point};
/// use iced_native::{clipboard, UserInterface, Cache, Size, Point};
/// use iced_wgpu::Renderer;
///
/// # mod iced_wgpu {
Expand All @@ -157,6 +157,7 @@ where
/// let mut renderer = Renderer::new();
/// let mut window_size = Size::new(1024.0, 768.0);
/// let mut cursor_position = Point::default();
/// let mut clipboard = clipboard::Null;
///
/// // Initialize our event storage
/// let mut events = Vec::new();
Expand All @@ -176,8 +177,8 @@ where
/// let event_statuses = user_interface.update(
/// &events,
/// cursor_position,
/// None,
/// &renderer,
/// &mut clipboard,
/// &mut messages
/// );
///
Expand All @@ -193,8 +194,8 @@ where
&mut self,
events: &[Event],
cursor_position: Point,
clipboard: Option<&dyn Clipboard>,
renderer: &Renderer,
clipboard: &mut dyn Clipboard,
messages: &mut Vec<Message>,
) -> Vec<event::Status> {
let (base_cursor, overlay_statuses) = if let Some(mut overlay) =
Expand All @@ -215,9 +216,9 @@ where
event,
Layout::new(&layer.layout),
cursor_position,
messages,
renderer,
clipboard,
messages,
)
})
.collect();
Expand Down Expand Up @@ -246,9 +247,9 @@ where
event,
Layout::new(&self.base.layout),
base_cursor,
messages,
renderer,
clipboard,
messages,
);

event_status.merge(overlay_status)
Expand All @@ -269,7 +270,7 @@ where
/// [completing the last example](#example-1):
///
/// ```no_run
/// use iced_native::{UserInterface, Cache, Size, Point};
/// use iced_native::{clipboard, UserInterface, Cache, Size, Point};
/// use iced_wgpu::Renderer;
///
/// # mod iced_wgpu {
Expand All @@ -292,6 +293,7 @@ where
/// let mut renderer = Renderer::new();
/// let mut window_size = Size::new(1024.0, 768.0);
/// let mut cursor_position = Point::default();
/// let mut clipboard = clipboard::Null;
/// let mut events = Vec::new();
/// let mut messages = Vec::new();
///
Expand All @@ -309,8 +311,8 @@ where
/// let event_statuses = user_interface.update(
/// &events,
/// cursor_position,
/// None,
/// &renderer,
/// &mut clipboard,
/// &mut messages
/// );
///
Expand Down
4 changes: 2 additions & 2 deletions native/src/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,9 @@ where
_event: Event,
_layout: Layout<'_>,
_cursor_position: Point,
_messages: &mut Vec<Message>,
_renderer: &Renderer,
_clipboard: Option<&dyn Clipboard>,
_clipboard: &mut dyn Clipboard,
_messages: &mut Vec<Message>,
) -> event::Status {
event::Status::Ignored
}
Expand Down
6 changes: 3 additions & 3 deletions native/src/widget/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,17 +160,17 @@ where
event: Event,
layout: Layout<'_>,
cursor_position: Point,
messages: &mut Vec<Message>,
renderer: &Renderer,
clipboard: Option<&dyn Clipboard>,
clipboard: &mut dyn Clipboard,
messages: &mut Vec<Message>,
) -> event::Status {
if let event::Status::Captured = self.content.on_event(
event.clone(),
layout.children().next().unwrap(),
cursor_position,
messages,
renderer,
clipboard,
messages,
) {
return event::Status::Captured;
}
Expand Down
Loading

0 comments on commit 7eb5127

Please sign in to comment.