Skip to content

Commit

Permalink
feat: set application progress bar, close #7999 (#8009)
Browse files Browse the repository at this point in the history
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
  • Loading branch information
pewsheen and lucasfernog committed Oct 17, 2023
1 parent a6ad540 commit c085add
Show file tree
Hide file tree
Showing 14 changed files with 432 additions and 212 deletions.
8 changes: 8 additions & 0 deletions .changes/add-progress-bar.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"tauri": 'patch:feat'
"tauri-runtime": 'patch:feat'
"tauri-runtime-wry": 'patch:feat'
"tauri-utils": 'patch:feat'
---

Added `set_progress_bar` to `Window`.
5 changes: 5 additions & 0 deletions .changes/set-progress-bar-api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tauri-apps/api": patch:feat
---

Added the `setProgressBar` API on the `Window` class.
52 changes: 49 additions & 3 deletions core/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ use wry::webview::WebViewBuilderExtWindows;

#[cfg(target_os = "macos")]
use tauri_utils::TitleBarStyle;
use tauri_utils::{config::WindowConfig, debug_eprintln, Theme};
use tauri_utils::{
config::WindowConfig, debug_eprintln, ProgressBarState, ProgressBarStatus, Theme,
};
use wry::{
application::{
dpi::{
Expand All @@ -56,8 +58,9 @@ use wry::{
},
monitor::MonitorHandle,
window::{
CursorIcon as WryCursorIcon, Fullscreen, Icon as WryWindowIcon, Theme as WryTheme,
UserAttentionType as WryUserAttentionType,
CursorIcon as WryCursorIcon, Fullscreen, Icon as WryWindowIcon,
ProgressBarState as WryProgressBarState, ProgressState as WryProgressState,
Theme as WryTheme, UserAttentionType as WryUserAttentionType,
},
},
webview::{FileDropEvent as WryFileDropEvent, Url, WebContext, WebView, WebViewBuilder},
Expand Down Expand Up @@ -520,6 +523,35 @@ impl From<CursorIcon> for CursorIconWrapper {
}
}

pub struct ProgressStateWrapper(pub WryProgressState);

impl From<ProgressBarStatus> for ProgressStateWrapper {
fn from(status: ProgressBarStatus) -> Self {
let state = match status {
ProgressBarStatus::None => WryProgressState::None,
ProgressBarStatus::Normal => WryProgressState::Normal,
ProgressBarStatus::Indeterminate => WryProgressState::Indeterminate,
ProgressBarStatus::Paused => WryProgressState::Paused,
ProgressBarStatus::Error => WryProgressState::Error,
};
Self(state)
}
}

pub struct ProgressBarStateWrapper(pub WryProgressBarState);

impl From<ProgressBarState> for ProgressBarStateWrapper {
fn from(progress_state: ProgressBarState) -> Self {
Self(WryProgressBarState {
progress: progress_state.progress,
state: progress_state
.status
.map(|state| ProgressStateWrapper::from(state).0),
unity_uri: progress_state.unity_uri,
})
}
}

#[derive(Clone, Default)]
pub struct WindowBuilderWrapper {
inner: WryWindowBuilder,
Expand Down Expand Up @@ -1006,6 +1038,7 @@ pub enum WindowMessage {
SetCursorIcon(CursorIcon),
SetCursorPosition(Position),
SetIgnoreCursorEvents(bool),
SetProgressBar(ProgressBarState),
DragWindow,
RequestRedraw,
}
Expand Down Expand Up @@ -1520,6 +1553,16 @@ impl<T: UserEvent> Dispatch<T> for WryDispatcher<T> {
),
)
}

fn set_progress_bar(&self, progress_state: ProgressBarState) -> Result<()> {
send_user_message(
&self.context,
Message::Window(
self.window_id,
WindowMessage::SetProgressBar(progress_state),
),
)
}
}

