Fix Iced sample

This commit is contained in:
Caelan
2024-09-28 13:47:31 -06:00
parent 06197f1009
commit ffbdd343c7
14 changed files with 1382 additions and 622 deletions

4699
samples/RustIced/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,17 @@
[package]
name = "velorusticedsample"
version = "0.0.1"
edition = "2021"
publish = false
[dependencies]
anyhow = "1.0"
iced = { version = "0.13", features = ["async-std", "debug"] }
async-std = "1.5"
once_cell = "1.19"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
uuid = { version = "1.0", features = ["v4", "fast-rng", "serde"] }
directories-next = "2.0"
tracing-subscriber = "0.3"
velopack = { path = "../../src/lib-rust", features = ["async"] }

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

View File

@@ -0,0 +1,26 @@
#!/bin/bash
# Find the absolute path of the script
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Check if version parameter is provided
if [ "$#" -ne 1 ]; then
echo "Version number is required."
echo "Usage: ./build.sh [version]"
exit 1
fi
BUILD_VERSION="$1"
RELEASE_DIR="$SCRIPT_DIR/releases"
PUBLISH_DIR="$SCRIPT_DIR/publish"
ICON_PATH="$SCRIPT_DIR/Velopack.icns"
echo ""
echo "Compiling Rust/Iced with cargo..."
cargo build -r
mkdir publish
cp target/release/velorusticedsample publish/VeloRustIcedSample
echo ""
echo "Building Velopack Release v$BUILD_VERSION"
vpk pack -u VeloRustIcedSample -v $BUILD_VERSION -o "$RELEASE_DIR" -p "$PUBLISH_DIR" -i "$ICON_PATH"

View File

@@ -0,0 +1,20 @@
@echo off
setlocal enabledelayedexpansion
if "%~1"=="" (
echo Version number is required.
echo Usage: build.bat [version] [extra_args...]
exit /b 1
)
cd %~dp0
echo.
echo Building Iced/Rust
cargo build
echo.
echo Building Velopack Release v%~1
mkdir publish
move target\debug\velorusticedsample.exe publish\velorusticedsample.exe
vpk pack -u VeloRustIcedSample -o releases -p publish -v %*

View File

@@ -0,0 +1,7 @@
use std::path::Path;
fn main() {
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
let releases_dir = Path::new(&manifest_dir).join("releases");
println!("cargo:rustc-env=RELEASES_DIR={}", releases_dir.to_string_lossy());
}

View File

@@ -0,0 +1,127 @@
#![windows_subsystem = "windows"]
use anyhow::Result;
use iced::widget::{Button, Column, Text};
use velopack::*;
#[derive(Debug, Clone)]
pub enum Message {
CheckForUpdates,
UpdatesFound(Option<UpdateInfo>),
DownloadUpdates,
DownloadProgress(i16),
DownloadComplete,
Restart,
}
pub struct GUI {
update_manager: Option<UpdateManager>,
state: GUIState,
current_version: Option<String>,
update_info: Option<UpdateInfo>,
download_progress: i16,
}
#[derive(Debug, Clone)]
pub enum GUIState {
NotInstalled,
Idle,
Checking,
UpdatesAvailable,
Downloading,
ReadyToRestart,
}
fn main() -> Result<()> {
VelopackApp::build().run();
let source = sources::FileSource::new(env!("RELEASES_DIR"));
let um = UpdateManager::new(source, None, None);
let mut version: Option<String> = None;
let mut state = GUIState::NotInstalled;
if um.is_ok() {
state = GUIState::Idle;
version = Some(um.as_ref().unwrap().current_version().unwrap());
}
let gui = GUI { update_manager: um.ok(), state, current_version: version, update_info: None, download_progress: 0 };
iced::application("A cool application", update, view)
.window_size(iced::Size::new(400.0, 200.0))
.run_with(move || (gui, iced::Task::none()))?;
Ok(())
}
fn update(gui: &mut GUI, message: Message) -> iced::Task<Message> {
match message {
Message::CheckForUpdates => {
gui.state = GUIState::Checking;
iced::Task::perform(gui.update_manager.as_ref().unwrap().check_for_updates_async(), |result| match result {
Ok(update_info) => {
match update_info {
UpdateCheck::RemoteIsEmpty => Message::UpdatesFound(None),
UpdateCheck::NoUpdateAvailable => Message::UpdatesFound(None),
UpdateCheck::UpdateAvailable(updates) => Message::UpdatesFound(Some(updates)),
}
},
Err(_) => {
// Handle the error case, perhaps by logging or setting an error state
// For simplicity, we're sending a None update here, but you should handle errors appropriately
Message::UpdatesFound(None)
}
})
}
Message::UpdatesFound(update) => {
gui.update_info = update;
gui.state = match gui.update_info {
Some(_) => GUIState::UpdatesAvailable,
None => GUIState::Idle,
};
iced::Task::none()
}
Message::DownloadUpdates => {
gui.state = GUIState::Downloading;
let update_info = gui.update_info.clone().unwrap(); // Ensure you handle this safely in your actual code
iced::Task::perform(gui.update_manager.as_ref().unwrap().download_updates_async(&update_info, None), |_| Message::DownloadComplete)
}
Message::DownloadProgress(progress) => {
gui.download_progress = progress;
iced::Task::none()
}
Message::DownloadComplete => {
gui.state = GUIState::ReadyToRestart;
iced::Task::none()
}
Message::Restart => {
let update_info = gui.update_info.clone().unwrap(); // Ensure you handle this safely in your actual code
gui.update_manager.as_ref().unwrap().apply_updates_and_restart(update_info).unwrap();
iced::Task::none()
}
}
}
fn view(gui: &GUI) -> iced::Element<Message> {
let content = match gui.state {
GUIState::NotInstalled => Column::new()
.push(Text::new("Can't check for updates if not installed")),
GUIState::Idle => Column::new()
.push(Text::new(format!("Current version: {}", gui.current_version.as_ref().unwrap_or(&"Unknown".to_string()))))
.push(Button::new(Text::new("Check for updates")).on_press(Message::CheckForUpdates)),
GUIState::Checking => Column::new()
.push(Text::new("Checking for updates...")),
GUIState::UpdatesAvailable => {
let update_version = gui.update_info.as_ref().map_or("Unknown", |info| &info.TargetFullRelease.Version);
Column::new()
.push(Text::new(format!("Update available: {}", update_version)))
.push(Button::new(Text::new("Download updates")).on_press(Message::DownloadUpdates))
}
GUIState::Downloading => Column::new()
.push(Text::new(format!("Downloading updates... Progress: {}%", gui.download_progress))),
GUIState::ReadyToRestart => Column::new()
.push(Text::new("Updates downloaded. Ready to restart."))
.push(Button::new(Text::new("Restart")).on_press(Message::Restart)),
};
content.into()
}