cocoawebview 0.6.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 17ab44091cb53d27e1f18d689ffbcac46b653c94ad06e96335dc85b1fc82238c
4
- data.tar.gz: ec4604b5e8a24b9839c7ff8ee5213d2869b64043f0f2c9e4beafc57ee9324e97
3
+ metadata.gz: 52d8ec84cc6c9e3502412e3db650e3c0c352606bf688589ae3fbeb72aac15aac
4
+ data.tar.gz: 25bf44fb2a4f47d861ad1a3ed530065e5f71433fc2a1209616160626ccac3777
5
5
  SHA512:
6
- metadata.gz: 8b59a45780e4c7c93ad20e5ee3aaa191dd3991d4a887207cf052491e7e1748f6de17e771bcc0902b0d8cdb30ca681c76c4a72add3b31035e8aa35054602323cf
7
- data.tar.gz: 5add581b2a0cf749a41d66bd6eee0502b82fcf17b5b62fe87cefcdd9f933a07de48d9e9ec9ab17ba9a2df4eb5d0aa9f56aa99e31ccceed94e6bf1a45053b90c9
6
+ metadata.gz: e85e7cf20d08cd51b562c5bd4e9718f0c10e0e019d3d0c2fe219497b7a6e727471107f57a6865c8f8a797b24249874bde6eb36ebc8cbd3ae5b06c5394ce9c261
7
+ data.tar.gz: 23658409bdb2faa237762429a1d81ee19155ccd4d5e4298e8036062662ac52ec6e0da945e981e19d4d789df19bd83f919098122c13affa4a16d459d31c6894ba
@@ -4,6 +4,7 @@
4
4
 
5
5
  VALUE rb_mCocoawebview;
6
6
  VALUE rb_mNSAppClass = Qnil;
7
+ VALUE rb_mNSMenuClass = Qnil;
7
8
  VALUE rb_mCocoaWebviewClass = Qnil;
8
9
  NSApplication *application = nil;
9
10
 
@@ -11,7 +12,15 @@ VALUE nsapp_initialize(VALUE self);
11
12
  VALUE nsapp_run(VALUE self);
12
13
  VALUE nsapp_exit(VALUE self);
13
14
 
14
- VALUE webview_initialize(VALUE self, VALUE debug, VALUE style, VALUE move_title_buttons, VALUE delta_y);
15
+ VALUE nsmenu_initialize(VALUE self);
16
+ VALUE nsmenu_new_menu_item(VALUE self);
17
+ VALUE nsmenu_create_menu_item(VALUE self, VALUE title, VALUE tag, VALUE key);
18
+ VALUE nsmenu_new_menu(VALUE self);
19
+ VALUE nsmenu_add_item_to_menu(VALUE self, VALUE item, VALUE menu);
20
+ VALUE nsmenu_set_submenu_to_menu(VALUE self, VALUE submenu, VALUE menu);
21
+ VALUE nsmenu_show(VALUE self);
22
+
23
+ VALUE webview_initialize(VALUE self, VALUE debug, VALUE style, VALUE move_title_buttons, VALUE delta_y, VALUE hide_title_bar);
15
24
  VALUE webview_navigate(VALUE self, VALUE url);
16
25
  VALUE webview_show(VALUE self);
17
26
  VALUE webview_hide(VALUE self);
@@ -95,6 +104,17 @@ VALUE webview_increase_normal_level(VALUE self, VALUE delta);
95
104
  @end
96
105
 
97
106
 
107
+ @interface Menu : NSObject {
108
+
109
+ }
110
+
111
+ @property (nonatomic, strong) NSMenu *mainMenu;
112
+ @end
113
+
114
+ @implementation Menu
115
+
116
+ @end
117
+
98
118
  @interface AppDelegate : NSObject <NSApplicationDelegate> {
99
119
  VALUE app;
100
120
  }
@@ -102,6 +122,27 @@ VALUE webview_increase_normal_level(VALUE self, VALUE delta);
102
122
 
103
123
  @implementation AppDelegate
104
124
 
125
+ - (void)handleMenuAction:(id)sender {
126
+ NSMenuItem *item = (NSMenuItem *)sender;
127
+ NSInteger tag = [item tag];
128
+
129
+ // Route logic based on the tag
130
+ switch (tag) {
131
+ case 100: // New File
132
+ NSLog(@"Creating a new file...");
133
+ break;
134
+ case 101: // Save
135
+ NSLog(@"Saving data...");
136
+ break;
137
+ case 999: // Quit
138
+ [NSApp terminate:nil];
139
+ break;
140
+ default:
141
+ NSLog(@"Clicked: %@", [item title]);
142
+ break;
143
+ }
144
+ }
145
+
105
146
  - (void)setApp:(VALUE)a {
106
147
  app = a;
107
148
  }
