Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(703)

Side by Side Diff: ui/gfx/decorated_text_mac.mm

Issue 2164483006: [MacViews] Implemented text context menu (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed tapted's comments and made things work Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "ui/gfx/decorated_text_mac.h"
6
7 #import <Cocoa/Cocoa.h>
8
9 #import "base/mac/scoped_nsobject.h"
10 #include "base/strings/sys_string_conversions.h"
11
12 namespace gfx {
13
14 NSAttributedString* GetAttributedStringFromDecoratedText(
15 const DecoratedText& decorated_text) {
16 base::scoped_nsobject<NSMutableAttributedString> str(
17 [[NSMutableAttributedString alloc]
18 initWithString:base::SysUTF16ToNSString(decorated_text.text)]);
19 [str beginEditing];
20
21 NSValue* const line_style =
22 @(NSUnderlineStyleSingle | NSUnderlinePatternSolid);
23
24 for (const auto& attribute : decorated_text.attributes) {
25 DCHECK(!attribute.range.is_reversed());
26 DCHECK_LE(attribute.range.end(), [str length]);
27
28 NSMutableDictionary* attrs = [NSMutableDictionary dictionary];
29 NSRange range = attribute.range.ToNSRange();
30
31 if (attribute.font.GetNativeFont())
32 attrs[NSFontAttributeName] = attribute.font.GetNativeFont();
33
34 // NSFont does not have underline as an attribute. Hence handle it
35 // separately.
36 const bool underline = attribute.font.GetStyle() & gfx::Font::UNDERLINE;
37 if (underline)
38 attrs[NSUnderlineStyleAttributeName] = line_style;
39
40 if (attribute.strike)
41 attrs[NSStrikethroughStyleAttributeName] = line_style;
42
43 [str setAttributes:attrs range:range];
44 }
45
46 [str endEditing];
47 return str.autorelease();
48 }
49
50 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698