mirror of
https://github.com/safing/portmaster
synced 2025-09-02 10:39:22 +00:00
Add icon source
This commit is contained in:
parent
496016b810
commit
f7f64e6b46
4 changed files with 46 additions and 12 deletions
|
@ -37,6 +37,7 @@ func GetIconAndName(ctx context.Context, binPath string, homeDir string) (icon *
|
|||
return &Icon{
|
||||
Type: IconTypeAPI,
|
||||
Value: filename,
|
||||
Source: IconSourceCore,
|
||||
}, name, nil
|
||||
}
|
||||
|
||||
|
|
|
@ -17,6 +17,7 @@ import (
|
|||
type Icon struct {
|
||||
Type IconType
|
||||
Value string
|
||||
Source IconSource
|
||||
}
|
||||
|
||||
// IconType describes the type of an Icon.
|
||||
|
@ -38,16 +39,46 @@ func (t IconType) sortOrder() int {
|
|||
case IconTypeFile:
|
||||
return 3
|
||||
default:
|
||||
return 100
|
||||
return 9
|
||||
}
|
||||
}
|
||||
|
||||
// IconSource describes the source of an Icon.
|
||||
type IconSource string
|
||||
|
||||
// Supported icon sources.
|
||||
const (
|
||||
IconSourceUser IconSource = "user"
|
||||
IconSourceImport IconSource = "import"
|
||||
IconSourceUI IconSource = "ui"
|
||||
IconSourceCore IconSource = "core"
|
||||
)
|
||||
|
||||
func (s IconSource) sortOrder() int {
|
||||
switch s {
|
||||
case IconSourceUser:
|
||||
return 10
|
||||
case IconSourceImport:
|
||||
return 20
|
||||
case IconSourceUI:
|
||||
return 30
|
||||
case IconSourceCore:
|
||||
return 40
|
||||
default:
|
||||
return 90
|
||||
}
|
||||
}
|
||||
|
||||
func (icon Icon) sortOrder() int {
|
||||
return icon.Source.sortOrder() + icon.Type.sortOrder()
|
||||
}
|
||||
|
||||
// SortAndCompactIcons sorts and compacts a list of icons.
|
||||
func SortAndCompactIcons(icons []Icon) []Icon {
|
||||
// Sort.
|
||||
slices.SortFunc[[]Icon, Icon](icons, func(a, b Icon) int {
|
||||
aOrder := a.Type.sortOrder()
|
||||
bOrder := b.Type.sortOrder()
|
||||
aOrder := a.sortOrder()
|
||||
bOrder := b.sortOrder()
|
||||
|
||||
switch {
|
||||
case aOrder != bOrder:
|
||||
|
@ -68,7 +99,7 @@ func SortAndCompactIcons(icons []Icon) []Icon {
|
|||
}
|
||||
|
||||
// GetIconAsDataURL returns the icon data as a data URL.
|
||||
func (icon *Icon) GetIconAsDataURL() (bloburl string, err error) {
|
||||
func (icon Icon) GetIconAsDataURL() (bloburl string, err error) {
|
||||
switch icon.Type {
|
||||
case IconTypeFile:
|
||||
return "", errors.New("getting icon from file is not supported")
|
||||
|
|
|
@ -94,6 +94,7 @@ func LoadAndSaveIcon(ctx context.Context, iconPath string) (*Icon, error) {
|
|||
return &Icon{
|
||||
Type: IconTypeAPI,
|
||||
Value: filename,
|
||||
Source: IconSourceCore,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
|
|
@ -422,6 +422,7 @@ func ImportProfile(r *ProfileImportRequest, requiredProfileSource profile.Profil
|
|||
p.Icons = []binmeta.Icon{{
|
||||
Type: binmeta.IconTypeAPI,
|
||||
Value: filename,
|
||||
Source: binmeta.IconSourceImport,
|
||||
}}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue