1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright 2023 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0

//! Create asynchronous actions to be performed in the background.

use iced::window;
use iced::Task;
use iced_core::window::Mode;
use iced_runtime::{task, Action};
use std::future::Future;

/// Yields a command which contains a batch of commands.
pub fn batch<X: Send + 'static + Into<Y>, Y: Send + 'static>(
    commands: impl IntoIterator<Item = Task<X>>,
) -> Task<Y> {
    Task::batch(commands).map(Into::into)
}

/// Yields a command which will run the future on the runtime executor.
pub fn future<X: Into<Y>, Y: 'static>(future: impl Future<Output = X> + Send + 'static) -> Task<Y> {
    Task::future(async move { future.await.into() })
}

/// Yields a command which will return a message.
pub fn message<X: Send + 'static + Into<Y>, Y: 'static>(message: X) -> Task<Y> {
    future(async move { message.into() })
}

/// Initiates a window drag.
pub fn drag<M>(id: window::Id) -> Task<M> {
    iced_runtime::window::drag(id)
}

/// Maximizes the window.
pub fn maximize<M>(id: window::Id, maximized: bool) -> Task<M> {
    iced_runtime::window::maximize(id, maximized)
}

/// Minimizes the window.
pub fn minimize<M>(id: window::Id) -> Task<M> {
    iced_runtime::window::minimize(id, true)
}

/// Sets the title of a window.
#[allow(unused_variables, clippy::needless_pass_by_value)]
pub fn set_title<M>(id: window::Id, title: String) -> Task<M> {
    Task::none()
}

/// Sets the window mode to windowed.
pub fn set_windowed<M>(id: window::Id) -> Task<M> {
    iced_runtime::window::change_mode(id, Mode::Windowed)
}

/// Toggles the windows' maximize state.
pub fn toggle_maximize<M>(id: window::Id) -> Task<M> {
    iced_runtime::window::toggle_maximize(id)
}