@kmamal/sdl 0.10.6 → 0.10.9

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.6",
2
+ "version": "0.10.9",
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,12 @@ const handleEvent = (event) => {
74
74
  delete event.family
75
75
 
76
76
  switch (family) {
77
+ case 'quit': {
78
+ for (const window of Globals.windows.all.values()) {
79
+ window.destroyGently()
80
+ }
81
+ } break
82
+
77
83
  // TODO: this also needs a reconcile function
78
84
  case 'display': {
79
85
  const { displayIndex } = event
@@ -153,13 +159,9 @@ const handleEvent = (event) => {
153
159
  } break
154
160
 
155
161
  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
162
+ window.destroyGently()
163
+ return
164
+ }
163
165
 
164
166
  default: return
165
167
  }
@@ -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::QUIT;
12
13
  std::string events::families::DISPLAY;
13
14
  std::string events::families::WINDOW;
14
15
  std::string events::families::DROP;
@@ -72,6 +73,11 @@ events::dispatchEvent(const SDL_Event &event)
72
73
  Napi::Object packed = Napi::Object::New(env);
73
74
 
74
75
  switch (event.type) {
76
+ case SDL_QUIT: {
77
+ packed.Set("family", events::families::QUIT);
78
+ break;
79
+ }
80
+
75
81
  case SDL_DISPLAYEVENT: {
76
82
  packed.Set("family", events::families::DISPLAY);
77
83
  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 QUIT;
11
12
  extern std::string DISPLAY;
12
13
  extern std::string WINDOW;
13
14
  extern std::string DROP;
@@ -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::QUIT = "quit";
44
44
  events::families::DISPLAY = "display";
45
45
  events::families::WINDOW = "window";
46
46
  events::families::DROP = "drop";
@@ -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
@@ -400,7 +400,7 @@ export namespace Sdl {
400
400
  on (event: 'dropText', listener: (event: Events.Window.DropText) => void): this
401
401
  on (event: 'dropFile', listener: (event: Events.Window.DropFile) => void): this
402
402
  on (event: 'dropComplete', listener: (event: Events.Window.DropComplete) => void): this
403
- on (event: '*', listener: (event: Events.Window.Any) => void): this
403
+ on (event: '*', listener: (type: string, event: Events.Window.Any) => void): this
404
404
 
405
405
  readonly id: number
406
406
 
@@ -476,7 +476,7 @@ export namespace Sdl {
476
476
  on (event: 'displayAdd', listener: (event: Events.Display.Add) => void): this
477
477
  on (event: 'displayRemove', listener: (event: Events.Display.Remove) => void): this
478
478
  on (event: 'displayOrient', listener: (event: Events.Display.Orient) => void): this
479
- on (event: '*', listener: (event: Events.Display.Any) => void): this
479
+ on (event: '*', listener: (type: string, event: Events.Display.Any) => void): this
480
480
 
481
481
  readonly displays: Display[]
482
482
 
@@ -873,7 +873,7 @@ export namespace Sdl {
873
873
  on (event: 'buttonUp', listener: (event: Events.Joystick.ButtonUp) => void): this
874
874
  on (event: 'hatMotion', listener: (event: Events.Joystick.HatMotion) => void): this
875
875
  on (event: 'close', listener: (event: Events.Joystick.Close) => void): this
876
- on (event: '*', listener: (event: Events.Joystick.Any) => void): this
876
+ on (event: '*', listener: (type: string, event: Events.Joystick.Any) => void): this
877
877
 
878
878
  readonly device: Device
879
879
  readonly firmwareVersion: number
@@ -907,7 +907,7 @@ export namespace Sdl {
907
907
  interface Module {
908
908
  on (event: 'deviceAdd', listener: (event: Events.JoystickDevice.Add) => void): this
909
909
  on (event: 'deviceRemove', listener: (event: Events.JoystickDevice.Remove) => void): this
910
- on (event: '*', listener: (event: Events.JoystickDevice.Any) => void): this
910
+ on (event: '*', listener: (type: string, event: Events.JoystickDevice.Any) => void): this
911
911
 
912
912
  readonly devices: Device[]
913
913
 
@@ -965,7 +965,7 @@ export namespace Sdl {
965
965
  on (event: 'buttonUp', listener: (event: Events.Controller.ButtonUp) => void): this
966
966
  on (event: 'remap', listener: (event: Events.Controller.Remap) => void): this
967
967
  on (event: 'close', listener: (event: Events.Controller.Close) => void): this
968
- on (event: '*', listener: (event: Events.Controller.Any) => void): this
968
+ on (event: '*', listener: (type: string, event: Events.Controller.Any) => void): this
969
969
 
970
970
  readonly device: Device
971
971
  readonly firmwareVersion: number
@@ -1025,7 +1025,7 @@ export namespace Sdl {
1025
1025
  interface Module {
1026
1026
  on (event: 'deviceAdd', listener: (event: Events.ControllerDevice.Add) => void): this
1027
1027
  on (event: 'deviceRemove', listener: (event: Events.ControllerDevice.Remove) => void): this
1028
- on (event: '*', listener: (event: Events.ControllerDevice.Any) => void): this
1028
+ on (event: '*', listener: (type: string, event: Events.ControllerDevice.Any) => void): this
1029
1029
 
1030
1030
  addMappings (mappings: string[]): void
1031
1031
 
@@ -1064,7 +1064,7 @@ export namespace Sdl {
1064
1064
  export class SensorInstance {
1065
1065
  on (event: 'update', listener: (event: Events.Sensor.Update) => void): this
1066
1066
  on (event: 'close', listener: (event: Events.Sensor.Close) => void): this
1067
- on (event: '*', listener: (event: Events.Sensor.Any) => void): this
1067
+ on (event: '*', listener: (type: string, event: Events.Sensor.Any) => void): this
1068
1068
 
1069
1069
  readonly device: Device
1070
1070
 
@@ -1121,7 +1121,7 @@ export namespace Sdl {
1121
1121
 
1122
1122
  export class AudioInstance {
1123
1123
  on (event: 'close', listener: (event: Events.Audio.Close) => void): this
1124
- on (event: '*', listener: (event: Events.Audio.Any) => void): this
1124
+ on (event: '*', listener: (type: string, event: Events.Audio.Any) => void): this
1125
1125
 
1126
1126
  readonly id: number
1127
1127
  readonly device: Device
@@ -1167,7 +1167,7 @@ export namespace Sdl {
1167
1167
  interface Module {
1168
1168
  on (event: 'deviceAdd', listener: (event: Events.AudioDevice.Add) => void): this
1169
1169
  on (event: 'deviceRemove', listener: (event: Events.AudioDevice.Remove) => void): this
1170
- on (event: '*', listener: (event: Events.AudioDevice.Any) => void): this
1170
+ on (event: '*', listener: (type: string, event: Events.AudioDevice.Any) => void): this
1171
1171
 
1172
1172
  readonly devices: Device[]
1173
1173
 
@@ -1187,7 +1187,7 @@ export namespace Sdl {
1187
1187
 
1188
1188
  interface Module {
1189
1189
  on (event: 'update', listener: (event: Events.Clipboard.Update) => void): this
1190
- on (event: '*', listener: (event: Events.Clipboard.Any) => void): this
1190
+ on (event: '*', listener: (type: string, event: Events.Clipboard.Any) => void): this
1191
1191
 
1192
1192
  readonly text: string
1193
1193
  setText (text: string): void