Skip to content

Commit

Permalink
Merge pull request #157 from SondreNjaastad/feature/user_selected_def…
Browse files Browse the repository at this point in the history
…ault_app

Synchronized user defined default apps for files
  • Loading branch information
jelveh authored Mar 22, 2024
2 parents fe55497 + 1e3fcc8 commit 4981d1c
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 4 deletions.
14 changes: 11 additions & 3 deletions src/UI/UIDesktop.js
Original file line number Diff line number Diff line change
Expand Up @@ -502,10 +502,18 @@ async function UIDesktop(options){

// update local user preferences
const user_preferences = {
show_hidden_files: (await puter.kv.get('user_preferences.show_hidden_files')) === 'true',
language: (await puter.kv.get('user_preferences.language'))
show_hidden_files: JSON.parse(await puter.kv.get('user_preferences.show_hidden_files')),
language: await puter.kv.get('user_preferences.language'),
};
update_user_preferences(user_preferences);

// update default apps
puter.kv.list('user_preferences.default_apps.*').then(async (default_app_keys) => {
for(let key in default_app_keys){
user_preferences[default_app_keys[key].substring(17)] = await puter.kv.get(default_app_keys[key]);
}

update_user_preferences(user_preferences);
});

// Append to <body>
$('body').append(h);
Expand Down
29 changes: 29 additions & 0 deletions src/UI/UIItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,35 @@ function UIItem(options){
html: suggested_app.title,
icon: `<img src="${html_encode(suggested_app.icon ?? window.icons['app.svg'])}" style="width:16px; height: 16px; margin-bottom: -4px;">`,
onClick: async function(){
var extension = path.extname($(el_item).attr('data-path')).toLowerCase();
if(
user_preferences[`default_apps${extension}`] !== suggested_app.name
&&
(
(!user_preferences[`default_apps${extension}`] && index > 0)
||
(user_preferences[`default_apps${extension}`])
)
){
const alert_resp = await UIAlert({
message: `${i18n('change_allways_open_with')} ` + html_encode(suggested_app.title) + '?',
body_icon: suggested_app.icon,
buttons:[
{
label: i18n('yes'),
type: 'primary',
value: 'yes'
},
{
label: i18n('no')
},
]
})
if((alert_resp) === 'yes'){
user_preferences['default_apps' + extension] = suggested_app.name;
window.mutate_user_preferences(user_preferences);
}
}
launch_app({
name: suggested_app.name,
file_path: $(el_item).attr('data-path'),
Expand Down
19 changes: 18 additions & 1 deletion src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ window.update_auth_data = (auth_token, user)=>{
window.mutate_user_preferences = function(user_preferences_delta) {
for (const [key, value] of Object.entries(user_preferences_delta)) {
// Don't wait for set to be done for better efficiency
puter.kv.set(`user_preferences.${key}`, String(value));
puter.kv.set(`user_preferences.${key}`, value);
}
// There may be syncing issues across multiple devices
update_user_preferences({ ...window.user_preferences, ...user_preferences_delta });
Expand Down Expand Up @@ -989,6 +989,10 @@ window.item_icon = async (fsentry)=>{
else if(fsentry.name.toLowerCase().endsWith('.psd')){
return {image: window.icons['file-psd.svg'], type: 'icon'};
}
// *.py
else if(fsentry.name.toLowerCase().endsWith('.py')){
return {image: window.icons['file-py.svg'], type: 'icon'};
}
// *.xlsx
else if(fsentry.name.toLowerCase().endsWith('.xlsx')){
return {image: window.icons['file-xlsx.svg'], type: 'icon'};
Expand Down Expand Up @@ -2031,6 +2035,7 @@ window.open_item = async function(options){
const is_shortcut = $(el_item).attr('data-is_shortcut') === '1';
const shortcut_to_path = $(el_item).attr('data-shortcut_to_path');
const associated_app_name = $(el_item).attr('data-associated_app_name');
const file_uid = $(el_item).attr('data-uid')
//----------------------------------------------------------------
// Is this a shortcut whose source is perma-deleted?
//----------------------------------------------------------------
Expand Down Expand Up @@ -2125,6 +2130,18 @@ window.open_item = async function(options){
}
}
//----------------------------------------------------------------
// Does the user have a preference for this file type?
//----------------------------------------------------------------
else if(user_preferences[`default_apps${path.extname(item_path).toLowerCase()}`]) {
launch_app({
name: user_preferences[`default_apps${path.extname(item_path).toLowerCase()}`],
file_path: item_path,
window_title: path.basename(item_path),
maximized: options.maximized,
file_uid: file_uid,
});
}
//----------------------------------------------------------------
// Is there an app associated with this item?
//----------------------------------------------------------------
else if(associated_app_name !== ''){
Expand Down
1 change: 1 addition & 0 deletions src/i18n/translations/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const en = {
change_username: "Change Username",
close_all_windows: "Close All Windows",
close_all_windows_and_log_out: 'Close Windows and Log Out',
change_allways_open_with: "Do you want to always open this type of file with",
color: 'Color',
confirm_account_for_free_referral_storage_c2a: 'Create an account and confirm your email address to receive 1 GB of free storage. Your friend will get 1 GB of free storage too.',
confirm_delete_multiple_items: 'Are you sure you want to permanently delete these items?',
Expand Down
1 change: 1 addition & 0 deletions src/i18n/translations/nb.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const nb = {
change_username: "Endre brukernavn",
close_all_windows: "Lukk alle vinduer",
close_all_windows_and_log_out: 'Lukk alle vinduer og logg ut',
change_allways_open_with: "Ønsker du å alltid åpne denne filtypen med",
color: "Farge",
confirm_account_for_free_referral_storage_c2a: "Opprett en konto og bekreft e-postadressen din for å motta 1 GB gratis lagringsplass. Din venn vil også få 1 GB gratis lagringsplass.",
confirm_delete_multiple_items: 'Er du sikker på at du vil slette disse elementene permanent?',
Expand Down

0 comments on commit 4981d1c

Please sign in to comment.