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

View File

@@ -205,7 +205,6 @@ fn js_appbuilder_run(mut cx: FunctionContext) -> JsResult<JsUndefined> {
let undefined = cx.undefined();
let cx_ref = Rc::new(RefCell::new(cx));
let cx_ref2 = cx_ref.clone();
let hook_handler = move |hook_name: &str, current_version: Version| {
let mut cx = cx_ref.borrow_mut();
@@ -228,14 +227,11 @@ fn js_appbuilder_run(mut cx: FunctionContext) -> JsResult<JsUndefined> {
builder = builder.set_locator(locator);
}
if let Some(argarray) = argarray {
builder = builder.set_args(argarray);
if let Some(arg_array) = argarray {
builder = builder.set_args(arg_array);
}
builder.run().or_else(|e| {
let mut cx = cx_ref2.borrow_mut();
cx.throw_error(e.to_string())
})?;
builder.run();
Ok(undefined)
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,17 +0,0 @@
[package]
name = "velorusticedsample"
version = "0.0.1"
edition = "2021"
publish = false
[dependencies]
anyhow = "1.0"
iced = { version = "0.12", 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 = "../../", features = ["async"]}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.4 KiB

View File

@@ -1,26 +0,0 @@
#!/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

@@ -1,20 +0,0 @@
@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

@@ -1,7 +0,0 @@
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

@@ -1,135 +0,0 @@
#![windows_subsystem = "windows"]
use anyhow::Result;
use iced::theme::Theme;
use iced::widget::{Button, Column, Text};
use iced::{Command, Application};
use velopack::*;
#[derive(Debug, Clone)]
pub enum Message {
CheckForUpdates,
UpdatesFound(Option<UpdateInfo>),
DownloadUpdates,
DownloadProgress(i16),
DownloadComplete,
Restart,
}
pub struct GUI {
update_manager: Option<UpdateManager<sources::FileSource>>,
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 mut set = iced::Settings::default();
set.window.size = iced::Size { width: 400.0, height: 200.0 };
GUI::run(set)?;
Ok(())
}
impl Application for GUI {
type Message = Message;
type Theme = Theme;
type Executor = iced::executor::Default;
type Flags = ();
fn new(_flags: ()) -> (Self, Command<Self::Message>) {
let sounce = sources::FileSource::new(env!("RELEASES_DIR"));
let um = UpdateManager::new(sounce, 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 = Self { update_manager: um.ok(), state: state, current_version: version, update_info: None, download_progress: 0 };
(gui, iced::Command::none())
}
fn title(&self) -> String {
String::from("Velopack Rust Demo")
}
fn update(&mut self, message: Self::Message) -> iced::Command<Self::Message> {
match message {
Message::CheckForUpdates => {
self.state = GUIState::Checking;
iced::Command::perform(self.update_manager.as_ref().unwrap().check_for_updates_async(), |result| match result {
Ok(update_info) => Message::UpdatesFound(update_info),
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) => {
self.update_info = update;
self.state = match self.update_info {
Some(_) => GUIState::UpdatesAvailable,
None => GUIState::Idle,
};
iced::Command::none()
}
Message::DownloadUpdates => {
self.state = GUIState::Downloading;
let update_info = self.update_info.clone().unwrap(); // Ensure you handle this safely in your actual code
iced::Command::perform(self.update_manager.as_ref().unwrap().download_updates_async(&update_info, None), |_| Message::DownloadComplete)
}
Message::DownloadProgress(progress) => {
self.download_progress = progress;
iced::Command::none()
}
Message::DownloadComplete => {
self.state = GUIState::ReadyToRestart;
iced::Command::none()
}
Message::Restart => {
let update_info = self.update_info.clone().unwrap(); // Ensure you handle this safely in your actual code
self.update_manager.as_ref().unwrap().apply_updates_and_restart(update_info, RestartArgs::None).unwrap();
iced::Command::none()
}
}
}
fn view(&self) -> iced::Element<Self::Message> {
let content = match self.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: {}", self.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 = self.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: {}%", self.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()
}
}

View File

@@ -111,7 +111,7 @@ impl<'a> VelopackApp<'a> {
/// Runs the Velopack startup logic. This should be the first thing to run in your app.
/// In some circumstances it may terminate/restart the process to perform tasks.
pub fn run(&mut self) -> Result<(), Error> {
pub fn run(&mut self) {
let args: Vec<String> = self.args.clone();
info!("VelopackApp: Running with args: {:?}", args);
@@ -125,15 +125,15 @@ impl<'a> VelopackApp<'a> {
_ => {} // Handle other cases or do nothing
}
}
let locator = if let Some(config) = &self.locator {
let manifest = config.load_manifest()?;
VelopackLocator::new(config.clone(), manifest)
} else {
auto_locate_app_manifest(LocationContext::FromCurrentExe)?
};
let my_version = locator.get_manifest_version();
let locator = self.load_locator();
if let Err(e) = locator {
error!("VelopackApp: Error loading locator: {:?}", e);
return;
}
let my_version = locator.unwrap().get_manifest_version();
let firstrun = env::var("VELOPACK_FIRSTRUN").is_ok();
let restarted = env::var("VELOPACK_RESTART").is_ok();
@@ -147,8 +147,6 @@ impl<'a> VelopackApp<'a> {
if restarted {
Self::call_hook(&mut self.restarted_hook, &my_version);
}
Ok(())
}
fn call_hook(hook_option: &mut Option<Box<dyn FnOnce(Version) + 'a>>, version: &Version) {
@@ -168,4 +166,14 @@ impl<'a> VelopackApp<'a> {
}
}
}
fn load_locator(&self) -> Result<VelopackLocator, Error> {
let locator = if let Some(config) = &self.locator {
let manifest = config.load_manifest()?;
VelopackLocator::new(config.clone(), manifest)
} else {
auto_locate_app_manifest(LocationContext::FromCurrentExe)?
};
Ok(locator)
}
}