cocoawebview 0.7.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: 1eaad92103199618abce2cb57159fe668e574ec4027c6f3b5f5a1d509630bc42
4
- data.tar.gz: 95871ddbcef856aae806203c7b30eb42b23a7ff7769e88e0e1a297bda6df3a17
3
+ metadata.gz: 52d8ec84cc6c9e3502412e3db650e3c0c352606bf688589ae3fbeb72aac15aac
4
+ data.tar.gz: 25bf44fb2a4f47d861ad1a3ed530065e5f71433fc2a1209616160626ccac3777
5
5
  SHA512:
6
- metadata.gz: efc2abd762985a17d26b50021e98cd4ee78d5db1d2080794376f09331643e0119cb901f7937bd581a9eae586428b77c70b23b3f61a018ec199c6cd8d56114c48
7
- data.tar.gz: a77f8969e8b8f647487485cdf3b2098fff4e5f0cfe26b6f16da03776b5d5576990ee3b3b2755bd3f2db5ddc838d66b2ce89076151a0fd66f992db4863e4b042f
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,6 +12,14 @@ VALUE nsapp_initialize(VALUE self);
11
12
  VALUE nsapp_run(VALUE self);
12
13
  VALUE nsapp_exit(VALUE self);
13
14
 
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
+
14
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);
@@ -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
  }
@@ -306,6 +347,16 @@ static const rb_data_type_t cocoawebview_obj_type = {
306
347
  0, 0, RUBY_TYPED_FREE_IMMEDIATELY
307
348
  };
308
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
+
309
360
 
310
361
  RUBY_FUNC_EXPORTED void
311
362
  Init_cocoawebview(void)
@@ -318,6 +369,17 @@ Init_cocoawebview(void)
318
369
  rb_define_method(rb_mNSAppClass, "run", nsapp_run, 0);
319
370
  rb_define_method(rb_mNSAppClass, "exit", nsapp_exit, 0);
320
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
+
321
383
  /* CocoaWebview */
322
384
  rb_mCocoaWebviewClass = rb_define_class_under(rb_mCocoawebview, "CocoaWebview", rb_cObject);
323
385
  rb_define_method(rb_mCocoaWebviewClass, "initialize", webview_initialize, 5);
@@ -355,6 +417,81 @@ VALUE nsapp_exit(VALUE self) {
355
417
  [[NSApplication sharedApplication] terminate:nil];
356
418
  }
357
419
 
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
+
358
495
  VALUE webview_initialize(VALUE self, VALUE debug, VALUE style, VALUE move_title_buttons, VALUE delta_y, VALUE hide_title_bar) {
359
496
  rb_iv_set(self, "@var", rb_hash_new());
360
497
  rb_iv_set(self, "@bindings", rb_hash_new());
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Cocoawebview
4
- VERSION = "0.7.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
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.7.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: