Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task API #2463

Merged
merged 11 commits into from
Jun 16, 2024
2 changes: 1 addition & 1 deletion .github/workflows/document.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
steps:
- uses: hecrj/setup-rust-action@v2
with:
rust-version: nightly-2023-12-11
rust-version: nightly
- uses: actions/checkout@v2
- name: Generate documentation
run: |
Expand Down
3 changes: 0 additions & 3 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,5 @@ web-time.workspace = true
dark-light.workspace = true
dark-light.optional = true

[target.'cfg(windows)'.dependencies]
raw-window-handle.workspace = true

[dev-dependencies]
approx = "0.5"
61 changes: 3 additions & 58 deletions core/src/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::{
Widget,
};

use std::any::Any;
use std::borrow::Borrow;

/// A generic [`Widget`].
Expand Down Expand Up @@ -305,63 +304,9 @@ where
tree: &mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
operation: &mut dyn widget::Operation<B>,
operation: &mut dyn widget::Operation<()>,
) {
struct MapOperation<'a, B> {
operation: &'a mut dyn widget::Operation<B>,
}

impl<'a, T, B> widget::Operation<T> for MapOperation<'a, B> {
fn container(
&mut self,
id: Option<&widget::Id>,
bounds: Rectangle,
operate_on_children: &mut dyn FnMut(
&mut dyn widget::Operation<T>,
),
) {
self.operation.container(id, bounds, &mut |operation| {
operate_on_children(&mut MapOperation { operation });
});
}

fn focusable(
&mut self,
state: &mut dyn widget::operation::Focusable,
id: Option<&widget::Id>,
) {
self.operation.focusable(state, id);
}

fn scrollable(
&mut self,
state: &mut dyn widget::operation::Scrollable,
id: Option<&widget::Id>,
bounds: Rectangle,
translation: Vector,
) {
self.operation.scrollable(state, id, bounds, translation);
}

fn text_input(
&mut self,
state: &mut dyn widget::operation::TextInput,
id: Option<&widget::Id>,
) {
self.operation.text_input(state, id);
}

fn custom(&mut self, state: &mut dyn Any, id: Option<&widget::Id>) {
self.operation.custom(state, id);
}
}

self.widget.operate(
tree,
layout,
renderer,
&mut MapOperation { operation },
);
self.widget.operate(tree, layout, renderer, operation);
}

fn on_event(
Expand Down Expand Up @@ -495,7 +440,7 @@ where
state: &mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
operation: &mut dyn widget::Operation<Message>,
operation: &mut dyn widget::Operation<()>,
) {
self.element
.widget
Expand Down
2 changes: 1 addition & 1 deletion core/src/overlay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ where
&mut self,
_layout: Layout<'_>,
_renderer: &Renderer,
_operation: &mut dyn widget::Operation<Message>,
_operation: &mut dyn widget::Operation<()>,
) {
}

Expand Down
60 changes: 4 additions & 56 deletions core/src/overlay/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ use crate::layout;
use crate::mouse;
use crate::renderer;
use crate::widget;
use crate::{Clipboard, Layout, Point, Rectangle, Shell, Size, Vector};

use std::any::Any;
use crate::{Clipboard, Layout, Point, Rectangle, Shell, Size};

/// A generic [`Overlay`].
#[allow(missing_debug_implementations)]
Expand Down Expand Up @@ -94,7 +92,7 @@ where
&mut self,
layout: Layout<'_>,
renderer: &Renderer,
operation: &mut dyn widget::Operation<Message>,
operation: &mut dyn widget::Operation<()>,
) {
self.overlay.operate(layout, renderer, operation);
}
Expand Down Expand Up @@ -146,59 +144,9 @@ where
&mut self,
layout: Layout<'_>,
renderer: &Renderer,
operation: &mut dyn widget::Operation<B>,
operation: &mut dyn widget::Operation<()>,
) {
struct MapOperation<'a, B> {
operation: &'a mut dyn widget::Operation<B>,
}

impl<'a, T, B> widget::Operation<T> for MapOperation<'a, B> {
fn container(
&mut self,
id: Option<&widget::Id>,
bounds: Rectangle,
operate_on_children: &mut dyn FnMut(
&mut dyn widget::Operation<T>,
),
) {
self.operation.container(id, bounds, &mut |operation| {
operate_on_children(&mut MapOperation { operation });
});
}

fn focusable(
&mut self,
state: &mut dyn widget::operation::Focusable,
id: Option<&widget::Id>,
) {
self.operation.focusable(state, id);
}

fn scrollable(
&mut self,
state: &mut dyn widget::operation::Scrollable,
id: Option<&widget::Id>,
bounds: Rectangle,
translation: Vector,
) {
self.operation.scrollable(state, id, bounds, translation);
}

fn text_input(
&mut self,
state: &mut dyn widget::operation::TextInput,
id: Option<&widget::Id>,
) {
self.operation.text_input(state, id);
}

fn custom(&mut self, state: &mut dyn Any, id: Option<&widget::Id>) {
self.operation.custom(state, id);
}
}

