summaryrefslogtreecommitdiffstats
path: root/macosx/HBPresetsMenuBuilder.m
blob: 931d8c6fae569585a81b927475200f771c223fed (plain)
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
/*  HBPresetsMenuBuilder.m $

 This file is part of the HandBrake source code.
 Homepage: <http://handbrake.fr/>.
 It may be used under the terms of the GNU General Public License. */

#import "HBPresetsMenuBuilder.h"

@interface HBPresetsMenuBuilder ()

@property (nonatomic, readonly) CGFloat size;
@property (nonatomic, readonly) HBPresetsManager *manager;
@property (nonatomic, readonly) SEL action;

@end

@implementation HBPresetsMenuBuilder

- (instancetype)initWithMenu:(NSMenu *)menu action:(SEL)action size:(CGFloat)size presetsManager:(HBPresetsManager *)manager
{
    self = [super init];
    if (self)
    {
        _menu = menu;
        _size = size;
        _manager = manager;
        _action = action;

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(build) name:HBPresetsChangedNotification object:nil];
    }

    return self;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

/**
 *  Adds the presets list to the menu.
 */
- (void)build
{
    // First we remove all the preset menu items
    // inserted previously
    NSArray *menuItems = [self.menu.itemArray copy];
    for (NSMenuItem *item in menuItems)
    {
        if (item.tag == 2)
        {
            [self.menu removeItem:item];
        }
    }

    BOOL builtInSeparatorInserted = NO;
    for (HBPreset *preset in self.manager.root.children)
    {
        if (preset.isBuiltIn == NO && builtInSeparatorInserted == NO)
        {
            [self.menu addItem:[NSMenuItem separatorItem]];
            builtInSeparatorInserted = YES;
        }
        [self.menu addItem:[self buildMenuItemWithPreset:preset]];
    }
}

- (NSMenuItem *)buildMenuItemWithPreset:(HBPreset *)preset
{
    NSMenuItem *item = [[NSMenuItem alloc] init];
    item.title = preset.name;
    item.toolTip = preset.presetDescription;
    item.tag = 2;

    if (preset.isLeaf)
    {
        item.action = self.action;
        item.representedObject = preset;

        // Make the default preset font bold.
        if ([preset isEqualTo:self.manager.defaultPreset])
        {
            NSAttributedString *newTitle = [[NSAttributedString alloc] initWithString:preset.name
                                                                           attributes:@{NSFontAttributeName: [NSFont boldSystemFontOfSize:self.size]}];
            [item setAttributedTitle:newTitle];
        }
    }
    else
    {
        NSMenu *menu = [[NSMenu alloc] init];
        for (HBPreset *childPreset in preset.children)
        {
            [menu addItem:[self buildMenuItemWithPreset:childPreset]];
        }

        item.submenu = menu;
    }

    return item;
}

@end