libcosmic/src/command/mod.rs

59 lines
1.7 KiB
Rust
Raw Normal View History

2023-08-02 11:54:07 +02:00
// Copyright 2023 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0
//! Create asynchronous actions to be performed in the background.
2023-08-02 11:54:07 +02:00
use iced::window;
2024-10-16 20:36:46 -04:00
use iced::Task;
2023-08-02 11:54:07 +02:00
use iced_core::window::Mode;
2024-10-16 20:36:46 -04:00
use iced_runtime::{task, Action};
2023-08-02 11:54:07 +02:00
use std::future::Future;
/// Yields a command which contains a batch of commands.
2024-10-16 20:36:46 -04:00
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)
2023-08-02 11:54:07 +02:00
}
/// Yields a command which will run the future on the runtime executor.
2024-10-16 20:36:46 -04:00
pub fn future<X: Into<Y>, Y: 'static>(future: impl Future<Output = X> + Send + 'static) -> Task<Y> {
Task::future(async move { future.await.into() })
2023-08-02 11:54:07 +02:00
}
/// Yields a command which will return a message.
2024-10-16 20:36:46 -04:00
pub fn message<X: Send + 'static + Into<Y>, Y: 'static>(message: X) -> Task<Y> {
future(async move { message.into() })
2023-08-02 11:54:07 +02:00
}
/// Initiates a window drag.
2024-10-16 20:36:46 -04:00
pub fn drag<M>(id: window::Id) -> Task<M> {
iced_runtime::window::drag(id)
2023-08-02 11:54:07 +02:00
}
/// Maximizes the window.
2024-10-16 20:36:46 -04:00
pub fn maximize<M>(id: window::Id, maximized: bool) -> Task<M> {
iced_runtime::window::maximize(id, maximized)
2023-08-02 11:54:07 +02:00
}
/// Minimizes the window.
2024-10-16 20:36:46 -04:00
pub fn minimize<M>(id: window::Id) -> Task<M> {
iced_runtime::window::minimize(id, true)
2023-08-02 11:54:07 +02:00
}
/// Sets the title of a window.
#[allow(unused_variables, clippy::needless_pass_by_value)]
2024-10-16 20:36:46 -04:00
pub fn set_title<M>(id: window::Id, title: String) -> Task<M> {
Task::none()
2023-08-02 11:54:07 +02:00
}
/// Sets the window mode to windowed.
2024-10-16 20:36:46 -04:00
pub fn set_windowed<M>(id: window::Id) -> Task<M> {
iced_runtime::window::change_mode(id, Mode::Windowed)
2023-08-02 11:54:07 +02:00
}
/// Toggles the windows' maximize state.
2024-10-16 20:36:46 -04:00
pub fn toggle_maximize<M>(id: window::Id) -> Task<M> {
iced_runtime::window::toggle_maximize(id)
2023-08-02 11:54:07 +02:00
}