Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add icon source #1400

Merged
merged 2 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions profile/binmeta/find_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ func GetIconAndName(ctx context.Context, binPath string, homeDir string) (icon *
}

return &Icon{
Type: IconTypeAPI,
Value: filename,
Type: IconTypeAPI,
Value: filename,
Source: IconSourceCore,
}, name, nil
}

Expand Down
43 changes: 37 additions & 6 deletions profile/binmeta/icon.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import (

// Icon describes an icon.
type Icon struct {
Type IconType
Value string
Type IconType
Value string
Source IconSource
}

// IconType describes the type of an Icon.
Expand All @@ -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:
Expand All @@ -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")
Expand Down
5 changes: 3 additions & 2 deletions profile/binmeta/icons.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@ func LoadAndSaveIcon(ctx context.Context, iconPath string) (*Icon, error) {
return nil, fmt.Errorf("failed to import icon %s: %w", iconPath, err)
}
return &Icon{
Type: IconTypeAPI,
Value: filename,
Type: IconTypeAPI,
Value: filename,
Source: IconSourceCore,
}, nil
}

Expand Down
5 changes: 3 additions & 2 deletions sync/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,9 @@ func ImportProfile(r *ProfileImportRequest, requiredProfileSource profile.Profil
return nil, fmt.Errorf("%w: icon is invalid: %w", ErrImportFailed, err)
}
p.Icons = []binmeta.Icon{{
Type: binmeta.IconTypeAPI,
Value: filename,
Type: binmeta.IconTypeAPI,
Value: filename,
Source: binmeta.IconSourceImport,
}}
}

Expand Down
Loading