Repository navigation
Expand file tree
/
Copy pathtabview.m
More file actions
196 lines (171 loc) · 6.13 KB
/
Copy pathtabview.m
File metadata and controls
196 lines (171 loc) · 6.13 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#pragma mark - Native tab containers
static void configure_segmented_control(NSSegmentedControl *selector) {
selector.trackingMode = NSSegmentSwitchTrackingSelectOne;
selector.segmentDistribution = NSSegmentDistributionFillEqually;
selector.segmentStyle = NSSegmentStyleAutomatic;
selector.borderShape = NSControlBorderShapeCapsule;
}
@interface LuaTabViewDelegate : NSObject <NSTabViewDelegate>
@property (nonatomic, strong) LuaReg *selection;
@end
@implementation LuaTabViewDelegate
- (void)dealloc {
[_selection dispose];
}
- (void)tabView:(NSTabView *)tabView didSelectTabViewItem:(NSTabViewItem *)tabViewItem {
lua_State *L = lua_reg_live_state(_selection);
if (!L) return;
int top = lua_gettop(L);
if (!lua_reg_push(_selection)) return;
push_objc(L, tabView, "nsview");
lua_pushinteger(L, [tabView indexOfTabViewItem:tabViewItem]);
NSString *ident = tabViewItem.identifier;
lua_pushstring(L, ident ? ident.UTF8String : "");
if ([tabViewItem.view isKindOfClass:[NSView class]]) {
push_objc(L, tabViewItem.view, "nsview");
} else {
lua_pushnil(L);
}
if (lua_pcall(L, 4, 0, 0) != LUA_OK) {
fprintf(stderr, "tab selection error: %s\n", lua_tostring(L, -1));
lua_pop(L, 1);
}
lua_settop(L, top);
}
@end
/* NSTabView owns selection and pane frames; bridge stacks need their layout
* pass after that native lifecycle finishes, including keyboard selection. */
/* macOS has no tab-bar accessory (SwiftUI's tabViewBottomAccessory is iOS
* only), so the view is retained for its template refs but not displayed;
* Mac apps surface the same state in the toolbar or their content. */
@interface LuaTabView : NSTabView
@property(nonatomic, strong) NSView *accessoryView;
@property(nonatomic) BOOL accessoryHidden;
@end
@implementation LuaTabView
- (void)layout {
[super layout];
NSView *content = self.selectedTabViewItem.view;
if (content) layout_recursive(content, content.bounds.size.width);
}
@end
static int bridge_tabview(lua_State *L) {
CGFloat width = luaL_optnumber(L, 1, 400);
CGFloat height = luaL_optnumber(L, 2, 200);
const char *style = luaL_optstring(L, 3, "top");
static NameValueEntry TabStyleMap[] = {
{@"top", NSTopTabsBezelBorder},
{@"bottom", NSBottomTabsBezelBorder},
{@"notabs", NSNoTabsNoBorder},
{nil, -1}
};
NSString *styleStr = [NSString stringWithUTF8String:style];
NSInteger tabType = lookupNameValue(styleStr, TabStyleMap, -1);
if (tabType < 0)
return luaL_error(
L, "tab view style must be 'top', 'bottom', or 'notabs'");
NSTabView *tv = [[LuaTabView alloc] initWithFrame:
NSMakeRect(0, 0, width, height)];
tv.tabViewType = (NSTabViewType)tabType;
/* A tab container owns its content area, just like UITabBarController.
* Its construction frame is not an intrinsic content size. */
objc_setAssociatedObject(tv, &kKeys[kFlexibleKey], @YES,
OBJC_ASSOCIATION_RETAIN);
push_objc(L, tv, "nsview");
return 1;
}
/* ─── NSTabView method implementations ───────────────────────────────── */
static int bridge_NSTabView_addTab_impl(lua_State *L, NSTabView *self,
const char *title,
NSView *content) {
NSString *titleStr = [NSString stringWithUTF8String:title];
NSTabViewItem *item = [[NSTabViewItem alloc]
initWithIdentifier:[NSUUID UUID].UUIDString];
item.label = titleStr;
item.view = content;
[self addTabViewItem:item];
push_objc(L, item, "nsobject");
return 1;
}
static int bridge_NSTabView_removeTab_impl(lua_State *L, NSTabView *self,
NSInteger index) {
if (index >= 0 && index < (NSInteger)self.numberOfTabViewItems) {
NSTabViewItem *item = [self tabViewItemAtIndex:index];
[self removeTabViewItem:item];
}
return 0;
}
static int bridge_NSTabView_selectTab_impl(lua_State *L, NSTabView *self,
NSInteger index) {
if (index >= 0 && index < (NSInteger)self.numberOfTabViewItems) {
[self selectTabViewItemAtIndex:index];
}
return 0;
}
static int bridge_NSTabView_tabCount_impl(lua_State *L, NSTabView *self) {
lua_pushinteger(L, (lua_Integer)self.numberOfTabViewItems);
return 1;
}
static int bridge_NSTabView_onChange_impl(lua_State *L, NSTabView *self,
LuaReg *callback) {
LuaTabViewDelegate *existing = objc_getAssociatedObject(
self, &kKeys[kTabViewDelegateKey]);
if (existing) {
self.delegate = nil;
objc_setAssociatedObject(self, &kKeys[kTabViewDelegateKey], nil,
OBJC_ASSOCIATION_RETAIN);
}
if (callback) {
LuaTabViewDelegate *delegate = [[LuaTabViewDelegate alloc] init];
delegate.selection = callback;
self.delegate = delegate;
objc_setAssociatedObject(self, &kKeys[kTabViewDelegateKey],
delegate, OBJC_ASSOCIATION_RETAIN);
}
return 0;
}
#pragma mark - NSSegmentedControl bridge
static int bridge_segmented_control(lua_State *L) {
luaL_checktype(L, 1, LUA_TTABLE);
NSInteger selectedIdx = (NSInteger)luaL_optinteger(L, 2, 0);
LuaReg *reg = lua_reg_opt(L, 3);
NSInteger count = (NSInteger)luaL_len(L, 1);
if (count < 1) {
lua_pushnil(L);
return 1;
}
NSSegmentedControl *seg = [[NSSegmentedControl alloc]
initWithFrame:NSMakeRect(0, 0, (CGFloat)count * 28, 28)];
seg.segmentCount = count;
configure_segmented_control(seg);
NSImageSymbolConfiguration *symConfig =
[NSImageSymbolConfiguration configurationWithPointSize:13
weight:NSFontWeightMedium];
for (NSInteger i = 0; i < count; i++) {
lua_rawgeti(L, 1, (int)(i + 1));
NSArray *pair = lua_to_objc_value(L, -1);
lua_pop(L, 1);
NSString *symbol = pair[0];
NSString *tip = pair[1];
[seg setWidth:28 forSegment:i];
if (symbol) {
NSImage *img = [NSImage imageWithSystemSymbolName:symbol
accessibilityDescription:nil];
if (img) {
img = [img imageWithSymbolConfiguration:symConfig];
[seg setImage:img forSegment:i];
}
}
if (tip) {
[seg setToolTip:tip forSegment:i];
}
[seg setSelected:(i == selectedIdx) forSegment:i];
}
if (reg) {
lua_reg_store(seg, &kKeys[kCallbackKey], reg);
seg.target = [LuaButtonTarget shared];
seg.action = @selector(onAction:);
}
push_objc(L, seg, "nsview");
return 1;
}