Skip to content

Commit 7c264a7

Browse files
committed
rename ProgressStyle & fix installView not being able to receive progress bar start event.
1 parent 87f9208 commit 7c264a7

File tree

12 files changed

+112
-100
lines changed

12 files changed

+112
-100
lines changed

rim_common/src/utils/download.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use tokio::io::AsyncWriteExt;
99
use url::Url;
1010

1111
use crate::types::Proxy as CrateProxy;
12-
use crate::utils::{ProgressHandler, ProgressStyle};
12+
use crate::utils::{ProgressHandler, ProgressKind};
1313
use crate::{build_config, setter};
1414

1515
fn default_proxy() -> reqwest::Proxy {
@@ -128,8 +128,10 @@ impl DownloadOpt {
128128
.content_length()
129129
.ok_or_else(|| anyhow!("unable to get file length of '{url}'"))?;
130130

131-
self.progress_handler
132-
.start(format!("downloading '{}'", &self.name), ProgressStyle::Bytes(total_size))?;
131+
self.progress_handler.start(
132+
format!("downloading '{}'", &self.name),
133+
ProgressKind::Bytes(total_size),
134+
)?;
133135

134136
while let Some(chunk) = resp.chunk().await? {
135137
file.write_all(&chunk).await?;

rim_common/src/utils/extraction.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use xz2::read::XzDecoder;
99
use zip::ZipArchive;
1010

1111
use crate::setter;
12-
use crate::utils::{ProgressHandler, ProgressStyle};
12+
use crate::utils::{ProgressHandler, ProgressKind};
1313

1414
use super::file_system::{ensure_dir, ensure_parent_dir, walk_dir};
1515
use super::progress_bar::CliProgress;
@@ -194,9 +194,11 @@ struct ExtractHelper<'a, T: ProgressHandler> {
194194
}
195195

196196
impl<T: ProgressHandler> ExtractHelper<'_, T> {
197-
fn start_progress_bar(&mut self, style: ProgressStyle) -> Result<()> {
198-
self.handler
199-
.start(format!("extracting file '{}'", self.file_path.display()), style)?;
197+
fn start_progress_bar(&mut self, style: ProgressKind) -> Result<()> {
198+
self.handler.start(
199+
format!("extracting file '{}'", self.file_path.display()),
200+
style,
201+
)?;
200202
Ok(())
201203
}
202204

@@ -212,7 +214,7 @@ impl<T: ProgressHandler> ExtractHelper<'_, T> {
212214
let zip_len = archive.len();
213215

214216
// Init progress
215-
self.start_progress_bar(ProgressStyle::Len(zip_len.try_into()?))?;
217+
self.start_progress_bar(ProgressKind::Len(zip_len.try_into()?))?;
216218

217219
for i in 0..zip_len {
218220
let mut zip_file = archive.by_index(i)?;
@@ -255,7 +257,7 @@ impl<T: ProgressHandler> ExtractHelper<'_, T> {
255257
let mut extracted_len: u64 = 0;
256258

257259
// Init progress bar
258-
self.start_progress_bar(ProgressStyle::Bytes(sz_len))?;
260+
self.start_progress_bar(ProgressKind::Bytes(sz_len))?;
259261

260262
archive.for_each_entries(|entry, reader| {
261263
let mut buf = [0_u8; 1024];
@@ -293,8 +295,6 @@ impl<T: ProgressHandler> ExtractHelper<'_, T> {
293295
})?;
294296
}
295297
}
296-
// NB: sevenz-rust does not support `unix-mode` like `zip` does, so we might ended up
297-
// mess up the extracted file's permission... let's hope that never happens.
298298
})?;
299299

300300
self.end_progress_bar()?;
@@ -306,7 +306,7 @@ impl<T: ProgressHandler> ExtractHelper<'_, T> {
306306
archive.set_preserve_permissions(true);
307307