@@ -132,7 +173,7 @@ VALUE webview_increase_normal_level(VALUE self, VALUE delta);
132
173
  - (void)setShouldMoveTitleButtons:(BOOL)flag;
133
174
  - (void)setDevTool:(BOOL)flag;
134
175
  - (void)setDeltaY:(int)dy;
135
- - (id)initWithFrame:(NSRect)frame debug:(BOOL)flag style:(int)style moveTitleButtons:(BOOL)moveTitleButtons deltaY:(int)dy;
176
+ - (id)initWithFrame:(NSRect)frame debug:(BOOL)flag style:(int)style moveTitleButtons:(BOOL)moveTitleButtons deltaY:(int)dy hideTitleBar:(BOOL)hideTitleBar;
136
177
  - (void)eval:(NSString*)code;
137
178
  - (void)navigate:(NSString*)url;
138
179
  - (void)setCocoaWebview:(VALUE)view;
@@ -140,7 +181,7 @@ VALUE webview_increase_normal_level(VALUE self, VALUE delta);
140
181
  @end
141
182
 
142
183
  @implementation CocoaWebview
143
- - (id)initWithFrame:(NSRect)frame debug:(BOOL)flag style:(int)style moveTitleButtons:(BOOL)moveTitleButtons deltaY:(int)dy {
184
+ - (id)initWithFrame:(NSRect)frame debug:(BOOL)flag style:(int)style moveTitleButtons:(BOOL)moveTitleButtons deltaY:(int)dy hideTitleBar:(BOOL)hideTitleBar{
144
185
  self = [super initWithContentRect:frame
145
186
  styleMask:style
146
187
  backing:NSBackingStoreBuffered
@@ -150,8 +191,12 @@ VALUE webview_increase_normal_level(VALUE self, VALUE delta);
150
191
  [self setTitle:@"My Custom Window"];
151
192
  [self setDevTool:flag];
152
193
  [self setDeltaY:dy];
153
- [self setTitlebarAppearsTransparent: YES];
154
- [self setTitleVisibility:NSWindowTitleHidden];
194
+ if (hideTitleBar) {
195
+ [self setTitlebarAppearsTransparent: YES];
196
+ [self setTitleVisibility:NSWindowTitleHidden];
197
+ } else {
198
+ [self setTitlebarAppearsTransparent: NO];
199
+ }
155
200
  [self addWebViewToWindow:self];
156
201
  [self setShouldMoveTitleButtons:moveTitleButtons];
157
202
  if (moveTitleButtons) {
@@ -302,6 +347,16 @@ static const rb_data_type_t cocoawebview_obj_type = {
302
347
  0, 0, RUBY_TYPED_FREE_IMMEDIATELY
303
348
  };
304
349
 
350
+ static void menu_obj_free(void *ptr) {
351
+
352
+ }
353
+
354
+ static const rb_data_type_t menu_obj_type = {
355
+ "MenuWrapper",
356
+ { 0, menu_obj_free, 0 },
357
+ 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
358
+ };
359
+
305
360
 
306
361
  RUBY_FUNC_EXPORTED void
307
362
  Init_cocoawebview(void)
@@ -314,9 +369,20 @@ Init_cocoawebview(void)
314
369
  rb_define_method(rb_mNSAppClass, "run", nsapp_run, 0);
315
370
  rb_define_method(rb_mNSAppClass, "exit", nsapp_exit, 0);
316
371
 
372
+ /* Menu */
373
+ rb_mNSMenuClass = rb_define_class_under(rb_mCocoawebview, "NSMenu", rb_cObject);
374
+ rb_define_method(rb_mNSMenuClass, "initialize", nsmenu_initialize, 0);
375
+ rb_define_method(rb_mNSMenuClass, "new_menu", nsmenu_new_menu, 0);
376
+ rb_define_method(rb_mNSMenuClass, "new_menu_item", nsmenu_new_menu_item, 0);
377
+ rb_define_method(rb_mNSMenuClass, "create_menu_item", nsmenu_create_menu_item, 3);
378
+ rb_define_method(rb_mNSMenuClass, "add_item_to_menu", nsmenu_add_item_to_menu, 2);
379
+ rb_define_method(rb_mNSMenuClass, "set_submenu_to_menu", nsmenu_set_submenu_to_menu, 2);
380
+ rb_define_method(rb_mNSMenuClass, "show", nsmenu_show, 0);
381
+
382
+
317
383
  /* CocoaWebview */
318
384
  rb_mCocoaWebviewClass = rb_define_class_under(rb_mCocoawebview, "CocoaWebview", rb_cObject);
319
- rb_define_method(rb_mCocoaWebviewClass, "initialize", webview_initialize, 4);
385
+ rb_define_method(rb_mCocoaWebviewClass, "initialize", webview_initialize, 5);
320
386
  rb_define_method(rb_mCocoaWebviewClass, "show", webview_show, 0);
321
387
  rb_define_method(rb_mCocoaWebviewClass, "hide", webview_hide, 0);
322
388
  rb_define_method(rb_mCocoaWebviewClass, "eval", webview_eval, 1);
@@ -351,7 +417,82 @@ VALUE nsapp_exit(VALUE self) {
351
417
  [[NSApplication sharedApplication] terminate:nil];
352
418
  }
353
419
 
354
- VALUE webview_initialize(VALUE self, VALUE debug, VALUE style, VALUE move_title_buttons, VALUE delta_y) {
420
+ VALUE nsmenu_initialize(VALUE self) {
421
+ rb_iv_set(self, "@var", rb_hash_new());
422
+ Menu *menu = [[Menu alloc] init];
423
+ menu.mainMenu = [NSMenu new];
424
+
425
+ // Wrap the Objective-C pointer into a Ruby object
426
+ VALUE wrapper = TypedData_Wrap_Struct(rb_cObject, &menu_obj_type, menu);
427
+
428
+ // Store the wrapper in an instance variable
429
+ rb_ivar_set(self, rb_intern("@menu"), wrapper);
430
+
431
+ VALUE wrapper2 = TypedData_Wrap_Struct(rb_cObject, &menu_obj_type, menu.mainMenu);
432
+ rb_ivar_set(self, rb_intern("@menu_bar"), wrapper2);
433
+
434
+ return self;
435
+ }
436
+
437
+ VALUE nsmenu_new_menu_item(VALUE self) {
438
+ NSMenuItem *menuItem = [NSMenuItem new];
439
+ // Wrap the Objective-C pointer into a Ruby object
440
+ VALUE wrapper = TypedData_Wrap_Struct(rb_cObject, &menu_obj_type, menuItem);
441
+ return wrapper;
442
+ }
443
+
444
+ VALUE nsmenu_create_menu_item(VALUE self, VALUE title, VALUE tag, VALUE key) {
445
+ int c_tag = NUM2INT(tag);
446
+ const char *title_c = StringValueCStr(title);
447
+ NSString *title_ns = [[NSString alloc] initWithCString:title_c encoding:NSUTF8StringEncoding];
448
+
449
+ const char *key_c = StringValueCStr(key);
450
+ NSString *key_ns = [[NSString alloc] initWithCString:key_c encoding:NSUTF8StringEncoding];
451
+
452
+ NSMenuItem *menuItem = [[NSMenuItem alloc] initWithTitle:title_ns
453
+ action:@selector(handleMenuAction:)
454
+ keyEquivalent:key_ns];
455
+
456
+ [menuItem setTag:c_tag];
457
+
458
+ VALUE wrapper = TypedData_Wrap_Struct(rb_cObject, &menu_obj_type, menuItem);
459
+ return wrapper;
460
+ }
461
+
462
+ VALUE nsmenu_new_menu(VALUE self) {
463
+ NSMenu *menu = [NSMenu new];
464
+ // Wrap the Objective-C pointer into a Ruby object
465
+ VALUE wrapper = TypedData_Wrap_Struct(rb_cObject, &menu_obj_type, menu);
466
+ return wrapper;
467
+ }
468
+
469
+ VALUE nsmenu_add_item_to_menu(VALUE self, VALUE item, VALUE menu) {
470
+ NSMenuItem *item_ns;
471
+ NSMenu *menu_ns;
472
+ TypedData_Get_Struct(item, NSMenuItem, &menu_obj_type, item_ns);
473
+ TypedData_Get_Struct(menu, NSMenu, &menu_obj_type, menu_ns);
474
+ [menu_ns addItem:item_ns];
475
+ }
476
+
477
+ VALUE nsmenu_set_submenu_to_menu(VALUE self, VALUE submenu, VALUE menu) {
478
+ NSMenu *submenu_ns;
479
+ NSMenu *menu_ns;
480
+ TypedData_Get_Struct(submenu, NSMenu, &menu_obj_type, submenu_ns);
481
+ TypedData_Get_Struct(menu, NSMenu, &menu_obj_type, menu_ns);
482
+ [menu_ns setSubmenu:submenu_ns];
483
+ }
484
+
485
+ VALUE nsmenu_show(VALUE self) {
486
+ NSApplication *app = [NSApplication sharedApplication];
487
+ [app setActivationPolicy:NSApplicationActivationPolicyRegular];
488
+
489
+ VALUE wrapper = rb_ivar_get(self, rb_intern("@menu"));
490
+ Menu *menu;
491
+ TypedData_Get_Struct(wrapper, Menu, &menu_obj_type, menu);
492
+ [app setMainMenu:menu.mainMenu];
493
+ }
494
+
495
+ VALUE webview_initialize(VALUE self, VALUE debug, VALUE style, VALUE move_title_buttons, VALUE delta_y, VALUE hide_title_bar) {
355
496
  rb_iv_set(self, "@var", rb_hash_new());
356
497
  rb_iv_set(self, "@bindings", rb_hash_new());
357
498
  BOOL flag = NO;
@@ -367,9 +508,15 @@ VALUE webview_initialize(VALUE self, VALUE debug, VALUE style, VALUE move_title_
367
508
  } else {
368
509
  c_move_title_buttons = NO;
369
510
  }
511
+ BOOL c_hide_title_bar = NO;
512
+ if (hide_title_bar == Qtrue) {
513
+ c_hide_title_bar = YES;
514
+ } else {
515
+ c_hide_title_bar = NO;
516
+ }
370
517
  int c_style = NUM2INT(style);
371
518
  int c_delta_y = NUM2INT(delta_y);
372
- CocoaWebview *webview = [[CocoaWebview alloc] initWithFrame:NSMakeRect(100, 100, 400, 500) debug:flag style:c_style moveTitleButtons:c_move_title_buttons deltaY:c_delta_y];
519
+ CocoaWebview *webview = [[CocoaWebview alloc] initWithFrame:NSMakeRect(100, 100, 400, 500) debug:flag style:c_style moveTitleButtons:c_move_title_buttons deltaY:c_delta_y hideTitleBar:c_hide_title_bar];
373
520
 
374
521
  [webview setReleasedWhenClosed:NO];
375
522
  [webview setCocoaWebview:self];
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Cocoawebview
4
- VERSION = "0.6.0"
4
+ VERSION = "0.8.0"
5
5
  end
data/lib/cocoawebview.rb CHANGED
@@ -14,6 +14,16 @@ module CocoaWebview
14
14
 
15
15
  class Error < StandardError; end
16
16
  # Your code goes here...
17
+
18
+ class NSMenu
19
+ def main_menu
20
+ @menu
21
+ end
22
+
23
+ def main_menu_bar
24
+ @menu_bar
25
+ end
26
+ end
17
27
 
18
28
  class NSApp
19
29
  def app_did_launch
@@ -32,16 +42,18 @@ module CocoaWebview
32
42
  class CocoaWebview
33
43
  attr_accessor :callback
34
44
 
35
- def self.create(debug: false, min: true, resize: true, close: true, move_title_buttons: false, delta_y: 10, &block)
45
+ def self.create(debug: false, min: true, resize: true, close: true, move_title_buttons: false, delta_y: 10, hide_title_bar: true, &block)
36
46
  style = NSWindowStyleMaskTitled | NSWindowStyleMaskFullSizeContentView
37
47
 
38
48
  style = style | NSWindowStyleMaskMiniaturizable if min
39
49
  style = style | NSWindowStyleMaskResizable if resize
40
50
  style = style | NSWindowStyleMaskClosable if close
41
51
 
42
- style &= ~NSWindowStyleMaskFullScreen
52
+ if hide_title_bar
53
+ style &= ~NSWindowStyleMaskFullScreen
54
+ end
43
55
 
44
- webview = new(debug, style, move_title_buttons, delta_y)
56
+ webview = new(debug, style, move_title_buttons, delta_y, hide_title_bar)
45
57
  webview.callback = block
46
58
  webview
47
59
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cocoawebview
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tommy Jeff
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2025-10-17 00:00:00.000000000 Z
10
+ date: 2026-02-06 00:00:00.000000000 Z
11
11
  dependencies: []
12
12
  description: Webview ruby binding for macOS
13
13
  email: