Skip to content

Commit

Permalink
enh(msix): force run as an appx package
Browse files Browse the repository at this point in the history
  • Loading branch information
eythaann committed Dec 13, 2024
1 parent 7d8186a commit e78f337
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 19 deletions.
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
- add new setting on dock and toolbar to maintain overlap state by monitor.
- add service to restart the seelen-ui app on crash.

### enhancements
- force run the app as an APPX if it was installed using msix.

### refactor
- custom http server to serve files instead bundle it in the executable on development (local).

Expand Down
2 changes: 1 addition & 1 deletion src/apps/settings/modules/information/infrastructure.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { Icon } from '../../../shared/components/Icon';
import { SettingsGroup, SettingsOption, SettingsSubGroup } from '../../components/SettingsBox';

export function Information() {
const [isMsixBuild, setIsMsixBuild] = useState(false);
const [isMsixBuild, setIsMsixBuild] = useState(true);
const [isDevMode, setIsDevMode] = useState(false);

const devTools = useSelector(newSelectors.devTools);
Expand Down
14 changes: 7 additions & 7 deletions src/apps/shared/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { path } from '@tauri-apps/api';
import { getRootElement, invoke, SeelenCommand } from 'seelen-core';
import { invoke } from '@tauri-apps/api/core';
import { getRootElement, SeelenCommand } from 'seelen-core';

export const getRootContainer = getRootElement;

export function toPhysicalPixels(size: number): number {
return Math.round(size * window.devicePixelRatio);
}

export async function wasInstalledUsingMSIX() {
let installPath = await path.resourceDir();
return installPath.startsWith('C:\\Program Files\\WindowsApps');
export async function wasInstalledUsingMSIX(): Promise<boolean> {
// Todo replace this when added to SeelenCommand
return invoke('is_appx_package');
}

export async function isDev() {
export async function isDev(): Promise<boolean> {
return invoke(SeelenCommand.IsDevMode);
}
}
10 changes: 9 additions & 1 deletion src/background/exposed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ use crate::seelen_weg::icon_extractor::{
use crate::seelen_wm_v2::handler::*;
use crate::state::infrastructure::*;
use crate::system::brightness::*;
use crate::utils::is_virtual_desktop_supported as virtual_desktop_supported;
use crate::utils::{
is_running_as_appx_package, is_virtual_desktop_supported as virtual_desktop_supported,
};
use crate::windows_api::WindowsApi;
use crate::winevent::{SyntheticFullscreenData, WinEvent};
use crate::{log_error, utils};
Expand Down Expand Up @@ -85,6 +87,11 @@ fn is_dev_mode() -> bool {
tauri::is_dev()
}

#[tauri::command(async)]
fn is_appx_package() -> bool {
is_running_as_appx_package()
}

#[tauri::command(async)]
pub fn get_user_envs() -> HashMap<String, String> {
std::env::vars().collect::<HashMap<String, String>>()
Expand Down Expand Up @@ -165,6 +172,7 @@ pub fn register_invoke_handler(app_builder: Builder<Wry>) -> Builder<Wry> {
// General
run,
is_dev_mode,
is_appx_package,
open_file,
run_as_admin,
select_file_on_explorer,
Expand Down
13 changes: 9 additions & 4 deletions src/background/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Prevents additional console window on Windows in release
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
#![feature(never_type)]

mod error_handler;
mod exposed;
Expand Down Expand Up @@ -44,10 +45,10 @@ use tauri::webview_version;
use tray::try_register_tray_icon;
use utils::{
integrity::{
check_for_webview_optimal_state, kill_slu_service, start_slu_service,
check_for_webview_optimal_state, kill_slu_service, restart_as_appx, start_slu_service,
validate_webview_runtime_is_installed,
},
PERFORMANCE_HELPER,
is_running_as_appx_package, was_installed_using_msix, PERFORMANCE_HELPER,
};
use windows::Win32::Security::{SE_DEBUG_NAME, SE_SHUTDOWN_NAME};
use windows_api::WindowsApi;
Expand Down Expand Up @@ -100,7 +101,7 @@ fn setup(app: &mut tauri::App<tauri::Wry>) -> Result<()> {
print_initial_information();
validate_webview_runtime_is_installed(app.handle())?;

if !tauri::is_dev() {
if !tauri::is_dev() || is_running_as_appx_package() {
start_slu_service(app)?;
}

Expand Down Expand Up @@ -162,7 +163,6 @@ fn is_already_runnning() -> bool {

fn main() -> Result<()> {
register_panic_hook()?;
trace_lock!(PERFORMANCE_HELPER).start("setup");

let command = trace_lock!(SEELEN_COMMAND_LINE).clone();
let matches = match command.try_get_matches() {
Expand Down Expand Up @@ -208,6 +208,11 @@ fn main() -> Result<()> {
}
}

if was_installed_using_msix() && !is_running_as_appx_package() {
restart_as_appx()?;
}

trace_lock!(PERFORMANCE_HELPER).start("setup");
let mut app_builder = tauri::Builder::default();
app_builder = register_plugins(app_builder);
app_builder = register_invoke_handler(app_builder);
Expand Down
14 changes: 11 additions & 3 deletions src/background/seelen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::{
state::application::{FullState, FULL_STATE},
system::{declare_system_events_handlers, release_system_events_handlers},
trace_lock,
utils::{ahk::AutoHotKey, is_msix_intallation, PERFORMANCE_HELPER},
utils::{ahk::AutoHotKey, is_running_as_appx_package, PERFORMANCE_HELPER},
windows_api::WindowsApi,
APP_HANDLE,
};
Expand Down Expand Up @@ -279,7 +279,7 @@ impl Seelen {
}

pub fn refresh_path_environment() -> Result<()> {
if tauri::is_dev() || is_msix_intallation() {
if tauri::is_dev() || is_running_as_appx_package() {
return Ok(());
}

Expand All @@ -304,7 +304,15 @@ impl Seelen {
let pwsh_script_path = temp_dir().join("schedule.ps1");
std::fs::write(&pwsh_script_path, pwsh_script).expect("Failed to write temp script file");

let exe_path = std::env::current_exe()?;
let exe_path = if is_running_as_appx_package() {
// this is the path of the alias generated by msix intaller
get_app_handle()
.path()
.local_data_dir()?
.join("Microsoft\\WindowsApps\\seelen-ui.exe")
} else {
std::env::current_exe()?
};

let output = get_app_handle()
.shell()
Expand Down
7 changes: 7 additions & 0 deletions src/background/utils/integrity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,10 @@ pub fn kill_slu_service() -> Result<()> {
}
Ok(())
}

pub fn restart_as_appx() -> Result<!> {
std::process::Command::new("explorer")
.arg(r"shell:AppsFolder\Seelen.SeelenUI_p6yyn03m1894e!App")
.spawn()?;
std::process::exit(0);
}
4 changes: 2 additions & 2 deletions src/background/utils/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use crate::{
utils::integrity::kill_slu_service,
};

use super::is_msix_intallation;
use super::is_running_as_appx_package;

pub async fn check_for_updates() -> Result<Option<Update>> {
if tauri::is_dev() || is_msix_intallation() {
if tauri::is_dev() || is_running_as_appx_package() {
return Ok(None);
}

Expand Down
9 changes: 8 additions & 1 deletion src/background/utils/winver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,18 @@ pub fn is_virtual_desktop_supported() -> bool {
false
}

pub fn is_msix_intallation() -> bool {
pub fn is_running_as_appx_package() -> bool {
unsafe {
let mut len = 0u32;
let _ = GetCurrentPackageId(&mut len, None);
let mut buffer = vec![0u8; len as usize];
GetCurrentPackageId(&mut len, Some(buffer.as_mut_ptr())).is_ok()
}
}

pub fn was_installed_using_msix() -> bool {
std::env::current_exe().is_ok_and(|p| {
p.with_file_name("AppxManifest.xml").exists()
|| p.starts_with("C:\\Program Files\\WindowsApps")
})
}

0 comments on commit e78f337

Please sign in to comment.