@kmamal/sdl 0.10.7 → 0.10.10

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.
package/README.md CHANGED
@@ -208,6 +208,7 @@ Check the [`examples/`](https://github.com/kmamal/node-sdl/tree/master/examples#
208
208
  * [window.stopFlashing()](#windowstopflashing)
209
209
  * [window.destroyed](#windowdestroyed)
210
210
  * [window.destroy()](#windowdestroy)
211
+ * [window.destroyGently()](#windowdestroygently)
211
212
  * [sdl.keyboard](#sdlkeyboard)
212
213
  * [Virtual keys](#virtual-keys)
213
214
  * [Enum: SCANCODE](#enum-scancode)
@@ -1058,6 +1059,11 @@ A destroyed window object should not be used any further.
1058
1059
 
1059
1060
  Destroys the window.
1060
1061
 
1062
+ ### window.destroyGently()
1063
+
1064
+ Asks before destroying the window.
1065
+ The difference between this function and [destroy()](#windowdestroy) is that this function will first make the window emit the [`'beforeClose'`](#event-beforeclose) event, giving you a chance to prevent the window from being destroyed.
1066
+
1061
1067
 
1062
1068
  ## sdl.keyboard
1063
1069
 
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.10.7",
2
+ "version": "0.10.10",
3
3
  "name": "@kmamal/sdl",
4
4
  "description": "SDL bindings for Node.js",
5
5
  "keywords": [
@@ -60,7 +60,7 @@
60
60
  "clean": "cd scripts && node clean.mjs"
61
61
  },
62
62
  "dependencies": {
63
- "tar": "^7.1.0"
63
+ "tar": "^7.4.3"
64
64
  },
65
65
  "sdl": {
66
66
  "owner": "kmamal",
@@ -69,9 +69,9 @@
69
69
  },
70
70
  "devDependencies": {
71
71
  "@kmamal/evdev": "^0.0.1",
72
- "@kmamal/testing": "^0.0.30",
73
- "@types/node": "^20.12.8",
74
- "node-addon-api": "^8.0.0",
75
- "node-gyp": "^10.1.0"
72
+ "@kmamal/testing": "^0.0.31",
73
+ "@types/node": "^22.10.7",
74
+ "node-addon-api": "^8.3.0",
75
+ "node-gyp": "^11.0.0"
76
76
  }
77
77
  }
@@ -74,6 +74,14 @@ const handleEvent = (event) => {
74
74
  delete event.family
75
75
 
76
76
  switch (family) {
77
+ case 'app': {
78
+ if (event.type !== 'quit') { return }
79
+
80
+ for (const window of Globals.windows.all.values()) {
81
+ window.destroyGently()
82
+ }
83
+ } break
84
+
77
85
  // TODO: this also needs a reconcile function
78
86
  case 'display': {
79
87
  const { displayIndex } = event
@@ -153,13 +161,9 @@ const handleEvent = (event) => {
153
161
  } break
154
162
 
155
163
  case 'close': {
156
- let shouldPrevent = false
157
- const prevent = () => { shouldPrevent = true }
158
- const type2 = 'beforeClose'
159
- const event2 = { type: type2, prevent }
160
- window.emit(type2, event2)
161
- if (shouldPrevent) { return }
162
- } break
164
+ window.destroyGently()
165
+ return
166
+ }
163
167
 
164
168
  default: return
165
169
  }
@@ -395,6 +395,19 @@ class Window extends EventsViaPoll {
395
395
 
396
396
  this.emit('close', { type: 'close' })
397
397
  }
398
+
399
+ destroyGently () {
400
+ if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
401
+
402
+ let shouldPrevent = false
403
+ this.emit('beforeClose', {
404
+ type: 'beforeClose',
405
+ prevent: () => { shouldPrevent = true },
406
+ })
407
+ if (shouldPrevent) { return }
408
+
409
+ this.destroy()
410
+ }
398
411
  }
399
412
 
400
413
  module.exports = { Window }
@@ -9,6 +9,7 @@
9
9
  #include <sstream>
10
10
 
11
11
 
12
+ std::string events::families::APP;
12
13
  std::string events::families::DISPLAY;
13
14
  std::string events::families::WINDOW;
14
15
  std::string events::families::DROP;
@@ -22,6 +23,7 @@ std::string events::families::SENSOR;
22
23
  std::string events::families::AUDIO_DEVICE;
23
24
  std::string events::families::CLIPBOARD;
24
25
 
26
+ std::string events::types::QUIT;
25
27
  std::string events::types::DISPLAY_ADD;
26
28
  std::string events::types::DISPLAY_REMOVE;
27
29
  std::string events::types::DISPLAY_ORIENT;
@@ -72,6 +74,12 @@ events::dispatchEvent(const SDL_Event &event)
72
74
  Napi::Object packed = Napi::Object::New(env);
73
75
 
74
76
  switch (event.type) {
77
+ case SDL_QUIT: {
78
+ packed.Set("family", events::families::APP);
79
+ packed.Set("type", events::types::QUIT);
80
+ break;
81
+ }
82
+
75
83
  case SDL_DISPLAYEVENT: {
76
84
  packed.Set("family", events::families::DISPLAY);
77
85
  packed.Set("displayIndex", Napi::Number::New(env, event.display.display));
@@ -8,6 +8,7 @@
8
8
  namespace events {
9
9
 
10
10
  namespace families {
11
+ extern std::string APP;
11
12
  extern std::string DISPLAY;
12
13
  extern std::string WINDOW;
13
14
  extern std::string DROP;
@@ -23,6 +24,7 @@ namespace events {
23
24
  };
24
25
 
25
26
  namespace types {
27
+ extern std::string QUIT;
26
28
  extern std::string DISPLAY_ADD;
27
29
  extern std::string DISPLAY_REMOVE;
28
30
  extern std::string DISPLAY_ORIENT;
@@ -40,7 +40,7 @@ global::initialize(const Napi::CallbackInfo &info)
40
40
  {
41
41
  Napi::Env env = info.Env();
42
42
 
43
-
43
+ events::families::APP = "app";
44
44
  events::families::DISPLAY = "display";
45
45
  events::families::WINDOW = "window";
46
46
  events::families::DROP = "drop";
@@ -54,6 +54,7 @@ global::initialize(const Napi::CallbackInfo &info)
54
54
  events::families::AUDIO_DEVICE = "audioDevice";
55
55
  events::families::CLIPBOARD = "clipboard";
56
56
 
57
+ events::types::QUIT = "quit";
57
58
  events::types::DISPLAY_ADD = "displayAdd";
58
59
  events::types::DISPLAY_REMOVE = "displayRemove";
59
60
  events::types::DISPLAY_ORIENT = "displayOrient";
@@ -62,7 +62,7 @@ export namespace Events {
62
62
  export interface MouseButtonUp extends MouseButtonEvent { readonly type: 'mouseButtonUp' }
63
63
 
64
64
  export interface MouseWheel extends MouseEvent {
65
- readonly type: 'flipped'
65
+ readonly type: 'mouseWheel'
66
66
  readonly dx: number
67
67
  readonly dy: number
68
68
  readonly flipped: boolean