diff options
author | tfarina@chromium.org <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-06-22 02:00:32 +0000 |
---|---|---|
committer | tfarina@chromium.org <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-06-22 02:00:32 +0000 |
commit | 58f343515fad33fa6fe096c8049bd91743f76c4e (patch) | |
tree | 2b109320c307f035d93a6ce540a2529e76fc7487 /ui/gfx/mac | |
parent | 0b70acc5aff4408dd5dd62c5fb2912db42c76eb3 (diff) | |
download | chromium_src-58f343515fad33fa6fe096c8049bd91743f76c4e.zip chromium_src-58f343515fad33fa6fe096c8049bd91743f76c4e.tar.gz chromium_src-58f343515fad33fa6fe096c8049bd91743f76c4e.tar.bz2 |
Move app/mac/ files to ui/gfx/mac/ directory.
BUG=72317
TEST=None
R=rsesek@chromium.org
Review URL: http://codereview.chromium.org/7200045
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@89956 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/gfx/mac')
-rw-r--r-- | ui/gfx/mac/nsimage_cache.h | 33 | ||||
-rw-r--r-- | ui/gfx/mac/nsimage_cache.mm | 73 | ||||
-rw-r--r-- | ui/gfx/mac/nsimage_cache_unittest.cc.README | 3 | ||||
-rw-r--r-- | ui/gfx/mac/scoped_ns_disable_screen_updates.h | 35 |
4 files changed, 144 insertions, 0 deletions
diff --git a/ui/gfx/mac/nsimage_cache.h b/ui/gfx/mac/nsimage_cache.h new file mode 100644 index 0000000..83179d2 --- /dev/null +++ b/ui/gfx/mac/nsimage_cache.h @@ -0,0 +1,33 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef UI_GFX_MAC_NSIMAGE_CACHE_H_ +#define UI_GFX_MAC_NSIMAGE_CACHE_H_ +#pragma once + +#ifdef __OBJC__ +@class NSImage; +@class NSString; +#else +class NSImage; +class NSString; +#endif + +namespace gfx { + +// Returns an autoreleased image from the main app bundle +// (mac_util::MainAppBundle()) with the given name, and keeps it in memory so +// future fetches are fast. +// NOTE: +// - This should only be called on the main thread. +// - The caller should retain the image if they want to keep it around, as +// the cache could have limit on size/lifetime, etc. +NSImage* GetCachedImageWithName(NSString* name); + +// Clears the image cache. +void ClearCachedImages(void); + +} // namespace gfx + +#endif // UI_GFX_MAC_NSIMAGE_CACHE_H_ diff --git a/ui/gfx/mac/nsimage_cache.mm b/ui/gfx/mac/nsimage_cache.mm new file mode 100644 index 0000000..e7bb6e3 --- /dev/null +++ b/ui/gfx/mac/nsimage_cache.mm @@ -0,0 +1,73 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "ui/gfx/mac/nsimage_cache.h" + +#import <AppKit/AppKit.h> + +#include "base/logging.h" +#include "base/mac/mac_util.h" + +// When C++ exceptions are disabled, the C++ library defines |try| and +// |catch| so as to allow exception-expecting C++ code to build properly when +// language support for exceptions is not present. These macros interfere +// with the use of |@try| and |@catch| in Objective-C files such as this one. +// Undefine these macros here, after everything has been #included, since +// there will be no C++ uses and only Objective-C uses from this point on. +#undef try +#undef catch + +namespace gfx { + +static NSMutableDictionary* image_cache = nil; + +NSImage* GetCachedImageWithName(NSString* name) { + DCHECK(name); + + // NOTE: to make this thread safe, we'd have to sync on the cache and + // also force all the bundle calls on the main thread. + + if (!image_cache) { + image_cache = [[NSMutableDictionary alloc] init]; + DCHECK(image_cache); + } + + NSImage* result = [image_cache objectForKey:name]; + if (!result) { + DVLOG_IF(1, [[name pathExtension] length] == 0) << "Suggest including the " + "extension in the image name"; + + NSString* path = [base::mac::MainAppBundle() pathForImageResource:name]; + if (path) { + @try { + result = [[[NSImage alloc] initWithContentsOfFile:path] autorelease]; + if (result) { + // Auto-template images with names ending in "Template". + NSString* extensionlessName = [name stringByDeletingPathExtension]; + if ([extensionlessName hasSuffix:@"Template"]) + [result setTemplate:YES]; + + [image_cache setObject:result forKey:name]; + } + } + @catch (id err) { + DLOG(ERROR) << "Failed to load the image for name '" + << [name UTF8String] << "' from path '" << [path UTF8String] + << "', error: " << [[err description] UTF8String]; + result = nil; + } + } + } + + // TODO: if we ever limit the cache size, this should retain & autorelease + // the image. + return result; +} + +void ClearCachedImages(void) { + // NOTE: to make this thread safe, we'd have to sync on the cache. + [image_cache removeAllObjects]; +} + +} // namespace gfx diff --git a/ui/gfx/mac/nsimage_cache_unittest.cc.README b/ui/gfx/mac/nsimage_cache_unittest.cc.README new file mode 100644 index 0000000..558426f --- /dev/null +++ b/ui/gfx/mac/nsimage_cache_unittest.cc.README @@ -0,0 +1,3 @@ +The unit test for this file is in +chrome/browser/ui/cocoa/nsimage_cache_unittest.mm since it uses certain Chrome +resources for the test. diff --git a/ui/gfx/mac/scoped_ns_disable_screen_updates.h b/ui/gfx/mac/scoped_ns_disable_screen_updates.h new file mode 100644 index 0000000..310672a --- /dev/null +++ b/ui/gfx/mac/scoped_ns_disable_screen_updates.h @@ -0,0 +1,35 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef UI_GFX_MAC_SCOPED_NS_DISABLE_SCREEN_UPDATES_H_ +#define UI_GFX_MAC_SCOPED_NS_DISABLE_SCREEN_UPDATES_H_ +#pragma once + +#import <Cocoa/Cocoa.h> + +#include "base/basictypes.h" + +namespace gfx { + +// A stack-based class to disable Cocoa screen updates. When instantiated, it +// disables screen updates and enables them when destroyed. Update disabling +// can be nested, and there is a time-maximum (about 1 second) after which +// Cocoa will automatically re-enable updating. This class doesn't attempt to +// overrule that. +class ScopedNSDisableScreenUpdates { + public: + ScopedNSDisableScreenUpdates() { + NSDisableScreenUpdates(); + } + ~ScopedNSDisableScreenUpdates() { + NSEnableScreenUpdates(); + } + + private: + DISALLOW_COPY_AND_ASSIGN(ScopedNSDisableScreenUpdates); +}; + +} // namespace gfx + +#endif // UI_GFX_MAC_SCOPED_NS_DISABLE_SCREEN_UPDATES_H_ |