cocoawebview 0.7.0 → 0.9.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: 75f756929b44cbb524409c3e12fcfcb01cb6d7f6dea1b75581512f845f173708
4
+ data.tar.gz: 0af8a6e058870c1c52769c2d7880a2c93bd10574d7d32a88c578aaebdab33a80
5
5
  SHA512:
6
- metadata.gz: efc2abd762985a17d26b50021e98cd4ee78d5db1d2080794376f09331643e0119cb901f7937bd581a9eae586428b77c70b23b3f61a018ec199c6cd8d56114c48
7
- data.tar.gz: a77f8969e8b8f647487485cdf3b2098fff4e5f0cfe26b6f16da03776b5d5576990ee3b3b2755bd3f2db5ddc838d66b2ce89076151a0fd66f992db4863e4b042f
6
+ metadata.gz: 709899b589ec4865a007c4820d5463c63bfb89144230baa3811e99c488a0bc762c5a455971468283c9440425c004ed10f5379d73362feb54383a29970b3ca181
7
+ data.tar.gz: c1bf0c84074f29c21cf6b2bec3060c0683dec4cb153aae9183a15065eeeed9a6467900a29232941fc616e876699a17dd35f1c7ce824ef83877bd0bf6d5e05c09
@@ -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,17 +104,40 @@ 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;
120
+ VALUE rb_menu;
100
121
  }
101
122
  @end
102
123
 
103
124
  @implementation AppDelegate
104
125
 
126
+ - (void)handleMenuAction:(id)sender {
127
+ NSMenuItem *item = (NSMenuItem *)sender;
128
+ NSInteger tag = [item tag];
129
+ VALUE rb_tag = INT2NUM(tag);
130
+ rb_funcall(rb_menu, rb_intern("handle_menu_action"), 1, rb_tag);
131
+ }
132
+
105
133
  - (void)setApp:(VALUE)a {
106
134
  app = a;
107
135
  }
108
136
 
137
+ - (void)setMenu:(VALUE)m {
138
+ rb_menu = m;
139
+ }
140
+
109
141
  - (void)applicationWillTerminate:(NSNotification *)notification {
110
142
  rb_funcall(app, rb_intern("app_will_exit"), 0);
111
143
  }
@@ -306,6 +338,16 @@ static const rb_data_type_t cocoawebview_obj_type = {
306
338
  0, 0, RUBY_TYPED_FREE_IMMEDIATELY
307
339
  };
308
340
 
341
+ static void menu_obj_free(void *ptr) {
342
+
343
+ }
344
+
345
+ static const rb_data_type_t menu_obj_type = {
346
+ "MenuWrapper",
347
+ { 0, menu_obj_free, 0 },
348
+ 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
349
+ };
350
+
309
351
 
310
352
  RUBY_FUNC_EXPORTED void
311
353
  Init_cocoawebview(void)
@@ -318,6 +360,17 @@ Init_cocoawebview(void)
318
360
  rb_define_method(rb_mNSAppClass, "run", nsapp_run, 0);
319
361
  rb_define_method(rb_mNSAppClass, "exit", nsapp_exit, 0);
320
362
 
363
+ /* Menu */
364
+ rb_mNSMenuClass = rb_define_class_under(rb_mCocoawebview, "NSMenu", rb_cObject);
365
+ rb_define_method(rb_mNSMenuClass, "initialize", nsmenu_initialize, 0);
366
+ rb_define_method(rb_mNSMenuClass, "new_menu", nsmenu_new_menu, 0);
367
+ rb_define_method(rb_mNSMenuClass, "new_menu_item", nsmenu_new_menu_item, 0);
368
+ rb_define_method(rb_mNSMenuClass, "create_menu_item", nsmenu_create_menu_item, 3);
369
+ rb_define_method(rb_mNSMenuClass, "add_item_to_menu", nsmenu_add_item_to_menu, 2);
370
+ rb_define_method(rb_mNSMenuClass, "set_submenu_to_menu", nsmenu_set_submenu_to_menu, 2);
371
+ rb_define_method(rb_mNSMenuClass, "show", nsmenu_show, 0);
372
+
373
+
321
374
  /* CocoaWebview */
322
375
  rb_mCocoaWebviewClass = rb_define_class_under(rb_mCocoawebview, "CocoaWebview", rb_cObject);
323
376
  rb_define_method(rb_mCocoaWebviewClass, "initialize", webview_initialize, 5);
@@ -355,6 +408,86 @@ VALUE nsapp_exit(VALUE self) {
355
408
  [[NSApplication sharedApplication] terminate:nil];
356
409
  }
357
410
 
