Skip to main content

cosmic/
command.rs

1// Copyright 2023 System76 <info@system76.com>
2// SPDX-License-Identifier: MPL-2.0
3
4#[cfg(xdg_portal)]
5use ashpd::documents::file_transfer::{
6    AddFilesOptions, FileTransfer, RetrieveFilesOptions, StartTransferOptions,
7};
8
9use iced::window;
10
11/// Initiates a window drag.
12pub fn drag<M>(id: window::Id) -> iced::Task<crate::Action<M>> {
13    iced_runtime::window::drag(id)
14}
15
16/// Maximizes the window.
17pub fn maximize<M>(id: window::Id, maximized: bool) -> iced::Task<crate::Action<M>> {
18    iced_runtime::window::maximize(id, maximized)
19}
20
21/// Minimizes the window.
22pub fn minimize<M>(id: window::Id) -> iced::Task<crate::Action<M>> {
23    iced_runtime::window::minimize(id, true)
24}
25
26/// Sets the title of a window.
27#[allow(unused_variables, clippy::needless_pass_by_value)]
28pub fn set_title<M>(id: window::Id, title: String) -> iced::Task<crate::Action<M>> {
29    iced::Task::none()
30}
31
32#[cfg(feature = "winit")]
33pub fn set_scaling_factor<M: Send + 'static>(factor: f32) -> iced::Task<crate::Action<M>> {
34    iced::Task::done(crate::app::Action::ScaleFactor(factor)).map(crate::Action::Cosmic)
35}
36
37#[cfg(feature = "winit")]
38pub fn set_theme<M: Send + 'static>(theme: crate::Theme) -> iced::Task<crate::Action<M>> {
39    iced::Task::done(crate::app::Action::AppThemeChange(theme)).map(crate::Action::Cosmic)
40}
41
42/// Sets the window mode to windowed.
43pub fn set_windowed<M>(id: window::Id) -> iced::Task<crate::Action<M>> {
44    iced_runtime::window::set_mode(id, window::Mode::Windowed)
45}
46
47/// Toggles the windows' maximize state.
48pub fn toggle_maximize<M>(id: window::Id) -> iced::Task<crate::Action<M>> {
49    iced_runtime::window::toggle_maximize(id)
50}
51
52#[cfg(xdg_portal)]
53pub fn file_transfer_send(
54    writeable: bool,
55    auto_stop: bool,
56    files: Vec<impl std::os::fd::AsFd + Send + Sync + 'static>,
57) -> iced::Task<ashpd::Result<String>> {
58    iced::Task::future(async move {
59        let transfer_options = StartTransferOptions::default()
60            .set_writeable(writeable)
61            .set_auto_stop(auto_stop);
62        let file_transfer = FileTransfer::new().await?;
63        let key = file_transfer.start_transfer(transfer_options).await?;
64        file_transfer
65            .add_files(&key, &files, AddFilesOptions::default())
66            .await?;
67        Ok(key)
68    })
69}
70
71/// Receive the files offered over the xdg share portal using the `key`.
72/// Returns a list of file paths.
73#[cfg(xdg_portal)]
74pub fn file_transfer_receive(key: String) -> iced::Task<ashpd::Result<Vec<String>>> {
75    iced::Task::future(async move {
76        let file_transfer = FileTransfer::new().await?;
77        file_transfer
78            .retrieve_files(&key, RetrieveFilesOptions::default())
79            .await
80    })
81}