308308
// Init progress bar, use spinner because the length of entries cannot be retrieved.
309-
self.start_progress_bar(ProgressStyle::Spinner {
309+
self.start_progress_bar(ProgressKind::Spinner {
310310
auto_tick_duration: Some(std::time::Duration::from_millis(100)),
311311
})?;
312312

rim_common/src/utils/progress_bar.rs

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::time::Duration;
88
/// Abstract progress sender/handler used for both CLI and GUI mode.
99
pub trait ProgressHandler: Send + Sync {
1010
/// Start the progress with a certain message and style.
11-
fn start(&mut self, msg: String, style: ProgressStyle) -> Result<()>;
11+
fn start(&mut self, msg: String, style: ProgressKind) -> Result<()>;
1212
/// Update the progress to a value, or tick once if the value is `None`.
1313
fn update(&self, value: Option<u64>) -> Result<()>;
1414
/// Finish progress with a certain message.
@@ -17,7 +17,7 @@ pub trait ProgressHandler: Send + Sync {
1717
// Optional overall (master) progress control
1818

1919
/// Start the master progress bar with a certain progress.
20-
fn start_master(&mut self, msg: String, style: ProgressStyle) -> Result<()> {
20+
fn start_master(&mut self, msg: String, style: ProgressKind) -> Result<()> {
2121
Ok(())
2222
}
2323
/// Update the master progress bar, or tick once if the value is `None`.
@@ -31,7 +31,7 @@ pub trait ProgressHandler: Send + Sync {
3131
}
3232

3333
#[derive(Debug, Clone, Copy)]
34-
pub enum ProgressStyle {
34+
pub enum ProgressKind {
3535
/// Display the progress base on number of bytes.
3636
Bytes(u64),
3737
/// Display the progress base on position & length parameters.
@@ -46,7 +46,7 @@ pub enum ProgressStyle {
4646
Hidden,
4747
}
4848

49-
impl ProgressStyle {
49+
impl ProgressKind {
5050
fn cli_pattern(&self) -> &str {
5151
match self {
5252
Self::Bytes(_) => "{msg}\n{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {bytes}/{total_bytes} ({eta})",
@@ -70,7 +70,7 @@ impl ProgressStyle {
7070
pub struct HiddenProgress;
7171

7272
impl ProgressHandler for HiddenProgress {
73-
fn start(&mut self, _msg: String, _style: ProgressStyle) -> Result<()> {
73+
fn start(&mut self, _msg: String, _style: ProgressKind) -> Result<()> {
7474
Ok(())
7575
}
7676
fn finish(&self, _msg: String) -> Result<()> {
@@ -85,50 +85,52 @@ impl ProgressHandler for HiddenProgress {
8585
#[derive(Debug, Clone)]
8686
pub struct CliProgress {
8787
bar: CliProgressBar,
88-
style: ProgressStyle,
88+
style: ProgressKind,
8989
}
9090

9191
impl Default for CliProgress {
9292
fn default() -> Self {
9393
Self {
9494
bar: CliProgressBar::hidden(),
95-
style: ProgressStyle::Hidden,
95+
style: ProgressKind::Hidden,
9696
}
9797
}
9898
}
9999

100100
impl ProgressHandler for CliProgress {
101-
fn start(&mut self, msg: String, style: ProgressStyle) -> Result<()> {
101+
fn start(&mut self, msg: String, style: ProgressKind) -> Result<()> {
102102
// log the starting of the progress
103103
info!("{msg}");
104104

105105
let bar = match style {
106-
ProgressStyle::Bytes(len) | ProgressStyle::Len(len) => CliProgressBar::new(len),
107-
ProgressStyle::Spinner { auto_tick_duration } => {
106+
ProgressKind::Bytes(len) | ProgressKind::Len(len) => CliProgressBar::new(len),
107+
ProgressKind::Spinner { auto_tick_duration } => {
108108
let bar = CliProgressBar::new_spinner();
109109
if let Some(interval) = auto_tick_duration {
110110
bar.enable_steady_tick(interval);
111111
}
112112
bar
113113
}
114-
ProgressStyle::Hidden => CliProgressBar::hidden(),
114+
ProgressKind::Hidden => CliProgressBar::hidden(),
115115
};
116116

117-
self.bar = bar.with_style(
118-
CliProgressStyle::with_template(style.cli_pattern())
119-
.with_context(|| {
120-
format!("Internal error: Invalid style pattern defined for {style:?}")
121-
})?
122-
.with_key(
123-
"eta",
124-
|state: &ProgressState, w: &mut dyn std::fmt::Write| {
125-
write!(w, "{:.1}s", state.eta().as_secs_f64())
126-
.expect("unable to display progress bar")
127-
},
128-
)
129-
.progress_chars("#>-"),
130-
).with_message(msg)
131-
.with_position(0);
117+
self.bar = bar
118+
.with_style(
119+
CliProgressStyle::with_template(style.cli_pattern())
120+
.with_context(|| {
121+
format!("Internal error: Invalid style pattern defined for {style:?}")
122+
})?
123+
.with_key(
124+
"eta",
125+
|state: &ProgressState, w: &mut dyn std::fmt::Write| {
126+
write!(w, "{:.1}s", state.eta().as_secs_f64())
127+
.expect("unable to display progress bar")
128+
},
129+
)
130+
.progress_chars("#>-"),
131+
)
132+
.with_message(msg)
133+
.with_position(0);
132134
self.style = style;
133135

134136
Ok(())

rim_gui/src-tauri/src/progress.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! GUI progress bar module
22
3-
use rim_common::utils::{ProgressHandler, ProgressStyle};
3+
use rim_common::utils::{ProgressHandler, ProgressKind};
44
use serde::Serialize;
55
use tauri::{AppHandle, Manager};
66

@@ -20,13 +20,13 @@ enum GuiProgressStyle {
2020
Hidden,
2121
}
2222

23-
impl From<ProgressStyle> for GuiProgressStyle {
24-
fn from(value: ProgressStyle) -> Self {
23+
impl From<ProgressKind> for GuiProgressStyle {
24+
fn from(value: ProgressKind) -> Self {
2525
match value {
26-
ProgressStyle::Bytes(_) => GuiProgressStyle::Bytes,
27-
ProgressStyle::Len(_) => GuiProgressStyle::Len,
28-
ProgressStyle::Spinner { .. } => GuiProgressStyle::Spinner,
29-
ProgressStyle::Hidden => GuiProgressStyle::Hidden,
26+
ProgressKind::Bytes(_) => GuiProgressStyle::Bytes,
27+
ProgressKind::Len(_) => GuiProgressStyle::Len,
28+
ProgressKind::Spinner { .. } => GuiProgressStyle::Spinner,
29+
ProgressKind::Hidden => GuiProgressStyle::Hidden,
3030
}
3131
}
3232
}
@@ -45,19 +45,17 @@ pub(crate) struct GuiProgress {
4545

4646
impl GuiProgress {
4747
pub(crate) fn new(handle: AppHandle) -> Self {
48-
Self {
49-
handle,
50-
}
48+
Self { handle }
5149
}
5250
}
5351

5452
impl ProgressHandler for GuiProgress {
55-
fn start(&mut self, msg: String, style: ProgressStyle) -> anyhow::Result<()> {
53+
fn start(&mut self, msg: String, style: ProgressKind) -> anyhow::Result<()> {
5654
let (length, gui_style) = match style {
57-
ProgressStyle::Bytes(len) => (Some(len), GuiProgressStyle::Bytes),
58-
ProgressStyle::Len(len) => (Some(len), GuiProgressStyle::Len),
59-
ProgressStyle::Spinner { .. } => (None, GuiProgressStyle::Spinner),
60-
ProgressStyle::Hidden => (None, GuiProgressStyle::Hidden),
55+
ProgressKind::Bytes(len) => (Some(len), GuiProgressStyle::Bytes),
56+
ProgressKind::Len(len) => (Some(len), GuiProgressStyle::Len),
57+
ProgressKind::Spinner { .. } => (None, GuiProgressStyle::Spinner),
58+
ProgressKind::Hidden => (None, GuiProgressStyle::Hidden),
6159
};
6260

6361
let payload = ProgressPayload {
@@ -80,7 +78,7 @@ impl ProgressHandler for GuiProgress {
8078
Ok(())
8179
}
8280

83-
fn start_master(&mut self, msg: String, style: ProgressStyle) -> anyhow::Result<()> {
81+
fn start_master(&mut self, msg: String, style: ProgressKind) -> anyhow::Result<()> {
8482
let payload = ProgressPayload {
8583
message: msg,
8684
length: style.length(),

rim_gui/src/components/BaseProgress.vue

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
<div class="progress-bar">
44
<div class="progress-fill"
55
:class="{ 'progress-transition': transition }"
6-
:style="{ width: valuePercentage(style, value, length) + '%' }"></div>
7-
<div class="progress-label" text-end>{{ progressFormat(style, value, length) }}</div>
6+
:style="{ width: valuePercentage(kind, value, length) + '%' }"></div>
7+
<div class="progress-label" text-end>{{ progressFormat(kind, value, length) }}</div>
88
</div>
99
</div>
1010
</template>
1111

1212
<script setup lang="ts">
1313
import { PropType } from 'vue';
1414
15-
type ProgressStyle = 'percentage' | 'len' | 'bytes' | 'spinner' | 'hidden';
15+
type ProgressKind = 'percentage' | 'len' | 'bytes' | 'spinner' | 'hidden';
1616
1717
defineProps({
1818
value: {
@@ -23,8 +23,8 @@ defineProps({
2323
type: Number,
2424
default: 0,
2525
},
26-
style: {
27-
type: String as PropType<ProgressStyle>,
26+
kind: {
27+
type: String as PropType<ProgressKind>,
2828
default: 'percentage',
2929
validator: (value: string) => ['percentage', 'len', 'bytes', 'spinner', 'hidden'].includes(value)
3030
},
@@ -35,8 +35,8 @@ defineProps({
3535
});
3636
3737
// calculate the progress bar fill percentage
38-
function valuePercentage(style: ProgressStyle, value: number, length?: number): number {
39-
switch (style) {
38+
function valuePercentage(kind: ProgressKind, value: number, length?: number): number {
39+
switch (kind) {
4040
case 'percentage':
4141
return value;
4242
case 'len':
@@ -51,8 +51,8 @@ function valuePercentage(style: ProgressStyle, value: number, length?: number):
5151
}
5252
}
5353
54-
function progressFormat(style: ProgressStyle, value: number, length?: number): string {
55-
switch (style) {
54+
function progressFormat(kind: ProgressKind, value: number, length?: number): string {
55+
switch (kind) {
5656
case 'bytes':
5757
return `${formatBytes(value)}${length ? ' | ' + formatBytes(length) : ''}`;
5858
case 'len':

rim_gui/src/utils/types/payloads.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export interface CliPayload {
33
command: string,
44
}
55

6-
export enum ProgressStyle {
6+
export enum ProgressKind {
77
Bytes = 'bytes',
88
Len = 'len',
99
Spinner = 'spinner',
@@ -12,6 +12,6 @@ export enum ProgressStyle {
1212

1313
export interface ProgressPayload {
1414
message: string,
15-
style: ProgressStyle,
15+
style: ProgressKind,
1616
length?: number,
1717
}

rim_gui/src/views/installer/ConfirmView.vue

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup lang="ts">
2-
import { installConf, invokeCommand, Component } from '@/utils/index';
2+
import { installConf } from '@/utils/index';
33
import { useCustomRouter } from '@/router/index';
44
import { computed } from 'vue';
55
@@ -11,12 +11,8 @@ const components = computed(() => {
1111
return list;
1212
});
1313
14-
async function handleNextClick() {
14+
function handleNextClick() {
1515
routerPush('/installer/install');
16-
await invokeCommand('install_toolkit', {
17-
componentsList: components.value as Component[],
18-
config: installConf.config.value,
19-
});
2016
}
2117
</script>
2218

0 commit comments

Comments
 (0)