411
+ VALUE nsmenu_initialize(VALUE self) {
412
+ rb_iv_set(self, "@var", rb_hash_new());
413
+ rb_iv_set(self, "@bindings", rb_hash_new());
414
+
415
+ Menu *menu = [[Menu alloc] init];
416
+ menu.mainMenu = [NSMenu new];
417
+
418
+ AppDelegate *delegate = application.delegate;
419
+ [delegate setMenu:self];
420
+
421
+ // Wrap the Objective-C pointer into a Ruby object
422
+ VALUE wrapper = TypedData_Wrap_Struct(rb_cObject, &menu_obj_type, menu);
423
+
424
+ // Store the wrapper in an instance variable
425
+ rb_ivar_set(self, rb_intern("@menu"), wrapper);
426
+
427
+ VALUE wrapper2 = TypedData_Wrap_Struct(rb_cObject, &menu_obj_type, menu.mainMenu);
428
+ rb_ivar_set(self, rb_intern("@menu_bar"), wrapper2);
429
+
430
+ return self;
431
+ }
432
+
433
+ VALUE nsmenu_new_menu_item(VALUE self) {
434
+ NSMenuItem *menuItem = [NSMenuItem new];
435
+ // Wrap the Objective-C pointer into a Ruby object
436
+ VALUE wrapper = TypedData_Wrap_Struct(rb_cObject, &menu_obj_type, menuItem);
437
+ return wrapper;
438
+ }
439
+
440
+ VALUE nsmenu_create_menu_item(VALUE self, VALUE title, VALUE tag, VALUE key) {
441
+ int c_tag = NUM2INT(tag);
442
+ const char *title_c = StringValueCStr(title);
443
+ NSString *title_ns = [[NSString alloc] initWithCString:title_c encoding:NSUTF8StringEncoding];
444
+
445
+ const char *key_c = StringValueCStr(key);
446
+ NSString *key_ns = [[NSString alloc] initWithCString:key_c encoding:NSUTF8StringEncoding];
447
+
448
+ NSMenuItem *menuItem = [[NSMenuItem alloc] initWithTitle:title_ns
449
+ action:@selector(handleMenuAction:)
450
+ keyEquivalent:key_ns];
451
+
452
+ [menuItem setTag:c_tag];
453
+
454
+ VALUE wrapper = TypedData_Wrap_Struct(rb_cObject, &menu_obj_type, menuItem);
455
+ return wrapper;
456
+ }
457
+
458
+ VALUE nsmenu_new_menu(VALUE self) {
459
+ NSMenu *menu = [NSMenu new];
460
+ // Wrap the Objective-C pointer into a Ruby object
461
+ VALUE wrapper = TypedData_Wrap_Struct(rb_cObject, &menu_obj_type, menu);
462
+ return wrapper;
463
+ }
464
+
465
+ VALUE nsmenu_add_item_to_menu(VALUE self, VALUE item, VALUE menu) {
466
+ NSMenuItem *item_ns;
467
+ NSMenu *menu_ns;
468
+ TypedData_Get_Struct(item, NSMenuItem, &menu_obj_type, item_ns);
469
+ TypedData_Get_Struct(menu, NSMenu, &menu_obj_type, menu_ns);
470
+ [menu_ns addItem:item_ns];
471
+ }
472
+
473
+ VALUE nsmenu_set_submenu_to_menu(VALUE self, VALUE submenu, VALUE menu) {
474
+ NSMenu *submenu_ns;
475
+ NSMenu *menu_ns;
476
+ TypedData_Get_Struct(submenu, NSMenu, &menu_obj_type, submenu_ns);
477
+ TypedData_Get_Struct(menu, NSMenu, &menu_obj_type, menu_ns);
478
+ [menu_ns setSubmenu:submenu_ns];
479
+ }
480
+
481
+ VALUE nsmenu_show(VALUE self) {
482
+ NSApplication *app = [NSApplication sharedApplication];
483
+ [app setActivationPolicy:NSApplicationActivationPolicyRegular];
484
+
485
+ VALUE wrapper = rb_ivar_get(self, rb_intern("@menu"));
486
+ Menu *menu;
487
+ TypedData_Get_Struct(wrapper, Menu, &menu_obj_type, menu);
488
+ [app setMainMenu:menu.mainMenu];
489
+ }
490
+
358
491
  VALUE webview_initialize(VALUE self, VALUE debug, VALUE style, VALUE move_title_buttons, VALUE delta_y, VALUE hide_title_bar) {
359
492
  rb_iv_set(self, "@var", rb_hash_new());
360
493
  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.9.0"
5
5
  end
data/lib/cocoawebview.rb CHANGED
@@ -14,6 +14,26 @@ module CocoaWebview
14
14
 
15
15
  class Error < StandardError; end
16
16
  # Your code goes here...
17
+
18
+ class NSMenu
19
+ def create_menu_item_with(title, tag, key, &block)
20
+ @bindings[tag] = block
21
+ create_menu_item(title, tag, key)
22
+ end
23
+
24
+ def main_menu
25
+ @menu
26
+ end
27
+
28
+ def main_menu_bar
29
+ @menu_bar
30
+ end
31
+
32
+ def handle_menu_action(tag)
33
+ callback = @bindings[tag]
34
+ callback.call
35
+ end
36
+ end
17
37
 
18
38
  class NSApp
19
39
  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.9.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-07 00:00:00.000000000 Z
11
11
  dependencies: []
12
12
  description: Webview ruby binding for macOS
13
13
  email: