libcosmic/src/command/mod.rs

118 lines
4 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;
use iced::Command;
use iced_core::window::Mode;
#[cfg(feature = "wayland")]
use iced_runtime::command::platform_specific::wayland::window::Action as WindowAction;
#[cfg(feature = "wayland")]
use iced_runtime::command::platform_specific::wayland::Action as WaylandAction;
#[cfg(feature = "wayland")]
use iced_runtime::command::platform_specific::Action as PlatformAction;
use iced_runtime::command::Action;
use std::future::Future;
/// Yields a command which contains a batch of commands.
pub fn batch<X: 'static + Into<Y>, Y: 'static>(
commands: impl IntoIterator<Item = Command<X>>,
) -> Command<Y> {
Command::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.
pub fn future<X: Into<Y>, Y>(future: impl Future<Output = X> + Send + 'static) -> Command<Y> {
Command::single(Action::Future(Box::pin(async move { future.await.into() })))
2023-08-02 11:54:07 +02:00
}
/// Yields a command which will return a message.
pub fn message<X: Send + 'static + Into<Y>, Y>(message: X) -> Command<Y> {
future(async move { message.into() })
2023-08-02 11:54:07 +02:00
}
/// Initiates a window drag.
#[cfg(feature = "wayland")]
2023-12-07 15:27:52 -05:00
pub fn drag<M>(id: Option<window::Id>) -> Command<M> {
iced_sctk::commands::window::start_drag_window(id.unwrap_or(window::Id::MAIN))
2023-08-02 11:54:07 +02:00
}
/// Initiates a window drag.
#[cfg(not(feature = "wayland"))]
2023-12-07 15:27:52 -05:00
pub fn drag<M>(id: Option<window::Id>) -> Command<M> {
iced_runtime::window::drag(id.unwrap_or(window::Id::MAIN))
2023-08-02 11:54:07 +02:00
}
/// Maximizes the window.
2023-08-02 11:54:07 +02:00
#[cfg(feature = "wayland")]
pub fn maximize<M>(id: Option<window::Id>, maximized: bool) -> Command<M> {
iced_sctk::commands::window::maximize(id.unwrap_or(window::Id::MAIN), maximized)
2023-08-02 11:54:07 +02:00
}
/// Maximizes the window.
2023-08-02 11:54:07 +02:00
#[cfg(not(feature = "wayland"))]
pub fn maximize<M>(id: Option<window::Id>, maximized: bool) -> Command<M> {
iced_runtime::window::maximize(id.unwrap_or(window::Id::MAIN), maximized)
2023-08-02 11:54:07 +02:00
}
/// Minimizes the window.
#[cfg(feature = "wayland")]
2023-12-07 15:27:52 -05:00
pub fn minimize<M>(id: Option<window::Id>) -> Command<M> {
iced_sctk::commands::window::set_mode_window(id.unwrap_or(window::Id::MAIN), Mode::Hidden)
2023-08-02 11:54:07 +02:00
}
/// Minimizes the window.
#[cfg(not(feature = "wayland"))]
2023-12-07 15:27:52 -05:00
pub fn minimize<M>(id: Option<window::Id>) -> Command<M> {
iced_runtime::window::minimize(id.unwrap_or(window::Id::MAIN), true)
2023-08-02 11:54:07 +02:00
}
/// Sets the title of a window.
#[cfg(feature = "wayland")]
2023-12-07 15:27:52 -05:00
pub fn set_title<M>(id: Option<window::Id>, title: String) -> Command<M> {
2023-08-02 11:54:07 +02:00
window_action(WindowAction::Title {
2023-12-07 15:27:52 -05:00
id: id.unwrap_or(window::Id::MAIN),
2023-08-02 11:54:07 +02:00
title,
})
}
/// Sets the title of a window.
#[cfg(not(feature = "wayland"))]
#[allow(unused_variables, clippy::needless_pass_by_value)]
2023-12-07 15:27:52 -05:00
pub fn set_title<M>(id: Option<window::Id>, title: String) -> Command<M> {
2023-08-02 11:54:07 +02:00
Command::none()
}
/// Sets the window mode to windowed.
#[cfg(feature = "wayland")]
2023-12-07 15:27:52 -05:00
pub fn set_windowed<M>(id: Option<window::Id>) -> Command<M> {
iced_sctk::commands::window::set_mode_window(id.unwrap_or(window::Id::MAIN), Mode::Windowed)
2023-08-02 11:54:07 +02:00
}
/// Sets the window mode to windowed.
#[cfg(not(feature = "wayland"))]
2023-12-07 15:27:52 -05:00
pub fn set_windowed<M>(id: Option<window::Id>) -> Command<M> {
iced_runtime::window::change_mode(id.unwrap_or(window::Id::MAIN), Mode::Windowed)
2023-08-02 11:54:07 +02:00
}
/// Toggles the windows' maximize state.
2023-08-02 11:54:07 +02:00
#[cfg(feature = "wayland")]
pub fn toggle_maximize<M>(id: Option<window::Id>) -> Command<M> {
iced_sctk::commands::window::toggle_maximize(id.unwrap_or(window::Id::MAIN))
2023-08-02 11:54:07 +02:00
}
/// Toggles the windows' maximize state.
2023-08-02 11:54:07 +02:00
#[cfg(not(feature = "wayland"))]
pub fn toggle_maximize<M>(id: Option<window::Id>) -> Command<M> {
2023-12-07 15:27:52 -05:00
iced_runtime::window::toggle_maximize(id.unwrap_or(window::Id::MAIN))
2023-08-02 11:54:07 +02:00
}
/// Creates a command to apply an action to a window.
#[cfg(feature = "wayland")]
pub fn window_action<M>(action: WindowAction<M>) -> Command<M> {
Command::single(Action::PlatformSpecific(PlatformAction::Wayland(
WaylandAction::Window(action),
)))
}