Skip to content

Commit c085add

Browse files
feat: set application progress bar, close #7999 (#8009)
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
1 parent a6ad540 commit c085add

File tree

14 files changed

+432
-212
lines changed

14 files changed

+432
-212
lines changed

.changes/add-progress-bar.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"tauri": 'patch:feat'
3+
"tauri-runtime": 'patch:feat'
4+
"tauri-runtime-wry": 'patch:feat'
5+
"tauri-utils": 'patch:feat'
6+
---
7+
8+
Added `set_progress_bar` to `Window`.

.changes/set-progress-bar-api.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@tauri-apps/api": patch:feat
3+
---
4+
5+
Added the `setProgressBar` API on the `Window` class.

core/tauri-runtime-wry/src/lib.rs

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ use wry::webview::WebViewBuilderExtWindows;
4141

4242
#[cfg(target_os = "macos")]
4343
use tauri_utils::TitleBarStyle;
44-
use tauri_utils::{config::WindowConfig, debug_eprintln, Theme};
44+
use tauri_utils::{
45+
config::WindowConfig, debug_eprintln, ProgressBarState, ProgressBarStatus, Theme,
46+
};
4547
use wry::{
4648
application::{
4749
dpi::{
@@ -56,8 +58,9 @@ use wry::{
5658
},
5759
monitor::MonitorHandle,
5860
window::{
59-
CursorIcon as WryCursorIcon, Fullscreen, Icon as WryWindowIcon, Theme as WryTheme,
60-
UserAttentionType as WryUserAttentionType,
61+
CursorIcon as WryCursorIcon, Fullscreen, Icon as WryWindowIcon,
62+
ProgressBarState as WryProgressBarState, ProgressState as WryProgressState,
63+
Theme as WryTheme, UserAttentionType as WryUserAttentionType,
6164
},
6265
},
6366
webview::{FileDropEvent as WryFileDropEvent, Url, WebContext, WebView, WebViewBuilder},
@@ -520,6 +523,35 @@ impl From<CursorIcon> for CursorIconWrapper {
520523
}
521524
}
522525

526+
pub struct ProgressStateWrapper(pub WryProgressState);
527+
528+
impl From<ProgressBarStatus> for ProgressStateWrapper {
529+
fn from(status: ProgressBarStatus) -> Self {
530+
let state = match status {
531+
ProgressBarStatus::None => WryProgressState::None,
532+
ProgressBarStatus::Normal => WryProgressState::Normal,
533+
ProgressBarStatus::Indeterminate => WryProgressState::Indeterminate,
534+
ProgressBarStatus::Paused => WryProgressState::Paused,
535+
ProgressBarStatus::Error => WryProgressState::Error,
536+
};
537+
Self(state)
538+
}
539+
}
540+
541+
pub struct ProgressBarStateWrapper(pub WryProgressBarState);
542+
543+
impl From<ProgressBarState> for ProgressBarStateWrapper {
544+
fn from(progress_state: ProgressBarState) -> Self {
545+
Self(WryProgressBarState {
546+
progress: progress_state.progress,
547+
state: progress_state
548+
.status
549+
.map(|state| ProgressStateWrapper::from(state).0),
550+
unity_uri: progress_state.unity_uri,
551+
})
552+
}
553+
}
554+
523555
#[derive(Clone, Default)]
524556
pub struct WindowBuilderWrapper {
525557
inner: WryWindowBuilder,
@@ -1006,6 +1038,7 @@ pub enum WindowMessage {
10061038
SetCursorIcon(CursorIcon),
10071039
SetCursorPosition(Position),
10081040
SetIgnoreCursorEvents(bool),
1041+
SetProgressBar(ProgressBarState),
10091042
DragWindow,
10101043
RequestRedraw,
10111044
}
@@ -1520,6 +1553,16 @@ impl<T: UserEvent> Dispatch<T> for WryDispatcher<T> {
15201553
),
15211554
)
15221555
}
1556+
1557+
fn set_progress_bar(&self, progress_state: ProgressBarState) -> Result<()> {
1558+
send_user_message(
1559+
&self.context,
1560+
Message::Window(
1561+
self.window_id,
1562+
WindowMessage::SetProgressBar(progress_state),
1563+
),
1564+
)
1565+
}
15231566
}
15241567

15251568
#[derive(Clone)]
@@ -2302,6 +2345,9 @@ fn handle_user_message<T: UserEvent>(
23022345
WindowMessage::RequestRedraw => {
23032346
window.request_redraw();
23042347
}
2348+
WindowMessage::SetProgressBar(progress_state) => {
2349+
window.set_progress_bar(ProgressBarStateWrapper::from(progress_state).0);
2350+
}
23052351
}
23062352
}
23072353
}

core/tauri-runtime/src/lib.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use raw_window_handle::RawDisplayHandle;
1616
use serde::Deserialize;
1717
use std::{fmt::Debug, sync::mpsc::Sender};
18-
use tauri_utils::Theme;
18+
use tauri_utils::{ProgressBarState, Theme};
1919
use url::Url;
2020

2121
/// Types useful for interacting with a user's monitors.
@@ -589,4 +589,12 @@ pub trait Dispatch<T: UserEvent>: Debug + Clone + Send + Sync + Sized + 'static
589589

