-
Notifications
You must be signed in to change notification settings - Fork 885
/
resourcemanager.go
129 lines (103 loc) · 3.49 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
121
122
123
124
125
126
127
128
129
// 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 loads a bitmap from file or resource identified by name, or an error if it could not be
// found. When bitmap is loaded, 96dpi is assumed.
//
// Deprecated: Newer applications should use BitmapForDPI.
func (rm *ResourceManager) Bitmap(name string) (*Bitmap, error) {
return rm.BitmapForDPI(name, 96)
}
// BitmapForDPI loads a bitmap from file or resource identified by name, or an error if it could
// not be found. When bitmap is loaded, given DPI is assumed.
func (rm *ResourceManager) BitmapForDPI(name string, dpi int) (*Bitmap, error) {
if bm := rm.bitmaps[name]; bm != nil {
return bm, nil
}
if bm, err := NewBitmapFromFileForDPI(filepath.Join(rm.rootDirPath, name), dpi); err == nil {
rm.bitmaps[name] = bm
return bm, nil
}
if bm, err := NewBitmapFromResourceForDPI(name, dpi); err == nil {
rm.bitmaps[name] = bm
return bm, nil
}
if id, err := strconv.Atoi(name); err == nil {
if bm, err := NewBitmapFromResourceIdForDPI(id, dpi); 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 newError(fmt.Sprintf("neither %s resource '%s' nor file '%s' could be found or the image format is not supported", typ, name, path))
}