self.content
.operate(layout, renderer, &mut MapOperation { operation });
self.content.operate(layout, renderer, operation);
}

fn on_event(
Expand Down
2 changes: 1 addition & 1 deletion core/src/overlay/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ where
&mut self,
layout: Layout<'_>,
renderer: &Renderer,
operation: &mut dyn widget::Operation<Message>,
operation: &mut dyn widget::Operation<()>,
) {
operation.container(None, layout.bounds(), &mut |operation| {
self.children.iter_mut().zip(layout.children()).for_each(
Expand Down
2 changes: 1 addition & 1 deletion core/src/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ where
_state: &mut Tree,
_layout: Layout<'_>,
_renderer: &Renderer,
_operation: &mut dyn Operation<Message>,
_operation: &mut dyn Operation<()>,
) {
}

Expand Down
10 changes: 5 additions & 5 deletions core/src/widget/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ use crate::{Rectangle, Vector};

use std::any::Any;
use std::fmt;
use std::rc::Rc;
use std::sync::Arc;

/// A piece of logic that can traverse the widget tree of an application in
/// order to query or update some widget state.
pub trait Operation<T> {
pub trait Operation<T>: Send {
/// Operates on a widget that contains other widgets.
///
/// The `operate_on_children` function can be called to return control to
Expand Down Expand Up @@ -81,7 +81,7 @@ where
/// Maps the output of an [`Operation`] using the given function.
pub fn map<A, B>(
operation: Box<dyn Operation<A>>,
f: impl Fn(A) -> B + 'static,
f: impl Fn(A) -> B + Send + Sync + 'static,
) -> impl Operation<B>
where
A: 'static,
Expand All @@ -90,7 +90,7 @@ where
#[allow(missing_debug_implementations)]
struct Map<A, B> {
operation: Box<dyn Operation<A>>,
f: Rc<dyn Fn(A) -> B>,
f: Arc<dyn Fn(A) -> B + Send + Sync>,
}

impl<A, B> Operation<B> for Map<A, B>
Expand Down Expand Up @@ -197,7 +197,7 @@ where

Map {
operation,
f: Rc::new(f),
f: Arc::new(f),
}
}

Expand Down
5 changes: 0 additions & 5 deletions core/src/window/settings/windows.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
//! Platform specific settings for Windows.
use raw_window_handle::RawWindowHandle;

/// The platform specific window settings of an application.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PlatformSpecific {
/// Parent window
pub parent: Option<RawWindowHandle>,

/// Drag and drop support
pub drag_and_drop: bool,

Expand All @@ -17,7 +13,6 @@ pub struct PlatformSpecific {
impl Default for PlatformSpecific {
fn default() -> Self {
Self {
parent: None,
drag_and_drop: true,
skip_taskbar: false,
}
Expand Down
26 changes: 13 additions & 13 deletions examples/editor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use iced::widget::{
button, column, container, horizontal_space, pick_list, row, text,
text_editor, tooltip,
};
use iced::{Alignment, Command, Element, Font, Length, Subscription, Theme};
use iced::{Alignment, Element, Font, Length, Subscription, Task, Theme};

use std::ffi;
use std::io;
Expand Down Expand Up @@ -51,42 +51,42 @@ impl Editor {
}
}

fn load() -> Command<Message> {
Command::perform(
fn load() -> Task<Message> {
Task::perform(
load_file(format!("{}/src/main.rs", env!("CARGO_MANIFEST_DIR"))),
Message::FileOpened,
)
}

fn update(&mut self, message: Message) -> Command<Message> {
fn update(&mut self, message: Message) -> Task<Message> {
match message {
Message::ActionPerformed(action) => {
self.is_dirty = self.is_dirty || action.is_edit();

self.content.perform(action);

Command::none()
Task::none()
}
Message::ThemeSelected(theme) => {
self.theme = theme;

Command::none()
Task::none()
}
Message::NewFile => {
if !self.is_loading {
self.file = None;
self.content = text_editor::Content::new();
}

Command::none()
Task::none()
}
Message::OpenFile => {
if self.is_loading {
Command::none()
Task::none()
} else {
self.is_loading = true;

Command::perform(open_file(), Message::FileOpened)
Task::perform(open_file(), Message::FileOpened)
}
}
Message::FileOpened(result) => {
Expand All @@ -98,15 +98,15 @@ impl Editor {
self.content = text_editor::Content::with_text(&contents);
}

Command::none()
Task::none()
}
Message::SaveFile => {
if self.is_loading {
Command::none()
Task::none()
} else {
self.is_loading = true;

Command::perform(
Task::perform(
save_file(self.file.clone(), self.content.text()),
Message::FileSaved,
)
Expand All @@ -120,7 +120,7 @@ impl Editor {
self.is_dirty = false;
}

Command::none()
Task::none()
}
}
}
Expand Down
Loading
Loading