forked from lxn/walk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resourcemanager.go
120 lines (95 loc) · 3.06 KB
/
resourcemanager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright 2017 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build windows
package walk
import (
"fmt"
"os"
"path/filepath"
"strconv"
)
func init() {
Resources.rootDirPath, _ = os.Getwd()
Resources.bitmaps = make(map[string]*Bitmap)
Resources.icons = make(map[string]*Icon)
}
// Resources is the singleton instance of ResourceManager.
var Resources ResourceManager
// ResourceManager is a cache for sharing resources like bitmaps and icons.
// The resources can be either embedded in the running executable
// file or located below a specified root directory in the file system.
type ResourceManager struct {
rootDirPath string
bitmaps map[string]*Bitmap
icons map[string]*Icon
}
// RootDirPath returns the root directory path where resources are to be loaded from.
func (rm *ResourceManager) RootDirPath() string {
return rm.rootDirPath
}
// SetRootDirPath sets the root directory path where resources are to be loaded from.
func (rm *ResourceManager) SetRootDirPath(rootDirPath string) error {
path, err := filepath.Abs(rootDirPath)
if err != nil {
return err
}
rm.rootDirPath = path
return nil
}
// Bitmap returns the Bitmap identified by name, or an error if it could not be found.
func (rm *ResourceManager) Bitmap(name string) (*Bitmap, error) {
if bm := rm.bitmaps[name]; bm != nil {
return bm, nil
}
if bm, err := NewBitmapFromFile(filepath.Join(rm.rootDirPath, name)); err == nil {
rm.bitmaps[name] = bm
return bm, nil
}
if bm, err := NewBitmapFromResource(name); err == nil {
rm.bitmaps[name] = bm
return bm, nil
}
if id, err := strconv.Atoi(name); err == nil {
if bm, err := NewBitmapFromResourceId(id); err == nil {
rm.bitmaps[name] = bm
return bm, nil
}
}
return nil, rm.notFoundErr("bitmap", name)
}
// Icon returns the Icon identified by name, or an error if it could not be found.
func (rm *ResourceManager) Icon(name string) (*Icon, error) {
if icon := rm.icons[name]; icon != nil {
return icon, nil
}
if icon, err := NewIconFromFile(filepath.Join(rm.rootDirPath, name)); err == nil {
rm.icons[name] = icon
return icon, nil
}
if icon, err := NewIconFromResource(name); err == nil {
rm.icons[name] = icon
return icon, nil
}
if id, err := strconv.Atoi(name); err == nil {
if icon, err := NewIconFromResourceId(id); err == nil {
rm.icons[name] = icon
return icon, nil
}
}
return nil, rm.notFoundErr("icon", name)
}
// Image returns the Image identified by name, or an error if it could not be found.
func (rm *ResourceManager) Image(name string) (Image, error) {
if icon, err := rm.Icon(name); err == nil {
return icon, nil
}
if bm, err := rm.Bitmap(name); err == nil {
return bm, nil
}
return nil, rm.notFoundErr("image", name)
}
func (rm *ResourceManager) notFoundErr(typ, name string) error {
path := filepath.Clean(filepath.Join(rm.rootDirPath, name))
return newErr(fmt.Sprintf("neither %s resource '%s' nor file '%s' could be found or the image format is not supported", typ, name, path))
}