#[derive(Clone)]
Expand Down Expand Up @@ -2302,6 +2345,9 @@ fn handle_user_message<T: UserEvent>(
WindowMessage::RequestRedraw => {
window.request_redraw();
}
WindowMessage::SetProgressBar(progress_state) => {
window.set_progress_bar(ProgressBarStateWrapper::from(progress_state).0);
}
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion core/tauri-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use raw_window_handle::RawDisplayHandle;
use serde::Deserialize;
use std::{fmt::Debug, sync::mpsc::Sender};
use tauri_utils::Theme;
use tauri_utils::{ProgressBarState, Theme};
use url::Url;

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

/// Executes javascript on the window this [`Dispatch`] represents.
fn eval_script<S: Into<String>>(&self, script: S) -> Result<()>;

/// Sets the taskbar progress state.
///
/// ## Platform-specific
///
/// - **Linux / macOS**: Progress bar is app-wide and not specific to this window. Only supported desktop environments with `libunity` (e.g. GNOME).
/// - **iOS / Android:** Unsupported.
fn set_progress_bar(&self, progress_state: ProgressBarState) -> Result<()>;
}
28 changes: 28 additions & 0 deletions core/tauri-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,3 +416,31 @@ pub fn display_path<P: AsRef<Path>>(p: P) -> String {
.display()
.to_string()
}

/// Progress bar status.
#[derive(Debug, Clone, Copy, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum ProgressBarStatus {
/// Hide progress bar.
None,
/// Normal state.
Normal,
/// Indeterminate state. **Treated as Normal on Linux and macOS**
Indeterminate,
/// Paused state. **Treated as Normal on Linux**
Paused,
/// Error state. **Treated as Normal on Linux**
Error,
}

/// Progress Bar State
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ProgressBarState {
/// The progress bar status.
pub status: Option<ProgressBarStatus>,
/// The progress bar progress. This can be a value ranging from `0` to `100`
pub progress: Option<u64>,
/// The identifier for your app to communicate with the Unity desktop window manager **Linux Only**
pub unity_uri: Option<String>,
}
2 changes: 1 addition & 1 deletion core/tauri/scripts/bundle.global.js

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion core/tauri/src/test/mock_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use tauri_runtime::{

#[cfg(target_os = "macos")]
use tauri_utils::TitleBarStyle;
use tauri_utils::{config::WindowConfig, Theme};
use tauri_utils::{config::WindowConfig, ProgressBarState, Theme};
use url::Url;

#[cfg(windows)]
Expand Down Expand Up @@ -676,6 +676,10 @@ impl<T: UserEvent> Dispatch<T> for MockDispatcher {
.replace(script.into());
Ok(())
}

fn set_progress_bar(&self, progress_state: ProgressBarState) -> Result<()> {
Ok(())
}
}

#[derive(Debug, Clone)]
Expand Down
20 changes: 19 additions & 1 deletion core/tauri/src/window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ use crate::{
},
sealed::ManagerBase,
sealed::RuntimeOrDispatch,
utils::config::{WindowConfig, WindowEffectsConfig, WindowUrl},
utils::{
config::{WindowConfig, WindowEffectsConfig, WindowUrl},
ProgressBarState,
},
EventLoopMessage, Manager, Runtime, Theme, WindowEvent,
};
#[cfg(desktop)]
Expand Down Expand Up @@ -2044,6 +2047,21 @@ impl<R: Runtime> Window<R> {
pub fn start_dragging(&self) -> crate::Result<()> {
self.window.dispatcher.start_dragging().map_err(Into::into)
}

/// Sets the taskbar progress state.
///
/// ## Platform-specific
///
/// - **Linux / macOS**: Progress bar is app-wide and not specific to this window.
/// - **Linux**: Only supported desktop environments with `libunity` (e.g. GNOME).
/// - **iOS / Android:** Unsupported.
pub fn set_progress_bar(&self, progress_state: ProgressBarState) -> crate::Result<()> {
self
.window
.dispatcher
.set_progress_bar(progress_state)
.map_err(Into::into)
}
}

/// Webview APIs.
Expand Down
3 changes: 3 additions & 0 deletions core/tauri/src/window/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::{
#[cfg(desktop)]
mod desktop_commands {
use serde::Deserialize;
use tauri_utils::ProgressBarState;

use super::*;
use crate::{
Expand Down Expand Up @@ -153,6 +154,7 @@ mod desktop_commands {
setter!(set_cursor_position, Position);
setter!(set_ignore_cursor_events, bool);
setter!(start_dragging);
setter!(set_progress_bar, ProgressBarState);
setter!(print);

#[command(root = "crate")]
Expand Down Expand Up @@ -302,6 +304,7 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
desktop_commands::set_cursor_position,
desktop_commands::set_ignore_cursor_events,
desktop_commands::start_dragging,
desktop_commands::set_progress_bar,
desktop_commands::print,
desktop_commands::set_icon,
desktop_commands::toggle_maximize,
Expand Down
76 changes: 39 additions & 37 deletions examples/api/dist/assets/index.js

Large diffs are not rendered by default.

Loading

0 comments on commit c085add

Please sign in to comment.