Skip to content

Commit 2b14ce3

Browse files
committed
Support registering and unregistering preferences
1 parent 2fb272d commit 2b14ce3

File tree

7 files changed

+167
-3
lines changed

7 files changed

+167
-3
lines changed

wasm-extension-go/pkg/api/api.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,16 @@ func OpenExternalUrl(url string) error {
375375
return err
376376
}
377377

378+
func RegisterUserPreference(prefs []types.PreferenceUIData) error {
379+
err := sendMainCommandOptional("RegisterUserPreference", prefs)
380+
return err
381+
}
382+
383+
func UnregisterUserPreference(prefs []string) error {
384+
err := sendMainCommandOptional("UnregisterUserPreference", prefs)
385+
return err
386+
}
387+
378388
// UpdateAccounts updates the accounts status.
379389
func UpdateAccounts(packageName string) error {
380390
err := sendMainCommandOptional("UpdateAccounts", packageName)

wasm-extension-go/pkg/api/exports.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,7 @@ func perform_account_login_wrapper() int32 {
335335
//go:wasmexport scrobble_wrapper
336336
func scrobble_wrapper() int32 {
337337
var song types.Song
338-
in := [...]any{&song}
339-
return runWrapper(&in, func() (any, error) {
338+
return runWrapper(&song, func() (any, error) {
340339
return nil, extension.Scrobble(song)
341340
})
342341
}

wasm-extension-go/pkg/types/types.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,43 @@ const (
234234
PlayerStateStopped PlayerState = "STOPPED"
235235
PlayerStateLoading PlayerState = "LOADING"
236236
)
237+
238+
type PreferenceTypes string
239+
240+
const (
241+
DirectoryGroup PreferenceTypes = "DirectoryGroup"
242+
EditText PreferenceTypes = "EditText"
243+
FilePicker PreferenceTypes = "FilePicker"
244+
CheckboxGroup PreferenceTypes = "CheckboxGroup"
245+
ThemeSelector PreferenceTypes = "ThemeSelector"
246+
Extensions PreferenceTypes = "Extensions"
247+
ButtonGroup PreferenceTypes = "ButtonGroup"
248+
ProgressBar PreferenceTypes = "ProgressBar"
249+
TextField PreferenceTypes = "TextField"
250+
InfoField PreferenceTypes = "InfoField"
251+
Dropdown PreferenceTypes = "Dropdown"
252+
)
253+
254+
type InputType string
255+
256+
const (
257+
Text InputType = "text"
258+
Number InputType = "number"
259+
)
260+
261+
type CheckboxItems struct {
262+
Title string `json:"title"`
263+
Key string `json:"key"`
264+
}
265+
266+
type PreferenceUIData struct {
267+
Type PreferenceTypes `json:"type"`
268+
Title string `json:"title"`
269+
Key string `json:"key"`
270+
Description string `json:"description"`
271+
InputType *InputType `json:"inputType,omitempty"`
272+
Single *bool `json:"single,omitempty"`
273+
Items *[]CheckboxItems `json:"items,omitempty"`
274+
Default any `json:"default,omitempty"`
275+
Mobile *bool `json:"mobile,omitempty"`
276+
}

wasm-extension-js/src/types.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,42 @@ export interface ContextMenuReturnType {
482482
action_id: string;
483483
}
484484

485+
export enum PreferenceTypes {
486+
DirectoryGroup = "DirectoryGroup",
487+
EditText = "EditText",
488+
FilePicker = "FilePicker",
489+
CheckboxGroup = "CheckboxGroup",
490+
ThemeSelector = "ThemeSelector",
491+
Extensions = "Extensions",
492+
ButtonGroup = "ButtonGroup",
493+
ProgressBar = "ProgressBar",
494+
TextField = "TextField",
495+
InfoField = "InfoField",
496+
Dropdown = "Dropdown",
497+
}
498+
499+
export enum InputType {
500+
Text = "text",
501+
Number = "number",
502+
}
503+
504+
export interface CheckboxItems {
505+
title: string;
506+
key: string;
507+
}
508+
509+
export interface PreferenceUIData {
510+
type: PreferenceTypes;
511+
title: string;
512+
key: string;
513+
description: string;
514+
inputType?: InputType;
515+
single?: boolean;
516+
items?: CheckboxItems[];
517+
default?: any;
518+
mobile?: boolean;
519+
}
520+
485521
/**
486522
* The API exposed to extensions
487523
*/
@@ -838,6 +874,18 @@ export interface ExtensionAPI {
838874
*/
839875
openExternalUrl(url: string): void;
840876

877+
/**
878+
* Registers user preferences to show in settings.
879+
* @param prefs An array of PreferenceUIData objects representing the preferences to register
880+
*/
881+
registerUserPreferences(prefs: PreferenceUIData[]): void;
882+
883+
/**
884+
* Unregisters user preferences from the main app.
885+
* @param pref_keys An array of preference keys to unregister
886+
*/
887+
unregisterUserPreferences(pref_keys: string[]): void;
888+
841889
/**
842890
* Updates the list of accounts in the main app.
843891
*/

wasm-extension-py/moosync_edk/__init__.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545

4646
class EnhancedJSONEncoder(json.JSONEncoder):
4747
def default(self, o):
48-
if dataclasses.is_dataclass(o):
48+
if dataclasses.is_dataclass(o) and not isinstance(o, type):
4949
return dataclasses.asdict(o)
5050
return super().default(o)
5151

@@ -329,6 +329,24 @@ def update_accounts(self) -> None:
329329
}
330330
send_main_command(json.dumps(data, cls=EnhancedJSONEncoder))
331331

332+
def register_user_preferences(self, prefs: List[PreferenceUIData]) -> None:
333+
"""
334+
Registered preferences will show up in settings
335+
"""
336+
data = {
337+
"RegisterUserPreference": prefs
338+
}
339+
send_main_command(json.dumps(data, cls=EnhancedJSONEncoder))
340+
341+
def unregister_user_preferences(self, pref_keys: List[str]) -> None:
342+
"""
343+
Removes preferences from settings
344+
"""
345+
data = {
346+
"UnregisterUserPreference": pref_keys
347+
}
348+
send_main_command(json.dumps(data, cls=EnhancedJSONEncoder))
349+
332350
class Extension:
333351
api = Api()
334352

wasm-extension-py/moosync_edk/custom_types.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from dataclasses import dataclass, field
22
from typing import Optional, List, Dict, Union, Any, Literal
33
import inspect
4+
from enum import Enum
45

56
ProviderScopes = Literal[
67
"search",
@@ -387,3 +388,37 @@ def from_dict(cls, data: dict) -> 'AddToPlaylistRequest':
387388
k: v for k, v in data.items()
388389
if k in inspect.signature(cls).parameters
389390
})
391+
392+
class PreferenceTypes(Enum):
393+
DirectoryGroup = "DirectoryGroup"
394+
EditText = "EditText"
395+
FilePicker = "FilePicker"
396+
CheckboxGroup = "CheckboxGroup"
397+
ThemeSelector = "ThemeSelector"
398+
Extensions = "Extensions"
399+
ButtonGroup = "ButtonGroup"
400+
ProgressBar = "ProgressBar"
401+
TextField = "TextField"
402+
InfoField = "InfoField"
403+
Dropdown = "Dropdown"
404+
405+
class InputType(Enum):
406+
Text = "text"
407+
Number = "number"
408+
409+
@dataclass(frozen=True)
410+
class CheckboxItems:
411+
title: str
412+
key: str
413+
414+
@dataclass
415+
class PreferenceUIData:
416+
type: 'PreferenceTypes' # Use string annotation for forward reference or import the actual class
417+
title: str
418+
key: str
419+
description: str
420+
inputType: Optional['InputType'] = None
421+
single: Optional[bool] = None
422+
items: Optional[List['CheckboxItems']] = None
423+
default: Optional[Any] = None # Replace Any with the actual Value type if available
424+
mobile: Optional[bool] = None

wasm-extension-rs/src/api.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,20 @@ pub mod extension_api {
439439
///
440440
/// * `package_name` - The optional package name to filter the accounts.
441441
update_accounts(UpdateAccounts, package_name: Option<String>) -> ();
442+
443+
/// Registers user preferences with the main app.
444+
///
445+
/// # Arguments
446+
///
447+
/// * `prefs` - A vector of `PreferenceUIData` representing the user preferences to register.
448+
register_user_preferences(RegisterUserPreference, prefs: Vec<PreferenceUIData>) -> ();
449+
450+
/// Unregisters user preferences from the main app.
451+
///
452+
/// # Arguments
453+
///
454+
/// * `pref_keys` - A vector of strings representing the keys of the preferences to unregister.
455+
unregister_user_preferences(UnregisterUserPreference, pref_keys: Vec<String>) -> ();
442456
}
443457

444458
pub fn get_system_time() -> u64 {

0 commit comments

Comments
 (0)