590590
/// Executes javascript on the window this [`Dispatch`] represents.
591591
fn eval_script<S: Into<String>>(&self, script: S) -> Result<()>;
592+
593+
/// Sets the taskbar progress state.
594+
///
595+
/// ## Platform-specific
596+
///
597+
/// - **Linux / macOS**: Progress bar is app-wide and not specific to this window. Only supported desktop environments with `libunity` (e.g. GNOME).
598+
/// - **iOS / Android:** Unsupported.
599+
fn set_progress_bar(&self, progress_state: ProgressBarState) -> Result<()>;
592600
}

core/tauri-utils/src/lib.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,3 +416,31 @@ pub fn display_path<P: AsRef<Path>>(p: P) -> String {
416416
.display()
417417
.to_string()
418418
}
419+
420+
/// Progress bar status.
421+
#[derive(Debug, Clone, Copy, Deserialize)]
422+
#[serde(rename_all = "camelCase")]
423+
pub enum ProgressBarStatus {
424+
/// Hide progress bar.
425+
None,
426+
/// Normal state.
427+
Normal,
428+
/// Indeterminate state. **Treated as Normal on Linux and macOS**
429+
Indeterminate,
430+
/// Paused state. **Treated as Normal on Linux**
431+
Paused,
432+
/// Error state. **Treated as Normal on Linux**
433+
Error,
434+
}
435+
436+
/// Progress Bar State
437+
#[derive(Debug, Deserialize)]
438+
#[serde(rename_all = "camelCase")]
439+
pub struct ProgressBarState {
440+
/// The progress bar status.
441+
pub status: Option<ProgressBarStatus>,
442+
/// The progress bar progress. This can be a value ranging from `0` to `100`
443+
pub progress: Option<u64>,
444+
/// The identifier for your app to communicate with the Unity desktop window manager **Linux Only**
445+
pub unity_uri: Option<String>,
446+
}

core/tauri/scripts/bundle.global.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/tauri/src/test/mock_runtime.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use tauri_runtime::{
1818

1919
#[cfg(target_os = "macos")]
2020
use tauri_utils::TitleBarStyle;
21-
use tauri_utils::{config::WindowConfig, Theme};
21+
use tauri_utils::{config::WindowConfig, ProgressBarState, Theme};
2222
use url::Url;
2323

2424
#[cfg(windows)]
@@ -676,6 +676,10 @@ impl<T: UserEvent> Dispatch<T> for MockDispatcher {
676676
.replace(script.into());
677677
Ok(())
678678
}
679+
680+
fn set_progress_bar(&self, progress_state: ProgressBarState) -> Result<()> {
681+
Ok(())
682+
}
679683
}
680684

681685
#[derive(Debug, Clone)]

core/tauri/src/window/mod.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ use crate::{
3232
},
3333
sealed::ManagerBase,
3434
sealed::RuntimeOrDispatch,
35-
utils::config::{WindowConfig, WindowEffectsConfig, WindowUrl},
35+
utils::{
36+
config::{WindowConfig, WindowEffectsConfig, WindowUrl},
37+
ProgressBarState,
38+
},
3639
EventLoopMessage, Manager, Runtime, Theme, WindowEvent,
3740
};
3841
#[cfg(desktop)]
@@ -2044,6 +2047,21 @@ impl<R: Runtime> Window<R> {
20442047
pub fn start_dragging(&self) -> crate::Result<()> {
20452048
self.window.dispatcher.start_dragging().map_err(Into::into)
20462049
}
2050+
2051+
/// Sets the taskbar progress state.
2052+
///
2053+
/// ## Platform-specific
2054+
///
2055+
/// - **Linux / macOS**: Progress bar is app-wide and not specific to this window.
2056+
/// - **Linux**: Only supported desktop environments with `libunity` (e.g. GNOME).
2057+
/// - **iOS / Android:** Unsupported.
2058+
pub fn set_progress_bar(&self, progress_state: ProgressBarState) -> crate::Result<()> {
2059+
self
2060+
.window
2061+
.dispatcher
2062+
.set_progress_bar(progress_state)
2063+
.map_err(Into::into)
2064+
}
20472065
}
20482066

20492067
/// Webview APIs.

core/tauri/src/window/plugin.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::{
1212
#[cfg(desktop)]
1313
mod desktop_commands {
1414
use serde::Deserialize;
15+
use tauri_utils::ProgressBarState;
1516

1617
use super::*;
1718
use crate::{
@@ -153,6 +154,7 @@ mod desktop_commands {
153154
setter!(set_cursor_position, Position);
154155
setter!(set_ignore_cursor_events, bool);
155156
setter!(start_dragging);
157+
setter!(set_progress_bar, ProgressBarState);
156158
setter!(print);
157159

158160
#[command(root = "crate")]
@@ -302,6 +304,7 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
302304
desktop_commands::set_cursor_position,
303305
desktop_commands::set_ignore_cursor_events,
304306
desktop_commands::start_dragging,
307+
desktop_commands::set_progress_bar,
305308
desktop_commands::print,
306309
desktop_commands::set_icon,
307310
desktop_commands::toggle_maximize,

examples/api/dist/assets/index.js

Lines changed: 39 additions & 37 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)